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
|
|
|
|
*/
|
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
#ifndef NIMBLE_CPP_DEVICE_H_
|
|
|
|
#define NIMBLE_CPP_DEVICE_H_
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
2024-09-29 23:59:42 +02:00
|
|
|
# ifdef ESP_PLATFORM
|
2024-12-03 16:47:41 +01:00
|
|
|
# ifndef CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
# include <esp_bt.h>
|
|
|
|
# endif
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_NIMBLE_CPP_IDF)
|
|
|
|
# include <host/ble_gap.h>
|
|
|
|
# else
|
|
|
|
# include <nimble/nimble/host/include/host/ble_gap.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/**** FIX COMPILATION ****/
|
|
|
|
# undef min
|
|
|
|
# undef max
|
|
|
|
/**************************/
|
|
|
|
|
|
|
|
# include <string>
|
|
|
|
# include <vector>
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
|
|
|
# include <array>
|
|
|
|
class NimBLEClient;
|
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
|
|
|
class NimBLEScan;
|
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
2024-09-29 23:59:42 +02:00
|
|
|
class NimBLEExtAdvertising;
|
2022-04-10 18:21:45 +02:00
|
|
|
# else
|
2024-09-29 23:59:42 +02:00
|
|
|
class NimBLEAdvertising;
|
2022-04-10 18:21:45 +02:00
|
|
|
# endif
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
class NimBLEServer;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
|
|
|
class NimBLEConnInfo;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
class NimBLEAddress;
|
|
|
|
|
|
|
|
# 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 BLEAddress NimBLEAddress
|
|
|
|
# define BLEUtils NimBLEUtils
|
|
|
|
# define BLEClientCallbacks NimBLEClientCallbacks
|
|
|
|
# define BLEAdvertisedDeviceCallbacks NimBLEScanCallbacks
|
|
|
|
# define BLEScanResults NimBLEScanResults
|
|
|
|
# define BLEServer NimBLEServer
|
|
|
|
# define BLEService NimBLEService
|
|
|
|
# define BLECharacteristic NimBLECharacteristic
|
|
|
|
# define BLEAdvertising NimBLEAdvertising
|
|
|
|
# define BLEServerCallbacks NimBLEServerCallbacks
|
|
|
|
# define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
|
|
|
|
# define BLEAdvertisementData NimBLEAdvertisementData
|
|
|
|
# define BLEDescriptor NimBLEDescriptor
|
|
|
|
# define BLE2904 NimBLE2904
|
|
|
|
# define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
|
|
|
|
# define BLEBeacon NimBLEBeacon
|
|
|
|
# define BLEEddystoneTLM NimBLEEddystoneTLM
|
|
|
|
# define BLEEddystoneURL NimBLEEddystoneURL
|
|
|
|
# define BLEConnInfo NimBLEConnInfo
|
|
|
|
|
|
|
|
# ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
|
|
|
|
# define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
|
|
|
|
# else
|
|
|
|
# define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
|
|
|
|
# endif
|
|
|
|
|
|
|
|
typedef int (*gap_event_handler)(ble_gap_event* event, void* arg);
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
2024-09-29 23:59:42 +02:00
|
|
|
* @brief A model of a BLE Device from which all the BLE roles are created.
|
2020-07-09 03:27:26 +02:00
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
class NimBLEDevice {
|
2024-09-29 23:59:42 +02:00
|
|
|
public:
|
|
|
|
static bool init(const std::string& deviceName);
|
|
|
|
static bool deinit(bool clearAll = false);
|
|
|
|
static bool setDeviceName(const std::string& deviceName);
|
|
|
|
static bool isInitialized();
|
|
|
|
static NimBLEAddress getAddress();
|
|
|
|
static std::string toString();
|
|
|
|
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);
|
|
|
|
static bool setOwnAddrType(uint8_t type);
|
2024-11-03 00:56:23 +01:00
|
|
|
static bool setOwnAddr(const NimBLEAddress& addr);
|
|
|
|
static bool setOwnAddr(const uint8_t* addr);
|
2024-09-29 23:59:42 +02:00
|
|
|
static void setScanDuplicateCacheSize(uint16_t cacheSize);
|
|
|
|
static void setScanFilterMode(uint8_t type);
|
|
|
|
static bool setCustomGapHandler(gap_event_handler handler);
|
|
|
|
static void setSecurityAuth(bool bonding, bool mitm, bool sc);
|
|
|
|
static void setSecurityAuth(uint8_t auth);
|
|
|
|
static void setSecurityIOCap(uint8_t iocap);
|
|
|
|
static void setSecurityInitKey(uint8_t initKey);
|
|
|
|
static void setSecurityRespKey(uint8_t respKey);
|
|
|
|
static void setSecurityPasskey(uint32_t passKey);
|
|
|
|
static uint32_t getSecurityPasskey();
|
2024-11-18 20:02:18 +01:00
|
|
|
static bool startSecurity(uint16_t connHandle, int* rcPtr = nullptr);
|
2024-09-29 23:59:42 +02:00
|
|
|
static bool setMTU(uint16_t mtu);
|
|
|
|
static uint16_t getMTU();
|
|
|
|
static void onReset(int reason);
|
|
|
|
static void onSync(void);
|
|
|
|
static void host_task(void* param);
|
2024-12-02 23:19:44 +01:00
|
|
|
static int getPower();
|
|
|
|
static bool setPower(int8_t dbm);
|
|
|
|
|
2024-12-03 16:47:41 +01:00
|
|
|
# ifdef ESP_PLATFORM
|
|
|
|
# ifndef CONFIG_IDF_TARGET_ESP32P4
|
2024-12-02 23:19:44 +01:00
|
|
|
static esp_power_level_t getPowerLevel(esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
|
|
|
|
static bool setPowerLevel(esp_power_level_t powerLevel, esp_ble_power_type_t powerType = ESP_BLE_PWR_TYPE_DEFAULT);
|
2024-12-03 16:47:41 +01:00
|
|
|
# endif
|
2024-12-02 23:19:44 +01:00
|
|
|
# endif
|
2024-09-29 23:59:42 +02:00
|
|
|
|
2024-11-28 20:50:24 +01:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask);
|
|
|
|
# endif
|
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
|
|
|
static NimBLEScan* getScan();
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
static NimBLEServer* createServer();
|
|
|
|
static NimBLEServer* getServer();
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
|
|
|
static bool injectConfirmPasskey(const NimBLEConnInfo& peerInfo, bool accept);
|
|
|
|
static bool injectPassKey(const NimBLEConnInfo& peerInfo, uint32_t pin);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
static NimBLEExtAdvertising* getAdvertising();
|
2024-09-29 23:59:42 +02:00
|
|
|
static bool startAdvertising(uint8_t instId, int duration = 0, int maxEvents = 0);
|
|
|
|
static bool stopAdvertising(uint8_t instId);
|
2022-04-10 18:21:45 +02:00
|
|
|
static bool stopAdvertising();
|
2022-08-27 20:38:53 +02:00
|
|
|
# endif
|
|
|
|
# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
|
2024-09-29 23:59:42 +02:00
|
|
|
static NimBLEAdvertising* getAdvertising();
|
|
|
|
static bool startAdvertising(uint32_t duration = 0);
|
|
|
|
static bool stopAdvertising();
|
2022-04-10 18:21:45 +02:00
|
|
|
# endif
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2024-10-29 20:50:12 +01:00
|
|
|
static NimBLEClient* createClient();
|
|
|
|
static NimBLEClient* createClient(const NimBLEAddress& peerAddress);
|
|
|
|
static bool deleteClient(NimBLEClient* pClient);
|
|
|
|
static NimBLEClient* getClientByHandle(uint16_t connHandle);
|
|
|
|
static NimBLEClient* getClientByPeerAddress(const NimBLEAddress& peerAddress);
|
|
|
|
static NimBLEClient* getDisconnectedClient();
|
|
|
|
static size_t getCreatedClientCount();
|
|
|
|
static std::vector<NimBLEClient*> getConnectedClients();
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# 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);
|
|
|
|
static bool deleteAllBonds();
|
|
|
|
static NimBLEAddress getBondedAddress(int index);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool m_synced;
|
|
|
|
static bool m_initialized;
|
|
|
|
static uint32_t m_passkey;
|
|
|
|
static ble_gap_event_listener m_listener;
|
|
|
|
static uint8_t m_ownAddrType;
|
|
|
|
static std::vector<NimBLEAddress> m_whiteList;
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
|
|
|
static NimBLEScan* m_pScan;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
static NimBLEServer* m_pServer;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
static NimBLEExtAdvertising* m_bleAdvertising;
|
|
|
|
# else
|
|
|
|
static NimBLEAdvertising* m_bleAdvertising;
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
|
|
|
static std::array<NimBLEClient*, NIMBLE_MAX_CONNECTIONS> m_pClients;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef ESP_PLATFORM
|
|
|
|
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
|
|
|
|
static uint16_t m_scanDuplicateSize;
|
|
|
|
static uint8_t m_scanFilterMode;
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
friend class NimBLEClient;
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-03-30 01:44:20 +02:00
|
|
|
friend class NimBLEScan;
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-05-14 06:03:56 +02:00
|
|
|
friend class NimBLEServer;
|
2020-04-14 03:13:51 +02:00
|
|
|
friend class NimBLECharacteristic;
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
2020-05-14 06:03:56 +02:00
|
|
|
friend class NimBLEAdvertising;
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
friend class NimBLEExtAdvertising;
|
|
|
|
friend class NimBLEExtAdvertisement;
|
|
|
|
# endif
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
|
|
|
};
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-09-29 23:59:42 +02:00
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
|
|
|
# include "NimBLEClient.h"
|
|
|
|
# include "NimBLERemoteService.h"
|
|
|
|
# include "NimBLERemoteCharacteristic.h"
|
|
|
|
# include "NimBLERemoteDescriptor.h"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
|
|
|
# include "NimBLEScan.h"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
# include "NimBLEServer.h"
|
|
|
|
# include "NimBLEService.h"
|
|
|
|
# include "NimBLECharacteristic.h"
|
|
|
|
# include "NimBLEDescriptor.h"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
2022-04-10 18:21:45 +02:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
2024-09-29 23:59:42 +02:00
|
|
|
# include "NimBLEExtAdvertising.h"
|
2022-04-10 18:21:45 +02:00
|
|
|
# else
|
2024-09-29 23:59:42 +02:00
|
|
|
# include "NimBLEAdvertising.h"
|
2023-05-29 17:08:14 +02:00
|
|
|
# endif
|
2024-09-29 23:59:42 +02:00
|
|
|
# endif
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
#endif // CONFIG_BT_ENABLED
|
2024-09-29 23:59:42 +02:00
|
|
|
#endif // NIMBLE_CPP_DEVICE_H_
|