Skip to content

Commit 84cca9d

Browse files
committed
firmware/sagas: finish EV3 message ID TODO
Implement unique message ID for EV3 firmware messages.
1 parent 7ef6c02 commit 84cca9d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/firmware/sagas.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { compile, didCompile, didFailToCompile } from '../mpy/actions';
6666
import { RootState } from '../reducers';
6767
import { LegoUsbProductId, legoUsbVendorId } from '../usb';
6868
import { assert, defined, ensureError, hex, maybe } from '../utils';
69+
import { createCountFunc } from '../utils/iter';
6970
import { crc32, fmod, sumComplement32 } from '../utils/math';
7071
import {
7172
EV3OfficialFirmwareVersion,
@@ -1013,6 +1014,8 @@ function* handleRestoreOfficialDfu(
10131014
}
10141015
}
10151016

1017+
const getNextEV3MessageId = createCountFunc();
1018+
10161019
function* handleFlashEV3(action: ReturnType<typeof firmwareFlashEV3>): Generator {
10171020
if (navigator.hid === undefined) {
10181021
yield* put(alertsShowAlert('firmware', 'noWebHid'));
@@ -1125,8 +1128,10 @@ function* handleFlashEV3(action: ReturnType<typeof firmwareFlashEV3>): Generator
11251128
const dataBuffer = new Uint8Array((payload?.byteLength ?? 0) + 6);
11261129
const data = new DataView(dataBuffer.buffer);
11271130

1131+
const messageId = getNextEV3MessageId() & 0xffff;
1132+
11281133
data.setInt16(0, (payload?.byteLength ?? 0) + 4, true);
1129-
data.setInt16(2, 0, true); // TODO: reply number
1134+
data.setInt16(2, messageId, true);
11301135
data.setUint8(4, 0x01); // system command w/ reply
11311136
data.setUint8(5, command);
11321137
if (payload) {
@@ -1152,6 +1157,15 @@ function* handleFlashEV3(action: ReturnType<typeof firmwareFlashEV3>): Generator
11521157

11531158
defined(reply);
11541159

1160+
if (reply.replyNumber !== messageId) {
1161+
return [
1162+
undefined,
1163+
new Error(
1164+
`EV3 reply message ID mismatch: expected ${messageId}, got ${reply.replyNumber}`,
1165+
),
1166+
];
1167+
}
1168+
11551169
if (reply.replyCommand !== command) {
11561170
return [
11571171
undefined,

0 commit comments

Comments
 (0)