Skip to content

Commit ad9e0d5

Browse files
committed
Merge branch 'bugfix/ble_common_issues_v5.4' into 'release/v5.4'
Bugfix/ble common issues v5.4 See merge request espressif/esp-idf!35790
2 parents ebd77e1 + 71b0d14 commit ad9e0d5

File tree

37 files changed

+271
-182
lines changed

37 files changed

+271
-182
lines changed

components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void esp_blufi_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *a
4444

4545
/* Initialise gatt server */
4646
int esp_blufi_gatt_svr_init(void);
47+
int esp_blufi_gatt_svr_deinit(void);
4748
void esp_blufi_btc_init(void);
4849
void esp_blufi_btc_deinit(void);
4950
#endif

components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -240,6 +240,32 @@ static void init_gatt_values(void)
240240

241241
}
242242

243+
static void deinit_gatt_values(void)
244+
{
245+
int i = 0;
246+
const struct ble_gatt_svc_def *svc;
247+
const struct ble_gatt_chr_def *chr;
248+
const struct ble_gatt_dsc_def *dsc;
249+
250+
for (svc = gatt_svr_svcs; svc && svc->uuid; svc++) {
251+
for (chr = svc->characteristics; chr && chr->uuid; chr++) {
252+
if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) {
253+
os_mbuf_free(gatt_values[i].buf); /* Free the buffer */
254+
gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */
255+
}
256+
++i;
257+
258+
for (dsc = chr->descriptors; dsc && dsc->uuid; dsc++) {
259+
if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) {
260+
os_mbuf_free(gatt_values[i].buf); /* Free the buffer */
261+
gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */
262+
}
263+
++i;
264+
}
265+
}
266+
}
267+
}
268+
243269
int esp_blufi_gatt_svr_init(void)
244270
{
245271
int rc;
@@ -260,6 +286,18 @@ int esp_blufi_gatt_svr_init(void)
260286
return 0;
261287
}
262288

289+
int esp_blufi_gatt_svr_deinit(void)
290+
{
291+
deinit_gatt_values();
292+
293+
ble_gatts_free_svcs();
294+
/* Deinitialize BLE GATT and GAP services */
295+
ble_svc_gatt_deinit();
296+
ble_svc_gap_deinit();
297+
298+
return 0;
299+
}
300+
263301
static int
264302
esp_blufi_gap_event(struct ble_gap_event *event, void *arg)
265303
{

components/bt/common/osi/pkt_queue.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct pkt_queue *pkt_queue_create(void)
2525
}
2626
if (osi_mutex_new(&queue->lock) != 0) {
2727
osi_free(queue);
28+
return NULL;
2829
}
2930
struct pkt_queue_header *p = &queue->header;
3031
STAILQ_INIT(p);

components/bt/host/nimble/Kconfig.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,15 @@ config BT_NIMBLE_HOST_QUEUE_CONG_CHECK
10721072
or application layer handling adv packets is slow, it will cause the controller memory
10731073
to run out. if enabled, adv packets will be lost when host queue is congested.
10741074

1075+
config BT_NIMBLE_GATTC_PROC_PREEMPTION_PROTECT
1076+
bool "Gatt-proc preemption protect check"
1077+
depends on BT_NIMBLE_ENABLED
1078+
default n
1079+
help
1080+
When BLE and Wireless protocol/IEEE 802.15.4 operate in coexistence, BLE preemption
1081+
can disrupt the GATT context,causing the service discovery callback to not be invoked.
1082+
A temporary list is maintained to preserve the GATT context and use it in case of preemption.
1083+
10751084
menu "Host-controller Transport"
10761085
config BT_NIMBLE_TRANSPORT_UART
10771086
bool "Enable Uart Transport"

components/bt/host/nimble/port/include/esp_nimble_cfg.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -1953,6 +1953,14 @@
19531953
#endif
19541954
#endif
19551955

1956+
#ifndef MYNEWT_VAL_BLE_GATTC_PROC_PREEMPTION_PROTECT
1957+
#ifdef CONFIG_BT_NIMBLE_GATTC_PROC_PREEMPTION_PROTECT
1958+
#define MYNEWT_VAL_BLE_GATTC_PROC_PREEMPTION_PROTECT CONFIG_BT_NIMBLE_GATTC_PROC_PREEMPTION_PROTECT
1959+
#else
1960+
#define MYNEWT_VAL_BLE_GATTC_PROC_PREEMPTION_PROTECT (0)
1961+
#endif
1962+
#endif
1963+
19561964
#ifndef MYNEWT_VAL_BLE_HOST_ALLOW_CONNECT_WITH_SCAN
19571965
#ifdef CONFIG_BT_NIMBLE_HOST_ALLOW_CONNECT_WITH_SCAN
19581966
#define MYNEWT_VAL_BLE_HOST_ALLOW_CONNECT_WITH_SCAN CONFIG_BT_NIMBLE_HOST_ALLOW_CONNECT_WITH_SCAN

