aa

Dependents:   Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal

Fork of nRF51822 by Nordic Semiconductor

Revision:
290:83c994bf62d0
Parent:
287:faa0dc7ae513
Child:
292:01b717803437
--- a/btle/btle_discovery.cpp	Fri Jun 19 15:55:27 2015 +0100
+++ b/btle/btle_discovery.cpp	Fri Jun 19 15:55:27 2015 +0100
@@ -25,6 +25,9 @@
 
 static NordicServiceDiscovery sdSingleton;
 
+static unsigned numServicesNeedingUUIDDiscovery;
+static int      servicesNeedingUUIDDiscovery[NordicServiceDiscovery::BLE_DB_DISCOVERY_MAX_SRV];
+
 ble_error_t
 ServiceDiscovery::launch(Gap::Handle_t             connectionHandle,
                          ServiceCallback_t         sc,
@@ -107,6 +110,56 @@
 }
 
 void
+NordicServiceDiscovery::triggerServiceUUIDDiscovery(void)
+{
+    while (numServicesNeedingUUIDDiscovery) {
+        sdSingleton.state = DISCOVER_SERVICE_UUIDS;
+
+        unsigned serviceIndex = servicesNeedingUUIDDiscovery[0];
+        ble_uuid_t uuid = {
+            .uuid = BLE_UUID_SERVICE_PRIMARY,
+            .type = BLE_UUID_TYPE_BLE,
+        };
+        ble_gattc_handle_range_t handleRange = {
+            .start_handle = services[serviceIndex].getStartHandle(),
+            .end_handle   = services[serviceIndex].getEndHandle(),
+        };
+        if (sd_ble_gattc_char_value_by_uuid_read(connHandle, &uuid, &handleRange) != NRF_SUCCESS) {
+            removeFirstServiceNeedingUUIDDiscovery();
+            continue;
+        }
+
+        return;
+    }
+
+    if (sdSingleton.state == DISCOVER_SERVICE_UUIDS) {
+        sdSingleton.state = SERVICE_DISCOVERY_ACTIVE;
+    }
+}
+
+void
+NordicServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response)
+{
+    printf("BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP: count %u, len %u\r\n", response->count, response->value_len);
+    for (unsigned i = 0; i < response->value_len; i++) {
+        printf("%02x ", response->handle_value[0].p_value[i]);
+    }
+    printf("\r\n");
+
+    removeFirstServiceNeedingUUIDDiscovery();
+    triggerServiceUUIDDiscovery();
+}
+
+void
+NordicServiceDiscovery::removeFirstServiceNeedingUUIDDiscovery(void)
+{
+    numServicesNeedingUUIDDiscovery--;
+    for (unsigned serviceIndex = 0; serviceIndex < numServicesNeedingUUIDDiscovery; serviceIndex++) {
+        servicesNeedingUUIDDiscovery[serviceIndex] = servicesNeedingUUIDDiscovery[serviceIndex + 1];
+    }
+}
+
+void
 NordicServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response)
 {
     serviceIndex = 0;
@@ -117,15 +170,23 @@
         numServices = BLE_DB_DISCOVERY_MAX_SRV;
     }
 
+    numServicesNeedingUUIDDiscovery = 0;
+    for (unsigned serviceIndex = 0; serviceIndex < BLE_DB_DISCOVERY_MAX_SRV; serviceIndex++) {
+        servicesNeedingUUIDDiscovery[serviceIndex] = -1;
+    }
     for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) {
         if (response->services[serviceIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
-            printf("service[0] uuid type %u\r\n", response->services[0].uuid.type);
+            servicesNeedingUUIDDiscovery[numServicesNeedingUUIDDiscovery++] = serviceIndex;
         }
-        // sd_ble_gattc_char_value_by_uuid_read
         services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid,
                                      response->services[serviceIndex].handle_range.start_handle,
                                      response->services[serviceIndex].handle_range.end_handle);
     }
+
+    /* Trigger discovery of service UUID if necessary. */
+    if (numServicesNeedingUUIDDiscovery) {
+        triggerServiceUUIDDiscovery();
+    }
 }
 
 void
@@ -248,6 +309,13 @@
                     break;
             }
             break;
+        case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP: {
+            if (sdSingleton.isActive()) {
+                sdSingleton.processDiscoverUUIDResponse(&p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp);
+            }
+
+            break;
+        }
     }
 
     sdSingleton.progressCharacteristicDiscovery();