esp-nimble-cpp  1.4.1
NimBLEDescriptor.h
1 /*
2  * NimBLEDescriptor.h
3  *
4  * Created: on March 10, 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLEDescriptor.h
10  *
11  * Created on: Jun 22, 2017
12  * Author: kolban
13  */
14 
15 #ifndef MAIN_NIMBLEDESCRIPTOR_H_
16 #define MAIN_NIMBLEDESCRIPTOR_H_
17 
18 #include "nimconfig.h"
19 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
20 
21 #include "NimBLECharacteristic.h"
22 #include "NimBLEUUID.h"
23 #include "NimBLEAttValue.h"
24 
25 #include <string>
26 
27 class NimBLEService;
30 
31 
36 public:
37  NimBLEDescriptor(const char* uuid, uint16_t properties,
38  uint16_t max_len,
39  NimBLECharacteristic* pCharacteristic = nullptr);
40 
41  NimBLEDescriptor(NimBLEUUID uuid, uint16_t properties,
42  uint16_t max_len,
43  NimBLECharacteristic* pCharacteristic = nullptr);
44 
46 
47  uint16_t getHandle();
49  std::string toString();
50  void setCallbacks(NimBLEDescriptorCallbacks* pCallbacks);
52 
53  size_t getLength();
54  NimBLEAttValue getValue(time_t *timestamp = nullptr);
55  std::string getStringValue();
56 
57  void setValue(const uint8_t* data, size_t size);
58  void setValue(const std::vector<uint8_t>& vec);
59 
60  /*********************** Template Functions ************************/
61 
66  template<typename T>
67  void setValue(const T &s) { m_value.setValue<T>(s); }
68 
77  template<typename T>
78  T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
79  return m_value.getValue<T>(timestamp, skipSizeCheck);
80  }
81 
82 private:
83  friend class NimBLECharacteristic;
84  friend class NimBLEService;
85  friend class NimBLE2904;
86 
87  static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
88  struct ble_gatt_access_ctxt *ctxt, void *arg);
89  void setHandle(uint16_t handle);
90  void setCharacteristic(NimBLECharacteristic* pChar);
91 
92  NimBLEUUID m_uuid;
93  uint16_t m_handle;
94  NimBLEDescriptorCallbacks* m_pCallbacks;
95  NimBLECharacteristic* m_pCharacteristic;
96  uint8_t m_properties;
97  NimBLEAttValue m_value;
98  uint8_t m_removed;
99 }; // NimBLEDescriptor
100 
101 
110 public:
111  virtual ~NimBLEDescriptorCallbacks();
112  virtual void onRead(NimBLEDescriptor* pDescriptor);
113  virtual void onWrite(NimBLEDescriptor* pDescriptor);
114 };
115 
116 #include "NimBLE2904.h"
117 
118 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
119 #endif /* MAIN_NIMBLEDESCRIPTOR_H_ */
Descriptor for Characteristic Presentation Format.
Definition: NimBLE2904.h:40
A specialized container class to hold BLE attribute values.
Definition: NimBLEAttValue.h:61
bool setValue(const uint8_t *value, uint16_t len)
Set the value from a buffer.
Definition: NimBLEAttValue.h:380
const uint8_t * getValue(time_t *timestamp)
Get a pointer to the value buffer with timestamp.
Definition: NimBLEAttValue.h:369
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:63
Callbacks that can be associated with a BLE descriptors to inform of events.
Definition: NimBLEDescriptor.h:109
virtual void onRead(NimBLEDescriptor *pDescriptor)
Callback function to support a read request.
Definition: NimBLEDescriptor.cpp:289
virtual void onWrite(NimBLEDescriptor *pDescriptor)
Callback function to support a write request.
Definition: NimBLEDescriptor.cpp:299
A model of a BLE descriptor.
Definition: NimBLEDescriptor.h:35
size_t getLength()
Get the length of the value of this descriptor.
Definition: NimBLEDescriptor.cpp:107
NimBLEDescriptor(const char *uuid, uint16_t properties, uint16_t max_len, NimBLECharacteristic *pCharacteristic=nullptr)
Construct a descriptor.
Definition: NimBLEDescriptor.cpp:37
NimBLECharacteristic * getCharacteristic()
Get the characteristic this descriptor belongs to.
Definition: NimBLEDescriptor.cpp:146
uint16_t getHandle()
Get the BLE handle for this descriptor.
Definition: NimBLEDescriptor.cpp:98
T getValue(time_t *timestamp=nullptr, bool skipSizeCheck=false)
Template to convert the descriptor data to <type>.
Definition: NimBLEDescriptor.h:78
std::string getStringValue()
Get the value of this descriptor as a string.
Definition: NimBLEDescriptor.cpp:137
NimBLEAttValue getValue(time_t *timestamp=nullptr)
Get the value of this descriptor.
Definition: NimBLEDescriptor.cpp:124
void setValue(const T &s)
Template to set the characteristic value to <type>val.
Definition: NimBLEDescriptor.h:67
void setValue(const uint8_t *data, size_t size)
Set the value of the descriptor.
Definition: NimBLEDescriptor.cpp:248
void setCallbacks(NimBLEDescriptorCallbacks *pCallbacks)
Set the callback handlers for this descriptor.
Definition: NimBLEDescriptor.cpp:221
NimBLEUUID getUUID()
Get the UUID of the descriptor.
Definition: NimBLEDescriptor.cpp:115
std::string toString()
Return a string representation of the descriptor.
Definition: NimBLEDescriptor.cpp:275
~NimBLEDescriptor()
NimBLEDescriptor destructor.
Definition: NimBLEDescriptor.cpp:91
The model of a BLE service.
Definition: NimBLEService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37