From 8bcef4e2413d12547e5fb90c2e4e811015d5ee36 Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Wed, 17 Jan 2024 14:24:59 +0530 Subject: [PATCH] Added API to get current stack operation status --- nimble/host/include/host/ble_esp_gap.h | 21 +++++ nimble/host/include/host/ble_gatt.h | 6 ++ nimble/host/src/ble_gap.c | 102 +++++++++++++++++++++++++ nimble/host/src/ble_gatts.c | 6 ++ nimble/host/src/ble_hs_resolv.c | 2 +- 5 files changed, 136 insertions(+), 1 deletion(-) diff --git a/nimble/host/include/host/ble_esp_gap.h b/nimble/host/include/host/ble_esp_gap.h index 64aa1104d..0abfbd057 100644 --- a/nimble/host/include/host/ble_esp_gap.h +++ b/nimble/host/include/host/ble_esp_gap.h @@ -11,6 +11,19 @@ extern "C" { #endif +enum gap_status { + BLE_GAP_STATUS_ADV = 0, + BLE_GAP_STATUS_EXT_ADV, + BLE_GAP_STATUS_SCAN, + BLE_GAP_STATUS_CONN, + BLE_GAP_STATUS_PAIRED, + BLE_GAP_STATUS_GATTS, + BLE_GAP_STATUS_HOST_PRIVACY, + BLE_GAP_STATUS_PERIODIC, +}; + +typedef enum gap_status gap_status_t; + #define BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_ADV_ADDR 0 #define BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_LINK_ID 1 #define BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_BEACON_TYPE 2 @@ -91,6 +104,14 @@ int ble_gap_wl_tx_clear(void); */ int ble_gap_wl_read_size(uint8_t *size); +/** + * This API gives the current status of various stack operations + * + * @return 0 on success; nonzero bits indicating different + * operations as per enum gap_status. + */ +int ble_gap_host_check_status(void); + #if MYNEWT_VAL(BLE_HCI_VS) #if MYNEWT_VAL(BLE_POWER_CONTROL) diff --git a/nimble/host/include/host/ble_gatt.h b/nimble/host/include/host/ble_gatt.h index fedfabb27..6ecde7a55 100644 --- a/nimble/host/include/host/ble_gatt.h +++ b/nimble/host/include/host/ble_gatt.h @@ -1394,6 +1394,12 @@ int ble_gatts_peer_cl_sup_feat_get(uint16_t conn_handle, uint8_t *out_supported_ #if MYNEWT_VAL(BLE_GATT_CACHING) int ble_gatts_calculate_hash(uint8_t *out_hash_key); #endif + +/** + * Returns the current number of configured characteristics + */ +int ble_gatts_get_cfgable_chrs(void); + #ifdef __cplusplus } #endif diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c index 94544f069..b7acb7bc6 100644 --- a/nimble/host/src/ble_gap.c +++ b/nimble/host/src/ble_gap.c @@ -31,6 +31,9 @@ #include "ble_gattc_cache_priv.h" #endif +#include "host/ble_hs_pvcy.h" +#include "host/util/util.h" + #ifndef min #define min(a, b) ((a) < (b) ? (a) : (b)) #endif @@ -7792,3 +7795,102 @@ ble_gap_deinit(void) { ble_npl_mutex_deinit(&preempt_done_mutex); } + +int +ble_gap_host_check_status(void) +{ + int status = 0; + + uint16_t conn_handle = 0; +#if MYNEWT_VAL(BLE_PERIODIC_ADV) + uint16_t sync_handle = 0; +#endif + struct ble_hs_conn *conn; + ble_addr_t oldest_peer_id_addr[MYNEWT_VAL(BLE_STORE_MAX_BONDS)]; + int num_peers; + + /* Stop Advertising */ +#if MYNEWT_VAL(BLE_EXT_ADV) + int i; + + for (i=0; i< BLE_ADV_INSTANCES; i++) { + if (ble_gap_ext_adv_active(i)) { + BLE_HS_LOG(ERROR, "Ext adv in progress for instance %d \n", i); + status |= BIT(BLE_GAP_STATUS_EXT_ADV); + break; + } + } + +#else + if (ble_gap_adv_active_instance(0)) { + BLE_HS_LOG(ERROR, "Gap Advertising is active \n"); + status |= BIT(BLE_GAP_STATUS_ADV); + } +#endif + + /* Stop scanning */ + if (ble_gap_disc_active()) { + BLE_HS_LOG(ERROR, "Scanning in progress \n"); + status |= BIT(BLE_GAP_STATUS_SCAN); + } + + /* Terminate links */ + for (conn_handle=0; conn_handle<50; conn_handle++) { + ble_hs_lock(); + + conn = ble_hs_conn_find(conn_handle); + + ble_hs_unlock(); + if (conn != NULL) { + BLE_HS_LOG(ERROR, "Connection exists \n"); + status |= BIT(BLE_GAP_STATUS_CONN); + break; + } + } + + /* Check for paired devices*/ + ble_store_util_bonded_peers(&oldest_peer_id_addr[0], + &num_peers, MYNEWT_VAL(BLE_STORE_MAX_BONDS)); + + if (num_peers) { + BLE_HS_LOG(ERROR, "Unpaired device exists\n"); + status |= BIT(BLE_GAP_STATUS_PAIRED); + } + + /* gatts reset */ + if (ble_gatts_get_cfgable_chrs()) { + BLE_HS_LOG(ERROR, "Gatt reset not done \n"); + status |= BIT(BLE_GAP_STATUS_GATTS); + } + + /* Check if privacy is disabled */ +#if MYNEWT_VAL(BLE_HOST_BASED_PRIVACY) + extern int is_ble_hs_resolv_enabled(void); + + if (is_ble_hs_resolv_enabled()) { + BLE_HS_LOG(ERROR, "Host based Privacy not disabled \n"); + status |= BIT(BLE_GAP_STATUS_HOST_PRIVACY); + } +#endif + + /* Periodic adv termination */ +#if MYNEWT_VAL(BLE_PERIODIC_ADV) + struct ble_hs_periodic_sync *psync; + + for (sync_handle=0; sync_handle<50; sync_handle++) { + ble_hs_lock(); + + psync = ble_hs_periodic_sync_find_by_handle(sync_handle); + + ble_hs_unlock(); + + if (psync) { + BLE_HS_LOG(ERROR, "Periodic adv not disabled \n"); + status |= BIT(BLE_GAP_STATUS_PERIODIC); + break; + } + } +#endif + + return status; +} diff --git a/nimble/host/src/ble_gatts.c b/nimble/host/src/ble_gatts.c index 4643fe29c..56715ee45 100644 --- a/nimble/host/src/ble_gatts.c +++ b/nimble/host/src/ble_gatts.c @@ -3149,6 +3149,12 @@ ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs) return 0; } +int +ble_gatts_get_cfgable_chrs(void) +{ + return ble_gatts_num_cfgable_chrs; +} + void ble_gatts_lcl_svc_foreach(ble_gatt_svc_foreach_fn cb, void *arg) { diff --git a/nimble/host/src/ble_hs_resolv.c b/nimble/host/src/ble_hs_resolv.c index f4b874619..6fcbc051a 100644 --- a/nimble/host/src/ble_hs_resolv.c +++ b/nimble/host/src/ble_hs_resolv.c @@ -327,7 +327,7 @@ ble_rpa_replace_peer_params_with_rl(uint8_t *peer_addr, uint8_t *addr_type, * * @return uint8_t */ -static uint8_t +uint8_t is_ble_hs_resolv_enabled(void) { return g_ble_hs_resolv_data.addr_res_enabled;