Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Aug 3, 2023
1 parent 7f63a97 commit e1dec42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ const DfuseCommand = {

const DfuBmRequestType = {
HOST_TO_DEVICE: 0x21,
DEVICE_TO_HOST: 0xA1
DEVICE_TO_HOST: 0xA1,
DEVICE_TO_HOST_STANDARD: 0x80 // Direction: device-to-host; type: standard; recipient: device
};

const DFU_STATUS_SIZE = 6;
Expand Down Expand Up @@ -632,16 +633,16 @@ class Dfu {
async _getStringDescriptor(index) {
// Read the size of the descriptor
let d = await this._dev.transferIn({
bmRequestType: 0x80, // Direction: device-to-host; type: standard; recipient: device
bmRequestType: DfuBmRequestType.DEVICE_TO_HOST_STANDARD,
bRequest: 0x06, // GET_DESCRIPTOR
wValue: (0x03 /* STRING */ << 8) | (index & 0xff),
wIndex: 0x0409, // English (US)
wLength: 1
});
let len = d.readUInt8(0); // bLength
const len = d.readUInt8(0); // bLength
// Read the descriptor
d = await this._dev.transferIn({
bmRequestType: 0x80,
bmRequestType: DfuBmRequestType.DEVICE_TO_HOST_STANDARD,
bRequest: 0x06,
wValue: (0x03 << 8) | (index & 0xff),
wIndex: 0x0409,
Expand All @@ -660,7 +661,7 @@ class Dfu {
async _getConfigDescriptor(index) {
// Read the total size of the descriptors
const d = await this._dev.transferIn({
bmRequestType: 0x80, // Direction: device-to-host; type: standard; recipient: device
bmRequestType: DfuBmRequestType.DEVICE_TO_HOST_STANDARD,
bRequest: 0x06, // GET_DESCRIPTOR
wValue: (0x02 /* CONFIGURATION */ << 8) | (index & 0xff),
wIndex: 0,
Expand All @@ -669,7 +670,7 @@ class Dfu {
const len = d.readUInt16LE(2); // wTotalLength
// Read the descriptors
return await this._dev.transferIn({
bmRequestType: 0x80,
bmRequestType: DfuBmRequestType.DEVICE_TO_HOST_STANDARD,
bRequest: 0x06,
wValue: (0x02 << 8) | (index & 0xff),
wIndex: 0,
Expand Down
15 changes: 12 additions & 3 deletions test/support/fake-usb.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class Protocol {
}

deviceToHostRequest(setup) {
if (setup.bmRequestType !== proto.BmRequestType.DEVICE_TO_HOST) {
if (setup.bmRequestType !== proto.BmRequestType.DEVICE_TO_HOST &&
setup.bmRequestType !== dfu.DfuBmRequestType.DEVICE_TO_HOST_STANDARD) {
throw new ProtocolError(`Unsupported device-to-host request: bmRequestType: ${setup.bmRequestType}`);
}
let data = null;
Expand Down Expand Up @@ -82,6 +83,9 @@ class Protocol {
}
break;
}
case 0x06:
// GET_DESCRIPTOR
break;
default: {
throw new ProtocolError(`Unsupported device-to-host request: bRequest: ${setup.bRequest}`);
}
Expand Down Expand Up @@ -346,7 +350,8 @@ class DfuClass {

deviceToHostRequest(setup) {
// Implements DFU_GETSTATUS only
if (setup.bmRequestType !== dfu.DfuBmRequestType.DEVICE_TO_HOST) {
if (setup.bmRequestType !== dfu.DfuBmRequestType.DEVICE_TO_HOST &&
setup.bmRequestType !== dfu.DfuBmRequestType.DEVICE_TO_HOST_STANDARD) {
throw new UsbError('Unknown bmRequestType');
}

Expand All @@ -358,7 +363,11 @@ class DfuClass {
case dfu.DfuRequestType.DFU_GETSTATUS: {
return this._getStatus(setup);
}

case 0x06: {
// (GET_DESCRIPTOR)
const sample = [9,2,36,0,1,1,4,192,50,9,4,0,0,0,254,1,2,5,9,4,0,1,0,254,1,2,6,9,33,11,255,0,0,16,26,1];
return Buffer.from(sample);
}
default: {
throw new UsbError('Unknown bRequest');
}
Expand Down

0 comments on commit e1dec42

Please sign in to comment.