Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/browser/deviceAccess.ts
3292 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
// https://wicg.github.io/webusb/
7
8
export interface UsbDeviceData {
9
readonly deviceClass: number;
10
readonly deviceProtocol: number;
11
readonly deviceSubclass: number;
12
readonly deviceVersionMajor: number;
13
readonly deviceVersionMinor: number;
14
readonly deviceVersionSubminor: number;
15
readonly manufacturerName?: string;
16
readonly productId: number;
17
readonly productName?: string;
18
readonly serialNumber?: string;
19
readonly usbVersionMajor: number;
20
readonly usbVersionMinor: number;
21
readonly usbVersionSubminor: number;
22
readonly vendorId: number;
23
}
24
25
export async function requestUsbDevice(options?: { filters?: unknown[] }): Promise<UsbDeviceData | undefined> {
26
const usb = (navigator as any).usb;
27
if (!usb) {
28
return undefined;
29
}
30
31
const device = await usb.requestDevice({ filters: options?.filters ?? [] });
32
if (!device) {
33
return undefined;
34
}
35
36
return {
37
deviceClass: device.deviceClass,
38
deviceProtocol: device.deviceProtocol,
39
deviceSubclass: device.deviceSubclass,
40
deviceVersionMajor: device.deviceVersionMajor,
41
deviceVersionMinor: device.deviceVersionMinor,
42
deviceVersionSubminor: device.deviceVersionSubminor,
43
manufacturerName: device.manufacturerName,
44
productId: device.productId,
45
productName: device.productName,
46
serialNumber: device.serialNumber,
47
usbVersionMajor: device.usbVersionMajor,
48
usbVersionMinor: device.usbVersionMinor,
49
usbVersionSubminor: device.usbVersionSubminor,
50
vendorId: device.vendorId,
51
};
52
}
53
54
// https://wicg.github.io/serial/
55
56
export interface SerialPortData {
57
readonly usbVendorId?: number | undefined;
58
readonly usbProductId?: number | undefined;
59
}
60
61
export async function requestSerialPort(options?: { filters?: unknown[] }): Promise<SerialPortData | undefined> {
62
const serial = (navigator as any).serial;
63
if (!serial) {
64
return undefined;
65
}
66
67
const port = await serial.requestPort({ filters: options?.filters ?? [] });
68
if (!port) {
69
return undefined;
70
}
71
72
const info = port.getInfo();
73
return {
74
usbVendorId: info.usbVendorId,
75
usbProductId: info.usbProductId
76
};
77
}
78
79
// https://wicg.github.io/webhid/
80
81
export interface HidDeviceData {
82
readonly opened: boolean;
83
readonly vendorId: number;
84
readonly productId: number;
85
readonly productName: string;
86
readonly collections: [];
87
}
88
89
export async function requestHidDevice(options?: { filters?: unknown[] }): Promise<HidDeviceData | undefined> {
90
const hid = (navigator as any).hid;
91
if (!hid) {
92
return undefined;
93
}
94
95
const devices = await hid.requestDevice({ filters: options?.filters ?? [] });
96
if (!devices.length) {
97
return undefined;
98
}
99
100
const device = devices[0];
101
return {
102
opened: device.opened,
103
vendorId: device.vendorId,
104
productId: device.productId,
105
productName: device.productName,
106
collections: device.collections
107
};
108
}
109
110