mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 03:00:34 +03:00
fix(nimble): Fix issues found in review for nimble examples
This commit is contained in:
@@ -78,6 +78,7 @@ enc_adv_data_cent_on_read(uint16_t conn_handle,
|
||||
{
|
||||
int rc;
|
||||
struct ble_store_value_ead value_ead = {0};
|
||||
struct ble_gap_conn_desc desc;
|
||||
struct peer *p;
|
||||
|
||||
MODLOG_DFLT(INFO, "Read complete; status=%d conn_handle=%d", error->status,
|
||||
@@ -119,6 +120,10 @@ enc_adv_data_cent_on_read(uint16_t conn_handle,
|
||||
print_bytes(value_ead.km.iv, BLE_EAD_IV_SIZE);
|
||||
|
||||
memcpy(&value_ead.peer_addr.val, &p->peer_addr, PEER_ADDR_VAL_SIZE);
|
||||
rc = ble_gap_conn_find(conn_handle, &desc);
|
||||
if (rc == 0) {
|
||||
value_ead.peer_addr.type = desc.peer_id_addr.type;
|
||||
}
|
||||
|
||||
rc = ble_store_write_ead(&value_ead);
|
||||
if (rc == 0) {
|
||||
@@ -184,6 +189,8 @@ enc_adv_data_cent_on_disc_complete(const struct peer *peer, int status, void *ar
|
||||
if (!enc_adv_data_check_km_exist(peer->peer_addr)) {
|
||||
/* Now perform GATT read procedures against the peer */
|
||||
enc_adv_data_cent_read(peer);
|
||||
} else {
|
||||
ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -289,7 +296,17 @@ enc_adv_data_cent_decrypt(uint8_t length_data, const uint8_t *data, const uint8_
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t actual_dec_len = enc_payload_len - (BLE_EAD_RANDOMIZER_SIZE + BLE_EAD_MIC_SIZE);
|
||||
if (actual_dec_len == 0) {
|
||||
free(enc_data);
|
||||
return 0;
|
||||
}
|
||||
dec_data_len = temp[0];
|
||||
if ((uint16_t)dec_data_len + 1 > actual_dec_len) {
|
||||
MODLOG_DFLT(ERROR, "Decrypted length field exceeds payload");
|
||||
free(enc_data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
MODLOG_DFLT(INFO, "Data after decryption:");
|
||||
for (int i = 0; i < dec_data_len + 1; i++) {
|
||||
@@ -318,12 +335,8 @@ enc_adv_data_cent_ext_should_connect(const struct ble_gap_ext_disc_desc *disc)
|
||||
{
|
||||
int offset = 0;
|
||||
int ad_struct_len = 0;
|
||||
#if CONFIG_EXAMPLE_USE_CI_ADDRESS
|
||||
uint32_t *addr_offset;
|
||||
#endif // CONFIG_EXAMPLE_USE_CI_ADDRESS
|
||||
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)) {
|
||||
@@ -333,14 +346,9 @@ enc_adv_data_cent_ext_should_connect(const struct ble_gap_ext_disc_desc *disc)
|
||||
peer_addr_parse(CONFIG_EXAMPLE_PEER_ADDR, test_addr);
|
||||
#endif
|
||||
|
||||
/* Conversion */
|
||||
for(int i=0; i<6; i++) {
|
||||
test_addr[i] = (uint8_t )peer_addr[i];
|
||||
}
|
||||
|
||||
#if CONFIG_EXAMPLE_USE_CI_ADDRESS
|
||||
addr_offset = (uint32_t *)&test_addr[1];
|
||||
*addr_offset = atoi(CONFIG_EXAMPLE_PEER_ADDR);
|
||||
uint32_t addr_val = (uint32_t)atoi(CONFIG_EXAMPLE_PEER_ADDR);
|
||||
memcpy(&test_addr[1], &addr_val, sizeof(addr_val));
|
||||
test_addr[5] = 0xC3;
|
||||
test_addr[0] = CONFIG_IDF_FIRMWARE_CHIP_ID;
|
||||
#endif
|
||||
@@ -372,6 +380,7 @@ enc_adv_data_cent_ext_should_connect(const struct ble_gap_ext_disc_desc *disc)
|
||||
MODLOG_DFLT(INFO, "Adding peer addr : %s", addr_str(&disc->addr.val));
|
||||
|
||||
memcpy(&kmp[counter].peer_addr, &disc->addr.val, PEER_ADDR_VAL_SIZE);
|
||||
kmp[counter].key_material_exist = false;
|
||||
counter++;
|
||||
|
||||
if (counter > CONFIG_BT_NIMBLE_MAX_CONNECTIONS) {
|
||||
@@ -399,7 +408,7 @@ enc_adv_data_cent_should_connect(const struct ble_gap_disc_desc *disc)
|
||||
struct ble_hs_adv_fields fields;
|
||||
int rc;
|
||||
int i;
|
||||
uint8_t test_addr[6];
|
||||
uint8_t test_addr[6] = {0};
|
||||
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
|
||||
disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
|
||||
return 0;
|
||||
@@ -433,6 +442,7 @@ enc_adv_data_cent_should_connect(const struct ble_gap_disc_desc *disc)
|
||||
MODLOG_DFLT(INFO, "Adding peer addr : %s", addr_str(&disc->addr.val));
|
||||
|
||||
memcpy(&kmp[counter].peer_addr, &disc->addr.val, PEER_ADDR_VAL_SIZE);
|
||||
kmp[counter].key_material_exist = false;
|
||||
counter++;
|
||||
|
||||
if (counter > CONFIG_BT_NIMBLE_MAX_CONNECTIONS) {
|
||||
@@ -487,6 +497,9 @@ enc_adv_data_cent_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))
|
||||
enc_adv_data_cent_scan();
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -505,6 +518,9 @@ enc_adv_data_cent_connect_if_interesting(void *disc)
|
||||
MODLOG_DFLT(ERROR, "Error: Failed to connect to device; addr_type=%d "
|
||||
"addr=%s; rc=%d\n",
|
||||
addr->type, addr_str(addr->val), rc);
|
||||
#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
|
||||
enc_adv_data_cent_scan();
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -572,12 +588,15 @@ enc_adv_data_cent_gap_event(struct ble_gap_event *event, void *arg)
|
||||
rc = peer_add(event->connect.conn_handle);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "Failed to add peer; rc=%d\n", rc);
|
||||
ble_gap_terminate(event->connect.conn_handle, BLE_ERR_REM_USER_CONN_TERM);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rc = peer_set_addr(event->connect.conn_handle, desc.peer_id_addr.val);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "Failed to set peer addr; rc=%d\n", rc);
|
||||
peer_delete(event->connect.conn_handle);
|
||||
ble_gap_terminate(event->connect.conn_handle, BLE_ERR_REM_USER_CONN_TERM);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -744,11 +763,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) {
|
||||
ESP_LOGE(tag, "Failed to init peer tracking; rc=%d", rc);
|
||||
nimble_port_deinit();
|
||||
return;
|
||||
}
|
||||
|
||||
#if CONFIG_BT_NIMBLE_GAP_SERVICE
|
||||
/* Set the default device name. */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
@@ -150,9 +150,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;
|
||||
@@ -169,7 +171,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;
|
||||
|
||||
@@ -140,8 +140,8 @@ enc_adv_data_prph_ext_advertise(void)
|
||||
/* enable connectable advertising */
|
||||
params.connectable = 1;
|
||||
|
||||
/* advertise using random addr */
|
||||
params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
|
||||
/* advertise using configured addr */
|
||||
params.own_addr_type = own_addr_type;
|
||||
|
||||
params.primary_phy = BLE_HCI_LE_PHY_1M;
|
||||
params.secondary_phy = BLE_HCI_LE_PHY_2M;
|
||||
@@ -163,6 +163,10 @@ enc_adv_data_prph_ext_advertise(void)
|
||||
|
||||
/* get mbuf with adv data */
|
||||
temp = malloc(sizeof(ext_adv_pattern) + 2 + encrypted_adv_data_len);
|
||||
if (temp == NULL) {
|
||||
MODLOG_DFLT(ERROR, "Failed to allocate temp buffer for ext adv data");
|
||||
return;
|
||||
}
|
||||
memcpy(temp, ext_adv_pattern, sizeof(ext_adv_pattern));
|
||||
temp[sizeof(ext_adv_pattern)] = 1 + encrypted_adv_data_len;
|
||||
temp[sizeof(ext_adv_pattern) + 1] = BLE_GAP_ENC_ADV_DATA;
|
||||
@@ -194,8 +198,11 @@ enc_adv_data_prph_advertise(void)
|
||||
struct ble_hs_adv_fields fields;
|
||||
int rc;
|
||||
|
||||
const unsigned encrypted_adv_data_len = BLE_EAD_ENCRYPTED_PAYLOAD_SIZE(sizeof(unencrypted_adv_pattern));
|
||||
uint8_t encrypted_adv_data[encrypted_adv_data_len];
|
||||
static const ble_uuid16_t adv_uuids16[] = {
|
||||
BLE_UUID16_INIT(0x2C01) /** For the central to recognise this device */
|
||||
};
|
||||
static uint8_t encrypted_adv_data[BLE_EAD_ENCRYPTED_PAYLOAD_SIZE(sizeof(unencrypted_adv_pattern))];
|
||||
const unsigned encrypted_adv_data_len = sizeof(encrypted_adv_data);
|
||||
memset(encrypted_adv_data, 0, encrypted_adv_data_len);
|
||||
|
||||
/* First check if any instance is already active */
|
||||
@@ -207,8 +214,6 @@ enc_adv_data_prph_advertise(void)
|
||||
memset (¶ms, 0, sizeof(params));
|
||||
memset (&fields, 0, sizeof(fields));
|
||||
|
||||
own_addr_type = BLE_OWN_ADDR_PUBLIC;
|
||||
|
||||
/* enable connectable advertising */
|
||||
params.conn_mode = BLE_GAP_CONN_MODE_UND;
|
||||
params.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
||||
@@ -222,9 +227,7 @@ enc_adv_data_prph_advertise(void)
|
||||
fields.name_len = 3;
|
||||
fields.name_is_complete = 1;
|
||||
|
||||
fields.uuids16 = (ble_uuid16_t[]) {
|
||||
BLE_UUID16_INIT(0x2C01) /** For the central to recognise this device */
|
||||
};
|
||||
fields.uuids16 = adv_uuids16;
|
||||
fields.num_uuids16 = 1;
|
||||
fields.uuids16_is_complete = 1;
|
||||
|
||||
@@ -397,17 +400,22 @@ enc_adv_data_prph_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, "Failed to generate random address; 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, "Failed to set random address; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user