Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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