-
Notifications
You must be signed in to change notification settings - Fork 13
/
webhid.js
167 lines (154 loc) · 4.52 KB
/
webhid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
export async function jsSleep(ms) {
await new Promise(resolve => setTimeout(resolve, ms));
}
export function hasWebHID() {
return !!navigator.hid;
}
class MessageQueue {
constructor() {
this.queue = [];
this.resolvers = [];
}
addMessage(msg) {
if (this.resolvers.length > 0) {
const resolveFunc = this.resolvers.shift();
resolveFunc(msg);
} else {
this.queue.push(msg);
}
}
getNextMessage() {
return new Promise((resolve) => {
if (this.queue.length > 0) {
const msg = this.queue.shift();
resolve(msg);
} else {
this.resolvers.push(resolve);
}
});
}
}
export async function getWebHIDDevice(vendorId, productId, onCloseCb) {
let device;
try {
const devices = await navigator.hid.requestDevice({filters: [{vendorId, productId}]});
const d = devices[0];
// Filter out other products that might be in the list presented by the Browser.
if (d.productName.includes('BitBox02')) {
device = d;
}
} catch (err) {
return null;
}
if (!device) {
return null;
}
await device.open();
// This is suboptimal API in WebHID - ideally we want to attach this event only when the above
// device is disconnected. This way will likely break if multiple devices are connected at the
// same time.
navigator.hid.addEventListener('disconnect', event => {
const disconnectedDevice = event.device;
if (disconnectedDevice.vendorId === device.vendorId && disconnectedDevice.productId === device.productId) {
if (onCloseCb) {
onCloseCb();
onCloseCb = undefined;
}
}
});
const msgQueue = new MessageQueue();
const onInputReport = event => {
msgQueue.addMessage(new Uint8Array(event.data.buffer));
};
device.addEventListener('inputreport', onInputReport);
return {
write: bytes => {
if (!device.opened) {
console.error('attempted write to a closed HID connection');
return;
}
device.sendReport(0, bytes);
},
read: async () => {
return await msgQueue.getNextMessage();
},
close: () => {
device.close().then(() => {
device.removeEventListener('inputreport', onInputReport);
// The disconnect event above is not fired when closing the
// device, so we manually invoke the callback.
if (onCloseCb) {
onCloseCb();
onCloseCb = undefined;
}
});
},
valid: () => device.opened,
};
}
async function getDevicePath() {
const attempts = 10;
for (let i = 0; i < attempts; i++){
let response;
let errorMessage;
try {
response = await fetch('http://localhost:8178/api/v1/devices', {
method: 'GET',
headers: {},
})
if (!response.ok && response.status === 403) {
errorMessage = 'Origin not whitelisted.';
throw new Error();
} else if (!response.ok) {
errorMessage = 'Unexpected bridge connection error.';
throw new Error();
}
} catch(err) {
throw new Error(errorMessage ? errorMessage : 'BitBoxBridge not found.');
}
const devices = (await response.json()).devices;
if (devices.length !== 1) {
await jsSleep(100);
continue;
}
const devicePath = devices[0].path;
return devicePath;
}
throw new Error('Expected exactly one BitBox02. If one is connected, it might already have an open connection another app. If so, please close the other app first.');
}
export async function getBridgeDevice(onCloseCb) {
let devicePath = await getDevicePath();
const socket = new WebSocket('ws://127.0.0.1:8178/api/v1/socket/' + devicePath);
const msgQueue = new MessageQueue();
return new Promise((resolve, reject) => {
socket.binaryType = 'arraybuffer';
socket.onmessage = event => { msgQueue.addMessage(new Uint8Array(event.data)); };
socket.onclose = event => {
if (onCloseCb) {
onCloseCb();
onCloseCb = undefined;
}
};
socket.onopen = function (event) {
resolve({
write: bytes => {
if (socket.readyState != WebSocket.OPEN) {
console.error('attempted write to a closed socket');
return;
}
socket.send(bytes);
},
read: async () => {
return await msgQueue.getNextMessage();
},
close: () => socket.close(),
valid: () => {
return socket.readyState == WebSocket.OPEN;
},
});
};
socket.onerror = function(event) {
reject(new Error('Your BitBox02 is busy.'));
};
});
}