This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

getting service count from database discovery module

I'm currently trying to use the database discovery module to get the total number of services and the number of discovered services so that I know when all of the services for a particular peripheral device are discovered and set up before discovering the next device. I'm doing this because from what I've found, you can't do things like sd_ble_gattc_write while a discovery is in progress.

The issue is that the srv_count and discoveries_count fields in ble_db_discovery_t don't seem to be updated correctly. The srv_count field is always zero, in fact I don't see it referenced anywhere but where it is defined in ble_db_discovery.h, and discoveries_count seems to be one less than the number of services registered using ble_db_discovery_evt_register

Am I missing something? Is there another way I can check if all the services have been discovered?

The services field is updated so I can iterate through that array to get total number of services and manually keep track of discoveries using the BLE_DB_DISCOVERY_COMPLETE event but, it would be nice to get my code working with the database discovery module

I'm using SDK v12.1 with a NRF52

EDIT 13 April 17

To add to this, the BLE_DB_DISCOVERY_AVAILABLE event is never triggered in db_discovery. I've fixed it as shown below. It's not thoroughy tested but, works so far. I've added it because the BLE_DB_DISCOVERY_COMPLETE event is generated before discovery_in_progress is set to false so, when i receive BLE_DB_DISCOVERY_AVAILABLE, i can start discovery of the next pending connection

diff --git a/v12.1/ble/ble_db_discovery/ble_db_discovery.c b/v12.1/ble/ble_db_discovery/ble_db_discovery.c
index ab9e98d..df43c0c 100644
--- a/v12.1/ble/ble_db_discovery/ble_db_discovery.c
+++ b/v12.1/ble/ble_db_discovery/ble_db_discovery.c
@@ -146,6 +146,27 @@ static void discovery_error_evt_trigger(ble_db_discovery_t * const p_db_discover
     }
 }
 
+static void discovery_available_evt_trigger(ble_db_discovery_t * const p_db_discovery,
+                                        uint32_t                   err_code,
+                                        uint16_t const             conn_handle)
+{
+    ble_db_discovery_evt_handler_t p_evt_handler;
+    ble_gatt_db_srv_t            * p_srv_being_discovered;
+
+    p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
+
+    p_evt_handler = registered_handler_get(&(p_srv_being_discovered->srv_uuid));
+
+    if (p_evt_handler != NULL)
+    {
+        ble_db_discovery_evt_t evt;
+
+        evt.conn_handle     = conn_handle;
+        evt.evt_type        = BLE_DB_DISCOVERY_AVAILABLE;
+        p_evt_handler(&evt);
+    }
+}
+
 
 /**@brief     Function for triggering a Discovery Complete or Service Not Found event to the
  *            application.
@@ -258,9 +279,11 @@ static void on_srv_disc_completion(ble_db_discovery_t * p_db_discovery,
             // Error with discovering the service.
             // Indicate the error to the registered user application.
             discovery_error_evt_trigger(p_db_discovery, err_code, conn_handle);
+            
+            discovery_available_evt_trigger(p_db_discovery, false, conn_handle);
 
-            m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
-            m_pending_user_evts[0].evt.conn_handle = conn_handle;
+            //m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
+            //m_pending_user_evts[0].evt.conn_handle = conn_handle;
 //            m_evt_handler(&m_pending_user_evts[0].evt);
 
             return;
@@ -270,8 +293,9 @@ static void on_srv_disc_completion(ble_db_discovery_t * p_db_discovery,
     {
         // No more service discovery is needed.
         p_db_discovery->discovery_in_progress  = false;
-        m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
-        m_pending_user_evts[0].evt.conn_handle = conn_handle;
+        //m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
+        //m_pending_user_evts[0].evt.conn_handle = conn_handle;
+        discovery_available_evt_trigger(p_db_discovery, false, conn_handle);
         //m_evt_handler(&m_pending_user_evts[0].evt);
     }
 }
@@ -552,8 +576,9 @@ static void on_primary_srv_discovery_rsp(ble_db_discovery_t * const    p_db_disc
                                         err_code,
                                         p_ble_gattc_evt->conn_handle);
 
-            m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
-            m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
+            discovery_available_evt_trigger(p_db_discovery, false, p_ble_gattc_evt->conn_handle);
+            //m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
+            //m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
             //m_evt_handler(&m_pending_user_evts[0].evt);
         }
     }
@@ -657,8 +682,9 @@ static void on_characteristic_discovery_rsp(ble_db_discovery_t * const    p_db_d
                                             err_code,
                                             p_ble_gattc_evt->conn_handle);
 
-                m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
-                m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
+                discovery_available_evt_trigger(p_db_discovery, false, p_ble_gattc_evt->conn_handle);
+                //m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
+                //m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
                 //m_evt_handler(&m_pending_user_evts[0].evt);
 
                 return;
@@ -690,8 +716,9 @@ static void on_characteristic_discovery_rsp(ble_db_discovery_t * const    p_db_d
                                         err_code,
                                         p_ble_gattc_evt->conn_handle);
 
-            m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
-            m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
+            discovery_available_evt_trigger(p_db_discovery, false, p_ble_gattc_evt->conn_handle);
+            //m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
+            //m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
             //m_evt_handler(&m_pending_user_evts[0].evt);
 
             return;
@@ -790,8 +817,9 @@ static void on_descriptor_discovery_rsp(ble_db_discovery_t * const    p_db_disco
                                         err_code,
                                         p_ble_gattc_evt->conn_handle);
 
-            m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
-            m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
+            discovery_available_evt_trigger(p_db_discovery, false, p_ble_gattc_evt->conn_handle);
+            //m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
+            //m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
 
             return;
         }
Related