Skip to content

Commit

Permalink
fix(transport): usb - update reference to device after every async op…
Browse files Browse the repository at this point in the history
…eration
  • Loading branch information
mroz22 committed Nov 18, 2024
1 parent 377d496 commit ffb4908
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/transport/src/api/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,21 +354,32 @@ export class UsbApi extends AbstractApi {
}

public async closeDevice(path: string) {
const device = this.findDevice(path);
let device = this.findDevice(path);
if (!device) {
return this.error({ error: ERRORS.DEVICE_NOT_FOUND });
}

this.logger?.debug(`usb: closeDevice. device.opened: ${device.opened}`);

if (device.opened) {
try {
const interfaceId = this.debugLink ? DEBUGLINK_INTERFACE_ID : INTERFACE_ID;
this.logger?.debug(`usb: device.releaseInterface: ${interfaceId}`);
if (!this.debugLink) {
if (!this.debugLink) {
try {
// NOTE: `device.reset()` interrupts transfers for all interfaces (debugLink and normal)
await device.reset();
} catch (err) {
this.logger?.error(
`usb: device.reset error ${err}. device: ${this.formatDeviceForLog(device)}`,
);
}
}
}

device = this.findDevice(path);
if (device?.opened) {
try {
const interfaceId = this.debugLink ? DEBUGLINK_INTERFACE_ID : INTERFACE_ID;
this.logger?.debug(`usb: device.releaseInterface: ${interfaceId}`);

await device.releaseInterface(interfaceId);
this.logger?.debug(
`usb: device.releaseInterface done: ${interfaceId}. device: ${this.formatDeviceForLog(device)}`,
Expand All @@ -380,7 +391,8 @@ export class UsbApi extends AbstractApi {
// ignore
}
}
if (device.opened) {
device = this.findDevice(path);
if (device?.opened) {
try {
this.logger?.debug(`usb: device.close`);
await device.close();
Expand Down

0 comments on commit ffb4908

Please sign in to comment.