High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
rgrover1
Date:
Fri Jun 19 15:52:58 2015 +0100
Parent:
641:5108db1d49c2
Child:
643:fa85e59ad928
Commit message:
Synchronized with git rev a7c6dc1a
Author: Rohit Grover
one massive commit to move towards GattClient.

Changed in this revision

common/DiscoveredCharacteristic.cpp Show annotated file Show diff for this revision Revisions of this file
public/BLEDevice.h Show annotated file Show diff for this revision Revisions of this file
public/DiscoveredCharacteristic.h Show annotated file Show diff for this revision Revisions of this file
public/GattClient.h Show annotated file Show diff for this revision Revisions of this file
public/ServiceDiscovery.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/DiscoveredCharacteristic.cpp	Fri Jun 19 15:52:58 2015 +0100
@@ -0,0 +1,71 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "DiscoveredCharacteristic.h"
+#include "GattClient.h"
+
+DiscoveredCharacteristic::ReadCallback_t DiscoveredCharacteristic::onDataReadCallback;
+
+/**
+ * Initiate (or continue) a read for the value attribute, optionally at a
+ * given offset. If the Characteristic or Descriptor to be read is longer
+ * than ATT_MTU - 1, this function must be called multiple times with
+ * appropriate offset to read the complete value.
+ *
+ * @return BLE_ERROR_NONE if a read has been initiated, else
+ *         BLE_ERROR_INVALID_STATE if some internal state about the connection is invalid, or
+ *         BLE_STACK_BUSY if some client procedure already in progress, or
+ *         BLE_ERROR_OPERATION_NOT_PERMITTED due to the characteristic's properties.
+ */
+ble_error_t
+DiscoveredCharacteristic::read(uint16_t offset) const
+{
+    if (!props.read()) {
+        return BLE_ERROR_OPERATION_NOT_PERMITTED;
+    }
+
+    return gattc->read(connHandle, valueHandle, offset);
+}
+
+/**
+ * Perform a write without response procedure.
+ *
+ * @param  length
+ *           The amount of data being written.
+ * @param  value
+ *           The bytes being written.
+ *
+ * @note   It is important to note that a write without response will
+ *         <b>consume an application buffer</b>, and will therefore
+ *         generate a onDataSent() callback when the packet has been
+ *         transmitted. The application should ensure that the buffer is
+ *         valid until the callback.
+ *
+ * @retval BLE_ERROR_NONE Successfully started the Write procedure, else
+ *         BLE_ERROR_INVALID_STATE if some internal state about the connection is invalid, or
+ *         BLE_STACK_BUSY if some client procedure already in progress, or
+ *         BLE_ERROR_NO_MEM if there are no available buffers left to process the request, or
+ *         BLE_ERROR_OPERATION_NOT_PERMITTED due to the characteristic's properties.
+ */
+ble_error_t
+DiscoveredCharacteristic::writeWoResponse(uint16_t length, const uint8_t *value) const
+{
+    if (!props.writeWoResp()) {
+        return BLE_ERROR_OPERATION_NOT_PERMITTED;
+    }
+
+    return gattc->write(GattClient::OP_WRITE_CMD, connHandle, length, value);
+}
\ No newline at end of file
--- a/public/BLEDevice.h	Fri Jun 19 15:52:57 2015 +0100
+++ b/public/BLEDevice.h	Fri Jun 19 15:52:58 2015 +0100
@@ -638,6 +638,77 @@
      */
     ble_error_t purgeAllBondingState(void);
 
