18#ifndef NIMBLE_CPP_DEVICE_H_
19#define NIMBLE_CPP_DEVICE_H_
22#if defined(CONFIG_BT_ENABLED)
24# ifndef CONFIG_IDF_TARGET_ESP32P4
29# if defined(CONFIG_NIMBLE_CPP_IDF)
30# include <host/ble_gap.h>
32# include <nimble/nimble/host/include/host/ble_gap.h>
43# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
48# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
52# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
53# if CONFIG_BT_NIMBLE_EXT_ADV
60# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
64# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
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
98# ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
99# define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
101# define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
104typedef int (*gap_event_handler)(
ble_gap_event* event,
void* arg);
111 static bool init(
const std::string& deviceName);
112 static bool deinit(
bool clearAll =
false);
125 static void setScanDuplicateCacheSize(uint16_t cacheSize);
126 static void setScanFilterMode(uint8_t type);
135 static bool startSecurity(uint16_t connHandle,
int* rcPtr =
nullptr);
136 static bool setMTU(uint16_t mtu);
138 static void onReset(
int reason);
145# ifndef CONFIG_IDF_TARGET_ESP32P4
146 static esp_power_level_t getPowerLevel(esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
147 static bool setPowerLevel(esp_power_level_t powerLevel, esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
151# if CONFIG_BT_NIMBLE_EXT_ADV
152 static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask);
155# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
159# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
164# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
169# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
170# if CONFIG_BT_NIMBLE_EXT_ADV
172 static bool startAdvertising(uint8_t instId,
int duration = 0,
int maxEvents = 0);
176# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
183# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
194# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
203 static bool m_synced;
204 static bool m_initialized;
205 static uint32_t m_passkey;
207 static uint8_t m_ownAddrType;
208 static std::vector<NimBLEAddress> m_whiteList;
210# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
214# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
218# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
219# if CONFIG_BT_NIMBLE_EXT_ADV
226# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
227 static std::array<NimBLEClient*, NIMBLE_MAX_CONNECTIONS> m_pClients;
231# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
232 static uint16_t m_scanDuplicateSize;
233 static uint8_t m_scanFilterMode;
237# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
241# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
245# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
250# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
252# if CONFIG_BT_NIMBLE_EXT_ADV
259# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
260# include "NimBLEClient.h"
261# include "NimBLERemoteService.h"
262# include "NimBLERemoteCharacteristic.h"
263# include "NimBLERemoteDescriptor.h"
266# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
267# include "NimBLEScan.h"
270# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
271# include "NimBLEServer.h"
272# include "NimBLEService.h"
273# include "NimBLECharacteristic.h"
274# include "NimBLEDescriptor.h"
277# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
278# if CONFIG_BT_NIMBLE_EXT_ADV
279# include "NimBLEExtAdvertising.h"
281# include "NimBLEAdvertising.h"
285# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
286# include "NimBLEConnInfo.h"
289# include "NimBLEUtils.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:109
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:760
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:733
static bool setOwnAddr(const NimBLEAddress &addr)
Set the device address to use.
Definition NimBLEDevice.cpp:1033
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:595
static bool setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition NimBLEDevice.cpp:1207
static bool deleteAllBonds()
Deletes all bonding information.
Definition NimBLEDevice.cpp:572
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:747
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition NimBLEDevice.cpp:586
static bool setPower(int8_t dbm)
Set the transmission power.
Definition NimBLEDevice.cpp:467
static bool init(const std::string &deviceName)
Initialize the BLE environment.
Definition NimBLEDevice.cpp:819
static bool setOwnAddrType(uint8_t type)
Sets the address type to use.
Definition NimBLEDevice.cpp:1002
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:968
static bool setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition NimBLEDevice.cpp:532
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition NimBLEDevice.cpp:705
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:1164
static uint16_t getMTU()
Get local MTU value set.
Definition NimBLEDevice.cpp:545
static bool deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition NimBLEDevice.cpp:913
static NimBLEAddress getAddress()
Get our device address.
Definition NimBLEDevice.cpp:980
static int getNumBonds()
Gets the number of bonded peers stored.
Definition NimBLEDevice.cpp:557
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition NimBLEDevice.cpp:674
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:1146
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:1136
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition NimBLEDevice.cpp:1063
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:1192
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition NimBLEDevice.cpp:655
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:1107
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition NimBLEDevice.cpp:696
static void host_task(void *param)
The main host task.
Definition NimBLEDevice.cpp:809
static void setSecurityPasskey(uint32_t passKey)
Set the passkey the server will ask for when pairing.
Definition NimBLEDevice.cpp:1128
static void setSecurityRespKey(uint8_t respKey)
Set the keys we are willing to accept during pairing.
Definition NimBLEDevice.cpp:1120
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition NimBLEDevice.cpp:1094
static std::string toString()
Return a string representation of the address of this device.
Definition NimBLEDevice.cpp:1223
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition NimBLEDevice.cpp:619
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:640
static int getPower()
Get the transmission power.
Definition NimBLEDevice.cpp:496
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:1176
Extended advertisement data.
Definition NimBLEExtAdvertising.h:46
Extended advertising class.
Definition NimBLEExtAdvertising.h:111
Perform and manage BLE scans.
Definition NimBLEScan.h:67
The model of a BLE server.
Definition NimBLEServer.h:60
Definition ble_gap.h:2568