My Project
 All Classes Functions Variables Pages
BLEDevice.h
1 /*
2  * BLEDevice.h
3  *
4  * Created on: Mar 16, 2017
5  * Author: kolban
6  */
7 
8 #ifndef MAIN_BLEDevice_H_
9 #define MAIN_BLEDevice_H_
10 #include "sdkconfig.h"
11 #if defined(CONFIG_BT_ENABLED)
12 #include <esp_gap_ble_api.h> // ESP32 BLE
13 #include <esp_gattc_api.h> // ESP32 BLE
14 #include <map> // Part of C++ STL
15 #include <string>
16 #include <esp_bt.h>
17 
18 #include "BLEServer.h"
19 #include "BLEClient.h"
20 #include "BLEUtils.h"
21 #include "BLEScan.h"
22 #include "BLEAddress.h"
23 
27 typedef void (*gap_event_handler)(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t* param);
28 typedef void (*gattc_event_handler)(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param);
29 typedef void (*gatts_event_handler)(esp_gatts_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gatts_cb_param_t* param);
30 
31 class BLEDevice {
32 public:
33 
34  static BLEClient* createClient(); // Create a new BLE client.
35  static BLEServer* createServer(); // Cretae a new BLE server.
36  static BLEAddress getAddress(); // Retrieve our own local BD address.
37  static BLEScan* getScan(); // Get the scan object
38  static std::string getValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a characteristic of a service on a server.
39  static void init(std::string deviceName); // Initialize the local BLE environment.
40  static void setPower(esp_power_level_t powerLevel); // Set our power level.
41  static void setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value); // Set the value of a characteristic on a service on a server.
42  static std::string toString(); // Return a string representation of our device.
43  static void whiteListAdd(BLEAddress address); // Add an entry to the BLE white list.
44  static void whiteListRemove(BLEAddress address); // Remove an entry from the BLE white list.
45  static void setEncryptionLevel(esp_ble_sec_act_t level);
46  static void setSecurityCallbacks(BLESecurityCallbacks* pCallbacks);
47  static esp_err_t setMTU(uint16_t mtu);
48  static uint16_t getMTU();
49  static bool getInitialized(); // Returns the state of the device, is it initialized or not?
50  /* move advertising to BLEDevice for saving ram and flash in beacons */
51  static BLEAdvertising* getAdvertising();
52  static void startAdvertising();
53  static uint16_t m_appId;
54  /* multi connect */
55  static std::map<uint16_t, conn_status_t> getPeerDevices(bool client);
56  static void addPeerDevice(void* peer, bool is_client, uint16_t conn_id);
57  static void updatePeerDevice(void* peer, bool _client, uint16_t conn_id);
58  static void removePeerDevice(uint16_t conn_id, bool client);
59  static BLEClient* getClientByGattIf(uint16_t conn_id);
60  static void setCustomGapHandler(gap_event_handler handler);
61  static void setCustomGattcHandler(gattc_event_handler handler);
62  static void setCustomGattsHandler(gatts_event_handler handler);
63  static void deinit(bool release_memory = false);
64  static uint16_t m_localMTU;
65  static esp_ble_sec_act_t m_securityLevel;
66 
67 private:
68  static BLEServer* m_pServer;
69  static BLEScan* m_pScan;
70  static BLEClient* m_pClient;
71  static BLESecurityCallbacks* m_securityCallbacks;
72  static BLEAdvertising* m_bleAdvertising;
73  static esp_gatt_if_t getGattcIF();
74  static std::map<uint16_t, conn_status_t> m_connectedClientsMap;
75 
76  static void gattClientEventHandler(
77  esp_gattc_cb_event_t event,
78  esp_gatt_if_t gattc_if,
79  esp_ble_gattc_cb_param_t* param);
80 
81  static void gattServerEventHandler(
82  esp_gatts_cb_event_t event,
83  esp_gatt_if_t gatts_if,
84  esp_ble_gatts_cb_param_t* param);
85 
86  static void gapEventHandler(
87  esp_gap_ble_cb_event_t event,
88  esp_ble_gap_cb_param_t* param);
89 
90 public:
91 /* custom gap and gatt handlers for flexibility */
92  static gap_event_handler m_customGapHandler;
93  static gattc_event_handler m_customGattcHandler;
94  static gatts_event_handler m_customGattsHandler;
95 
96 }; // class BLE
97 
98 #endif // CONFIG_BT_ENABLED
99 #endif /* MAIN_BLEDevice_H_ */