+    /**
+     * Launch service discovery. Once launched, service discovery will remain
+     * active with callbacks being issued back into the application for matching
+     * services/characteristics. isActive() can be used to determine status; and
+     * a termination callback (if setup) will be invoked at the end. Service
+     * discovery can be terminated prematurely if needed using terminate().
+     *
+     * @param  connectionHandle
+     *           Handle for the connection with the peer.
+     * @param  sc
+     *           This is the application callback for matching service. Taken as
+     *           NULL by default. Note: service discovery may still be active
+     *           when this callback is issued; calling asynchronous BLE-stack
+     *           APIs from within this application callback might cause the
+     *           stack to abort service discovery. If this becomes an issue, it
+     *           may be better to make local copy of the discoveredService and
+     *           wait for service discovery to terminate before operating on the
+     *           service.
+     * @param  cc
+     *           This is the application callback for matching characteristic.
+     *           Taken as NULL by default. Note: service discovery may still be
+     *           active when this callback is issued; calling asynchronous
+     *           BLE-stack APIs from within this application callback might cause
+     *           the stack to abort service discovery. If this becomes an issue,
+     *           it may be better to make local copy of the discoveredCharacteristic
+     *           and wait for service discovery to terminate before operating on the
+     *           characteristic.
+     * @param  matchingServiceUUID
+     *           UUID based filter for specifying a service in which the application is
+     *           interested. By default it is set as the wildcard UUID_UNKNOWN,
+     *           in which case it matches all services. If characteristic-UUID
+     *           filter (below) is set to the wildcard value, then a service
+     *           callback will be invoked for the matching service (or for every
+     *           service if the service filter is a wildcard).
+     * @param  matchingCharacteristicUUIDIn
+     *           UUID based filter for specifying characteristic in which the application
+     *           is interested. By default it is set as the wildcard UUID_UKNOWN
+     *           to match against any characteristic. If both service-UUID
+     *           filter and characteristic-UUID filter are used with non- wildcard
+     *           values, then only a single characteristic callback is
+     *           invoked for the matching characteristic.
+     *
+     * @Note     Using wildcard values for both service-UUID and characteristic-
+     *           UUID will result in complete service discovery--callbacks being
+     *           called for every service and characteristic.
+     *
+     * @return
+     *           BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error.
+     */
+    ble_error_t launchServiceDiscovery(Gap::Handle_t                               connectionHandle,
+                                       ServiceDiscovery::ServiceCallback_t         sc = NULL,
+                                       ServiceDiscovery::CharacteristicCallback_t  cc = NULL,
+                                       const UUID                                 &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
+                                       const UUID                                 &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
+
+    /**
+     * Setup callback for when serviceDiscovery terminates.
+     */
+    void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback);
+
+    /**
+     * Is service-discovery currently active?
+     */
+    bool isServiceDiscoveryActive(void);
+
+    /**
+     * Terminate an ongoing service-discovery. This should result in an
+     * invocation of the TerminationCallback if service-discovery is active.
+     */
+    void terminateServiceDiscovery(void);
+
 public:
     BLEDevice() : transport(createBLEDeviceInstance()), advParams(), advPayload(), scanResponse(), needToSetAdvPayload(true), scanningParams() {
         advPayload.clear();
@@ -1129,4 +1200,40 @@
     return transport->getGap().purgeAllBondingState();
 }
 
+inline ble_error_t
+BLEDevice::launchServiceDiscovery(Gap::Handle_t                               connectionHandle,
+                                  ServiceDiscovery::ServiceCallback_t         sc,
+                                  ServiceDiscovery::CharacteristicCallback_t  cc,
+                                  const UUID                                 &matchingServiceUUID,
+                                  const UUID                                 &matchingCharacteristicUUID)
+{
+    return transport->getGattClient().launchServiceDiscovery(connectionHandle, sc, cc, matchingServiceUUID, matchingCharacteristicUUID);
+}
+
+inline void
+BLEDevice::onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback)
+{
+    transport->getGattClient().onServiceDiscoveryTermination(callback);
+}
+
+/**
+ * Is service-discovery currently active?
+ */
+inline bool
+BLEDevice::isServiceDiscoveryActive(void)
+{
+    return transport->getGattClient().isServiceDiscoveryActive();
+}
+
+/**
+ * Terminate an ongoing service-discovery. This should result in an
+ * invocation of the TerminationCallback if service-discovery is active.
+ */
+inline void
+BLEDevice::terminateServiceDiscovery(void)
+{
+    transport->getGattClient().terminateServiceDiscovery();
+}
+
+
 #endif // ifndef __BLE_DEVICE__
\ No newline at end of file
--- a/public/DiscoveredCharacteristic.h	Fri Jun 19 15:52:57 2015 +0100
+++ b/public/DiscoveredCharacteristic.h	Fri Jun 19 15:52:58 2015 +0100
@@ -18,8 +18,10 @@
 #define __DISCOVERED_CHARACTERISTIC_H__
 
 #include "UUID.h"
+#include "Gap.h"
 #include "GattAttribute.h"
-#include "GattClient.h"
+
+class GattClient; /* forward declaration */
 
 /**
  * Structure for holding information about the service and the characteristics
@@ -69,13 +71,7 @@
      *         BLE_STACK_BUSY if some client procedure already in progress, or
      *         BLE_ERROR_OPERATION_NOT_PERMITTED due to the characteristic's properties.
      */
-    ble_error_t read(uint16_t offset = 0) const {
-        if (!props.read()) {
-            return BLE_ERROR_OPERATION_NOT_PERMITTED;
-        }
-
-        return gattc->read(connHandle, valueHandle, offset);
-    }
+    ble_error_t read(uint16_t offset = 0) const;
 
     /**
      * Perform a write without response procedure.
@@ -97,14 +93,7 @@
      *         BLE_ERROR_NO_MEM if there are no available buffers left to process the request, or
      *         BLE_ERROR_OPERATION_NOT_PERMITTED due to the characteristic's properties.
      */
