High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Mon Jan 11 08:51:32 2016 +0000
Revision:
1053:ec4a5b9b254e
Parent:
1042:21a86ac7f5b1
Synchronized with git rev 13bf70b6
Author: Rohit Grover
Release 2.1.5
=============

A minor release to separate the concept of minlen and len in
GattCharacteristic. Also contains some improvements to documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 712:b04b5db36865 1 /* mbed Microcontroller Library
rgrover1 712:b04b5db36865 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 712:b04b5db36865 3 *
rgrover1 712:b04b5db36865 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 712:b04b5db36865 5 * you may not use this file except in compliance with the License.
rgrover1 712:b04b5db36865 6 * You may obtain a copy of the License at
rgrover1 712:b04b5db36865 7 *
rgrover1 712:b04b5db36865 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 712:b04b5db36865 9 *
rgrover1 712:b04b5db36865 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 712:b04b5db36865 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 712:b04b5db36865 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 712:b04b5db36865 13 * See the License for the specific language governing permissions and
rgrover1 712:b04b5db36865 14 * limitations under the License.
rgrover1 712:b04b5db36865 15 */
rgrover1 712:b04b5db36865 16
rgrover1 712:b04b5db36865 17 #ifndef __BLE_LINK_LOSS_SERVICE_H__
rgrover1 712:b04b5db36865 18 #define __BLE_LINK_LOSS_SERVICE_H__
rgrover1 712:b04b5db36865 19
rgrover1 912:f728aa46e7df 20 #include "ble/Gap.h"
rgrover1 712:b04b5db36865 21
rgrover1 712:b04b5db36865 22 /**
rgrover1 712:b04b5db36865 23 * @class LinkLossService
vcoubard 1053:ec4a5b9b254e 24 * @brief This service defines behavior when a link is lost between two devices.
vcoubard 1053:ec4a5b9b254e 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml
vcoubard 1053:ec4a5b9b254e 26 * Alertness Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.alert_level.xml
rgrover1 712:b04b5db36865 27 */
rgrover1 712:b04b5db36865 28 class LinkLossService {
rgrover1 712:b04b5db36865 29 public:
rgrover1 712:b04b5db36865 30 enum AlertLevel_t {
rgrover1 712:b04b5db36865 31 NO_ALERT = 0,
rgrover1 712:b04b5db36865 32 MILD_ALERT = 1,
rgrover1 712:b04b5db36865 33 HIGH_ALERT = 2
rgrover1 712:b04b5db36865 34 };
rgrover1 712:b04b5db36865 35
rgrover1 712:b04b5db36865 36 typedef void (* callback_t)(AlertLevel_t level);
rgrover1 712:b04b5db36865 37
rgrover1 712:b04b5db36865 38 /**
rgrover1 712:b04b5db36865 39 * @param[ref] ble
rgrover1 712:b04b5db36865 40 * BLE object for the underlying controller.
rgrover1 712:b04b5db36865 41 */
rgrover1 712:b04b5db36865 42 LinkLossService(BLE &bleIn, callback_t callbackIn, AlertLevel_t levelIn = NO_ALERT) :
rgrover1 712:b04b5db36865 43 ble(bleIn),
rgrover1 712:b04b5db36865 44 alertLevel(levelIn),
rgrover1 712:b04b5db36865 45 callback(callbackIn),
rgrover1 712:b04b5db36865 46 alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel)) {
rgrover1 712:b04b5db36865 47 static bool serviceAdded = false; /* We should only ever add one LinkLoss service. */
rgrover1 712:b04b5db36865 48 if (serviceAdded) {
rgrover1 712:b04b5db36865 49 return;
rgrover1 712:b04b5db36865 50 }
rgrover1 712:b04b5db36865 51
rgrover1 712:b04b5db36865 52 GattCharacteristic *charTable[] = {&alertLevelChar};
rgrover1 712:b04b5db36865 53 GattService linkLossService(GattService::UUID_LINK_LOSS_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 712:b04b5db36865 54
rgrover1 912:f728aa46e7df 55 ble.gattServer().addService(linkLossService);
rgrover1 712:b04b5db36865 56 serviceAdded = true;
rgrover1 712:b04b5db36865 57
rgrover1 912:f728aa46e7df 58 ble.gap().onDisconnection(this, &LinkLossService::onDisconnectionFilter);
rgrover1 912:f728aa46e7df 59 ble.gattServer().onDataWritten(this, &LinkLossService::onDataWritten);
rgrover1 712:b04b5db36865 60 }
rgrover1 712:b04b5db36865 61
rgrover1 712:b04b5db36865 62 /**
rgrover1 712:b04b5db36865 63 * Update the callback.
rgrover1 712:b04b5db36865 64 */
rgrover1 712:b04b5db36865 65 void setCallback(callback_t newCallback) {
rgrover1 712:b04b5db36865 66 callback = newCallback;
rgrover1 712:b04b5db36865 67 }
rgrover1 712:b04b5db36865 68
rgrover1 712:b04b5db36865 69 /**
vcoubard 1053:ec4a5b9b254e 70 * Update alertness level.
rgrover1 712:b04b5db36865 71 */
rgrover1 712:b04b5db36865 72 void setAlertLevel(AlertLevel_t newLevel) {
rgrover1 712:b04b5db36865 73 alertLevel = newLevel;
rgrover1 712:b04b5db36865 74 }
rgrover1 712:b04b5db36865 75
rgrover1 728:997ba5e7b3b6 76 protected:
rgrover1 712:b04b5db36865 77 /**
vcoubard 1053:ec4a5b9b254e 78 * This callback allows receiving updates to the AlertLevel characteristic.
rgrover1 712:b04b5db36865 79 *
rgrover1 712:b04b5db36865 80 * @param[in] params
rgrover1 712:b04b5db36865 81 * Information about the characterisitc being updated.
rgrover1 712:b04b5db36865 82 */
rgrover1 712:b04b5db36865 83 virtual void onDataWritten(const GattWriteCallbackParams *params) {
rgrover1 728:997ba5e7b3b6 84 if (params->handle == alertLevelChar.getValueHandle()) {
rgrover1 712:b04b5db36865 85 alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data);
rgrover1 712:b04b5db36865 86 }
rgrover1 712:b04b5db36865 87 }
rgrover1 712:b04b5db36865 88
rgrover1 912:f728aa46e7df 89 void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params) {
rgrover1 712:b04b5db36865 90 if (alertLevel != NO_ALERT) {
rgrover1 712:b04b5db36865 91 callback(alertLevel);
rgrover1 712:b04b5db36865 92 }
rgrover1 712:b04b5db36865 93 }
rgrover1 712:b04b5db36865 94
rgrover1 712:b04b5db36865 95 protected:
rgrover1 712:b04b5db36865 96 BLE &ble;
rgrover1 712:b04b5db36865 97 AlertLevel_t alertLevel;
rgrover1 712:b04b5db36865 98 callback_t callback;
rgrover1 712:b04b5db36865 99
rgrover1 712:b04b5db36865 100 ReadWriteGattCharacteristic<uint8_t> alertLevelChar;
rgrover1 712:b04b5db36865 101 };
rgrover1 712:b04b5db36865 102
rgrover1 712:b04b5db36865 103 #endif /* __BLE_LINK_LOSS_SERVICE_H__ */