From 0181cbd3ca3014fa7adc6219ba33a1e3beb41b7c Mon Sep 17 00:00:00 2001 From: Astha Verma Date: Mon, 30 Jun 2025 14:00:11 +0530 Subject: [PATCH] fix(nimble): Added missing api in nimble which present in bluedroid --- components/bt/host/nimble/Kconfig.in | 6 +++ components/bt/host/nimble/nimble | 2 +- .../host/nimble/port/include/esp_nimble_cfg.h | 8 ++++ .../nimble/ble_cts/cts_cent/main/main.c | 33 ++++++++++++++++ .../nimble/ble_htp/htp_cent/main/main.c | 33 ++++++++++++++++ .../proximity_sensor_cent/main/main.c | 33 ++++++++++++++++ examples/bluetooth/nimble/blecent/main/main.c | 39 +++++++++++++++++-- 7 files changed, 150 insertions(+), 4 deletions(-) diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index 117461966d..36c4c2e8e6 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -794,6 +794,12 @@ config BT_NIMBLE_GATT_CACHING_DISABLE_AUTO help When client receives ATT out-of-sync error message, it will not automatically start the discovery procedure to correct the invalid cache. +config BT_NIMBLE_GATT_CACHING_ASSOC_ENABLE + bool "Enable association-based GATT caching" + depends on BT_NIMBLE_GATT_CACHING + default n + help + Enable this option to use associated address caching instead of performing service discovery. config BT_NIMBLE_WHITELIST_SIZE int "BLE white list size" diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index b04e3d1e96..db2f236ec1 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit b04e3d1e96b0e6c3f52982290fc35274d5fad24d +Subproject commit db2f236ec106d4dff8f282e28acc6f6798e3cfdb diff --git a/components/bt/host/nimble/port/include/esp_nimble_cfg.h b/components/bt/host/nimble/port/include/esp_nimble_cfg.h index 2d4879153f..01adb1bacf 100644 --- a/components/bt/host/nimble/port/include/esp_nimble_cfg.h +++ b/components/bt/host/nimble/port/include/esp_nimble_cfg.h @@ -196,6 +196,14 @@ #define MYNEWT_VAL_BLE_GATT_CACHING_DISABLE_AUTO (0) #endif +#ifndef MYNEWT_VAL_BLE_GATT_CACHING_ASSOC_ENABLE +#ifdef CONFIG_BT_NIMBLE_GATT_CACHING_ASSOC_ENABLE +#define MYNEWT_VAL_BLE_GATT_CACHING_ASSOC_ENABLE (CONFIG_BT_NIMBLE_GATT_CACHING_ASSOC_ENABLE) +#else +#define MYNEWT_VAL_BLE_GATT_CACHING_ASSOC_ENABLE (0) +#endif +#endif + #endif #ifndef MYNEWT_VAL_BLE_GATT_CSFC_SIZE diff --git a/examples/bluetooth/nimble/ble_cts/cts_cent/main/main.c b/examples/bluetooth/nimble/ble_cts/cts_cent/main/main.c index efe14e368e..0a99776f2c 100644 --- a/examples/bluetooth/nimble/ble_cts/cts_cent/main/main.c +++ b/examples/bluetooth/nimble/ble_cts/cts_cent/main/main.c @@ -426,6 +426,13 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg) } else { MODLOG_DFLT(INFO, "Connection secured\n"); } +#else +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } #else /* Perform service discovery */ rc = peer_disc_all(event->connect.conn_handle, @@ -434,6 +441,7 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } +#endif // BLE_GATT_CACHING_ASSOC_ENABLE #endif } else { /* Connection attempt failed; resume scanning. */ @@ -470,6 +478,13 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg) assert(rc == 0); print_conn_desc(&desc); #if CONFIG_EXAMPLE_ENCRYPTION +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } +#else /*** Go for service discovery after encryption has been successfully enabled ***/ rc = peer_disc_all(event->connect.conn_handle, ble_cts_cent_on_disc_complete, NULL); @@ -477,9 +492,27 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } +#endif // BLE_GATT_CACHING_ASSOC_ENABLE #endif return 0; + case BLE_GAP_EVENT_CACHE_ASSOC: +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + /* Cache association result for this connection */ + MODLOG_DFLT(INFO, "cache association; conn_handle=%d status=%d cache_state=%s\n", + event->cache_assoc.conn_handle, + event->cache_assoc.status, + (event->cache_assoc.cache_state == 0) ? "INVALID" : "LOADED"); + /* Perform service discovery */ + rc = peer_disc_all(event->connect.conn_handle, + blecent_on_disc_complete, NULL); + if(rc != 0) { + MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); + return 0; + } +#endif + return 0; + case BLE_GAP_EVENT_NOTIFY_RX: /* Peer sent us a notification or indication. */ MODLOG_DFLT(INFO, "received %s; conn_handle=%d attr_handle=%d " diff --git a/examples/bluetooth/nimble/ble_htp/htp_cent/main/main.c b/examples/bluetooth/nimble/ble_htp/htp_cent/main/main.c index 510139146e..2e47880916 100644 --- a/examples/bluetooth/nimble/ble_htp/htp_cent/main/main.c +++ b/examples/bluetooth/nimble/ble_htp/htp_cent/main/main.c @@ -540,6 +540,13 @@ ble_htp_cent_gap_event(struct ble_gap_event *event, void *arg) } else { MODLOG_DFLT(INFO, "Connection secured\n"); } +#else +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } #else /* Perform service discovery */ rc = peer_disc_all(event->connect.conn_handle, @@ -548,6 +555,7 @@ ble_htp_cent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } +#endif // BLE_GATT_CACHING_ASSOC_ENABLE #endif } else { /* Connection attempt failed; resume scanning. */ @@ -584,6 +592,13 @@ ble_htp_cent_gap_event(struct ble_gap_event *event, void *arg) assert(rc == 0); print_conn_desc(&desc); #if CONFIG_EXAMPLE_ENCRYPTION +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } +#else /*** Go for service discovery after encryption has been successfully enabled ***/ rc = peer_disc_all(event->connect.conn_handle, ble_htp_cent_on_disc_complete, NULL); @@ -591,9 +606,27 @@ ble_htp_cent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } +#endif // BLE_GATT_CACHING_ASSOC_ENABLE #endif return 0; + case BLE_GAP_EVENT_CACHE_ASSOC: +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + /* Cache association result for this connection */ + MODLOG_DFLT(INFO, "cache association; conn_handle=%d status=%d cache_state=%s\n", + event->cache_assoc.conn_handle, + event->cache_assoc.status, + (event->cache_assoc.cache_state == 0) ? "INVALID" : "LOADED"); + /* Perform service discovery */ + rc = peer_disc_all(event->connect.conn_handle, + blecent_on_disc_complete, NULL); + if(rc != 0) { + MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); + return 0; + } +#endif + return 0; + case BLE_GAP_EVENT_NOTIFY_RX: /* Peer sent us a notification or indication. */ MODLOG_DFLT(INFO, "received %s; conn_handle=%d attr_handle=%d " diff --git a/examples/bluetooth/nimble/ble_proximity_sensor/proximity_sensor_cent/main/main.c b/examples/bluetooth/nimble/ble_proximity_sensor/proximity_sensor_cent/main/main.c index 4c73890b93..0f6f9b309d 100644 --- a/examples/bluetooth/nimble/ble_proximity_sensor/proximity_sensor_cent/main/main.c +++ b/examples/bluetooth/nimble/ble_proximity_sensor/proximity_sensor_cent/main/main.c @@ -472,6 +472,13 @@ ble_prox_cent_gap_event(struct ble_gap_event *event, void *arg) } else { MODLOG_DFLT(INFO, "Connection secured\n"); } +#else +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } #else /* Perform service discovery */ rc = peer_disc_all(event->connect.conn_handle, @@ -480,6 +487,7 @@ ble_prox_cent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } +#endif // BLE_GATT_CACHING_ASSOC_ENABLE #endif } else { /* Connection attempt failed; resume scanning. */ @@ -532,6 +540,13 @@ ble_prox_cent_gap_event(struct ble_gap_event *event, void *arg) assert(rc == 0); print_conn_desc(&desc); #if CONFIG_EXAMPLE_ENCRYPTION +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } +#else /*** Go for service discovery after encryption has been successfully enabled ***/ rc = peer_disc_all(event->connect.conn_handle, ble_prox_cent_on_disc_complete, NULL); @@ -539,9 +554,27 @@ ble_prox_cent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } +#endif // BLE_GATT_CACHING_ASSOC_ENABLE #endif return 0; + case BLE_GAP_EVENT_CACHE_ASSOC: +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + /* Cache association result for this connection */ + MODLOG_DFLT(INFO, "cache association; conn_handle=%d status=%d cache_state=%s\n", + event->cache_assoc.conn_handle, + event->cache_assoc.status, + (event->cache_assoc.cache_state == 0) ? "INVALID" : "LOADED"); + /* Perform service discovery */ + rc = peer_disc_all(event->connect.conn_handle, + blecent_on_disc_complete, NULL); + if(rc != 0) { + MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); + return 0; + } +#endif + return 0; + case BLE_GAP_EVENT_NOTIFY_RX: /* Peer sent us a notification or indication. */ MODLOG_DFLT(INFO, "received %s; conn_handle=%d attr_handle=%d " diff --git a/examples/bluetooth/nimble/blecent/main/main.c b/examples/bluetooth/nimble/blecent/main/main.c index d0b0c65e28..b73ebe974f 100644 --- a/examples/bluetooth/nimble/blecent/main/main.c +++ b/examples/bluetooth/nimble/blecent/main/main.c @@ -789,6 +789,13 @@ blecent_gap_event(struct ble_gap_event *event, void *arg) } #else #if MYNEWT_VAL(BLE_GATTC) +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } +#else /* Perform service discovery */ rc = peer_disc_all(event->connect.conn_handle, blecent_on_disc_complete, NULL); @@ -796,8 +803,9 @@ blecent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } -#endif -#endif +#endif // BLE_GATT_CACHING_ASSOC_ENABLE +#endif // BLE_GATTC +#endif // EXAMPLE_ENCRYPTION } else { /* Connection attempt failed; resume scanning. */ MODLOG_DFLT(ERROR, "Error: Connection failed; status=%d\n", @@ -842,6 +850,13 @@ blecent_gap_event(struct ble_gap_event *event, void *arg) print_conn_desc(&desc); #if !MYNEWT_VAL(BLE_EATT_CHAN_NUM) #if CONFIG_EXAMPLE_ENCRYPTION && MYNEWT_VAL(BLE_GATTC) +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + rc = ble_gattc_cache_assoc(desc.peer_id_addr); + if (rc != 0) { + MODLOG_DFLT(ERROR, "Cache Association Failed; rc=%d\n", rc); + return 0; + } +#else /*** Go for service discovery after encryption has been successfully enabled ***/ rc = peer_disc_all(event->enc_change.conn_handle, blecent_on_disc_complete, NULL); @@ -849,10 +864,28 @@ blecent_gap_event(struct ble_gap_event *event, void *arg) MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } -#endif +#endif // BLE_GATT_CACHING_ASSOC_ENABLE +#endif // EXAMPLE_ENCRYPTION #endif return 0; + case BLE_GAP_EVENT_CACHE_ASSOC: +#if MYNEWT_VAL(BLE_GATT_CACHING_ASSOC_ENABLE) + /* Cache association result for this connection */ + MODLOG_DFLT(INFO, "cache association; conn_handle=%d status=%d cache_state=%s\n", + event->cache_assoc.conn_handle, + event->cache_assoc.status, + (event->cache_assoc.cache_state == 0) ? "INVALID" : "LOADED"); + /* Perform service discovery */ + rc = peer_disc_all(event->connect.conn_handle, + blecent_on_disc_complete, NULL); + if(rc != 0) { + MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); + return 0; + } +#endif + return 0; + case BLE_GAP_EVENT_NOTIFY_RX: /* Peer sent us a notification or indication. */ MODLOG_DFLT(INFO, "received %s; conn_handle=%d attr_handle=%d "