esp-nimble-cpp  1.4.0
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 bool getInitialized();
101  static NimBLEAddress getAddress();
102  static std::string toString();
103  static bool whiteListAdd(const NimBLEAddress & address);
104  static bool whiteListRemove(const NimBLEAddress & address);
105  static bool onWhiteList(const NimBLEAddress & address);
106  static size_t getWhiteListCount();
107  static NimBLEAddress getWhiteListAddress(size_t index);
108 
109 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
110  static NimBLEScan* getScan();
111 #endif
112 
113 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
114  static NimBLEServer* createServer();
115  static NimBLEServer* getServer();
116 #endif
117 
118 #ifdef ESP_PLATFORM
119  static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
120  static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
121  static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false);
122  static void setScanDuplicateCacheSize(uint16_t cacheSize);
123  static void setScanFilterMode(uint8_t type);
124 #else
125  static void setPower(int dbm);
126  static int getPower();
127 #endif
128 
129  static void setCustomGapHandler(gap_event_handler handler);
130  static void setSecurityAuth(bool bonding, bool mitm, bool sc);
131  static void setSecurityAuth(uint8_t auth_req);
132  static void setSecurityIOCap(uint8_t iocap);
133  static void setSecurityInitKey(uint8_t init_key);
134  static void setSecurityRespKey(uint8_t init_key);
135  static void setSecurityPasskey(uint32_t pin);
136  static uint32_t getSecurityPasskey();
137  static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks);
138  static int startSecurity(uint16_t conn_id);
139  static int setMTU(uint16_t mtu);
140  static uint16_t getMTU();
141  static bool isIgnored(const NimBLEAddress &address);
142  static void addIgnored(const NimBLEAddress &address);
143  static void removeIgnored(const NimBLEAddress &address);
144 
145 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
146 # if CONFIG_BT_NIMBLE_EXT_ADV
148  static bool startAdvertising(uint8_t inst_id,
149  int duration = 0,
150  int max_events = 0);
151  static bool stopAdvertising(uint8_t inst_id);
152  static bool stopAdvertising();
153 # else
155  static bool startAdvertising();
156  static bool stopAdvertising();
157 # endif
158 #endif
159 
160 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
161  static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
162  static bool deleteClient(NimBLEClient* pClient);
163  static NimBLEClient* getClientByID(uint16_t conn_id);
164  static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
166  static size_t getClientListSize();
167  static std::list<NimBLEClient*>* getClientList();
168 #endif
169 
170 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
171  static bool deleteBond(const NimBLEAddress &address);
172  static int getNumBonds();
173  static bool isBonded(const NimBLEAddress &address);
174  static void deleteAllBonds();
175  static NimBLEAddress getBondedAddress(int index);
176 #endif
177 
178 private:
179 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
180  friend class NimBLEClient;
181 #endif
182 
183 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
184  friend class NimBLEScan;
185 #endif
186 
187 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
188  friend class NimBLEServer;
189  friend class NimBLECharacteristic;
190 #endif
191 
192 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
193  friend class NimBLEAdvertising;
194 # if CONFIG_BT_NIMBLE_EXT_ADV
195  friend class NimBLEExtAdvertising;
196  friend class NimBLEExtAdvertisement;
197 # endif
198 #endif
199 
200  static void onReset(int reason);
201  static void onSync(void);
202  static void host_task(void *param);
203  static bool m_synced;
204 
205 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
206  static NimBLEScan* m_pScan;
207 #endif
208 
209 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
210  static NimBLEServer* m_pServer;
211 #endif
212 
213 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
214 # if CONFIG_BT_NIMBLE_EXT_ADV
215  static NimBLEExtAdvertising* m_bleAdvertising;
216 # else
217  static NimBLEAdvertising* m_bleAdvertising;
218 # endif
219 #endif
220 
221 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
222  static std::list <NimBLEClient*> m_cList;
223 #endif
224  static std::list <NimBLEAddress> m_ignoreList;
225  static NimBLESecurityCallbacks* m_securityCallbacks;
226  static uint32_t m_passkey;
227  static ble_gap_event_listener m_listener;
228  static gap_event_handler m_customGapHandler;
229  static uint8_t m_own_addr_type;
230 #ifdef ESP_PLATFORM
231  static uint16_t m_scanDuplicateSize;
232  static uint8_t m_scanFilterMode;
233 #endif
234  static std::vector<NimBLEAddress> m_whiteList;
235 };
236 
237 
238 #endif // CONFIG_BT_ENABLED
239 #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:1178
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:1152
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:1136
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:1057
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:1168
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:1088
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:1067
static void setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition: NimBLEDevice.cpp:1193
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:1042
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition: NimBLEDevice.cpp:1077
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition: NimBLEDevice.cpp:992
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:1027
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 getInitialized()
Check if the initialization is complete.
Definition: NimBLEDevice.cpp:980
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 bool startAdvertising(uint8_t inst_id, int duration=0, int max_events=0)
Convenience function to begin advertising.
Definition: NimBLEDevice.cpp:141
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