Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_API by
LinkLossService.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __BLE_LINK_LOSS_SERVICE_H__ 00018 #define __BLE_LINK_LOSS_SERVICE_H__ 00019 00020 #include "Gap.h" 00021 00022 /** 00023 * @class LinkLossService 00024 * @brief This service defines behavior when a link is lost between two devices. <br> 00025 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml <br> 00026 * Alertness Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.alert_level.xml <br> 00027 */ 00028 class LinkLossService { 00029 public: 00030 enum AlertLevel_t { 00031 NO_ALERT = 0, 00032 MILD_ALERT = 1, 00033 HIGH_ALERT = 2 00034 }; 00035 00036 typedef void (* callback_t)(AlertLevel_t level); 00037 00038 /** 00039 * @param[ref] ble 00040 * BLEDevice object for the underlying controller. 00041 */ 00042 LinkLossService (BLEDevice &bleIn, callback_t callbackIn, AlertLevel_t levelIn = NO_ALERT) : 00043 ble(bleIn), 00044 alertLevel(levelIn), 00045 callback(callbackIn), 00046 alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel), sizeof(uint8_t), sizeof(uint8_t), 00047 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) { 00048 static bool serviceAdded = false; /* We should only ever add one LinkLoss service. */ 00049 if (serviceAdded) { 00050 return; 00051 } 00052 00053 GattCharacteristic *charTable[] = {&alertLevelChar}; 00054 GattService linkLossService(GattService::UUID_LINK_LOSS_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00055 00056 ble.addService(linkLossService); 00057 serviceAdded = true; 00058 00059 ble.addToDisconnectionCallChain(this, &LinkLossService::onDisconnectionFilter); 00060 ble.onDataWritten(this, &LinkLossService::onDataWritten); 00061 } 00062 00063 /** 00064 * Update the callback. 00065 */ 00066 void setCallback(callback_t newCallback) { 00067 callback = newCallback; 00068 } 00069 00070 /** 00071 * Update Alertness Level. 00072 */ 00073 void setAlertLevel(AlertLevel_t newLevel) { 00074 alertLevel = newLevel; 00075 } 00076 00077 private: 00078 /** 00079 * This callback allows receiving updates to the AlertLevel Characteristic. 00080 * 00081 * @param[in] params 00082 * Information about the characterisitc being updated. 00083 */ 00084 virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { 00085 if (params->charHandle == alertLevelChar.getValueHandle()) { 00086 alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data); 00087 } 00088 } 00089 00090 void onDisconnectionFilter(void) { 00091 if (alertLevel != NO_ALERT) { 00092 callback(alertLevel); 00093 } 00094 } 00095 00096 private: 00097 BLEDevice &ble; 00098 AlertLevel_t alertLevel; 00099 callback_t callback; 00100 GattCharacteristic alertLevelChar; 00101 }; 00102 00103 #endif /* __BLE_LINK_LOSS_SERVICE_H__ */
Generated on Tue Jul 12 2022 21:47:54 by
1.7.2
