esp-nimble-cpp / NimBLE-Arduino  1.3.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 #include "sdkconfig.h"
18 #if defined(CONFIG_BT_ENABLED)
19 
20 #include "nimconfig.h"
21 
22 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
23 #include "NimBLEScan.h"
24 #endif
25 
26 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
27 #include "NimBLEAdvertising.h"
28 #endif
29 
30 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
31 #include "NimBLEClient.h"
32 #endif
33 
34 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
35 #include "NimBLEServer.h"
36 #endif
37 
38 #include "NimBLEUtils.h"
39 #include "NimBLESecurity.h"
40 #include "NimBLEAddress.h"
41 
42 #include "esp_bt.h"
43 
44 #include <map>
45 #include <string>
46 #include <list>
47 
48 #define BLEDevice NimBLEDevice
49 #define BLEClient NimBLEClient
50 #define BLERemoteService NimBLERemoteService
51 #define BLERemoteCharacteristic NimBLERemoteCharacteristic
52 #define BLERemoteDescriptor NimBLERemoteDescriptor
53 #define BLEAdvertisedDevice NimBLEAdvertisedDevice
54 #define BLEScan NimBLEScan
55 #define BLEUUID NimBLEUUID
56 #define BLESecurity NimBLESecurity
57 #define BLESecurityCallbacks NimBLESecurityCallbacks
58 #define BLEAddress NimBLEAddress
59 #define BLEUtils NimBLEUtils
60 #define BLEClientCallbacks NimBLEClientCallbacks
61 #define BLEAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks
62 #define BLEScanResults NimBLEScanResults
63 #define BLEServer NimBLEServer
64 #define BLEService NimBLEService
65 #define BLECharacteristic NimBLECharacteristic
66 #define BLEAdvertising NimBLEAdvertising
67 #define BLEServerCallbacks NimBLEServerCallbacks
68 #define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
69 #define BLEAdvertisementData NimBLEAdvertisementData
70 #define BLEDescriptor NimBLEDescriptor
71 #define BLE2902 NimBLE2902
72 #define BLE2904 NimBLE2904
73 #define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
74 #define BLEBeacon NimBLEBeacon
75 #define BLEEddystoneTLM NimBLEEddystoneTLM
76 #define BLEEddystoneURL NimBLEEddystoneURL
77 
78 #ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
79 #define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
80 #else
81 #define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
82 #endif
83 
84 typedef int (*gap_event_handler)(ble_gap_event *event, void *arg);
85 
86 extern "C" void ble_store_config_init(void);
87 
91 class NimBLEDevice {
92 public:
93  static void init(const std::string &deviceName);
94  static void deinit(bool clearAll = false);
95  static bool getInitialized();
96  static NimBLEAddress getAddress();
97  static std::string toString();
98  static bool whiteListAdd(const NimBLEAddress & address);
99  static bool whiteListRemove(const NimBLEAddress & address);
100  static bool onWhiteList(const NimBLEAddress & address);
101  static size_t getWhiteListCount();
102  static NimBLEAddress getWhiteListAddress(size_t index);
103 
104 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
105  static NimBLEScan* getScan();
106 #endif
107 
108 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
109  static NimBLEServer* createServer();
110  static NimBLEServer* getServer();
111 #endif
112 
113  static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
114  static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
115  static void setCustomGapHandler(gap_event_handler handler);
116  static void setSecurityAuth(bool bonding, bool mitm, bool sc);
117  static void setSecurityAuth(uint8_t auth_req);
118  static void setSecurityIOCap(uint8_t iocap);
119  static void setSecurityInitKey(uint8_t init_key);
120  static void setSecurityRespKey(uint8_t init_key);
121  static void setSecurityPasskey(uint32_t pin);
122  static uint32_t getSecurityPasskey();
123  static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks);
124  static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false);
125  static int startSecurity(uint16_t conn_id);
126  static int setMTU(uint16_t mtu);
127  static uint16_t getMTU();
128  static bool isIgnored(const NimBLEAddress &address);
129  static void addIgnored(const NimBLEAddress &address);
130  static void removeIgnored(const NimBLEAddress &address);
131  static void setScanDuplicateCacheSize(uint16_t cacheSize);
132  static void setScanFilterMode(uint8_t type);
133 
134 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
136  static void startAdvertising();
137  static void stopAdvertising();
138 #endif
139 
140 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
141  static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
142  static bool deleteClient(NimBLEClient* pClient);
143  static NimBLEClient* getClientByID(uint16_t conn_id);
144  static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
146  static size_t getClientListSize();
147  static std::list<NimBLEClient*>* getClientList();
148 #endif
149 
150 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
151  static bool deleteBond(const NimBLEAddress &address);
152  static int getNumBonds();
153  static bool isBonded(const NimBLEAddress &address);
154  static void deleteAllBonds();
155  static NimBLEAddress getBondedAddress(int index);
156 #endif
157 
158 private:
159 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
160  friend class NimBLEClient;
161 #endif
162 
163 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
164  friend class NimBLEScan;
165 #endif
166 
167 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
168  friend class NimBLEServer;
169  friend class NimBLECharacteristic;
170 #endif
171 
172 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
173  friend class NimBLEAdvertising;
174 #endif
175 
176  static void onReset(int reason);
177  static void onSync(void);
178  static void host_task(void *param);
179  static bool m_synced;
180 
181 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
182  static NimBLEScan* m_pScan;
183 #endif
184 
185 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
186  static NimBLEServer* m_pServer;
187 #endif
188 
189 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
190  static NimBLEAdvertising* m_bleAdvertising;
191 #endif
192 
193 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
194  static std::list <NimBLEClient*> m_cList;
195 #endif
196  static std::list <NimBLEAddress> m_ignoreList;
197  static NimBLESecurityCallbacks* m_securityCallbacks;
198  static uint32_t m_passkey;
199  static ble_gap_event_listener m_listener;
200  static gap_event_handler m_customGapHandler;
201  static uint8_t m_own_addr_type;
202  static uint16_t m_scanDuplicateSize;
203  static uint8_t m_scanFilterMode;
204  static std::vector<NimBLEAddress> m_whiteList;
205 };
206 
207 
208 #endif // CONFIG_BT_ENABLED
209 #endif // MAIN_NIMBLEDEVICE_H_
A BLE device address.
Definition: NimBLEAddress.h:34
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:83
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:60
A model of a BLE client.
Definition: NimBLEClient.h:41
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:91
static void removeIgnored(const NimBLEAddress &address)
Remove a device from the ignore list.
Definition: NimBLEDevice.cpp:1054
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client in the list.
Definition: NimBLEDevice.cpp:261
static bool isIgnored(const NimBLEAddress &address)
Check if the device address is on our ignore list.
Definition: NimBLEDevice.cpp:1030
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition: NimBLEDevice.cpp:505
static std::list< NimBLEClient * > * getClientList()
Get the list of created client objects.
Definition: NimBLEDevice.cpp:212
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition: NimBLEDevice.cpp:485
static int startSecurity(uint16_t conn_id)
Start the connection securing and authorization for this connection.
Definition: NimBLEDevice.cpp:1011
static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT)
Get the transmission power.
Definition: NimBLEDevice.cpp:326
static NimBLEServer * getServer()
Get the instance of the server.
Definition: NimBLEDevice.cpp:91
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition: NimBLEDevice.cpp:649
static NimBLEServer * createServer()
Create a new instance of a server.
Definition: NimBLEDevice.cpp:75
static uint16_t getMTU()
Get local MTU value set.
Definition: NimBLEDevice.cpp:401
static void setSecurityRespKey(uint8_t init_key)
Set the keys we are willing to accept during pairing.
Definition: NimBLEDevice.cpp:938
static void init(const std::string &deviceName)
Initialize the BLE environment.
Definition: NimBLEDevice.cpp:738
static void addIgnored(const NimBLEAddress &address)
Add a device to the ignore list.
Definition: NimBLEDevice.cpp:1045
static NimBLEAddress getAddress()
Get our device address.
Definition: NimBLEDevice.cpp:356
static void setSecurityCallbacks(NimBLESecurityCallbacks *pCallbacks)
Set callbacks that will be used to handle encryption negotiation events and authentication events.
Definition: NimBLEDevice.cpp:966
static int setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition: NimBLEDevice.cpp:383
static int getNumBonds()
Gets the number of bonded peers stored.
Definition: NimBLEDevice.cpp:457
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition: NimBLEDevice.cpp:600
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:168
static void setSecurityPasskey(uint32_t pin)
Set the passkey the server will ask for when pairing.
Definition: NimBLEDevice.cpp:947
static void stopAdvertising()
Convenience function to stop advertising.
Definition: NimBLEDevice.cpp:121
static void setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition: NimBLEDevice.cpp:1068
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:924
static void setScanDuplicateCacheSize(uint16_t cacheSize)
Set the duplicate filter cache size for filtering scanned devices.
Definition: NimBLEDevice.cpp:413
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition: NimBLEDevice.cpp:956
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition: NimBLEDevice.cpp:877
static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false)
Set the own address type.
Definition: NimBLEDevice.cpp:981
static NimBLEAdvertising * getAdvertising()
Get the instance of the advertising object.
Definition: NimBLEDevice.cpp:102
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition: NimBLEDevice.cpp:569
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition: NimBLEDevice.cpp:639
static void deleteAllBonds()
Deletes all bonding information.
Definition: NimBLEDevice.cpp:474
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition: NimBLEDevice.cpp:910
static void setScanFilterMode(uint8_t type)
Set the duplicate filter mode for filtering scanned devices.
Definition: NimBLEDevice.cpp:440
static size_t getClientListSize()
Get the number of created client objects.
Definition: NimBLEDevice.cpp:221
static std::string toString()
Return a string representation of the address of this device.
Definition: NimBLEDevice.cpp:373
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peer_addr)
Get a reference to a client by peer address.
Definition: NimBLEDevice.cpp:247
static NimBLEClient * getClientByID(uint16_t conn_id)
Get a reference to a client by connection ID.
Definition: NimBLEDevice.cpp:231
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition: NimBLEDevice.cpp:531
static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT)
Set the transmission power.
Definition: NimBLEDevice.cpp:298
static void startAdvertising()
Convenience function to begin advertising.
Definition: NimBLEDevice.cpp:113
static bool getInitialized()
Check if the initialization is complete.
Definition: NimBLEDevice.cpp:866
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition: NimBLEDevice.cpp:553
static void deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition: NimBLEDevice.cpp:810
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:150
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition: NimBLEDevice.cpp:133
Perform and manage BLE scans.
Definition: NimBLEScan.h:61
Callbacks to handle GAP events related to authorization. Deprecated - provided for backward compatibi...
Definition: NimBLESecurity.h:88
The model of a BLE server.
Definition: NimBLEServer.h:42