BLE_API_Tiny_BLE

Dependents:   CSSE4011_BLE_IMU

Fork of BLE_API by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
rgrover1
Date:
Thu Jan 22 09:59:44 2015 +0000
Parent:
276:daa42f59bdb8
Child:
278:8a935a2d4a16
Commit message:
Synchronized with git rev 4a268db0
Author: Rohit Grover
Release 0.2.11
=============

Enhancements
~~~~~~~~~~~~

* Use template classes to simplify definitions of GattCharacteristic within services.

Bugfixes
~~~~~~~~

Compatibility
~~~~~~~~~~~~~

This release is API compatible with 0.2.4.

Changed in this revision

public/GattCharacteristic.h Show annotated file Show diff for this revision Revisions of this file
services/BatteryService.h Show annotated file Show diff for this revision Revisions of this file
services/DFUService.h Show annotated file Show diff for this revision Revisions of this file
services/HealthThermometerService.h Show annotated file Show diff for this revision Revisions of this file
services/HeartRateService.h Show annotated file Show diff for this revision Revisions of this file
services/LinkLossService.h Show annotated file Show diff for this revision Revisions of this file
services/URIBeaconConfigService.h Show annotated file Show diff for this revision Revisions of this file
--- a/public/GattCharacteristic.h	Thu Jan 22 09:59:43 2015 +0000
+++ b/public/GattCharacteristic.h	Thu Jan 22 09:59:44 2015 +0000
@@ -385,14 +385,14 @@
      * determine the authorization reply for a read request.
      * @param  params to capture the context of the read-auth request.
      *
-     * @NOTE:  To authorize/deny the read the params->authorizationReply field 
+     * @NOTE:  To authorize/deny the read the params->authorizationReply field
      *         should be set to true/false.
      *
-     *         If the read is approved and params->data is unchanged (NULL), 
+     *         If the read is approved and params->data is unchanged (NULL),
      *         the current characteristic value will be used.
      *
-     *         If the read is approved, a new value can be provided by setting 
-     *         the params->data pointer and params->len fields. 
+     *         If the read is approved, a new value can be provided by setting
+     *         the params->data pointer and params->len fields.
      *
      * @return        true if the read is authorized to proceed.
      */
@@ -441,4 +441,62 @@
     GattCharacteristic& operator=(const GattCharacteristic &);
 };
 
+template <typename T>
+class ReadOnlyGattCharacteristic : public GattCharacteristic {
+public:
+    ReadOnlyGattCharacteristic<T>(const UUID &uuid, T *valuePtr, uint8_t additionalProperties = BLE_GATT_CHAR_PROPERTIES_NONE) :
+        GattCharacteristic(uuid, reinterpret_cast<uint8_t *>(valuePtr), sizeof(T), sizeof(T), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | additionalProperties) {
+        /* empty */
+    }
+};
+
+template <typename T>
+class WriteOnlyGattCharacteristic : public GattCharacteristic {
+public:
+    WriteOnlyGattCharacteristic<T>(const UUID &uuid, T *valuePtr, uint8_t additionalProperties = BLE_GATT_CHAR_PROPERTIES_NONE) :
+        GattCharacteristic(uuid, reinterpret_cast<uint8_t *>(valuePtr), sizeof(T), sizeof(T), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | additionalProperties) {
+        /* empty */
+    }
+};
+
+template <typename T>
+class ReadWriteGattCharacteristic : public GattCharacteristic {
+public:
+    ReadWriteGattCharacteristic<T>(const UUID &uuid, T *valuePtr, uint8_t additionalProperties = BLE_GATT_CHAR_PROPERTIES_NONE) :
+        GattCharacteristic(uuid, reinterpret_cast<uint8_t *>(valuePtr), sizeof(T), sizeof(T),
+                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | additionalProperties) {
+        /* empty */
+    }
+};
+
+template <typename T, unsigned NUM_ELEMENTS>
+class WriteOnlyArrayGattCharacteristic : public GattCharacteristic {
+public:
+    WriteOnlyArrayGattCharacteristic<T, NUM_ELEMENTS>(const UUID &uuid, T valuePtr[NUM_ELEMENTS], uint8_t additionalProperties = BLE_GATT_CHAR_PROPERTIES_NONE) :
+        GattCharacteristic(uuid, reinterpret_cast<uint8_t *>(valuePtr), sizeof(T) * NUM_ELEMENTS, sizeof(T) * NUM_ELEMENTS,
+                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | additionalProperties) {
+        /* empty */
+    }
+};
+
+template <typename T, unsigned NUM_ELEMENTS>
+class ReadOnlyArrayGattCharacteristic : public GattCharacteristic {
+public:
+    ReadOnlyArrayGattCharacteristic<T, NUM_ELEMENTS>(const UUID &uuid, T valuePtr[NUM_ELEMENTS], uint8_t additionalProperties = BLE_GATT_CHAR_PROPERTIES_NONE) :
+        GattCharacteristic(uuid, reinterpret_cast<uint8_t *>(valuePtr), sizeof(T) * NUM_ELEMENTS, sizeof(T) * NUM_ELEMENTS,
+                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | additionalProperties) {
+        /* empty */
+    }
+};
+
+template <typename T, unsigned NUM_ELEMENTS>
+class ReadWriteArrayGattCharacteristic : public GattCharacteristic {
+public:
+    ReadWriteArrayGattCharacteristic<T, NUM_ELEMENTS>(const UUID &uuid, T valuePtr[NUM_ELEMENTS], uint8_t additionalProperties = BLE_GATT_CHAR_PROPERTIES_NONE) :
+        GattCharacteristic(uuid, reinterpret_cast<uint8_t *>(valuePtr), sizeof(T) * NUM_ELEMENTS, sizeof(T) * NUM_ELEMENTS,
+                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | additionalProperties) {
+        /* empty */
+    }
+};
+
 #endif // ifndef __GATT_CHARACTERISTIC_H__