-    ble_error_t writeWoResponse(uint16_t length, const uint8_t *value) const {
-        if (!props.writeWoResp()) {
-            return BLE_ERROR_OPERATION_NOT_PERMITTED;
-        }
-
-        return BLE_ERROR_NONE;
-        // return (ble.getGattClient())->write(BLE_GATT_OP_WRITE_CMD, connHandle, length, value);
-    }
+    ble_error_t writeWoResponse(uint16_t length, const uint8_t *value) const;
 
     static void setupOnDataRead(ReadCallback_t callback) {
         onDataReadCallback = callback;
--- a/public/GattClient.h	Fri Jun 19 15:52:57 2015 +0100
+++ b/public/GattClient.h	Fri Jun 19 15:52:58 2015 +0100
@@ -20,12 +20,66 @@
 #include "blecommon.h"
 #include "Gap.h"
 #include "GattAttribute.h"
+#include "ServiceDiscovery.h"
 
 class GattClient {
 public:
+    enum Command_t {
+        OP_WRITE_CMD
+    };
+
+public:
+    /**
+     * Launch service discovery. Once launched, service discovery will remain
+     * active with callbacks being issued back into the application for matching
+     * services/characteristics. isActive() can be used to determine status; and
+     * a termination callback (if setup) will be invoked at the end. Service
+     * discovery can be terminated prematurely if needed using terminate().
+     *
+     * @param  connectionHandle
+     *           Handle for the connection with the peer.
+     * @param  sc
+     *           This is the application callback for matching service.
+     * @param  cc
+     *           This is the application callback for matching characteristic.
+     * @param  matchingServiceUUID
+     *           UUID based filter for specifying a service in which the application is
+     *           interested.
+     * @param  matchingCharacteristicUUIDIn
+     *           UUID based filter for specifying characteristic in which the application
+     *           is interested.
+     *
+     * @Note     Using wildcard values for both service-UUID and characteristic-
+     *           UUID will result in complete service discovery--callbacks being
+     *           called for every service and characteristic.
+     *
+     * @return
+     *           BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error.
+     */
+    virtual ble_error_t launchServiceDiscovery(Gap::Handle_t                               connectionHandle,
+                                               ServiceDiscovery::ServiceCallback_t         sc                           = NULL,
+                                               ServiceDiscovery::CharacteristicCallback_t  cc                           = NULL,
+                                               const UUID                                 &matchingServiceUUID          = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
+                                               const UUID                                 &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) = 0;
+
+    virtual void        onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) = 0;
+
+    /**
+     * Is service-discovery currently active?
+     */
+    virtual bool isServiceDiscoveryActive(void) const = 0;
+
+    /**
+     * Terminate an ongoing service-discovery. This should result in an
+     * invocation of the TerminationCallback if service-discovery is active.
+     */
+    virtual void terminateServiceDiscovery(void) = 0;
+
     /* Initiate a Gatt Client read procedure by attribute-handle.*/
     virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const = 0;
 
+    virtual ble_error_t write(GattClient::Command_t cmd, Gap::Handle_t connHandle, size_t length, const uint8_t *value) const = 0;
+
 #if 0
 public:
     /* Event callback handlers. */
--- a/public/ServiceDiscovery.h	Fri Jun 19 15:52:57 2015 +0100
+++ b/public/ServiceDiscovery.h	Fri Jun 19 15:52:58 2015 +0100
@@ -104,27 +104,27 @@
      * @return
      *           BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error.
      */
-    static ble_error_t launch(Gap::Handle_t             connectionHandle,
-                              ServiceCallback_t         sc = NULL,
-                              CharacteristicCallback_t  cc = NULL,
-                              const UUID               &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
-                              const UUID               &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
+    virtual ble_error_t launch(Gap::Handle_t             connectionHandle,
+                               ServiceCallback_t         sc = NULL,
+                               CharacteristicCallback_t  cc = NULL,
+                               const UUID               &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
+                               const UUID               &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) = 0;
+
+    /**
+     * Is service-discovery currently active?
+     */
+    virtual bool        isActive(void) const = 0;
 
     /**
      * Terminate an ongoing service-discovery. This should result in an
      * invocation of the TerminationCallback if service-discovery is active.
      */
-    static void        terminate(void);
-
-    /**
-     * Is service-discovery currently active?
-     */
-    static bool        isActive(void);
+    virtual void        terminate(void) = 0;
 
     /**
      * Setup callback to be invoked when service discovery is terminated.
      */
-    static void        onTermination(TerminationCallback_t callback);
+    virtual void        onTermination(TerminationCallback_t callback) = 0;
 
 protected:
     Gap::Handle_t            connHandle; /**< Connection handle as provided by the SoftDevice. */