Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LinkLossService.h Source File

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