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

This commit is contained in:
Rahul Tank
2026-08-31 20:27:23 +08:00
committed by BOT
parent 64d3d6a9c3
commit 2961c16685
73 changed files with 1290 additions and 611 deletions
@@ -66,17 +66,28 @@ blecent_on_read(uint16_t conn_handle,
MODLOG_DFLT(INFO, " attr_handle=%d value=", attr->handle);
print_mbuf(attr->om);
}
MODLOG_DFLT(INFO, "\n");
if (error->status != 0) {
MODLOG_DFLT(ERROR, "Read failed; terminating connection\n");
ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
return 0;
}
/* Write 1000 bytes to the LE PHY characteristic.*/
const struct peer_chr *chr;
int len = 1000;
uint8_t value[len];
const int len = 1000;
uint8_t *value;
int rc;
struct os_mbuf *txom;
const struct peer *peer = peer_find(conn_handle);
if (peer == NULL) {
MODLOG_DFLT(ERROR, "Error: peer not found for conn_handle=%d\n", conn_handle);
ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
return 0;
}
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(LE_PHY_UUID16),
BLE_UUID16_DECLARE(LE_PHY_CHR_UUID16));
@@ -86,12 +97,19 @@ blecent_on_read(uint16_t conn_handle,
goto err;
}
value = malloc(len);
if (value == NULL) {
MODLOG_DFLT(ERROR, "Insufficient memory\n");
goto err;
}
/* Fill the value array with data */
for (int i = 0; i < len; i++) {
value[i] = i;
}
txom = ble_hs_mbuf_from_flat(&value, len);
txom = ble_hs_mbuf_from_flat(value, len);
free(value);
if (!txom) {
MODLOG_DFLT(ERROR, "Insufficient memory");
goto err;
@@ -108,7 +126,7 @@ blecent_on_read(uint16_t conn_handle,
err:
/* Terminate the connection. */
return ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
return ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}
static void
@@ -234,18 +252,15 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
{
int offset = 0;
int ad_struct_len = 0;
uint8_t test_addr[6];
if (disc->legacy_event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
disc->legacy_event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
uint8_t test_addr[6] = {0};
if (!(disc->props & BLE_HCI_ADV_CONN_MASK)) {
return 0;
}
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) && (strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
/* Convert string to address */
uint8_t parsed_addr[6];
peer_addr_parse(CONFIG_EXAMPLE_PEER_ADDR, parsed_addr);
for (int i = 0; i < 6; i++) {
test_addr[5 - i] = parsed_addr[i];
if (peer_addr_parse(CONFIG_EXAMPLE_PEER_ADDR, test_addr) != 6) {
return 0;
}
if (memcmp(test_addr, disc->addr.val, sizeof(disc->addr.val)) != 0) {
@@ -255,19 +270,24 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
/* The device has to advertise support LE PHY UUID (0xABF2).
*/
do {
while (offset < disc->length_data) {
if (offset + 1 >= disc->length_data) {
break;
}
ad_struct_len = disc->data[offset];
if (!ad_struct_len || (offset + ad_struct_len + 1 > disc->length_data)) {
break;
}
/* Search if LE PHY UUID is advertised */
if (disc->data[offset] == 0x03 && disc->data[offset + 1] == 0x03) {
if ( disc->data[offset + 2] == 0xAB && disc->data[offset + 3] == 0xF2 ) {
return 1;
/* Search if LE PHY UUID is advertised (UUID list, type 0x02 or 0x03) */
if (ad_struct_len >= 3 && (disc->data[offset + 1] == 0x02 || disc->data[offset + 1] == 0x03)) {
for (int i = 2; i + 1 <= ad_struct_len; i += 2) {
if (disc->data[offset + i] == 0xF2 && disc->data[offset + i + 1] == 0xAB) {
return 1;
}
}
}
offset += ad_struct_len + 1;
} while (offset < disc->length_data);
}
return 0;
}
@@ -301,6 +321,9 @@ blecent_connect_if_interesting(void *disc)
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
blecent_scan();
#endif
return;
}
@@ -322,6 +345,9 @@ blecent_connect_if_interesting(void *disc)
MODLOG_DFLT(ERROR, "Error: Failed to connect to device; addr_type=%d "
"addr=%s; rc=%d\n",
conn_addr.type, addr_str(conn_addr.val), rc);
#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
blecent_scan();
#endif
return;
}
}
@@ -344,6 +370,7 @@ static int
blecent_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
uint8_t own_addr_type;
int rc;
switch (event->type) {
@@ -419,20 +446,36 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
return 0;
default:
return 0;
}
vTaskDelay(200);
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
return 0;
}
/* Attempt direct connection on 2M or Coded phy now */
if (s_current_phy == BLE_HCI_LE_PHY_CODED_PREF_MASK) {
MODLOG_DFLT(INFO, " Attempting to initiate connection on Coded PHY \n");
ble_gap_ext_connect(0, &conn_addr, 30000, BLE_HCI_LE_PHY_CODED_PREF_MASK,
NULL, NULL, NULL, blecent_gap_event, NULL);
rc = ble_gap_ext_connect(own_addr_type, &conn_addr, 30000, BLE_HCI_LE_PHY_CODED_PREF_MASK,
NULL, NULL, NULL, blecent_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to initiate ext connect; rc=%d\n", rc);
}
}
else if (s_current_phy == BLE_HCI_LE_PHY_2M_PREF_MASK) {
MODLOG_DFLT(INFO, " Attempting to initiate connection on 2M PHY \n");
ble_gap_ext_connect(0, &conn_addr, 30000, (BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK),
NULL, NULL, NULL, blecent_gap_event, NULL);
rc = ble_gap_ext_connect(own_addr_type, &conn_addr, 30000,
(BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK),
NULL, NULL, NULL, blecent_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to initiate ext connect; rc=%d\n", rc);
}
}
return 0;
@@ -464,11 +507,18 @@ blecent_on_sync(void)
{
int ii, rc;
uint8_t all_phy;
uint8_t own_addr_type;
uint8_t test_addr[6];
/* Make sure we have proper identity address set (public preferred) */
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
return;
}
all_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;
set_default_le_phy(all_phy, all_phy);
@@ -484,10 +534,14 @@ blecent_on_sync(void)
vTaskDelay(300);
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK ;
s_current_phy = BLE_HCI_LE_PHY_2M_PREF_MASK;
ble_gap_ext_connect(0, &conn_addr, 30000, s_current_phy,
NULL, NULL, NULL, blecent_gap_event, NULL);
rc = ble_gap_ext_connect(own_addr_type, &conn_addr, 30000,
BLE_HCI_LE_PHY_1M_PREF_MASK | s_current_phy,
NULL, NULL, NULL, blecent_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to initiate ext connect; rc=%d\n", rc);
}
}
else {
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK;
@@ -531,11 +585,14 @@ app_main(void)
/* Initialize data structures to track connected peers. */
#if MYNEWT_VAL(BLE_INCL_SVC_DISCOVERY) || MYNEWT_VAL(BLE_GATT_CACHING_INCLUDE_SERVICES)
rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64, 64);
assert(rc == 0);
#else
rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
assert(rc == 0);
#endif
if (rc != 0) {
MODLOG_DFLT(ERROR, "Failed to init peer; rc=%d\n", rc);
nimble_port_deinit();
return;
}
#if CONFIG_BT_NIMBLE_GAP_SERVICE
/* Set the default device name. */
rc = ble_svc_gap_device_name_set("blecent-phy");
@@ -31,8 +31,7 @@ static const struct ble_gatt_svc_def gatt_svr_svcs_le_phy[] = {
.uuid = BLE_UUID16_DECLARE(LE_PHY_CHR_UUID16),
.access_cb = gatt_svr_chr_access_le_phy,
.val_handle = &gatt_svr_chr_val_handle,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC | BLE_GATT_CHR_F_WRITE
| BLE_GATT_CHR_F_WRITE_ENC,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
}, {
0, /* No more characteristics in this service. */
}
@@ -17,22 +17,19 @@
static uint8_t ext_adv_pattern_1M[] = {
0x02, BLE_HS_ADV_TYPE_FLAGS, 0x06,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xab, 0xcd,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xAB, 0xF2,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xF2, 0xAB,
0x0e, BLE_HS_ADV_TYPE_COMP_NAME, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', '1', 'M',
};
static uint8_t ext_adv_pattern_2M[] = {
0x02, BLE_HS_ADV_TYPE_FLAGS, 0x06,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xab, 0xcd,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xAB, 0xF2,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xF2, 0xAB,
0x0e, BLE_HS_ADV_TYPE_COMP_NAME, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', '2', 'M',
};
static uint8_t ext_adv_pattern_coded[] = {
0x02, BLE_HS_ADV_TYPE_FLAGS, 0x06,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xab, 0xcd,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xAB, 0xF2,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xF2, 0xAB,
0x11, BLE_HS_ADV_TYPE_COMP_NAME, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', 'c', 'o', 'd', 'e',
'd',
};
@@ -239,7 +236,10 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
MODLOG_DFLT(INFO, "connection updated; status=%d ",
event->conn_update.status);
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
assert(rc == 0);
if (rc != 0) {
MODLOG_DFLT(ERROR, "ble_gap_conn_find failed; rc=%d\n", rc);
return 0;
}
bleprph_print_conn_desc(&desc);
MODLOG_DFLT(INFO, "\n");
return 0;
@@ -352,9 +352,11 @@ app_main(void)
assert(rc == 0);
#endif
#if CONFIG_BT_NIMBLE_GAP_SERVICE
/* Set the default device name. */
rc = ble_svc_gap_device_name_set("bleprph-phy");
assert(rc == 0);
#endif
/* XXX Need to have template for store */
ble_store_config_init();
@@ -12,3 +12,4 @@ CONFIG_BT_BLUEDROID_ENABLED=n
CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_BT_NIMBLE_EXT_ADV=y
CONFIG_BT_NIMBLE_BLE_GATT_BLOB_TRANSFER=y
CONFIG_BT_NIMBLE_MSYS_1_BLOCK_COUNT=64