esp-nimble-cpp / NimBLE-Arduino  1.3.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 #include "sdkconfig.h"
18 #if defined(CONFIG_BT_ENABLED)
19 
20 #include "nimconfig.h"
21 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
22 
23 #include "NimBLECharacteristic.h"
24 #include "NimBLEUUID.h"
25 
26 #include <string>
27 
28 
29 typedef struct
30 {
31  uint16_t attr_max_len;
32  uint16_t attr_len;
33  uint8_t *attr_value;
34 } attr_value_t;
35 
36 class NimBLEService;
39 
40 
45 public:
46  NimBLEDescriptor(const char* uuid, uint16_t properties,
47  uint16_t max_len,
48  NimBLECharacteristic* pCharacteristic = nullptr);
49 
50  NimBLEDescriptor(NimBLEUUID uuid, uint16_t properties,
51  uint16_t max_len,
52  NimBLECharacteristic* pCharacteristic = nullptr);
53 
55 
56  uint16_t getHandle();
58  std::string toString();
59 
60  void setCallbacks(NimBLEDescriptorCallbacks* pCallbacks);
61 
62  size_t getLength();
63  uint8_t* getValue();
64  std::string getStringValue();
65 
66  void setValue(const uint8_t* data, size_t size);
67  void setValue(const std::string &value);
69 
74  template<typename T>
75  void setValue(const T &s) {
76  setValue((uint8_t*)&s, sizeof(T));
77  }
78 
79 private:
80  friend class NimBLECharacteristic;
81  friend class NimBLEService;
82  friend class NimBLE2904;
83 
84  static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
85  struct ble_gatt_access_ctxt *ctxt, void *arg);
86  void setHandle(uint16_t handle);
87  void setCharacteristic(NimBLECharacteristic* pChar);
88 
89  NimBLEUUID m_uuid;
90  uint16_t m_handle;
91  NimBLEDescriptorCallbacks* m_pCallbacks;
92  NimBLECharacteristic* m_pCharacteristic;
93  uint8_t m_properties;
94  attr_value_t m_value;
95  portMUX_TYPE m_valMux;
96  uint8_t m_removed;
97 }; // NimBLEDescriptor
98 
99 
108 public:
109  virtual ~NimBLEDescriptorCallbacks();
110  virtual void onRead(NimBLEDescriptor* pDescriptor);
111  virtual void onWrite(NimBLEDescriptor* pDescriptor);
112 };
113 
114 #include "NimBLE2904.h"
115 
116 #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
117 #endif /* CONFIG_BT_ENABLED */
118 #endif /* MAIN_NIMBLEDESCRIPTOR_H_ */
Descriptor for Characteristic Presentation Format.
Definition: NimBLE2904.h:43
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:60
Callbacks that can be associated with a BLE descriptors to inform of events.
Definition: NimBLEDescriptor.h:107
virtual void onRead(NimBLEDescriptor *pDescriptor)
Callback function to support a read request.
Definition: NimBLEDescriptor.cpp:279
virtual void onWrite(NimBLEDescriptor *pDescriptor)
Callback function to support a write request.
Definition: NimBLEDescriptor.cpp:288
A model of a BLE descriptor.
Definition: NimBLEDescriptor.h:44
size_t getLength()
Get the length of the value of this descriptor.
Definition: NimBLEDescriptor.cpp:106
NimBLEDescriptor(const char *uuid, uint16_t properties, uint16_t max_len, NimBLECharacteristic *pCharacteristic=nullptr)
NimBLEDescriptor constructor.
Definition: NimBLEDescriptor.cpp:35
NimBLECharacteristic * getCharacteristic()
Get the characteristic this descriptor belongs to.
Definition: NimBLEDescriptor.cpp:141
uint16_t getHandle()
Get the BLE handle for this descriptor.
Definition: NimBLEDescriptor.cpp:97
std::string getStringValue()
Get the value of this descriptor as a string.
Definition: NimBLEDescriptor.cpp:132
void setValue(const T &s)
Convenience template to set the descriptor value to <type>val.
Definition: NimBLEDescriptor.h:75
void setValue(const uint8_t *data, size_t size)
Set the value of the descriptor.
Definition: NimBLEDescriptor.cpp:232
void setCallbacks(NimBLEDescriptorCallbacks *pCallbacks)
Set the callback handlers for this descriptor.
Definition: NimBLEDescriptor.cpp:205
NimBLEUUID getUUID()
Get the UUID of the descriptor.
Definition: NimBLEDescriptor.cpp:114
std::string toString()
Return a string representation of the descriptor.
Definition: NimBLEDescriptor.cpp:265
uint8_t * getValue()
Get the value of this descriptor.
Definition: NimBLEDescriptor.cpp:123
~NimBLEDescriptor()
NimBLEDescriptor destructor.
Definition: NimBLEDescriptor.cpp:89
The model of a BLE service.
Definition: NimBLEService.h:36
A model of a BLE UUID.
Definition: NimBLEUUID.h:31