Updated
Fork of BLE_API by
ble/services/LinkLossService.h@727:1a1f5c5aedfe, 2015-07-06 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Jul 06 10:10:34 2015 +0100
- Revision:
- 727:1a1f5c5aedfe
- Parent:
- 726:6884e374e2eb
- Child:
- 728:997ba5e7b3b6
Synchronized with git rev 854fd111
Author: James Crosby
use mbed-classic to build (this will probably be a target-dependendency in the future)
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 | 712:b04b5db36865 | 20 | #include "Gap.h" |
rgrover1 | 712:b04b5db36865 | 21 | |
rgrover1 | 712:b04b5db36865 | 22 | /** |
rgrover1 | 712:b04b5db36865 | 23 | * @class LinkLossService |
rgrover1 | 712:b04b5db36865 | 24 | * @brief This service defines behavior when a link is lost between two devices. <br> |
rgrover1 | 712:b04b5db36865 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml <br> |
rgrover1 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 55 | ble.addService(linkLossService); |
rgrover1 | 712:b04b5db36865 | 56 | serviceAdded = true; |
rgrover1 | 712:b04b5db36865 | 57 | |
rgrover1 | 712:b04b5db36865 | 58 | ble.addToDisconnectionCallChain(this, &LinkLossService::onDisconnectionFilter); |
rgrover1 | 712:b04b5db36865 | 59 | ble.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 | 712:b04b5db36865 | 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 | 727:1a1f5c5aedfe | 76 | private: |
rgrover1 | 712:b04b5db36865 | 77 | /** |
rgrover1 | 712:b04b5db36865 | 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 | 727:1a1f5c5aedfe | 84 | if (params->charHandle == 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 | 712:b04b5db36865 | 89 | void onDisconnectionFilter(void) { |
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__ */ |