No changes

Fork of nRF51822 by Nordic Semiconductor

Revision:
570:f162898cb6c4
Parent:
567:e1800bd55a9e
Child:
590:3bdd5346ded1
--- a/source/nRF5xServiceDiscovery.cpp	Mon Jan 11 10:19:20 2016 +0000
+++ b/source/nRF5xServiceDiscovery.cpp	Mon Jan 11 10:19:21 2016 +0000
@@ -27,32 +27,22 @@
         .start_handle = startHandle,
         .end_handle   = endHandle
     };
-    uint32_t rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange);
-    ble_error_t err = BLE_ERROR_NONE;
-
-    switch (rc) {
-        case NRF_SUCCESS:
-            err = BLE_ERROR_NONE;
-            break;
-        case BLE_ERROR_INVALID_CONN_HANDLE:
-        case NRF_ERROR_INVALID_ADDR:
-            err = BLE_ERROR_INVALID_PARAM;
-            break;
-        case NRF_ERROR_BUSY:
-            err = BLE_STACK_BUSY;
-            break;
-        case NRF_ERROR_INVALID_STATE:
-            err = BLE_ERROR_INVALID_STATE;
-            break;
-        default:
-            err = BLE_ERROR_UNSPECIFIED;
-            break;
+    uint32_t rc;
+    if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
+        terminateCharacteristicDiscovery();
+        switch (rc) {
+            case BLE_ERROR_INVALID_CONN_HANDLE:
+            case NRF_ERROR_INVALID_ADDR:
+                return BLE_ERROR_INVALID_PARAM;
+            case NRF_ERROR_BUSY:
+                return BLE_STACK_BUSY;
+            default:
+            case NRF_ERROR_INVALID_STATE:
+                return BLE_ERROR_INVALID_STATE;
+        }
     }
 
-    if (err) {
-        terminateCharacteristicDiscovery(err);
-    }
-    return err;
+    return BLE_ERROR_NONE;
 }
 
 void
@@ -88,6 +78,7 @@
 void
 nRF5xServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
 {
+    characteristicIndex = 0;
     numCharacteristics  = response->count;
 
     /* Account for the limitation on the number of discovered characteristics we can handle at a time. */
@@ -123,61 +114,37 @@
 void
 nRF5xServiceDiscovery::progressCharacteristicDiscovery(void)
 {
-    if (state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
-        return;
-    }
-
-    if ((discoveredCharacteristic != nRF5xDiscoveredCharacteristic()) && (numCharacteristics > 0)) {
-        discoveredCharacteristic.setLastHandle(characteristics[0].getDeclHandle() - 1);
-
+    /* Iterate through the previously discovered characteristics cached in characteristics[]. */
+    while ((state == CHARACTERISTIC_DISCOVERY_ACTIVE) && (characteristicIndex < numCharacteristics)) {
         if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
-            ((matchingCharacteristicUUID == discoveredCharacteristic.getUUID()) &&
+            ((matchingCharacteristicUUID == characteristics[characteristicIndex].getUUID()) &&
              (matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
             if (characteristicCallback) {
-                characteristicCallback(&discoveredCharacteristic);
+                characteristicCallback(&characteristics[characteristicIndex]);
             }
         }
-    }
-
-    for (uint8_t i = 0; i < numCharacteristics; ++i) {
-        if (state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
-            return;
-        }
-
-        if (i == (numCharacteristics - 1)) {
-            discoveredCharacteristic = characteristics[i];
-            break;
-        } else {
-            characteristics[i].setLastHandle(characteristics[i + 1].getDeclHandle() - 1);
-        }
 
-        if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
-            ((matchingCharacteristicUUID == characteristics[i].getUUID()) &&
-             (matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
-            if (characteristicCallback) {
-                characteristicCallback(&characteristics[i]);
-            }
-        }
-    }
-
-    if (state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
-        return;
+        characteristicIndex++;
     }
 
-    Gap::Handle_t startHandle = (numCharacteristics > 0) ? characteristics[numCharacteristics - 1].getValueHandle() + 1 : SRV_DISC_END_HANDLE;
-    Gap::Handle_t endHandle   = services[serviceIndex].getEndHandle();
-    resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */
+    /* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */
+    if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
+        /* Determine the ending handle of the last cached characteristic. */
+        Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1;
+        Gap::Handle_t endHandle   = services[serviceIndex].getEndHandle();
+        resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */
 
-    if (startHandle < endHandle) {
-        ble_gattc_handle_range_t handleRange = {
-            .start_handle = startHandle,
-            .end_handle   = endHandle
-        };
-        if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
-            terminateCharacteristicDiscovery(BLE_ERROR_UNSPECIFIED);
+        if (startHandle < endHandle) {
+            ble_gattc_handle_range_t handleRange = {
+                .start_handle = startHandle,
+                .end_handle   = endHandle
+            };
+            if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
+                terminateCharacteristicDiscovery();
+            }
+        } else {
+            terminateCharacteristicDiscovery();
         }
-    } else {
-        terminateCharacteristicDiscovery(BLE_ERROR_NONE);
     }
 }