esp-nimble-cpp 2.3.0
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#include "nimconfig.h"
21#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
22
24class NimBLEService;
27class NimBLE2904;
28
29# include "NimBLELocalValueAttribute.h"
30
31# include <string>
32# include <vector>
33
40class NimBLECharacteristic : public NimBLELocalValueAttribute {
41 public:
42 NimBLECharacteristic(const char* uuid,
43 uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
44 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
45 NimBLEService* pService = nullptr);
47 uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
48 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
49 NimBLEService* pService = nullptr);
50
52
53 std::string toString() const;
54 void addDescriptor(NimBLEDescriptor* pDescriptor);
55 void removeDescriptor(NimBLEDescriptor* pDescriptor, bool deleteDsc = false);
56 uint16_t getProperties() const;
58 bool indicate(uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
59 bool indicate(const uint8_t* value, size_t length, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
60 bool notify(uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
61 bool notify(const uint8_t* value, size_t length, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
62
63 NimBLEDescriptor* createDescriptor(const char* uuid,
64 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
65 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
67 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
68 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
70 NimBLEDescriptor* getDescriptorByUUID(const char* uuid) const;
72 NimBLEDescriptor* getDescriptorByHandle(uint16_t handle) const;
74
76
77 /*********************** Template Functions ************************/
78
79# if __cplusplus < 201703L
86 template <typename T>
87# ifdef _DOXYGEN_
88 bool
89# else
90 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value && !Has_c_str_length<T>::value &&
91 !Has_data_size<T>::value,
92 bool>::type
93# endif
94 notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
95 return notify(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
96 }
97
103 template <typename T>
104# ifdef _DOXYGEN_
105 bool
106# else
107 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
108# endif
109 notify(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
110 return notify(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
111 }
112
118 template <typename T>
119# ifdef _DOXYGEN_
120 bool
121# else
122 typename std::enable_if<Has_data_size<T>::value, bool>::type
123# endif
124 notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
125 return notify(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
126 }
127
134 template <typename T>
135# ifdef _DOXYGEN_
136 bool
137# else
138 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value && !Has_c_str_length<T>::value &&
139 !Has_data_size<T>::value,
140 bool>::type
141# endif
142 indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
143 return indicate(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
144 }
145
151 template <typename T>
152# ifdef _DOXYGEN_
153 bool
154# else
155 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
156# endif
157 indicate(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
158 return indicate(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
159 }
160
166 template <typename T>
167# ifdef _DOXYGEN_
168 bool
169# else
170 typename std::enable_if<Has_data_size<T>::value, bool>::type
171# endif
172 indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
173 return indicate(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
174 }
175
176# else
177
188 template <typename T>
189 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value, bool>::type notify(
190 const T& value, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
191 if constexpr (Has_data_size<T>::value) {
192 return notify(reinterpret_cast<const uint8_t*>(value.data()), value.size(), connHandle);
193 } else if constexpr (Has_c_str_length<T>::value) {
194 return notify(reinterpret_cast<const uint8_t*>(value.c_str()), value.length(), connHandle);
195 } else {
196 return notify(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
197 }
198 }
199
210 template <typename T>
211 typename std::enable_if<!std::is_pointer<T>::value && !std::is_array<T>::value, bool>::type indicate(
212 const T& value, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
213 if constexpr (Has_data_size<T>::value) {
214 return indicate(reinterpret_cast<const uint8_t*>(value.data()), value.size(), connHandle);
215 } else if constexpr (Has_c_str_length<T>::value) {
216 return indicate(reinterpret_cast<const uint8_t*>(value.c_str()), value.length(), connHandle);
217 } else {
218 return indicate(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
219 }
220 }
221# endif
222
223 private:
224 friend class NimBLEServer;
225 friend class NimBLEService;
226
227 void setService(NimBLEService* pService);
228 void readEvent(NimBLEConnInfo& connInfo) override;
229 void writeEvent(const uint8_t* val, uint16_t len, NimBLEConnInfo& connInfo) override;
230 bool sendValue(const uint8_t* value,
231 size_t length,
232 bool is_notification = true,
233 uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
234
235 NimBLECharacteristicCallbacks* m_pCallbacks{nullptr};
236 NimBLEService* m_pService{nullptr};
237 std::vector<NimBLEDescriptor*> m_vDescriptors{};
238}; // NimBLECharacteristic
239
248 public:
250 virtual void onRead(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
251 virtual void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
252 virtual void onStatus(NimBLECharacteristic* pCharacteristic, int code);
253 virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo, uint16_t subValue);
254};
255
256#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
257#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:247
virtual void onSubscribe(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo, uint16_t subValue)
Callback function called when a client changes subscription status.
Definition NimBLECharacteristic.cpp:412
virtual void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
Callback function to support a read request.
Definition NimBLECharacteristic.cpp:378
virtual void onStatus(NimBLECharacteristic *pCharacteristic, int code)
Callback function to support a Notify/Indicate Status report.
Definition NimBLECharacteristic.cpp:398
virtual void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
Callback function to support a write request.
Definition NimBLECharacteristic.cpp:387
The model of a BLE Characteristic.
Definition NimBLECharacteristic.h:40
uint16_t getProperties() const
Get the properties of the characteristic.
Definition NimBLECharacteristic.cpp:200
NimBLEDescriptor * getDescriptorByHandle(uint16_t handle) const
Return the BLE Descriptor for the given handle.
Definition NimBLECharacteristic.cpp:187
void setCallbacks(NimBLECharacteristicCallbacks *pCallbacks)
Set the callback handlers for this characteristic.
Definition NimBLECharacteristic.cpp:339
void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc=false)
Remove a descriptor from the characteristic.
Definition NimBLECharacteristic.cpp:137
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:157
NimBLEDescriptor * getDescriptorByUUID(const char *uuid) const
Return the BLE Descriptor for the given UUID.
Definition NimBLECharacteristic.cpp:164
~NimBLECharacteristic()
Destructor.
Definition NimBLECharacteristic.cpp:54
NimBLEService * getService() const
Get the service that owns this characteristic.
Definition NimBLECharacteristic.cpp:207
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:94
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:142
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:67
bool indicate(uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Send an indication.
Definition NimBLECharacteristic.cpp:221
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:109
NimBLECharacteristicCallbacks * getCallbacks() const
Get the callback handlers for this characteristic.
Definition NimBLECharacteristic.cpp:350
bool notify(uint16_t connHandle=BLE_HS_CONN_HANDLE_NONE) const
Send a notification.
Definition NimBLECharacteristic.cpp:243
NimBLE2904 * create2904()
Create a Characteristic Presentation Format Descriptor for this characteristic.
Definition NimBLECharacteristic.cpp:95
void addDescriptor(NimBLEDescriptor *pDescriptor)
Add a descriptor to the characteristic.
Definition NimBLECharacteristic.cpp:105
std::string toString() const
Return a string representation of the characteristic.
Definition NimBLECharacteristic.cpp:358
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:60
The model of a BLE service.
Definition NimBLEService.h:34
A model of a BLE UUID.
Definition NimBLEUUID.h:41