Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeuz committed Aug 3, 2023
1 parent 1ff6f0b commit c548102
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
31 changes: 20 additions & 11 deletions src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Dfu {
await this._dev.claimInterface(this._interface);
this._claimed = true;
await this._dev.setAltSetting(this._interface, this._alternate);
let desc = await this._getConfigDescriptor(this._interface); // XXX: Changing the interface is not supported
let desc = await this._getConfigDescriptor(0); // Use the default config
desc = this._parseConfigDescriptor(desc);
this._allInterfaces = desc.interfaces;
}
Expand Down Expand Up @@ -204,15 +204,28 @@ class Dfu {
/**
* Set the alternate interface for DFU and initialize memory information.
*
* @param {number} ifaceIdx - The alternate interface index to set.
* @param {number} setting - The alternate interface index to set.
* @return {Promise}
*/
async setAltSetting(setting) {
let iface = null;
let xferSize = null;
for (const i of this._allInterfaces) {
if (i.bInterfaceNumber === this._interface && i.bAlternateSetting === setting) {
iface = i;
break;
if (i.bInterfaceNumber === this._interface) {
if (!iface && i.bAlternateSetting === setting) {
iface = i;
if (i.dfuFunctional) {
xferSize = i.dfuFunctional.wTransferSize;
break;
}
}
// DFU_FUNCTIONAL descriptor may not be available for each interface with the given number
if (!xferSize && i.dfuFunctional) {
xferSize = i.dfuFunctional.wTransferSize;
if (iface) {
break;
}
}
}
}
if (!iface) {
Expand All @@ -223,12 +236,8 @@ class Dfu {
}
const ifaceName = await this._getStringDescriptor(iface.iInterface);
const memInfo = this._parseMemoryDescriptor(ifaceName);
await this._dev.setAltInterface(setting);
if (iface.dfuFunctional && iface.dfuFunctional.wTransferSize) {
this._transferSize = iface.dfuFunctional.wTransferSize;
} else {
this._transferSize = DEFAULT_TRANSFER_SIZE;
}
await this._dev.setAltSetting(this._interface, setting);
this._transferSize = xferSize || DEFAULT_TRANSFER_SIZE;
this._memoryInfo = memInfo;
this._alternate = setting;
}
Expand Down
4 changes: 2 additions & 2 deletions src/usb-device-webusb.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class UsbDevice {
index: setup.wIndex
}, setup.wLength);
if (res.status !== 'ok') {
throw new Error(res.status);
throw new Error(`Status: ${res.status}`);
}
return Buffer.from(res.data.buffer);
} catch (err) {
Expand All @@ -94,7 +94,7 @@ class UsbDevice {
index: setup.wIndex
}, data); // data is optional
if (res.status !== 'ok') {
throw new Error(res.status);
throw new Error(`Status: ${res.status}`);
}
} catch (err) {
throw new UsbError('OUT control transfer failed', { cause: err });
Expand Down

0 comments on commit c548102

Please sign in to comment.