My Project
 All Classes Functions Variables Pages
BLEServer.h
1 /*
2  * BLEServer.h
3  *
4  * Created on: Apr 16, 2017
5  * Author: kolban
6  */
7 
8 #ifndef COMPONENTS_CPP_UTILS_BLESERVER_H_
9 #define COMPONENTS_CPP_UTILS_BLESERVER_H_
10 #include "sdkconfig.h"
11 #if defined(CONFIG_BT_ENABLED)
12 #include <esp_gatts_api.h>
13 
14 #include <string>
15 #include <string.h>
16 // #include "BLEDevice.h"
17 
18 #include "BLEUUID.h"
19 #include "BLEAdvertising.h"
20 #include "BLECharacteristic.h"
21 #include "BLEService.h"
22 #include "BLESecurity.h"
23 #include "FreeRTOS.h"
24 #include "BLEAddress.h"
25 
26 class BLEServerCallbacks;
27 /* TODO possibly refactor this struct */
28 typedef struct {
29  void *peer_device; // peer device BLEClient or BLEServer - maybe its better to have 2 structures or union here
30  bool connected; // do we need it?
31  uint16_t mtu; // every peer device negotiate own mtu
32 } conn_status_t;
33 
34 
38 class BLEServiceMap {
39 public:
40  BLEService* getByHandle(uint16_t handle);
41  BLEService* getByUUID(const char* uuid);
42  BLEService* getByUUID(BLEUUID uuid, uint8_t inst_id = 0);
43  void handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);
44  void setByHandle(uint16_t handle, BLEService* service);
45  void setByUUID(const char* uuid, BLEService* service);
46  void setByUUID(BLEUUID uuid, BLEService* service);
47  std::string toString();
48  BLEService* getFirst();
49  BLEService* getNext();
50  void removeService(BLEService *service);
51  int getRegisteredServiceCount();
52 
53 private:
54  std::map<uint16_t, BLEService*> m_handleMap;
55  std::map<BLEService*, std::string> m_uuidMap;
56  std::map<BLEService*, std::string>::iterator m_iterator;
57 };
58 
59 
63 class BLEServer {
64 public:
65  uint32_t getConnectedCount();
66  BLEService* createService(const char* uuid);
67  BLEService* createService(BLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0);
68  BLEAdvertising* getAdvertising();
69  void setCallbacks(BLEServerCallbacks* pCallbacks);
70  void startAdvertising();
71  void removeService(BLEService* service);
72  BLEService* getServiceByUUID(const char* uuid);
73  BLEService* getServiceByUUID(BLEUUID uuid);
74  bool connect(BLEAddress address);
75  uint16_t m_appId;
76  void updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
77 
78  /* multi connection support */
79  std::map<uint16_t, conn_status_t> getPeerDevices(bool client);
80  void addPeerDevice(void* peer, bool is_client, uint16_t conn_id);
81  void removePeerDevice(uint16_t conn_id, bool client);
82  BLEServer* getServerByConnId(uint16_t conn_id);
83  void updatePeerMTU(uint16_t connId, uint16_t mtu);
84  uint16_t getPeerMTU(uint16_t conn_id);
85  uint16_t getConnId();
86 
87 
88 private:
89  BLEServer();
90  friend class BLEService;
91  friend class BLECharacteristic;
92  friend class BLEDevice;
93  esp_ble_adv_data_t m_adv_data;
94  // BLEAdvertising m_bleAdvertising;
95  uint16_t m_connId;
96  uint32_t m_connectedCount;
97  uint16_t m_gatts_if;
98  std::map<uint16_t, conn_status_t> m_connectedServersMap;
99 
100  FreeRTOS::Semaphore m_semaphoreRegisterAppEvt = FreeRTOS::Semaphore("RegisterAppEvt");
101  FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
102  FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
103  BLEServiceMap m_serviceMap;
104  BLEServerCallbacks* m_pServerCallbacks = nullptr;
105 
106  void createApp(uint16_t appId);
107  uint16_t getGattsIf();
108  void handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
109  void registerApp(uint16_t);
110 }; // BLEServer
111 
112 
116 class BLEServerCallbacks {
117 public:
118  virtual ~BLEServerCallbacks() {};
126  virtual void onConnect(BLEServer* pServer);
127  virtual void onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param);
135  virtual void onDisconnect(BLEServer* pServer);
136 }; // BLEServerCallbacks
137 
138 
139 #endif /* CONFIG_BT_ENABLED */
140 #endif /* COMPONENTS_CPP_UTILS_BLESERVER_H_ */
Definition: FreeRTOS.h:31