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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.