@@ -65,6 +65,7 @@ export default class RaftFileHandler {
6565 private _ackedFilePos = 0 ;
6666 private _batchAckReceived = false ;
6767 private _isTxCancelled = false ;
68+ private _fileTxStreamID = 0 ;
6869
6970 // File receive info
7071 private _isRxCancelled = false ;
@@ -78,7 +79,6 @@ export default class RaftFileHandler {
7879 private _fileRxLastAckTime = 0 ;
7980 private _fileRxLastBlockTime = 0 ;
8081 private _fileRxLastAckPos = 0 ;
81- private OVERALL_FILE_TRANSFER_TIMEOUT_MS = 100000 ;
8282 private FILE_RX_ACK_RESEND_TIMEOUT_MS = 1000 ;
8383
8484 // RaftCommsStats
@@ -111,6 +111,7 @@ export default class RaftFileHandler {
111111 progressCallback : ( ( sent : number , total : number , progress : number ) => void ) | undefined ,
112112 ) : Promise < boolean > {
113113 this . _isTxCancelled = false ;
114+ this . _fileTxStreamID = 0 ;
114115
115116 // Send file start message
116117 if ( ! await this . _sendFileStartMsg ( fileName , fileType , fileDest , fileContents ) )
@@ -166,8 +167,8 @@ export default class RaftFileHandler {
166167 RaftLog . warn ( `sendFileStartMsg error ${ err } ` ) ;
167168 return false ;
168169 }
169- if ( fileStartResp . rslt !== 'ok' ) {
170- RaftLog . warn ( `sendFileStartMsg error ${ fileStartResp . rslt } ` ) ;
170+ if ( ! fileStartResp || fileStartResp . rslt !== 'ok' ) {
171+ RaftLog . warn ( `sendFileStartMsg error ${ fileStartResp ? .rslt ?? 'no response' } ` ) ;
171172 return false ;
172173 }
173174
@@ -182,12 +183,17 @@ export default class RaftFileHandler {
182183 } else {
183184 this . _batchAckSize = this . _requestedBatchAckSize ;
184185 }
186+ const streamID = fileStartResp . streamID ;
187+ this . _fileTxStreamID = streamID !== undefined && streamID > 0 && streamID <= 0xff ? streamID : 0 ;
185188 RaftLog . debug (
186189 `_fileSendStartMsg fileBlockSize req ${ this . _requestedFileBlockSize } resp ${ fileStartResp . batchMsgSize } actual ${ this . _fileBlockSize } ` ,
187190 ) ;
188191 RaftLog . debug (
189192 `_fileSendStartMsg batchAckSize req ${ this . _requestedBatchAckSize } resp ${ fileStartResp . batchAckSize } actual ${ this . _batchAckSize } ` ,
190193 ) ;
194+ RaftLog . debug (
195+ `_fileSendStartMsg streamID ${ this . _fileTxStreamID } ` ,
196+ ) ;
191197 return true ;
192198 }
193199
@@ -203,7 +209,8 @@ export default class RaftFileHandler {
203209 ? 'espfwupdate'
204210 : 'fileupload' ;
205211 const fileLen = fileContents . length ;
206- const cmdMsg = `{"cmdName":"ufEnd","reqStr":"${ reqStr } ","fileType":"${ fileDest } ","fileName":"${ fileName } ","fileLen":${ fileLen } }` ;
212+ const streamIDJson = this . _fileTxStreamID !== 0 ? `,"streamID":${ this . _fileTxStreamID } ` : '' ;
213+ const cmdMsg = `{"cmdName":"ufEnd","reqStr":"${ reqStr } ","fileType":"${ fileDest } ","fileName":"${ fileName } ","fileLen":${ fileLen } ${ streamIDJson } }` ;
207214
208215 // Await outstanding promises
209216 try {
@@ -224,12 +231,13 @@ export default class RaftFileHandler {
224231 RaftLog . warn ( `sendFileEndMsg error ${ err } ` ) ;
225232 return false ;
226233 }
227- return fileEndResp . rslt === 'ok' ;
234+ return fileEndResp ? .rslt === 'ok' ;
228235 }
229236
230237 async _sendFileCancelMsg ( ) : Promise < void > {
231238 // File cancel command message
232- const cmdMsg = `{"cmdName":"ufCancel"}` ;
239+ const streamIDJson = this . _fileTxStreamID !== 0 ? `,"streamID":${ this . _fileTxStreamID } ` : '' ;
240+ const cmdMsg = `{"cmdName":"ufCancel"${ streamIDJson } }` ;
233241
234242 // Await outstanding promises
235243 await this . awaitOutstandingMsgPromises ( true ) ;
@@ -368,7 +376,7 @@ export default class RaftFileHandler {
368376 await this . awaitOutstandingMsgPromises ( false ) ;
369377
370378 // Send
371- const promRslt = this . _msgHandler . sendFileBlock ( fileContents . subarray ( blockStart , blockEnd ) , blockStart ) ;
379+ const promRslt = this . _msgHandler . sendFileBlock ( fileContents . subarray ( blockStart , blockEnd ) , blockStart , this . _fileTxStreamID ) ;
372380 if ( ! promRslt ) {
373381 return false ;
374382 }
@@ -438,8 +446,8 @@ export default class RaftFileHandler {
438446 // Establish a bridge
439447 const bridgedDeviceSerialPort = "Serial" + fileSource . slice ( bridgeSerialPrefix . length ) ;
440448 const cmdResp = await this . _msgHandler . createCommsBridge ( bridgedDeviceSerialPort , "fileSource" ) ;
441- if ( cmdResp . rslt != "ok" ) {
442- RaftLog . warn ( `fileReceive - failed to setup bridge ${ cmdResp . rslt } ` ) ;
449+ if ( ! cmdResp || cmdResp . rslt != "ok" ) {
450+ RaftLog . warn ( `fileReceive - failed to setup bridge ${ cmdResp ? .rslt ?? 'no response' } ` ) ;
443451 return new RaftFileDownloadResult ( ) ;
444452 }
445453 bridgeID = cmdResp . bridgeID ;
@@ -498,7 +506,7 @@ export default class RaftFileHandler {
498506 return false ;
499507 }
500508 RaftLog . info ( `_receiveFileStartMsg rslt ${ JSON . stringify ( cmdResp ) } ` ) ;
501- if ( cmdResp . rslt === 'ok' ) {
509+ if ( cmdResp ? .rslt === 'ok' ) {
502510 this . _fileRxBatchMsgSize = cmdResp . batchMsgSize ;
503511 this . _fileRxBatchAckSize = cmdResp . batchAckSize ;
504512 this . _fileRxStreamID = cmdResp . streamID ;
@@ -511,6 +519,10 @@ export default class RaftFileHandler {
511519 this . _fileRxLastBlockTime = Date . now ( ) ;
512520 this . _fileRxActive = true ;
513521 }
522+ if ( ! cmdResp ) {
523+ RaftLog . warn ( `_receiveFileStartMsg failed no response` ) ;
524+ return false ;
525+ }
514526 return cmdResp . rslt === 'ok' ;
515527 }
516528
@@ -581,6 +593,7 @@ export default class RaftFileHandler {
581593 `elapsed ${ now - startTime } ms overallTimeout ${ overallTimeoutMs } ms ` +
582594 `blockGap ${ now - this . _fileRxLastBlockTime } ms blockTimeout ${ blockTimeoutMs } ms` ) ;
583595 this . _fileRxActive = false ;
596+ this . _sendFileRxCancelMsg ( bridgeID ) ;
584597 reject ( new Error ( 'fileReceive failed' ) ) ;
585598 return ;
586599 }
@@ -652,6 +665,12 @@ export default class RaftFileHandler {
652665 }
653666
654667 // Check deferred CRC if start response didn't include one
668+ if ( ! cmdResp ) {
669+ RaftLog . warn ( `_receiveFileEnd failed no response` ) ;
670+ this . _fileRxActive = false ;
671+ return false ;
672+ }
673+
655674 if ( this . _fileRxCrc16 < 0 && cmdResp . crc16 ) {
656675 const expectedCrc = parseInt ( cmdResp . crc16 , 16 ) ;
657676 const actualCrc = RaftMiniHDLC . crc16 ( this . _fileRxBuffer ) ;
@@ -684,8 +703,20 @@ export default class RaftFileHandler {
684703 ) : void {
685704 // RaftLog.info(`onFileBlock filePos ${filePos} fileBlockData ${RaftUtils.bufferToHex(fileBlockData)}`);
686705
706+ if ( ! this . _fileRxActive ) {
707+ RaftLog . verbose ( `onFileBlock ignored inactive transfer filePos ${ filePos } len ${ fileBlockData . length } ` ) ;
708+ return ;
709+ }
710+
711+ const streamID = ( filePos >>> 24 ) & 0xff ;
712+ const streamFilePos = filePos & 0x00ffffff ;
713+ if ( streamID !== 0 && streamID !== this . _fileRxStreamID ) {
714+ RaftLog . verbose ( `onFileBlock ignored stale streamID ${ streamID } active ${ this . _fileRxStreamID } ` ) ;
715+ return ;
716+ }
717+
687718 // Check if this is the next block we are expecting
688- if ( filePos === this . _fileRxBuffer . length ) {
719+ if ( streamFilePos === this . _fileRxBuffer . length ) {
689720
690721 // Add to buffer
691722 const tmpArray = new Uint8Array ( this . _fileRxBuffer . length + fileBlockData . length ) ;
@@ -700,7 +731,7 @@ export default class RaftFileHandler {
700731 // RaftLog.info(`onFileBlock filePos ${filePos} fileBlockData ${RaftUtils.bufferToHex(fileBlockData)} added to buffer`);
701732
702733 } else {
703- RaftLog . warn ( `onFileBlock expected streamID ${ this . _fileRxStreamID } filePos ${ filePos } fileBlockData ${ RaftUtils . bufferToHex ( fileBlockData ) } out of sequence` ) ;
734+ RaftLog . warn ( `onFileBlock expected streamID ${ this . _fileRxStreamID } filePos ${ streamFilePos } fileBlockData ${ RaftUtils . bufferToHex ( fileBlockData ) } out of sequence` ) ;
704735 }
705736 }
706737
0 commit comments