@@ -556,7 +556,8 @@ static void wifi_manager_event_handler(void* arg, esp_event_base_t event_base, i
556556 case WIFI_EVENT_SCAN_DONE :
557557 ESP_LOGD (TAG , "WIFI_EVENT_SCAN_DONE" );
558558 xEventGroupClearBits (wifi_manager_event_group , WIFI_MANAGER_SCAN_BIT );
559- wifi_manager_send_message (WM_EVENT_SCAN_DONE , NULL );
559+ wifi_event_sta_scan_done_t event_sta_scan_done = * ((wifi_event_sta_scan_done_t * )event_data );
560+ wifi_manager_send_message (WM_EVENT_SCAN_DONE , & event_sta_scan_done );
560561 break ;
561562
562563 /* If esp_wifi_start() returns ESP_OK and the current Wi-Fi mode is Station or AP+Station, then this event will
@@ -959,25 +960,29 @@ void wifi_manager( void * pvParameters ){
959960 if ( xStatus == pdPASS ){
960961 switch (msg .code ){
961962
962- case WM_EVENT_SCAN_DONE :
963- /* As input param, it stores max AP number ap_records can hold. As output param, it receives the actual AP number this API returns.
964- * As a consequence, ap_num MUST be reset to MAX_AP_NUM at every scan */
965- ap_num = MAX_AP_NUM ;
966- ESP_ERROR_CHECK (esp_wifi_scan_get_ap_records (& ap_num , accessp_records ));
967- /* make sure the http server isn't trying to access the list while it gets refreshed */
968- if (wifi_manager_lock_json_buffer ( pdMS_TO_TICKS (1000 ) )){
969- /* Will remove the duplicate SSIDs from the list and update ap_num */
970- wifi_manager_filter_unique (accessp_records , & ap_num );
971- wifi_manager_generate_acess_points_json ();
972- wifi_manager_unlock_json_buffer ();
973- }
974- else {
975- ESP_LOGE (TAG , "could not get access to json mutex in wifi_scan" );
963+ case WM_EVENT_SCAN_DONE :{
964+ wifi_event_sta_scan_done_t * evt_scan_done = (wifi_event_sta_scan_done_t * )msg .param ;
965+ /* only check for AP if the scan is succesful */
966+ if (evt_scan_done -> status == 0 ){
967+ /* As input param, it stores max AP number ap_records can hold. As output param, it receives the actual AP number this API returns.
968+ * As a consequence, ap_num MUST be reset to MAX_AP_NUM at every scan */
969+ ap_num = MAX_AP_NUM ;
970+ ESP_ERROR_CHECK (esp_wifi_scan_get_ap_records (& ap_num , accessp_records ));
971+ /* make sure the http server isn't trying to access the list while it gets refreshed */
972+ if (wifi_manager_lock_json_buffer ( pdMS_TO_TICKS (1000 ) )){
973+ /* Will remove the duplicate SSIDs from the list and update ap_num */
974+ wifi_manager_filter_unique (accessp_records , & ap_num );
975+ wifi_manager_generate_acess_points_json ();
976+ wifi_manager_unlock_json_buffer ();
977+ }
978+ else {
979+ ESP_LOGE (TAG , "could not get access to json mutex in wifi_scan" );
980+ }
976981 }
977982
978983 /* callback */
979984 if (cb_ptr_arr [msg .code ]) (* cb_ptr_arr [msg .code ])(NULL );
980-
985+ }
981986 break ;
982987
983988 case WM_ORDER_START_WIFI_SCAN :
@@ -1030,6 +1035,12 @@ void wifi_manager( void * pvParameters ){
10301035 if ( ! (uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT ) ){
10311036 /* update config to latest and attempt connection */
10321037 ESP_ERROR_CHECK (esp_wifi_set_config (ESP_IF_WIFI_STA , wifi_manager_get_wifi_sta_config ()));
1038+
1039+ /* if there is a wifi scan in progress abort it first
1040+ Calling esp_wifi_scan_stop will trigger a SCAN_DONE event which will reset this bit */
1041+ if (uxBits & WIFI_MANAGER_SCAN_BIT ){
1042+ esp_wifi_scan_stop ();
1043+ }
10331044 ESP_ERROR_CHECK (esp_wifi_connect ());
10341045 }
10351046
0 commit comments