esp-nimble-cpp  1.4.1
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 # if CONFIG_BT_NIMBLE_EXT_ADV
27 # include "NimBLEExtAdvertising.h"
28 # else
29 # include "NimBLEAdvertising.h"
30 # endif
31 #endif
32 
33 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
34 #include "NimBLEClient.h"
35 #endif
36 
37 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
38 #include "NimBLEServer.h"
39 #endif
40 
41 #include "NimBLEUtils.h"
42 #include "NimBLESecurity.h"
43 #include "NimBLEAddress.h"
44 
45 #ifdef ESP_PLATFORM
46 # include "esp_bt.h"
47 #endif
48 
49 #include <map>
50 #include <string>
51 #include <list>
52 
53 #define BLEDevice NimBLEDevice
54 #define BLEClient NimBLEClient
55 #define BLERemoteService NimBLERemoteService
56 #define BLERemoteCharacteristic NimBLERemoteCharacteristic
57 #define BLERemoteDescriptor NimBLERemoteDescriptor
58 #define BLEAdvertisedDevice NimBLEAdvertisedDevice
59 #define BLEScan NimBLEScan
60 #define BLEUUID NimBLEUUID
61 #define BLESecurity NimBLESecurity
62 #define BLESecurityCallbacks NimBLESecurityCallbacks
63 #define BLEAddress NimBLEAddress
64 #define BLEUtils NimBLEUtils
65 #define BLEClientCallbacks NimBLEClientCallbacks
66 #define BLEAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks
67 #define BLEScanResults NimBLEScanResults
68 #define BLEServer NimBLEServer
69 #define BLEService NimBLEService
70 #define BLECharacteristic NimBLECharacteristic
71 #define BLEAdvertising NimBLEAdvertising
72 #define BLEServerCallbacks NimBLEServerCallbacks
73 #define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
74 #define BLEAdvertisementData NimBLEAdvertisementData
75 #define BLEDescriptor NimBLEDescriptor
76 #define BLE2902 NimBLE2902
77 #define BLE2904 NimBLE2904
78 #define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
79 #define BLEBeacon NimBLEBeacon
80 #define BLEEddystoneTLM NimBLEEddystoneTLM
81 #define BLEEddystoneURL NimBLEEddystoneURL
82 
83 #ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
84 #define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
85 #else
86 #define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
87 #endif
88 
89 typedef int (*gap_event_handler)(ble_gap_event *event, void *arg);
90 
91 extern "C" void ble_store_config_init(void);
92 
96 class NimBLEDevice {
97 public:
98  static void init(const std::string &deviceName);
99  static void deinit(bool clearAll = false);
100  static void setDeviceName(const std::string &deviceName);
101  static bool getInitialized();
102  static NimBLEAddress getAddress();
103  static std::string toString();
104  static bool whiteListAdd(const NimBLEAddress & address);
105  static bool whiteListRemove(const NimBLEAddress & address);
106  static bool onWhiteList(const NimBLEAddress & address);
107  static size_t getWhiteListCount();
108  static NimBLEAddress getWhiteListAddress(size_t index);
109 
110 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
111  static NimBLEScan* getScan();
112 #endif
113 
114 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
115  static NimBLEServer* createServer();
116  static NimBLEServer* getServer();
117 #endif
118 
119 #ifdef ESP_PLATFORM
120  static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
121  static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
122  static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false);
123  static void setScanDuplicateCacheSize(uint16_t cacheSize);
124  static void setScanFilterMode(uint8_t type);
125 #else
126  static void setPower(int dbm);
127  static int getPower();
128 #endif
129 
130  static void setCustomGapHandler(gap_event_handler handler);
131  static void setSecurityAuth(bool bonding, bool mitm, bool sc);
132  static void setSecurityAuth(uint8_t auth_req);
133  static void setSecurityIOCap(uint8_t iocap);
134  static void setSecurityInitKey(uint8_t init_key);
135  static void setSecurityRespKey(uint8_t init_key);
136  static void setSecurityPasskey(uint32_t pin);
137  static uint32_t getSecurityPasskey();
138  static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks);
139  static int startSecurity(uint16_t conn_id);
140  static int setMTU(uint16_t mtu);
141  static uint16_t getMTU();
142  static bool isIgnored(const NimBLEAddress &address);
143  static void addIgnored(const NimBLEAddress &address);
144  static void removeIgnored(const NimBLEAddress &address);
145 
146 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
147 # if CONFIG_BT_NIMBLE_EXT_ADV
149  static bool startAdvertising(uint8_t inst_id,
150  int duration = 0,
151  int max_events = 0);
152  static bool stopAdvertising(uint8_t inst_id);
153  static bool stopAdvertising();
154 # endif
155 # if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
157  static bool startAdvertising();
158  static bool stopAdvertising();
159 # endif
160 #endif
161 
162 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
163  static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
164  static bool deleteClient(NimBLEClient* pClient);
165  static NimBLEClient* getClientByID(uint16_t conn_id);
166  static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
168  static size_t getClientListSize();
169  static std::list<NimBLEClient*>* getClientList();
170 #endif
171 
172 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
173  static bool deleteBond(const NimBLEAddress &address);
174  static int getNumBonds();
175  static bool isBonded(const NimBLEAddress &address);
176  static void deleteAllBonds();
177  static NimBLEAddress getBondedAddress(int index);
178 #endif
179 
180 private:
181 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
182  friend class NimBLEClient;
183 #endif
184 
185 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
186  friend class NimBLEScan;
187 #endif
188 
189 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
190  friend class NimBLEServer;
191  friend class NimBLECharacteristic;
192 #endif
193 
194 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
195  friend class NimBLEAdvertising;
196 # if CONFIG_BT_NIMBLE_EXT_ADV
197  friend class NimBLEExtAdvertising;
198  friend class NimBLEExtAdvertisement;
199 # endif
200 #endif
201 
202  static void onReset(int reason);
203  static void onSync(void);
204  static void host_task(void *param);
205  static bool m_synced;
206 
207 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
208  static NimBLEScan* m_pScan;
209 #endif
210 
211 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
212  static NimBLEServer* m_pServer;
213 #endif
214 
215 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
216 # if CONFIG_BT_NIMBLE_EXT_ADV
217  static NimBLEExtAdvertising* m_bleAdvertising;
218 # else
219  static NimBLEAdvertising* m_bleAdvertising;
220 # endif
221 #endif
222 
223 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
224  static std::list <NimBLEClient*> m_cList;
225 #endif
226  static std::list <NimBLEAddress> m_ignoreList;
227  static NimBLESecurityCallbacks* m_securityCallbacks;
228  static uint32_t m_passkey;
229  static ble_gap_event_listener m_listener;
230  static gap_event_handler m_customGapHandler;
231  static uint8_t m_own_addr_type;
232 #ifdef ESP_PLATFORM
233  static uint16_t m_scanDuplicateSize;
234  static uint8_t m_scanFilterMode;
235 #endif
236  static std::vector<NimBLEAddress> m_whiteList;
237 };
238 
239 
240 #endif // CONFIG_BT_ENABLED
241 #endif // MAIN_NIMBLEDEVICE_H_
A BLE device address.
Definition: NimBLEAddress.h:39
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:87
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:63
A model of a BLE client.
Definition: NimBLEClient.h:39
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:96
static void removeIgnored(const NimBLEAddress &address)
Remove a device from the ignore list.
Definition: NimBLEDevice.cpp:1187
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client in the list.
Definition: NimBLEDevice.cpp:333
static bool isIgnored(const NimBLEAddress &address)
Check if the device address is on our ignore list.
Definition: NimBLEDevice.cpp:1161
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition: NimBLEDevice.cpp:596
static std::list< NimBLEClient * > * getClientList()
Get the list of created client objects.
Definition: NimBLEDevice.cpp:280
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition: NimBLEDevice.cpp:576
static int startSecurity(uint16_t conn_id)
Start the connection securing and authorization for this connection.
Definition: NimBLEDevice.cpp:1145
static bool stopAdvertising()
Convenience function to stop all advertising.
Definition: NimBLEDevice.cpp:185
static NimBLEServer * getServer()
Get the instance of the server.
Definition: NimBLEDevice.cpp:114
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition: NimBLEDevice.cpp:745
static NimBLEServer * createServer()
Create a new instance of a server.
Definition: NimBLEDevice.cpp:98
static uint16_t getMTU()
Get local MTU value set.
Definition: NimBLEDevice.cpp:490
static void setSecurityRespKey(uint8_t init_key)
Set the keys we are willing to accept during pairing.
Definition: NimBLEDevice.cpp:1066
static void init(const std::string &deviceName)
Initialize the BLE environment.
Definition: NimBLEDevice.cpp:847
static void addIgnored(const NimBLEAddress &address)
Add a device to the ignore list.
Definition: NimBLEDevice.cpp:1177
static NimBLEAddress getAddress()
Get our device address.
Definition: NimBLEDevice.cpp:442
static void setSecurityCallbacks(NimBLESecurityCallbacks *pCallbacks)
Set callbacks that will be used to handle encryption negotiation events and authentication events.
Definition: NimBLEDevice.cpp:1097
static int setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition: NimBLEDevice.cpp:471
static int getNumBonds()
Gets the number of bonded peers stored.
Definition: NimBLEDevice.cpp:548
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition: NimBLEDevice.cpp:694
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:235
static void setSecurityPasskey(uint32_t pin)
Set the passkey the server will ask for when pairing.
Definition: NimBLEDevice.cpp:1076
static void setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition: NimBLEDevice.cpp:1202
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:1051
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition: NimBLEDevice.cpp:1086
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition: NimBLEDevice.cpp:1001
static NimBLEExtAdvertising * getAdvertising()
Get the instance of the advertising object.
Definition: NimBLEDevice.cpp:126
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition: NimBLEDevice.cpp:662
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition: NimBLEDevice.cpp:734
static void deleteAllBonds()
Deletes all bonding information.
Definition: NimBLEDevice.cpp:565
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition: NimBLEDevice.cpp:1036
static size_t getClientListSize()
Get the number of created client objects.
Definition: NimBLEDevice.cpp:290
static std::string toString()
Return a string representation of the address of this device.
Definition: NimBLEDevice.cpp:460
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peer_addr)
Get a reference to a client by peer address.
Definition: NimBLEDevice.cpp:318
static NimBLEClient * getClientByID(uint16_t conn_id)
Get a reference to a client by connection ID.
Definition: NimBLEDevice.cpp:301
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition: NimBLEDevice.cpp:622
static bool startAdvertising()
Convenience function to begin advertising.
Definition: NimBLEDevice.cpp:176
static bool getInitialized()
Check if the initialization is complete.
Definition: NimBLEDevice.cpp:989
static void setDeviceName(const std::string &deviceName)
Set the BLEDevice's name.
Definition: NimBLEDevice.cpp:979
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition: NimBLEDevice.cpp:645
static void deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition: NimBLEDevice.cpp:923
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:216
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition: NimBLEDevice.cpp:198
Extended advertisement data.
Definition: NimBLEExtAdvertising.h:37
Extended advertising class.
Definition: NimBLEExtAdvertising.h:92
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:46