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 /* MBED_DEPRECATED */
20 #warning "These services are deprecated and will be removed. Please see services.md for details about replacement services."
21 
22 #ifndef __BLE_LINK_LOSS_SERVICE_H__
23 #define __BLE_LINK_LOSS_SERVICE_H__
24 
25 #include "ble/BLE.h"
26 #include "ble/Gap.h"
27 #include "ble/GattServer.h"
28 
29 #if BLE_FEATURE_GATT_SERVER
30 
31 /**
32 * @class LinkLossService
33 * @brief This service defines behavior when a link is lost between two devices.
34 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml
35 * Alertness Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.alert_level.xml
36 */
38 {
39 public:
40  enum AlertLevel_t {
41  NO_ALERT = 0,
42  MILD_ALERT = 1,
43  HIGH_ALERT = 2
44  };
45 
46  typedef void (* callback_t)(AlertLevel_t level);
47 
48  /**
49  * @param bleIn
50  * BLE object for the underlying controller.
51  * @param callbackIn Callback invoked upon disconnection.
52  * @param levelIn Alert level.
53  */
54  LinkLossService(BLE &bleIn, callback_t callbackIn, AlertLevel_t levelIn = NO_ALERT) :
55  ble(bleIn),
56  alertLevel(levelIn),
57  callback(callbackIn),
58  alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel)) {
59  static bool serviceAdded = false; /* We should only ever add one LinkLoss service. */
60  if (serviceAdded) {
61  return;
62  }
63 
64  GattCharacteristic *charTable[] = {&alertLevelChar};
65  GattService linkLossService(GattService::UUID_LINK_LOSS_SERVICE, charTable, sizeof(charTable) / sizeof(charTable[0]));
66 
67  ble.gattServer().addService(linkLossService);
68  serviceAdded = true;
69 
70  ble.gap().setEventHandler(this);
71  ble.gattServer().onDataWritten(this, &LinkLossService::onDataWritten);
72  }
73 
74  /**
75  * Update the callback.
76  */
77  void setCallback(callback_t newCallback) {
78  callback = newCallback;
79  }
80 
81  /**
82  * Update alertness level.
83  */
84  void setAlertLevel(AlertLevel_t newLevel) {
85  alertLevel = newLevel;
86  }
87 
88 protected:
89  /**
90  * This callback allows receiving updates to the AlertLevel characteristic.
91  *
92  * @param[in] params
93  * Information about the characteristic being updated.
94  */
95  virtual void onDataWritten(const GattWriteCallbackParams *params) {
96  if (params->handle == alertLevelChar.getValueHandle()) {
97  alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data);
98  }
99  }
100 
102  if (alertLevel != NO_ALERT) {
103  callback(alertLevel);
104  }
105  }
106 
107 protected:
108  BLE &ble;
109  AlertLevel_t alertLevel;
110  callback_t callback;
111 
113 };
114 
115 #endif // BLE_FEATURE_GATT_SERVER
116 
117 #endif /* __BLE_LINK_LOSS_SERVICE_H__ */
const uint8_t * data
Pointer to the data to write.
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.
UUID of the Link Loss service.
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.