esp-nimble-cpp  1.4.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 
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 #include "NimBLELog.h"
27 
30 
31 
32 typedef std::function<void (NimBLERemoteCharacteristic* pBLERemoteCharacteristic,
33  uint8_t* pData, size_t length, bool isNotify)> notify_callback;
34 
35 typedef struct {
36  const NimBLEUUID *uuid;
37  void *task_data;
38 } desc_filter_t;
39 
40 
45 public:
47 
48  // Public member functions
49  bool canBroadcast();
50  bool canIndicate();
51  bool canNotify();
52  bool canRead();
53  bool canWrite();
54  bool canWriteNoResponse();
55  std::vector<NimBLERemoteDescriptor*>::iterator begin();
56  std::vector<NimBLERemoteDescriptor*>::iterator end();
58  std::vector<NimBLERemoteDescriptor*>* getDescriptors(bool refresh = false);
59  void deleteDescriptors();
60  size_t deleteDescriptor(const NimBLEUUID &uuid);
61  uint16_t getHandle();
62  uint16_t getDefHandle();
64  NimBLEAttValue readValue(time_t *timestamp = nullptr);
65  std::string toString();
67 
68  uint8_t readUInt8() __attribute__ ((deprecated("Use template readValue<uint8_t>()")));
69  uint16_t readUInt16() __attribute__ ((deprecated("Use template readValue<uint16_t>()")));
70  uint32_t readUInt32() __attribute__ ((deprecated("Use template readValue<uint32_t>()")));
71  float readFloat() __attribute__ ((deprecated("Use template readValue<float>()")));
72  NimBLEAttValue getValue(time_t *timestamp = nullptr);
73 
74  bool subscribe(bool notifications = true,
75  notify_callback notifyCallback = nullptr,
76  bool response = false);
77  bool unsubscribe(bool response = false);
78  bool registerForNotify(notify_callback notifyCallback,
79  bool notifications = true,
80  bool response = true)
81  __attribute__ ((deprecated("Use subscribe()/unsubscribe()")));
82  bool writeValue(const uint8_t* data,
83  size_t length,
84  bool response = false);
85  bool writeValue(const std::vector<uint8_t>& v, bool response = false);
86  bool writeValue(const char* s, bool response = false);
87 
88 
89  /*********************** Template Functions ************************/
90 
97  template<typename T>
98 #ifdef _DOXYGEN_
99  bool
100 #else
101  typename std::enable_if<!std::is_array<T>::value && !Has_c_str_len<T>::value, bool>::type
102 #endif
103  writeValue(const T& s, bool response = false) {
104  return writeValue((uint8_t*)&s, sizeof(T), response);
105  }
106 
113  template<typename T>
114 #ifdef _DOXYGEN_
115  bool
116 #else
117  typename std::enable_if<Has_c_str_len<T>::value, bool>::type
118 #endif
119  writeValue(const T& s, bool response = false) {
120  return writeValue((uint8_t*)s.c_str(), s.length(), response);
121  }
122 
132  template<typename T>
133  T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
134  if(!skipSizeCheck && m_value.size() < sizeof(T)) return T();
135  return *((T *)m_value.getValue(timestamp));
136  }
137 
147  template<typename T>
148  T readValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
149  NimBLEAttValue value = readValue();
150  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
151  return *((T *)value.getValue(timestamp));
152  }
153 
154 private:
155 
156  NimBLERemoteCharacteristic(NimBLERemoteService *pRemoteservice, const struct ble_gatt_chr *chr);
157 
158  friend class NimBLEClient;
159  friend class NimBLERemoteService;
160  friend class NimBLERemoteDescriptor;
161 
162  // Private member functions
163  bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true);
164  bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr);
165  static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
166  struct ble_gatt_attr *attr, void *arg);
167  static int onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error,
168  struct ble_gatt_attr *attr, void *arg);
169  static int descriptorDiscCB(uint16_t conn_handle, const struct ble_gatt_error *error,
170  uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc,
171  void *arg);
172  static int nextCharCB(uint16_t conn_handle, const struct ble_gatt_error *error,
173  const struct ble_gatt_chr *chr, void *arg);
174 
175  // Private properties
176  NimBLEUUID m_uuid;
177  uint8_t m_charProp;
178  uint16_t m_handle;
179  uint16_t m_defHandle;
180  uint16_t m_endHandle;
181  NimBLERemoteService* m_pRemoteService;
182  NimBLEAttValue m_value;
183  notify_callback m_notifyCallback;
184 
185  // We maintain a vector of descriptors owned by this characteristic.
186  std::vector<NimBLERemoteDescriptor*> m_descriptorVector;
187 }; // NimBLERemoteCharacteristic
188 
189 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
190 #endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */
A specialized container class to hold BLE attribute values.
Definition: NimBLEAttValue.h:61
const uint8_t * getValue(time_t *timestamp)
Get a pointer to the value buffer with timestamp.
Definition: NimBLEAttValue.h:369
uint16_t size() const
Returns the current size of the value in bytes.
Definition: NimBLEAttValue.h:151
A model of a BLE client.
Definition: NimBLEClient.h:39
A model of a remote BLE characteristic.
Definition: NimBLERemoteCharacteristic.h:44
bool canRead()
Does the characteristic support reading?
Definition: NimBLERemoteCharacteristic.cpp:115
uint16_t readUInt16() __attribute__((deprecated("Use template readValue<uint16_t>()")))
Read an unsigned 16 bit value.
Definition: NimBLERemoteCharacteristic.cpp:451
bool canWriteNoResponse()
Does the characteristic support writing with no response?
Definition: NimBLERemoteCharacteristic.cpp:133
std::vector< NimBLERemoteDescriptor * >::iterator end()
Get iterator to the end of the vector of remote descriptor pointers.
Definition: NimBLERemoteCharacteristic.cpp:392
bool writeValue(const uint8_t *data, size_t length, bool response=false)
Write a new value to the remote characteristic from a data buffer.
Definition: NimBLERemoteCharacteristic.cpp:764
NimBLEAttValue readValue(time_t *timestamp=nullptr)
Read the value of the remote characteristic.
Definition: NimBLERemoteCharacteristic.cpp:490
T readValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
Template to convert the remote characteristic data to <type>.
Definition: NimBLERemoteCharacteristic.h:148
std::string toString()
Convert a NimBLERemoteCharacteristic to a string representation;.
Definition: NimBLERemoteCharacteristic.cpp:713
bool canIndicate()
Does the characteristic support indications?
Definition: NimBLERemoteCharacteristic.cpp:97
void deleteDescriptors()
Delete the descriptors in the descriptor vector.
Definition: NimBLERemoteCharacteristic.cpp:676
NimBLERemoteDescriptor * getDescriptor(const NimBLEUUID &uuid)
Get the descriptor instance with the given UUID that belongs to this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:307
NimBLEAttValue getValue(time_t *timestamp=nullptr)
Get the value of the remote characteristic.
Definition: NimBLERemoteCharacteristic.cpp:437
bool unsubscribe(bool response=false)
Unsubscribe for notifications or indications.
Definition: NimBLERemoteCharacteristic.cpp:645
bool canBroadcast()
Does the characteristic support broadcasting?
Definition: NimBLERemoteCharacteristic.cpp:88
uint32_t readUInt32() __attribute__((deprecated("Use template readValue<uint32_t>()")))
Read an unsigned 32 bit value.
Definition: NimBLERemoteCharacteristic.cpp:461
T getValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
Template to convert the remote characteristic data to <type>.
Definition: NimBLERemoteCharacteristic.h:133
std::vector< NimBLERemoteDescriptor * >::iterator begin()
Get iterator to the beginning of the vector of remote descriptor pointers.
Definition: NimBLERemoteCharacteristic.cpp:383
float readFloat() __attribute__((deprecated("Use template readValue<float>()")))
Read a float value.
Definition: NimBLERemoteCharacteristic.cpp:480
~NimBLERemoteCharacteristic()
Destructor.
Definition: NimBLERemoteCharacteristic.cpp:69
NimBLERemoteService * getRemoteService()
Get the remote service associated with this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:418
bool canNotify()
Does the characteristic support notifications?
Definition: NimBLERemoteCharacteristic.cpp:106
bool subscribe(bool notifications=true, notify_callback notifyCallback=nullptr, bool response=false)
Subscribe for notifications or indications.
Definition: NimBLERemoteCharacteristic.cpp:631
size_t deleteDescriptor(const NimBLEUUID &uuid)
Delete descriptor by UUID.
Definition: NimBLERemoteCharacteristic.cpp:692
bool canWrite()
Does the characteristic support writing?
Definition: NimBLERemoteCharacteristic.cpp:124
uint16_t getHandle()
Get the handle for this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:401
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:659
std::vector< NimBLERemoteDescriptor * > * getDescriptors(bool refresh=false)
Get a pointer to the vector of found descriptors.
Definition: NimBLERemoteCharacteristic.cpp:364
bool writeValue(const T &s, bool response=false)
Template to set the remote characteristic value to <type>val.
Definition: NimBLERemoteCharacteristic.h:103
NimBLEUUID getUUID()
Get the UUID for this characteristic.
Definition: NimBLERemoteCharacteristic.cpp:427
uint8_t readUInt8() __attribute__((deprecated("Use template readValue<uint8_t>()")))
Read a byte value.
Definition: NimBLERemoteCharacteristic.cpp:471
uint16_t getDefHandle()
Get the handle for this characteristics definition.
Definition: NimBLERemoteCharacteristic.cpp:409
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