[improv_serial] Report stopped state when Wi-Fi is disabled (#16904)

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Keith Burzinski
2026-06-10 17:26:50 -05:00
committed by GitHub
parent 77009cfafe
commit 92c82f3d25
3 changed files with 31 additions and 1 deletions

View File

@@ -338,6 +338,14 @@ void ESP32ImprovComponent::process_incoming_data_() {
this->incoming_data_.clear(); this->incoming_data_.clear();
return; return;
} }
if (wifi::global_wifi_component->is_disabled()) {
// Wi-Fi is disabled, so we can't provision. Respond immediately
// instead of letting the client wait out its provisioning timeout.
ESP_LOGW(TAG, "Wi-Fi is disabled; cannot provision");
this->set_error_(improv::ERROR_UNABLE_TO_CONNECT);
this->incoming_data_.clear();
return;
}
wifi::WiFiAP sta{}; wifi::WiFiAP sta{};
sta.set_ssid(command.ssid.c_str()); sta.set_ssid(command.ssid.c_str());
sta.set_password(command.password.c_str()); sta.set_password(command.password.c_str());

View File

@@ -22,7 +22,9 @@ void ImprovSerialComponent::setup() {
if (wifi::global_wifi_component->has_sta()) { if (wifi::global_wifi_component->has_sta()) {
this->state_ = improv::STATE_PROVISIONED; this->state_ = improv::STATE_PROVISIONED;
} else { } else if (!wifi::global_wifi_component->is_disabled()) {
// Respect Wi-Fi's disabled state; forcing a scan while disabled throws
// the wifi component into an invalid state from which it cannot recover.
wifi::global_wifi_component->start_scanning(); wifi::global_wifi_component->start_scanning();
} }
} }
@@ -230,6 +232,13 @@ bool ImprovSerialComponent::parse_improv_serial_byte_(uint8_t byte) {
bool ImprovSerialComponent::parse_improv_payload_(improv::ImprovCommand &command) { bool ImprovSerialComponent::parse_improv_payload_(improv::ImprovCommand &command) {
switch (command.command) { switch (command.command) {
case improv::WIFI_SETTINGS: { case improv::WIFI_SETTINGS: {
if (wifi::global_wifi_component->is_disabled()) {
// Wi-Fi is disabled, so we can't provision. Respond immediately
// instead of letting the client wait out its provisioning timeout.
ESP_LOGW(TAG, "Wi-Fi is disabled; cannot provision");
this->set_error_(improv::ERROR_UNABLE_TO_CONNECT);
return true;
}
wifi::WiFiAP sta{}; wifi::WiFiAP sta{};
sta.set_ssid(command.ssid.c_str()); sta.set_ssid(command.ssid.c_str());
sta.set_password(command.password.c_str()); sta.set_password(command.password.c_str());
@@ -245,6 +254,14 @@ bool ImprovSerialComponent::parse_improv_payload_(improv::ImprovCommand &command
return true; return true;
} }
case improv::GET_CURRENT_STATE: case improv::GET_CURRENT_STATE:
if (wifi::global_wifi_component->is_disabled()) {
// Wi-Fi is disabled; report the Improv "stopped" state so a client can tell
// the user that provisioning is unavailable. Reported transiently without
// disturbing our internal provisioning state machine, so a later `wifi.enable`
// still reports the correct state.
this->send_current_state_(improv::STATE_STOPPED);
return true;
}
this->set_state_(this->state_); this->set_state_(this->state_);
if (this->state_ == improv::STATE_PROVISIONED) { if (this->state_ == improv::STATE_PROVISIONED) {
std::vector<uint8_t> url = this->build_rpc_settings_response_(improv::GET_CURRENT_STATE); std::vector<uint8_t> url = this->build_rpc_settings_response_(improv::GET_CURRENT_STATE);
@@ -299,6 +316,10 @@ bool ImprovSerialComponent::parse_improv_payload_(improv::ImprovCommand &command
void ImprovSerialComponent::set_state_(improv::State state) { void ImprovSerialComponent::set_state_(improv::State state) {
this->state_ = state; this->state_ = state;
this->send_current_state_(state);
}
void ImprovSerialComponent::send_current_state_(improv::State state) {
this->tx_header_[TX_TYPE_IDX] = TYPE_CURRENT_STATE; this->tx_header_[TX_TYPE_IDX] = TYPE_CURRENT_STATE;
this->tx_header_[TX_DATA_IDX] = state; this->tx_header_[TX_DATA_IDX] = state;
this->write_data_(); this->write_data_();

View File

@@ -57,6 +57,7 @@ class ImprovSerialComponent : public Component, public improv_base::ImprovBase {
bool parse_improv_payload_(improv::ImprovCommand &command); bool parse_improv_payload_(improv::ImprovCommand &command);
void set_state_(improv::State state); void set_state_(improv::State state);
void send_current_state_(improv::State state);
void set_error_(improv::Error error); void set_error_(improv::Error error);
void send_response_(std::vector<uint8_t> &response); void send_response_(std::vector<uint8_t> &response);
void on_wifi_connect_timeout_(); void on_wifi_connect_timeout_();