High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
ble/services/LinkLossService.h@1017:ccb8c0646e55, 2015-12-10 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Dec 10 09:15:01 2015 +0000
- Revision:
- 1017:ccb8c0646e55
- Parent:
- 1008:c27e0c6f1f38
- Child:
- 1019:575852ad31a2
Synchronized with git rev 12e27cd4
Author: Rohit Grover
Release 2.1.3
=============
* Improvements to CallChainOfFunctionPointerswithContext:
- add a `detach` function to be able to remove callbacks.
- detach function now return true if a function has been detached and
false otherwise.
- add a function call operator.
- use safe-bool idiom. see : http://www.artima.com/cppsource/safebool.html
* Add SafeBool class which allow to easily declare a safe bool operator in
c++03.
* Improvements to FunctionPointerWithContext:
- fix call propagation
- use safe bool idiom
* Add config file for generating Doxygen.
* Setup for onRadioNotification callback does not call initRadioNotification
anymore.
* GapAdvertisementData now handles replacement and appending of data fields
based on type. Some fields can be replaced with new values, and others
require the payload to be appended.
Who changed what in which revision?
User | Revision | Line number | New 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 |
rgrover1 | 1017:ccb8c0646e55 | 24 | * @brief This service defines behavior when a link is lost between two devices. <br> |
rgrover1 | 1017:ccb8c0646e55 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml <br> |
rgrover1 | 1017:ccb8c0646e55 | 26 | * Alertness Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.alert_level.xml <br> |
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 | /** |
rgrover1 | 1017:ccb8c0646e55 | 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 | /** |
rgrover1 | 1017:ccb8c0646e55 | 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__ */ |