Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.

Fork of BLE_API by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
rgrover1
Date:
Mon Mar 02 11:50:47 2015 +0000
Parent:
291:0b66dd15728e
Child:
293:6278354cfd98
Commit message:
Synchronized with git rev 8d1b53ed
Author: Jeremy Brodt
Added callback on characteristic reads

Changed in this revision

common/UUID.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/GattCharacteristicCallbackParams.h Show annotated file Show diff for this revision Revisions of this file
public/GattServer.h Show annotated file Show diff for this revision Revisions of this file
public/UUID.h Show annotated file Show diff for this revision Revisions of this file
--- a/common/UUID.cpp	Mon Mar 02 11:50:47 2015 +0000
+++ b/common/UUID.cpp	Mon Mar 02 11:50:47 2015 +0000
@@ -1,108 +1,93 @@
-/* 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 <string.h>
-
-#include "UUID.h"
-
-UUID::UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) {
-    /* empty */
-}
-
-/**************************************************************************/
-/*!
-    @brief  Creates a new 128-bit UUID
-
-    @note   The UUID is a unique 128-bit (16 byte) ID used to identify
-            different service or characteristics on the BLE device.
-
-    @note   When creating a UUID, the constructor will check if all bytes
-            except bytes 2/3 are equal to 0.  If only bytes 2/3 have a
-            value, the UUID will be treated as a short/BLE UUID, and the
-            .type field will be set to UUID::UUID_TYPE_SHORT.  If any
-            of the bytes outside byte 2/3 have a non-zero value, the UUID
-            will be considered a 128-bit ID, and .type will be assigned
-            as UUID::UUID_TYPE_LONG.
-
-    @param[in]  uuid_base
-                The 128-bit (16-byte) UUID value.  For 128-bit values,
-                assign all 16 bytes.  For 16-bit values, assign the
-                16-bits to byte 2 and 3, and leave the rest of the bytes
-                as 0.
-
-    @section EXAMPLE
-
-    @code
-
-    // Create a short UUID (0x180F)
-    uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-    UUID ble_uuid = UUID(shortID);
-    // ble_uuid.type  = UUID_TYPE_SHORT
-    // ble_uuid.value = 0x180F
-
-    // Creeate a long UUID
-    uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
-                           0x44, 0x55, 0x66, 0x77,
-                           0x88, 0x99, 0xAA, 0xBB,
-                           0xCC, 0xDD, 0xEE, 0xFF };
-    UUID custom_uuid = UUID(longID);
-    // custom_uuid.type  = UUID_TYPE_LONG
-    // custom_uuid.value = 0x3322
-    // custom_uuid.base  = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
-
-    @endcode
-*/
-/**************************************************************************/
-UUID::UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(0)
-{
-    memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID);
-    shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3]));
-
-    /* Check if this is a short of a long UUID */
-    unsigned index;
-    for (index = 0; index < LENGTH_OF_LONG_UUID; index++) {
-        if ((index == 2) || (index == 3)) {
-            continue; /* we should not consider bytes 2 and 3 because that's
-                       * where the 16-bit relative UUID is placed. */
-        }
-
-        if (baseUUID[index] != 0) {
-            type = UUID_TYPE_LONG;
-
-            /* zero out the 16-bit part in the base; this will help equate long
-             * UUIDs when they differ only in this 16-bit relative part.*/
-            baseUUID[2] = 0;
-            baseUUID[3] = 0;
-
-            return;
-        }
-    }
-}
-
-bool UUID::operator==(const UUID &other) const
-{
-    if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) &&
-        (this->shortUUID == other.shortUUID)) {
-        return true;
-    }
-
-    if ((this->type == LENGTH_OF_LONG_UUID) && (other.type == LENGTH_OF_LONG_UUID) &&
-        (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) {
-        return true;
-    }
-
-    return false;
-}
\ No newline at end of file
+/* 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 <string.h>
+
+#include "UUID.h"
+
+UUID::UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) {
+    /* empty */
+}
+
+/**************************************************************************/
+/*!
+    @brief  Creates a new 128-bit UUID
+
+    @note   The UUID is a unique 128-bit (16 byte) ID used to identify
+            different service or characteristics on the BLE device.
+
+    @note   When creating a UUID, the constructor will check if all bytes
+            except bytes 2/3 are equal to 0.  If only bytes 2/3 have a
+            value, the UUID will be treated as a short/BLE UUID, and the
+            .type field will be set to UUID::UUID_TYPE_SHORT.  If any
+            of the bytes outside byte 2/3 have a non-zero value, the UUID
+            will be considered a 128-bit ID, and .type will be assigned
+            as UUID::UUID_TYPE_LONG.
+
+    @param[in]  uuid_base
+                The 128-bit (16-byte) UUID value.  For 128-bit values,
+                assign all 16 bytes.  For 16-bit values, assign the
+                16-bits to byte 2 and 3, and leave the rest of the bytes
+                as 0.
+
+    @section EXAMPLE
+
+    @code
+
+    // Create a short UUID (0x180F)
+    uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+    UUID ble_uuid = UUID(shortID);
+    // ble_uuid.type  = UUID_TYPE_SHORT
+    // ble_uuid.value = 0x180F
+
+    // Creeate a long UUID
+    uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
+                           0x44, 0x55, 0x66, 0x77,
+                           0x88, 0x99, 0xAA, 0xBB,
+                           0xCC, 0xDD, 0xEE, 0xFF };
+    UUID custom_uuid = UUID(longID);
+    // custom_uuid.type  = UUID_TYPE_LONG
+    // custom_uuid.value = 0x3322
+    // custom_uuid.base  = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
+
+    @endcode
+*/
+/**************************************************************************/
+UUID::UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(0)
+{
+    memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID);
+    shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3]));
+
+    /* Check if this is a short of a long UUID */
+    unsigned index;
+    for (index = 0; index < LENGTH_OF_LONG_UUID; index++) {
+        if ((index == 2) || (index == 3)) {
+            continue; /* we should not consider bytes 2 and 3 because that's
+                       * where the 16-bit relative UUID is placed. */
+        }
+
+        if (baseUUID[index] != 0) {
+            type = UUID_TYPE_LONG;
+
+            /* zero out the 16-bit part in the base; this will help equate long
+             * UUIDs when they differ only in this 16-bit relative part.*/
+            baseUUID[2] = 0;
+            baseUUID[3] = 0;
+
+            return;
+        }
+    }
+}
--- a/public/BLEDevice.h	Mon Mar 02 11:50:47 2015 +0000
+++ b/public/BLEDevice.h	Mon Mar 02 11:50:47 2015 +0000
@@ -252,6 +252,20 @@
     void onDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP));
     template <typename T> void onDataWritten(T * objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context));
 
