esp-nimble-cpp 2.0.2
Loading...
Searching...
No Matches
NimBLECharacteristic.h
1/*
2 * Copyright 2020-2024 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 && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
91# endif
92 notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
93 return notify(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
94 }
95
101 template <typename T>
102# ifdef _DOXYGEN_
103 bool
104# else
105 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
106# endif
107 notify(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
108 return notify(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
109 }
110
116 template <typename T>
117# ifdef _DOXYGEN_
118 bool
119# else
120 typename std::enable_if<Has_data_size<T>::value, bool>::type
121# endif
122 notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
123 return notify(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
124 }
125
132 template <typename T>
133# ifdef _DOXYGEN_
134 bool
135# else
136 typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
137# endif
138 indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
139 return indicate(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
140 }
141
147 template <typename T>
148# ifdef _DOXYGEN_
149 bool
150# else
151 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
152# endif
153 indicate(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
154 return indicate(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
155 }
156
162 template <typename T>
163# ifdef _DOXYGEN_
164 bool
165# else
166 typename std::enable_if<Has_data_size<T>::value, bool>::type
167# endif
168 indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
169 return indicate(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
170 }
171
172# else
173
184 template <typename T>
185 typename std::enable_if<!std::is_pointer<T>::value, bool>::type notify(const T& value,
186 uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
187 if constexpr (Has_data_size<T>::value) {
188 return notify(reinterpret_cast<const uint8_t*>(value.data()), value.size(), connHandle);
189 } else if constexpr (Has_c_str_length<T>::value) {
190 return notify(reinterpret_cast<const uint8_t*>(value.c_str()), value.length(), connHandle);
191 } else {
192 return notify(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
193 }
194 }
195
206 template <typename T>
207 typename std::enable_if<!std::is_pointer<T>::value, bool>::type indicate(
208 const T& value, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
209 if constexpr (Has_data_size<T>::value) {
210 return indicate(reinterpret_cast<const uint8_t*>(value.data()), value.size(), connHandle);
211 } else if constexpr (Has_c_str_length<T>::value) {
212 return indicate(reinterpret_cast<const uint8_t*>(value.c_str()), value.length(), connHandle);
213 } else {
214 return indicate(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
215 }
216 }
217# endif
218
219 private:
220 friend class NimBLEServer;
221 friend class NimBLEService;
222
223 void setService(NimBLEService* pService);
224 void readEvent(NimBLEConnInfo& connInfo) override;
225 void writeEvent(const uint8_t* val, uint16_t len, NimBLEConnInfo& connInfo) override;
226 bool sendValue(const uint8_t* value,
227 size_t length,
228 bool is_notification = true,
229 uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
230
231 NimBLECharacteristicCallbacks* m_pCallbacks{nullptr};
232 NimBLEService* m_pService{nullptr};
233 std::vector<NimBLEDescriptor*> m_vDescriptors{};
234}; // NimBLECharacteristic
235
244 public:
246 virtual void onRead(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
247 virtual void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
248 virtual void onStatus(NimBLECharacteristic* pCharacteristic, int code);
249 virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo, uint16_t subValue);
250};
251
252#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
253#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:243
virtual void onSubscribe(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo, uint16_t subValue)
Callback function called when a client changes subscription status.
Definition NimBLECharacteristic.cpp:396
virtual void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
Callback function to support a read request.
Definition NimBLECharacteristic.cpp:362
virtual void onStatus(NimBLECharacteristic *pCharacteristic, int code)
Callback function to support a Notify/Indicate Status report.
Definition NimBLECharacteristic.cpp:382
virtual void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
Callback function to support a write request.
Definition NimBLECharacteristic.cpp:371
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:323
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:153
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:92
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:138
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:107
NimBLECharacteristicCallbacks * getCallbacks() const
Get the callback handlers for this characteristic.
Definition NimBLECharacteristic.cpp:334
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:342
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