[rp2_ble_tracker] Extract stamp-and-start helper; pin capability/switch independence (#18072)

This commit is contained in:
Edvard Filistovič
2026-08-04 18:20:57 +00:00
committed by GitHub
parent 8bd9f213e5
commit 914cab301f
3 changed files with 25 additions and 11 deletions
@@ -163,11 +163,7 @@ bool RP2BLETracker::request_scan_mode(bool active) {
// An idle scanner picks the mode up on its next start.
if (this->scan_running_) {
this->parent_->scan_stop();
// Stamp the attempt so the SCAN_START_RETRY_MS floor covers this start
// like every other one.
this->last_scan_start_attempt_ = App.get_loop_component_start_time();
if (!this->parent_->scan_start(static_cast<uint16_t>(this->scan_interval_),
static_cast<uint16_t>(this->scan_window_), this->scan_active_)) {
if (!this->controller_scan_start_()) {
// The controller really stopped: behave exactly like loop()'s
// reconciliation branch - notify listeners and let its retry recover.
this->scan_running_ = false;
@@ -186,16 +182,19 @@ void RP2BLETracker::stop_scan() {
this->disable_loop();
}
// Stamp-and-start for every controller scan attempt: the stamp keeps the
// SCAN_START_RETRY_MS floor covering all callers, not only loop()'s retry.
bool RP2BLETracker::controller_scan_start_() {
this->last_scan_start_attempt_ = App.get_loop_component_start_time();
return this->parent_->scan_start(static_cast<uint16_t>(this->scan_interval_),
static_cast<uint16_t>(this->scan_window_), this->scan_active_);
}
void RP2BLETracker::start_scan_() {
if (this->scan_running_)
return;
// Stamp every attempt regardless of caller so the loop's rate limit also
// covers a failed start that came through the public start_scan().
this->last_scan_start_attempt_ = App.get_loop_component_start_time();
if (!this->parent_->scan_start(static_cast<uint16_t>(this->scan_interval_), static_cast<uint16_t>(this->scan_window_),
this->scan_active_))
if (!this->controller_scan_start_())
return;
this->scan_running_ = true;
@@ -80,6 +80,7 @@ class RP2BLETracker : public Component,
protected:
void start_scan_();
bool controller_scan_start_();
void stop_scan_();
void fire_scan_end_();
@@ -36,6 +36,13 @@ class SwitchingHub : public DefaultHub {
}
};
// The esp32 shape: the controller supports active scanning but the hub keeps
// the refusing default (mode is driven through its own tracker API).
class CapableRefusingHub : public DefaultHub {
public:
HubCapabilities get_capabilities() const override { return {true, false, false}; }
};
} // namespace
TEST(BLEHubScanModeRequest, DefaultRefusesAndChangesNothing) {
@@ -56,4 +63,11 @@ TEST(BLEHubScanModeRequest, OverrideHonorsAndApplies) {
EXPECT_FALSE(hub.scan_active());
}
TEST(BLEHubScanModeRequest, CapabilityAndSwitchAreIndependent) {
CapableRefusingHub hub;
EXPECT_TRUE(hub.get_capabilities().active_scan);
EXPECT_FALSE(hub.request_scan_mode(false));
EXPECT_TRUE(hub.scan_active());
}
} // namespace esphome::ble_device_base::testing