Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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