Skip to content

Commit de91ecd

Browse files
osmakarikartben
authored andcommitted
drivers: wifi: esp32: Fix AP-STA mode interface init order
Wi-Fi AP-STA mode fails on ESP32 due to incorrect interface initialization. In AP-STA mode, esp32_wifi_init first sets the driver's AP interface, followed by the STA interface. However, the driver initializes them in reverse order, causing both to read incorrect device data and resulting in a "Wi-Fi not in station mode" error. This fix handles STA and AP initialization in separate functions and checks for correct mode in esp32_wifi_status. Tested on a custom ESP32-S3-WROOM-1-N4R2 board, where both STA and AP can connect, send, and receive data. Signed-off-by: Oskari Seppä <[email protected]>
1 parent a58588e commit de91ecd

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

drivers/wifi/esp32/src/esp_wifi_drv.c

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,13 @@ static int esp32_wifi_status(const struct device *dev, struct wifi_iface_status
773773
status->mfp = WIFI_MFP_DISABLE;
774774

775775
if (esp_wifi_get_mode(&mode) == ESP_OK) {
776+
#if defined(CONFIG_ESP32_WIFI_AP_STA_MODE)
777+
if (mode == ESP32_WIFI_MODE_APSTA && data == &esp32_data) {
778+
mode = ESP32_WIFI_MODE_STA;
779+
} else if (mode == ESP32_WIFI_MODE_APSTA && data == &esp32_ap_sta_data) {
780+
mode = ESP32_WIFI_MODE_AP;
781+
}
782+
#endif
776783
if (mode == ESP32_WIFI_MODE_STA) {
777784
wifi_phy_mode_t phy_mode;
778785
esp_err_t err;
@@ -842,21 +849,14 @@ static void esp32_wifi_init(struct net_if *iface)
842849
#if defined(CONFIG_ESP32_WIFI_AP_STA_MODE)
843850
struct wifi_nm_instance *nm = wifi_nm_get_instance("esp32_wifi_nm");
844851

845-
if (!esp32_wifi_iface_ap) {
846-
esp32_wifi_iface_ap = iface;
847-
dev_data->state = ESP32_AP_STOPPED;
852+
esp32_wifi_iface = iface;
853+
dev_data->state = ESP32_STA_STOPPED;
848854

849-
esp_read_mac(dev_data->mac_addr, ESP_MAC_WIFI_SOFTAP);
850-
wifi_nm_register_mgd_type_iface(nm, WIFI_TYPE_SAP, esp32_wifi_iface_ap);
851-
} else {
852-
esp32_wifi_iface = iface;
853-
dev_data->state = ESP32_STA_STOPPED;
855+
/* Start interface when we are actually connected with Wi-Fi network */
856+
esp_read_mac(dev_data->mac_addr, ESP_MAC_WIFI_STA);
857+
esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, eth_esp32_rx);
858+
wifi_nm_register_mgd_type_iface(nm, WIFI_TYPE_STA, esp32_wifi_iface);
854859

855-
/* Start interface when we are actually connected with Wi-Fi network */
856-
esp_read_mac(dev_data->mac_addr, ESP_MAC_WIFI_STA);
857-
esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, eth_esp32_rx);
858-
wifi_nm_register_mgd_type_iface(nm, WIFI_TYPE_STA, esp32_wifi_iface);
859-
}
860860
#else
861861

862862
esp32_wifi_iface = iface;
@@ -875,6 +875,31 @@ static void esp32_wifi_init(struct net_if *iface)
875875
net_if_carrier_off(iface);
876876
}
877877

878+
#if defined(CONFIG_ESP32_WIFI_AP_STA_MODE)
879+
static void esp32_wifi_init_ap(struct net_if *iface)
880+
{
881+
const struct device *dev = net_if_get_device(iface);
882+
struct esp32_wifi_runtime *dev_data = dev->data;
883+
struct ethernet_context *eth_ctx = net_if_l2_data(iface);
884+
885+
eth_ctx->eth_if_type = L2_ETH_IF_TYPE_WIFI;
886+
887+
struct wifi_nm_instance *nm = wifi_nm_get_instance("esp32_wifi_nm");
888+
889+
esp32_wifi_iface_ap = iface;
890+
dev_data->state = ESP32_AP_STOPPED;
891+
892+
esp_read_mac(dev_data->mac_addr, ESP_MAC_WIFI_SOFTAP);
893+
wifi_nm_register_mgd_type_iface(nm, WIFI_TYPE_SAP, esp32_wifi_iface_ap);
894+
895+
/* Assign link local address. */
896+
net_if_set_link_addr(iface, dev_data->mac_addr, 6, NET_LINK_ETHERNET);
897+
898+
ethernet_init(iface);
899+
net_if_carrier_off(iface);
900+
}
901+
#endif
902+
878903
#if defined(CONFIG_NET_STATISTICS_WIFI)
879904
static int esp32_wifi_stats(const struct device *dev, struct net_stats_wifi *stats)
880905
{
@@ -905,6 +930,7 @@ static int esp32_wifi_dev_init(const struct device *dev)
905930

906931
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
907932
esp_err_t ret = esp_wifi_init(&config);
933+
esp_wifi_set_mode(ESP32_WIFI_MODE_NULL);
908934

909935
if (ret == ESP_ERR_NO_MEM) {
910936
LOG_ERR("Not enough memory to initialize Wi-Fi.");
@@ -939,6 +965,13 @@ static const struct net_wifi_mgmt_offload esp32_api = {
939965
.wifi_iface.send = esp32_wifi_send,
940966
.wifi_mgmt_api = &esp32_wifi_mgmt,
941967
};
968+
#if defined(CONFIG_ESP32_WIFI_AP_STA_MODE)
969+
static const struct net_wifi_mgmt_offload esp32_api_ap = {
970+
.wifi_iface.iface_api.init = esp32_wifi_init_ap,
971+
.wifi_iface.send = esp32_wifi_send,
972+
.wifi_mgmt_api = &esp32_wifi_mgmt,
973+
};
974+
#endif
942975

943976
NET_DEVICE_DT_INST_DEFINE(0,
944977
esp32_wifi_dev_init, NULL,
@@ -950,7 +983,7 @@ NET_DEVICE_DT_INST_DEFINE(0,
950983
NET_DEVICE_DT_INST_DEFINE(1,
951984
NULL, NULL,
952985
&esp32_ap_sta_data, NULL, CONFIG_WIFI_INIT_PRIORITY,
953-
&esp32_api, ETHERNET_L2,
986+
&esp32_api_ap, ETHERNET_L2,
954987
NET_L2_GET_CTX_TYPE(ETHERNET_L2), NET_ETH_MTU);
955988

956989
DEFINE_WIFI_NM_INSTANCE(esp32_wifi_nm, &esp32_wifi_mgmt);

0 commit comments

Comments
 (0)