mirror of
https://github.com/esphome/esphome.git
synced 2026-06-02 03:02:19 +08:00
[esp32_ble] Extract state transitions from ESP32BLE::loop() hot path (#13903)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -369,42 +369,9 @@ bool ESP32BLE::ble_dismantle_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ESP32BLE::loop() {
|
void ESP32BLE::loop() {
|
||||||
switch (this->state_) {
|
if (this->state_ != BLE_COMPONENT_STATE_ACTIVE) {
|
||||||
case BLE_COMPONENT_STATE_OFF:
|
this->loop_handle_state_transition_not_active_();
|
||||||
case BLE_COMPONENT_STATE_DISABLED:
|
return;
|
||||||
return;
|
|
||||||
case BLE_COMPONENT_STATE_DISABLE: {
|
|
||||||
ESP_LOGD(TAG, "Disabling");
|
|
||||||
|
|
||||||
#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
|
|
||||||
for (auto *ble_event_handler : this->ble_status_event_handlers_) {
|
|
||||||
ble_event_handler->ble_before_disabled_event_handler();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!ble_dismantle_()) {
|
|
||||||
ESP_LOGE(TAG, "Could not be dismantled");
|
|
||||||
this->mark_failed();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this->state_ = BLE_COMPONENT_STATE_DISABLED;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
case BLE_COMPONENT_STATE_ENABLE: {
|
|
||||||
ESP_LOGD(TAG, "Enabling");
|
|
||||||
this->state_ = BLE_COMPONENT_STATE_OFF;
|
|
||||||
|
|
||||||
if (!ble_setup_()) {
|
|
||||||
ESP_LOGE(TAG, "Could not be set up");
|
|
||||||
this->mark_failed();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this->state_ = BLE_COMPONENT_STATE_ACTIVE;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
case BLE_COMPONENT_STATE_ACTIVE:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BLEEvent *ble_event = this->ble_events_.pop();
|
BLEEvent *ble_event = this->ble_events_.pop();
|
||||||
@@ -520,6 +487,37 @@ void ESP32BLE::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESP32BLE::loop_handle_state_transition_not_active_() {
|
||||||
|
// Caller ensures state_ != ACTIVE
|
||||||
|
if (this->state_ == BLE_COMPONENT_STATE_DISABLE) {
|
||||||
|
ESP_LOGD(TAG, "Disabling");
|
||||||
|
|
||||||
|
#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
|
||||||
|
for (auto *ble_event_handler : this->ble_status_event_handlers_) {
|
||||||
|
ble_event_handler->ble_before_disabled_event_handler();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!ble_dismantle_()) {
|
||||||
|
ESP_LOGE(TAG, "Could not be dismantled");
|
||||||
|
this->mark_failed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this->state_ = BLE_COMPONENT_STATE_DISABLED;
|
||||||
|
} else if (this->state_ == BLE_COMPONENT_STATE_ENABLE) {
|
||||||
|
ESP_LOGD(TAG, "Enabling");
|
||||||
|
this->state_ = BLE_COMPONENT_STATE_OFF;
|
||||||
|
|
||||||
|
if (!ble_setup_()) {
|
||||||
|
ESP_LOGE(TAG, "Could not be set up");
|
||||||
|
this->mark_failed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->state_ = BLE_COMPONENT_STATE_ACTIVE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Helper function to load new event data based on type
|
// Helper function to load new event data based on type
|
||||||
void load_ble_event(BLEEvent *event, esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p) {
|
void load_ble_event(BLEEvent *event, esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p) {
|
||||||
event->load_gap_event(e, p);
|
event->load_gap_event(e, p);
|
||||||
|
|||||||
@@ -155,6 +155,10 @@ class ESP32BLE : public Component {
|
|||||||
#endif
|
#endif
|
||||||
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
||||||
|
|
||||||
|
// Handle DISABLE and ENABLE transitions when not in the ACTIVE state.
|
||||||
|
// Other non-ACTIVE states (e.g. OFF, DISABLED) are currently treated as no-ops.
|
||||||
|
void __attribute__((noinline)) loop_handle_state_transition_not_active_();
|
||||||
|
|
||||||
bool ble_setup_();
|
bool ble_setup_();
|
||||||
bool ble_dismantle_();
|
bool ble_dismantle_();
|
||||||
bool ble_pre_setup_();
|
bool ble_pre_setup_();
|
||||||
|
|||||||
Reference in New Issue
Block a user