Bike service

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Mon Nov 02 09:09:03 2015 +0000
Revision:
837:66f14bc44c30
Parent:
835:6556fcb2f51d
Child:
838:e2f69ba31b02
Synchronized with git rev 13a9c192
Author: xcrespo
New call to onDisconnection callback in LinkLossService

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 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 837:66f14bc44c30 55 ble.gattServer().addService(linkLossService);
rgrover1 712:b04b5db36865 56 serviceAdded = true;
rgrover1 712:b04b5db36865 57
rgrover1 837:66f14bc44c30 58 ble.gap().onDisconnection(this, &LinkLossService::onDisconnectionFilter);
rgrover1 837:66f14bc44c30 59 ble.gattServer().onDataWritten(this, &LinkLossService::onDataWritten);
rgrover1 837:66f14bc44c30 60
rgrover1 712:b04b5db36865 61 }
rgrover1 712:b04b5db36865 62
rgrover1 712:b04b5db36865 63 /**
rgrover1 712:b04b5db36865 64 * Update the callback.
rgrover1 712:b04b5db36865 65 */
rgrover1 712:b04b5db36865 66 void setCallback(callback_t newCallback) {
rgrover1 712:b04b5db36865 67 callback = newCallback;
rgrover1 712:b04b5db36865 68 }
rgrover1 712:b04b5db36865 69
rgrover1 712:b04b5db36865 70 /**
rgrover1 712:b04b5db36865 71 * Update Alertness Level.
rgrover1 712:b04b5db36865 72 */
rgrover1 712:b04b5db36865 73 void setAlertLevel(AlertLevel_t newLevel) {
rgrover1 712:b04b5db36865 74 alertLevel = newLevel;
rgrover1 712:b04b5db36865 75 }
rgrover1 712:b04b5db36865 76
rgrover1 728:997ba5e7b3b6 77 protected:
rgrover1 712:b04b5db36865 78 /**
rgrover1 712:b04b5db36865 79 * This callback allows receiving updates to the AlertLevel Characteristic.
rgrover1 712:b04b5db36865 80 *
rgrover1 712:b04b5db36865 81 * @param[in] params
rgrover1 712:b04b5db36865 82 * Information about the characterisitc being updated.
rgrover1 712:b04b5db36865 83 */
rgrover1 712:b04b5db36865 84 virtual void onDataWritten(const GattWriteCallbackParams *params) {
rgrover1 728:997ba5e7b3b6 85 if (params->handle == alertLevelChar.getValueHandle()) {
rgrover1 712:b04b5db36865 86 alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data);
rgrover1 712:b04b5db36865 87 }
rgrover1 712:b04b5db36865 88 }
rgrover1 712:b04b5db36865 89
rgrover1 837:66f14bc44c30 90 void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params) {
rgrover1 712:b04b5db36865 91 if (alertLevel != NO_ALERT) {
rgrover1 712:b04b5db36865 92 callback(alertLevel);
rgrover1 712:b04b5db36865 93 }
rgrover1 712:b04b5db36865 94 }
rgrover1 712:b04b5db36865 95
rgrover1 712:b04b5db36865 96 protected:
rgrover1 712:b04b5db36865 97 BLE &ble;
rgrover1 712:b04b5db36865 98 AlertLevel_t alertLevel;
rgrover1 712:b04b5db36865 99 callback_t callback;
rgrover1 712:b04b5db36865 100
rgrover1 712:b04b5db36865 101 ReadWriteGattCharacteristic<uint8_t> alertLevelChar;
rgrover1 712:b04b5db36865 102 };
rgrover1 712:b04b5db36865 103
rgrover1 712:b04b5db36865 104 #endif /* __BLE_LINK_LOSS_SERVICE_H__ */