components/bt/porting/mem/bt_osi_mem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ IRAM_ATTR void *bt_osi_mem_malloc(size_t size)
2525
#endif
2626
if (!mem) {
2727
log_count ++;
28-
if ((log_count % 40) == 0) {
29-
esp_rom_printf("malloc failed (size %zu)",size);
28+
if ((log_count % 100) == 0) {
29+
ESP_EARLY_LOGI("ESP_LOG_INFO","malloc failed (size %zu)",size);
3030
log_count = 0;
3131
}
3232
assert(mem != NULL);

components/wifi_provisioning/src/scheme_ble.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, ui
227227
realloc(ble_config->nu_lookup, (ble_config->nu_lookup_count + 1) * sizeof(protocomm_ble_name_uuid_t)));
228228
if (!lookup_table) {
229229
ESP_LOGE(TAG, "Error allocating memory for EP-UUID lookup table");
230+
free(copy_ep_name);
230231
return ESP_ERR_NO_MEM;
231232
}
232233

examples/bluetooth/blufi/main/blufi_init.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -233,14 +233,17 @@ esp_err_t esp_blufi_host_deinit(void)
233233
{
234234
esp_err_t ret = ESP_OK;
235235

236+
esp_blufi_gatt_svr_deinit();
236237
ret = nimble_port_stop();
237-
238+
if (ret != ESP_OK) {
239+
return ret;
240+
}
238241
if (ret == 0) {
239242
esp_nimble_deinit();
240243
}
241244

242245
ret = esp_blufi_profile_deinit();
243-
if(ret != ESP_OK) {
246+
if (ret != ESP_OK) {
244247
return ret;
245248
}
246249

examples/bluetooth/esp_hid_host/main/esp_hid_host_main.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ static const char *TAG = "ESP_HIDH_DEMO";
5555
static const char * remote_device_name = CONFIG_EXAMPLE_PEER_DEVICE_NAME;
5656
#endif // CONFIG_BT_HID_HOST_ENABLED
5757

58-
static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
58+
#if !CONFIG_BT_NIMBLE_ENABLED
59+
static char *bda2str(uint8_t *bda, char *str, size_t size)
5960
{
6061
if (bda == NULL || str == NULL || size < 18) {
6162
return NULL;
@@ -66,6 +67,7 @@ static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
6667
p[0], p[1], p[2], p[3], p[4], p[5]);
6768
return str;
6869
}
70+
#endif
6971

7072
void hidh_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data)
7173
{
@@ -201,7 +203,6 @@ void ble_store_config_init(void);
201203
#endif
202204
void app_main(void)
203205
{
204-
char bda_str[18] = {0};
205206
esp_err_t ret;
206207
#if HID_HOST_MODE == HIDH_IDLE_MODE
207208
ESP_LOGE(TAG, "Please turn on BT HID host or BLE!");
@@ -225,7 +226,12 @@ void app_main(void)
225226
};
226227
ESP_ERROR_CHECK( esp_hidh_init(&config) );
227228

229+
#if !CONFIG_BT_NIMBLE_ENABLED
230+
char bda_str[18] = {0};
228231
ESP_LOGI(TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
232+
#endif
233+
234+
229235
#if CONFIG_BT_NIMBLE_ENABLED
230236
/* XXX Need to have template for store */
231237
ble_store_config_init();
@@ -236,6 +242,28 @@ void app_main(void)
236242
if (ret) {
237243
ESP_LOGE(TAG, "esp_nimble_enable failed: %d", ret);
238244
}
245+
246+
vTaskDelay(200);
247+
248+
uint8_t own_addr_type = 0;
249+
int rc;
250+
uint8_t addr_val[6] = {0};
251+
252+
rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, NULL, NULL);
253+
254+
rc = ble_hs_id_infer_auto(0, &own_addr_type);
255+
256+
if (rc != 0) {
257+
ESP_LOGI(TAG, "error determining address type; rc=%d\n", rc);
258+
return;
259+
}
260+
261+
rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
262+
263+
ESP_LOGI(TAG, "Device Address: ");
264+
ESP_LOGI(TAG, "%02x:%02x:%02x:%02x:%02x:%02x \n", addr_val[5], addr_val[4], addr_val[3],
265+
addr_val[2], addr_val[1], addr_val[0]);
266+
239267
#endif
240268
xTaskCreate(&hid_demo_task, "hid_task", 6 * 1024, NULL, 2, NULL);
241269
}

examples/bluetooth/nimble/ble_cts/cts_cent/main/main.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -375,17 +375,17 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
375375

376376
case BLE_GAP_EVENT_LINK_ESTAB:
377377
/* A new connection was established or a connection attempt failed. */
378-
if (event->connect.status == 0) {
378+
if (event->link_estab.status == 0) {
379379
/* Connection successfully established. */
380380
MODLOG_DFLT(INFO, "Connection established ");
381381

382-
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
382+
rc = ble_gap_conn_find(event->link_estab.conn_handle, &desc);
383383
assert(rc == 0);
384384
print_conn_desc(&desc);
385385
MODLOG_DFLT(INFO, "\n");
386386

387387
/* Remember peer. */
388-
rc = peer_add(event->connect.conn_handle);
388+
rc = peer_add(event->link_estab.conn_handle);
389389
if (rc != 0) {
390390
MODLOG_DFLT(ERROR, "Failed to add peer; rc=%d\n", rc);
391391
return 0;
@@ -398,17 +398,17 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
398398
* Encryption (Enable encryption)
399399
* Will invoke event BLE_GAP_EVENT_ENC_CHANGE
400400
**/
401-
rc = ble_gap_security_initiate(event->connect.conn_handle);
401+
rc = ble_gap_security_initiate(event->link_estab.conn_handle);
402402
if (rc != 0) {
403403
MODLOG_DFLT(INFO, "Security could not be initiated, rc = %d\n", rc);
404-
return ble_gap_terminate(event->connect.conn_handle,
404+
return ble_gap_terminate(event->link_estab.conn_handle,
405405
BLE_ERR_REM_USER_CONN_TERM);
406406
} else {
407407
MODLOG_DFLT(INFO, "Connection secured\n");
408408
}
409409
#else
410410
/* Perform service discovery */
411-
rc = peer_disc_all(event->connect.conn_handle,
411+
rc = peer_disc_all(event->link_estab.conn_handle,
412412
ble_cts_cent_on_disc_complete, NULL);
413413
if (rc != 0) {
414414
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
@@ -418,7 +418,7 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
418418
} else {
419419
/* Connection attempt failed; resume scanning. */
420420
MODLOG_DFLT(ERROR, "Error: Connection failed; status=%d\n",
421-
event->connect.status);
421+
event->link_estab.status);
422422
ble_cts_cent_scan();
423423
}
424424

examples/bluetooth/nimble/ble_cts/cts_prph/main/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -181,10 +181,10 @@ ble_cts_prph_gap_event(struct ble_gap_event *event, void *arg)
181181
case BLE_GAP_EVENT_LINK_ESTAB:
182182
/* A new connection was established or a connection attempt failed */
183183
MODLOG_DFLT(INFO, "connection %s; status=%d\n",
184-
event->connect.status == 0 ? "established" : "failed",
185-
event->connect.status);
184+
event->link_estab.status == 0 ? "established" : "failed",
185+
event->link_estab.status);
186186

187-
if (event->connect.status != 0) {
187+
if (event->link_estab.status != 0) {
188188
/* Connection failed; resume advertising */
189189
#if CONFIG_EXAMPLE_EXTENDED_ADV
190190
ext_ble_cts_prph_advertise();

examples/bluetooth/nimble/ble_dynamic_service/main/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -139,16 +139,16 @@ dynamic_service_gap_event(struct ble_gap_event *event, void *arg)
139139
case BLE_GAP_EVENT_LINK_ESTAB:
140140
/* A new connection was established or a connection attempt failed. */
141141
MODLOG_DFLT(INFO, "connection %s; status=%d ",
142-
event->connect.status == 0 ? "established" : "failed",
143-
event->connect.status);
144-
if (event->connect.status == 0) {
145-
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
142+
event->link_estab.status == 0 ? "established" : "failed",
143+
event->link_estab.status);
144+
if (event->link_estab.status == 0) {
145+
rc = ble_gap_conn_find(event->link_estab.conn_handle, &desc);
146146
assert(rc == 0);
147147
dynamic_service_print_conn_desc(&desc);
148148
}
149149
MODLOG_DFLT(INFO, "\n");
150150

151-
if (event->connect.status != 0) {
151+
if (event->link_estab.status != 0) {
152152
/* Connection failed; resume advertising. */
153153
dynamic_service_advertise();
154154
}

0 commit comments

Comments
 (0)