Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/browser/deviceAccess.ts
5222 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
interface UsbDevice {
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
interface USB {
26
requestDevice(options: { filters: unknown[] }): Promise<UsbDevice>;
27
}
28
29
export interface UsbDeviceData {
30
readonly deviceClass: number;
31
readonly deviceProtocol: number;
32
readonly deviceSubclass: number;
33
readonly deviceVersionMajor: number;
34
readonly deviceVersionMinor: number;
35
readonly deviceVersionSubminor: number;
36
readonly manufacturerName?: string;
37
readonly productId: number;
38
readonly productName?: string;
39
readonly serialNumber?: string;
40
readonly usbVersionMajor: number;
41
readonly usbVersionMinor: number;
42
readonly usbVersionSubminor: number;
43
readonly vendorId: number;
44
}
45
46
export async function requestUsbDevice(options?: { filters?: unknown[] }): Promise<UsbDeviceData | undefined> {
47
const usb = (navigator as Navigator & { usb?: USB }).usb;
48
if (!usb) {
49
return undefined;
50
}
51
52
const device = await usb.requestDevice({ filters: options?.filters ?? [] });
53
if (!device) {
54
return undefined;
55
}
56
57
return {
58
deviceClass: device.deviceClass,
59
deviceProtocol: device.deviceProtocol,
60
deviceSubclass: device.deviceSubclass,
61
deviceVersionMajor: device.deviceVersionMajor,
62
deviceVersionMinor: device.deviceVersionMinor,
63
deviceVersionSubminor: device.deviceVersionSubminor,
64
manufacturerName: device.manufacturerName,
65
productId: device.productId,
66
productName: device.productName,
67
serialNumber: device.serialNumber,
68
usbVersionMajor: device.usbVersionMajor,
69
usbVersionMinor: device.usbVersionMinor,
70
usbVersionSubminor: device.usbVersionSubminor,
71
vendorId: device.vendorId,
72
};
73
}
74
75
// https://wicg.github.io/serial/
76
77
interface SerialPortInfo {
78
readonly usbVendorId?: number | undefined;
79
readonly usbProductId?: number | undefined;
80
}
81
82
interface SerialPort {
83
getInfo(): SerialPortInfo;
84
}
85
86
interface Serial {
87
requestPort(options: { filters: unknown[] }): Promise<SerialPort>;
88
}
89
90
export interface SerialPortData {
91
readonly usbVendorId?: number | undefined;
92
readonly usbProductId?: number | undefined;
93
}
94
95
export async function requestSerialPort(options?: { filters?: unknown[] }): Promise<SerialPortData | undefined> {
96
const serial = (navigator as Navigator & { serial?: Serial }).serial;
97
if (!serial) {
98
return undefined;
99
}
100
101
const port = await serial.requestPort({ filters: options?.filters ?? [] });
102
if (!port) {
103
return undefined;
104
}
105
106
const info = port.getInfo();
107
return {
108
usbVendorId: info.usbVendorId,
109
usbProductId: info.usbProductId
110
};
111
}
112
113
// https://wicg.github.io/webhid/
114
115
interface HidDevice {
116
readonly opened: boolean;
117
readonly vendorId: number;
118
readonly productId: number;
119
readonly productName: string;
120
readonly collections: [];
121
}
122
123
interface HID {
124
requestDevice(options: { filters: unknown[] }): Promise<HidDevice[]>;
125
}
126
127
export interface HidDeviceData {
128
readonly opened: boolean;
129
readonly vendorId: number;
130
readonly productId: number;
131
readonly productName: string;
132
readonly collections: [];
133
}
134
135
export async function requestHidDevice(options?: { filters?: unknown[] }): Promise<HidDeviceData | undefined> {
136
const hid = (navigator as Navigator & { hid?: HID }).hid;
137
if (!hid) {
138
return undefined;
139
}
140
141
const devices = await hid.requestDevice({ filters: options?.filters ?? [] });
142
if (!devices.length) {
143
return undefined;
144
}
145
146
const device = devices[0];
147
return {
148
opened: device.opened,
149
vendorId: device.vendorId,
150
productId: device.productId,
151
productName: device.productName,
152
collections: device.collections
153
};
154
}
155
156