Skip to content

Commit b6f1e5e

Browse files
Check crc of the receive sector before write to the flash
1 parent c683b16 commit b6f1e5e

File tree

1 file changed

+12
-1
lines changed
  • components/bluetooth/ble_profiles/esp/ble_ota/src

1 file changed

+12
-1
lines changed

components/bluetooth/ble_profiles/esp/ble_ota/src/nimble_ota.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,29 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
221221
return;
222222

223223
sector_end:
224+
// Check crc
225+
uint16_t cal_crc = crc16_ccitt(fw_buf, fw_buf_offset - 2);
226+
uint8_t crc_L = fw_buf[fw_buf_offset - 2];
227+
uint8_t crc_H = fw_buf[fw_buf_offset - 1];
228+
uint16_t crc = crc_H << 8 | crc_L;
229+
bool match = (cal_crc == crc ? true : false);
230+
if (match){
224231
if (fw_buf_offset < ota_block_size) {
225232
esp_ble_ota_recv_fw_handler(fw_buf, fw_buf_offset);
226233
} else {
227234
esp_ble_ota_recv_fw_handler(fw_buf, 4096);
228235
}
236+
}
229237

230238
fw_buf_offset = 0;
231239
memset(fw_buf, 0x0, ota_block_size);
232240

233241
cmd_ack[0] = om->om_data[0];
234242
cmd_ack[1] = om->om_data[1];
235-
cmd_ack[2] = 0x00; //success
243+
if (match)
244+
cmd_ack[2] = 0x00; // success
245+
else
246+
cmd_ack[2] = 0x01; // crc error
236247
cmd_ack[3] = 0x00;
237248
crc16 = crc16_ccitt(cmd_ack, 18);
238249
cmd_ack[18] = crc16 & 0xff;

0 commit comments

Comments
 (0)