Skip to content

Commit ad34908

Browse files
committed
Merge branch 'contrib/github_pr_502' into 'master'
fix(ble_ota): fix reading of NimBLE's mbufs (GitHub PR) Closes AEGHB-1060 See merge request ae_group/esp-iot-solution!1326
2 parents 5c90005 + 3e7ce1e commit ad34908

File tree

6 files changed

+40
-25
lines changed

6 files changed

+40
-25
lines changed

components/bluetooth/ble_profiles/esp/ble_ota/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@
7777
## v0.1.14 - 2024-11-21
7878
* BLE-OTA:
7979
* Add support for enabling BLE 5.0 for NimBLE
80+
81+
## v0.1.15 - 2025-07-29
82+
* BLE-OTA:
83+
* Fixed BLE OTA data corruption issue caused by incomplete NimBLE's mbufs reading [#502](https://github.com/espressif/esp-iot-solution/pull/502)

components/bluetooth/ble_profiles/esp/ble_ota/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ You can create a project from this example by the following command:
1111
```
1212
idf.py create-project-from-example "espressif/ble_ota^0.1.8:ble_ota"
1313
```
14+
15+
> ![TIP]
16+
>
17+
> When using NimBLE, it may be necessary to increase `CONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE`

components/bluetooth/ble_profiles/esp/ble_ota/README_CN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
## 从机示例代码
66

77
[ESP BLE OTA](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_ota)
8+
9+
> ![TIP]
10+
>
11+
> 使用 NimBLE 时,可能需要增大 `CONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE`

components/bluetooth/ble_profiles/esp/ble_ota/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.1.14"
1+
version: "0.1.15"
22
targets:
33
- esp32
44
- esp32c3

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -19,6 +19,25 @@
1919
#include "freertos/semphr.h"
2020
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
2121
#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+
}
2241
#endif
2342

2443
#ifdef CONFIG_PRE_ENC_OTA
@@ -117,14 +136,9 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
117136
esp_err_t err;
118137
pre_enc_decrypt_arg_t pargs = {};
119138

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;
126140

127-
pargs.data_in = (const char *)malloc(pargs.data_in_len * sizeof(char *));
141+
pargs.data_in = (const char *)malloc(pargs.data_in_len);
128142
err = os_mbuf_copydata(om, 3, pargs.data_in_len, pargs.data_in);
129143

130144
if (om->om_data[2] == 0xff) {
@@ -191,23 +205,11 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
191205
ESP_LOGD(TAG, "DEBUG: Sector:%" PRIu32 ", total length:%" PRIu32 ", length:%d", cur_sector,
192206
fw_buf_offset, pargs.data_out_len);
193207
#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;
208210

209211
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);
211213
#endif
212214
if (om->om_data[2] == 0xff) {
213215
cur_packet = 0;
@@ -446,7 +448,7 @@ ble_ota_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,
446448
case BLE_GATT_ACCESS_OP_WRITE_CHR:
447449

448450
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));
450452

451453
if (ota_char == RECV_FW_CHAR) {
452454
if (start_ota) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CONFIG_BT_NIMBLE_ENABLED=y
2+
CONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE=6144

0 commit comments

Comments
 (0)