6767
6868static wifi_config_t new_wifi_config = {0 };
6969static bool new_config_recvd = false;
70+ static bool suppress_disconnect = false; // true when we want to suppress the disconnect event
7071static wifi_event_sta_connected_t lkg_sta_connected_event = {0 };
7172
7273enum {
@@ -871,10 +872,10 @@ static void event_handler_wifi(void* arg, esp_event_base_t event_base,
871872 int ret = esp_wifi_set_config (WIFI_IF_STA , & new_wifi_config );
872873 if (ret ) {
873874 ESP_LOGE (TAG , "Error[0x%x] while setting the wifi config" , ret );
874- } else {
875- new_config_recvd = 0 ;
876875 }
877876 esp_wifi_disconnect ();
877+ // suppress the disconnect event since we force disconnect here
878+ suppress_disconnect = true;
878879 return ;
879880 }
880881 station_connecting = false;
@@ -884,22 +885,31 @@ static void event_handler_wifi(void* arg, esp_event_base_t event_base,
884885 esp_wifi_internal_reg_rxcb (WIFI_IF_STA , (wifi_rxcb_t ) wlan_sta_rx_callback );
885886 station_connected = true;
886887 } else if (event_id == WIFI_EVENT_STA_DISCONNECTED ) {
887- station_connected = false ;
888+ ESP_LOGI ( TAG , "Sta mode disconnect" ) ;
888889 if (new_config_recvd ) {
889890 ESP_LOGI (TAG , "New wifi config still unapplied, applying it" );
890891 /* Still not applied new config, so apply it */
891892 int ret = esp_wifi_set_config (WIFI_IF_STA , & new_wifi_config );
892893 if (ret ) {
893894 ESP_LOGE (TAG , "Error[0x%x] while setting the wifi config" , ret );
894895 } else {
895- new_config_recvd = 0 ;
896+ new_config_recvd = false ;
896897 }
898+ station_connecting = true;
899+ esp_wifi_connect ();
897900 }
901+ station_connected = false;
898902 esp_wifi_internal_reg_rxcb (WIFI_IF_STA , NULL );
899- ESP_LOGI (TAG , "Sta mode disconnect" );
900903 station_connecting = false;
901- send_event_data_to_host (RPC_ID__Event_StaDisconnected ,
902- event_data , sizeof (wifi_event_sta_disconnected_t ));
904+ if (!suppress_disconnect ) {
905+ send_event_data_to_host (RPC_ID__Event_StaDisconnected ,
906+ event_data , sizeof (wifi_event_sta_disconnected_t ));
907+ wifi_event_sta_disconnected_t * ptr = (wifi_event_sta_disconnected_t * )event_data ;
908+ ESP_LOGI (TAG , "disconnect due to reason: %d" , ptr -> reason );
909+ } else {
910+ ESP_LOGI (TAG , "suppressing disconnect event due to new config" );
911+ suppress_disconnect = false;
912+ }
903913#if CONFIG_SOC_WIFI_HE_SUPPORT
904914 } else if (event_id == WIFI_EVENT_ITWT_SETUP ) {
905915 ESP_LOGI (TAG , "Itwt Setup" );
@@ -1474,7 +1484,7 @@ static esp_err_t req_wifi_connect(Rpc *req, Rpc *resp, void *priv_data)
14741484
14751485 if (new_config_recvd || !station_connected ) {
14761486 ESP_LOGI (TAG , "************ connect ****************" );
1477- // station_connecting = true;
1487+ station_connecting = true;
14781488 ret = esp_wifi_connect ();
14791489 if (ret != ESP_OK ) {
14801490 ESP_LOGE (TAG , "Failed to connect to WiFi: %d" , ret );
@@ -1527,7 +1537,7 @@ static bool wifi_is_provisioned(wifi_config_t *wifi_cfg)
15271537 ESP_LOGI (TAG , "Wifi provisioned" );
15281538 return true;
15291539 }
1530- ESP_LOGI (TAG , "Wifi not provisioned, Fallback to example config " );
1540+ ESP_LOGI (TAG , "Wifi not provisioned" );
15311541
15321542 return false;
15331543}
@@ -1584,16 +1594,26 @@ esp_err_t esp_hosted_set_sta_config(wifi_interface_t iface, wifi_config_t *cfg)
15841594 ESP_LOGW (TAG , "not provisioned and failed to set wifi config" );
15851595 } else {
15861596 ESP_LOGI (TAG , "Provisioned new Wi-Fi config" );
1587- new_config_recvd = true ;
1588- station_connecting = false ;
1597+ new_config_recvd = false ;
1598+ return ESP_OK ;
15891599 }
15901600 }
15911601
15921602 if (!is_wifi_config_equal (cfg , & current_config )) {
1593- new_config_recvd = true;
1594- station_connecting = false;
1595- ESP_LOGI (TAG , "Setting new WiFi config SSID: %s" , cfg -> sta .ssid );
1596- memcpy (& new_wifi_config , cfg , sizeof (wifi_config_t ));
1603+ if (station_connecting ) {
1604+ ESP_LOGI (TAG , "Caching new WiFi config SSID: %s" , cfg -> sta .ssid );
1605+ memcpy (& new_wifi_config , cfg , sizeof (wifi_config_t ));
1606+ new_config_recvd = true;
1607+ } else {
1608+ if (esp_wifi_set_config (WIFI_IF_STA , cfg ) != ESP_OK ) {
1609+ ESP_LOGW (TAG , "already provisioned but failed to set wifi config: copying to cache instead" );
1610+ memcpy (& new_wifi_config , cfg , sizeof (wifi_config_t ));
1611+ new_config_recvd = true;
1612+ } else {
1613+ ESP_LOGI (TAG , "Setting new WiFi config SSID: %s" , cfg -> sta .ssid );
1614+ new_config_recvd = false;
1615+ }
1616+ }
15971617 } else {
15981618 ESP_LOGI (TAG , "WiFi config unchanged, keeping current connection" );
15991619 new_config_recvd = false;
@@ -2048,8 +2068,12 @@ static esp_err_t req_wifi_get_protocol(Rpc *req, Rpc *resp, void *priv_data)
20482068 RpcReqWifiGetProtocol , req_wifi_get_protocol ,
20492069 rpc__resp__wifi_get_protocol__init );
20502070
2051- uint8_t protocol_bitmap = 0 ;
2052- RPC_RET_FAIL_IF (esp_wifi_get_protocol (req_payload -> ifx , & protocol_bitmap ));
2071+ /** due to a bug in some ESP-IDF releases, esp_wifi_get_protocol() treats
2072+ * the incoming pointer as a uint16_t *, corrupting the next byte
2073+ * see https://github.com/espressif/esp-idf/issues/17502
2074+ */
2075+ uint32_t protocol_bitmap = 0 ; // for safety
2076+ RPC_RET_FAIL_IF (esp_wifi_get_protocol (req_payload -> ifx , (uint8_t * )& protocol_bitmap ));
20532077
20542078 resp_payload -> protocol_bitmap = protocol_bitmap ;
20552079 return ESP_OK ;
0 commit comments