esp-nimble-cpp  1.3.2
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 
24 #include <string>
25 
26 
27 typedef struct
28 {
29  uint16_t attr_max_len;
30  uint16_t attr_len;
31  uint8_t *attr_value;
32 } attr_value_t;
33 
34 class NimBLEService;
37 
38 
43 public:
44  NimBLEDescriptor(const char* uuid, uint16_t properties,
45  uint16_t max_len,
46  NimBLECharacteristic* pCharacteristic = nullptr);
47 
48  NimBLEDescriptor(NimBLEUUID uuid, uint16_t properties,
49  uint16_t max_len,
50  NimBLECharacteristic* pCharacteristic = nullptr);
51 
53 
54  uint16_t getHandle();
56  std::string toString();
57 
58  void setCallbacks(NimBLEDescriptorCallbacks* pCallbacks);
59 
60  size_t getLength();
61  uint8_t* getValue();
62  std::string getStringValue();
63 
64  void setValue(const uint8_t* data, size_t size);
65  void setValue(const std::string &value);
67 
72  template<typename T>
73  void setValue(const T &s) {
74  setValue((uint8_t*)&s, sizeof(T));
75  }
76 
77 private:
78  friend class NimBLECharacteristic;
79  friend class NimBLEService;
80  friend class NimBLE2904;
81 
82  static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
83  struct ble_gatt_access_ctxt *ctxt, void *arg);
84  void setHandle(uint16_t handle);
85  void setCharacteristic(NimBLECharacteristic* pChar);
86 
87  NimBLEUUID m_uuid;
88  uint16_t m_handle;
89  NimBLEDescriptorCallbacks* m_pCallbacks;
90  NimBLECharacteristic* m_pCharacteristic;
91  uint8_t m_properties;
92  attr_value_t m_value;
93  uint8_t m_removed;
94 }; // NimBLEDescriptor
95 
96 
105 public:
106  virtual ~NimBLEDescriptorCallbacks();
107  virtual void onRead(NimBLEDescriptor* pDescriptor);
108  virtual void onWrite(NimBLEDescriptor* pDescriptor);
109 };
110 
111 #include "NimBLE2904.h"
112 
113 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
114 #endif /* MAIN_NIMBLEDESCRIPTOR_H_ */
Descriptor for Characteristic Presentation Format.
Definition: NimBLE2904.h:40
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:62
Callbacks that can be associated with a BLE descriptors to inform of events.
Definition: NimBLEDescriptor.h:104
virtual void onRead(NimBLEDescriptor *pDescriptor)
Callback function to support a read request.
Definition: NimBLEDescriptor.cpp:282
virtual void onWrite(NimBLEDescriptor *pDescriptor)
Callback function to support a write request.
Definition: NimBLEDescriptor.cpp:292
A model of a BLE descriptor.
Definition: NimBLEDescriptor.h:42
size_t getLength()
Get the length of the value of this descriptor.
Definition: NimBLEDescriptor.cpp:103
NimBLEDescriptor(const char *uuid, uint16_t properties, uint16_t max_len, NimBLECharacteristic *pCharacteristic=nullptr)
NimBLEDescriptor constructor.
Definition: NimBLEDescriptor.cpp:33
NimBLECharacteristic * getCharacteristic()
Get the characteristic this descriptor belongs to.
Definition: NimBLEDescriptor.cpp:138
uint16_t getHandle()
Get the BLE handle for this descriptor.
Definition: NimBLEDescriptor.cpp:94
std::string getStringValue()
Get the value of this descriptor as a string.
Definition: NimBLEDescriptor.cpp:129
void setValue(const T &s)
Convenience template to set the descriptor value to <type>val.
Definition: NimBLEDescriptor.h:73
void setValue(const uint8_t *data, size_t size)
Set the value of the descriptor.
Definition: NimBLEDescriptor.cpp:233
void setCallbacks(NimBLEDescriptorCallbacks *pCallbacks)
Set the callback handlers for this descriptor.
Definition: NimBLEDescriptor.cpp:206
NimBLEUUID getUUID()
Get the UUID of the descriptor.
Definition: NimBLEDescriptor.cpp:111
std::string toString()
Return a string representation of the descriptor.
Definition: NimBLEDescriptor.cpp:268
uint8_t * getValue()
Get the value of this descriptor.
Definition: NimBLEDescriptor.cpp:120
~NimBLEDescriptor()
NimBLEDescriptor destructor.
Definition: NimBLEDescriptor.cpp:86
The model of a BLE service.
Definition: NimBLEService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37