esp-nimble-cpp / NimBLE-Arduino  1.3.1
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 #include "sdkconfig.h"
18 #if defined(CONFIG_BT_ENABLED)
19 
20 #include "nimconfig.h"
21 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
22 
23 #include "NimBLERemoteService.h"
24 #include "NimBLERemoteDescriptor.h"
25 
26 #include <vector>
27 #include <functional>
28 
31 
32 
33 typedef std::function<void (NimBLERemoteCharacteristic* pBLERemoteCharacteristic,
34  uint8_t* pData, size_t length, bool isNotify)> notify_callback;
35 
36 typedef struct {
37  const NimBLEUUID *uuid;
38  void *task_data;
39 } desc_filter_t;
40 
41 
46 public:
48 
49  // Public member functions
50  bool canBroadcast();
51  bool canIndicate();
52  bool canNotify();
53  bool canRead();
54  bool canWrite();
55  bool canWriteNoResponse();
56  std::vector<NimBLERemoteDescriptor*>::iterator begin();
57  std::vector<NimBLERemoteDescriptor*>::iterator end();
59  std::vector<NimBLERemoteDescriptor*>* getDescriptors(bool refresh = false);
60  void deleteDescriptors();
61  size_t deleteDescriptor(const NimBLEUUID &uuid);
62  uint16_t getHandle();
63  uint16_t getDefHandle();
65  std::string readValue(time_t *timestamp = nullptr);
66 
76  template<typename T>
77  T readValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
78  std::string value = readValue(timestamp);
79  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
80  const char *pData = value.data();
81  return *((T *)pData);
82  }
83 
84  uint8_t readUInt8() __attribute__ ((deprecated("Use template readValue<uint8_t>()")));
85  uint16_t readUInt16() __attribute__ ((deprecated("Use template readValue<uint16_t>()")));
86  uint32_t readUInt32() __attribute__ ((deprecated("Use template readValue<uint32_t>()")));
87  float readFloat() __attribute__ ((deprecated("Use template readValue<float>()")));
88  std::string getValue(time_t *timestamp = nullptr);
89 
99  template<typename T>
100  T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
101  std::string value = getValue(timestamp);
102  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
103  const char *pData = value.data();
104  return *((T *)pData);
105  }
106 
107  bool subscribe(bool notifications = true,
108  notify_callback notifyCallback = nullptr,
109  bool response = false);
110  bool unsubscribe(bool response = false);
111  bool registerForNotify(notify_callback notifyCallback,
112  bool notifications = true,
113  bool response = true)
114  __attribute__ ((deprecated("Use subscribe()/unsubscribe()")));
115  bool writeValue(const uint8_t* data,
116  size_t length,
117  bool response = false);
118  bool writeValue(const std::string &newValue,
119  bool response = false);
125  template<typename T>
126  bool writeValue(const T &s, bool response = false) {
127  return writeValue((uint8_t*)&s, sizeof(T), response);
128  }
129 
130  std::string toString();
132 
133 private:
134 
135  NimBLERemoteCharacteristic(NimBLERemoteService *pRemoteservice, const struct ble_gatt_chr *chr);
136 
137  friend class NimBLEClient;
138  friend class NimBLERemoteService;
139  friend class NimBLERemoteDescriptor;
140 
141  // Private member functions
142  bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true);
143  bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr);
144  static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
145  struct ble_gatt_attr *attr, void *arg);
146  static int onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error,
147  struct ble_gatt_attr *attr, void *arg);
148  static int descriptorDiscCB(uint16_t conn_handle, const struct ble_gatt_error *error,
149  uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc,
150  void *arg);
151  static int nextCharCB(uint16_t conn_handle, const struct ble_gatt_error *error,
152  const struct ble_gatt_chr *chr, void *arg);
153 
154  // Private properties
155  NimBLEUUID m_uuid;
156  uint8_t m_charProp;
157  uint16_t m_handle;
158  uint16_t m_defHandle;
159  uint16_t m_endHandle;
160  NimBLERemoteService* m_pRemoteService;
161  std::string m_value;
162  notify_callback m_notifyCallback;
163  time_t m_timestamp;
164  portMUX_TYPE m_valMux;
165 
166  // We maintain a vector of descriptors owned by this characteristic.
167  std::vector<NimBLERemoteDescriptor*> m_descriptorVector;
168 }; // NimBLERemoteCharacteristic
169 
170 #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
171 #endif /* CONFIG_BT_ENABLED */
172 #endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */
A model of a BLE client.
Definition: NimBLEClient.h:41
A model of a remote BLE characteristic.
Definition: NimBLERemoteCharacteristic.h:45
bool canRead()
Does the characteristic support reading?
Definition: NimBLERemoteCharacteristic.cpp:118
uint16_t readUInt16() __attribute__((deprecated("Use template readValue<uint16_t>()")))
Read an unsigned 16 bit value.
Definition: NimBLERemoteCharacteristic.cpp:427
bool canWriteNoResponse()
Does the characteristic support writing with no response?
Definition: NimBLERemoteCharacteristic.cpp:136
std::vector< NimBLERemoteDescriptor * >::iterator end()
Get iterator to the end of the vector of remote descriptor pointers.
Definition: NimBLERemoteCharacteristic.cpp:365
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:726
T readValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
A template to convert the remote characteristic data to <type>.
Definition: NimBLERemoteCharacteristic.h:77
std::string toString()
Convert a NimBLERemoteCharacteristic to a string representation;.
Definition: NimBLERemoteCharacteristic.cpp:686
bool canIndicate()
Does the characteristic support indications?
Definition: NimBLERemoteCharacteristic.cpp:100
void deleteDescriptors()
Delete the descriptors in the descriptor vector.
Definition: NimBLERemoteCharacteristic.cpp:649
NimBLERemoteDescriptor * getDescriptor(const NimBLEUUID &uuid)
Get the descriptor instance with the given UUID that belongs to this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:297
bool unsubscribe(bool response=false)
Unsubscribe for notifications or indications.
Definition: NimBLERemoteCharacteristic.cpp:618
bool canBroadcast()
Does the characteristic support broadcasting?
Definition: NimBLERemoteCharacteristic.cpp:91
uint32_t readUInt32() __attribute__((deprecated("Use template readValue<uint32_t>()")))
Read an unsigned 32 bit value.
Definition: NimBLERemoteCharacteristic.cpp:437
std::vector< NimBLERemoteDescriptor * >::iterator begin()
Get iterator to the beginning of the vector of remote descriptor pointers.
Definition: NimBLERemoteCharacteristic.cpp:356
std::string readValue(time_t *timestamp=nullptr)
Read the value of the remote characteristic.
Definition: NimBLERemoteCharacteristic.cpp:466
float readFloat() __attribute__((deprecated("Use template readValue<float>()")))
Read a float value.
Definition: NimBLERemoteCharacteristic.cpp:456
~NimBLERemoteCharacteristic()
Destructor.
Definition: NimBLERemoteCharacteristic.cpp:72
NimBLERemoteService * getRemoteService()
Get the remote service associated with this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:391
bool canNotify()
Does the characteristic support notifications?
Definition: NimBLERemoteCharacteristic.cpp:109
bool subscribe(bool notifications=true, notify_callback notifyCallback=nullptr, bool response=false)
Subscribe for notifications or indications.
Definition: NimBLERemoteCharacteristic.cpp:604
size_t deleteDescriptor(const NimBLEUUID &uuid)
Delete descriptor by UUID.
Definition: NimBLERemoteCharacteristic.cpp:665
bool canWrite()
Does the characteristic support writing?
Definition: NimBLERemoteCharacteristic.cpp:127
uint16_t getHandle()
Get the handle for this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:374
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:632
std::vector< NimBLERemoteDescriptor * > * getDescriptors(bool refresh=false)
Get a pointer to the vector of found descriptors.
Definition: NimBLERemoteCharacteristic.cpp:337
NimBLEUUID getUUID()
Get the UUID for this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:400
std::string getValue(time_t *timestamp=nullptr)
Get the value of the remote characteristic.
Definition: NimBLERemoteCharacteristic.cpp:410
uint8_t readUInt8() __attribute__((deprecated("Use template readValue<uint8_t>()")))
Read a byte value.
Definition: NimBLERemoteCharacteristic.cpp:447
uint16_t getDefHandle()
Get the handle for this characteristics definition.
Definition: NimBLERemoteCharacteristic.cpp:382
A model of remote BLE descriptor.
Definition: NimBLERemoteDescriptor.h:29
A model of a remote BLE service.
Definition: NimBLERemoteService.h:36
A model of a BLE UUID.
Definition: NimBLEUUID.h:31