@@ -2442,8 +2442,8 @@ MspHelper.prototype.setRawRx = function (channels) {
2442
2442
} ;
2443
2443
2444
2444
/**
2445
- * Send a request to read a block of data from the dataflash at the given address and pass that address and a dataview
2446
- * of the returned data to the given callback ( or null for the data if an error occurred) .
2445
+ * Send a request to read a block of data from the dataflash at the given address and pass that address and a DataView
2446
+ * of the returned data to the given callback. If an error or CRC fail occurs, the raw data is delivered instead of null .
2447
2447
*/
2448
2448
MspHelper . prototype . dataflashRead = function ( address , blockSize , onDataCallback ) {
2449
2449
let outData = [
@@ -2465,49 +2465,63 @@ MspHelper.prototype.dataflashRead = function(address, blockSize, onDataCallback)
2465
2465
2466
2466
mspObj . send_message ( MSPCodes . MSP_DATAFLASH_READ , outData , false , function ( response ) {
2467
2467
let payloadView = null ;
2468
+ let bytesCompressed = 0 ;
2468
2469
2469
2470
if ( response && response . data ) {
2470
2471
const headerSize = 7 ;
2471
2472
const chunkAddress = response . data . readU32 ( ) ;
2472
2473
const dataSize = response . data . readU16 ( ) ;
2473
2474
const dataCompressionType = response . data . readU8 ( ) ;
2474
2475
2475
- if ( chunkAddress === address ) {
2476
- try {
2477
- if ( dataCompressionType === 0 ) {
2478
- payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2479
- } else if ( dataCompressionType === 1 ) {
2480
- const compressedCharCount = response . data . readU16 ( ) ;
2481
- const compressedArray = new Uint8Array (
2482
- response . data . buffer ,
2483
- response . data . byteOffset + headerSize + 2 ,
2484
- dataSize - 2
2485
- ) ;
2486
- const decompressedArray = huffmanDecodeBuf (
2487
- compressedArray ,
2488
- compressedCharCount ,
2489
- defaultHuffmanTree ,
2490
- defaultHuffmanLenIndex
2491
- ) ;
2492
- payloadView = new DataView ( decompressedArray . buffer ) ;
2493
- }
2494
- } catch ( e ) {
2495
- console . warn ( 'Decompression or read failed, delivering raw data anyway' ) ;
2476
+ if ( chunkAddress !== address ) {
2477
+ console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2478
+ }
2479
+
2480
+ try {
2481
+ if ( dataCompressionType === 0 ) {
2482
+ // Raw, uncompressed
2496
2483
payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2484
+ bytesCompressed = dataSize ;
2485
+ } else if ( dataCompressionType === 1 ) {
2486
+ // Compressed
2487
+ const compressedCharCount = response . data . readU16 ( ) ;
2488
+ const compressedArray = new Uint8Array (
2489
+ response . data . buffer ,
2490
+ response . data . byteOffset + headerSize + 2 ,
2491
+ dataSize - 2
2492
+ ) ;
2493
+ const decompressedArray = huffmanDecodeBuf (
2494
+ compressedArray ,
2495
+ compressedCharCount ,
2496
+ defaultHuffmanTree ,
2497
+ defaultHuffmanLenIndex
2498
+ ) ;
2499
+ payloadView = new DataView ( decompressedArray . buffer ) ;
2500
+ bytesCompressed = compressedArray . length ;
2497
2501
}
2498
- } else {
2499
- console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2502
+ } catch ( e ) {
2503
+ console . warn ( 'Decompression failed, delivering raw data anyway:' , e ) ;
2504
+ // fallback to raw block
2505
+ payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2506
+ bytesCompressed = dataSize ;
2500
2507
}
2501
2508
}
2502
2509
2503
- // Deliver payloadView if defined, otherwise pass null
2510
+ // Deliver payloadView even if CRC failed
2504
2511
onDataCallback ( address , payloadView ) ;
2505
2512
2513
+ // Logging
2506
2514
if ( ! response || response . crcError ) {
2507
- console . log ( `CRC error or missing data at address ${ address } - delivering whatever we got ` ) ;
2515
+ console . log ( `CRC error or missing data at address ${ address } - delivering raw data ( ${ payloadView ? payloadView . byteLength : 0 } bytes) ` ) ;
2508
2516
} else if ( payloadView ) {
2509
2517
console . log ( `Block at ${ address } received (${ payloadView . byteLength } bytes)` ) ;
2510
2518
}
2519
+
2520
+ // Track total compressed bytes globally, if needed
2521
+ if ( typeof bytesCompressed === "number" ) {
2522
+ if ( typeof totalBytesCompressed === "undefined" || totalBytesCompressed == null ) totalBytesCompressed = 0 ;
2523
+ totalBytesCompressed += bytesCompressed ;
2524
+ }
2511
2525
} , true ) ; // end of send_message
2512
2526
} ; // end of dataflashRead
2513
2527
0 commit comments