18#ifndef NIMBLE_CPP_DEVICE_H_
19#define NIMBLE_CPP_DEVICE_H_
24# ifndef CONFIG_IDF_TARGET_ESP32P4
27# define NIMBLE_CPP_SCAN_DUPL_ENABLED \
28 (CONFIG_BTDM_BLE_SCAN_DUPL || CONFIG_BT_LE_SCAN_DUPL || CONFIG_BT_CTRL_BLE_SCAN_DUPL)
31# if defined(CONFIG_NIMBLE_CPP_IDF)
32# include <host/ble_gap.h>
34# include <nimble/nimble/host/include/host/ble_gap.h>
45# if CONFIG_BT_NIMBLE_ROLE_CENTRAL
50# if CONFIG_BT_NIMBLE_ROLE_OBSERVER
54# if CONFIG_BT_NIMBLE_ROLE_BROADCASTER
55# if CONFIG_BT_NIMBLE_EXT_ADV
62# if CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
64# if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 0
65class NimBLEL2CAPServer;
69# if CONFIG_BT_NIMBLE_ROLE_PERIPHERAL || CONFIG_BT_NIMBLE_ROLE_CENTRAL
76# define BLEDevice NimBLEDevice
77# define BLEClient NimBLEClient
78# define BLERemoteService NimBLERemoteService
79# define BLERemoteCharacteristic NimBLERemoteCharacteristic
80# define BLERemoteDescriptor NimBLERemoteDescriptor
81# define BLEAdvertisedDevice NimBLEAdvertisedDevice
82# define BLEScan NimBLEScan
83# define BLEUUID NimBLEUUID
84# define BLEAddress NimBLEAddress
85# define BLEUtils NimBLEUtils
86# define BLEClientCallbacks NimBLEClientCallbacks
87# define BLEAdvertisedDeviceCallbacks NimBLEScanCallbacks
88# define BLEScanResults NimBLEScanResults
89# define BLEServer NimBLEServer
90# define BLEService NimBLEService
91# define BLECharacteristic NimBLECharacteristic
92# define BLEAdvertising NimBLEAdvertising
93# define BLEServerCallbacks NimBLEServerCallbacks
94# define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
95# define BLEAdvertisementData NimBLEAdvertisementData
96# define BLEDescriptor NimBLEDescriptor
97# define BLE2904 NimBLE2904
98# define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
99# define BLEBeacon NimBLEBeacon
100# define BLEEddystoneTLM NimBLEEddystoneTLM
101# define BLEEddystoneURL NimBLEEddystoneURL
102# define BLEConnInfo NimBLEConnInfo
103# define BLEL2CAPServer NimBLEL2CAPServer
104# define BLEL2CAPService NimBLEL2CAPService
105# define BLEL2CAPServiceCallbacks NimBLEL2CAPServiceCallbacks
106# define BLEL2CAPClient NimBLEL2CAPClient
107# define BLEL2CAPClientCallbacks NimBLEL2CAPClientCallbacks
108# define BLEL2CAPChannel NimBLEL2CAPChannel
109# define BLEL2CAPChannelCallbacks NimBLEL2CAPChannelCallbacks
111# ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
112# define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
114# define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
117enum class NimBLETxPowerType { All = 0, Advertise = 1, Scan = 2, Connection = 3 };
119typedef int (*gap_event_handler)(ble_gap_event* event,
void* arg);
126 static bool init(
const std::string& deviceName);
127 static bool deinit(
bool clearAll =
false);
141 static void setScanDuplicateCacheSize(uint16_t cacheSize);
142 static void setScanFilterMode(uint8_t type);
143 static void setScanDuplicateCacheResetTime(uint16_t time);
152 static bool startSecurity(uint16_t connHandle,
int* rcPtr =
nullptr);
153 static bool setMTU(uint16_t mtu);
155 static void onReset(
int reason);
158 static int getPower(NimBLETxPowerType type = NimBLETxPowerType::All);
159 static bool setPower(int8_t dbm, NimBLETxPowerType type = NimBLETxPowerType::All);
160 static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask);
163# ifndef CONFIG_IDF_TARGET_ESP32P4
164 static esp_power_level_t getPowerLevel(esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
165 static bool setPowerLevel(esp_power_level_t powerLevel, esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
169# if CONFIG_BT_NIMBLE_ROLE_OBSERVER
173# if CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
176# if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 0
177 static NimBLEL2CAPServer* createL2CAPServer();
178 static NimBLEL2CAPServer* getL2CAPServer();
182# if CONFIG_BT_NIMBLE_ROLE_PERIPHERAL || CONFIG_BT_NIMBLE_ROLE_CENTRAL
187# if CONFIG_BT_NIMBLE_ROLE_BROADCASTER
188# if CONFIG_BT_NIMBLE_EXT_ADV
190 static bool startAdvertising(uint8_t instId,
int duration = 0,
int maxEvents = 0);
194# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
201# if CONFIG_BT_NIMBLE_ROLE_CENTRAL
212# if CONFIG_BT_NIMBLE_ROLE_CENTRAL || CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
221 static bool m_synced;
222 static bool m_initialized;
223 static uint32_t m_passkey;
224 static ble_gap_event_listener m_listener;
225 static uint8_t m_ownAddrType;
226 static std::vector<NimBLEAddress> m_whiteList;
230# if CONFIG_BT_NIMBLE_ROLE_OBSERVER
234# if CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
236# if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 0
237 static NimBLEL2CAPServer* m_pL2CAPServer;
241# if CONFIG_BT_NIMBLE_ROLE_BROADCASTER
242# if CONFIG_BT_NIMBLE_EXT_ADV
249# if CONFIG_BT_NIMBLE_ROLE_CENTRAL
250 static std::array<NimBLEClient*, NIMBLE_MAX_CONNECTIONS> m_pClients;
254# if NIMBLE_CPP_SCAN_DUPL_ENABLED
255 static uint16_t m_scanDuplicateSize;
256 static uint8_t m_scanFilterMode;
257 static uint16_t m_scanDuplicateResetTime;
261# if CONFIG_BT_NIMBLE_ROLE_CENTRAL
265# if CONFIG_BT_NIMBLE_ROLE_OBSERVER
269# if CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
274# if CONFIG_BT_NIMBLE_ROLE_BROADCASTER
276# if CONFIG_BT_NIMBLE_EXT_ADV
283# if CONFIG_BT_NIMBLE_ROLE_CENTRAL
284# include "NimBLEClient.h"
285# include "NimBLERemoteService.h"
286# include "NimBLERemoteCharacteristic.h"
287# include "NimBLERemoteDescriptor.h"
290# if CONFIG_BT_NIMBLE_ROLE_OBSERVER
291# include "NimBLEScan.h"
294# if CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
295# include "NimBLEServer.h"
296# include "NimBLEService.h"
297# include "NimBLECharacteristic.h"
298# include "NimBLEDescriptor.h"
299# if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM
300# include "NimBLEL2CAPServer.h"
301# include "NimBLEL2CAPChannel.h"
305# if CONFIG_BT_NIMBLE_ROLE_BROADCASTER
306# if CONFIG_BT_NIMBLE_EXT_ADV
307# include "NimBLEExtAdvertising.h"
309# include "NimBLEAdvertising.h"
313# if CONFIG_BT_NIMBLE_ROLE_CENTRAL || CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
314# include "NimBLEConnInfo.h"
317# include "NimBLEAddress.h"
318# include "NimBLEUtils.h"
339 virtual int onStoreStatus(
struct ble_store_status_event* event,
void* arg);
A BLE device address.
Definition NimBLEAddress.h:42
Perform and manage BLE advertising.
Definition NimBLEAdvertising.h:51
The model of a BLE Characteristic.
Definition NimBLECharacteristic.h:41
A model of a BLE client.
Definition NimBLEClient.h:49
Connection information.
Definition NimBLEConnInfo.h:33
Callbacks associated with a BLE device.
Definition NimBLEDevice.h:323
virtual int onStoreStatus(struct ble_store_status_event *event, void *arg)
Indicates an inability to perform a store operation. This callback should do one of two things: -Addr...
Definition NimBLEDevice.cpp:1348
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:124
static NimBLEClient * createClient()
Creates a new client object, each client can connect to 1 peripheral device.
Definition NimBLEDevice.cpp:334
static void onSync(void)
Host synced with controller, all clear to make calls to the stack.
Definition NimBLEDevice.cpp:820
static size_t getCreatedClientCount()
Get the number of created client objects.
Definition NimBLEDevice.cpp:394
static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask)
Set the preferred default phy to use for connections.
Definition NimBLEDevice.cpp:794
static bool setOwnAddr(const NimBLEAddress &addr)
Set the device address to use.
Definition NimBLEDevice.cpp:1136
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client available.
Definition NimBLEDevice.cpp:439
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition NimBLEDevice.cpp:657
static bool deleteAllBonds()
Deletes all bonding information.
Definition NimBLEDevice.cpp:634
static bool startAdvertising(uint8_t instId, int duration=0, int maxEvents=0)
Convenience function to begin advertising.
Definition NimBLEDevice.cpp:191
static void onReset(int reason)
Host reset, we pass the message so we don't make calls until re-synced.
Definition NimBLEDevice.cpp:807
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition NimBLEDevice.cpp:648
static bool init(const std::string &deviceName)
Initialize the BLE environment.
Definition NimBLEDevice.cpp:879
static bool setOwnAddrType(uint8_t type)
Sets the address type to use.
Definition NimBLEDevice.cpp:1107
static bool stopAdvertising()
Convenience function to stop all advertising.
Definition NimBLEDevice.cpp:231
static NimBLEServer * getServer()
Get the instance of the server.
Definition NimBLEDevice.cpp:140
static bool isInitialized()
Check if the initialization is complete.
Definition NimBLEDevice.cpp:1073
static bool setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition NimBLEDevice.cpp:594
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition NimBLEDevice.cpp:767
static NimBLEServer * createServer()
Create an instance of a server.
Definition NimBLEDevice.cpp:125
static bool injectPassKey(const NimBLEConnInfo &peerInfo, uint32_t pin)
Inject the provided passkey into the Security Manager.
Definition NimBLEDevice.cpp:1267
static uint16_t getMTU()
Get local MTU value set.
Definition NimBLEDevice.cpp:607
static int getPower(NimBLETxPowerType type=NimBLETxPowerType::All)
Get the transmission power.
Definition NimBLEDevice.cpp:553
static bool deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition NimBLEDevice.cpp:1012
static NimBLEAddress getAddress()
Get our device address.
Definition NimBLEDevice.cpp:1085
static int getNumBonds()
Gets the number of bonded peers stored.
Definition NimBLEDevice.cpp:619
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition NimBLEDevice.cpp:736
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:361
static bool startSecurity(uint16_t connHandle, int *rcPtr=nullptr)
Start the connection securing and authorization for this connection.
Definition NimBLEDevice.cpp:1249
static NimBLEClient * getClientByHandle(uint16_t connHandle)
Get a reference to a client by connection handle.
Definition NimBLEDevice.cpp:410
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition NimBLEDevice.cpp:1239
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition NimBLEDevice.cpp:1166
static NimBLEExtAdvertising * getAdvertising()
Get the instance of the extended advertising object.
Definition NimBLEDevice.cpp:176
static bool setDeviceName(const std::string &deviceName)
Set the BLEDevice name.
Definition NimBLEDevice.cpp:1295
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition NimBLEDevice.cpp:717
static bool setCustomGapHandler(gap_event_handler handler, void *arg=nullptr)
Set a custom callback for gap events.
Definition NimBLEDevice.cpp:1311
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:1210
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition NimBLEDevice.cpp:758
static void host_task(void *param)
The main host task.
Definition NimBLEDevice.cpp:869
static void setSecurityPasskey(uint32_t passKey)
Set the passkey the server will ask for when pairing.
Definition NimBLEDevice.cpp:1231
static void setSecurityRespKey(uint8_t respKey)
Set the keys we are willing to accept during pairing.
Definition NimBLEDevice.cpp:1223
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition NimBLEDevice.cpp:1197
static std::string toString()
Return a string representation of the address of this device.
Definition NimBLEDevice.cpp:1327
static bool setPower(int8_t dbm, NimBLETxPowerType type=NimBLETxPowerType::All)
Set the transmission power.
Definition NimBLEDevice.cpp:510
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition NimBLEDevice.cpp:681
static std::vector< NimBLEClient * > getConnectedClients()
Get a list of connected clients.
Definition NimBLEDevice.cpp:453
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition NimBLEDevice.cpp:702
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition NimBLEDevice.cpp:246
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peerAddress)
Get a reference to a client by peer address.
Definition NimBLEDevice.cpp:425
static bool injectConfirmPasskey(const NimBLEConnInfo &peerInfo, bool accept)
Inject the provided numeric comparison response into the Security Manager.
Definition NimBLEDevice.cpp:1279
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:62