+    /**
+     * Setup a callback for when a characteristic is being read by a client.
+     *
+     * @Note: it is possible to chain together multiple onDataRead callbacks
+     * (potentially from different modules of an application) to receive updates
+     * to characteristics. Services may add their own onDataRead callbacks
+     * behind the scenes to trap interesting events.
+     *
+     * @Note: it is also possible to setup a callback into a member function of
+     * some object.
+     */
+    void onDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP));
+    template <typename T> void onDataRead(T * objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context));
+
     void onUpdatesEnabled(GattServer::EventCallback_t callback);
     void onUpdatesDisabled(GattServer::EventCallback_t callback);
     void onConfirmationReceived(GattServer::EventCallback_t callback);
@@ -554,6 +568,16 @@
 }
 
 inline void
+BLEDevice::onDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP)) {
+    transport->getGattServer().setOnDataRead(callback);
+}
+
+template <typename T> inline void
+BLEDevice::onDataRead(T *objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context)) {
+    transport->getGattServer().setOnDataRead(objPtr, memberPtr);
+}
+
+inline void
 BLEDevice::onUpdatesEnabled(GattServer::EventCallback_t callback)
 {
     transport->getGattServer().setOnUpdatesEnabled(callback);
--- a/public/GattCharacteristicCallbackParams.h	Mon Mar 02 11:50:47 2015 +0000
+++ b/public/GattCharacteristicCallbackParams.h	Mon Mar 02 11:50:47 2015 +0000
@@ -33,6 +33,17 @@
     const uint8_t *data;   /**< Incoming data, variable length. */
 };
 
+struct GattCharacteristicReadCBParams {
+    GattAttribute::Handle_t charHandle;
+    enum Type {
+        GATTS_CHAR_OP_INVALID               = 0x00,  /**< Invalid Operation. */
+        GATTS_CHAR_OP_READ_REQ              = 0x0A,  /**< Read Request. */
+    } op;                  /**< Type of write operation, */
+    uint16_t       offset; /**< Offset for the read operation. */
+    uint16_t       *len;   /**< Length of the outgoing data. */
+    uint8_t        *data;  /**< Outgoing data, variable length. */
+};
+
 struct GattCharacteristicWriteAuthCBParams {
     GattAttribute::Handle_t  charHandle;
     uint16_t                 offset; /**< Offset for the write operation. */
--- a/public/GattServer.h	Mon Mar 02 11:50:47 2015 +0000
+++ b/public/GattServer.h	Mon Mar 02 11:50:47 2015 +0000
@@ -34,6 +34,7 @@
         characteristicCount(0),
         onDataSent(),
         onDataWritten(),
+        onDataRead(),
         onUpdatesEnabled(NULL),
         onUpdatesDisabled(NULL),
         onConfirmationReceived(NULL) {
@@ -63,6 +64,11 @@
     void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) {
         onDataWritten.add(objPtr, memberPtr);
     }
+    void setOnDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP)) {onDataRead.add(callback);}
+    template <typename T>
+    void setOnDataRead(T *objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context)) {
+        onDataRead.add(objPtr, memberPtr);
+    }
     void setOnUpdatesEnabled(EventCallback_t callback) {onUpdatesEnabled = callback;}
     void setOnUpdatesDisabled(EventCallback_t callback) {onUpdatesDisabled = callback;}
     void setOnConfirmationReceived(EventCallback_t callback) {onConfirmationReceived = callback;}
