@aepyornis/fastboot.ts 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/client.ts +19 -16
- package/src/device.ts +33 -21
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -60,11 +60,14 @@ export class FastbootClient {
|
|
|
60
60
|
async reboot() {
|
|
61
61
|
this.logger.log("rebooting")
|
|
62
62
|
await this.fd.exec("reboot")
|
|
63
|
+
await new Promise((resolve) => setTimeout(resolve, 5000))
|
|
64
|
+
await this.fd.waitForReconnect()
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
async rebootBootloader() {
|
|
66
68
|
this.logger.log("rebooting into bootloader")
|
|
67
|
-
this.fd.exec("reboot-bootloader")
|
|
69
|
+
await this.fd.exec("reboot-bootloader")
|
|
70
|
+
await new Promise((resolve) => setTimeout(resolve, 5000))
|
|
68
71
|
await this.fd.waitForReconnect()
|
|
69
72
|
}
|
|
70
73
|
|
|
@@ -72,16 +75,12 @@ export class FastbootClient {
|
|
|
72
75
|
const serialNumber = this.fd.serialNumber
|
|
73
76
|
this.logger.log("rebooting into fastboot")
|
|
74
77
|
await this.fd.exec("reboot-fastboot")
|
|
78
|
+
await new Promise((resolve) => setTimeout(resolve, 5000))
|
|
75
79
|
// Devices may not automatically reconnect after entering fastbootd, so
|
|
76
80
|
// after 30 seconds if the device has not been connected we prompt again.
|
|
77
81
|
setTimeout(async () => {
|
|
78
|
-
|
|
79
|
-
if (!devices.some(device => device.serialNumber === serialNumber)) {
|
|
80
|
-
this.logger.log("Device did not reconnect, requesting new device")
|
|
81
|
-
await this.requestUsbDevice()
|
|
82
|
-
}
|
|
82
|
+
await FastbootClient.findOrRequestDevice(serialNumber)
|
|
83
83
|
}, 30000)
|
|
84
|
-
|
|
85
84
|
await this.fd.waitForReconnect()
|
|
86
85
|
}
|
|
87
86
|
|
|
@@ -165,14 +164,8 @@ export class FastbootClient {
|
|
|
165
164
|
}
|
|
166
165
|
this.logger.log(`ACTION NEEDED: flashing ${command}`)
|
|
167
166
|
await this.fd.exec(`flashing ${command}`)
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// waitForReconnect()
|
|
171
|
-
if (MotorolaProducts.includes(await this.getVarCache("product")) && (await navigator.usb.getDevices()).length > 0) {
|
|
172
|
-
await this.fd.reconnect()
|
|
173
|
-
} else {
|
|
174
|
-
await this.fd.waitForReconnect()
|
|
175
|
-
}
|
|
167
|
+
await new Promise((resolve) => setTimeout(resolve, 5000))
|
|
168
|
+
await this.fd.waitForReconnect()
|
|
176
169
|
}
|
|
177
170
|
|
|
178
171
|
// run text, typically the contents of fastboot-info.txt
|
|
@@ -369,9 +362,19 @@ export class FastbootClient {
|
|
|
369
362
|
return new FastbootClient(await this.requestUsbDevice(), window.console)
|
|
370
363
|
}
|
|
371
364
|
|
|
372
|
-
static requestUsbDevice(): Promise<USBDevice> {
|
|
365
|
+
static async requestUsbDevice(): Promise<USBDevice> {
|
|
373
366
|
return window.navigator.usb.requestDevice({
|
|
374
367
|
filters: [FastbootUSBDeviceFilter],
|
|
375
368
|
})
|
|
376
369
|
}
|
|
370
|
+
|
|
371
|
+
static async findOrRequestDevice(serialNumber: string): Promise<Void> {
|
|
372
|
+
for (const device of (await navigator.usb.getDevices())) {
|
|
373
|
+
if (device.serialNumber === serialNumber) {
|
|
374
|
+
return device
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return await FastbootClient.requestUsbDevice()
|
|
378
|
+
}
|
|
379
|
+
|
|
377
380
|
}
|
package/src/device.ts
CHANGED
|
@@ -34,11 +34,11 @@ export class FastbootDevice {
|
|
|
34
34
|
serialNumber: string;
|
|
35
35
|
in: USBEndpoint
|
|
36
36
|
out: USBEndpoint
|
|
37
|
-
session: FastbootSession
|
|
37
|
+
session: FastbootSession | null
|
|
38
38
|
sessions: FastbootSession[]
|
|
39
39
|
logger: Logger
|
|
40
40
|
|
|
41
|
-
constructor(device, logger = window.console) {
|
|
41
|
+
constructor(device: USBDevice, logger: Logger = window.console) {
|
|
42
42
|
this.device = device
|
|
43
43
|
this.serialNumber = this.device.serialNumber
|
|
44
44
|
this.session = null
|
|
@@ -97,24 +97,33 @@ export class FastbootDevice {
|
|
|
97
97
|
throw new Error("Could not find device in navigator.usb.getDevices()")
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
waitForReconnect(): Promise<boolean> {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
100
|
+
async waitForReconnect(): Promise<boolean> {
|
|
101
|
+
const devices = await navigator.usb.getDevices()
|
|
102
|
+
|
|
103
|
+
if (devices.some(device => device.serialNumber === this.serialNumber)) {
|
|
104
|
+
this.logger.log(`waitForReconnect: Found device ${this.serialNumber}`)
|
|
105
|
+
await this.reconnect()
|
|
106
|
+
return Promise.resolve(true)
|
|
107
|
+
} else {
|
|
108
|
+
this.logger.log(`waitForReconnect: Adding navigator.usb event listener`)
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
navigator.usb.addEventListener(
|
|
111
|
+
"connect",
|
|
112
|
+
async (event) => {
|
|
113
|
+
this.logger.log(
|
|
114
|
+
`waitForReconnect: Device connected ${event.device.productName}`,
|
|
115
|
+
)
|
|
116
|
+
try {
|
|
117
|
+
await this.reconnect()
|
|
118
|
+
resolve(true)
|
|
119
|
+
} catch (e) {
|
|
120
|
+
reject(e)
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
{ once: true },
|
|
124
|
+
)
|
|
125
|
+
})
|
|
126
|
+
}
|
|
118
127
|
}
|
|
119
128
|
|
|
120
129
|
async getPacket(): ResponsePacket {
|
|
@@ -193,12 +202,15 @@ export class FastbootDevice {
|
|
|
193
202
|
return this.sendCommand(command)
|
|
194
203
|
}
|
|
195
204
|
|
|
196
|
-
async getVar(variable:
|
|
205
|
+
async getVar(variable: string): Promise<string> {
|
|
197
206
|
await this.exec(`getvar:${variable}`)
|
|
198
207
|
return this.lastPacket.message
|
|
199
208
|
}
|
|
200
209
|
|
|
201
210
|
get lastPacket() {
|
|
211
|
+
if (!this.session || this.session.packets.length === 0) {
|
|
212
|
+
return null
|
|
213
|
+
}
|
|
202
214
|
return this.session.packets[this.session.packets.length - 1]
|
|
203
215
|
}
|
|
204
216
|
|