|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
|
19 | 19 | #include "freertos/semphr.h" |
20 | 20 | #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) |
21 | 21 | #include "esp_nimble_hci.h" |
| 22 | + |
| 23 | +/* |
| 24 | + * This is a workaround for the missing os_mbuf_len function in NimBLE. |
| 25 | + * It is not present in NimBLE 1.3, but is present in NimBLE 1.4. |
| 26 | + * This function is used to get the length of an os_mbuf. |
| 27 | + */ |
| 28 | +uint16_t |
| 29 | +os_mbuf_len(const struct os_mbuf *om) |
| 30 | +{ |
| 31 | + uint16_t len; |
| 32 | + |
| 33 | + len = 0; |
| 34 | + while (om != NULL) { |
| 35 | + len += om->om_len; |
| 36 | + om = SLIST_NEXT(om, om_next); |
| 37 | + } |
| 38 | + |
| 39 | + return len; |
| 40 | +} |
22 | 41 | #endif |
23 | 42 |
|
24 | 43 | #ifdef CONFIG_PRE_ENC_OTA |
@@ -117,14 +136,9 @@ esp_ble_ota_write_chr(struct os_mbuf *om) |
117 | 136 | esp_err_t err; |
118 | 137 | pre_enc_decrypt_arg_t pargs = {}; |
119 | 138 |
|
120 | | - pargs.data_in_len = om->om_len - 3; |
121 | | - |
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 | | - } |
| 139 | + pargs.data_in_len = os_mbuf_len(om) - 3; |
126 | 140 |
|
127 | | - pargs.data_in = (const char *)malloc(pargs.data_in_len * sizeof(char *)); |
| 141 | + pargs.data_in = (const char *)malloc(pargs.data_in_len); |
128 | 142 | err = os_mbuf_copydata(om, 3, pargs.data_in_len, pargs.data_in); |
129 | 143 |
|
130 | 144 | if (om->om_data[2] == 0xff) { |
@@ -191,23 +205,11 @@ esp_ble_ota_write_chr(struct os_mbuf *om) |
191 | 205 | ESP_LOGD(TAG, "DEBUG: Sector:%" PRIu32 ", total length:%" PRIu32 ", length:%d", cur_sector, |
192 | 206 | fw_buf_offset, pargs.data_out_len); |
193 | 207 | #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 | | - } |
| 208 | + os_mbuf_copydata(om, 3, os_mbuf_len(om) - 3, fw_buf + fw_buf_offset); |
| 209 | + fw_buf_offset += os_mbuf_len(om) - 3; |
208 | 210 |
|
209 | 211 | ESP_LOGD(TAG, "DEBUG: Sector:%" PRIu32 ", total length:%" PRIu32 ", length:%d", cur_sector, |
210 | | - fw_buf_offset, om->om_len - 3); |
| 212 | + fw_buf_offset, os_mbuf_len(om) - 3); |
211 | 213 | #endif |
212 | 214 | if (om->om_data[2] == 0xff) { |
213 | 215 | cur_packet = 0; |
@@ -446,7 +448,7 @@ ble_ota_gatt_handler(uint16_t conn_handle, uint16_t attr_handle, |
446 | 448 | case BLE_GATT_ACCESS_OP_WRITE_CHR: |
447 | 449 |
|
448 | 450 | ota_char = find_ota_char_and_desr_by_handle(attr_handle); |
449 | | - ESP_LOGD(TAG, "client write; len = %d", ctxt->om->om_len); |
| 451 | + ESP_LOGD(TAG, "client write; len = %d", os_mbuf_len(ctxt->om)); |
450 | 452 |
|
451 | 453 | if (ota_char == RECV_FW_CHAR) { |
452 | 454 | if (start_ota) { |
|
0 commit comments