My Project
 All Classes Functions Variables Pages
BLEClient.h
1 /*
2  * BLEDevice.h
3  *
4  * Created on: Mar 22, 2017
5  * Author: kolban
6  */
7 
8 #ifndef MAIN_BLEDEVICE_H_
9 #define MAIN_BLEDEVICE_H_
10 
11 #include "sdkconfig.h"
12 #if defined(CONFIG_BT_ENABLED)
13 
14 #include <esp_gattc_api.h>
15 #include <string.h>
16 #include <map>
17 #include <string>
18 #include "BLEExceptions.h"
19 #include "BLERemoteService.h"
20 #include "BLEService.h"
21 #include "BLEAddress.h"
22 #include "BLEAdvertisedDevice.h"
23 
24 class BLERemoteService;
25 class BLEClientCallbacks;
26 class BLEAdvertisedDevice;
27 
31 class BLEClient {
32 public:
33  BLEClient();
34  ~BLEClient();
35 
36  bool connect(BLEAdvertisedDevice* device);
37  bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC); // Connect to the remote BLE Server
38  void disconnect(); // Disconnect from the remote BLE Server
39  BLEAddress getPeerAddress(); // Get the address of the remote BLE Server
40  int getRssi(); // Get the RSSI of the remote BLE Server
41  std::map<std::string, BLERemoteService*>* getServices(); // Get a map of the services offered by the remote BLE Server
42  BLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server.
43  BLERemoteService* getService(BLEUUID uuid); // Get a reference to a specified service offered by the remote BLE server.
44  std::string getValue(BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a given characteristic at a given service.
45 
46 
47  void handleGAPEvent(
48  esp_gap_ble_cb_event_t event,
49  esp_ble_gap_cb_param_t* param);
50 
51  bool isConnected(); // Return true if we are connected.
52 
53  void setClientCallbacks(BLEClientCallbacks *pClientCallbacks);
54  void setValue(BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value); // Set the value of a given characteristic at a given service.
55 
56  std::string toString(); // Return a string representation of this client.
57  uint16_t getConnId();
58  esp_gatt_if_t getGattcIf();
59  uint16_t getMTU();
60 
61 uint16_t m_appId;
62 private:
63  friend class BLEDevice;
64  friend class BLERemoteService;
65  friend class BLERemoteCharacteristic;
66  friend class BLERemoteDescriptor;
67 
68  void gattClientEventHandler(
69  esp_gattc_cb_event_t event,
70  esp_gatt_if_t gattc_if,
71  esp_ble_gattc_cb_param_t* param);
72 
73  BLEAddress m_peerAddress = BLEAddress((uint8_t*)"\0\0\0\0\0\0"); // The BD address of the remote server.
74  uint16_t m_conn_id;
75 // int m_deviceType;
76  esp_gatt_if_t m_gattc_if;
77  bool m_haveServices = false; // Have we previously obtain the set of services from the remote server.
78  bool m_isConnected = false; // Are we currently connected.
79 
80  BLEClientCallbacks* m_pClientCallbacks;
81  FreeRTOS::Semaphore m_semaphoreRegEvt = FreeRTOS::Semaphore("RegEvt");
82  FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
83  FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");
84  FreeRTOS::Semaphore m_semaphoreRssiCmplEvt = FreeRTOS::Semaphore("RssiCmplEvt");
85  std::map<std::string, BLERemoteService*> m_servicesMap;
86  std::map<BLERemoteService*, uint16_t> m_servicesMapByInstID;
87  void clearServices(); // Clear any existing services.
88  uint16_t m_mtu = 23;
89 }; // class BLEDevice
90 
91 
95 class BLEClientCallbacks {
96 public:
97  virtual ~BLEClientCallbacks() {};
98  virtual void onConnect(BLEClient *pClient) = 0;
99  virtual void onDisconnect(BLEClient *pClient) = 0;
100 };
101 
102 #endif // CONFIG_BT_ENABLED
103 #endif /* MAIN_BLEDEVICE_H_ */
Definition: FreeRTOS.h:31