Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/wifi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
idf_component_register(
INCLUDE_DIRS "include"
SRC_DIRS "src"
PRIV_REQUIRES esp_wifi nvs_flash base_component cli)
REQUIRES esp_wifi nvs_flash base_component cli)
36 changes: 33 additions & 3 deletions components/wifi/include/wifi_ap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,22 @@ class WifiAp : public WifiBase {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}

logger_.debug("Setting WiFi mode to AP");
err = esp_wifi_set_mode(WIFI_MODE_AP);
// Get current mode and add AP to it if needed
wifi_mode_t current_mode;
err = esp_wifi_get_mode(&current_mode);
wifi_mode_t new_mode = WIFI_MODE_AP;
if (err == ESP_OK) {
if (current_mode == WIFI_MODE_STA) {
new_mode = WIFI_MODE_APSTA;
logger_.debug("STA mode already active, setting mode to APSTA");
} else if (current_mode == WIFI_MODE_APSTA) {
new_mode = WIFI_MODE_APSTA;
logger_.debug("APSTA mode already set");
}
}

logger_.debug("Setting WiFi mode to {}", new_mode);
err = esp_wifi_set_mode(new_mode);
if (err != ESP_OK) {
logger_.error("Could not set WiFi to AP: {}", err);
return false;
Expand Down Expand Up @@ -328,8 +342,24 @@ class WifiAp : public WifiBase {
* @return True if the operation was successful, false otherwise.
*/
bool stop() override {
// Check if we're in APSTA mode - if so, just switch to STA mode, don't stop WiFi entirely
wifi_mode_t mode;
esp_err_t err = esp_wifi_get_mode(&mode);
if (err == ESP_OK && mode == WIFI_MODE_APSTA) {
// In APSTA mode, switch to STA-only mode
logger_.debug("In APSTA mode, switching to STA mode");
err = esp_wifi_set_mode(WIFI_MODE_STA);
if (err != ESP_OK) {
logger_.error("Could not switch to STA mode: {}", esp_err_to_name(err));
return false;
}
logger_.info("WiFi AP stopped, STA still running");
return true;
}

// In AP-only mode, stop WiFi entirely
logger_.debug("Stopping WiFi");
esp_err_t err = esp_wifi_stop();
err = esp_wifi_stop();
if (err != ESP_OK) {
logger_.error("Could not stop WiFi AP: {}", esp_err_to_name(err));
return false;
Expand Down
45 changes: 37 additions & 8 deletions components/wifi/include/wifi_sta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,28 @@ class WifiSta : public WifiBase {
}

// NOTE: Deinit phase
// stop the wifi
logger_.debug("Stopping WiFi");
esp_err_t err = esp_wifi_stop();
if (err != ESP_OK) {
logger_.error("Could not stop WiFiSta: {}", esp_err_to_name(err));
// Check if we're in APSTA mode - if so, just disconnect STA, don't stop WiFi entirely
wifi_mode_t mode;
esp_err_t err = esp_wifi_get_mode(&mode);
if (err == ESP_OK && mode == WIFI_MODE_APSTA) {
// In APSTA mode, just disconnect the STA interface
logger_.debug("In APSTA mode, disconnecting STA interface");
esp_wifi_disconnect();
// Switch to AP-only mode
err = esp_wifi_set_mode(WIFI_MODE_AP);
if (err != ESP_OK) {
logger_.error("Could not switch to AP mode: {}", esp_err_to_name(err));
}
logger_.info("WiFi STA disconnected, AP still running");
Comment on lines +117 to +123
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of esp_wifi_disconnect() is not checked. If the disconnect operation fails, the subsequent mode switch to WIFI_MODE_AP could leave the system in an inconsistent state. Consider checking the return value and logging an error if the disconnect fails.

Suggested change
esp_wifi_disconnect();
// Switch to AP-only mode
err = esp_wifi_set_mode(WIFI_MODE_AP);
if (err != ESP_OK) {
logger_.error("Could not switch to AP mode: {}", esp_err_to_name(err));
}
logger_.info("WiFi STA disconnected, AP still running");
err = esp_wifi_disconnect();
if (err != ESP_OK) {
logger_.error("Failed to disconnect STA interface: {}", esp_err_to_name(err));
} else {
// Switch to AP-only mode
err = esp_wifi_set_mode(WIFI_MODE_AP);
if (err != ESP_OK) {
logger_.error("Could not switch to AP mode: {}", esp_err_to_name(err));
}
logger_.info("WiFi STA disconnected, AP still running");
}

Copilot uses AI. Check for mistakes.
} else {
// In STA-only mode, stop WiFi entirely
logger_.debug("Stopping WiFi");
err = esp_wifi_stop();
if (err != ESP_OK) {
logger_.error("Could not stop WiFiSta: {}", esp_err_to_name(err));
}
logger_.info("WiFi STA stopped");
}
logger_.info("WiFi STA stopped");
// Note: WiFi deinit and netif destruction are handled by Wifi singleton
}

Expand Down Expand Up @@ -364,8 +379,22 @@ class WifiSta : public WifiBase {
memcpy(wifi_config.sta.bssid, config.ap_mac, 6);
}

logger_.debug("Setting WiFi mode to STA");
esp_err_t err = esp_wifi_set_mode(WIFI_MODE_STA);
// Get current mode and add STA to it if needed
wifi_mode_t current_mode;
esp_err_t err = esp_wifi_get_mode(&current_mode);
wifi_mode_t new_mode = WIFI_MODE_STA;
if (err == ESP_OK) {
if (current_mode == WIFI_MODE_AP) {
new_mode = WIFI_MODE_APSTA;
logger_.debug("AP mode already active, setting mode to APSTA");
} else if (current_mode == WIFI_MODE_APSTA) {
new_mode = WIFI_MODE_APSTA;
logger_.debug("APSTA mode already set");
}
}

logger_.debug("Setting WiFi mode to {}", new_mode);
err = esp_wifi_set_mode(new_mode);
if (err != ESP_OK) {
logger_.error("Could not set WiFi mode STA: {}", err);
return false;
Expand Down