esp-nimble-cpp  1.3.2
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 
48 #include <string>
49 #include <vector>
50 
51 class NimBLEService;
52 class NimBLEDescriptor;
54 
55 
63 public:
64  NimBLECharacteristic(const char* uuid,
65  uint16_t properties =
66  NIMBLE_PROPERTY::READ |
67  NIMBLE_PROPERTY::WRITE,
68  NimBLEService* pService = nullptr);
69  NimBLECharacteristic(const NimBLEUUID &uuid,
70  uint16_t properties =
71  NIMBLE_PROPERTY::READ |
72  NIMBLE_PROPERTY::WRITE,
73  NimBLEService* pService = nullptr);
74 
76 
77  uint16_t getHandle();
79  std::string toString();
80 
83  getCallbacks();
84 
85  void indicate();
86  void notify(bool is_notification = true);
87  void notify(std::string value, bool is_notification = true);
88 
89  size_t getSubscribedCount();
90 
91  NimBLEDescriptor* createDescriptor(const char* uuid,
92  uint32_t properties =
93  NIMBLE_PROPERTY::READ |
94  NIMBLE_PROPERTY::WRITE,
95  uint16_t max_len = 100);
97  uint32_t properties =
98  NIMBLE_PROPERTY::READ |
99  NIMBLE_PROPERTY::WRITE,
100  uint16_t max_len = 100);
101 
102  void addDescriptor(NimBLEDescriptor *pDescriptor);
103  NimBLEDescriptor* getDescriptorByUUID(const char* uuid);
105  NimBLEDescriptor* getDescriptorByHandle(uint16_t handle);
106  void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc = false);
107 
108  std::string getValue(time_t *timestamp = nullptr);
109  size_t getDataLength();
119  template<typename T>
120  T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
121  std::string value = getValue();
122  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
123  const char *pData = value.data();
124  return *((T *)pData);
125  }
126 
127  void setValue(const uint8_t* data, size_t size);
128  void setValue(const std::string &value);
133  template<typename T>
134  void setValue(const T &s) {
135  setValue((uint8_t*)&s, sizeof(T));
136  }
137 
139  uint16_t getProperties();
140 
141 private:
142 
143  friend class NimBLEServer;
144  friend class NimBLEService;
145 
146  void setService(NimBLEService *pService);
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);
150 
151  NimBLEUUID m_uuid;
152  uint16_t m_handle;
153  uint16_t m_properties;
154  NimBLECharacteristicCallbacks* m_pCallbacks;
155  NimBLEService* m_pService;
156  std::string m_value;
157  std::vector<NimBLEDescriptor*> m_dscVec;
158  time_t m_timestamp;
159  uint8_t m_removed;
160 
161  std::vector<std::pair<uint16_t, uint16_t>> m_subscribedVec;
162 }; // NimBLECharacteristic
163 
164 
173 public:
174 
180  typedef enum {
181  SUCCESS_INDICATE,
182  SUCCESS_NOTIFY,
183  ERROR_INDICATE_DISABLED,
184  ERROR_NOTIFY_DISABLED,
185  ERROR_GATT,
186  ERROR_NO_CLIENT,
187  ERROR_INDICATE_TIMEOUT,
188  ERROR_INDICATE_FAILURE
189  }Status;
190 
192  virtual void onRead(NimBLECharacteristic* pCharacteristic);
193  virtual void onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
194  virtual void onWrite(NimBLECharacteristic* pCharacteristic);
195  virtual void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
196  virtual void onNotify(NimBLECharacteristic* pCharacteristic);
197  virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
198  virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue);
199 };
200 
201 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
202 #endif /*MAIN_NIMBLECHARACTERISTIC_H_*/
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