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"
66 NIMBLE_PROPERTY::READ |
67 NIMBLE_PROPERTY::WRITE,
71 NIMBLE_PROPERTY::READ |
72 NIMBLE_PROPERTY::WRITE,
86 void notify(
bool is_notification =
true);
87 void notify(std::string value,
bool is_notification =
true);
93 NIMBLE_PROPERTY::READ |
94 NIMBLE_PROPERTY::WRITE,
95 uint16_t max_len = 100);
98 NIMBLE_PROPERTY::READ |
99 NIMBLE_PROPERTY::WRITE,
100 uint16_t max_len = 100);
108 std::string
getValue(time_t *timestamp =
nullptr);
120 T
getValue(time_t *timestamp =
nullptr,
bool skipSizeCheck =
false) {
122 if(!skipSizeCheck && value.size() <
sizeof(T))
return T();
123 const char *pData = value.data();
124 return *((T *)pData);
127 void setValue(
const uint8_t* data,
size_t size);
128 void setValue(
const std::string &value);
147 void setSubscribe(
struct ble_gap_event *event);
148 static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
149 struct ble_gatt_access_ctxt *ctxt,
void *arg);
153 uint16_t m_properties;
157 std::vector<NimBLEDescriptor*> m_dscVec;
161 std::vector<std::pair<uint16_t, uint16_t>> m_subscribedVec;
183 ERROR_INDICATE_DISABLED,
184 ERROR_NOTIFY_DISABLED,
187 ERROR_INDICATE_TIMEOUT,
188 ERROR_INDICATE_FAILURE
Callbacks that can be associated with a BLE characteristic to inform of events.
Definition: NimBLECharacteristic.h:172
Status
An enum to provide the callback the status of the notification/indication, implemented for backward c...
Definition: NimBLECharacteristic.h:180
virtual void onNotify(NimBLECharacteristic *pCharacteristic)
Callback function to support a Notify request.
Definition: NimBLECharacteristic.cpp:616
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:642
virtual void onRead(NimBLECharacteristic *pCharacteristic)
Callback function to support a read request.
Definition: NimBLECharacteristic.cpp:582
virtual void onWrite(NimBLECharacteristic *pCharacteristic)
Callback function to support a write request.
Definition: NimBLECharacteristic.cpp:599
virtual void onStatus(NimBLECharacteristic *pCharacteristic, Status s, int code)
Callback function to support a Notify/Indicate Status report.
Definition: NimBLECharacteristic.cpp:627
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:62
NimBLEDescriptor * getDescriptorByHandle(uint16_t handle)
Return the BLE Descriptor for the given handle.
Definition: NimBLECharacteristic.cpp:182
NimBLECharacteristicCallbacks * getCallbacks()
Get the callback handlers for this characteristic.
Definition: NimBLECharacteristic.cpp:513
NimBLEUUID getUUID()
Get the UUID of the characteristic.
Definition: NimBLECharacteristic.cpp:227
void setCallbacks(NimBLECharacteristicCallbacks *pCallbacks)
Set the callback handlers for this characteristic.
Definition: NimBLECharacteristic.cpp:502
void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc=false)
Remove a descriptor from the characterisitc.
Definition: NimBLECharacteristic.cpp:130
void indicate()
Send an indication. An indication is a transmission of up to the first 20 bytes of the characteristic...
Definition: NimBLECharacteristic.cpp:391
std::string getValue(time_t *timestamp=nullptr)
Retrieve the current value of the characteristic.
Definition: NimBLECharacteristic.cpp:236
std::string toString()
Return a string representation of the characteristic.
Definition: NimBLECharacteristic.cpp:559
size_t getDataLength()
Retrieve the the current data length of the characteristic.
Definition: NimBLECharacteristic.cpp:252
uint16_t getProperties()
Get the properties of the characteristic.
Definition: NimBLECharacteristic.cpp:205
void setValue(const uint8_t *data, size_t size)
Set the value of the characteristic.
Definition: NimBLECharacteristic.cpp:523
T getValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
A template to convert the characteristic data to <type>.
Definition: NimBLECharacteristic.h:120
~NimBLECharacteristic()
Destructor.
Definition: NimBLECharacteristic.cpp:59
NimBLEService * getService()
Get the service associated with this characteristic.
Definition: NimBLECharacteristic.cpp:213
NimBLECharacteristic(const char *uuid, uint16_t properties=NIMBLE_PROPERTY::READ|NIMBLE_PROPERTY::WRITE, NimBLEService *pService=nullptr)
Construct a characteristic.
Definition: NimBLECharacteristic.cpp:35
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:404
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:73
void setValue(const T &s)
Convenience template to set the characteristic value to <type>val.
Definition: NimBLECharacteristic.h:134
NimBLEDescriptor * getDescriptorByUUID(const char *uuid)
Return the BLE Descriptor for the given UUID.
Definition: NimBLECharacteristic.cpp:158
uint16_t getHandle()
Get the handle of the characteristic.
Definition: NimBLECharacteristic.cpp:196
size_t getSubscribedCount()
Get the number of clients subscribed to the characteristic.
Definition: NimBLECharacteristic.cpp:334
void addDescriptor(NimBLEDescriptor *pDescriptor)
Add a descriptor to the characteristic.
Definition: NimBLECharacteristic.cpp:104
A model of a BLE descriptor.
Definition: NimBLEDescriptor.h:42
The model of a BLE server.
Definition: NimBLEServer.h:42
The model of a BLE service.
Definition: NimBLEService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37