2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEDevice.h
|
|
|
|
*
|
|
|
|
* Created: on Jan 24 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEDevice.h
|
|
|
|
*
|
|
|
|
* Created on: Mar 16, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MAIN_NIMBLEDEVICE_H_
|
|
|
|
#define MAIN_NIMBLEDEVICE_H_
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-03-31 00:12:22 +02:00
|
|
|
#include "NimBLEScan.h"
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
# include "NimBLEExtAdvertising.h"
|
|
|
|
# else
|
|
|
|
# include "NimBLEAdvertising.h"
|
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEClient.h"
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEServer.h"
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "NimBLEUtils.h"
|
|
|
|
#include "NimBLEAddress.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2021-12-29 16:08:25 +01:00
|
|
|
#ifdef ESP_PLATFORM
|
|
|
|
# include "esp_bt.h"
|
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#define BLEDevice NimBLEDevice
|
|
|
|
#define BLEClient NimBLEClient
|
|
|
|
#define BLERemoteService NimBLERemoteService
|
|
|
|
#define BLERemoteCharacteristic NimBLERemoteCharacteristic
|
|
|
|
#define BLERemoteDescriptor NimBLERemoteDescriptor
|
|
|
|
#define BLEAdvertisedDevice NimBLEAdvertisedDevice
|
|
|
|
#define BLEScan NimBLEScan
|
|
|
|
#define BLEUUID NimBLEUUID
|
|
|
|
#define BLESecurity NimBLESecurity
|
|
|
|
#define BLESecurityCallbacks NimBLESecurityCallbacks
|
|
|
|
#define BLEAddress NimBLEAddress
|
|
|
|
#define BLEUtils NimBLEUtils
|
|
|
|
#define BLEClientCallbacks NimBLEClientCallbacks
|
2022-08-27 21:13:27 +02:00
|
|
|
#define BLEAdvertisedDeviceCallbacks NimBLEScanCallbacks
|
2020-03-30 01:44:20 +02:00
|
|
|
#define BLEScanResults NimBLEScanResults
|
|
|
|
#define BLEServer NimBLEServer
|
|
|
|
#define BLEService NimBLEService
|
|
|
|
#define BLECharacteristic NimBLECharacteristic
|
|
|
|
#define BLEAdvertising NimBLEAdvertising
|
|
|
|
#define BLEServerCallbacks NimBLEServerCallbacks
|
|
|
|
#define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
|
2020-05-14 06:03:56 +02:00
|
|
|
#define BLEAdvertisementData NimBLEAdvertisementData
|
|
|
|
#define BLEDescriptor NimBLEDescriptor
|
|
|
|
#define BLE2902 NimBLE2902
|
|
|
|
#define BLE2904 NimBLE2904
|
|
|
|
#define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
|
2020-03-30 01:44:20 +02:00
|
|
|
#define BLEBeacon NimBLEBeacon
|
|
|
|
#define BLEEddystoneTLM NimBLEEddystoneTLM
|
|
|
|
#define BLEEddystoneURL NimBLEEddystoneURL
|
2022-08-27 16:28:15 +02:00
|
|
|
#define BLEConnInfo NimBLEConnInfo
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-04-14 03:13:51 +02:00
|
|
|
#ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
|
2020-03-30 01:44:20 +02:00
|
|
|
#define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
|
2020-05-14 06:03:56 +02:00
|
|
|
#else
|
2020-04-14 03:13:51 +02:00
|
|
|
#define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
|
|
|
|
#endif
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
typedef int (*gap_event_handler)(ble_gap_event *event, void *arg);
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
extern "C" void ble_store_config_init(void);
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief A model of a %BLE Device from which all the BLE roles are created.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
class NimBLEDevice {
|
|
|
|
public:
|
2020-05-14 06:03:56 +02:00
|
|
|
static void init(const std::string &deviceName);
|
2020-07-29 04:57:33 +02:00
|
|
|
static void deinit(bool clearAll = false);
|
2022-07-22 01:33:21 +02:00
|
|
|
static void setDeviceName(const std::string &deviceName);
|
2020-03-30 01:44:20 +02:00
|
|
|
static bool getInitialized();
|
|
|
|
static NimBLEAddress getAddress();
|
|
|
|
static std::string toString();
|
2021-05-23 21:12:44 +02:00
|
|
|
static bool whiteListAdd(const NimBLEAddress & address);
|
|
|
|
static bool whiteListRemove(const NimBLEAddress & address);
|
|
|
|
static bool onWhiteList(const NimBLEAddress & address);
|
|
|
|
static size_t getWhiteListCount();
|
|
|
|
static NimBLEAddress getWhiteListAddress(size_t index);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
|
|
|
static NimBLEScan* getScan();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
static NimBLEServer* createServer();
|
2020-05-14 20:59:35 +02:00
|
|
|
static NimBLEServer* getServer();
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
2021-12-29 16:08:25 +01:00
|
|
|
#ifdef ESP_PLATFORM
|
2020-04-14 03:13:51 +02:00
|
|
|
static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
|
2020-05-14 06:03:56 +02:00
|
|
|
static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
|
2021-12-29 16:08:25 +01:00
|
|
|
static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false);
|
|
|
|
static void setScanDuplicateCacheSize(uint16_t cacheSize);
|
|
|
|
static void setScanFilterMode(uint8_t type);
|
|
|
|
#else
|
|
|
|
static void setPower(int dbm);
|
|
|
|
static int getPower();
|
|
|
|
#endif
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
static void setCustomGapHandler(gap_event_handler handler);
|
|
|
|
static void setSecurityAuth(bool bonding, bool mitm, bool sc);
|
|
|
|
static void setSecurityAuth(uint8_t auth_req);
|
|
|
|
static void setSecurityIOCap(uint8_t iocap);
|
|
|
|
static void setSecurityInitKey(uint8_t init_key);
|
|
|
|
static void setSecurityRespKey(uint8_t init_key);
|
|
|
|
static void setSecurityPasskey(uint32_t pin);
|
|
|
|
static uint32_t getSecurityPasskey();
|
2020-06-23 05:46:22 +02:00
|
|
|
static int startSecurity(uint16_t conn_id);
|
2024-06-07 03:36:14 +02:00
|
|
|
static bool injectConfirmPIN(const NimBLEConnInfo& peerInfo, bool accept);
|
|
|
|
static bool injectPassKey(const NimBLEConnInfo& peerInfo, uint32_t pin);
|
2020-03-30 01:44:20 +02:00
|
|
|
static int setMTU(uint16_t mtu);
|
|
|
|
static uint16_t getMTU();
|
2020-05-10 15:21:46 +02:00
|
|
|
static bool isIgnored(const NimBLEAddress &address);
|
|
|
|
static void addIgnored(const NimBLEAddress &address);
|
|
|
|
static void removeIgnored(const NimBLEAddress &address);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
static NimBLEExtAdvertising* getAdvertising();
|
|
|
|
static bool startAdvertising(uint8_t inst_id,
|
|
|
|
int duration = 0,
|
|
|
|
int max_events = 0);
|
|
|
|
static bool stopAdvertising(uint8_t inst_id);
|
|
|
|
static bool stopAdvertising();
|
2022-08-27 20:38:53 +02:00
|
|
|
# endif
|
|
|
|
# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
|
2022-04-10 18:21:45 +02:00
|
|
|
static NimBLEAdvertising* getAdvertising();
|
2022-08-27 03:32:01 +02:00
|
|
|
static bool startAdvertising(uint32_t duration = 0);
|
2022-04-10 18:21:45 +02:00
|
|
|
static bool stopAdvertising();
|
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-07-30 18:04:34 +02:00
|
|
|
static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
|
2020-05-14 06:03:56 +02:00
|
|
|
static bool deleteClient(NimBLEClient* pClient);
|
2020-03-30 01:44:20 +02:00
|
|
|
static NimBLEClient* getClientByID(uint16_t conn_id);
|
2020-05-10 15:21:46 +02:00
|
|
|
static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
|
2020-03-30 01:44:20 +02:00
|
|
|
static NimBLEClient* getDisconnectedClient();
|
2020-05-14 06:03:56 +02:00
|
|
|
static size_t getClientListSize();
|
|
|
|
static std::list<NimBLEClient*>* getClientList();
|
|
|
|
#endif
|
|
|
|
|
2021-04-25 16:06:38 +02:00
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
static bool deleteBond(const NimBLEAddress &address);
|
|
|
|
static int getNumBonds();
|
|
|
|
static bool isBonded(const NimBLEAddress &address);
|
2024-06-05 00:52:55 +02:00
|
|
|
static bool deleteAllBonds();
|
2021-04-25 16:06:38 +02:00
|
|
|
static NimBLEAddress getBondedAddress(int index);
|
|
|
|
#endif
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
private:
|
2020-05-14 06:03:56 +02:00
|
|
|
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
friend class NimBLEClient;
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-03-30 01:44:20 +02:00
|
|
|
friend class NimBLEScan;
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
friend class NimBLEServer;
|
2020-04-14 03:13:51 +02:00
|
|
|
friend class NimBLECharacteristic;
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
|
|
|
friend class NimBLEAdvertising;
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
friend class NimBLEExtAdvertising;
|
|
|
|
friend class NimBLEExtAdvertisement;
|
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
static void onReset(int reason);
|
|
|
|
static void onSync(void);
|
|
|
|
static void host_task(void *param);
|
2020-05-14 06:03:56 +02:00
|
|
|
static bool m_synced;
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-03-30 01:44:20 +02:00
|
|
|
static NimBLEScan* m_pScan;
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
static NimBLEServer* m_pServer;
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
static NimBLEExtAdvertising* m_bleAdvertising;
|
|
|
|
# else
|
2020-03-30 01:44:20 +02:00
|
|
|
static NimBLEAdvertising* m_bleAdvertising;
|
2022-04-10 18:21:45 +02:00
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
static std::list <NimBLEClient*> m_cList;
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
static std::list <NimBLEAddress> m_ignoreList;
|
2020-05-14 06:03:56 +02:00
|
|
|
static uint32_t m_passkey;
|
|
|
|
static ble_gap_event_listener m_listener;
|
2020-03-30 01:44:20 +02:00
|
|
|
static gap_event_handler m_customGapHandler;
|
2021-01-14 02:00:48 +01:00
|
|
|
static uint8_t m_own_addr_type;
|
2021-12-29 16:08:25 +01:00
|
|
|
#ifdef ESP_PLATFORM
|
2023-05-29 17:08:14 +02:00
|
|
|
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
|
2021-02-01 05:15:31 +01:00
|
|
|
static uint16_t m_scanDuplicateSize;
|
|
|
|
static uint8_t m_scanFilterMode;
|
2023-05-29 17:08:14 +02:00
|
|
|
# endif
|
2021-12-29 16:08:25 +01:00
|
|
|
#endif
|
2021-05-23 21:12:44 +02:00
|
|
|
static std::vector<NimBLEAddress> m_whiteList;
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // CONFIG_BT_ENABLED
|
|
|
|
#endif // MAIN_NIMBLEDEVICE_H_
|