14#include <esp_bt_defs.h>
15#include <esp_gap_ble_api.h>
16#include <esp_gattc_api.h>
18#include <freertos/FreeRTOS.h>
19#include <freertos/semphr.h>
28#ifdef USE_OTA_STATE_LISTENER
34using namespace esp32_ble;
38#ifdef USE_ESP32_BLE_UUID
42#ifdef USE_ESP32_BLE_DEVICE
57#ifndef USE_ESP32_BLE_DEVICE
112 esp_ble_gattc_cb_param_t *param) = 0;
113 virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
123 if (st == ClientState::IDLE) {
127 ClientState
state()
const {
return this->state_; }
144 if (this->tracker_state_version_ !=
nullptr) {
145 (*this->tracker_state_version_)++;
156 ClientState state_{ClientState::INIT};
160 uint8_t *tracker_state_version_{
nullptr};
164#ifdef USE_OTA_STATE_LISTENER
172#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
180 void setup()
override;
184 void loop()
override;
195#ifdef USE_BLE_SCANNER_STATE_CALLBACK
203 return {
true,
true,
true,
206 void get_adapter_mac(uint8_t out[MAC_ADDRESS_SIZE]) { this->parent_->get_mac_msb_first(out); }
213#ifdef USE_ESP32_BLE_DEVICE
220 void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
221 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
225#ifdef USE_OTA_STATE_LISTENER
260#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
267#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
268 for (
auto *client : this->
clients_) {
269 switch (client->state()) {
270 case ClientState::DISCONNECTING:
273 case ClientState::DISCOVERED:
276 case ClientState::CONNECTING:
279 case ClientState::CONNECTED:
280 case ClientState::ESTABLISHED:
292#ifdef ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT
295#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
300#ifdef ESPHOME_BLE_DEVICE_BASE_LISTENER_COUNT
304#ifdef USE_BLE_SCANNER_STATE_CALLBACK
307#ifdef USE_ESP32_BLE_DEVICE
322#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
351#ifdef USE_OTA_STATE_LISTENER
356#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
360#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
Helper class to easily give an object a parent of type T.
Minimal static vector - saves memory by avoiding std::vector overhead.
Per-scan-period "Found device" DEBUG logger, deduplicated by MAC address.
void try_promote_discovered_clients_()
Try to promote discovered clients to ready to connect.
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
void set_scan_active(bool scan_active)
bool get_scan_active() const
uint8_t state_version_
Version counter for loop() fast-path optimization.
StaticVector< ESPBTClient *, ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT > clients_
void set_raw_advertisement_callback(ble_device_base::RawAdvertisementCallback callback)
void notify_scan_end_()
Fire on_scan_end on every listener unless a window-change restart suppressed it.
uint32_t scan_start_time_
uint8_t scan_start_fail_count_
void gap_scan_stop_complete_(const esp_ble_gap_cb_param_t::ble_scan_stop_cmpl_evt_param ¶m)
Called when a ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT event is received.
ClientStateCounts count_client_states_() const
Count clients in each state.
ClientStateCounts client_state_counts_
ble_device_base::ScannerStateCallback scanner_state_callback_
uint8_t last_processed_version_
Last state_version_ value when loop() did full processing.
void gap_scan_event_handler(const BLEScanResult &scan_result)
float get_setup_priority() const override
void register_client(ESPBTClient *client)
bool parse_advertisements_
bool skip_next_scan_end_
Suppress the window-change restart's on_scan_end sweeps (stop and start).
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
void dump_config() override
ble_device_base::RawAdvertisementCallback raw_advertisement_callback_
bool request_scan_mode(bool active)
ScanTimeoutState scan_timeout_state_
ScannerState get_scanner_state() const
uint32_t desired_scan_window_(uint8_t active) const
The window to scan at for the given number of active GATT connections.
ScannerState scanner_state_
esp_ble_scan_params_t scan_params_
A structure holding the ESP BLE scan parameters.
StaticVector< ESPBTDeviceListener *, ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT > listeners_
void register_listener(ESPBTDeviceListener *listener)
uint32_t scan_timeout_ms_
Precomputed timeout value: scan_duration_ * 2000.
static constexpr ble_device_base::HubCapabilities get_capabilities()
void update_coex_preference_(bool force_ble)
Update BLE coexistence preference.
const char * scanner_state_to_string_(ScannerState state) const
Convert scanner state enum to string for logging.
void gap_scan_set_param_complete_(const esp_ble_gap_cb_param_t::ble_scan_param_cmpl_evt_param ¶m)
Called when a ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT event is received.
bool scan_continuous_before_ota_
uint32_t scan_duration_
The interval in seconds to perform scans.
uint32_t connection_scan_window_
Window used while a GATT connection is active; set by the user, or defaulted when the window was rais...
void set_connection_scan_window(uint32_t scan_window)
void setup() override
Setup the FreeRTOS task and the Bluetooth stack.
void ble_before_disabled_event_handler()
esp_bt_status_t scan_start_failed_
void handle_scanner_failure_()
Handle scanner failure states.
esp_bt_status_t scan_set_param_failed_
void cleanup_scan_state_(bool is_stop_complete)
Common cleanup logic when transitioning scanner to IDLE state.
ble_device_base::DiscoveredDeviceLog discovered_log_
Per-period "Found device" DEBUG log with MAC dedup (shared ble_device_base impl)
void set_scanner_state_(ScannerState state)
Called to set the scanner state. Will also call callbacks to let listeners know when state is changed...
void set_scan_continuous(bool scan_continuous)
void print_bt_device_info(const ESPBTDevice &device)
void set_scan_duration(uint32_t scan_duration)
void set_scan_interval(uint32_t scan_interval)
StaticVector< ble_device_base::ESPBTDeviceListener *, ESPHOME_BLE_DEVICE_BASE_LISTENER_COUNT > neutral_listeners_
void process_scan_result_(const BLEScanResult &scan_result)
Process a single scan result immediately.
void gap_scan_start_complete_(const esp_ble_gap_cb_param_t::ble_scan_start_cmpl_evt_param ¶m)
Called when a ESP_GAP_BLE_SCAN_START_COMPLETE_EVT event is received.
void log_unexpected_state_(const char *operation, ScannerState expected_state) const
Log an unexpected scanner state.
bool stop_scan_()
Returns true when a stop was issued to the controller.
void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override
void gap_scan_result_(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param ¶m)
Called when a ESP_GAP_BLE_SCAN_RESULT_EVT event is received.
void set_scan_window(uint32_t scan_window)
void get_adapter_mac(uint8_t out[MAC_ADDRESS_SIZE])
void start_scan_(bool first)
Start a single scan by setting up the parameters and doing some esp-idf calls.
void set_scanner_state_callback(ble_device_base::ScannerStateCallback callback)
Base class for BLE GATT clients that connect to remote devices.
void cancel_pending_disconnect()
ClientState state() const
void set_tracker_state_version(uint8_t *version)
Called by ESP32BLETracker::register_client() to enable state change notifications.
virtual void disconnect()=0
virtual void set_state(ClientState st)
Set the client state with IDLE handling (clears want_disconnect_).
bool disconnect_pending() const
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)=0
virtual bool wants_parsed_advertisements()
False keeps the tracker from building parsed ESPBTDevice objects on this client's account (raw consum...
virtual bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)=0
void set_state_internal_(ClientState st)
Set state without IDLE handling - use for direct state transitions.
void set_parent(ESP32BLETracker *parent)
bool parse_device(const ble_device_base::ESPBTDevice &device) override
ESP32BLETracker * parent_
Listener interface for global OTA state changes (includes OTA component pointer).
const char * client_state_to_string(ClientState state)
std::vector< uint8_t > adv_data_t
ScannerState
Scanner lifecycle, wire-value aligned with the api enum so consumers cast directly (pinned by static_...
ble_device_base::adv_data_t adv_data_t
ESP32BLETracker * global_esp32_ble_tracker
What a tracker's controller/SDK can do — consumers branch on data, not #ifdefs.
Subscriber slot for the raw-advertisement stream (the bluetooth_proxy path).
Subscriber slot for scanner-state transitions; same shape as RawAdvertisementCallback,...
bool operator==(const ClientStateCounts &other) const
bool operator!=(const ClientStateCounts &other) const