nordic

Fork of nRF51822 by Nordic Semiconductor

Revision:
240:693a1f145b5a
Child:
241:75b69581d1dd
diff -r 6e85a2484de7 -r 693a1f145b5a btle/btle_discovery.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/btle/btle_discovery.cpp	Fri Jun 19 15:55:21 2015 +0100
@@ -0,0 +1,99 @@
+#include "blecommon.h"
+#include "UUID.h"
+#include "Gap.h"
+#include "nrf_error.h"
+#include "btle_discovery.h"
+#include "ble_err.h"
+
+ServiceDiscovery *ServiceDiscovery::getSingleton(void) {
+    static ServiceDiscovery discoverySingleton;
+
+    return &discoverySingleton;
+}
+
+ble_error_t
+ServiceDiscovery::launch(Gap::Handle_t connectionHandle, ServiceCallback_t sc, CharacteristicCallback_t cc)
+{
+    ServiceDiscovery *singleton = getSingleton();
+    singleton->serviceDiscoveryStarted(connectionHandle);
+
+    uint32_t rc;
+    if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) {
+        singleton->terminate();
+        switch (rc) {
+            case NRF_ERROR_INVALID_PARAM:
+            case BLE_ERROR_INVALID_CONN_HANDLE:
+                return BLE_ERROR_INVALID_PARAM;
+            case NRF_ERROR_BUSY:
+                return BLE_STACK_BUSY;
+            default:
+            case NRF_ERROR_INVALID_STATE:
+                return BLE_ERROR_INVALID_STATE;
+        }
+    }
+
+    return BLE_ERROR_NONE;
+}
+
+ble_error_t ServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle) {
+    ServiceDiscovery *singleton = getSingleton();
+    singleton->characteristicDiscoveryStarted(connectionHandle);
+
+    ble_gattc_handle_range_t handleRange = {
+        .start_handle = startHandle,
+        .end_handle   = endHandle
+    };
+    uint32_t rc;
+    if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
+        singleton->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;
+        }
+    }
+
+    return BLE_ERROR_NONE;
+}
+
+void
+ServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response)
+{
+    currSrvInd = 0;
+    srvCount   = response->count;
+
+    /* Account for the limitation on the number of discovered services we can handle at a time. */
+    if (srvCount > BLE_DB_DISCOVERY_MAX_SRV) {
+        srvCount = BLE_DB_DISCOVERY_MAX_SRV;
+    }
+
+    for (unsigned serviceIndex = 0; serviceIndex < srvCount; serviceIndex++) {
+        services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid,
+                                     response->services[serviceIndex].handle_range.start_handle,
+                                     response->services[serviceIndex].handle_range.end_handle);
+    }
+}
+
+void
+ServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
+{
+    currCharInd = 0;
+    charCount   = response->count;
+
+    /* Account for the limitation on the number of discovered characteristics we can handle at a time. */
+    if (charCount > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) {
+        charCount = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV;
+    }
+
+    for (unsigned charIndex = 0; charIndex < charCount; charIndex++) {
+        characteristics[charIndex].setup(response->chars[charIndex].uuid.uuid,
+                                         *(const uint8_t *)(&response->chars[charIndex].char_props),
+                                         response->chars[charIndex].handle_decl,
+                                         response->chars[charIndex].handle_value);
+    }
+}
\ No newline at end of file