mirror of
https://github.com/esphome/esphome.git
synced 2026-08-22 22:26:21 +00:00
[rp2_ble_tracker] Extract stamp-and-start helper; pin capability/switch independence (#18072)
This commit is contained in:
@@ -163,11 +163,7 @@ bool RP2BLETracker::request_scan_mode(bool active) {
|
|||||||
// An idle scanner picks the mode up on its next start.
|
// An idle scanner picks the mode up on its next start.
|
||||||
if (this->scan_running_) {
|
if (this->scan_running_) {
|
||||||
this->parent_->scan_stop();
|
this->parent_->scan_stop();
|
||||||
// Stamp the attempt so the SCAN_START_RETRY_MS floor covers this start
|
if (!this->controller_scan_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_)) {
|
|
||||||
// The controller really stopped: behave exactly like loop()'s
|
// The controller really stopped: behave exactly like loop()'s
|
||||||
// reconciliation branch - notify listeners and let its retry recover.
|
// reconciliation branch - notify listeners and let its retry recover.
|
||||||
this->scan_running_ = false;
|
this->scan_running_ = false;
|
||||||
@@ -186,16 +182,19 @@ void RP2BLETracker::stop_scan() {
|
|||||||
this->disable_loop();
|
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_() {
|
void RP2BLETracker::start_scan_() {
|
||||||
if (this->scan_running_)
|
if (this->scan_running_)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Stamp every attempt regardless of caller so the loop's rate limit also
|
if (!this->controller_scan_start_())
|
||||||
// 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_))
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this->scan_running_ = true;
|
this->scan_running_ = true;
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ class RP2BLETracker : public Component,
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void start_scan_();
|
void start_scan_();
|
||||||
|
bool controller_scan_start_();
|
||||||
void stop_scan_();
|
void stop_scan_();
|
||||||
void fire_scan_end_();
|
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
|
} // namespace
|
||||||
|
|
||||||
TEST(BLEHubScanModeRequest, DefaultRefusesAndChangesNothing) {
|
TEST(BLEHubScanModeRequest, DefaultRefusesAndChangesNothing) {
|
||||||
@@ -56,4 +63,11 @@ TEST(BLEHubScanModeRequest, OverrideHonorsAndApplies) {
|
|||||||
EXPECT_FALSE(hub.scan_active());
|
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
|
} // namespace esphome::ble_device_base::testing
|
||||||
|
|||||||
Reference in New Issue
Block a user