Skip to content

Commit 6c6cd32

Browse files
committed
improved STA connection handling
1 parent 4d453ba commit 6c6cd32

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

wifi_manager/main/http_server.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,11 @@ void http_server_netconn_serve(struct netconn *conn) {
384384
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
385385
}
386386
else if(strstr(line, "GET /ap.json ")) {
387-
if(wifi_scan_lock_json_buffer()){
387+
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
388388
netconn_write(conn, http_json_hdr, sizeof(http_json_hdr) - 1, NETCONN_NOCOPY);
389389
char *buff = wifi_manager_get_ap_list_json();
390390
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
391-
wifi_scan_unlock_json_buffer();
391+
wifi_manager_unlock_json_buffer();
392392
}
393393
else{
394394
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
@@ -420,12 +420,12 @@ void http_server_netconn_serve(struct netconn *conn) {
420420
netconn_write(conn, wifi3_png_start, wifi3_png_end - wifi3_png_start, NETCONN_NOCOPY);
421421
}
422422
else if(strstr(line, "GET /status ")){
423-
if(wifi_scan_lock_json_buffer()){
423+
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
424424
char *buff = wifi_manager_get_ip_info_json();
425425
if(buff){
426426
netconn_write(conn, http_json_hdr, sizeof(http_json_hdr) - 1, NETCONN_NOCOPY);
427427
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
428-
wifi_scan_unlock_json_buffer();
428+
wifi_manager_unlock_json_buffer();
429429
}
430430
else{
431431
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);

wifi_manager/main/wifi_manager.c

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,22 @@ uint8_t wifi_manager_fetch_wifi_sta_config(){
128128

129129
void 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

142149
void 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");

wifi_manager/main/wifi_manager.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,18 @@ void wifi_manager_connect_async();
139139
* The mutex is used by both the access point list json and the connection status json.\n
140140
* These two resources should technically have their own mutex but we lose some flexibility to save
141141
* on memory.
142+
*
143+
* This is a simple wrapper around freeRTOS function xSemaphoreTake.
144+
*
145+
* @param xTicksToWait The time in ticks to wait for the semaphore to become available.
146+
* @return true in success, false otherwise.
142147
*/
143-
bool wifi_scan_lock_json_buffer();
148+
bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait);
144149

145150
/**
146151
* @brief Releases the json buffer mutex.
147152
*/
148-
void wifi_scan_unlock_json_buffer();
153+
void wifi_manager_unlock_json_buffer();
149154

150155
void wifi_manager_generate_ip_info_json();
151156
void wifi_manager_generate_acess_points_json();

0 commit comments

Comments
 (0)