ESPHome 2026.8.1
Loading...
Searching...
No Matches
esp32_ble_tracker.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <array>
9#include <span>
10#include <string>
11
12#ifdef USE_ESP32
13
14#include <esp_bt_defs.h>
15#include <esp_gap_ble_api.h>
16#include <esp_gattc_api.h>
17
18#include <freertos/FreeRTOS.h>
19#include <freertos/semphr.h>
20
27
28#ifdef USE_OTA_STATE_LISTENER
30#endif
31
33
34using namespace esp32_ble;
35
37
38#ifdef USE_ESP32_BLE_UUID
40#endif
41
42#ifdef USE_ESP32_BLE_DEVICE
43// The advertisement device types are owned by the platform-neutral
44// ble_device_base layer; re-exported here (esp32 only) for backward
45// compatibility. ESPBTDevice::parse_scan_rst() (esp32-only) adapts BLEScanResult.
48#endif // USE_ESP32_BLE_DEVICE
49
50class ESP32BLETracker;
51
52// esp32-flavored listener: the neutral parse_device/on_scan_end come from
53// ble_device_base; this subclass adds the esp32-only raw-advertisement path
54// (BLEScanResult batches) and the tracker back-pointer.
56 public:
57#ifndef USE_ESP32_BLE_DEVICE
58 // Raw-only build: no parsed-device support is compiled in.
59 bool parse_device(const ble_device_base::ESPBTDevice &device) override { return false; }
60#endif
61 void set_parent(ESP32BLETracker *parent) { parent_ = parent; }
62
63 protected:
65};
66
68 uint8_t connecting = 0;
69 uint8_t discovered = 0;
70 uint8_t disconnecting = 0;
71 // CONNECTED + ESTABLISHED clients. Tracked so coex stays at PREFER_BT
72 // while active connections may still need to send/receive GATT traffic.
73 uint8_t active = 0;
74
75 bool operator==(const ClientStateCounts &other) const {
76 return connecting == other.connecting && discovered == other.discovered && disconnecting == other.disconnecting &&
77 active == other.active;
78 }
79
80 bool operator!=(const ClientStateCounts &other) const { return !(*this == other); }
81};
82
83// The client connection state types are owned by the platform-neutral
84// ble_device_base layer; re-exported here for backward compatibility.
85using ClientState = ble_device_base::ClientState;
86using ConnectionType = ble_device_base::ConnectionType;
88
89// Neutral scanner lifecycle re-exported for backward compatibility.
91
106 public:
109 virtual bool wants_parsed_advertisements() { return true; }
110
111 virtual bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
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;
114 virtual void connect() = 0;
115 virtual void disconnect() = 0;
116 bool disconnect_pending() const { return this->want_disconnect_; }
118
121 virtual void set_state(ClientState st) {
122 this->set_state_internal_(st);
123 if (st == ClientState::IDLE) {
124 this->want_disconnect_ = false;
125 }
126 }
127 ClientState state() const { return this->state_; }
128
132 void set_tracker_state_version(uint8_t *version) { this->tracker_state_version_ = version; }
133
134 // Memory optimized layout
135 uint8_t app_id; // App IDs are small integers assigned sequentially
136
137 protected:
141 void set_state_internal_(ClientState st) {
142 this->state_ = st;
143 // Notify tracker that state changed (tracker_state_version_ is owned by ESP32BLETracker)
144 if (this->tracker_state_version_ != nullptr) {
145 (*this->tracker_state_version_)++;
146 }
147 }
148
149 // want_disconnect_ is set to true when a disconnect is requested
150 // while the client is connecting. This is used to disconnect the
151 // client as soon as we get the connection id (conn_id_) from the
152 // ESP_GATTC_OPEN_EVT event.
153 bool want_disconnect_{false};
154
155 private:
156 ClientState state_{ClientState::INIT};
160 uint8_t *tracker_state_version_{nullptr};
161};
162
163class ESP32BLETracker final : public Component,
164#ifdef USE_OTA_STATE_LISTENER
166#endif
167 public Parented<ESP32BLE> {
168 public:
169 void set_scan_duration(uint32_t scan_duration) { scan_duration_ = scan_duration; }
170 void set_scan_interval(uint32_t scan_interval) { scan_interval_ = scan_interval; }
171 void set_scan_window(uint32_t scan_window) { scan_window_ = scan_window; }
172#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
174#endif
176 bool get_scan_active() const { return scan_active_; }
177 void set_scan_continuous(bool scan_continuous) { scan_continuous_ = scan_continuous; }
178
180 void setup() override;
181 void dump_config() override;
182 float get_setup_priority() const override;
183
184 void loop() override;
185
186 // esp32-flavored path (unmigrated esp32 sensors; sets the tracker back-pointer).
188 void register_client(ESPBTClient *client);
189
190 // ---- ble_device_base::BLEHub (the platform-neutral tracker contract) ----
195#ifdef USE_BLE_SCANNER_STATE_CALLBACK
199#endif
201 // scan_mode_switch is false: the mode is driven through this tracker's own
202 // API (set_scan_active + restart), not the neutral request_scan_mode().
203 return {/* active_scan = */ true, /* merges_scan_response = */ true, /* gatt = */ true,
204 /* scan_mode_switch = */ false};
205 }
206 void get_adapter_mac(uint8_t out[MAC_ADDRESS_SIZE]) { this->parent_->get_mac_msb_first(out); }
207 bool scan_running() { return this->scanner_state_ == ScannerState::RUNNING; }
208 bool scan_active() { return this->scan_active_; }
209 // The mode is driven through this tracker's own API (see get_capabilities);
210 // the neutral request refuses without changing any state.
211 bool request_scan_mode(bool active) { return false; }
212
213#ifdef USE_ESP32_BLE_DEVICE
214 void print_bt_device_info(const ESPBTDevice &device);
215#endif
216
217 void start_scan();
218 void stop_scan();
219
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);
222 void gap_scan_event_handler(const BLEScanResult &scan_result);
224
225#ifdef USE_OTA_STATE_LISTENER
226 void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override;
227#endif
228
230
231 protected:
233 bool stop_scan_();
235 void notify_scan_end_();
237 void start_scan_(bool first);
239 void gap_scan_result_(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &param);
241 void gap_scan_set_param_complete_(const esp_ble_gap_cb_param_t::ble_scan_param_cmpl_evt_param &param);
243 void gap_scan_start_complete_(const esp_ble_gap_cb_param_t::ble_scan_start_cmpl_evt_param &param);
245 void gap_scan_stop_complete_(const esp_ble_gap_cb_param_t::ble_scan_stop_cmpl_evt_param &param);
249 void cleanup_scan_state_(bool is_stop_complete);
251 void process_scan_result_(const BLEScanResult &scan_result);
259 void log_unexpected_state_(const char *operation, ScannerState expected_state) const;
260#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
262 void update_coex_preference_(bool force_ble);
263#endif
266 ClientStateCounts counts;
267#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
268 for (auto *client : this->clients_) {
269 switch (client->state()) {
270 case ClientState::DISCONNECTING:
271 counts.disconnecting++;
272 break;
273 case ClientState::DISCOVERED:
274 counts.discovered++;
275 break;
276 case ClientState::CONNECTING:
277 counts.connecting++;
278 break;
279 case ClientState::CONNECTED:
280 case ClientState::ESTABLISHED:
281 counts.active++;
282 break;
283 default:
284 break;
285 }
286 }
287#endif
288 return counts;
289 }
290
291 // Group 1: Large objects (12+ bytes) - vectors
292#ifdef ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT
294#endif
295#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
297#endif
298 // Parsed listeners registered through the neutral BLEHub contract (migrated
299 // sensors); dispatched alongside listeners_.
300#ifdef ESPHOME_BLE_DEVICE_BASE_LISTENER_COUNT
302#endif
304#ifdef USE_BLE_SCANNER_STATE_CALLBACK
306#endif
307#ifdef USE_ESP32_BLE_DEVICE
310#endif
311
312 // Group 2: Structs (aligned to 4 bytes)
314 esp_ble_scan_params_t scan_params_;
316
317 // Group 3: 4-byte types
322#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
327 uint32_t desired_scan_window_(uint8_t active) const {
328 return (this->connection_scan_window_ != 0 && active > 0) ? this->connection_scan_window_ : this->scan_window_;
329 }
330#endif
331 esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS};
332 esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS};
333
334 // Group 4: 1-byte types (enums, uint8_t, bool)
335 uint8_t app_id_{0};
343 uint8_t state_version_{0};
347 ScannerState scanner_state_{ScannerState::IDLE};
348 // Packed 1-bit flags.
350 bool scan_active_ : 1;
351#ifdef USE_OTA_STATE_LISTENER
353#endif
354 bool ble_was_disabled_ : 1 {true};
355 bool parse_advertisements_ : 1 {false};
356#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
358 bool skip_next_scan_end_ : 1 {false};
359#endif
360#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
361 bool coex_prefer_ble_ : 1 {false};
362#endif
363 // Scan timeout state machine
364 enum class ScanTimeoutState : uint8_t {
365 INACTIVE, // No timeout monitoring
366 MONITORING, // Actively monitoring for timeout
367 EXCEEDED_WAIT, // Timeout exceeded, waiting one loop before reboot
368 };
373};
374
375// NOLINTNEXTLINE
376extern ESP32BLETracker *global_esp32_ble_tracker;
377
378} // namespace esphome::esp32_ble_tracker
379
380#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:1907
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:227
Per-scan-period "Found device" DEBUG logger, deduplicated by MAC address.
Definition ble_device.h:269
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)
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.
void gap_scan_stop_complete_(const esp_ble_gap_cb_param_t::ble_scan_stop_cmpl_evt_param &param)
Called when a ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT event is received.
ClientStateCounts count_client_states_() const
Count clients in each state.
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)
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)
ble_device_base::RawAdvertisementCallback raw_advertisement_callback_
uint32_t desired_scan_window_(uint8_t active) const
The window to scan at for the given number of active GATT connections.
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 &param)
Called when a ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT event is received.
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 handle_scanner_failure_()
Handle scanner failure states.
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 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 &param)
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 &param)
Called when a ESP_GAP_BLE_SCAN_RESULT_EVT event is received.
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 set_tracker_state_version(uint8_t *version)
Called by ESP32BLETracker::register_client() to enable state change notifications.
virtual void set_state(ClientState st)
Set the client state with IDLE handling (clears want_disconnect_).
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.
bool parse_device(const ble_device_base::ESPBTDevice &device) override
Listener interface for global OTA state changes (includes OTA component pointer).
bool state
Definition fan.h:2
const char * client_state_to_string(ClientState state)
std::vector< uint8_t > adv_data_t
Definition ble_device.h:38
ScannerState
Scanner lifecycle, wire-value aligned with the api enum so consumers cast directly (pinned by static_...
Definition ble_hub.h:53
ble_device_base::adv_data_t adv_data_t
ESP32BLETracker * global_esp32_ble_tracker
static void uint32_t
What a tracker's controller/SDK can do — consumers branch on data, not #ifdefs.
Definition ble_hub.h:73
Subscriber slot for the raw-advertisement stream (the bluetooth_proxy path).
Definition ble_hub.h:43
Subscriber slot for scanner-state transitions; same shape as RawAdvertisementCallback,...
Definition ble_hub.h:65
bool operator==(const ClientStateCounts &other) const
bool operator!=(const ClientStateCounts &other) const