@@ -492,8 +492,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
492
492
switch (event_id ) {
493
493
494
494
case WIFI_EVENT_STA_START :
495
- ESP_LOGI (TAG , "station started and disabled softap mode" );
496
- softap_started = 0 ;
495
+ ESP_LOGI (TAG , "station started" );
497
496
sta_init_flag = 1 ;
498
497
break ;
499
498
@@ -1281,7 +1280,8 @@ int process_disconnect(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
1281
1280
esp_wifi_deauthenticate_internal (cmd_disconnect -> reason_code );
1282
1281
}
1283
1282
} else if (if_type == ESP_AP_IF ) {
1284
- if (softap_started && (wifi_mode == WIFI_MODE_AP || wifi_mode == WIFI_MODE_APSTA )) {
1283
+ if (softap_started &&
1284
+ (wifi_mode == WIFI_MODE_AP || wifi_mode == WIFI_MODE_APSTA )) {
1285
1285
ieee80211_delete_node (cmd_disconnect -> mac );
1286
1286
}
1287
1287
}
@@ -1528,43 +1528,40 @@ int process_deinit_interface(uint8_t if_type, uint8_t *payload, uint16_t payload
1528
1528
int process_init_interface (uint8_t if_type , uint8_t * payload , uint16_t payload_len )
1529
1529
{
1530
1530
esp_err_t ret = ESP_OK ;
1531
- wifi_mode_t mode = WIFI_MODE_STA ;
1532
1531
uint16_t cmd_status = CMD_RESPONSE_FAIL ;
1533
1532
1534
- if (!sta_init_flag ) {
1535
-
1533
+ if (!sta_init_flag || (if_type == ESP_AP_IF && !softap_started )) {
1536
1534
/* Register to get events from wifi driver */
1537
1535
esp_create_wifi_event_loop ();
1538
1536
1539
- /* we will use this mac for both ap and station mode */
1537
+ /* Use same MAC for AP and STA */
1540
1538
esp_read_mac (dev_mac , ESP_MAC_WIFI_STA );
1541
- if (if_type == ESP_STA_IF ) {
1542
- mode = WIFI_MODE_STA ;
1539
+
1540
+ if (if_type == ESP_AP_IF ) {
1541
+ ESP_GOTO_ON_ERROR (esp_wifi_disconnect (), done , TAG , "Station Disconnect failed" );
1542
+ ESP_GOTO_ON_ERROR (esp_wifi_set_mode (WIFI_MODE_APSTA ), done , TAG , "Setting mode to APSTA failed" );
1543
+ ESP_LOGI (TAG , "Setting APSTA mode" );
1544
+ wifi_config_t wifi_config = {0 };
1545
+ ESP_GOTO_ON_ERROR (esp_wifi_set_config (WIFI_IF_STA , & wifi_config ), done , TAG , "Set config for sta failed" );
1546
+ } else if (if_type == ESP_STA_IF ) {
1547
+ ESP_LOGI (TAG , "Setting STA mode" );
1548
+ ESP_GOTO_ON_ERROR (esp_wifi_set_mode (WIFI_MODE_STA ), done , TAG , "Setting mode to STA failed" );
1543
1549
} else {
1544
- mode = WIFI_MODE_APSTA ;
1550
+ ESP_LOGE (TAG , "Invalid interface type" );
1551
+ goto done ;
1545
1552
}
1546
1553
1547
- ret = esp_wifi_set_mode ( mode );
1554
+ ESP_GOTO_ON_ERROR ( esp_wifi_start (), done , TAG , "Failed to start Wi-Fi" );
1548
1555
1549
- if (ret ) {
1550
- ESP_LOGE (TAG , "Failed to set wifi mode\n" );
1551
- goto DONE ;
1552
- }
1553
- ret = esp_wifi_start ();
1554
- if (ret ) {
1555
- ESP_LOGE (TAG , "Failed to start wifi\n" );
1556
- goto DONE ;
1557
- }
1558
1556
}
1559
1557
cmd_status = CMD_RESPONSE_SUCCESS ;
1560
- DONE :
1558
+ done :
1561
1559
ret = send_command_resp (if_type , CMD_INIT_INTERFACE , cmd_status , NULL , 0 , 0 );
1562
- if (ret ) {
1560
+ if (ret != ESP_OK ) {
1563
1561
deinitialize_wifi ();
1564
1562
}
1565
1563
1566
1564
return ret ;
1567
-
1568
1565
}
1569
1566
1570
1567
int process_get_mac (uint8_t if_type )
@@ -1806,17 +1803,19 @@ int process_set_mode(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
1806
1803
if (ret ) {
1807
1804
ESP_LOGE (TAG , "Failed to stop wifi\n" );
1808
1805
}
1809
-
1806
+
1810
1807
if (mode -> mode == WIFI_MODE_AP || mode -> mode == WIFI_MODE_APSTA ) {
1811
1808
ESP_LOGI (TAG , "Setting APSTA mode" );
1812
1809
ESP_GOTO_ON_ERROR (esp_wifi_set_mac (WIFI_IF_STA , dummy_mac ), send_err , TAG , "Setting MAC on STA failed" );
1813
1810
ESP_GOTO_ON_ERROR (esp_wifi_set_mode (WIFI_MODE_APSTA ), send_err , TAG , "Setting mode to APSTA failed" );
1814
1811
ESP_GOTO_ON_ERROR (esp_wifi_set_mac (WIFI_IF_AP , dev_mac ), send_err , TAG , "Setting MAC on AP failed" );
1812
+ wifi_config_t wifi_config = {0 };
1813
+ ESP_GOTO_ON_ERROR (esp_wifi_set_config (WIFI_IF_STA , & wifi_config ), send_err , TAG , "Set config for sta failed" );
1815
1814
} else if (mode -> mode == WIFI_MODE_STA ) {
1816
1815
ESP_LOGI (TAG , "Setting STA mode" );
1817
1816
ESP_GOTO_ON_ERROR (esp_wifi_set_mac (WIFI_IF_AP , dummy_mac2 ), send_err , TAG , "Setting MAC on AP failed" );
1818
1817
ESP_GOTO_ON_ERROR (esp_wifi_set_mode (WIFI_MODE_STA ), send_err , TAG , "Setting mode to STA failed" );
1819
- ESP_GOTO_ON_ERROR (esp_wifi_set_mac (WIFI_IF_STA , dev_mac ), send_err , TAG , "Setting MAC on STA failed" );
1818
+ ESP_ERROR_CHECK (esp_wifi_set_mac (WIFI_IF_STA , dev_mac ));
1820
1819
}
1821
1820
1822
1821
ESP_GOTO_ON_ERROR (esp_wifi_start (), send_err , TAG , "Cannot start wifi" );
@@ -1949,8 +1948,8 @@ int process_set_ap_config(uint8_t if_type, uint8_t *payload, uint16_t payload_le
1949
1948
1950
1949
ret = esp_wifi_stop ();
1951
1950
1952
- if (old_mode != WIFI_MODE_AP ) {
1953
- ret = esp_wifi_set_mode (WIFI_MODE_AP );
1951
+ if (old_mode != WIFI_MODE_APSTA ) {
1952
+ ret = esp_wifi_set_mode (WIFI_MODE_APSTA );
1954
1953
}
1955
1954
esp_wifi_set_config (WIFI_IF_AP , & wifi_config );
1956
1955
0 commit comments