Skip to content

Commit f8e5865

Browse files
committed
feat(wifi): Improve Wifi STA + AP init/deinit to have better support for simultaneity
1 parent 6972195 commit f8e5865

3 files changed

Lines changed: 71 additions & 12 deletions

File tree

components/wifi/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
idf_component_register(
22
INCLUDE_DIRS "include"
33
SRC_DIRS "src"
4-
PRIV_REQUIRES esp_wifi nvs_flash base_component cli)
4+
REQUIRES esp_wifi nvs_flash base_component cli)

components/wifi/include/wifi_ap.hpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,22 @@ class WifiAp : public WifiBase {
278278
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
279279
}
280280

281-
logger_.debug("Setting WiFi mode to AP");
282-
err = esp_wifi_set_mode(WIFI_MODE_AP);
281+
// Get current mode and add AP to it if needed
282+
wifi_mode_t current_mode;
283+
err = esp_wifi_get_mode(&current_mode);
284+
wifi_mode_t new_mode = WIFI_MODE_AP;
285+
if (err == ESP_OK) {
286+
if (current_mode == WIFI_MODE_STA) {
287+
new_mode = WIFI_MODE_APSTA;
288+
logger_.debug("STA mode already active, setting mode to APSTA");
289+
} else if (current_mode == WIFI_MODE_APSTA) {
290+
new_mode = WIFI_MODE_APSTA;
291+
logger_.debug("APSTA mode already set");
292+
}
293+
}
294+
295+
logger_.debug("Setting WiFi mode to {}", new_mode);
296+
err = esp_wifi_set_mode(new_mode);
283297
if (err != ESP_OK) {
284298
logger_.error("Could not set WiFi to AP: {}", err);
285299
return false;
@@ -328,8 +342,24 @@ class WifiAp : public WifiBase {
328342
* @return True if the operation was successful, false otherwise.
329343
*/
330344
bool stop() override {
345+
// Check if we're in APSTA mode - if so, just switch to STA mode, don't stop WiFi entirely
346+
wifi_mode_t mode;
347+
esp_err_t err = esp_wifi_get_mode(&mode);
348+
if (err == ESP_OK && mode == WIFI_MODE_APSTA) {
349+
// In APSTA mode, switch to STA-only mode
350+
logger_.debug("In APSTA mode, switching to STA mode");
351+
err = esp_wifi_set_mode(WIFI_MODE_STA);
352+
if (err != ESP_OK) {
353+
logger_.error("Could not switch to STA mode: {}", esp_err_to_name(err));
354+
return false;
355+
}
356+
logger_.info("WiFi AP stopped, STA still running");
357+
return true;
358+
}
359+
360+
// In AP-only mode, stop WiFi entirely
331361
logger_.debug("Stopping WiFi");
332-
esp_err_t err = esp_wifi_stop();
362+
err = esp_wifi_stop();
333363
if (err != ESP_OK) {
334364
logger_.error("Could not stop WiFi AP: {}", esp_err_to_name(err));
335365
return false;

components/wifi/include/wifi_sta.hpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,28 @@ class WifiSta : public WifiBase {
108108
}
109109

110110
// NOTE: Deinit phase
111-
// stop the wifi
112-
logger_.debug("Stopping WiFi");
113-
esp_err_t err = esp_wifi_stop();
114-
if (err != ESP_OK) {
115-
logger_.error("Could not stop WiFiSta: {}", esp_err_to_name(err));
111+
// Check if we're in APSTA mode - if so, just disconnect STA, don't stop WiFi entirely
112+
wifi_mode_t mode;
113+
esp_err_t err = esp_wifi_get_mode(&mode);
114+
if (err == ESP_OK && mode == WIFI_MODE_APSTA) {
115+
// In APSTA mode, just disconnect the STA interface
116+
logger_.debug("In APSTA mode, disconnecting STA interface");
117+
esp_wifi_disconnect();
118+
// Switch to AP-only mode
119+
err = esp_wifi_set_mode(WIFI_MODE_AP);
120+
if (err != ESP_OK) {
121+
logger_.error("Could not switch to AP mode: {}", esp_err_to_name(err));
122+
}
123+
logger_.info("WiFi STA disconnected, AP still running");
124+
} else {
125+
// In STA-only mode, stop WiFi entirely
126+
logger_.debug("Stopping WiFi");
127+
err = esp_wifi_stop();
128+
if (err != ESP_OK) {
129+
logger_.error("Could not stop WiFiSta: {}", esp_err_to_name(err));
130+
}
131+
logger_.info("WiFi STA stopped");
116132
}
117-
logger_.info("WiFi STA stopped");
118133
// Note: WiFi deinit and netif destruction are handled by Wifi singleton
119134
}
120135

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

367-
logger_.debug("Setting WiFi mode to STA");
368-
esp_err_t err = esp_wifi_set_mode(WIFI_MODE_STA);
382+
// Get current mode and add STA to it if needed
383+
wifi_mode_t current_mode;
384+
esp_err_t err = esp_wifi_get_mode(&current_mode);
385+
wifi_mode_t new_mode = WIFI_MODE_STA;
386+
if (err == ESP_OK) {
387+
if (current_mode == WIFI_MODE_AP) {
388+
new_mode = WIFI_MODE_APSTA;
389+
logger_.debug("AP mode already active, setting mode to APSTA");
390+
} else if (current_mode == WIFI_MODE_APSTA) {
391+
new_mode = WIFI_MODE_APSTA;
392+
logger_.debug("APSTA mode already set");
393+
}
394+
}
395+
396+
logger_.debug("Setting WiFi mode to {}", new_mode);
397+
err = esp_wifi_set_mode(new_mode);
369398
if (err != ESP_OK) {
370399
logger_.error("Could not set WiFi mode STA: {}", err);
371400
return false;

0 commit comments

Comments
 (0)