Shuta Nakamae / nRF51822

Fork of nRF51822 by Nordic Semiconductor

Revision:
195:061ed80ffbcf
Parent:
194:c99fc3160091
Child:
196:70f1b8354d57
diff -r c99fc3160091 -r 061ed80ffbcf btle/btle_gattc.cpp
--- a/btle/btle_gattc.cpp	Fri Jun 19 15:55:15 2015 +0100
+++ b/btle/btle_gattc.cpp	Fri Jun 19 15:55:15 2015 +0100
@@ -15,6 +15,7 @@
  */
 
 #include "btle_gattc.h"
+#include "UUID.h"
 
 #define BLE_DB_DISCOVERY_MAX_SRV          4  /**< Maximum number of services supported by this module. This also indicates the maximum number of users allowed to be registered to this module. (one user per service). */
 #define BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV 4  /**< Maximum number of characteristics per service supported by this module. */
@@ -25,27 +26,43 @@
 /**@brief Structure for holding information about the service and the characteristics found during
  *        the discovery process.
  */
-typedef struct
-{
-    ble_uuid_t               srvUUID;                                           /**< UUID of the service. */
+struct DiscoveredService {
+    ShortUUIDBytes_t         uuid;                                           /**< UUID of the service. */
     // uint8_t                  char_count;                                         /**< Number of characteristics present in the service. */
     // ble_db_discovery_char_t  charateristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];  /**< Array of information related to the characteristics present in the service. */
-    ble_gattc_handle_range_t handleRange;                                       /**< Service Handle Range. */
-} ble_db_discovery_srv_t;
+    Gap::Handle_t startHandle;    /**< Service Handle Range. */
+    Gap::Handle_t endHandle;      /**< Service Handle Range. */
+
+    DiscoveredService() {
+        /* empty */
+    }
+    DiscoveredService(ShortUUIDBytes_t uuidIn, Gap::Handle_t start, Gap::Handle_t end) {
+        setup(uuidIn, start, end);
+    }
+
+    void setup(ShortUUIDBytes_t uuidIn, Gap::Handle_t start, Gap::Handle_t end) {
+        uuid        = uuidIn;
+        startHandle = start;
+        endHandle   = end;
+    }
+};
 
 typedef struct
 {
-    ble_db_discovery_srv_t services[BLE_DB_DISCOVERY_MAX_SRV];  /**< Information related to the current service being discovered. This is intended for internal use during service discovery.*/
+    DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV];  /**< Information related to the current service being discovered. This is intended for internal use during service discovery.*/
     uint16_t               connHandle;                         /**< Connection handle as provided by the SoftDevice. */
     uint8_t                srvCount;                           /**< Number of services at the peers GATT database.*/
     // uint8_t                currCharInd;                       /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
     uint8_t                currSrvInd;                        /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
-    // bool                   discoveryInProgress;               /**< Variable to indicate if there is a service discovery in progress. */
+    bool                   discoveryInProgress;               /**< Variable to indicate if there is a service discovery in progress. */
 } ble_db_discovery_t;
 
+static ble_db_discovery_t  discoveryStatus;
+
 void launchServiceDiscovery(Gap::Handle_t connectionHandle)
 {
     // printf("connectionHandle %u\r\n", connectionHandle);
+    discoveryStatus.discoveryInProgress = true;
     printf("launch service discovery returned %u\r\n", sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL));
 }
 
@@ -56,32 +73,27 @@
             switch (p_ble_evt->evt.gattc_evt.gatt_status) {
                 case BLE_GATT_STATUS_SUCCESS: {
                     printf("count of primary services: %u\r\n", p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count);
-
-                    unsigned index;
-                    for (index = 0; index < p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count; index++) {
-                        printf("%x [%u %u]\r\n",
-                            p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].uuid.uuid,
-                            p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.start_handle,
-                            p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.end_handle);
+                    discoveryStatus.connHandle = p_ble_evt->evt.gattc_evt.conn_handle;
+                    discoveryStatus.currSrvInd = 0;
+                    discoveryStatus.srvCount   = p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count;
 
-                        ble_gattc_handle_range_t handleRange = {
-                            .start_handle = p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.start_handle,
-                            .end_handle   = p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.end_handle
-                        };
-                        printf("characteristics_discover returned %u\r\n",
-                               sd_ble_gattc_characteristics_discover(p_ble_evt->evt.gattc_evt.conn_handle, &handleRange));
+                    for (unsigned serviceIndex = 0; serviceIndex < discoveryStatus.srvCount; serviceIndex++) {
+                        discoveryStatus.services[serviceIndex].
+                            setup(p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[serviceIndex].uuid.uuid,
+                                  p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[serviceIndex].handle_range.start_handle,
+                                  p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[serviceIndex].handle_range.end_handle);
                     }
-                    printf("services discover returned %u\r\n",
-                        sd_ble_gattc_primary_services_discover(p_ble_evt->evt.gattc_evt.conn_handle,
-                                                               p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index -1].handle_range.end_handle,
-                                                               NULL));
-                        break;
+                    break;
                 }
+
                 case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: {
+                    discoveryStatus.discoveryInProgress = false;
                     printf("end of service discovery\r\n");
                     break;
                 }
+
                 default: {
+                    discoveryStatus.discoveryInProgress = false;
                     printf("gatt failure status: %u\r\n", p_ble_evt->evt.gattc_evt.gatt_status);
                     break;
                 }
@@ -97,4 +109,28 @@
             break;
         }
     }
+
+    if (!discoveryStatus.discoveryInProgress) {
+        return;
+    }
+
+    while (discoveryStatus.currSrvInd < discoveryStatus.srvCount) {
+        printf("%x [%u %u]\r\n",
+            p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[discoveryStatus.currSrvInd].uuid.uuid,
+            p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[discoveryStatus.currSrvInd].handle_range.start_handle,
+            p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[discoveryStatus.currSrvInd].handle_range.end_handle);
+
+        // ble_gattc_handle_range_t handleRange = {
+        //     .start_handle = p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[discoveryStatus.currSrvInd].handle_range.start_handle,
+        //     .end_handle   = p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[discoveryStatus.currSrvInd].handle_range.end_handle
+        // };
+        // printf("characteristics_discover returned %u\r\n",
+        //        sd_ble_gattc_characteristics_discover(p_ble_evt->evt.gattc_evt.conn_handle, &handleRange));
+
+        discoveryStatus.currSrvInd++;
+    }
+    printf("services discover returned %u\r\n",
+        sd_ble_gattc_primary_services_discover(p_ble_evt->evt.gattc_evt.conn_handle,
+                                               p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[discoveryStatus.currSrvInd -1].handle_range.end_handle,
+                                               NULL));
 }
\ No newline at end of file