@@ -128,15 +128,22 @@ uint8_t wifi_manager_fetch_wifi_sta_config(){
128128
129129void wifi_manager_generate_ip_info_json (){
130130
131+ EventBits_t uxBits = xEventGroupGetBits (wifi_manager_event_group );
132+ if (uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT ){
133+ tcpip_adapter_ip_info_t ip_info ;
134+ ESP_ERROR_CHECK (tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_STA , & ip_info ));
135+
136+ const char ip_info_json_format [] = "{\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\"}\n" ;
137+ sprintf (ip_info_json , ip_info_json_format ,
138+ ip4addr_ntoa (& ip_info .ip ),
139+ ip4addr_ntoa (& ip_info .netmask ),
140+ ip4addr_ntoa (& ip_info .gw ));
141+ }
142+ else {
143+ strcpy (ip_info_json , "{}\n" );
144+ }
131145
132- tcpip_adapter_ip_info_t ip_info ;
133- ESP_ERROR_CHECK (tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_STA , & ip_info ));
134146
135- const char ip_info_json_format [] = "{\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\"}\n" ;
136- sprintf (ip_info_json , ip_info_json_format ,
137- ip4addr_ntoa (& ip_info .ip ),
138- ip4addr_ntoa (& ip_info .netmask ),
139- ip4addr_ntoa (& ip_info .gw ));
140147}
141148
142149void wifi_manager_generate_acess_points_json (){
@@ -164,9 +171,9 @@ void wifi_manager_generate_acess_points_json(){
164171}
165172
166173
167- bool wifi_scan_lock_json_buffer ( ){
174+ bool wifi_manager_lock_json_buffer ( TickType_t xTicksToWait ){
168175 if (wifi_manager_json_mutex ){
169- if ( xSemaphoreTake ( wifi_manager_json_mutex , ( TickType_t ) 10 ) == pdTRUE ) {
176+ if ( xSemaphoreTake ( wifi_manager_json_mutex , xTicksToWait ) == pdTRUE ) {
170177 return true;
171178 }
172179 else {
@@ -178,7 +185,7 @@ bool wifi_scan_lock_json_buffer(){
178185 }
179186
180187}
181- void wifi_scan_unlock_json_buffer (){
188+ void wifi_manager_unlock_json_buffer (){
182189 xSemaphoreGive ( wifi_manager_json_mutex );
183190}
184191
@@ -214,6 +221,7 @@ esp_err_t wifi_manager_event_handler(void *ctx, system_event_t *event)
214221 case SYSTEM_EVENT_STA_DISCONNECTED :
215222 xEventGroupSetBits (wifi_manager_event_group , WIFI_MANAGER_STA_DISCONNECT_BIT );
216223 xEventGroupClearBits (wifi_manager_event_group , WIFI_MANAGER_WIFI_CONNECTED_BIT );
224+ //clear json
217225 break ;
218226
219227 default :
@@ -424,26 +432,34 @@ void wifi_manager( void * pvParameters ){
424432 ESP_ERROR_CHECK (esp_wifi_connect ());
425433
426434 /* 2 scenarios here: connection is successful and SYSTEM_EVENT_STA_GOT_IP will be posted
427- * or it's a failure and we get a SYSTEM_EVENT_STA_DISCONNECTED with a reason code
435+ * or it's a failure and we get a SYSTEM_EVENT_STA_DISCONNECTED with a reason code.
436+ * Note that the reason code is not exploited. For all intent and purposes a failure is a failure.
428437 */
429438 uxBits = xEventGroupWaitBits (wifi_manager_event_group , WIFI_MANAGER_WIFI_CONNECTED_BIT | WIFI_MANAGER_STA_DISCONNECT_BIT , pdFALSE , pdFALSE , portMAX_DELAY );
430- if (uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT ){
431- /* save IP address */
432- ESP_ERROR_CHECK (tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_STA , & ip_info ));
433- printf ("IP Address: %s\n" , ip4addr_ntoa (& ip_info .ip ));
434- printf ("Subnet mask: %s\n" , ip4addr_ntoa (& ip_info .netmask ));
435- printf ("Gateway: %s\n" , ip4addr_ntoa (& ip_info .gw ));
436439
437- }
438- else if (uxBits & WIFI_MANAGER_STA_DISCONNECT_BIT ){
439- printf ("Unable to connect. Check password and make sure wifi has sufficient signal strength\n" );
440+ if (uxBits & (WIFI_MANAGER_WIFI_CONNECTED_BIT | WIFI_MANAGER_STA_DISCONNECT_BIT )){
440441
442+ /* Update the json regardless of connection status.
443+ * If connection was succesful an IP will get assigned.
444+ */
445+ if (wifi_manager_lock_json_buffer ( portMAX_DELAY )){
446+ wifi_manager_generate_ip_info_json ();
447+ wifi_manager_unlock_json_buffer ();
448+ }
449+ else {
450+ /* Even if someone were to furiously refresh a web resource that needs the json mutex,
451+ * it seems impossible that this thread cannot obtain the mutex. Abort here is reasonnable.
452+ */
453+ abort ();
454+ }
441455 }
442456 else {
443457 /* hit portMAX_DELAY limit ? */
444458 abort ();
445459 }
446- break ;
460+
461+ /* finally: release the connection request bit */
462+ xEventGroupClearBits (wifi_manager_event_group , WIFI_MANAGER_REQUEST_STA_CONNECT_BIT );
447463 }
448464 else {
449465
@@ -456,9 +472,9 @@ void wifi_manager( void * pvParameters ){
456472 ESP_ERROR_CHECK (esp_wifi_scan_get_ap_records (& ap_num , accessp_records ));
457473
458474 //make sure the http server isn't trying to access the list while it gets refreshed
459- if (wifi_scan_lock_json_buffer ( )){
475+ if (wifi_manager_lock_json_buffer ( ( TickType_t ) 10 )){
460476 wifi_manager_generate_acess_points_json ();
461- wifi_scan_unlock_json_buffer ();
477+ wifi_manager_unlock_json_buffer ();
462478 }
463479 else {
464480 printf ("Could not get access to xSemaphoreScan in wifi_scan\n" );
0 commit comments