\ No newline at end of file
--- a/services/BatteryService.h	Thu Jan 22 09:59:43 2015 +0000
+++ b/services/BatteryService.h	Thu Jan 22 09:59:44 2015 +0000
@@ -36,8 +36,7 @@
     BatteryService(BLEDevice &_ble, uint8_t level = 100) :
         ble(_ble),
         batteryLevel(level),
-        batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel),
-                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
+        batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
 
         static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
         if (serviceAdded) {
@@ -64,9 +63,10 @@
     }
 
 private:
-    BLEDevice          &ble;
-    uint8_t             batteryLevel;
-    GattCharacteristic  batteryLevelCharacteristic;
+    BLEDevice &ble;
+
+    uint8_t    batteryLevel;
+    ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic;
 };
 
 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
\ No newline at end of file
--- a/services/DFUService.h	Thu Jan 22 09:59:43 2015 +0000
+++ b/services/DFUService.h	Thu Jan 22 09:59:44 2015 +0000
@@ -56,8 +56,7 @@
         ble(_ble),
         controlBytes(),
         packetBytes(),
-        controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, SIZEOF_CONTROL_BYTES, SIZEOF_CONTROL_BYTES,
-                     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
         packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES,
                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) {
         static bool serviceAdded = false; /* We should only ever need to add the DFU service once. */
@@ -120,7 +119,7 @@
     /**< Writing to the control characteristic triggers the handover to dfu-
       *  bootloader. At present, writing anything will do the trick--this needs
       *  to be improved. */
-    GattCharacteristic  controlPoint;
+    WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_CONTROL_BYTES> controlPoint;
 
     /**< The packet characteristic in this service doesn't do anything meaningful, but
       *  is only a placeholder to mimic the corresponding characteristic in the
--- a/services/HealthThermometerService.h	Thu Jan 22 09:59:43 2015 +0000
+++ b/services/HealthThermometerService.h	Thu Jan 22 09:59:44 2015 +0000
@@ -54,11 +54,8 @@
     HealthThermometerService(BLEDevice &_ble, float initialTemp, uint8_t _location) :
         ble(_ble),
         valueBytes(initialTemp),
-        tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, valueBytes.getPointer(),
-                        sizeof(TemperatureValueBytes), sizeof(TemperatureValueBytes),
-                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, (uint8_t *)&_location, sizeof(_location), sizeof(_location),
-                     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) {
+        tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, (TemperatureValueBytes *)valueBytes.getPointer(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, &_location) {
 
         GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, };
         GattService         hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
@@ -144,10 +141,10 @@
     };
 
 private:
-    BLEDevice             &ble;
-    TemperatureValueBytes  valueBytes;
-    GattCharacteristic     tempMeasurement;
-    GattCharacteristic     tempLocation;
+    BLEDevice                                         &ble;
+    TemperatureValueBytes                              valueBytes;
+    ReadOnlyGattCharacteristic<TemperatureValueBytes>  tempMeasurement;
+    ReadOnlyGattCharacteristic<uint8_t>                tempLocation;
 };
 
 #endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/
\ No newline at end of file
--- a/services/HeartRateService.h	Thu Jan 22 09:59:43 2015 +0000
+++ b/services/HeartRateService.h	Thu Jan 22 09:59:44 2015 +0000
@@ -59,10 +59,8 @@
         hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
                 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location),
-                    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
-        controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue,
-                     sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) {
+        hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
+        controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
         setupService();
     }
 
@@ -82,10 +80,8 @@
         hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
                 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location),
-                    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
-        controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue,
-                     sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) {
+        hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
+        controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
         setupService();
     }
 
@@ -193,11 +189,13 @@
 
 private:
     BLEDevice           &ble;
+
     HeartRateValueBytes  valueBytes;
     uint8_t              controlPointValue;
-    GattCharacteristic   hrmRate;
-    GattCharacteristic   hrmLocation;
-    GattCharacteristic   controlPoint;
+
+    GattCharacteristic                   hrmRate;
+    ReadOnlyGattCharacteristic<uint8_t>  hrmLocation;
+    WriteOnlyGattCharacteristic<uint8_t> controlPoint;
 };
 
 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/
\ No newline at end of file
--- a/services/LinkLossService.h	Thu Jan 22 09:59:43 2015 +0000
+++ b/services/LinkLossService.h	Thu Jan 22 09:59:44 2015 +0000
@@ -43,8 +43,7 @@
         ble(bleIn),
         alertLevel(levelIn),
         callback(callbackIn),
-        alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel), sizeof(uint8_t), sizeof(uint8_t),
-                       GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) {
+        alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel)) {
         static bool serviceAdded = false; /* We should only ever add one LinkLoss service. */
         if (serviceAdded) {
             return;
@@ -94,10 +93,11 @@
     }
 
 private:
-    BLEDevice          &ble;
-    AlertLevel_t        alertLevel;
-    callback_t          callback;
-    GattCharacteristic  alertLevelChar;
+    BLEDevice    &ble;
+    AlertLevel_t  alertLevel;
+    callback_t    callback;
+
+    ReadWriteGattCharacteristic<uint8_t> alertLevelChar;
 };
 
 #endif /* __BLE_LINK_LOSS_SERVICE_H__ */
