NimBLE-Arduino 2.3.7
Loading...
Searching...
No Matches
NimBLECharacteristic.h
1/*
2 * Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_CHARACTERISTIC_H_
19#define NIMBLE_CPP_CHARACTERISTIC_H_
20
21#include "nimconfig.h"
22#if CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
23
25class NimBLEService;
28class NimBLE2904;
29
30# include "NimBLELocalValueAttribute.h"
31
32# include <string>
33# include <vector>
34
41class NimBLECharacteristic : public NimBLELocalValueAttribute {
42 public:
43 NimBLECharacteristic(const char* uuid,
44 uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
45 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
46 NimBLEService* pService = nullptr);
48 uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
49 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
50 NimBLEService* pService = nullptr);
51
53
54 std::string toString() const;
55 void addDescriptor(NimBLEDescriptor* pDescriptor);
56 void removeDescriptor(NimBLEDescriptor* pDescriptor, bool deleteDsc = false);
57 uint16_t getProperties() const;
59 bool indicate(uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
60 bool indicate(const uint8_t* value, size_t length, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
61 bool notify(uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
62 bool notify(const uint8_t* value, size_t length, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
63
64 NimBLEDescriptor* createDescriptor(const char* uuid,
65 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
66 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
68 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
69 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
71 NimBLEDescriptor* getDescriptorByUUID(const char* uuid) const;
73 NimBLEDescriptor* getDescriptorByHandle(uint16_t handle) const;
75
77
78 /*********************** Template Functions ************************/
79
80# if __cplusplus < 201703L
87 template <typename T>
88# ifdef _DOXYGEN_
89 bool
90# else
91 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value && !Has_c_str_length<T>::value &&
92 !Has_data_size<T>::value,
93 bool>::type
94# endif
95 notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
96 return notify(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
97 }
98
104 template <typename T>
105# ifdef _DOXYGEN_
106 bool
107# else
108 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
109# endif
110 notify(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
111 return notify(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
112 }
113
120 template <typename T>
121# ifdef _DOXYGEN_
122 bool
123# else
124 typename std::enable_if<Has_data_size<T>::value && Has_value_type<T>::value, bool>::type
125# endif
126 notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
127 return notify(
128 reinterpret_cast<const uint8_t*>(v.data()),
129 v.size() * sizeof(typename T::value_type),
130 connHandle
131 );
132 }
133
139 template <typename T>
140# ifdef _DOXYGEN_
141 bool
142# else
143 typename std::enable_if<Has_data_size<T>::value && !Has_value_type<T>::value, bool>::type
144# endif
145 notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
146 return notify(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
147 }
148
155 template <typename T>
156# ifdef _DOXYGEN_
157 bool
158# else
159 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value && !Has_c_str_length<T>::value &&
160 !Has_data_size<T>::value,
161 bool>::type
162# endif
163 indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
164 return indicate(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
165 }
166
172 template <typename T>
173# ifdef _DOXYGEN_
174 bool
175# else
176 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
177# endif
178 indicate(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
179 return indicate(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
180 }
181
188 template <typename T>
189# ifdef _DOXYGEN_
190 bool
191# else
192 typename std::enable_if<Has_data_size<T>::value && Has_value_type<T>::value, bool>::type
193# endif
194 indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
195 return indicate(
196 reinterpret_cast<const uint8_t*>(v.data()),
197 v.size() * sizeof(typename T::value_type),
198 connHandle
199 );
200 }
201
207 template <typename T>
208# ifdef _DOXYGEN_
209 bool
210# else
211 typename std::enable_if<Has_data_size<T>::value && !Has_value_type<T>::value, bool>::type
212# endif
213 indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
214 return indicate(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
215 }
216
217# else
218
229 template <typename T>
230 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value, bool>::type notify(
231 const T& value, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
232 if constexpr (Has_data_size<T>::value) {
233 if constexpr (Has_value_type<T>::value) {
234 return notify(reinterpret_cast<const uint8_t*>(value.data()), value.size() * sizeof(typename T::value_type), connHandle);
235 } else {
236 return notify(reinterpret_cast<const uint8_t*>(value.data()), value.size(), connHandle);
237 }
238 } else if constexpr (Has_c_str_length<T>::value) {
239 return notify(reinterpret_cast<const uint8_t*>(value.c_str()), value.length(), connHandle);
240 } else {
241 return notify(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
242 }
243 }
244
255 template <typename T>
256 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value, bool>::type indicate(
257 const T& value, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
258 if constexpr (Has_data_size<T>::value) {
259 if constexpr (Has_value_type<T>::value) {
260 return indicate(reinterpret_cast<const uint8_t*>(value.data()), value.size() * sizeof(typename T::value_type), connHandle);
261 } else {
262 return indicate(reinterpret_cast<const uint8_t*>(value.data()), value.size(), connHandle);
263 }
264 } else if constexpr (Has_c_str_length<T>::value) {
265 return indicate(reinterpret_cast<const uint8_t*>(value.c_str()), value.length(), connHandle);
266 } else {
267 return indicate(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
268 }
269 }
270# endif
271
272 private:
273 friend class NimBLEServer;
274 friend class NimBLEService;
275
276 void setService(NimBLEService* pService);
277 void readEvent(NimBLEConnInfo& connInfo) override;
278 void writeEvent(const uint8_t* val, uint16_t len, NimBLEConnInfo& connInfo) override;
279 bool sendValue(const uint8_t* value,
280 size_t length,
281 bool is_notification = true,
282 uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
283
284 NimBLECharacteristicCallbacks* m_pCallbacks{nullptr};
285 NimBLEService* m_pService{nullptr};
286 std::vector<NimBLEDescriptor*> m_vDescriptors{};
287}; // NimBLECharacteristic
288
297 public:
299 virtual void onRead(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
300 virtual void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
301 virtual void onStatus(NimBLECharacteristic* pCharacteristic, int code); // deprecated
302 virtual void onStatus(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo, int code);
303 virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo, uint16_t subValue);
304};
305
306#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
307#endif // NIMBLE_CPP_CHARACTERISTIC_H_
Descriptor for Characteristic Presentation Format.
Definition NimBLE2904.h:39
Callbacks that can be associated with a BLE characteristic to inform of events.
Definition NimBLECharacteristic.h:296
virtual void onSubscribe(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo, uint16_t subValue)
Callback function called when a client changes subscription status.
Definition NimBLECharacteristic.cpp:423
virtual void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
Callback function to support a read request.
Definition NimBLECharacteristic.cpp:377
virtual void onStatus(NimBLECharacteristic *pCharacteristic, int code)
Callback function to support a Notify/Indicate Status report.
Definition NimBLECharacteristic.cpp:397
virtual void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
Callback function to support a write request.
Definition NimBLECharacteristic.cpp:386
The model of a BLE Characteristic.
Definition NimBLECharacteristic.h:41
uint16_t getProperties() const
Get the properties of the characteristic.
Definition NimBLECharacteristic.cpp:199
NimBLEDescriptor * getDescriptorByHandle(uint16_t handle) const
Return the BLE Descriptor for the given handle.
Definition NimBLECharacteristic.cpp:186
void setCallbacks(NimBLECharacteristicCallbacks *pCallbacks)
Set the callback handlers for this characteristic.
Definition NimBLECharacteristic.cpp:338
void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc=false)
Remove a descriptor from the characteristic.
Definition NimBLECharacteristic.cpp:136
bool indicate(const T &s, uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Template to send a indication with a value from a class that has a c_str() and length() method.
Definition NimBLECharacteristic.h:178
NimBLEDescriptor * getDescriptorByUUID(const char *uuid) const
Return the BLE Descriptor for the given UUID.
Definition NimBLECharacteristic.cpp:163
~NimBLECharacteristic()
Destructor.
Definition NimBLECharacteristic.cpp:53
NimBLEService * getService() const
Get the service that owns this characteristic.
Definition NimBLECharacteristic.cpp:206
bool notify(const T &v, uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Template to send a notification with a value from a struct or array.
Definition NimBLECharacteristic.h:95
bool indicate(const T &v, uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Template to send an indication with a value from a struct or array.
Definition NimBLECharacteristic.h:163
NimBLEDescriptor * createDescriptor(const char *uuid, uint32_t properties=NIMBLE_PROPERTY::READ|NIMBLE_PROPERTY::WRITE, uint16_t maxLen=BLE_ATT_ATTR_MAX_LEN)
Create a new BLE Descriptor associated with this characteristic.
Definition NimBLECharacteristic.cpp:66
bool indicate(uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Send an indication.
Definition NimBLECharacteristic.cpp:220
bool notify(const T &s, uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Template to send a notification with a value from a class that has a c_str() and length() method.
Definition NimBLECharacteristic.h:110
NimBLECharacteristicCallbacks * getCallbacks() const
Get the callback handlers for this characteristic.
Definition NimBLECharacteristic.cpp:349
bool notify(uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Send a notification.
Definition NimBLECharacteristic.cpp:242
NimBLE2904 * create2904()
Create a Characteristic Presentation Format Descriptor for this characteristic.
Definition NimBLECharacteristic.cpp:94
void addDescriptor(NimBLEDescriptor *pDescriptor)
Add a descriptor to the characteristic.
Definition NimBLECharacteristic.cpp:104
std::string toString() const
Return a string representation of the characteristic.
Definition NimBLECharacteristic.cpp:357
Connection information.
Definition NimBLEConnInfo.h:32
A model of a BLE descriptor.
Definition NimBLEDescriptor.h:33
The model of a BLE server.
Definition NimBLEServer.h:62
The model of a BLE service.
Definition NimBLEService.h:34
A model of a BLE UUID.
Definition NimBLEUUID.h:41