14 #ifndef MAIN_NIMBLECHARACTERISTIC_H_
15 #define MAIN_NIMBLECHARACTERISTIC_H_
16 #include "sdkconfig.h"
17 #if defined(CONFIG_BT_ENABLED)
20 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
22 #include "host/ble_hs.h"
29 READ = BLE_GATT_CHR_F_READ,
30 READ_ENC = BLE_GATT_CHR_F_READ_ENC,
31 READ_AUTHEN = BLE_GATT_CHR_F_READ_AUTHEN,
32 READ_AUTHOR = BLE_GATT_CHR_F_READ_AUTHOR,
33 WRITE = BLE_GATT_CHR_F_WRITE,
34 WRITE_NR = BLE_GATT_CHR_F_WRITE_NO_RSP,
35 WRITE_ENC = BLE_GATT_CHR_F_WRITE_ENC,
36 WRITE_AUTHEN = BLE_GATT_CHR_F_WRITE_AUTHEN,
37 WRITE_AUTHOR = BLE_GATT_CHR_F_WRITE_AUTHOR,
38 BROADCAST = BLE_GATT_CHR_F_BROADCAST,
39 NOTIFY = BLE_GATT_CHR_F_NOTIFY,
40 INDICATE = BLE_GATT_CHR_F_INDICATE
43 #include "NimBLEService.h"
44 #include "NimBLEDescriptor.h"
64 NIMBLE_PROPERTY::READ |
65 NIMBLE_PROPERTY::WRITE,
69 NIMBLE_PROPERTY::READ |
70 NIMBLE_PROPERTY::WRITE,
84 void notify(
bool is_notification =
true);
89 NIMBLE_PROPERTY::READ |
90 NIMBLE_PROPERTY::WRITE,
91 uint16_t max_len = 100);
94 NIMBLE_PROPERTY::READ |
95 NIMBLE_PROPERTY::WRITE,
96 uint16_t max_len = 100);
104 std::string
getValue(time_t *timestamp =
nullptr);
116 T
getValue(time_t *timestamp =
nullptr,
bool skipSizeCheck =
false) {
118 if(!skipSizeCheck && value.size() <
sizeof(T))
return T();
119 const char *pData = value.data();
120 return *((T *)pData);
123 void setValue(
const uint8_t* data,
size_t size);
124 void setValue(
const std::string &value);
143 void setSubscribe(
struct ble_gap_event *event);
144 static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
145 struct ble_gatt_access_ctxt *ctxt,
void *arg);
149 uint16_t m_properties;
153 std::vector<NimBLEDescriptor*> m_dscVec;
154 portMUX_TYPE m_valMux;
158 std::vector<std::pair<uint16_t, uint16_t>> m_subscribedVec;
180 ERROR_INDICATE_DISABLED,
181 ERROR_NOTIFY_DISABLED,
184 ERROR_INDICATE_TIMEOUT,
185 ERROR_INDICATE_FAILURE
Callbacks that can be associated with a BLE characteristic to inform of events.
Definition: NimBLECharacteristic.h:169
Status
An enum to provide the callback the status of the notification/indication, implemented for backward c...
Definition: NimBLECharacteristic.h:177
virtual void onNotify(NimBLECharacteristic *pCharacteristic)
Callback function to support a Notify request.
Definition: NimBLECharacteristic.cpp:611
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:637
virtual void onRead(NimBLECharacteristic *pCharacteristic)
Callback function to support a read request.
Definition: NimBLECharacteristic.cpp:577
virtual void onWrite(NimBLECharacteristic *pCharacteristic)
Callback function to support a write request.
Definition: NimBLECharacteristic.cpp:594
virtual void onStatus(NimBLECharacteristic *pCharacteristic, Status s, int code)
Callback function to support a Notify/Indicate Status report.
Definition: NimBLECharacteristic.cpp:622
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:60
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:508
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:497
void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc=false)
Remove a descriptor from the characterisitc.
Definition: NimBLECharacteristic.cpp:133
void indicate()
Send an indication. An indication is a transmission of up to the first 20 bytes of the characteristic...
Definition: NimBLECharacteristic.cpp:396
std::string getValue(time_t *timestamp=nullptr)
Retrieve the current value of the characteristic.
Definition: NimBLECharacteristic.cpp:239
std::string toString()
Return a string representation of the characteristic.
Definition: NimBLECharacteristic.cpp:554
size_t getDataLength()
Retrieve the the current data length of the characteristic.
Definition: NimBLECharacteristic.cpp:255
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.
Definition: NimBLECharacteristic.cpp:518
T getValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
A template to convert the characteristic data to <type>.
Definition: NimBLECharacteristic.h:116
~NimBLECharacteristic()
Destructor.
Definition: NimBLECharacteristic.cpp:62
NimBLEService * getService()
Get the service associated with this characteristic.
Definition: NimBLECharacteristic.cpp:216
NimBLECharacteristic(const char *uuid, uint16_t properties=NIMBLE_PROPERTY::READ|NIMBLE_PROPERTY::WRITE, NimBLEService *pService=nullptr)
Construct a characteristic.
Definition: NimBLECharacteristic.cpp:37
void notify(bool is_notification=true)
Send a notification. A notification is a transmission of up to the first 20 bytes of the characterist...
Definition: NimBLECharacteristic.cpp:408
NimBLEDescriptor * createDescriptor(const char *uuid, uint32_t properties=NIMBLE_PROPERTY::READ|NIMBLE_PROPERTY::WRITE, uint16_t max_len=100)
Create a new BLE Descriptor associated with this characteristic.
Definition: NimBLECharacteristic.cpp:76
void setValue(const T &s)
Convenience template to set the characteristic value to <type>val.
Definition: NimBLECharacteristic.h:130
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 addDescriptor(NimBLEDescriptor *pDescriptor)
Add a descriptor to the characteristic.
Definition: NimBLECharacteristic.cpp:107
A model of a BLE descriptor.
Definition: NimBLEDescriptor.h:44
The model of a BLE server.
Definition: NimBLEServer.h:42
The model of a BLE service.
Definition: NimBLEService.h:36
A model of a BLE UUID.
Definition: NimBLEUUID.h:31