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

Dependencies:   BLE_API mbed nRF51822

Committer:
alexanderlea
Date:
Sun Jan 18 15:17:53 2015 +0000
Revision:
0:af868ad47854
Child:
1:ebdf445c4bcc
Initial commit to test 1 of BLE gatt test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexanderlea 0:af868ad47854 1 /* mbed Microcontroller Library
alexanderlea 0:af868ad47854 2 * Copyright (c) 2006-2013 ARM Limited
alexanderlea 0:af868ad47854 3 *
alexanderlea 0:af868ad47854 4 * Licensed under the Apache License, Version 2.0 (the "License");
alexanderlea 0:af868ad47854 5 * you may not use this file except in compliance with the License.
alexanderlea 0:af868ad47854 6 * You may obtain a copy of the License at
alexanderlea 0:af868ad47854 7 *
alexanderlea 0:af868ad47854 8 * http://www.apache.org/licenses/LICENSE-2.0
alexanderlea 0:af868ad47854 9 *
alexanderlea 0:af868ad47854 10 * Unless required by applicable law or agreed to in writing, software
alexanderlea 0:af868ad47854 11 * distributed under the License is distributed on an "AS IS" BASIS,
alexanderlea 0:af868ad47854 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
alexanderlea 0:af868ad47854 13 * See the License for the specific language governing permissions and
alexanderlea 0:af868ad47854 14 * limitations under the License.
alexanderlea 0:af868ad47854 15 */
alexanderlea 0:af868ad47854 16
alexanderlea 0:af868ad47854 17 #ifndef __BLE_TEST_SERVICE_H__
alexanderlea 0:af868ad47854 18 #define __BLE_TEST_SERVICE_H__
alexanderlea 0:af868ad47854 19
alexanderlea 0:af868ad47854 20 #include "BLEDevice.h"
alexanderlea 0:af868ad47854 21
alexanderlea 0:af868ad47854 22 /**
alexanderlea 0:af868ad47854 23 * @class BatteryService
alexanderlea 0:af868ad47854 24 * @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br>
alexanderlea 0:af868ad47854 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br>
alexanderlea 0:af868ad47854 26 * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml
alexanderlea 0:af868ad47854 27 */
alexanderlea 0:af868ad47854 28 class TestGattService {
alexanderlea 0:af868ad47854 29 public:
alexanderlea 0:af868ad47854 30 /**
alexanderlea 0:af868ad47854 31 * @param[ref] _ble
alexanderlea 0:af868ad47854 32 * BLEDevice object for the underlying controller.
alexanderlea 0:af868ad47854 33 * @param[in] level
alexanderlea 0:af868ad47854 34 * 8bit batterly level. Usually used to represent percentage of batterly charge remaining.
alexanderlea 0:af868ad47854 35 */
alexanderlea 0:af868ad47854 36 TestGattService(BLEDevice &_ble, uint8_t level = 100) :
alexanderlea 0:af868ad47854 37 ble(_ble),
alexanderlea 0:af868ad47854 38 testLevel(level),
alexanderlea 0:af868ad47854 39 testCharacteristic(0x2A67, &level, sizeof(level), sizeof(level),
alexanderlea 0:af868ad47854 40 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
alexanderlea 0:af868ad47854 41
alexanderlea 0:af868ad47854 42 static bool serviceAdded = false; /* We should only ever need to add the service once. */
alexanderlea 0:af868ad47854 43 if (serviceAdded) {
alexanderlea 0:af868ad47854 44 return;
alexanderlea 0:af868ad47854 45 }
alexanderlea 0:af868ad47854 46
alexanderlea 0:af868ad47854 47 GattCharacteristic *charTable[] = {&testCharacteristic};
alexanderlea 0:af868ad47854 48 GattService testService(0x1817, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
alexanderlea 0:af868ad47854 49
alexanderlea 0:af868ad47854 50 ble.addService(testService);
alexanderlea 0:af868ad47854 51 serviceAdded = true;
alexanderlea 0:af868ad47854 52 }
alexanderlea 0:af868ad47854 53
alexanderlea 0:af868ad47854 54 /**
alexanderlea 0:af868ad47854 55 * @brief Update the battery level with a new value. Valid values range from
alexanderlea 0:af868ad47854 56 * 0..100. Anything outside this range will be ignored.
alexanderlea 0:af868ad47854 57 *
alexanderlea 0:af868ad47854 58 * @param newLevel
alexanderlea 0:af868ad47854 59 * update to battery level.
alexanderlea 0:af868ad47854 60 */
alexanderlea 0:af868ad47854 61 void updateTestLevel(uint8_t newLevel) {
alexanderlea 0:af868ad47854 62 testLevel = newLevel;
alexanderlea 0:af868ad47854 63 ble.updateCharacteristicValue(testCharacteristic.getValueAttribute().getHandle(), &testLevel, 1);
alexanderlea 0:af868ad47854 64 }
alexanderlea 0:af868ad47854 65
alexanderlea 0:af868ad47854 66 private:
alexanderlea 0:af868ad47854 67 BLEDevice &ble;
alexanderlea 0:af868ad47854 68 uint8_t testLevel;
alexanderlea 0:af868ad47854 69 GattCharacteristic testCharacteristic;
alexanderlea 0:af868ad47854 70 };
alexanderlea 0:af868ad47854 71
alexanderlea 0:af868ad47854 72 #endif /* #ifndef __BLE_TEST_SERVICE_H__*/