14#ifndef MAIN_NIMBLECHARACTERISTIC_H_
15#define MAIN_NIMBLECHARACTERISTIC_H_
17#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
19#if defined(CONFIG_NIMBLE_CPP_IDF)
20#include "host/ble_hs.h"
22#include "nimble/nimble/host/include/host/ble_hs.h"
31 READ = BLE_GATT_CHR_F_READ,
32 READ_ENC = BLE_GATT_CHR_F_READ_ENC,
33 READ_AUTHEN = BLE_GATT_CHR_F_READ_AUTHEN,
34 READ_AUTHOR = BLE_GATT_CHR_F_READ_AUTHOR,
35 WRITE = BLE_GATT_CHR_F_WRITE,
36 WRITE_NR = BLE_GATT_CHR_F_WRITE_NO_RSP,
37 WRITE_ENC = BLE_GATT_CHR_F_WRITE_ENC,
38 WRITE_AUTHEN = BLE_GATT_CHR_F_WRITE_AUTHEN,
39 WRITE_AUTHOR = BLE_GATT_CHR_F_WRITE_AUTHOR,
40 BROADCAST = BLE_GATT_CHR_F_BROADCAST,
41 NOTIFY = BLE_GATT_CHR_F_NOTIFY,
42 INDICATE = BLE_GATT_CHR_F_INDICATE
45#include "NimBLEService.h"
46#include "NimBLEDescriptor.h"
47#include "NimBLEAttValue.h"
67 NIMBLE_PROPERTY::READ |
68 NIMBLE_PROPERTY::WRITE,
69 uint16_t max_len = BLE_ATT_ATTR_MAX_LEN,
73 NIMBLE_PROPERTY::READ |
74 NIMBLE_PROPERTY::WRITE,
75 uint16_t max_len = BLE_ATT_ATTR_MAX_LEN,
84 void indicate(
const uint8_t* value,
size_t length);
85 void indicate(
const std::vector<uint8_t>& value);
86 void notify(
bool is_notification =
true);
87 void notify(
const uint8_t* value,
size_t length,
bool is_notification =
true);
88 void notify(
const std::vector<uint8_t>& value,
bool is_notification =
true);
99 void setValue(
const uint8_t* data,
size_t size);
100 void setValue(
const std::vector<uint8_t>& vec);
103 uint32_t properties =
104 NIMBLE_PROPERTY::READ |
105 NIMBLE_PROPERTY::WRITE,
106 uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);;
108 uint32_t properties =
109 NIMBLE_PROPERTY::READ |
110 NIMBLE_PROPERTY::WRITE,
111 uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
134 T
getValue(time_t *timestamp =
nullptr,
bool skipSizeCheck =
false) {
135 return m_value.
getValue<T>(timestamp, skipSizeCheck);
149 typename std::enable_if<Has_c_str_len<T>::value,
void>::type
151 notify(
const T& value,
bool is_notification =
true) {
152 notify((uint8_t*)value.c_str(), value.length(), is_notification);
165 typename std::enable_if<Has_c_str_len<T>::value,
void>::type
168 indicate((uint8_t*)value.c_str(), value.length());
177 void setSubscribe(
struct ble_gap_event *event);
178 static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
179 struct ble_gatt_access_ctxt *ctxt,
void *arg);
183 uint16_t m_properties;
187 std::vector<NimBLEDescriptor*> m_dscVec;
190 std::vector<std::pair<uint16_t, uint16_t>> m_subscribedVec;
212 ERROR_INDICATE_DISABLED,
213 ERROR_NOTIFY_DISABLED,
216 ERROR_INDICATE_TIMEOUT,
217 ERROR_INDICATE_FAILURE
A specialized container class to hold BLE attribute values.
Definition: NimBLEAttValue.h:61
bool setValue(const uint8_t *value, uint16_t len)
Set the value from a buffer.
Definition: NimBLEAttValue.h:380
const uint8_t * getValue(time_t *timestamp)
Get a pointer to the value buffer with timestamp.
Definition: NimBLEAttValue.h:369
Callbacks that can be associated with a BLE characteristic to inform of events.
Definition: NimBLECharacteristic.h:201
Status
An enum to provide the callback the status of the notification/indication, implemented for backward c...
Definition: NimBLECharacteristic.h:209
virtual void onNotify(NimBLECharacteristic *pCharacteristic)
Callback function to support a Notify request.
Definition: NimBLECharacteristic.cpp:633
virtual void onSubscribe(NimBLECharacteristic *pCharacteristic, ble_gap_conn_desc *desc, uint16_t subValue)
Callback function called when a client changes subscription status.
Definition: NimBLECharacteristic.cpp:659
virtual void onRead(NimBLECharacteristic *pCharacteristic)
Callback function to support a read request.
Definition: NimBLECharacteristic.cpp:599
virtual void onWrite(NimBLECharacteristic *pCharacteristic)
Callback function to support a write request.
Definition: NimBLECharacteristic.cpp:616
virtual void onStatus(NimBLECharacteristic *pCharacteristic, Status s, int code)
Callback function to support a Notify/Indicate Status report.
Definition: NimBLECharacteristic.cpp:644
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:63
NimBLEDescriptor * getDescriptorByHandle(uint16_t handle)
Return the BLE Descriptor for the given handle.
Definition: NimBLECharacteristic.cpp:185
NimBLECharacteristicCallbacks * getCallbacks()
Get the callback handlers for this characteristic.
Definition: NimBLECharacteristic.cpp:540
NimBLEDescriptor * createDescriptor(const char *uuid, uint32_t properties=NIMBLE_PROPERTY::READ|NIMBLE_PROPERTY::WRITE, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
Create a new BLE Descriptor associated with this characteristic.
Definition: NimBLECharacteristic.cpp:76
NimBLEUUID getUUID()
Get the UUID of the characteristic.
Definition: NimBLECharacteristic.cpp:230
void setCallbacks(NimBLECharacteristicCallbacks *pCallbacks)
Set the callback handlers for this characteristic.
Definition: NimBLECharacteristic.cpp:529
void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc=false)
Remove a descriptor from the characteristic.
Definition: NimBLECharacteristic.cpp:133
void indicate()
Send an indication.
Definition: NimBLECharacteristic.cpp:394
std::string toString()
Return a string representation of the characteristic.
Definition: NimBLECharacteristic.cpp:576
size_t getDataLength()
Retrieve the the current data length of the characteristic.
Definition: NimBLECharacteristic.cpp:252
NimBLEAttValue getValue(time_t *timestamp=nullptr)
Retrieve the current value of the characteristic.
Definition: NimBLECharacteristic.cpp:239
uint16_t getProperties()
Get the properties of the characteristic.
Definition: NimBLECharacteristic.cpp:208
void setValue(const uint8_t *data, size_t size)
Set the value of the characteristic from a data buffer .
Definition: NimBLECharacteristic.cpp:550
T getValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
Template to convert the characteristic data to <type>.
Definition: NimBLECharacteristic.h:134
~NimBLECharacteristic()
Destructor.
Definition: NimBLECharacteristic.cpp:62
NimBLEService * getService()
Get the service associated with this characteristic.
Definition: NimBLECharacteristic.cpp:216
void notify(bool is_notification=true)
Send a notification or indication.
Definition: NimBLECharacteristic.cpp:422
void setValue(const T &s)
Template to set the characteristic value to <type>val.
Definition: NimBLECharacteristic.h:123
NimBLEDescriptor * getDescriptorByUUID(const char *uuid)
Return the BLE Descriptor for the given UUID.
Definition: NimBLECharacteristic.cpp:161
uint16_t getHandle()
Get the handle of the characteristic.
Definition: NimBLECharacteristic.cpp:199
size_t getSubscribedCount()
Get the number of clients subscribed to the characteristic.
Definition: NimBLECharacteristic.cpp:339
void notify(const T &value, bool is_notification=true)
Template to send a notification from a class type that has a c_str() and length() method.
Definition: NimBLECharacteristic.h:151
void addDescriptor(NimBLEDescriptor *pDescriptor)
Add a descriptor to the characteristic.
Definition: NimBLECharacteristic.cpp:107
void indicate(const T &value)
Template to send an indication from a class type that has a c_str() and length() method.
Definition: NimBLECharacteristic.h:167
A model of a BLE descriptor.
Definition: NimBLEDescriptor.h:35
The model of a BLE server.
Definition: NimBLEServer.h:46
The model of a BLE service.
Definition: NimBLEService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37