forked from mschilder123/webusb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
190 lines (172 loc) · 4.93 KB
/
index.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<html><head><script>
// serial.js
u2f = {};
(function() {
'use strict';
u2f.getPorts = function() {
return navigator.usb.getDevices().then(devices => {
return devices.map(device => new u2f.Port(device));
});
};
u2f.requestPort = function() {
const filters = [
{ 'vendorId': 0x18d1, 'productId': 0x5026, 'classCode': 255 }, // hg (use 2nd interface)
];
return navigator.usb.requestDevice({ 'filters': filters }).then(
device => new u2f.Port(device)
);
};
u2f.log = function(dir, data) {
var u8 = new Uint8Array(data.buffer);
// hexdump
var s = '' + dir + '[' + u8.length + ']:';
for (var i = 0; i < u8.length; ++i) {
s += ('0123456789abcdef'.charAt(u8[i] >> 4));
s += ('0123456789abcdef'.charAt(u8[i] & 15));
}
// to textarea
var log = document.getElementById('log');
log.value = log.value + '' + s + '\r\n';
// and console
console.log(s);
};
u2f.Port = function(device) {
this.device_ = device;
console.log(device);
};
u2f.Port.prototype.connect = function() {
let readLoop = () => {
this.device_.transferIn(this.epIn_, this.inSize_).then(result => {
u2f.log('<', result.data);
this.onReceive(result.data);
readLoop();
}, error => {
this.onReceiveError(error);
});
};
return this.device_.open()
.then(() => {
if (this.device_.configuration === null) {
return this.device_.selectConfiguration(0);
}
})
.then(() => {
// Default to the first interface if there is no vendor one
var ifaces = this.device_.configuration.interfaces;
var vendor = ifaces[0];
for (var iface in ifaces) {
if (ifaces[iface].alternates[0].interfaceClass == 255) {
vendor = ifaces[iface];
break;
}
}
console.log(vendor);
this.ifaceNum_ = vendor.interfaceNumber;
var eps = vendor.alternates[0].endpoints;
this.epIn_ = 1;
this.epOut_ = 1;
this.inSize_ = 2048;
for (var ep in eps) {
if (eps[ep].direction == "in" && eps[ep].type == "bulk") {
this.epIn_ = eps[ep].endpointNumber;
this.inSize_ = eps[ep].packetSize;
}
if (eps[ep].direction == "out" && eps[ep].type == "bulk")
this.epOut_ = eps[ep].endpointNumber;
}
})
.then(() => this.device_.claimInterface(this.ifaceNum_))
.then(() => {
var cmd = new Uint8Array(4+3+8);
cmd[0] = 0xff;
cmd[1] = 0xff;
cmd[2] = 0xff;
cmd[3] = 0xff;
cmd[4] = 0x86; // INIT
cmd[5] = 0; // bcnth
cmd[6] = 8; // bcntl
this.send(cmd);
})
.then(() => {
readLoop();
});
};
u2f.Port.prototype.disconnect = function() {
return this.device_.close();
};
u2f.Port.prototype.send = function(data) {
u2f.log('>', data);
return this.device_.transferOut(this.epOut_, data);
};
})();
// rgb.js
(function() {
'use strict';
document.addEventListener('DOMContentLoaded', event => {
let connectButton = document.querySelector("#connect");
let statusDisplay = document.querySelector('#status');
let infoButton = document.querySelector('#info');
let port;
function connect() {
port.connect().then(() => {
statusDisplay.textContent = '';
connectButton.textContent = 'Disconnect';
port.onReceive = data => {
// do something w/ data.buffer
}
port.onReceiveError = error => {
console.error(error);
}
}, error => {
statusDisplay.textContent = error;
});
}
function onUpdate() {
if (!port) {
return;
}
let u8 = new Uint8Array(7);
u8[0] = 1; // CID != 0
u8[4] = 0x85; // SYSINFO
u8[6] = 0;
u8[7] = 0;
port.send(u8);
}
infoButton.addEventListener('click', onUpdate);
connectButton.addEventListener('click', function() {
if (port) {
port.disconnect();
connectButton.textContent = 'Connect';
statusDisplay.textContent = '';
port = null;
} else {
u2f.requestPort().then(selectedPort => {
port = selectedPort;
connect();
}).catch(error => {
statusDisplay.textContent = error;
});
}
});
u2f.getPorts().then(ports => {
if (ports.length == 0) {
statusDisplay.textContent = 'No device found.';
} else {
statusDisplay.textContent = 'Connecting...';
port = ports[0];
connect();
}
});
});
})();
</script>
</head>
<body>
<p>
<button id="connect">Connect</button>
<button id="info">Info</button>
<span id="status"></span>
</p>
<textarea id="log" readonly cols="120" rows="50"></textarea>
</body>
</html>