Skip to content

Commit d86ae49

Browse files
committed
fix(ble_ota): fix reading of NimBLE's mbufs
Also, avoid reading the internals of the mbuf struct, prefering using provided APIs. MBuf is a linked list of buffers, and in the code path of the pre-encoded OTA, the code was only reading the first two buffers. Fixed that by using `os_mbuf_copydata`, `os_mbuf_len`, etc., instead of manually iterating over the linked list.
1 parent dc88ac6 commit d86ae49

File tree

1 file changed

+6
-23
lines changed
  • components/bluetooth/ble_profiles/esp/ble_ota/src

1 file changed

+6
-23
lines changed

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,9 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
117117
esp_err_t err;
118118
pre_enc_decrypt_arg_t pargs = {};
119119

120-
pargs.data_in_len = om->om_len - 3;
120+
pargs.data_in_len = os_mbuf_len(om) - 3;
121121

122-
if (SLIST_NEXT(om, om_next) != NULL) {
123-
struct os_mbuf *temp2 = SLIST_NEXT(om, om_next);
124-
pargs.data_in_len += temp2->om_len;
125-
}
126-
127-
pargs.data_in = (const char *)malloc(pargs.data_in_len * sizeof(char *));
122+
pargs.data_in = (const char *)malloc(pargs.data_in_len);
128123
err = os_mbuf_copydata(om, 3, pargs.data_in_len, pargs.data_in);
129124

130125
if (om->om_data[2] == 0xff) {
@@ -191,23 +186,11 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
191186
ESP_LOGD(TAG, "DEBUG: Sector:%" PRIu32 ", total length:%" PRIu32 ", length:%d", cur_sector,
192187
fw_buf_offset, pargs.data_out_len);
193188
#else
194-
memcpy(fw_buf + fw_buf_offset, om->om_data + 3, om->om_len - 3);
195-
fw_buf_offset += om->om_len - 3;
196-
197-
if (SLIST_NEXT(om, om_next) != NULL) {
198-
struct os_mbuf *last;
199-
last = om;
200-
while (SLIST_NEXT(last, om_next) != NULL) {
201-
struct os_mbuf *temp = SLIST_NEXT(last, om_next);
202-
memcpy(fw_buf + fw_buf_offset, temp->om_data, temp->om_len);
203-
fw_buf_offset += temp->om_len;
204-
last = SLIST_NEXT(last, om_next);
205-
temp = NULL;
206-
}
207-
}
189+
os_mbuf_copydata(om, 3, os_mbuf_len(om) - 3, fw_buf + fw_buf_offset);
190+
fw_buf_offset += os_mbuf_len(om) - 3;
208191

209192
ESP_LOGD(TAG, "DEBUG: Sector:%" PRIu32 ", total length:%" PRIu32 ", length:%d", cur_sector,
210-
fw_buf_offset, om->om_len - 3);
193+
fw_buf_offset, os_buf_len(om) - 3);
211194
#endif
212195
if (om->om_data[2] == 0xff) {
213196
cur_packet = 0;
@@ -446,7 +429,7 @@ ble_ota_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,
446429
case BLE_GATT_ACCESS_OP_WRITE_CHR:
447430

448431
ota_char = find_ota_char_and_desr_by_handle(attr_handle);
449-
ESP_LOGD(TAG, "client write; len = %d", ctxt->om->om_len);
432+
ESP_LOGD(TAG, "client write; len = %d", os_mbuf_len(ctxt->om));
450433

451434
if (ota_char == RECV_FW_CHAR) {
452435
if (start_ota) {

0 commit comments

Comments
 (0)