@@ -74,6 +80,12 @@
         }
     }
 
+    void handleDataReadEvent(const GattCharacteristicReadCBParams *params) {
+        if (onDataRead.hasCallbacksAttached()) {
+            onDataRead.call(params);
+        }
+    }
+
     void handleEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
         switch (type) {
             case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
@@ -109,6 +121,7 @@
 private:
     CallChainOfFunctionPointersWithContext<unsigned> onDataSent;
     CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *> onDataWritten;
+    CallChainOfFunctionPointersWithContext<const GattCharacteristicReadCBParams *> onDataRead;
     EventCallback_t onUpdatesEnabled;
     EventCallback_t onUpdatesDisabled;
     EventCallback_t onConfirmationReceived;
--- a/public/UUID.h	Mon Mar 02 11:50:47 2015 +0000
+++ b/public/UUID.h	Mon Mar 02 11:50:47 2015 +0000
@@ -1,60 +1,52 @@
-/* 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.
- */
-
-#ifndef __UUID_H__
-#define __UUID_H__
-
-#include "blecommon.h"
-
-const unsigned   LENGTH_OF_LONG_UUID = 16;
-typedef uint16_t ShortUUIDBytes_t;
-typedef uint8_t  LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
-
-class UUID {
-public:
-    enum {
-        UUID_TYPE_SHORT = 0,    // Short BLE UUID
-        UUID_TYPE_LONG  = 1     // Full 128-bit UUID
-    };
-
-public:
-    UUID(const LongUUIDBytes_t);
-    UUID(ShortUUIDBytes_t);
-
-public:
-    uint8_t           shortOrLong(void)  const {return type;     }
-    const uint8_t    *getBaseUUID(void)  const {
-        if (type == UUID_TYPE_SHORT) { return (uint8_t*)&shortUUID; }
-        else { return baseUUID; }
-    }
-    ShortUUIDBytes_t  getShortUUID(void) const {return shortUUID;}
-    uint8_t           getLen(void)       const {
-        return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID);
-    }
-
-    bool operator== (const UUID&) const;
-
-private:
-    uint8_t          type;      // UUID_TYPE_SHORT or UUID_TYPE_LONG
-    LongUUIDBytes_t  baseUUID;  /* the base of the long UUID (if
-                            * used). Note: bytes 12 and 13 (counting from LSB)
-                            * are zeroed out to allow comparison with other long
-                            * UUIDs which differ only in the 16-bit relative
-                            * part.*/
-    ShortUUIDBytes_t shortUUID; // 16 bit uuid (byte 2-3 using with base)
-};
-
+/* 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.
+ */
+
+#ifndef __UUID_H__
+#define __UUID_H__
+
+#include "blecommon.h"
+
+const unsigned   LENGTH_OF_LONG_UUID = 16;
+typedef uint16_t ShortUUIDBytes_t;
+typedef uint8_t  LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
+
+class UUID {
+public:
+    enum {
+        UUID_TYPE_SHORT = 0,    // Short BLE UUID
+        UUID_TYPE_LONG  = 1     // Full 128-bit UUID
+    };
+
+public:
+    UUID(const LongUUIDBytes_t);
+    UUID(ShortUUIDBytes_t);
+
+public:
+    uint8_t           shortOrLong(void)  const {return type;     }
+    const uint8_t    *getBaseUUID(void)  const {return baseUUID; }
+    ShortUUIDBytes_t  getShortUUID(void) const {return shortUUID;}
+
+private:
+    uint8_t          type;      // UUID_TYPE_SHORT or UUID_TYPE_LONG
+    LongUUIDBytes_t  baseUUID;  /* the base of the long UUID (if
+                            * used). Note: bytes 12 and 13 (counting from LSB)
+                            * are zeroed out to allow comparison with other long
+                            * UUIDs which differ only in the 16-bit relative
+                            * part.*/
+    ShortUUIDBytes_t shortUUID; // 16 bit uuid (byte 2-3 using with base)
+};
+
 #endif // ifndef __UUID_H__
\ No newline at end of file