Retire the raw listener path and parser-type enum from the tracker

This commit is contained in:
J. Nick Koston
2026-08-08 00:36:22 -05:00
parent 3e9808162b
commit 67cc43c8ef
5 changed files with 16 additions and 56 deletions
@@ -479,10 +479,9 @@ esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enabl
return this->check_and_log_error_("esp_ble_gattc_unregister_for_notify", err);
}
esp32_ble_tracker::AdvertisementParserType BluetoothConnection::get_advertisement_parser_type() {
// RAW keeps the tracker from building parsed ESPBTDevice objects for the
// proxy's connections (the proxy itself consumes the hub raw callback).
return esp32_ble_tracker::AdvertisementParserType::RAW_ADVERTISEMENTS;
bool BluetoothConnection::wants_parsed_advertisements() {
// The proxy's connections never consume parsed ESPBTDevice objects.
return false;
}
} // namespace esphome::bluetooth_connection
@@ -21,7 +21,7 @@ class BluetoothConnection final : public esp32_ble_client::BLEClientBase {
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t *param) override;
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
esp32_ble_tracker::AdvertisementParserType get_advertisement_parser_type() override;
bool wants_parsed_advertisements() override;
esp_err_t read_characteristic(uint16_t handle);
esp_err_t write_characteristic(uint16_t handle, const uint8_t *data, size_t length, bool response);
@@ -23,7 +23,6 @@
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
#include <esp_bt.h>
#endif
#include <esp_bt_device.h>
#else
#include "esphome/components/ble_device_base/ble_hub.h"
#ifdef USE_BLE_GATT_CLIENT
@@ -182,19 +181,10 @@ class BluetoothProxy final : public Component {
}
void get_bluetooth_mac_address_pretty(std::span<char, 18> output) {
#ifdef USE_ESP32
const uint8_t *mac = esp_bt_dev_get_address();
if (mac != nullptr) {
format_mac_addr_upper(mac, output.data());
} else {
output[0] = '\0';
}
#else
uint8_t mac[6] = {};
this->hub_->get_adapter_mac(mac);
// Mirror the esp32 arm's unavailable -> empty-string fallback: some hubs
// (rp2040's BTstack) only learn the address once the link layer is up, and
// report all-zero until then.
// Unavailable -> empty string: some hubs (rp2040's BTstack) only learn
// the address once the link layer is up, and report all-zero until then.
bool nonzero = false;
for (uint8_t b : mac)
nonzero |= b != 0;
@@ -203,7 +193,6 @@ class BluetoothProxy final : public Component {
} else {
output[0] = '\0';
}
#endif
}
protected:
@@ -271,7 +271,7 @@ void ESP32BLETracker::register_client(ESPBTClient *client) {
// Safe because ESP32BLETracker (singleton) outlives all registered clients.
client->set_tracker_state_version(&this->state_version_);
this->clients_.push_back(client);
this->recalculate_advertisement_parser_types();
this->recalculate_parse_advertisements_();
#endif
}
@@ -294,36 +294,29 @@ void ESP32BLETracker::register_listener(ESPBTDeviceListener *listener) {
#ifdef ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT
listener->set_parent(this);
this->listeners_.push_back(listener);
this->recalculate_advertisement_parser_types();
this->recalculate_parse_advertisements_();
#endif
}
void ESP32BLETracker::recalculate_advertisement_parser_types() {
this->raw_advertisements_ = false;
void ESP32BLETracker::recalculate_parse_advertisements_() {
this->parse_advertisements_ = false;
#ifdef ESPHOME_BLE_DEVICE_BASE_LISTENER_COUNT
// Neutral (BLEHub) listeners are parsed-advertisement consumers and are not in
// listeners_; without this, any later esp32-path registration (e.g. the proxy's
// GATT clients) would recompute the flags and silently drop parsed dispatch.
// GATT clients) would recompute the flag and silently drop parsed dispatch.
if (!this->neutral_listeners_.empty())
this->parse_advertisements_ = true;
#endif
#ifdef ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT
for (auto *listener : this->listeners_) {
if (listener->get_advertisement_parser_type() == AdvertisementParserType::PARSED_ADVERTISEMENTS) {
if (listener->wants_parsed_advertisements())
this->parse_advertisements_ = true;
} else {
this->raw_advertisements_ = true;
}
}
#endif
#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
for (auto *client : this->clients_) {
if (client->get_advertisement_parser_type() == AdvertisementParserType::PARSED_ADVERTISEMENTS) {
if (client->wants_parsed_advertisements())
this->parse_advertisements_ = true;
} else {
this->raw_advertisements_ = true;
}
}
#endif
}
@@ -480,20 +473,6 @@ void ESP32BLETracker::process_scan_result_(const BLEScanResult &scan_result) {
this->raw_advertisement_callback_.invoke(adv);
}
// Process raw advertisements
if (this->raw_advertisements_) {
#ifdef ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT
for (auto *listener : this->listeners_) {
listener->parse_devices(&scan_result, 1);
}
#endif
#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
for (auto *client : this->clients_) {
client->parse_devices(&scan_result, 1);
}
#endif
}
// Process parsed advertisements
if (this->parse_advertisements_) {
#ifdef USE_ESP32_BLE_DEVICE
@@ -35,11 +35,6 @@ using namespace esp32_ble;
using adv_data_t = ble_device_base::adv_data_t;
enum AdvertisementParserType {
PARSED_ADVERTISEMENTS,
RAW_ADVERTISEMENTS,
};
#ifdef USE_ESP32_BLE_UUID
using ServiceData = ble_device_base::ServiceData;
#endif
@@ -63,10 +58,9 @@ class ESPBTDeviceListener : public ble_device_base::ESPBTDeviceListener {
// Raw-only build: no parsed-device support is compiled in.
bool parse_device(const ble_device_base::ESPBTDevice &device) override { return false; }
#endif
virtual bool parse_devices(const BLEScanResult *scan_results, size_t count) { return false; };
virtual AdvertisementParserType get_advertisement_parser_type() {
return AdvertisementParserType::PARSED_ADVERTISEMENTS;
};
/// False keeps the tracker from building parsed ESPBTDevice objects on
/// this registrant's account (raw consumers use the hub callback).
virtual bool wants_parsed_advertisements() { return true; }
void set_parent(ESP32BLETracker *parent) { parent_ = parent; }
protected:
@@ -209,7 +203,6 @@ class ESP32BLETracker final : public Component,
// esp32-flavored path (unmigrated esp32 sensors; sets the tracker back-pointer).
void register_listener(ESPBTDeviceListener *listener);
void register_client(ESPBTClient *client);
void recalculate_advertisement_parser_types();
// ---- ble_device_base::BLEHub (the platform-neutral tracker contract) ----
void register_listener(ble_device_base::ESPBTDeviceListener *listener) override;
@@ -267,6 +260,7 @@ class ESP32BLETracker final : public Component,
void gap_scan_stop_complete_(const esp_ble_gap_cb_param_t::ble_scan_stop_cmpl_evt_param &param);
/// Called to set the scanner state. Will also call callbacks to let listeners know when state is changed.
void set_scanner_state_(ScannerState state);
void recalculate_parse_advertisements_();
/// Common cleanup logic when transitioning scanner to IDLE state
void cleanup_scan_state_(bool is_stop_complete);
/// Process a single scan result immediately
@@ -365,7 +359,6 @@ class ESP32BLETracker final : public Component,
bool scan_continuous_before_ota_{false};
#endif
bool ble_was_disabled_{true};
bool raw_advertisements_{false};
bool parse_advertisements_{false};
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
bool coex_prefer_ble_{false};