NimBLE-Arduino 2.2.0
Loading...
Searching...
No Matches
NimBLEDevice.h
1/*
2 * Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_DEVICE_H_
19#define NIMBLE_CPP_DEVICE_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED)
23# ifdef ESP_PLATFORM
24# ifndef CONFIG_IDF_TARGET_ESP32P4
25# include <esp_bt.h>
26# endif
27# endif
28
29# if defined(CONFIG_NIMBLE_CPP_IDF)
30# include <host/ble_gap.h>
31# else
32# include <nimble/nimble/host/include/host/ble_gap.h>
33# endif
34
35/**** FIX COMPILATION ****/
36# undef min
37# undef max
38/**************************/
39
40# include <string>
41# include <vector>
42
43# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
44# include <array>
45class NimBLEClient;
46# endif
47
48# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
49class NimBLEScan;
50# endif
51
52# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
53# if CONFIG_BT_NIMBLE_EXT_ADV
55# else
57# endif
58# endif
59
60# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
61class NimBLEServer;
62# endif
63
64# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
65class NimBLEConnInfo;
66# endif
67
68class NimBLEAddress;
69
70# define BLEDevice NimBLEDevice
71# define BLEClient NimBLEClient
72# define BLERemoteService NimBLERemoteService
73# define BLERemoteCharacteristic NimBLERemoteCharacteristic
74# define BLERemoteDescriptor NimBLERemoteDescriptor
75# define BLEAdvertisedDevice NimBLEAdvertisedDevice
76# define BLEScan NimBLEScan
77# define BLEUUID NimBLEUUID
78# define BLEAddress NimBLEAddress
79# define BLEUtils NimBLEUtils
80# define BLEClientCallbacks NimBLEClientCallbacks
81# define BLEAdvertisedDeviceCallbacks NimBLEScanCallbacks
82# define BLEScanResults NimBLEScanResults
83# define BLEServer NimBLEServer
84# define BLEService NimBLEService
85# define BLECharacteristic NimBLECharacteristic
86# define BLEAdvertising NimBLEAdvertising
87# define BLEServerCallbacks NimBLEServerCallbacks
88# define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
89# define BLEAdvertisementData NimBLEAdvertisementData
90# define BLEDescriptor NimBLEDescriptor
91# define BLE2904 NimBLE2904
92# define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
93# define BLEBeacon NimBLEBeacon
94# define BLEEddystoneTLM NimBLEEddystoneTLM
95# define BLEEddystoneURL NimBLEEddystoneURL
96# define BLEConnInfo NimBLEConnInfo
97
98# ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
99# define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
100# else
101# define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
102# endif
103
104enum class NimBLETxPowerType {
105 All = 0,
106 Advertise = 1,
107 Scan = 2,
108 Connection = 3
109};
110
111typedef int (*gap_event_handler)(ble_gap_event* event, void* arg);
112
117 public:
118 static bool init(const std::string& deviceName);
119 static bool deinit(bool clearAll = false);
120 static bool setDeviceName(const std::string& deviceName);
121 static bool isInitialized();
122 static NimBLEAddress getAddress();
123 static std::string toString();
124 static bool whiteListAdd(const NimBLEAddress& address);
125 static bool whiteListRemove(const NimBLEAddress& address);
126 static bool onWhiteList(const NimBLEAddress& address);
127 static size_t getWhiteListCount();
128 static NimBLEAddress getWhiteListAddress(size_t index);
129 static bool setOwnAddrType(uint8_t type);
130 static bool setOwnAddr(const NimBLEAddress& addr);
131 static bool setOwnAddr(const uint8_t* addr);
132 static void setScanDuplicateCacheSize(uint16_t cacheSize);
133 static void setScanFilterMode(uint8_t type);
134 static bool setCustomGapHandler(gap_event_handler handler);
135 static void setSecurityAuth(bool bonding, bool mitm, bool sc);
136 static void setSecurityAuth(uint8_t auth);
137 static void setSecurityIOCap(uint8_t iocap);
138 static void setSecurityInitKey(uint8_t initKey);
139 static void setSecurityRespKey(uint8_t respKey);
140 static void setSecurityPasskey(uint32_t passKey);
141 static uint32_t getSecurityPasskey();
142 static bool startSecurity(uint16_t connHandle, int* rcPtr = nullptr);
143 static bool setMTU(uint16_t mtu);
144 static uint16_t getMTU();
145 static void onReset(int reason);
146 static void onSync(void);
147 static void host_task(void* param);
148 static int getPower(NimBLETxPowerType type = NimBLETxPowerType::All);
149 static bool setPower(int8_t dbm, NimBLETxPowerType type = NimBLETxPowerType::All);
150
151# ifdef ESP_PLATFORM
152# ifndef CONFIG_IDF_TARGET_ESP32P4
153 static esp_power_level_t getPowerLevel(esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
154 static bool setPowerLevel(esp_power_level_t powerLevel, esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
155# endif
156# endif
157
158# if CONFIG_BT_NIMBLE_EXT_ADV
159 static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask);
160# endif
161
162# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
163 static NimBLEScan* getScan();
164# endif
165
166# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
167 static NimBLEServer* createServer();
168 static NimBLEServer* getServer();
169# endif
170
171# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
172 static bool injectConfirmPasskey(const NimBLEConnInfo& peerInfo, bool accept);
173 static bool injectPassKey(const NimBLEConnInfo& peerInfo, uint32_t pin);
174# endif
175
176# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
177# if CONFIG_BT_NIMBLE_EXT_ADV
179 static bool startAdvertising(uint8_t instId, int duration = 0, int maxEvents = 0);
180 static bool stopAdvertising(uint8_t instId);
181 static bool stopAdvertising();
182# endif
183# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
185 static bool startAdvertising(uint32_t duration = 0);
186 static bool stopAdvertising();
187# endif
188# endif
189
190# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
191 static NimBLEClient* createClient();
192 static NimBLEClient* createClient(const NimBLEAddress& peerAddress);
193 static bool deleteClient(NimBLEClient* pClient);
194 static NimBLEClient* getClientByHandle(uint16_t connHandle);
195 static NimBLEClient* getClientByPeerAddress(const NimBLEAddress& peerAddress);
197 static size_t getCreatedClientCount();
198 static std::vector<NimBLEClient*> getConnectedClients();
199# endif
200
201# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
202 static bool deleteBond(const NimBLEAddress& address);
203 static int getNumBonds();
204 static bool isBonded(const NimBLEAddress& address);
205 static bool deleteAllBonds();
206 static NimBLEAddress getBondedAddress(int index);
207# endif
208
209 private:
210 static bool m_synced;
211 static bool m_initialized;
212 static uint32_t m_passkey;
213 static ble_gap_event_listener m_listener;
214 static uint8_t m_ownAddrType;
215 static std::vector<NimBLEAddress> m_whiteList;
216
217# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
218 static NimBLEScan* m_pScan;
219# endif
220
221# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
222 static NimBLEServer* m_pServer;
223# endif
224
225# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
226# if CONFIG_BT_NIMBLE_EXT_ADV
227 static NimBLEExtAdvertising* m_bleAdvertising;
228# else
229 static NimBLEAdvertising* m_bleAdvertising;
230# endif
231# endif
232
233# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
234 static std::array<NimBLEClient*, NIMBLE_MAX_CONNECTIONS> m_pClients;
235# endif
236
237# ifdef ESP_PLATFORM
238# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
239 static uint16_t m_scanDuplicateSize;
240 static uint8_t m_scanFilterMode;
241# endif
242# endif
243
244# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
245 friend class NimBLEClient;
246# endif
247
248# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
249 friend class NimBLEScan;
250# endif
251
252# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
253 friend class NimBLEServer;
254 friend class NimBLECharacteristic;
255# endif
256
257# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
258 friend class NimBLEAdvertising;
259# if CONFIG_BT_NIMBLE_EXT_ADV
260 friend class NimBLEExtAdvertising;
261 friend class NimBLEExtAdvertisement;
262# endif
263# endif
264};
265
266# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
267# include "NimBLEClient.h"
268# include "NimBLERemoteService.h"
269# include "NimBLERemoteCharacteristic.h"
270# include "NimBLERemoteDescriptor.h"
271# endif
272
273# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
274# include "NimBLEScan.h"
275# endif
276
277# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
278# include "NimBLEServer.h"
279# include "NimBLEService.h"
280# include "NimBLECharacteristic.h"
281# include "NimBLEDescriptor.h"
282# endif
283
284# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
285# if CONFIG_BT_NIMBLE_EXT_ADV
286# include "NimBLEExtAdvertising.h"
287# else
288# include "NimBLEAdvertising.h"
289# endif
290# endif
291
292# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
293# include "NimBLEConnInfo.h"
294# endif
295
296# include "NimBLEUtils.h"
297
298#endif // CONFIG_BT_ENABLED
299#endif // NIMBLE_CPP_DEVICE_H_
A BLE device address.
Definition NimBLEAddress.h:41
Perform and manage BLE advertising.
Definition NimBLEAdvertising.h:52
The model of a BLE Characteristic.
Definition NimBLECharacteristic.h:40
A model of a BLE client.
Definition NimBLEClient.h:49
Connection information.
Definition NimBLEConnInfo.h:32
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:116
static NimBLEClient * createClient()
Creates a new client object, each client can connect to 1 peripheral device.
Definition NimBLEDevice.cpp:291
static void onSync(void)
Host synced with controller, all clear to make calls to the stack.
Definition NimBLEDevice.cpp:781
static size_t getCreatedClientCount()
Get the number of created client objects.
Definition NimBLEDevice.cpp:351
static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask)
Set the preferred default phy to use for connections.
Definition NimBLEDevice.cpp:754
static bool setOwnAddr(const NimBLEAddress &addr)
Set the device address to use.
Definition NimBLEDevice.cpp:1058
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client available.
Definition NimBLEDevice.cpp:396
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition NimBLEDevice.cpp:616
static bool setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition NimBLEDevice.cpp:1232
static bool deleteAllBonds()
Deletes all bonding information.
Definition NimBLEDevice.cpp:593
static bool startAdvertising(uint8_t instId, int duration=0, int maxEvents=0)
Convenience function to begin advertising.
Definition NimBLEDevice.cpp:167
static void onReset(int reason)
Host reset, we pass the message so we don't make calls until re-synced.
Definition NimBLEDevice.cpp:768
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition NimBLEDevice.cpp:607
static bool init(const std::string &deviceName)
Initialize the BLE environment.
Definition NimBLEDevice.cpp:840
static bool setOwnAddrType(uint8_t type)
Sets the address type to use.
Definition NimBLEDevice.cpp:1027
static bool stopAdvertising()
Convenience function to stop all advertising.
Definition NimBLEDevice.cpp:207
static NimBLEServer * getServer()
Get the instance of the server.
Definition NimBLEDevice.cpp:137
static bool isInitialized()
Check if the initialization is complete.
Definition NimBLEDevice.cpp:993
static bool setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition NimBLEDevice.cpp:553
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition NimBLEDevice.cpp:726
static NimBLEServer * createServer()
Create an instance of a server.
Definition NimBLEDevice.cpp:122
static bool injectPassKey(const NimBLEConnInfo &peerInfo, uint32_t pin)
Inject the provided passkey into the Security Manager.
Definition NimBLEDevice.cpp:1189
static uint16_t getMTU()
Get local MTU value set.
Definition NimBLEDevice.cpp:566
static int getPower(NimBLETxPowerType type=NimBLETxPowerType::All)
Get the transmission power.
Definition NimBLEDevice.cpp:512
static bool deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition NimBLEDevice.cpp:938
static NimBLEAddress getAddress()
Get our device address.
Definition NimBLEDevice.cpp:1005
static int getNumBonds()
Gets the number of bonded peers stored.
Definition NimBLEDevice.cpp:578
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition NimBLEDevice.cpp:695
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:318
static bool startSecurity(uint16_t connHandle, int *rcPtr=nullptr)
Start the connection securing and authorization for this connection.
Definition NimBLEDevice.cpp:1171
static NimBLEClient * getClientByHandle(uint16_t connHandle)
Get a reference to a client by connection handle.
Definition NimBLEDevice.cpp:367
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition NimBLEDevice.cpp:1161
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition NimBLEDevice.cpp:1088
static NimBLEExtAdvertising * getAdvertising()
Get the instance of the extended advertising object.
Definition NimBLEDevice.cpp:152
static bool setDeviceName(const std::string &deviceName)
Set the BLEDevice name.
Definition NimBLEDevice.cpp:1217
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition NimBLEDevice.cpp:676
static void setSecurityInitKey(uint8_t initKey)
If we are the initiator of the security procedure this sets the keys we will distribute.
Definition NimBLEDevice.cpp:1132
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition NimBLEDevice.cpp:717
static void host_task(void *param)
The main host task.
Definition NimBLEDevice.cpp:830
static void setSecurityPasskey(uint32_t passKey)
Set the passkey the server will ask for when pairing.
Definition NimBLEDevice.cpp:1153
static void setSecurityRespKey(uint8_t respKey)
Set the keys we are willing to accept during pairing.
Definition NimBLEDevice.cpp:1145
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition NimBLEDevice.cpp:1119
static std::string toString()
Return a string representation of the address of this device.
Definition NimBLEDevice.cpp:1248
static bool setPower(int8_t dbm, NimBLETxPowerType type=NimBLETxPowerType::All)
Set the transmission power.
Definition NimBLEDevice.cpp:467
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition NimBLEDevice.cpp:640
static std::vector< NimBLEClient * > getConnectedClients()
Get a list of connected clients.
Definition NimBLEDevice.cpp:410
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition NimBLEDevice.cpp:661
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition NimBLEDevice.cpp:222
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peerAddress)
Get a reference to a client by peer address.
Definition NimBLEDevice.cpp:382
static bool injectConfirmPasskey(const NimBLEConnInfo &peerInfo, bool accept)
Inject the provided numeric comparison response into the Security Manager.
Definition NimBLEDevice.cpp:1201
Extended advertisement data.
Definition NimBLEExtAdvertising.h:46
Extended advertising class.
Definition NimBLEExtAdvertising.h:110
Perform and manage BLE scans.
Definition NimBLEScan.h:67
The model of a BLE server.
Definition NimBLEServer.h:60
Definition ble_gap.h:2568
Definition ble_gap.h:510