fix(nimble): Fix issues found in review for nimble examples

This commit is contained in:
Rahul Tank
2026-07-24 17:50:29 +08:00
committed by BOT
parent 99b87ca81f
commit fa1bf5e2fc
73 changed files with 1299 additions and 635 deletions
@@ -145,9 +145,11 @@ gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
sizeof(gatt_svr_chr_val),
sizeof(gatt_svr_chr_val),
&gatt_svr_chr_val, NULL);
ble_gatts_chr_updated(attr_handle);
MODLOG_DFLT(INFO, "Notification/Indication scheduled for "
"all subscribed peers.\n");
if (rc == 0) {
ble_gatts_chr_updated(attr_handle);
MODLOG_DFLT(INFO, "Notification/Indication scheduled for "
"all subscribed peers.\n");
}
return rc;
}
goto unknown;
@@ -164,7 +166,7 @@ gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
if (ble_uuid_cmp(uuid, &gatt_svr_dsc_uuid.u) == 0) {
rc = os_mbuf_append(ctxt->om,
&gatt_svr_dsc_val,
sizeof(gatt_svr_chr_val));
sizeof(gatt_svr_dsc_val));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
goto unknown;
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -85,8 +85,8 @@ ext_bleprph_advertise(void)
/* enable connectable advertising */
params.connectable = 1;
/* advertise using random addr */
params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
/* advertise using configured/inferred addr type */
params.own_addr_type = own_addr_type;
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_2M;
@@ -155,15 +155,18 @@ bleprph_advertise(void)
fields.tx_pwr_lvl_is_present = 1;
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
#if CONFIG_BT_NIMBLE_GAP_SERVICE
const char *name;
name = ble_svc_gap_device_name();
fields.name = (uint8_t *)name;
fields.name_len = strlen(name);
fields.name_is_complete = 1;
#endif
fields.uuids16 = (ble_uuid16_t[]) {
static const ble_uuid16_t adv_uuids16[] = {
BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)
};
fields.uuids16 = adv_uuids16;
fields.num_uuids16 = 1;
fields.uuids16_is_complete = 1;
@@ -191,11 +194,16 @@ static void bleprph_power_control(uint16_t conn_handle)
{
int rc;
rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01 ); // Attempting on LE 1M phy
assert (rc == 0);
rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01);
if (rc != 0) {
MODLOG_DFLT(WARN, "ble_gap_read_remote_transmit_power_level failed; rc=%d\n", rc);
return;
}
rc = ble_gap_set_transmit_power_reporting_enable(conn_handle, 0x1, 0x1);
assert (rc == 0);
if (rc != 0) {
MODLOG_DFLT(WARN, "ble_gap_set_transmit_power_reporting_enable failed; rc=%d\n", rc);
}
}
#endif
@@ -419,17 +427,21 @@ bleprph_on_reset(int reason)
static void
ble_app_set_addr(void)
{
ble_addr_t addr;
ble_addr_t addr = {0};
int rc;
/* generate new non-resolvable private address */
rc = ble_hs_id_gen_rnd(0, &addr);
assert(rc == 0);
if (rc != 0) {
MODLOG_DFLT(ERROR, "ble_hs_id_gen_rnd failed; rc=%d\n", rc);
return;
}
/* set generated address */
rc = ble_hs_id_set_rnd(addr.val);
assert(rc == 0);
if (rc != 0) {
MODLOG_DFLT(ERROR, "ble_hs_id_set_rnd failed; rc=%d\n", rc);
}
}
#endif
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@@ -53,18 +53,36 @@ static void IRAM_ATTR hci_uart_rx_task(void *arg)
int len_now_read = -1;
uint32_t len_to_read = 1;
uint32_t len_total_read = 0;
uint32_t len_state_read = 0;
uint8_t rx_st = UART_RX_TYPE;
while (!s_shutdown_flag) {
// Use timeout instead of portMAX_DELAY to allow periodic shutdown flag check
len_now_read = uart_read_bytes(UART_NO, &buf[len_total_read], len_to_read, pdMS_TO_TICKS(100));
if (len_state_read < len_to_read) {
// Use timeout instead of portMAX_DELAY to allow periodic shutdown flag check
len_now_read = uart_read_bytes(UART_NO, &buf[len_total_read],
len_to_read - len_state_read,
pdMS_TO_TICKS(100));
// If timeout occurred, continue loop to check shutdown flag again
if (len_now_read == 0) {
continue;
// If timeout occurred, continue loop to check shutdown flag again
if (len_now_read == 0) {
continue;
}
if (len_now_read < 0) {
ESP_LOGE(TAG, "uart_read_bytes failed: %d", len_now_read);
rx_st = UART_RX_TYPE;
len_to_read = 1;
len_total_read = 0;
len_state_read = 0;
continue;
}
len_total_read += len_now_read;
len_state_read += len_now_read;
if (len_state_read < len_to_read) {
continue;
}
}
len_total_read += len_now_read;
len_state_read = 0;
switch (rx_st) {
case UART_RX_TYPE: {
@@ -83,6 +101,18 @@ static void IRAM_ATTR hci_uart_rx_task(void *arg)
case UART_RX_LEN: {
if (buf[0] == DATA_TYPE_ACL) {
len_to_read = buf[3] | (buf[4] << 8);
if (len_total_read + len_to_read > sizeof(buf)) {
ESP_LOGE(TAG, "ACL packet length %lu exceeds buffer size, discarding", (unsigned long)len_to_read);
/* Drain the unread payload bytes so the UART stream stays
* synchronised; without this the next UART_RX_TYPE read
* would pick up a payload byte, not a valid H4 type byte. */
uart_flush_input(UART_NO);
rx_st = UART_RX_TYPE;
len_to_read = 1;
len_total_read = 0;
len_state_read = 0;
break;
}
} else if (buf[0] == DATA_TYPE_EVENT) {
len_to_read = buf[2];
} else {
@@ -107,6 +137,10 @@ static void IRAM_ATTR hci_uart_rx_task(void *arg)
ESP_LOGE(TAG, "Received HCI data length at host (%d)"
"exceeds maximum configured HCI event buffer size (%d).",
totlen, MYNEWT_VAL(BLE_TRANSPORT_EVT_SIZE));
rx_st = UART_RX_TYPE;
len_to_read = 1;
len_total_read = 0;
len_state_read = 0;
break;
}
@@ -121,6 +155,10 @@ static void IRAM_ATTR hci_uart_rx_task(void *arg)
/* Skip advertising report if we're out of memory */
if (!evbuf) {
ESP_LOGE(TAG, "No buffers");
rx_st = UART_RX_TYPE;
len_to_read = 1;
len_total_read = 0;
len_state_read = 0;
break;
}
} else {
@@ -141,13 +179,18 @@ static void IRAM_ATTR hci_uart_rx_task(void *arg)
rx_st = UART_RX_TYPE;
len_to_read = 1;
len_total_read = 0;
len_state_read = 0;
break;
}
if ((rc = os_mbuf_append(m, &data[1], len_total_read - 1)) != 0) {
ESP_LOGE(TAG, "%s failed to os_mbuf_append; rc = %d", __func__, rc);
os_mbuf_free_chain(m);
return;
rx_st = UART_RX_TYPE;
len_to_read = 1;
len_total_read = 0;
len_state_read = 0;
break;
}
ble_transport_to_hs_acl(m);
@@ -156,6 +199,7 @@ static void IRAM_ATTR hci_uart_rx_task(void *arg)
rx_st = UART_RX_TYPE;
len_to_read = 1;
len_total_read = 0;
len_state_read = 0;
}
break;
@@ -166,7 +210,8 @@ static void IRAM_ATTR hci_uart_rx_task(void *arg)
}
}
vTaskDelete(NULL);
s_rx_task_hdl = NULL;
vTaskDelete(NULL);
}
void hci_uart_send(uint8_t *buf, uint16_t len)
@@ -176,7 +221,10 @@ void hci_uart_send(uint8_t *buf, uint16_t len)
while (len) {
len_write = uart_write_bytes(UART_NO, p, len);
assert(len_write > 0);
if (len_write <= 0) {
ESP_LOGE(TAG, "uart_write_bytes failed: %d", len_write);
return;
}
len -= len_write;
p += len_write;
}
@@ -216,7 +264,7 @@ int
ble_transport_to_ll_cmd_impl(void *buf)
{
int len = 3 + ((uint8_t *)buf)[2] + 1;
uint8_t data[258];
uint8_t data[259];
data[0] = HCI_H4_CMD;
memcpy(data + 1, buf, len - 1);
hci_uart_send(data, len);
@@ -262,7 +310,7 @@ void hci_uart_close(void)
const int max_wait_count = 5;
TaskHandle_t task_handle = s_rx_task_hdl;
while (wait_count < max_wait_count) {
while (wait_count < max_wait_count && s_rx_task_hdl == task_handle) {
vTaskDelay(pdMS_TO_TICKS(100));
wait_count++;
@@ -272,8 +320,8 @@ void hci_uart_close(void)
if (s_rx_task_hdl == task_handle) {
vTaskDelete(s_rx_task_hdl);
vTaskDelay(pdMS_TO_TICKS(100));
s_rx_task_hdl = NULL;
}
s_rx_task_hdl = NULL;
}
uart_driver_delete(UART_NO);