15 #ifndef MAIN_NIMBLEDEVICE_H_
16 #define MAIN_NIMBLEDEVICE_H_
19 #if defined(CONFIG_BT_ENABLED)
21 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
22 #include "NimBLEScan.h"
25 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
26 # if CONFIG_BT_NIMBLE_EXT_ADV
27 # include "NimBLEExtAdvertising.h"
29 # include "NimBLEAdvertising.h"
33 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
34 #include "NimBLEClient.h"
37 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
38 #include "NimBLEServer.h"
41 #include "NimBLEUtils.h"
42 #include "NimBLESecurity.h"
43 #include "NimBLEAddress.h"
53 #define BLEDevice NimBLEDevice
54 #define BLEClient NimBLEClient
55 #define BLERemoteService NimBLERemoteService
56 #define BLERemoteCharacteristic NimBLERemoteCharacteristic
57 #define BLERemoteDescriptor NimBLERemoteDescriptor
58 #define BLEAdvertisedDevice NimBLEAdvertisedDevice
59 #define BLEScan NimBLEScan
60 #define BLEUUID NimBLEUUID
61 #define BLESecurity NimBLESecurity
62 #define BLESecurityCallbacks NimBLESecurityCallbacks
63 #define BLEAddress NimBLEAddress
64 #define BLEUtils NimBLEUtils
65 #define BLEClientCallbacks NimBLEClientCallbacks
66 #define BLEAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks
67 #define BLEScanResults NimBLEScanResults
68 #define BLEServer NimBLEServer
69 #define BLEService NimBLEService
70 #define BLECharacteristic NimBLECharacteristic
71 #define BLEAdvertising NimBLEAdvertising
72 #define BLEServerCallbacks NimBLEServerCallbacks
73 #define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
74 #define BLEAdvertisementData NimBLEAdvertisementData
75 #define BLEDescriptor NimBLEDescriptor
76 #define BLE2902 NimBLE2902
77 #define BLE2904 NimBLE2904
78 #define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
79 #define BLEBeacon NimBLEBeacon
80 #define BLEEddystoneTLM NimBLEEddystoneTLM
81 #define BLEEddystoneURL NimBLEEddystoneURL
83 #ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
84 #define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
86 #define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
89 typedef int (*gap_event_handler)(ble_gap_event *event,
void *arg);
91 extern "C" void ble_store_config_init(
void);
98 static void init(
const std::string &deviceName);
99 static void deinit(
bool clearAll =
false);
110 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
114 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
120 static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
121 static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
122 static void setOwnAddrType(uint8_t own_addr_type,
bool useNRPA=
false);
123 static void setScanDuplicateCacheSize(uint16_t cacheSize);
124 static void setScanFilterMode(uint8_t type);
126 static void setPower(
int dbm);
127 static int getPower();
140 static int setMTU(uint16_t mtu);
146 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
147 # if CONFIG_BT_NIMBLE_EXT_ADV
155 # if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
162 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
172 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
181 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
185 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
189 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
194 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
196 # if CONFIG_BT_NIMBLE_EXT_ADV
202 static void onReset(
int reason);
203 static void onSync(
void);
204 static void host_task(
void *param);
205 static bool m_synced;
207 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
211 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
215 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
216 # if CONFIG_BT_NIMBLE_EXT_ADV
223 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
224 static std::list <NimBLEClient*> m_cList;
226 static std::list <NimBLEAddress> m_ignoreList;
228 static uint32_t m_passkey;
229 static ble_gap_event_listener m_listener;
230 static gap_event_handler m_customGapHandler;
231 static uint8_t m_own_addr_type;
233 static uint16_t m_scanDuplicateSize;
234 static uint8_t m_scanFilterMode;
236 static std::vector<NimBLEAddress> m_whiteList;
A BLE device address.
Definition: NimBLEAddress.h:39
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:87
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:63
A model of a BLE client.
Definition: NimBLEClient.h:39
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:96
static void removeIgnored(const NimBLEAddress &address)
Remove a device from the ignore list.
Definition: NimBLEDevice.cpp:1187
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client in the list.
Definition: NimBLEDevice.cpp:333
static bool isIgnored(const NimBLEAddress &address)
Check if the device address is on our ignore list.
Definition: NimBLEDevice.cpp:1161
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition: NimBLEDevice.cpp:596
static std::list< NimBLEClient * > * getClientList()
Get the list of created client objects.
Definition: NimBLEDevice.cpp:280
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition: NimBLEDevice.cpp:576
static int startSecurity(uint16_t conn_id)
Start the connection securing and authorization for this connection.
Definition: NimBLEDevice.cpp:1145
static bool stopAdvertising()
Convenience function to stop all advertising.
Definition: NimBLEDevice.cpp:185
static NimBLEServer * getServer()
Get the instance of the server.
Definition: NimBLEDevice.cpp:114
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition: NimBLEDevice.cpp:745
static NimBLEServer * createServer()
Create a new instance of a server.
Definition: NimBLEDevice.cpp:98
static uint16_t getMTU()
Get local MTU value set.
Definition: NimBLEDevice.cpp:490
static void setSecurityRespKey(uint8_t init_key)
Set the keys we are willing to accept during pairing.
Definition: NimBLEDevice.cpp:1066
static void init(const std::string &deviceName)
Initialize the BLE environment.
Definition: NimBLEDevice.cpp:847
static void addIgnored(const NimBLEAddress &address)
Add a device to the ignore list.
Definition: NimBLEDevice.cpp:1177
static NimBLEAddress getAddress()
Get our device address.
Definition: NimBLEDevice.cpp:442
static void setSecurityCallbacks(NimBLESecurityCallbacks *pCallbacks)
Set callbacks that will be used to handle encryption negotiation events and authentication events.
Definition: NimBLEDevice.cpp:1097
static int setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition: NimBLEDevice.cpp:471
static int getNumBonds()
Gets the number of bonded peers stored.
Definition: NimBLEDevice.cpp:548
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition: NimBLEDevice.cpp:694
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:235
static void setSecurityPasskey(uint32_t pin)
Set the passkey the server will ask for when pairing.
Definition: NimBLEDevice.cpp:1076
static void setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition: NimBLEDevice.cpp:1202
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:1051
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition: NimBLEDevice.cpp:1086
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition: NimBLEDevice.cpp:1001
static NimBLEExtAdvertising * getAdvertising()
Get the instance of the advertising object.
Definition: NimBLEDevice.cpp:126
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition: NimBLEDevice.cpp:662
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition: NimBLEDevice.cpp:734
static void deleteAllBonds()
Deletes all bonding information.
Definition: NimBLEDevice.cpp:565
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition: NimBLEDevice.cpp:1036
static size_t getClientListSize()
Get the number of created client objects.
Definition: NimBLEDevice.cpp:290
static std::string toString()
Return a string representation of the address of this device.
Definition: NimBLEDevice.cpp:460
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peer_addr)
Get a reference to a client by peer address.
Definition: NimBLEDevice.cpp:318
static NimBLEClient * getClientByID(uint16_t conn_id)
Get a reference to a client by connection ID.
Definition: NimBLEDevice.cpp:301
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition: NimBLEDevice.cpp:622
static bool startAdvertising()
Convenience function to begin advertising.
Definition: NimBLEDevice.cpp:176
static bool getInitialized()
Check if the initialization is complete.
Definition: NimBLEDevice.cpp:989
static void setDeviceName(const std::string &deviceName)
Set the BLEDevice's name.
Definition: NimBLEDevice.cpp:979
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition: NimBLEDevice.cpp:645
static void deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition: NimBLEDevice.cpp:923
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:216
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition: NimBLEDevice.cpp:198
Extended advertisement data.
Definition: NimBLEExtAdvertising.h:37
Extended advertising class.
Definition: NimBLEExtAdvertising.h:92
Perform and manage BLE scans.
Definition: NimBLEScan.h:63
Callbacks to handle GAP events related to authorization. Deprecated - provided for backward compatibi...
Definition: NimBLESecurity.h:94
The model of a BLE server.
Definition: NimBLEServer.h:46