esp-nimble-cpp / NimBLE-Arduino  1.3.1
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 "sdkconfig.h"
17 #if defined(CONFIG_BT_ENABLED)
18 
19 #include "nimconfig.h"
20 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
21 
22 #include "host/ble_hs.h"
23 /**** FIX COMPILATION ****/
24 #undef min
25 #undef max
26 /**************************/
27 
28 typedef enum {
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
41 } NIMBLE_PROPERTY;
42 
43 #include "NimBLEService.h"
44 #include "NimBLEDescriptor.h"
45 
46 #include <string>
47 #include <vector>
48 
49 class NimBLEService;
50 class NimBLEDescriptor;
52 
53 
61 public:
62  NimBLECharacteristic(const char* uuid,
63  uint16_t properties =
64  NIMBLE_PROPERTY::READ |
65  NIMBLE_PROPERTY::WRITE,
66  NimBLEService* pService = nullptr);
67  NimBLECharacteristic(const NimBLEUUID &uuid,
68  uint16_t properties =
69  NIMBLE_PROPERTY::READ |
70  NIMBLE_PROPERTY::WRITE,
71  NimBLEService* pService = nullptr);
72 
74 
75  uint16_t getHandle();
77  std::string toString();
78 
81  getCallbacks();
82 
83  void indicate();
84  void notify(bool is_notification = true);
85  size_t getSubscribedCount();
86 
87  NimBLEDescriptor* createDescriptor(const char* uuid,
88  uint32_t properties =
89  NIMBLE_PROPERTY::READ |
90  NIMBLE_PROPERTY::WRITE,
91  uint16_t max_len = 100);
93  uint32_t properties =
94  NIMBLE_PROPERTY::READ |
95  NIMBLE_PROPERTY::WRITE,
96  uint16_t max_len = 100);
97 
98  void addDescriptor(NimBLEDescriptor *pDescriptor);
99  NimBLEDescriptor* getDescriptorByUUID(const char* uuid);
101  NimBLEDescriptor* getDescriptorByHandle(uint16_t handle);
102  void removeDescriptor(NimBLEDescriptor *pDescriptor, bool deleteDsc = false);
103 
104  std::string getValue(time_t *timestamp = nullptr);
105  size_t getDataLength();
115  template<typename T>
116  T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
117  std::string value = getValue();
118  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
119  const char *pData = value.data();
120  return *((T *)pData);
121  }
122 
123  void setValue(const uint8_t* data, size_t size);
124  void setValue(const std::string &value);
129  template<typename T>
130  void setValue(const T &s) {
131  setValue((uint8_t*)&s, sizeof(T));
132  }
133 
135  uint16_t getProperties();
136 
137 private:
138 
139  friend class NimBLEServer;
140  friend class NimBLEService;
141 
142  void setService(NimBLEService *pService);
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);
146 
147  NimBLEUUID m_uuid;
148  uint16_t m_handle;
149  uint16_t m_properties;
150  NimBLECharacteristicCallbacks* m_pCallbacks;
151  NimBLEService* m_pService;
152  std::string m_value;
153  std::vector<NimBLEDescriptor*> m_dscVec;
154  portMUX_TYPE m_valMux;
155  time_t m_timestamp;
156  uint8_t m_removed;
157 
158  std::vector<std::pair<uint16_t, uint16_t>> m_subscribedVec;
159 }; // NimBLECharacteristic
160 
161 
170 public:
171 
177  typedef enum {
178  SUCCESS_INDICATE,
179  SUCCESS_NOTIFY,
180  ERROR_INDICATE_DISABLED,
181  ERROR_NOTIFY_DISABLED,
182  ERROR_GATT,
183  ERROR_NO_CLIENT,
184  ERROR_INDICATE_TIMEOUT,
185  ERROR_INDICATE_FAILURE
186  }Status;
187 
189  virtual void onRead(NimBLECharacteristic* pCharacteristic);
190  virtual void onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
191  virtual void onWrite(NimBLECharacteristic* pCharacteristic);
192  virtual void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
193  virtual void onNotify(NimBLECharacteristic* pCharacteristic);
194  virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
195  virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue);
196 };
197 
198 #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
199 #endif /* CONFIG_BT_ENABLED */
200 #endif /*MAIN_NIMBLECHARACTERISTIC_H_*/
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