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 #include "NimBLEAdvertising.h"
29 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
30 #include "NimBLEClient.h"
33 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
34 #include "NimBLEServer.h"
37 #include "NimBLEUtils.h"
38 #include "NimBLESecurity.h"
39 #include "NimBLEAddress.h"
49 #define BLEDevice NimBLEDevice
50 #define BLEClient NimBLEClient
51 #define BLERemoteService NimBLERemoteService
52 #define BLERemoteCharacteristic NimBLERemoteCharacteristic
53 #define BLERemoteDescriptor NimBLERemoteDescriptor
54 #define BLEAdvertisedDevice NimBLEAdvertisedDevice
55 #define BLEScan NimBLEScan
56 #define BLEUUID NimBLEUUID
57 #define BLESecurity NimBLESecurity
58 #define BLESecurityCallbacks NimBLESecurityCallbacks
59 #define BLEAddress NimBLEAddress
60 #define BLEUtils NimBLEUtils
61 #define BLEClientCallbacks NimBLEClientCallbacks
62 #define BLEAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks
63 #define BLEScanResults NimBLEScanResults
64 #define BLEServer NimBLEServer
65 #define BLEService NimBLEService
66 #define BLECharacteristic NimBLECharacteristic
67 #define BLEAdvertising NimBLEAdvertising
68 #define BLEServerCallbacks NimBLEServerCallbacks
69 #define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
70 #define BLEAdvertisementData NimBLEAdvertisementData
71 #define BLEDescriptor NimBLEDescriptor
72 #define BLE2902 NimBLE2902
73 #define BLE2904 NimBLE2904
74 #define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
75 #define BLEBeacon NimBLEBeacon
76 #define BLEEddystoneTLM NimBLEEddystoneTLM
77 #define BLEEddystoneURL NimBLEEddystoneURL
79 #ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
80 #define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
82 #define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
85 typedef int (*gap_event_handler)(ble_gap_event *event,
void *arg);
87 extern "C" void ble_store_config_init(
void);
94 static void init(
const std::string &deviceName);
95 static void deinit(
bool clearAll =
false);
105 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
109 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
115 static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
116 static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
117 static void setOwnAddrType(uint8_t own_addr_type,
bool useNRPA=
false);
118 static void setScanDuplicateCacheSize(uint16_t cacheSize);
119 static void setScanFilterMode(uint8_t type);
121 static void setPower(
int dbm);
122 static int getPower();
135 static int setMTU(uint16_t mtu);
141 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
147 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
157 #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
166 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
170 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
174 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
179 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
183 static void onReset(
int reason);
184 static void onSync(
void);
185 static void host_task(
void *param);
186 static bool m_synced;
188 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
192 #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
196 #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
200 #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
201 static std::list <NimBLEClient*> m_cList;
203 static std::list <NimBLEAddress> m_ignoreList;
205 static uint32_t m_passkey;
206 static ble_gap_event_listener m_listener;
207 static gap_event_handler m_customGapHandler;
208 static uint8_t m_own_addr_type;
210 static uint16_t m_scanDuplicateSize;
211 static uint8_t m_scanFilterMode;
213 static std::vector<NimBLEAddress> m_whiteList;
A BLE device address.
Definition: NimBLEAddress.h:39
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:85
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:62
A model of a BLE client.
Definition: NimBLEClient.h:38
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:92
static void removeIgnored(const NimBLEAddress &address)
Remove a device from the ignore list.
Definition: NimBLEDevice.cpp:1133
static NimBLEClient * getDisconnectedClient()
Finds the first disconnected client in the list.
Definition: NimBLEDevice.cpp:288
static bool isIgnored(const NimBLEAddress &address)
Check if the device address is on our ignore list.
Definition: NimBLEDevice.cpp:1107
static bool isBonded(const NimBLEAddress &address)
Checks if a peer device is bonded.
Definition: NimBLEDevice.cpp:551
static std::list< NimBLEClient * > * getClientList()
Get the list of created client objects.
Definition: NimBLEDevice.cpp:235
static bool deleteBond(const NimBLEAddress &address)
Deletes a peer bond.
Definition: NimBLEDevice.cpp:531
static int startSecurity(uint16_t conn_id)
Start the connection securing and authorization for this connection.
Definition: NimBLEDevice.cpp:1091
static NimBLEServer * getServer()
Get the instance of the server.
Definition: NimBLEDevice.cpp:110
static NimBLEAddress getWhiteListAddress(size_t index)
Gets the address at the vector index.
Definition: NimBLEDevice.cpp:700
static NimBLEServer * createServer()
Create a new instance of a server.
Definition: NimBLEDevice.cpp:94
static uint16_t getMTU()
Get local MTU value set.
Definition: NimBLEDevice.cpp:445
static void setSecurityRespKey(uint8_t init_key)
Set the keys we are willing to accept during pairing.
Definition: NimBLEDevice.cpp:1012
static void init(const std::string &deviceName)
Initialize the BLE environment.
Definition: NimBLEDevice.cpp:802
static void addIgnored(const NimBLEAddress &address)
Add a device to the ignore list.
Definition: NimBLEDevice.cpp:1123
static NimBLEAddress getAddress()
Get our device address.
Definition: NimBLEDevice.cpp:397
static void setSecurityCallbacks(NimBLESecurityCallbacks *pCallbacks)
Set callbacks that will be used to handle encryption negotiation events and authentication events.
Definition: NimBLEDevice.cpp:1043
static int setMTU(uint16_t mtu)
Setup local mtu that will be used to negotiate mtu during request from client peer.
Definition: NimBLEDevice.cpp:426
static int getNumBonds()
Gets the number of bonded peers stored.
Definition: NimBLEDevice.cpp:503
static bool whiteListRemove(const NimBLEAddress &address)
Remove a peer address from the whitelist.
Definition: NimBLEDevice.cpp:649
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:190
static void setSecurityPasskey(uint32_t pin)
Set the passkey the server will ask for when pairing.
Definition: NimBLEDevice.cpp:1022
static void stopAdvertising()
Convenience function to stop advertising.
Definition: NimBLEDevice.cpp:140
static void setCustomGapHandler(gap_event_handler handler)
Set a custom callback for gap events.
Definition: NimBLEDevice.cpp:1148
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:997
static uint32_t getSecurityPasskey()
Get the current passkey used for pairing.
Definition: NimBLEDevice.cpp:1032
static void setSecurityAuth(bool bonding, bool mitm, bool sc)
Set the authorization mode for this device.
Definition: NimBLEDevice.cpp:947
static NimBLEAdvertising * getAdvertising()
Get the instance of the advertising object.
Definition: NimBLEDevice.cpp:121
static bool whiteListAdd(const NimBLEAddress &address)
Add a peer address to the whitelist.
Definition: NimBLEDevice.cpp:617
static size_t getWhiteListCount()
Gets the count of addresses in the whitelist.
Definition: NimBLEDevice.cpp:689
static void deleteAllBonds()
Deletes all bonding information.
Definition: NimBLEDevice.cpp:520
static void setSecurityIOCap(uint8_t iocap)
Set the Input/Output capabilities of this device.
Definition: NimBLEDevice.cpp:982
static size_t getClientListSize()
Get the number of created client objects.
Definition: NimBLEDevice.cpp:245
static std::string toString()
Return a string representation of the address of this device.
Definition: NimBLEDevice.cpp:415
static NimBLEClient * getClientByPeerAddress(const NimBLEAddress &peer_addr)
Get a reference to a client by peer address.
Definition: NimBLEDevice.cpp:273
static NimBLEClient * getClientByID(uint16_t conn_id)
Get a reference to a client by connection ID.
Definition: NimBLEDevice.cpp:256
static NimBLEAddress getBondedAddress(int index)
Get the address of a bonded peer device by index.
Definition: NimBLEDevice.cpp:577
static void startAdvertising()
Convenience function to begin advertising.
Definition: NimBLEDevice.cpp:132
static bool getInitialized()
Check if the initialization is complete.
Definition: NimBLEDevice.cpp:935
static bool onWhiteList(const NimBLEAddress &address)
Checks if a peer device is whitelisted.
Definition: NimBLEDevice.cpp:600
static void deinit(bool clearAll=false)
Shutdown the NimBLE stack/controller.
Definition: NimBLEDevice.cpp:878
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:171
static NimBLEScan * getScan()
Retrieve the Scan object that we use for scanning.
Definition: NimBLEDevice.cpp:153
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:42