esp-nimble-cpp  1.4.0
NimBLECharacteristic.h
1 /*
2  * NimBLECharacteristic.h
3  *
4  * Created: on March 3, 2020
5  * Author H2zero
6  *
7  * Originally:
8  * BLECharacteristic.h
9  *
10  * Created on: Jun 22, 2017
11  * Author: kolban
12  */
13 
14 #ifndef MAIN_NIMBLECHARACTERISTIC_H_
15 #define MAIN_NIMBLECHARACTERISTIC_H_
16 #include "nimconfig.h"
17 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
18 
19 #if defined(CONFIG_NIMBLE_CPP_IDF)
20 #include "host/ble_hs.h"
21 #else
22 #include "nimble/nimble/host/include/host/ble_hs.h"
23 #endif
24 
25 /**** FIX COMPILATION ****/
26 #undef min
27 #undef max
28 /**************************/
29 
30 typedef enum {
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
43 } NIMBLE_PROPERTY;
44 
45 #include "NimBLEService.h"
46 #include "NimBLEDescriptor.h"
47 #include "NimBLEAttValue.h"
48 
49 #include <string>
50 #include <vector>
51 
52 class NimBLEService;
53 class NimBLEDescriptor;
55 
56 
64 public:
65  NimBLECharacteristic(const char* uuid,
66  uint16_t properties =
67  NIMBLE_PROPERTY::READ |
68  NIMBLE_PROPERTY::WRITE,
69  uint16_t max_len = BLE_ATT_ATTR_MAX_LEN,
70  NimBLEService* pService = nullptr);
71  NimBLECharacteristic(const NimBLEUUID &uuid,
72  uint16_t properties =
73  NIMBLE_PROPERTY::READ |
74  NIMBLE_PROPERTY::WRITE,
75  uint16_t max_len = BLE_ATT_ATTR_MAX_LEN,
76  NimBLEService* pService = nullptr);
77 
79 
80  uint16_t getHandle();
82  std::string toString();
83  void indicate();
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);
89  size_t getSubscribedCount();
90  void addDescriptor(NimBLEDescriptor *pDescriptor);
91  NimBLEDescriptor* getDescriptorByUUID(const char* uuid);
93  NimBLEDescriptor* getDescriptorByHandle(uint16_t handle);
94  void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc = false);
96  uint16_t getProperties();
97  NimBLEAttValue getValue(time_t *timestamp = nullptr);
98  size_t getDataLength();
99  void setValue(const uint8_t* data, size_t size);
100  void setValue(const std::vector<uint8_t>& vec);
102  NimBLEDescriptor* createDescriptor(const char* uuid,
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);
112 
114 
115 
116  /*********************** Template Functions ************************/
117 
122  template<typename T>
123  void setValue(const T &s) { m_value.setValue<T>(s); }
124 
133  template<typename T>
134  T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
135  return m_value.getValue<T>(timestamp, skipSizeCheck);
136  }
137 
145  template<typename T>
146 #ifdef _DOXYGEN_
147  void
148 #else
149  typename std::enable_if<Has_c_str_len<T>::value, void>::type
150 #endif
151  notify(const T& value, bool is_notification = true) {
152  notify((uint8_t*)value.c_str(), value.length(), is_notification);
153  }
154 
161  template<typename T>
162 #ifdef _DOXYGEN_
163  void
164 #else
165  typename std::enable_if<Has_c_str_len<T>::value, void>::type
166 #endif
167  indicate(const T& value) {
168  indicate((uint8_t*)value.c_str(), value.length());
169  }
170 
171 private:
172 
173  friend class NimBLEServer;
174  friend class NimBLEService;
175 
176  void setService(NimBLEService *pService);
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);
180 
181  NimBLEUUID m_uuid;
182  uint16_t m_handle;
183  uint16_t m_properties;
184  NimBLECharacteristicCallbacks* m_pCallbacks;
185  NimBLEService* m_pService;
186  NimBLEAttValue m_value;
187  std::vector<NimBLEDescriptor*> m_dscVec;
188  uint8_t m_removed;
189 
190  std::vector<std::pair<uint16_t, uint16_t>> m_subscribedVec;
191 }; // NimBLECharacteristic
192 
193 
202 public:
203 
209  typedef enum {
210  SUCCESS_INDICATE,
211  SUCCESS_NOTIFY,
212  ERROR_INDICATE_DISABLED,
213  ERROR_NOTIFY_DISABLED,
214  ERROR_GATT,
215  ERROR_NO_CLIENT,
216  ERROR_INDICATE_TIMEOUT,
217  ERROR_INDICATE_FAILURE
218  }Status;
219 
221  virtual void onRead(NimBLECharacteristic* pCharacteristic);
222  virtual void onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
223  virtual void onWrite(NimBLECharacteristic* pCharacteristic);
224  virtual void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
225  virtual void onNotify(NimBLECharacteristic* pCharacteristic);
226  virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
227  virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue);
228 };
229 
230 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
231 #endif /*MAIN_NIMBLECHARACTERISTIC_H_*/
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:628
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:654
virtual void onRead(NimBLECharacteristic *pCharacteristic)
Callback function to support a read request.
Definition: NimBLECharacteristic.cpp:594
virtual void onWrite(NimBLECharacteristic *pCharacteristic)
Callback function to support a write request.
Definition: NimBLECharacteristic.cpp:611
virtual void onStatus(NimBLECharacteristic *pCharacteristic, Status s, int code)
Callback function to support a Notify/Indicate Status report.
Definition: NimBLECharacteristic.cpp:639
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:535
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:524
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:389
std::string toString()
Return a string representation of the characteristic.
Definition: NimBLECharacteristic.cpp:571
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:545
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
NimBLECharacteristic(const char *uuid, uint16_t properties=NIMBLE_PROPERTY::READ|NIMBLE_PROPERTY::WRITE, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN, NimBLEService *pService=nullptr)
Construct a characteristic.
Definition: NimBLECharacteristic.cpp:36
void notify(bool is_notification=true)
Send a notification or indication.
Definition: NimBLECharacteristic.cpp:417
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:334
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