esp-nimble-cpp  1.4.1
NimBLERemoteDescriptor.h
1 /*
2  * NimBLERemoteDescriptor.h
3  *
4  * Created: on Jan 27 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLERemoteDescriptor.h
10  *
11  * Created on: Jul 8, 2017
12  * Author: kolban
13  */
14 
15 #ifndef COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_
16 #define COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_
17 
18 #include "nimconfig.h"
19 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
20 
21 #include "NimBLERemoteCharacteristic.h"
22 
28 public:
29  uint16_t getHandle();
33 
34  uint8_t readUInt8() __attribute__ ((deprecated("Use template readValue<uint8_t>()")));
35  uint16_t readUInt16() __attribute__ ((deprecated("Use template readValue<uint16_t>()")));
36  uint32_t readUInt32() __attribute__ ((deprecated("Use template readValue<uint32_t>()")));
37  std::string toString(void);
38  bool writeValue(const uint8_t* data, size_t length, bool response = false);
39  bool writeValue(const std::vector<uint8_t>& v, bool response = false);
40  bool writeValue(const char* s, bool response = false);
41 
42 
43  /*********************** Template Functions ************************/
44 
51  template<typename T>
52 #ifdef _DOXYGEN_
53  bool
54 #else
55  typename std::enable_if<!std::is_array<T>::value && !Has_c_str_len<T>::value, bool>::type
56 #endif
57  writeValue(const T& s, bool response = false) {
58  return writeValue((uint8_t*)&s, sizeof(T), response);
59  }
60 
67  template<typename T>
68 #ifdef _DOXYGEN_
69  bool
70 #else
71  typename std::enable_if<Has_c_str_len<T>::value, bool>::type
72 #endif
73  writeValue(const T& s, bool response = false) {
74  return writeValue((uint8_t*)s.c_str(), s.length(), response);
75  }
76 
85  template<typename T>
86  T readValue(bool skipSizeCheck = false) {
87  NimBLEAttValue value = readValue();
88  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
89  return *((T *)value.data());
90  }
91 
92 private:
93  friend class NimBLERemoteCharacteristic;
94 
95  NimBLERemoteDescriptor (NimBLERemoteCharacteristic* pRemoteCharacteristic,
96  const struct ble_gatt_dsc *dsc);
97  static int onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error,
98  struct ble_gatt_attr *attr, void *arg);
99  static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
100  struct ble_gatt_attr *attr, void *arg);
101 
102  uint16_t m_handle;
103  NimBLEUUID m_uuid;
104  NimBLERemoteCharacteristic* m_pRemoteCharacteristic;
105 };
106 
107 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
108 #endif /* COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ */
A specialized container class to hold BLE attribute values.
Definition: NimBLEAttValue.h:61
const uint8_t * data() const
Returns a pointer to the internal buffer of the value.
Definition: NimBLEAttValue.h:154
uint16_t size() const
Returns the current size of the value in bytes.
Definition: NimBLEAttValue.h:151
A model of a remote BLE characteristic.
Definition: NimBLERemoteCharacteristic.h:44
A model of remote BLE descriptor.
Definition: NimBLERemoteDescriptor.h:27
std::string toString(void)
Return a string representation of this Remote Descriptor.
Definition: NimBLERemoteDescriptor.cpp:222
bool writeValue(const uint8_t *data, size_t length, bool response=false)
Write a new value to a remote descriptor.
Definition: NimBLERemoteDescriptor.cpp:286
uint16_t readUInt16() __attribute__((deprecated("Use template readValue<uint16_t>()")))
Read an unsigned 16 bit value.
Definition: NimBLERemoteDescriptor.cpp:98
bool writeValue(const T &s, bool response=false)
Template to set the remote descriptor value to <type>val.
Definition: NimBLERemoteDescriptor.h:57
uint32_t readUInt32() __attribute__((deprecated("Use template readValue<uint32_t>()")))
Read an unsigned 32 bit value.
Definition: NimBLERemoteDescriptor.cpp:108
NimBLERemoteCharacteristic * getRemoteCharacteristic()
Get the characteristic that owns this descriptor.
Definition: NimBLERemoteDescriptor.cpp:69
NimBLEAttValue readValue()
Read the value of the remote descriptor.
Definition: NimBLERemoteDescriptor.cpp:117
uint8_t readUInt8() __attribute__((deprecated("Use template readValue<uint8_t>()")))
Read a byte value.
Definition: NimBLERemoteDescriptor.cpp:88
uint16_t getHandle()
Retrieve the handle associated with this remote descriptor.
Definition: NimBLERemoteDescriptor.cpp:60
T readValue(bool skipSizeCheck=false)
Template to convert the remote descriptor data to <type>.
Definition: NimBLERemoteDescriptor.h:86
NimBLEUUID getUUID()
Retrieve the UUID associated this remote descriptor.
Definition: NimBLERemoteDescriptor.cpp:78
A model of a BLE UUID.
Definition: NimBLEUUID.h:37