diff --git a/esphome/components/ble_device_base/ble_device.h b/esphome/components/ble_device_base/ble_device.h index fba1fe2347..b5f198375c 100644 --- a/esphome/components/ble_device_base/ble_device.h +++ b/esphome/components/ble_device_base/ble_device.h @@ -154,12 +154,9 @@ class ESPBLEiBeacon { }; /// Pack a controller-order (LSB-first) MAC into the uint64 the API speaks. -/// -/// The result is the printable-order value esp32 has always sent -/// (esp32_ble::ble_addr_to_uint64), so both proxy paths agree on the wire. -/// This takes the raw controller order delivered by BLEHub's raw-advertisement -/// callback; ESPBTDevice::address_uint64() is the equivalent for an already -/// parsed device, whose address is stored MSB-first. +/// Trackers with LSB-native SDKs call this at the emit site before filling +/// RawAdvertisement::address; ESPBTDevice::address_uint64() is the equivalent +/// for an already parsed device, whose address is stored MSB-first. inline uint64_t mac_lsb_first_to_uint64(const uint8_t *mac) { uint64_t addr = 0; for (int i = 0; i < 6; i++) diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index 7907d00b68..e78d0c4781 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -8,6 +8,7 @@ #include "esphome/core/macros.h" #include "esphome/core/application.h" #include +#include #include #include @@ -90,7 +91,7 @@ void BluetoothProxy::on_raw_advertisement_(const ble_device_base::RawAdvertiseme this->response_.advertisements_len++; - ESP_LOGV(TAG, "Queuing raw packet from %012llX, length %d. RSSI: %d dB", raw.address, length, raw.rssi); + ESP_LOGV(TAG, "Queuing raw packet from %012" PRIX64 ", length %d. RSSI: %d dB", raw.address, length, raw.rssi); // Flush if we have reached BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE if (this->response_.advertisements_len >= BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE) { @@ -460,13 +461,13 @@ void BluetoothProxy::bluetooth_set_connection_params(const api::BluetoothSetConn #ifdef USE_ESP32 void BluetoothProxy::bluetooth_scanner_set_mode(bool active) { - if (this->parent_->get_scan_active() == active) { + if (this->parent_()->get_scan_active() == active) { return; } ESP_LOGD(TAG, "Setting scanner mode to %s", active ? "active" : "passive"); - this->parent_->set_scan_active(active); - this->parent_->stop_scan(); - this->parent_->set_scan_continuous( + this->parent_()->set_scan_active(active); + this->parent_()->stop_scan(); + this->parent_()->set_scan_continuous( true); // Set this to true to automatically start scanning again when it has cleaned up. } @@ -613,7 +614,7 @@ void BluetoothProxy::subscribe_api_connection(api::APIConnection *api_connection } this->api_connection_ = api_connection; #ifdef USE_ESP32 - this->send_bluetooth_scanner_state_(static_cast(this->parent_->get_scanner_state())); + this->send_bluetooth_scanner_state_(static_cast(this->parent_()->get_scanner_state())); #else this->send_polled_scanner_state_(); #endif diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.h b/esphome/components/bluetooth_proxy/bluetooth_proxy.h index 2e07a01dee..a4a28dd5bc 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.h +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.h @@ -79,12 +79,9 @@ class BluetoothProxy final : public Component { public: BluetoothProxy(); #ifdef USE_ESP32 - // Advertisements arrive through the hub's raw callback; the tracker is - // still typed for the esp32-only scan-mode and scanner-state calls. - void set_parent(esp32_ble_tracker::ESP32BLETracker *parent) { - this->parent_ = parent; - this->hub_ = parent; - } + // Advertisements arrive through the hub's raw callback; parent_() below + // recovers the tracker type for the esp32-only scan-mode calls. + void set_parent(esp32_ble_tracker::ESP32BLETracker *parent) { this->hub_ = parent; } #endif // USE_ESP32 void dump_config() override; void setup() override; @@ -269,7 +266,11 @@ class BluetoothProxy final : public Component { #endif ble_device_base::BLEHub *hub_{nullptr}; #ifdef USE_ESP32 - esp32_ble_tracker::ESP32BLETracker *parent_{nullptr}; + // set_parent() is the only writer of hub_ on esp32, so the downcast is + // exact; ESP32BLETracker derives from BLEHub non-virtually. + esp32_ble_tracker::ESP32BLETracker *parent_() { + return static_cast(this->hub_); + } #endif // BLE advertisement batching diff --git a/tests/components/ble_device_base/test_raw_callback.cpp b/tests/components/ble_device_base/test_raw_callback.cpp index c28edfb7ef..4d72c8fb18 100644 --- a/tests/components/ble_device_base/test_raw_callback.cpp +++ b/tests/components/ble_device_base/test_raw_callback.cpp @@ -46,7 +46,7 @@ struct CapturingSubscriber { } }; -// Device AA:BB:CC:DD:EE:FF — controller order delivers FF first. +// Device AA:BB:CC:DD:EE:FF, packed the way the API speaks it. constexpr uint64_t TEST_ADDRESS = 0xAABBCCDDEEFFULL; const uint8_t ADV_DATA[4] = {0x02, 0x01, 0x06, 0x00};