-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
138 lines (121 loc) · 4.29 KB
/
test.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
<!DOCTYPE html>
<html>
<head>
<title>WebUSB Example</title>
</head>
<body>
<button id="connectButton">Connect to USB Device</button>
<script>
// Include the USBIO class here
//USB I/O class for WebUSB
//(c) Luxferre 2020-present
class USBIO {
//Possible devFilter fields: classCode, subclassCode, protocolCode, vendorId, productId, serialNumber (not recommended)
constructor(devFilter = {}) {
this.requestDeviceObject = {filters:(devFilter ? [devFilter] : [])}
this.devFilter = devFilter
this.dev = null
this.confNum = 0
this.interfaceNum = 0
this.alt = null
this.inEndpointNum = 0
this.outEndpointNum = 0
this.packetSize = 0
this.PID = 0
this.VID = 0
}
async connect() {
this.dev = await navigator.usb.requestDevice(this.requestDeviceObject)
this.PID = this.dev.vendorId
this.VID = this.dev.productId
await this.dev.open()
for(let conf of this.dev.configurations)
for(let intf of conf.interfaces)
for(let alt of intf.alternates)
if('protocolCode' in this.devFilter) { //look for a particular protocol
if(this.devFilter.classCode == alt.interfaceClass &&
this.devFilter.subclassCode == alt.interfaceSubclass &&
this.devFilter.protocolCode == alt.interfaceProtocol) {
this.confNum = conf.configurationValue
this.interfaceNum = intf.interfaceNumber
this.alt = alt
}
}
else { //just look for a bulk transfer interface
if((alt.interfaceClass === 10 || alt.interfaceClass === 255) && alt.endpoints.length === 2 && alt.endpoints.every(endp=>endp.type==='bulk')) {
this.confNum = conf.configurationValue
this.interfaceNum = intf.interfaceNumber
this.alt = alt
}
}
if(this.alt) {
if(this.alt.endpoints[0].direction === 'in') {
this.inEndpointNum = this.alt.endpoints[0].endpointNumber
this.packetSize = this.alt.endpoints[0].packetSize
this.outEndpointNum = this.alt.endpoints[1].endpointNumber
} else {
this.inEndpointNum = this.alt.endpoints[1].endpointNumber
this.packetSize = this.alt.endpoints[1].packetSize
this.outEndpointNum = this.alt.endpoints[0].endpointNumber
}
}
await this.dev.selectConfiguration(this.confNum)
await this.dev.claimInterface(this.interfaceNum)
await this.dev.selectAlternateInterface(this.interfaceNum, this.alt.alternateSetting)
return this.dev
}
async disconnect() {
await this.dev.clearHalt('in', this.inEndpointNum)
await this.dev.clearHalt('out', this.outEndpointNum)
await this.dev.close()
this.dev = null
}
handleDisconnect(cb) {
navigator.usb.addEventListener('disconnect', e => {
if(e.device.productId === this.PID && e.device.vendorId === this.VID) {
this.dev = null
cb(e.device)
}
})
}
async sendData(buf) {
await this.dev.transferOut(this.outEndpointNum, buf)
}
async receiveData(len) {
let data = await this.dev.transferIn(this.inEndpointNum, len)
return data
}
}
// Replace 'your_vendor_id' and 'your_product_id' with the actual Vendor ID and Product ID of your USB device
const DEVICE_FILTER = {
vendorId: '0E8D',
productId: 2000,
};
async function main() {
try {
const usbIO = new USBIO(DEVICE_FILTER);
const device = await usbIO.connect();
// If you need to do additional setup or checks after connection, you can implement them here.
// Send the "FASTBOOT" command
const command = "FASTBOOT";
const data = new TextEncoder().encode(command);
await usbIO.sendData(data);
// Receive the response
const response = await usbIO.receiveData(8);
const responseString = new TextDecoder().decode(response);
console.log("Response:", responseString);
if (responseString === "READYTOO") {
console.log("Entered fastboot mode");
}
// Close the USB connection when done
await usbIO.disconnect();
} catch (error) {
console.error("Error:", error);
}
}
document.getElementById("connectButton").addEventListener("click", () => {
main();
});
</script>
</body>
</html>