@@ -66,6 +66,7 @@ import { compile, didCompile, didFailToCompile } from '../mpy/actions';
6666import { RootState } from '../reducers' ;
6767import { LegoUsbProductId , legoUsbVendorId } from '../usb' ;
6868import { assert , defined , ensureError , hex , maybe } from '../utils' ;
69+ import { createCountFunc } from '../utils/iter' ;
6970import { crc32 , fmod , sumComplement32 } from '../utils/math' ;
7071import {
7172 EV3OfficialFirmwareVersion ,
@@ -1013,6 +1014,8 @@ function* handleRestoreOfficialDfu(
10131014 }
10141015}
10151016
1017+ const getNextEV3MessageId = createCountFunc ( ) ;
1018+
10161019function * 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