No changes

Fork of nRF51822 by Nordic Semiconductor

Revision:
563:9c4b96f7be8d
Parent:
561:613dbbdeed27
Child:
565:cf03471a4ec4
--- a/source/nRF5xServiceDiscovery.h	Mon Jan 11 10:19:15 2016 +0000
+++ b/source/nRF5xServiceDiscovery.h	Mon Jan 11 10:19:16 2016 +0000
@@ -41,7 +41,6 @@
         gattc(gattcIn),
         serviceIndex(0),
         numServices(0),
-        characteristicIndex(0),
         numCharacteristics(0),
         state(INACTIVE),
         services(),
@@ -95,36 +94,14 @@
         terminateServiceDiscovery();
     }
 
-    virtual void onTermination(ServiceDiscovery::TerminationCallback_t callback) {
-        onTerminationCallback = callback;
+    void terminate(Gap::Handle_t connectionHandle) { 
+        if(connHandle == connectionHandle) { 
+            terminate();
+        }
     }
 
-    /**
-     * @brief  Clear nRF5xServiceDiscovery's state.
-     *
-     * @return
-     *           BLE_ERROR_NONE if successful.
-     */
-    virtual ble_error_t cleanup(void) {
-        /* Clear all state that is from the parent, including private members */
-        if (ServiceDiscovery::cleanup() != BLE_ERROR_NONE) {
-            return BLE_ERROR_INVALID_STATE;
-        }
-
-        /* Clear derived class members */
-        serviceIndex = 0;
-        numServices = 0;
-        characteristicIndex = 0;
-        numCharacteristics = 0;
-
-        state = INACTIVE;
-
-        serviceUUIDDiscoveryQueue.reset();
-        charUUIDDiscoveryQueue.reset();
-
-        onTerminationCallback = NULL;
-
-        return BLE_ERROR_NONE;
+    virtual void onTermination(ServiceDiscovery::TerminationCallback_t callback) {
+        onTerminationCallback = callback;
     }
 
 private:
@@ -139,6 +116,8 @@
     void removeFirstServiceNeedingUUIDDiscovery(void);
 
     void terminateServiceDiscovery(void) {
+        remainingCharacteristic = nRF5xDiscoveredCharacteristic();
+
         bool wasActive = isActive();
         state = INACTIVE;
 
@@ -147,8 +126,24 @@
         }
     }
 
-    void terminateCharacteristicDiscovery(void) {
+    void terminateCharacteristicDiscovery(ble_error_t err) {
         if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
+            if(remainingCharacteristic != nRF5xDiscoveredCharacteristic()) { 
+               if(err == BLE_ERROR_NONE) {
+                    // fullfill the last characteristic
+                    remainingCharacteristic.setLastHandle(services[serviceIndex].getEndHandle());
+
+                    if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
+                        ((matchingCharacteristicUUID == remainingCharacteristic.getUUID()) &&
+                         (matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
+                        if (characteristicCallback) {
+                            characteristicCallback(&remainingCharacteristic);
+                        }
+                    }
+               }
+               remainingCharacteristic = nRF5xDiscoveredCharacteristic();
+            }
+
             state = SERVICE_DISCOVERY_ACTIVE;
         }
         serviceIndex++; /* Progress service index to keep discovery alive. */
@@ -162,7 +157,6 @@
 
     void resetDiscoveredCharacteristics(void) {
         numCharacteristics  = 0;
-        characteristicIndex = 0;
     }
 
 private:
@@ -309,7 +303,6 @@
 private:
     uint8_t  serviceIndex;        /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
     uint8_t  numServices;         /**< Number of services at the peers GATT database.*/
-    uint8_t  characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
     uint8_t  numCharacteristics;  /**< Number of characteristics within the service.*/
 
     enum State_t {
@@ -328,6 +321,8 @@
     CharUUIDDiscoveryQueue      charUUIDDiscoveryQueue;
 
     TerminationCallback_t       onTerminationCallback;
+
+    nRF5xDiscoveredCharacteristic remainingCharacteristic;
 };
 
 #endif /*__NRF_SERVICE_DISCOVERY_H__*/
\ No newline at end of file