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);
109 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
113 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
119 static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
120 static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
121 static void setOwnAddrType(uint8_t own_addr_type,
bool useNRPA=
false);
122 static void setScanDuplicateCacheSize(uint16_t cacheSize);
123 static void setScanFilterMode(uint8_t type);
125 static void setPower(
int dbm);
126 static int getPower();
139 static int setMTU(uint16_t mtu);
145 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
146 # if CONFIG_BT_NIMBLE_EXT_ADV
160 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
170 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
179 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
183 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
187 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
192 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
194 # if CONFIG_BT_NIMBLE_EXT_ADV
200 static void onReset(
int reason);
201 static void onSync(
void);
202 static void host_task(
void *param);
203 static bool m_synced;
205 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
209 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
213 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
214 # if CONFIG_BT_NIMBLE_EXT_ADV
221 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
222 static std::list <NimBLEClient*> m_cList;
224 static std::list <NimBLEAddress> m_ignoreList;
226 static uint32_t m_passkey;
227 static ble_gap_event_listener m_listener;
228 static gap_event_handler m_customGapHandler;
229 static uint8_t m_own_addr_type;
231 static uint16_t m_scanDuplicateSize;
232 static uint8_t m_scanFilterMode;
234 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:1178
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:1152
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:1136
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:1057
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:1168
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:1088
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:1067
static void setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition: NimBLEDevice.cpp:1193
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:1042
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition: NimBLEDevice.cpp:1077
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition: NimBLEDevice.cpp:992
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:1027
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 getInitialized()
Check if the initialization is complete.
Definition: NimBLEDevice.cpp:980
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 bool startAdvertising(uint8_t inst_id, int duration=0, int max_events=0)
Convenience function to begin advertising.
Definition: NimBLEDevice.cpp:141
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