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