esp-nimble-cpp  1.3.2
NimBLERemoteCharacteristic.h
1 /*
2  * NimBLERemoteCharacteristic.h
3  *
4  * Created: on Jan 27 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLERemoteCharacteristic.h
10  *
11  * Created on: Jul 8, 2017
12  * Author: kolban
13  */
14 
15 #ifndef COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_
16 #define COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_
17 
18 #include "nimconfig.h"
19 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
20 
21 #include "NimBLERemoteService.h"
22 #include "NimBLERemoteDescriptor.h"
23 
24 #include <vector>
25 #include <functional>
26 
29 
30 
31 typedef std::function<void (NimBLERemoteCharacteristic* pBLERemoteCharacteristic,
32  uint8_t* pData, size_t length, bool isNotify)> notify_callback;
33 
34 typedef struct {
35  const NimBLEUUID *uuid;
36  void *task_data;
37 } desc_filter_t;
38 
39 
44 public:
46 
47  // Public member functions
48  bool canBroadcast();
49  bool canIndicate();
50  bool canNotify();
51  bool canRead();
52  bool canWrite();
53  bool canWriteNoResponse();
54  std::vector<NimBLERemoteDescriptor*>::iterator begin();
55  std::vector<NimBLERemoteDescriptor*>::iterator end();
57  std::vector<NimBLERemoteDescriptor*>* getDescriptors(bool refresh = false);
58  void deleteDescriptors();
59  size_t deleteDescriptor(const NimBLEUUID &uuid);
60  uint16_t getHandle();
61  uint16_t getDefHandle();
63  std::string readValue(time_t *timestamp = nullptr);
64 
74  template<typename T>
75  T readValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
76  std::string value = readValue(timestamp);
77  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
78  const char *pData = value.data();
79  return *((T *)pData);
80  }
81 
82  uint8_t readUInt8() __attribute__ ((deprecated("Use template readValue<uint8_t>()")));
83  uint16_t readUInt16() __attribute__ ((deprecated("Use template readValue<uint16_t>()")));
84  uint32_t readUInt32() __attribute__ ((deprecated("Use template readValue<uint32_t>()")));
85  float readFloat() __attribute__ ((deprecated("Use template readValue<float>()")));
86  std::string getValue(time_t *timestamp = nullptr);
87 
97  template<typename T>
98  T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
99  std::string value = getValue(timestamp);
100  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
101  const char *pData = value.data();
102  return *((T *)pData);
103  }
104 
105  bool subscribe(bool notifications = true,
106  notify_callback notifyCallback = nullptr,
107  bool response = false);
108  bool unsubscribe(bool response = false);
109  bool registerForNotify(notify_callback notifyCallback,
110  bool notifications = true,
111  bool response = true)
112  __attribute__ ((deprecated("Use subscribe()/unsubscribe()")));
113  bool writeValue(const uint8_t* data,
114  size_t length,
115  bool response = false);
116  bool writeValue(const std::string &newValue,
117  bool response = false);
123  template<typename T>
124  bool writeValue(const T &s, bool response = false) {
125  return writeValue((uint8_t*)&s, sizeof(T), response);
126  }
127 
128  std::string toString();
130 
131 private:
132 
133  NimBLERemoteCharacteristic(NimBLERemoteService *pRemoteservice, const struct ble_gatt_chr *chr);
134 
135  friend class NimBLEClient;
136  friend class NimBLERemoteService;
137  friend class NimBLERemoteDescriptor;
138 
139  // Private member functions
140  bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true);
141  bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr);
142  static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
143  struct ble_gatt_attr *attr, void *arg);
144  static int onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error,
145  struct ble_gatt_attr *attr, void *arg);
146  static int descriptorDiscCB(uint16_t conn_handle, const struct ble_gatt_error *error,
147  uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc,
148  void *arg);
149  static int nextCharCB(uint16_t conn_handle, const struct ble_gatt_error *error,
150  const struct ble_gatt_chr *chr, void *arg);
151 
152  // Private properties
153  NimBLEUUID m_uuid;
154  uint8_t m_charProp;
155  uint16_t m_handle;
156  uint16_t m_defHandle;
157  uint16_t m_endHandle;
158  NimBLERemoteService* m_pRemoteService;
159  std::string m_value;
160  notify_callback m_notifyCallback;
161  time_t m_timestamp;
162 
163  // We maintain a vector of descriptors owned by this characteristic.
164  std::vector<NimBLERemoteDescriptor*> m_descriptorVector;
165 }; // NimBLERemoteCharacteristic
166 
167 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
168 #endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */
A model of a BLE client.
Definition: NimBLEClient.h:38
A model of a remote BLE characteristic.
Definition: NimBLERemoteCharacteristic.h:43
bool canRead()
Does the characteristic support reading?
Definition: NimBLERemoteCharacteristic.cpp:116
uint16_t readUInt16() __attribute__((deprecated("Use template readValue<uint16_t>()")))
Read an unsigned 16 bit value.
Definition: NimBLERemoteCharacteristic.cpp:434
bool canWriteNoResponse()
Does the characteristic support writing with no response?
Definition: NimBLERemoteCharacteristic.cpp:134
std::vector< NimBLERemoteDescriptor * >::iterator end()
Get iterator to the end of the vector of remote descriptor pointers.
Definition: NimBLERemoteCharacteristic.cpp:372
bool writeValue(const uint8_t *data, size_t length, bool response=false)
Write the new value for the characteristic from a data buffer.
Definition: NimBLERemoteCharacteristic.cpp:739
T readValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
A template to convert the remote characteristic data to <type>.
Definition: NimBLERemoteCharacteristic.h:75
std::string toString()
Convert a NimBLERemoteCharacteristic to a string representation;.
Definition: NimBLERemoteCharacteristic.cpp:699
bool canIndicate()
Does the characteristic support indications?
Definition: NimBLERemoteCharacteristic.cpp:98
void deleteDescriptors()
Delete the descriptors in the descriptor vector.
Definition: NimBLERemoteCharacteristic.cpp:662
NimBLERemoteDescriptor * getDescriptor(const NimBLEUUID &uuid)
Get the descriptor instance with the given UUID that belongs to this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:304
bool unsubscribe(bool response=false)
Unsubscribe for notifications or indications.
Definition: NimBLERemoteCharacteristic.cpp:631
bool canBroadcast()
Does the characteristic support broadcasting?
Definition: NimBLERemoteCharacteristic.cpp:89
uint32_t readUInt32() __attribute__((deprecated("Use template readValue<uint32_t>()")))
Read an unsigned 32 bit value.
Definition: NimBLERemoteCharacteristic.cpp:444
std::vector< NimBLERemoteDescriptor * >::iterator begin()
Get iterator to the beginning of the vector of remote descriptor pointers.
Definition: NimBLERemoteCharacteristic.cpp:363
std::string readValue(time_t *timestamp=nullptr)
Read the value of the remote characteristic.
Definition: NimBLERemoteCharacteristic.cpp:473
float readFloat() __attribute__((deprecated("Use template readValue<float>()")))
Read a float value.
Definition: NimBLERemoteCharacteristic.cpp:463
~NimBLERemoteCharacteristic()
Destructor.
Definition: NimBLERemoteCharacteristic.cpp:70
NimBLERemoteService * getRemoteService()
Get the remote service associated with this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:398
bool canNotify()
Does the characteristic support notifications?
Definition: NimBLERemoteCharacteristic.cpp:107
bool subscribe(bool notifications=true, notify_callback notifyCallback=nullptr, bool response=false)
Subscribe for notifications or indications.
Definition: NimBLERemoteCharacteristic.cpp:617
size_t deleteDescriptor(const NimBLEUUID &uuid)
Delete descriptor by UUID.
Definition: NimBLERemoteCharacteristic.cpp:678
bool canWrite()
Does the characteristic support writing?
Definition: NimBLERemoteCharacteristic.cpp:125
uint16_t getHandle()
Get the handle for this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:381
bool registerForNotify(notify_callback notifyCallback, bool notifications=true, bool response=true) __attribute__((deprecated("Use subscribe()/unsubscribe()")))
backward-compatibility method for subscribe/unsubscribe notifications/indications
Definition: NimBLERemoteCharacteristic.cpp:645
std::vector< NimBLERemoteDescriptor * > * getDescriptors(bool refresh=false)
Get a pointer to the vector of found descriptors.
Definition: NimBLERemoteCharacteristic.cpp:344
NimBLEUUID getUUID()
Get the UUID for this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:407
std::string getValue(time_t *timestamp=nullptr)
Get the value of the remote characteristic.
Definition: NimBLERemoteCharacteristic.cpp:417
uint8_t readUInt8() __attribute__((deprecated("Use template readValue<uint8_t>()")))
Read a byte value.
Definition: NimBLERemoteCharacteristic.cpp:454
uint16_t getDefHandle()
Get the handle for this characteristics definition.
Definition: NimBLERemoteCharacteristic.cpp:389
A model of remote BLE descriptor.
Definition: NimBLERemoteDescriptor.h:27
A model of a remote BLE service.
Definition: NimBLERemoteService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37