Workings and tests to create custom GATT services, for use as part of peripheral communication in cars

Dependencies:   BLE_API mbed nRF51822

Revision:
0:af868ad47854
Child:
1:ebdf445c4bcc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestGattService.h	Sun Jan 18 15:17:53 2015 +0000
@@ -0,0 +1,72 @@
+/* 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 __BLE_TEST_SERVICE_H__
+#define __BLE_TEST_SERVICE_H__
+
+#include "BLEDevice.h"
+
+/**
+* @class BatteryService
+* @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br>
+* Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br>
+* Battery Level Char:  https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml
+*/
+class TestGattService {
+public:
+    /**
+    * @param[ref] _ble
+    *               BLEDevice object for the underlying controller.
+    * @param[in] level
+    *               8bit batterly level. Usually used to represent percentage of batterly charge remaining.
+    */
+    TestGattService(BLEDevice &_ble, uint8_t level = 100) :
+        ble(_ble),
+        testLevel(level),
+        testCharacteristic(0x2A67, &level, sizeof(level), sizeof(level),
+                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
+
+        static bool serviceAdded = false; /* We should only ever need to add the service once. */
+        if (serviceAdded) {
+            return;
+        }
+
+        GattCharacteristic *charTable[] = {&testCharacteristic};
+        GattService         testService(0x1817, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+
+        ble.addService(testService);
+        serviceAdded = true;
+    }
+
+    /**
+     * @brief Update the battery level with a new value. Valid values range from
+     * 0..100. Anything outside this range will be ignored.
+     *
+     * @param newLevel
+     *              update to battery level.
+     */
+    void updateTestLevel(uint8_t newLevel) {
+        testLevel = newLevel;
+        ble.updateCharacteristicValue(testCharacteristic.getValueAttribute().getHandle(), &testLevel, 1);
+    }
+
+private:
+    BLEDevice           &ble;
+    uint8_t             testLevel;
+    GattCharacteristic  testCharacteristic;
+};
+
+#endif /* #ifndef __BLE_TEST_SERVICE_H__*/
\ No newline at end of file