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)
71# define BLEDevice NimBLEDevice
72# define BLEClient NimBLEClient
73# define BLERemoteService NimBLERemoteService
74# define BLERemoteCharacteristic NimBLERemoteCharacteristic
75# define BLERemoteDescriptor NimBLERemoteDescriptor
76# define BLEAdvertisedDevice NimBLEAdvertisedDevice
77# define BLEScan NimBLEScan
78# define BLEUUID NimBLEUUID
79# define BLEAddress NimBLEAddress
80# define BLEUtils NimBLEUtils
81# define BLEClientCallbacks NimBLEClientCallbacks
82# define BLEAdvertisedDeviceCallbacks NimBLEScanCallbacks
83# define BLEScanResults NimBLEScanResults
84# define BLEServer NimBLEServer
85# define BLEService NimBLEService
86# define BLECharacteristic NimBLECharacteristic
87# define BLEAdvertising NimBLEAdvertising
88# define BLEServerCallbacks NimBLEServerCallbacks
89# define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
90# define BLEAdvertisementData NimBLEAdvertisementData
91# define BLEDescriptor NimBLEDescriptor
92# define BLE2904 NimBLE2904
93# define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
94# define BLEBeacon NimBLEBeacon
95# define BLEEddystoneTLM NimBLEEddystoneTLM
96# define BLEEddystoneURL NimBLEEddystoneURL
97# define BLEConnInfo NimBLEConnInfo
99# ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
100# define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
102# define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
105enum class NimBLETxPowerType {
112typedef int (*gap_event_handler)(ble_gap_event* event,
void* arg);
119 static bool init(
const std::string& deviceName);
120 static bool deinit(
bool clearAll =
false);
134 static void setScanDuplicateCacheSize(uint16_t cacheSize);
135 static void setScanFilterMode(uint8_t type);
144 static bool startSecurity(uint16_t connHandle,
int* rcPtr =
nullptr);
145 static bool setMTU(uint16_t mtu);
147 static void onReset(
int reason);
150 static int getPower(NimBLETxPowerType type = NimBLETxPowerType::All);
151 static bool setPower(int8_t dbm, NimBLETxPowerType type = NimBLETxPowerType::All);
154# ifndef CONFIG_IDF_TARGET_ESP32P4
155 static esp_power_level_t getPowerLevel(esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
156 static bool setPowerLevel(esp_power_level_t powerLevel, esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
160# if CONFIG_BT_NIMBLE_EXT_ADV
161 static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask);
164# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
168# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
173# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
178# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
179# if CONFIG_BT_NIMBLE_EXT_ADV
181 static bool startAdvertising(uint8_t instId,
int duration = 0,
int maxEvents = 0);
185# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
192# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
203# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
212 static bool m_synced;
213 static bool m_initialized;
214 static uint32_t m_passkey;
215 static ble_gap_event_listener m_listener;
216 static uint8_t m_ownAddrType;
217 static std::vector<NimBLEAddress> m_whiteList;
221# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
225# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
229# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
230# if CONFIG_BT_NIMBLE_EXT_ADV
237# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
238 static std::array<NimBLEClient*, NIMBLE_MAX_CONNECTIONS> m_pClients;
242# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
243 static uint16_t m_scanDuplicateSize;
244 static uint8_t m_scanFilterMode;
248# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
252# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
256# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
261# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
263# if CONFIG_BT_NIMBLE_EXT_ADV
270# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
271# include "NimBLEClient.h"
272# include "NimBLERemoteService.h"
273# include "NimBLERemoteCharacteristic.h"
274# include "NimBLERemoteDescriptor.h"
277# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
278# include "NimBLEScan.h"
281# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
282# include "NimBLEServer.h"
283# include "NimBLEService.h"
284# include "NimBLECharacteristic.h"
285# include "NimBLEDescriptor.h"
288# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
289# if CONFIG_BT_NIMBLE_EXT_ADV
290# include "NimBLEExtAdvertising.h"
292# include "NimBLEAdvertising.h"
296# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
297# include "NimBLEConnInfo.h"
300# include "NimBLEUtils.h"
321 virtual int onStoreStatus(
struct ble_store_status_event* event,
void* arg);
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
Callbacks associated with a BLE device.
Definition NimBLEDevice.h:305
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:1274
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:117
static NimBLEClient * createClient()
Creates a new client object, each client can connect to 1 peripheral device.
Definition NimBLEDevice.cpp:294
static void onSync(void)
Host synced with controller, all clear to make calls to the stack.
Definition NimBLEDevice.cpp:784
static size_t getCreatedClientCount()
Get the number of created client objects.
Definition NimBLEDevice.cpp:354
static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask)
Set the preferred default phy to use for connections.
Definition NimBLEDevice.cpp:757
static bool setOwnAddr(const NimBLEAddress &addr)
Set the device address to use.
Definition NimBLEDevice.cpp:1063
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client available.
Definition NimBLEDevice.cpp:399
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition NimBLEDevice.cpp:619
static bool setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition NimBLEDevice.cpp:1237
static bool deleteAllBonds()
Deletes all bonding information.
Definition NimBLEDevice.cpp:596
static bool startAdvertising(uint8_t instId, int duration=0, int maxEvents=0)
Convenience function to begin advertising.
Definition NimBLEDevice.cpp:170
static void onReset(int reason)
Host reset, we pass the message so we don't make calls until re-synced.
Definition NimBLEDevice.cpp:771
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition NimBLEDevice.cpp:610
static bool init(const std::string &deviceName)
Initialize the BLE environment.
Definition NimBLEDevice.cpp:843
static bool setOwnAddrType(uint8_t type)
Sets the address type to use.
Definition NimBLEDevice.cpp:1032
static bool stopAdvertising()
Convenience function to stop all advertising.
Definition NimBLEDevice.cpp:210
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:998
static bool setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition NimBLEDevice.cpp:556
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition NimBLEDevice.cpp:729
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:1194
static uint16_t getMTU()
Get local MTU value set.
Definition NimBLEDevice.cpp:569
static int getPower(NimBLETxPowerType type=NimBLETxPowerType::All)
Get the transmission power.
Definition NimBLEDevice.cpp:515
static bool deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition NimBLEDevice.cpp:943
static NimBLEAddress getAddress()
Get our device address.
Definition NimBLEDevice.cpp:1010
static int getNumBonds()
Gets the number of bonded peers stored.
Definition NimBLEDevice.cpp:581
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition NimBLEDevice.cpp:698
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:321
static bool startSecurity(uint16_t connHandle, int *rcPtr=nullptr)
Start the connection securing and authorization for this connection.
Definition NimBLEDevice.cpp:1176
static NimBLEClient * getClientByHandle(uint16_t connHandle)
Get a reference to a client by connection handle.
Definition NimBLEDevice.cpp:370
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition NimBLEDevice.cpp:1166
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition NimBLEDevice.cpp:1093
static NimBLEExtAdvertising * getAdvertising()
Get the instance of the extended advertising object.
Definition NimBLEDevice.cpp:155
static bool setDeviceName(const std::string &deviceName)
Set the BLEDevice name.
Definition NimBLEDevice.cpp:1222
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition NimBLEDevice.cpp:679
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:1137
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition NimBLEDevice.cpp:720
static void host_task(void *param)
The main host task.
Definition NimBLEDevice.cpp:833
static void setSecurityPasskey(uint32_t passKey)
Set the passkey the server will ask for when pairing.
Definition NimBLEDevice.cpp:1158
static void setSecurityRespKey(uint8_t respKey)
Set the keys we are willing to accept during pairing.
Definition NimBLEDevice.cpp:1150
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition NimBLEDevice.cpp:1124
static std::string toString()
Return a string representation of the address of this device.
Definition NimBLEDevice.cpp:1253
static bool setPower(int8_t dbm, NimBLETxPowerType type=NimBLETxPowerType::All)
Set the transmission power.
Definition NimBLEDevice.cpp:470
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition NimBLEDevice.cpp:643
static std::vector< NimBLEClient * > getConnectedClients()
Get a list of connected clients.
Definition NimBLEDevice.cpp:413
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition NimBLEDevice.cpp:664
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition NimBLEDevice.cpp:225
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peerAddress)
Get a reference to a client by peer address.
Definition NimBLEDevice.cpp:385
static bool injectConfirmPasskey(const NimBLEConnInfo &peerInfo, bool accept)
Inject the provided numeric comparison response into the Security Manager.
Definition NimBLEDevice.cpp:1206
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