\ No newline at end of file
--- a/services/URIBeaconConfigService.h	Thu Jan 22 09:59:43 2015 +0000
+++ b/services/URIBeaconConfigService.h	Thu Jan 22 09:59:44 2015 +0000
@@ -88,19 +88,15 @@
         flags(flagsIn),
         powerLevels(),
         beaconPeriod(beaconPeriodIn),
-        lockedStateChar(lockedStateCharUUID, reinterpret_cast<uint8_t *>(&lockedState), 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
-        lockChar(lockCharUUID, lockBits, SIZEOF_LOCK_BITS, SIZEOF_LOCK_BITS, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
-        unlockChar(unlockCharUUID, lockBits, SIZEOF_LOCK_BITS, SIZEOF_LOCK_BITS, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
-        uriDataChar(uriDataCharUUID, uriData, MAX_SIZE_URI_DATA_CHAR_VALUE, MAX_SIZE_URI_DATA_CHAR_VALUE,
-                    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
-        flagsChar(flagsCharUUID, &flags, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
-        txPowerLevelsChar(txPowerLevelsCharUUID, reinterpret_cast<uint8_t *>(powerLevels), sizeof(powerLevels), sizeof(powerLevels),
-                          GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
-        txPowerModeChar(txPowerModeCharUUID, reinterpret_cast<uint8_t *>(&txPowerMode), sizeof(uint8_t), sizeof(uint8_t),
-                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
-        beaconPeriodChar(beaconPeriodCharUUID, reinterpret_cast<uint8_t *>(&beaconPeriod), 2, 2,
-                         GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
-        resetChar(resetCharUUID, reinterpret_cast<uint8_t *>(&resetFlag), 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)
+        lockedStateChar(lockedStateCharUUID, &lockedState),
+        lockChar(lockCharUUID, lockBits),
+        unlockChar(unlockCharUUID, lockBits),
+        uriDataChar(uriDataCharUUID, uriData),
+        flagsChar(flagsCharUUID, &flags),
+        txPowerLevelsChar(txPowerLevelsCharUUID, powerLevels),
+        txPowerModeChar(txPowerModeCharUUID, &txPowerMode),
+        beaconPeriodChar(beaconPeriodCharUUID, &beaconPeriod),
+        resetChar(resetCharUUID, &resetFlag)
     {
         if ((uriDataIn == NULL) || ((uriDataLength = strlen(uriDataIn)) == 0) || (uriDataLength > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
             return;
@@ -536,15 +532,15 @@
     uint16_t            beaconPeriod;
     bool                resetFlag;
 
-    GattCharacteristic  lockedStateChar;
-    GattCharacteristic  lockChar;
-    GattCharacteristic  unlockChar;
-    GattCharacteristic  uriDataChar;
-    GattCharacteristic  flagsChar;
-    GattCharacteristic  txPowerLevelsChar;
-    GattCharacteristic  txPowerModeChar;
-    GattCharacteristic  beaconPeriodChar;
-    GattCharacteristic  resetChar;
+    ReadOnlyGattCharacteristic<bool>                                        lockedStateChar;
+    WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_LOCK_BITS>             lockChar;
+    WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_LOCK_BITS>             unlockChar;
+    ReadWriteArrayGattCharacteristic<uint8_t, MAX_SIZE_URI_DATA_CHAR_VALUE> uriDataChar;
+    ReadWriteGattCharacteristic<uint8_t>                                    flagsChar;
+    ReadWriteArrayGattCharacteristic<int8_t, NUM_POWER_MODES>               txPowerLevelsChar;
+    ReadWriteGattCharacteristic<TXPowerModes_t>                             txPowerModeChar;
+    ReadWriteGattCharacteristic<uint16_t>                                   beaconPeriodChar;
+    WriteOnlyGattCharacteristic<bool>                                       resetChar;
 };
 
 #endif /* #ifndef __BLE_URI_BEACON_CONFIG_SERVICE_H__*/
\ No newline at end of file