45#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S3)
47#define HAS_CLASSIC_BLUETOOTH 1
48#define HAS_BLE_SUPPORT 1
49#define HAS_A2DP_SUPPORT 1
50#define HAS_AVRCP_SUPPORT 1
51#define HAS_SPP_SUPPORT 1
52#if defined(CONFIG_BT_NIMBLE_ENABLED)
53#define HAS_NIMBLE_SUPPORT 1
54#define HAS_BLUEDROID_SUPPORT 0
56#define HAS_NIMBLE_SUPPORT 0
57#define HAS_BLUEDROID_SUPPORT 1
59#elif defined(CONFIG_IDF_TARGET_ESP32C6)
61#define HAS_CLASSIC_BLUETOOTH 0
62#define HAS_BLE_SUPPORT 1
63#define HAS_A2DP_SUPPORT 0
64#define HAS_AVRCP_SUPPORT 0
65#define HAS_SPP_SUPPORT 0
66#if defined(CONFIG_BT_NIMBLE_ENABLED)
67#define HAS_NIMBLE_SUPPORT 1
68#define HAS_BLUEDROID_SUPPORT 0
70#define HAS_NIMBLE_SUPPORT 0
71#define HAS_BLUEDROID_SUPPORT 1
73#elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32H2)
75#define HAS_CLASSIC_BLUETOOTH 0
76#define HAS_BLE_SUPPORT 1
77#define HAS_A2DP_SUPPORT 0
78#define HAS_AVRCP_SUPPORT 0
79#define HAS_SPP_SUPPORT 0
80#if defined(CONFIG_BT_NIMBLE_ENABLED)
81#define HAS_NIMBLE_SUPPORT 1
82#define HAS_BLUEDROID_SUPPORT 0
84#define HAS_NIMBLE_SUPPORT 0
85#define HAS_BLUEDROID_SUPPORT 1
87#elif defined(CONFIG_IDF_TARGET_ESP32S2)
89#define HAS_CLASSIC_BLUETOOTH 0
90#define HAS_BLE_SUPPORT 0
91#define HAS_A2DP_SUPPORT 0
92#define HAS_AVRCP_SUPPORT 0
93#define HAS_SPP_SUPPORT 0
94#define HAS_NIMBLE_SUPPORT 0
95#define HAS_BLUEDROID_SUPPORT 0
98#define HAS_CLASSIC_BLUETOOTH 0
99#define HAS_BLE_SUPPORT 1
100#define HAS_A2DP_SUPPORT 0
101#define HAS_AVRCP_SUPPORT 0
102#define HAS_SPP_SUPPORT 0
103#define HAS_NIMBLE_SUPPORT 0
104#define HAS_BLUEDROID_SUPPORT 1
110#if HAS_NIMBLE_SUPPORT && defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ENABLED)
112#define NIMBLE_HEADERS_AVAILABLE 1
114#include "host/ble_att.h"
115#include "host/ble_gap.h"
116#include "host/ble_gatt.h"
117#include "host/ble_hs.h"
118#include "host/ble_uuid.h"
119#include "host/util/util.h"
120#include "nimble/nimble_port.h"
121#include "nimble/nimble_port_freertos.h"
122#include "services/gap/ble_svc_gap.h"
123#include "services/gatt/ble_svc_gatt.h"
126#define NIMBLE_HEADERS_AVAILABLE 0
129#if HAS_BLUEDROID_SUPPORT && defined(CONFIG_BT_ENABLED)
133#include "esp_bt_defs.h"
134#include "esp_bt_main.h"
135#include "esp_gap_ble_api.h"
136#include "esp_gatt_common_api.h"
137#include "esp_gattc_api.h"
138#include "esp_gatts_api.h"
140#define BLUEDROID_HEADERS_AVAILABLE 1
142#define BLUEDROID_HEADERS_AVAILABLE 0
145#if HAS_CLASSIC_BLUETOOTH && defined(CONFIG_BT_ENABLED)
148#include "esp_gap_bt_api.h"
149#include "esp_hf_client_api.h"
150#include "esp_spp_api.h"
152#include "esp_a2dp_api.h"
153#include "esp_avrc_api.h"
200#if HAS_NIMBLE_SUPPORT && NIMBLE_HEADERS_AVAILABLE
203 uint16_t m_conn_handle;
207 static int GapEventHandler(
struct ble_gap_event* event,
void* arg);
208 static int GattSvrCharAccess(uint16_t conn_handle, uint16_t attr_handle,
209 struct ble_gatt_access_ctxt* ctxt,
void* arg);
222#if HAS_BLUEDROID_SUPPORT && defined(CONFIG_BT_ENABLED)
228 static void GapEventHandler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t* param);
229 static void GattcEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
230 esp_ble_gattc_cb_param_t* param);
231 static void GattsEventHandler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
232 esp_ble_gatts_cb_param_t* param);
235#if HAS_CLASSIC_BLUETOOTH && defined(CONFIG_BT_ENABLED)
241 static void ClassicGapEventHandler(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t* param);
242 static void SppEventHandler(esp_spp_cb_event_t event, esp_spp_cb_param_t* param);
301 bool IsEnabled() const noexcept override;
347 uint32_t duration_ms = 0,
406 uint32_t timeout_ms = 0) noexcept override;
438 const std::
string& pin = "") noexcept override;
463 const std::vector<uint8_t>& data) noexcept override;
480 size_t max_bytes = 0) noexcept override;
513 const std::
string& service_uuid,
514 const std::
string& characteristic_uuid,
515 std::vector<uint8_t>& value) noexcept override;
527 const std::
string& service_uuid,
528 const std::
string& characteristic_uuid,
529 const std::vector<uint8_t>& value,
530 bool with_response = true) noexcept override;
541 const std::
string& service_uuid,
542 const std::
string& characteristic_uuid,
543 bool enable) noexcept override;
659 bool IsInitialized() const noexcept
override {
Unified Bluetooth base class for all Bluetooth implementations.
hf_bluetooth_err_t
Definition BaseBluetooth.h:104
@ BLUETOOTH_ERR_NOT_SUPPORTED
Platform-agnostic hardware type definitions for the HardFOC system.
Abstract base class for Bluetooth functionality.
Definition BaseBluetooth.h:359
ESP32 Bluetooth implementation class.
Definition EspBluetooth.h:176
hf_bluetooth_err_t StartAdvertising() noexcept override
Start BLE advertising.
Definition EspBluetooth.cpp:814
hf_bluetooth_err_t StopAdvertising() noexcept override
Stop BLE advertising.
Definition EspBluetooth.cpp:819
hf_bluetooth_err_t RegisterEventCallback(hf_bluetooth_event_callback_t callback) noexcept override
Register event callback function.
Definition EspBluetooth.cpp:931
hf_bluetooth_err_t GetConnectedDevices(std::vector< hf_bluetooth_device_info_t > &devices) noexcept override
Get list of connected devices.
Definition EspBluetooth.cpp:843
hf_bluetooth_data_callback_t m_data_callback
Definition EspBluetooth.h:197
hf_bluetooth_err_t GetPairedDevices(std::vector< hf_bluetooth_device_info_t > &devices) noexcept override
Get list of paired devices.
Definition EspBluetooth.cpp:976
hf_bluetooth_err_t DiscoverServices(const hf_bluetooth_address_t &address, std::vector< hf_bluetooth_gatt_service_t > &services) noexcept override
Discover GATT services on a connected device.
Definition EspBluetooth.cpp:884
hf_bluetooth_err_t Pair(const hf_bluetooth_address_t &address, const std::string &pin="") noexcept override
Pair with a remote device.
Definition EspBluetooth.cpp:853
uint32_t GetSupportedFeatures() const
Get supported features for current target.
Definition EspBluetooth.cpp:1007
hf_bluetooth_err_t ConfigureBle(const hf_bluetooth_ble_config_t &config) noexcept override
Configure Bluetooth Low Energy parameters.
Definition EspBluetooth.cpp:971
hf_bluetooth_err_t ReadData(const hf_bluetooth_address_t &address, std::vector< uint8_t > &data, size_t max_bytes=0) noexcept override
Read available data from a connected device.
Definition EspBluetooth.cpp:878
hf_bluetooth_err_t GetLocalAddress(hf_bluetooth_address_t &address) const noexcept override
Get local Bluetooth address.
Definition EspBluetooth.cpp:596
hf_bluetooth_err_t SubscribeCharacteristic(const hf_bluetooth_address_t &address, const std::string &service_uuid, const std::string &characteristic_uuid, bool enable) noexcept override
Subscribe to GATT characteristic notifications.
Definition EspBluetooth.cpp:914
hf_bluetooth_err_t Disconnect(const hf_bluetooth_address_t &address) noexcept override
Disconnect from a remote device.
Definition EspBluetooth.cpp:834
hf_bluetooth_err_t UnregisterEventCallback() noexcept override
Unregister event callback function.
Definition EspBluetooth.cpp:937
bool IsInitialized() const noexcept override
Check if Bluetooth is initialized.
Definition EspBluetooth.cpp:185
bool IsDiscoverable() const noexcept override
Check if device is discoverable.
Definition EspBluetooth.cpp:967
hf_bluetooth_err_t Deinitialize() noexcept override
Deinitialize the Bluetooth subsystem.
Definition EspBluetooth.cpp:152
hf_bluetooth_err_t ClearDiscoveredDevices() noexcept override
Clear discovered devices list.
Definition EspBluetooth.cpp:805
hf_bluetooth_err_t StopScan() noexcept override
Stop device discovery/scanning.
Definition EspBluetooth.cpp:770
hf_bluetooth_err_t Disable() noexcept override
Disable Bluetooth.
Definition EspBluetooth.cpp:249
hf_bluetooth_err_t ReadCharacteristic(const hf_bluetooth_address_t &address, const std::string &service_uuid, const std::string &characteristic_uuid, std::vector< uint8_t > &value) noexcept override
Read GATT characteristic value.
Definition EspBluetooth.cpp:897
hf_bluetooth_err_t SetDeviceName(const std::string &name) noexcept override
Set local device name.
Definition EspBluetooth.cpp:628
bool m_initialized
Definition EspBluetooth.h:179
hf_bluetooth_err_t SetMode(hf_bluetooth_mode_t mode) noexcept override
Set Bluetooth operating mode.
Definition EspBluetooth.cpp:293
hf_bluetooth_mode_t m_mode
Definition EspBluetooth.h:181
hf_bluetooth_err_t SendData(const hf_bluetooth_address_t &address, const std::vector< uint8_t > &data) noexcept override
Send data to a connected device.
Definition EspBluetooth.cpp:868
hf_bluetooth_err_t StartScan(uint32_t duration_ms=0, hf_bluetooth_scan_type_t type=hf_bluetooth_scan_type_t::HF_BLUETOOTH_SCAN_TYPE_ACTIVE) noexcept override
Start device discovery/scanning.
Definition EspBluetooth.cpp:681
std::string GetDeviceName() const noexcept override
Get local device name.
Definition EspBluetooth.cpp:656
hf_bluetooth_mode_t GetMode() const noexcept override
Get current Bluetooth operating mode.
Definition EspBluetooth.cpp:311
void * m_callback_context
Definition EspBluetooth.h:198
hf_bluetooth_ble_config_t m_ble_config
Definition EspBluetooth.h:189
PlatformMutex m_state_mutex
Definition EspBluetooth.h:193
bool m_enabled
Definition EspBluetooth.h:180
hf_bluetooth_err_t DiscoverCharacteristics(const hf_bluetooth_address_t &address, const std::string &service_uuid, std::vector< hf_bluetooth_gatt_characteristic_t > &characteristics) noexcept override
Discover GATT characteristics for a service.
Definition EspBluetooth.cpp:890
hf_bluetooth_err_t GetDiscoveredDevices(std::vector< hf_bluetooth_device_info_t > &devices) noexcept override
Get list of discovered devices.
Definition EspBluetooth.cpp:798
bool IsEnabled() const noexcept override
Check if Bluetooth is enabled.
Definition EspBluetooth.cpp:288
hf_bluetooth_err_t UnregisterDataCallback() noexcept override
Unregister data callback function.
Definition EspBluetooth.cpp:949
hf_bluetooth_err_t Connect(const hf_bluetooth_address_t &address, uint32_t timeout_ms=0) noexcept override
Connect to a remote device.
Definition EspBluetooth.cpp:828
hf_bluetooth_err_t Enable() noexcept override
Enable Bluetooth.
Definition EspBluetooth.cpp:200
EspBluetooth()
Constructor.
Definition EspBluetooth.cpp:56
hf_bluetooth_err_t ValidateAddress(const hf_bluetooth_address_t &address) const
Definition EspBluetooth.cpp:1034
std::vector< hf_bluetooth_device_info_t > m_discovered_devices
Definition EspBluetooth.h:185
void TriggerEvent(hf_bluetooth_event_t event, const void *data=nullptr)
Definition EspBluetooth.cpp:1028
bool IsScanning() const noexcept override
Check if currently scanning.
Definition EspBluetooth.cpp:793
std::string AddressToString(const hf_bluetooth_address_t &address) const
Definition EspBluetooth.cpp:1051
hf_bluetooth_state_t GetState() const noexcept override
Get current Bluetooth state.
Definition EspBluetooth.cpp:922
hf_bluetooth_address_t StringToAddress(const std::string &address_str) const
Definition EspBluetooth.cpp:1062
bool IsPaired(const hf_bluetooth_address_t &address) const noexcept override
Check if paired with a device.
Definition EspBluetooth.cpp:864
bool IsAdvertising() const noexcept override
Check if currently advertising.
Definition EspBluetooth.cpp:824
hf_bluetooth_event_callback_t m_event_callback
Definition EspBluetooth.h:196
int8_t GetRssi(const hf_bluetooth_address_t &address) const noexcept override
Get signal strength for a connected device.
Definition EspBluetooth.cpp:927
hf_bluetooth_err_t SetDiscoverable(bool discoverable, uint32_t timeout_ms=0) noexcept override
Make device discoverable.
Definition EspBluetooth.cpp:962
int GetAvailableData(const hf_bluetooth_address_t &address) const noexcept override
Check if data is available to read.
Definition EspBluetooth.cpp:874
std::map< std::string, hf_bluetooth_device_info_t > m_connected_devices
Definition EspBluetooth.h:186
PlatformMutex m_device_mutex
Definition EspBluetooth.h:192
bool IsConnected(const hf_bluetooth_address_t &address) const noexcept override
Check if connected to a device.
Definition EspBluetooth.cpp:839
hf_bluetooth_err_t RegisterDataCallback(hf_bluetooth_data_callback_t callback) noexcept override
Register data callback function.
Definition EspBluetooth.cpp:943
hf_bluetooth_err_t Unpair(const hf_bluetooth_address_t &address) noexcept override
Unpair from a remote device.
Definition EspBluetooth.cpp:859
virtual ~EspBluetooth()
Destructor.
Definition EspBluetooth.cpp:82
std::string GetImplementationInfo() const
Get implementation-specific information.
Definition EspBluetooth.cpp:983
hf_bluetooth_state_t m_state
Definition EspBluetooth.h:182
hf_bluetooth_err_t WriteCharacteristic(const hf_bluetooth_address_t &address, const std::string &service_uuid, const std::string &characteristic_uuid, const std::vector< uint8_t > &value, bool with_response=true) noexcept override
Write GATT characteristic value.
Definition EspBluetooth.cpp:905
hf_bluetooth_err_t Initialize(hf_bluetooth_mode_t mode) noexcept override
Initialize the Bluetooth subsystem.
Definition EspBluetooth.cpp:96
hf_bluetooth_err_t ConfigureClassic(const hf_bluetooth_classic_config_t &config) noexcept override
Configure Bluetooth Classic parameters.
Definition EspBluetooth.cpp:957
std::function< void(const hf_bluetooth_address_t &address, const std::vector< uint8_t > &data)> hf_bluetooth_data_callback_t
Data received callback function type.
Definition BaseBluetooth.h:343
hf_bluetooth_mode_t
Bluetooth operating modes.
Definition BaseBluetooth.h:129
hf_bluetooth_state_t
Bluetooth connection states.
Definition BaseBluetooth.h:151
std::function< void(hf_bluetooth_event_t event, void *event_data)> hf_bluetooth_event_callback_t
Bluetooth event callback function type.
Definition BaseBluetooth.h:336
hf_bluetooth_event_t
Bluetooth event types for callback functions.
Definition BaseBluetooth.h:220
hf_bluetooth_scan_type_t
Bluetooth scan types.
Definition BaseBluetooth.h:179
@ HF_BLUETOOTH_SCAN_TYPE_ACTIVE
Bluetooth address structure (6 bytes)
Definition BaseBluetooth.h:245
Bluetooth Low Energy configuration structure.
Definition BaseBluetooth.h:299
Bluetooth Classic configuration structure.
Definition BaseBluetooth.h:287
Bluetooth device information structure.
Definition BaseBluetooth.h:272
GATT characteristic structure.
Definition BaseBluetooth.h:325
GATT service structure.
Definition BaseBluetooth.h:315