Minor temporary patch to allow DFU packet callback

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Thu Jan 22 09:59:44 2015 +0000
Revision:
277:1407d2f1ce3c
Parent:
246:6c2402f286f6
Synchronized with git rev 4a268db0
Author: Rohit Grover
Release 0.2.11
=============

Enhancements
~~~~~~~~~~~~

* Use template classes to simplify definitions of GattCharacteristic within services.

Bugfixes
~~~~~~~~

Compatibility
~~~~~~~~~~~~~

This release is API compatible with 0.2.4.

Who changed what in which revision?

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