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

This commit is contained in:
Rahul Tank
2026-07-24 15:19:38 +05:30
parent 0185451ed1
commit 46f3837611
74 changed files with 1298 additions and 612 deletions
@@ -206,16 +206,17 @@ ext_ble_cts_cent_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 */
peer_addr_parse(CONFIG_EXAMPLE_PEER_ADDR, test_addr);
/* Convert string to address */
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) {
return 0;
}
@@ -224,17 +225,24 @@ ext_ble_cts_cent_should_connect(const struct ble_gap_ext_disc_desc *disc)
/* The device has to advertise support for the CTS
* service (0x1805).
*/
do {
while (offset < disc->length_data) {
if (offset + 1 >= disc->length_data) {
break;
}
ad_struct_len = disc->data[offset];
if (!ad_struct_len) {
break;
}
if (offset + ad_struct_len >= disc->length_data) {
break;
}
/* Search if cts UUID is advertised */
if (disc->data[offset + 1] == 0x03) {
int temp = 2;
while (temp < ad_struct_len) {
while (temp < ad_struct_len && (offset + temp + 1) < disc->length_data) {
if(disc->data[offset + temp] == 0x05 &&
disc->data[offset + temp + 1] == 0x18) {
return 1;
@@ -243,7 +251,7 @@ ext_ble_cts_cent_should_connect(const struct ble_gap_ext_disc_desc *disc)
}
}
offset += ad_struct_len + 1;
} while ( offset < disc->length_data );
}
return 0;
}
@@ -255,7 +263,7 @@ ble_cts_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};
/* The device has to be advertising connectability. */
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
@@ -271,7 +279,9 @@ ble_cts_cent_should_connect(const struct ble_gap_disc_desc *disc)
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 */
peer_addr_parse(CONFIG_EXAMPLE_PEER_ADDR, test_addr);
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) {
return 0;
}
@@ -326,6 +336,9 @@ ble_cts_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))
ble_cts_cent_scan();
#endif
return;
}
@@ -343,6 +356,9 @@ ble_cts_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))
ble_cts_cent_scan();
#endif
return;
}
}
@@ -365,11 +381,13 @@ static int
ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
struct ble_hs_adv_fields fields;
int rc;
switch (event->type) {
case BLE_GAP_EVENT_DISC:
#if !CONFIG_EXAMPLE_EXTENDED_ADV
{
struct ble_hs_adv_fields fields;
rc = ble_hs_adv_parse_fields(&fields, event->disc.data,
event->disc.length_data);
if (rc != 0) {
@@ -381,6 +399,8 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
/* Try to connect to the advertiser if it looks interesting. */
ble_cts_cent_connect_if_interesting(&event->disc);
}
#endif
return 0;
case BLE_GAP_EVENT_CONNECT:
@@ -597,11 +617,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: 2017-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2017-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -71,9 +71,8 @@ int fetch_current_time(struct ble_svc_cts_curr_time *ctime) {
ctime->et_256.d_d_t.d_t.seconds = timeinfo.tm_sec;
/* day of week */
/* time gives day range of [0, 6], current_time_sevice
has day range of [1,7] */
ctime->et_256.d_d_t.day_of_week = timeinfo.tm_wday + 1;
/* tm_wday: 0=Sunday..6=Saturday; CTS: 1=Monday..7=Sunday */
ctime->et_256.d_d_t.day_of_week = (timeinfo.tm_wday == 0) ? 7 : timeinfo.tm_wday;
/* fractions_256 */
ctime->et_256.fractions_256 = (((uint64_t)tv_now.tv_usec * 256L )/ 1000000L);
@@ -108,7 +107,9 @@ int set_current_time(struct ble_svc_cts_curr_time ctime) {
timeinfo.tm_hour = ctime.et_256.d_d_t.d_t.hours;
timeinfo.tm_min = ctime.et_256.d_d_t.d_t.minutes;
timeinfo.tm_sec = ctime.et_256.d_d_t.d_t.seconds;
timeinfo.tm_wday = ctime.et_256.d_d_t.day_of_week - 1;
/* CTS day_of_week: 1=Monday..7=Sunday; tm_wday: 0=Sunday..6=Saturday */
timeinfo.tm_wday = (ctime.et_256.d_d_t.day_of_week == 7) ? 0 : ctime.et_256.d_d_t.day_of_week;
timeinfo.tm_isdst = -1;
now = mktime(&timeinfo);
if (now == (time_t)-1) {
ESP_LOGE(TAG, "Failed to convert current time");
@@ -137,6 +138,7 @@ int set_local_time_info(struct ble_svc_cts_local_time_info info) {
local_info.timezone = info.timezone;
local_info.dst_offset = info.dst_offset;
gettimeofday(&last_updated, NULL);
adjust_reason = (CHANGE_OF_DST_MASK | CHANGE_OF_TIME_ZONE_MASK);
return 0;
}
int fetch_reference_time_info(struct ble_svc_cts_reference_time_info *info) {
@@ -160,8 +162,6 @@ int fetch_reference_time_info(struct ble_svc_cts_reference_time_info *info) {
hours_since_update = (tv_now.tv_sec % 86400L) / 3600;
info->hours_since_update = hours_since_update;
}
adjust_reason = (CHANGE_OF_DST_MASK | CHANGE_OF_TIME_ZONE_MASK);
return 0;
}
int
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2017-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -79,8 +79,8 @@ ext_ble_cts_prph_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 = ble_cts_prph_addr_type;
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_2M;
@@ -148,9 +148,10 @@ ble_cts_prph_advertise(void)
fields.name_len = strlen(device_name);
fields.name_is_complete = 1;
fields.uuids16 = (ble_uuid16_t[]) {
static const ble_uuid16_t adv_uuids16[] = {
BLE_UUID16_INIT(BLE_SVC_CTS_UUID16)
};
fields.uuids16 = adv_uuids16;
fields.num_uuids16 = 1;
fields.uuids16_is_complete = 1;