BA
/
BaBoRo1
Embed:
(wiki syntax)
Show/hide line numbers
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 "ble/Gap.h" 00021 00022 /** 00023 * @class LinkLossService 00024 * @brief This service defines behavior when a link is lost between two devices. 00025 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml 00026 * Alertness Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.alert_level.xml 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 bleIn 00040 * BLE object for the underlying controller. 00041 * @param callbackIn Callback invoked upon disconnection. 00042 * @param levelIn Alert level. 00043 */ 00044 LinkLossService (BLE &bleIn, callback_t callbackIn, AlertLevel_t levelIn = NO_ALERT) : 00045 ble(bleIn), 00046 alertLevel(levelIn), 00047 callback(callbackIn), 00048 alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel)) { 00049 static bool serviceAdded = false; /* We should only ever add one LinkLoss service. */ 00050 if (serviceAdded) { 00051 return; 00052 } 00053 00054 GattCharacteristic *charTable[] = {&alertLevelChar}; 00055 GattService linkLossService(GattService::UUID_LINK_LOSS_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00056 00057 ble.gattServer().addService(linkLossService); 00058 serviceAdded = true; 00059 00060 ble.gap().onDisconnection(this, &LinkLossService::onDisconnectionFilter); 00061 ble.gattServer().onDataWritten(this, &LinkLossService::onDataWritten); 00062 } 00063 00064 /** 00065 * Update the callback. 00066 */ 00067 void setCallback(callback_t newCallback) { 00068 callback = newCallback; 00069 } 00070 00071 /** 00072 * Update alertness level. 00073 */ 00074 void setAlertLevel(AlertLevel_t newLevel) { 00075 alertLevel = newLevel; 00076 } 00077 00078 protected: 00079 /** 00080 * This callback allows receiving updates to the AlertLevel characteristic. 00081 * 00082 * @param[in] params 00083 * Information about the characteristic being updated. 00084 */ 00085 virtual void onDataWritten(const GattWriteCallbackParams *params) { 00086 if (params->handle == alertLevelChar.getValueHandle()) { 00087 alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data); 00088 } 00089 } 00090 00091 void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params) { 00092 if (alertLevel != NO_ALERT) { 00093 callback(alertLevel); 00094 } 00095 } 00096 00097 protected: 00098 BLE &ble; 00099 AlertLevel_t alertLevel; 00100 callback_t callback; 00101 00102 ReadWriteGattCharacteristic<uint8_t> alertLevelChar; 00103 }; 00104 00105 #endif /* __BLE_LINK_LOSS_SERVICE_H__ */
Generated on Tue Jul 12 2022 12:21:58 by
