esp-nimble-cpp / NimBLE-Arduino  1.3.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 #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 "NimBLERemoteCharacteristic.h"
24 
30 public:
31  uint16_t getHandle();
34  std::string readValue();
35 
44  template<typename T>
45  T readValue(bool skipSizeCheck = false) {
46  std::string value = readValue();
47  if(!skipSizeCheck && value.size() < sizeof(T)) return T();
48  const char *pData = value.data();
49  return *((T *)pData);
50  }
51 
52  uint8_t readUInt8() __attribute__ ((deprecated("Use template readValue<uint8_t>()")));
53  uint16_t readUInt16() __attribute__ ((deprecated("Use template readValue<uint16_t>()")));
54  uint32_t readUInt32() __attribute__ ((deprecated("Use template readValue<uint32_t>()")));
55  std::string toString(void);
56  bool writeValue(const uint8_t* data, size_t length, bool response = false);
57  bool writeValue(const std::string &newValue, bool response = false);
58 
64  template<typename T>
65  bool writeValue(const T &s, bool response = false) {
66  return writeValue((uint8_t*)&s, sizeof(T), response);
67  }
68 
69 private:
70  friend class NimBLERemoteCharacteristic;
71 
72  NimBLERemoteDescriptor (NimBLERemoteCharacteristic* pRemoteCharacteristic,
73  const struct ble_gatt_dsc *dsc);
74  static int onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error,
75  struct ble_gatt_attr *attr, void *arg);
76  static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
77  struct ble_gatt_attr *attr, void *arg);
78 
79  uint16_t m_handle;
80  NimBLEUUID m_uuid;
81  NimBLERemoteCharacteristic* m_pRemoteCharacteristic;
82 };
83 
84 #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
85 #endif /* CONFIG_BT_ENABLED */
86 #endif /* COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ */
A model of a remote BLE characteristic.
Definition: NimBLERemoteCharacteristic.h:45
A model of remote BLE descriptor.
Definition: NimBLERemoteDescriptor.h:29
std::string toString(void)
Return a string representation of this Remote Descriptor.
Definition: NimBLERemoteDescriptor.cpp:227
bool writeValue(const uint8_t *data, size_t length, bool response=false)
Write data to the BLE Remote Descriptor.
Definition: NimBLERemoteDescriptor.cpp:269
uint16_t readUInt16() __attribute__((deprecated("Use template readValue<uint16_t>()")))
Read an unsigned 16 bit value.
Definition: NimBLERemoteDescriptor.cpp:102
uint32_t readUInt32() __attribute__((deprecated("Use template readValue<uint32_t>()")))
Read an unsigned 32 bit value.
Definition: NimBLERemoteDescriptor.cpp:116
std::string readValue()
Read the value of the remote descriptor.
Definition: NimBLERemoteDescriptor.cpp:129
NimBLERemoteCharacteristic * getRemoteCharacteristic()
Get the characteristic that owns this descriptor.
Definition: NimBLERemoteDescriptor.cpp:69
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)
A template to convert the remote descriptor data to <type>.
Definition: NimBLERemoteDescriptor.h:45
NimBLEUUID getUUID()
Retrieve the UUID associated this remote descriptor.
Definition: NimBLERemoteDescriptor.cpp:78
A model of a BLE UUID.
Definition: NimBLEUUID.h:31