My Project
 All Classes Functions Variables Pages
BLECharacteristic.h
1 /*
2  * BLECharacteristic.h
3  *
4  * Created on: Jun 22, 2017
5  * Author: kolban
6  */
7 
8 #ifndef COMPONENTS_CPP_UTILS_BLECHARACTERISTIC_H_
9 #define COMPONENTS_CPP_UTILS_BLECHARACTERISTIC_H_
10 #include "sdkconfig.h"
11 #if defined(CONFIG_BT_ENABLED)
12 #include <string>
13 #include <map>
14 #include "BLEUUID.h"
15 #include <esp_gatts_api.h>
16 #include <esp_gap_ble_api.h>
17 #include "BLEDescriptor.h"
18 #include "BLEValue.h"
19 #include "FreeRTOS.h"
20 
21 class BLEService;
22 class BLEDescriptor;
23 class BLECharacteristicCallbacks;
24 
28 class BLEDescriptorMap {
29 public:
30  void setByUUID(const char* uuid, BLEDescriptor* pDescriptor);
31  void setByUUID(BLEUUID uuid, BLEDescriptor* pDescriptor);
32  void setByHandle(uint16_t handle, BLEDescriptor* pDescriptor);
33  BLEDescriptor* getByUUID(const char* uuid);
34  BLEDescriptor* getByUUID(BLEUUID uuid);
35  BLEDescriptor* getByHandle(uint16_t handle);
36  std::string toString();
37  void handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);
38  BLEDescriptor* getFirst();
39  BLEDescriptor* getNext();
40 private:
41  std::map<BLEDescriptor*, std::string> m_uuidMap;
42  std::map<uint16_t, BLEDescriptor*> m_handleMap;
43  std::map<BLEDescriptor*, std::string>::iterator m_iterator;
44 };
45 
46 
53 class BLECharacteristic {
54 public:
55  BLECharacteristic(const char* uuid, uint32_t properties = 0);
56  BLECharacteristic(BLEUUID uuid, uint32_t properties = 0);
57  virtual ~BLECharacteristic();
58 
59  void addDescriptor(BLEDescriptor* pDescriptor);
60  BLEDescriptor* getDescriptorByUUID(const char* descriptorUUID);
61  BLEDescriptor* getDescriptorByUUID(BLEUUID descriptorUUID);
62  BLEUUID getUUID();
63  std::string getValue();
64  uint8_t* getData();
65 
66  void indicate();
67  void notify(bool is_notification = true);
68  void setBroadcastProperty(bool value);
69  void setCallbacks(BLECharacteristicCallbacks* pCallbacks);
70  void setIndicateProperty(bool value);
71  void setNotifyProperty(bool value);
72  void setReadProperty(bool value);
73  void setValue(uint8_t* data, size_t size);
74  void setValue(std::string value);
75  void setValue(uint16_t& data16);
76  void setValue(uint32_t& data32);
77  void setValue(int& data32);
78  void setValue(float& data32);
79  void setValue(double& data64);
80  void setWriteProperty(bool value);
81  void setWriteNoResponseProperty(bool value);
82  std::string toString();
83  uint16_t getHandle();
84  void setAccessPermissions(esp_gatt_perm_t perm);
85 
86  static const uint32_t PROPERTY_READ = 1<<0;
87  static const uint32_t PROPERTY_WRITE = 1<<1;
88  static const uint32_t PROPERTY_NOTIFY = 1<<2;
89  static const uint32_t PROPERTY_BROADCAST = 1<<3;
90  static const uint32_t PROPERTY_INDICATE = 1<<4;
91  static const uint32_t PROPERTY_WRITE_NR = 1<<5;
92 
93 private:
94 
95  friend class BLEServer;
96  friend class BLEService;
97  friend class BLEDescriptor;
98  friend class BLECharacteristicMap;
99 
100  BLEUUID m_bleUUID;
101  BLEDescriptorMap m_descriptorMap;
102  uint16_t m_handle;
103  esp_gatt_char_prop_t m_properties;
104  BLECharacteristicCallbacks* m_pCallbacks;
105  BLEService* m_pService;
106  BLEValue m_value;
107  esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
108 
109  void handleGATTServerEvent(
110  esp_gatts_cb_event_t event,
111  esp_gatt_if_t gatts_if,
112  esp_ble_gatts_cb_param_t* param);
113 
114  void executeCreate(BLEService* pService);
115  esp_gatt_char_prop_t getProperties();
116  BLEService* getService();
117  void setHandle(uint16_t handle);
118  FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
119  FreeRTOS::Semaphore m_semaphoreConfEvt = FreeRTOS::Semaphore("ConfEvt");
120 }; // BLECharacteristic
121 
122 
130 class BLECharacteristicCallbacks {
131 public:
132  virtual ~BLECharacteristicCallbacks();
133  virtual void onRead(BLECharacteristic* pCharacteristic);
134  virtual void onWrite(BLECharacteristic* pCharacteristic);
135 };
136 #endif /* CONFIG_BT_ENABLED */
137 #endif /* COMPONENTS_CPP_UTILS_BLECHARACTERISTIC_H_ */
Definition: FreeRTOS.h:31