18#ifndef NIMBLE_CPP_CHARACTERISTIC_H_
19#define NIMBLE_CPP_CHARACTERISTIC_H_
21#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
29# include "NimBLELocalValueAttribute.h"
43 uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
44 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
47 uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
48 uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
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;
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);
79# if __cplusplus < 201703L
90 typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value,
bool>::type
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);
101 template <
typename T>
105 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value,
bool>::type
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);
116 template <
typename T>
120 typename std::enable_if<Has_data_size<T>::value,
bool>::type
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);
132 template <
typename T>
136 typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value,
bool>::type
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);
147 template <
typename T>
151 typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value,
bool>::type
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);
162 template <
typename T>
166 typename std::enable_if<Has_data_size<T>::value,
bool>::type
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);
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);
192 return notify(
reinterpret_cast<const uint8_t*
>(&value),
sizeof(value), connHandle);
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);
214 return indicate(
reinterpret_cast<const uint8_t*
>(&value),
sizeof(value), connHandle);
225 void writeEvent(
const uint8_t* val, uint16_t len,
NimBLEConnInfo& connInfo)
override;
226 bool sendValue(
const uint8_t* value,
228 bool is_notification =
true,
229 uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE)
const;
233 std::vector<NimBLEDescriptor*> m_vDescriptors{};
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