esp-nimble-cpp  1.3.2
NimBLEDevice.h
1 /*
2  * NimBLEDevice.h
3  *
4  * Created: on Jan 24 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLEDevice.h
10  *
11  * Created on: Mar 16, 2017
12  * Author: kolban
13  */
14 
15 #ifndef MAIN_NIMBLEDEVICE_H_
16 #define MAIN_NIMBLEDEVICE_H_
17 
18 #include "nimconfig.h"
19 #if defined(CONFIG_BT_ENABLED)
20 
21 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
22 #include "NimBLEScan.h"
23 #endif
24 
25 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
26 #include "NimBLEAdvertising.h"
27 #endif
28 
29 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
30 #include "NimBLEClient.h"
31 #endif
32 
33 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
34 #include "NimBLEServer.h"
35 #endif
36 
37 #include "NimBLEUtils.h"
38 #include "NimBLESecurity.h"
39 #include "NimBLEAddress.h"
40 
41 #ifdef ESP_PLATFORM
42 # include "esp_bt.h"
43 #endif
44 
45 #include <map>
46 #include <string>
47 #include <list>
48 
49 #define BLEDevice NimBLEDevice
50 #define BLEClient NimBLEClient
51 #define BLERemoteService NimBLERemoteService
52 #define BLERemoteCharacteristic NimBLERemoteCharacteristic
53 #define BLERemoteDescriptor NimBLERemoteDescriptor
54 #define BLEAdvertisedDevice NimBLEAdvertisedDevice
55 #define BLEScan NimBLEScan
56 #define BLEUUID NimBLEUUID
57 #define BLESecurity NimBLESecurity
58 #define BLESecurityCallbacks NimBLESecurityCallbacks
59 #define BLEAddress NimBLEAddress
60 #define BLEUtils NimBLEUtils
61 #define BLEClientCallbacks NimBLEClientCallbacks
62 #define BLEAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks
63 #define BLEScanResults NimBLEScanResults
64 #define BLEServer NimBLEServer
65 #define BLEService NimBLEService
66 #define BLECharacteristic NimBLECharacteristic
67 #define BLEAdvertising NimBLEAdvertising
68 #define BLEServerCallbacks NimBLEServerCallbacks
69 #define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
70 #define BLEAdvertisementData NimBLEAdvertisementData
71 #define BLEDescriptor NimBLEDescriptor
72 #define BLE2902 NimBLE2902
73 #define BLE2904 NimBLE2904
74 #define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
75 #define BLEBeacon NimBLEBeacon
76 #define BLEEddystoneTLM NimBLEEddystoneTLM
77 #define BLEEddystoneURL NimBLEEddystoneURL
78 
79 #ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
80 #define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
81 #else
82 #define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
83 #endif
84 
85 typedef int (*gap_event_handler)(ble_gap_event *event, void *arg);
86 
87 extern "C" void ble_store_config_init(void);
88 
92 class NimBLEDevice {
93 public:
94  static void init(const std::string &deviceName);
95  static void deinit(bool clearAll = false);
96  static bool getInitialized();
97  static NimBLEAddress getAddress();
98  static std::string toString();
99  static bool whiteListAdd(const NimBLEAddress & address);
100  static bool whiteListRemove(const NimBLEAddress & address);
101  static bool onWhiteList(const NimBLEAddress & address);
102  static size_t getWhiteListCount();
103  static NimBLEAddress getWhiteListAddress(size_t index);
104 
105 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
106  static NimBLEScan* getScan();
107 #endif
108 
109 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
110  static NimBLEServer* createServer();
111  static NimBLEServer* getServer();
112 #endif
113 
114 #ifdef ESP_PLATFORM
115  static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
116  static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
117  static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false);
118  static void setScanDuplicateCacheSize(uint16_t cacheSize);
119  static void setScanFilterMode(uint8_t type);
120 #else
121  static void setPower(int dbm);
122  static int getPower();
123 #endif
124 
125  static void setCustomGapHandler(gap_event_handler handler);
126  static void setSecurityAuth(bool bonding, bool mitm, bool sc);
127  static void setSecurityAuth(uint8_t auth_req);
128  static void setSecurityIOCap(uint8_t iocap);
129  static void setSecurityInitKey(uint8_t init_key);
130  static void setSecurityRespKey(uint8_t init_key);
131  static void setSecurityPasskey(uint32_t pin);
132  static uint32_t getSecurityPasskey();
133  static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks);
134  static int startSecurity(uint16_t conn_id);
135  static int setMTU(uint16_t mtu);
136  static uint16_t getMTU();
137  static bool isIgnored(const NimBLEAddress &address);
138  static void addIgnored(const NimBLEAddress &address);
139  static void removeIgnored(const NimBLEAddress &address);
140 
141 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
143  static void startAdvertising();
144  static void stopAdvertising();
145 #endif
146 
147 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
148  static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
149  static bool deleteClient(NimBLEClient* pClient);
150  static NimBLEClient* getClientByID(uint16_t conn_id);
151  static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
153  static size_t getClientListSize();
154  static std::list<NimBLEClient*>* getClientList();
155 #endif
156 
157 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
158  static bool deleteBond(const NimBLEAddress &address);
159  static int getNumBonds();
160  static bool isBonded(const NimBLEAddress &address);
161  static void deleteAllBonds();
162  static NimBLEAddress getBondedAddress(int index);
163 #endif
164 
165 private:
166 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
167  friend class NimBLEClient;
168 #endif
169 
170 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
171  friend class NimBLEScan;
172 #endif
173 
174 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
175  friend class NimBLEServer;
176  friend class NimBLECharacteristic;
177 #endif
178 
179 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
180  friend class NimBLEAdvertising;
181 #endif
182 
183  static void onReset(int reason);
184  static void onSync(void);
185  static void host_task(void *param);
186  static bool m_synced;
187 
188 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
189  static NimBLEScan* m_pScan;
190 #endif
191 
192 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
193  static NimBLEServer* m_pServer;
194 #endif
195 
196 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
197  static NimBLEAdvertising* m_bleAdvertising;
198 #endif
199 
200 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
201  static std::list <NimBLEClient*> m_cList;
202 #endif
203  static std::list <NimBLEAddress> m_ignoreList;
204  static NimBLESecurityCallbacks* m_securityCallbacks;
205  static uint32_t m_passkey;
206  static ble_gap_event_listener m_listener;
207  static gap_event_handler m_customGapHandler;
208  static uint8_t m_own_addr_type;
209 #ifdef ESP_PLATFORM
210  static uint16_t m_scanDuplicateSize;
211  static uint8_t m_scanFilterMode;
212 #endif
213  static std::vector<NimBLEAddress> m_whiteList;
214 };
215 
216 
217 #endif // CONFIG_BT_ENABLED
218 #endif // MAIN_NIMBLEDEVICE_H_
A BLE device address.
Definition: NimBLEAddress.h:39
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:85
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:62
A model of a BLE client.
Definition: NimBLEClient.h:38
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:92
static void removeIgnored(const NimBLEAddress &address)
Remove a device from the ignore list.
Definition: NimBLEDevice.cpp:1133
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client in the list.
Definition: NimBLEDevice.cpp:288
static bool isIgnored(const NimBLEAddress &address)
Check if the device address is on our ignore list.
Definition: NimBLEDevice.cpp:1107
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition: NimBLEDevice.cpp:551
static std::list< NimBLEClient * > * getClientList()
Get the list of created client objects.
Definition: NimBLEDevice.cpp:235
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition: NimBLEDevice.cpp:531
static int startSecurity(uint16_t conn_id)
Start the connection securing and authorization for this connection.
Definition: NimBLEDevice.cpp:1091
static NimBLEServer * getServer()
Get the instance of the server.
Definition: NimBLEDevice.cpp:110
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition: NimBLEDevice.cpp:700
static NimBLEServer * createServer()
Create a new instance of a server.
Definition: NimBLEDevice.cpp:94
static uint16_t getMTU()
Get local MTU value set.
Definition: NimBLEDevice.cpp:445
static void setSecurityRespKey(uint8_t init_key)
Set the keys we are willing to accept during pairing.
Definition: NimBLEDevice.cpp:1012
static void init(const std::string &deviceName)
Initialize the BLE environment.
Definition: NimBLEDevice.cpp:802
static void addIgnored(const NimBLEAddress &address)
Add a device to the ignore list.
Definition: NimBLEDevice.cpp:1123
static NimBLEAddress getAddress()
Get our device address.
Definition: NimBLEDevice.cpp:397
static void setSecurityCallbacks(NimBLESecurityCallbacks *pCallbacks)
Set callbacks that will be used to handle encryption negotiation events and authentication events.
Definition: NimBLEDevice.cpp:1043
static int setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition: NimBLEDevice.cpp:426
static int getNumBonds()
Gets the number of bonded peers stored.
Definition: NimBLEDevice.cpp:503
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition: NimBLEDevice.cpp:649
static bool deleteClient(NimBLEClient *pClient)
Delete the client object and remove it from the list. Checks if it is connected or trying to connect ...
Definition: NimBLEDevice.cpp:190
static void setSecurityPasskey(uint32_t pin)
Set the passkey the server will ask for when pairing.
Definition: NimBLEDevice.cpp:1022
static void stopAdvertising()
Convenience function to stop advertising.
Definition: NimBLEDevice.cpp:140
static void setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition: NimBLEDevice.cpp:1148
static void setSecurityInitKey(uint8_t init_key)
If we are the initiator of the security procedure this sets the keys we will distribute.
Definition: NimBLEDevice.cpp:997
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition: NimBLEDevice.cpp:1032
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition: NimBLEDevice.cpp:947
static NimBLEAdvertising * getAdvertising()
Get the instance of the advertising object.
Definition: NimBLEDevice.cpp:121
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition: NimBLEDevice.cpp:617
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition: NimBLEDevice.cpp:689
static void deleteAllBonds()
Deletes all bonding information.
Definition: NimBLEDevice.cpp:520
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition: NimBLEDevice.cpp:982
static size_t getClientListSize()
Get the number of created client objects.
Definition: NimBLEDevice.cpp:245
static std::string toString()
Return a string representation of the address of this device.
Definition: NimBLEDevice.cpp:415
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peer_addr)
Get a reference to a client by peer address.
Definition: NimBLEDevice.cpp:273
static NimBLEClient * getClientByID(uint16_t conn_id)
Get a reference to a client by connection ID.
Definition: NimBLEDevice.cpp:256
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition: NimBLEDevice.cpp:577
static void startAdvertising()
Convenience function to begin advertising.
Definition: NimBLEDevice.cpp:132
static bool getInitialized()
Check if the initialization is complete.
Definition: NimBLEDevice.cpp:935
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition: NimBLEDevice.cpp:600
static void deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition: NimBLEDevice.cpp:878
static NimBLEClient * createClient(NimBLEAddress peerAddress=NimBLEAddress(""))
Creates a new client object and maintains a list of all client objects each client can connect to 1 p...
Definition: NimBLEDevice.cpp:171
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition: NimBLEDevice.cpp:153
Perform and manage BLE scans.
Definition: NimBLEScan.h:63
Callbacks to handle GAP events related to authorization. Deprecated - provided for backward compatibi...
Definition: NimBLESecurity.h:94
The model of a BLE server.
Definition: NimBLEServer.h:42