Skip to content

Commit 3c27784

Browse files
committed
fix: simplify USB device configuration checks and firmware error handling
1 parent 77fbc89 commit 3c27784

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

src/firmware/sagas.ts

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,62 +1158,58 @@ function* handleFlashEV3(action: ReturnType<typeof firmwareFlashEV3>): Generator
11581158
}
11591159
});
11601160

1161-
try {
1162-
// Send the command
1163-
const dataBuffer = new Uint8Array((payload?.byteLength ?? 0) + 6);
1164-
const data = new DataView(dataBuffer.buffer);
1165-
1166-
data.setInt16(0, (payload?.byteLength ?? 0) + 4, true);
1167-
data.setInt16(2, 0, true); // TODO: reply number
1168-
data.setUint8(4, 0x01); // system command w/ reply
1169-
data.setUint8(5, command);
1170-
if (payload) {
1171-
dataBuffer.set(payload, 6);
1172-
}
1173-
1174-
const [, sendError] = yield* call(() =>
1175-
maybe(hidDevice.sendReport(0, data)),
1176-
);
1161+
// Send the command
1162+
const dataBuffer = new Uint8Array((payload?.byteLength ?? 0) + 6);
1163+
const data = new DataView(dataBuffer.buffer);
1164+
1165+
data.setInt16(0, (payload?.byteLength ?? 0) + 4, true);
1166+
data.setInt16(2, 0, true); // TODO: reply number
1167+
data.setUint8(4, 0x01); // system command w/ reply
1168+
data.setUint8(5, command);
1169+
if (payload) {
1170+
dataBuffer.set(payload, 6);
1171+
}
11771172

1178-
if (sendError) {
1179-
return [undefined, sendError];
1180-
}
1173+
const [, sendError] = yield* call(() => maybe(hidDevice.sendReport(0, data)));
11811174

1182-
const { reply, timeout } = yield* race({
1183-
reply: take(replyChannel),
1184-
timeout: delay(5000),
1185-
});
1175+
if (sendError) {
1176+
return [undefined, sendError];
1177+
}
11861178

1187-
if (timeout) {
1188-
return [undefined, new Error('Timeout waiting for EV3 reply')];
1189-
}
1179+
const { reply, timeout } = yield* race({
1180+
reply: take(replyChannel),
1181+
timeout: delay(5000),
1182+
});
11901183

1191-
defined(reply);
1184+
if (timeout) {
1185+
return [undefined, new Error('Timeout waiting for EV3 reply')];
1186+
}
11921187

1193-
if (reply.replyCommand !== command) {
1194-
return [
1195-
undefined,
1196-
new Error(
1197-
`EV3 reply command mismatch: expected ${command}, got ${reply.replyCommand}`,
1198-
),
1199-
];
1200-
}
1188+
defined(reply);
12011189

1202-
if (reply.status !== 0) {
1203-
return [
1204-
undefined,
1205-
new Error(
1206-
`EV3 reply status error: ${reply.status} for command ${command}`,
1207-
),
1208-
];
1209-
}
1190+
if (reply.replyCommand !== command) {
1191+
return [
1192+
undefined,
1193+
new Error(
1194+
`EV3 reply command mismatch: expected ${command}, got ${reply.replyCommand}`,
1195+
),
1196+
];
1197+
}
12101198

1211-
return [new DataView(reply.payload), undefined];
1212-
} finally {
1213-
// Always clean up
1214-
yield* cancel(forwardTask);
1215-
yield* call(() => replyChannel.close());
1199+
if (reply.status !== 0) {
1200+
return [
1201+
undefined,
1202+
new Error(
1203+
`EV3 reply status error: ${reply.status} for command ${command}`,
1204+
),
1205+
];
12161206
}
1207+
1208+
// Clean up
1209+
yield* cancel(forwardTask);
1210+
yield* call(() => replyChannel.close());
1211+
1212+
return [new DataView(reply.payload), undefined];
12171213
}
12181214

12191215
const [version, versionError] = yield* sendCommand(0xf6); // get version

src/usb/sagas.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ function* handleUsbConnectPybricks(hotPlugDevice?: USBDevice): Generator {
165165
// configuration and it is already selected and that it contains a Pybricks
166166
// interface.
167167

168-
if (
169-
(usbDevice.configuration === undefined || usbDevice.configuration === null) &&
170-
usbDevice.configurations.length > 0
171-
) {
168+
if (!usbDevice.configuration && usbDevice.configurations.length > 0) {
172169
const [, selectErr] = yield* call(() =>
173170
maybe(
174171
usbDevice.selectConfiguration(
@@ -186,10 +183,7 @@ function* handleUsbConnectPybricks(hotPlugDevice?: USBDevice): Generator {
186183
}
187184
}
188185

189-
assert(
190-
usbDevice.configuration !== undefined && usbDevice.configuration !== null,
191-
'USB device configuration is undefined',
192-
);
186+
assert(!!usbDevice.configuration, 'USB device configuration is undefined');
193187
assert(
194188
usbDevice.configuration.interfaces.length > 0,
195189
'USB device has no interfaces',

0 commit comments

Comments
 (0)