diff --git a/CMakeLists.txt b/CMakeLists.txt index 74ea43d5..f9e3e3b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,11 +2,11 @@ if(IDF_VERSION_MAJOR GREATER_EQUAL 4) idf_component_register(SRC_DIRS src REQUIRES log nvs_flash mdns wpa_supplicant lwip esp_http_server INCLUDE_DIRS src - EMBED_FILES src/style.css src/code.js src/index.html) + EMBED_FILES src/web/style.css src/web/code.js src/web/index.html) else() set(COMPONENT_SRCDIRS src) set(COMPONENT_ADD_INCLUDEDIRS src) set(COMPONENT_REQUIRES log nvs_flash mdns wpa_supplicant lwip esp_http_server) - set(COMPONENT_EMBED_FILES src/style.css src/code.js src/index.html) + set(COMPONENT_EMBED_FILES src/web/style.css src/web/code.js src/web/index.html) register_component() endif() diff --git a/Kconfig b/Kconfig index 59c5a9a8..3ab7b59a 100644 --- a/Kconfig +++ b/Kconfig @@ -1,5 +1,11 @@ menu "Wifi Manager Configuration" +config WIFI_MANAGER_DNS_SERVER_ENABLE + bool "Enable the DNS server if set" + default y + help + Enables the DNS Server when set to true. + config WIFI_MANAGER_TASK_PRIORITY int "RTOS Task Priority for the wifi_manager" default 5 diff --git a/component.mk b/component.mk index 75e34089..02acbff8 100644 --- a/component.mk +++ b/component.mk @@ -1,4 +1,4 @@ COMPONENT_ADD_INCLUDEDIRS = src COMPONENT_SRCDIRS = src COMPONENT_DEPENDS = log esp_http_server -COMPONENT_EMBED_FILES := src/style.css src/code.js src/index.html +COMPONENT_EMBED_FILES := src/web/style.css src/web/code.js src/web/index.html diff --git a/src/lock.svg b/src/assets/lock.svg similarity index 100% rename from src/lock.svg rename to src/assets/lock.svg diff --git a/src/settings.svg b/src/assets/settings.svg similarity index 100% rename from src/settings.svg rename to src/assets/settings.svg diff --git a/src/wifi0.svg b/src/assets/wifi0.svg similarity index 100% rename from src/wifi0.svg rename to src/assets/wifi0.svg diff --git a/src/wifi1.svg b/src/assets/wifi1.svg similarity index 100% rename from src/wifi1.svg rename to src/assets/wifi1.svg diff --git a/src/wifi2.svg b/src/assets/wifi2.svg similarity index 100% rename from src/wifi2.svg rename to src/assets/wifi2.svg diff --git a/src/wifi3.svg b/src/assets/wifi3.svg similarity index 100% rename from src/wifi3.svg rename to src/assets/wifi3.svg diff --git a/src/connect b/src/connect deleted file mode 100644 index 8c7fe211..00000000 --- a/src/connect +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/src/ap.json b/src/examples/ap.json similarity index 100% rename from src/ap.json rename to src/examples/ap.json diff --git a/src/status b/src/examples/status.json similarity index 100% rename from src/status rename to src/examples/status.json diff --git a/src/code.js b/src/web/code.js similarity index 100% rename from src/code.js rename to src/web/code.js diff --git a/src/compress.bat b/src/web/compress.bat similarity index 100% rename from src/compress.bat rename to src/web/compress.bat diff --git a/src/index.html b/src/web/index.html similarity index 100% rename from src/index.html rename to src/web/index.html diff --git a/src/style.css b/src/web/style.css similarity index 100% rename from src/style.css rename to src/web/style.css diff --git a/src/wifi_manager.c b/src/wifi_manager.c index 3d11a940..188c2b31 100644 --- a/src/wifi_manager.c +++ b/src/wifi_manager.c @@ -53,6 +53,7 @@ Contains the freeRTOS task and all necessary support #include "lwip/err.h" #include "lwip/netdb.h" #include "lwip/ip4_addr.h" +#include "sdkconfig.h" #include "json.h" @@ -60,7 +61,11 @@ Contains the freeRTOS task and all necessary support #include "nvs_sync.h" #include "wifi_manager.h" - +#ifdef CONFIG_WIFI_MANAGER_DNS_SERVER_ENABLE + uint8_t dns_server_enable = 1; +#else + uint8_t dns_server_enable = 0; +#endif /* objects used to manipulate the main queue of events */ QueueHandle_t wifi_manager_queue; @@ -77,6 +82,7 @@ SemaphoreHandle_t wifi_manager_json_mutex = NULL; SemaphoreHandle_t wifi_manager_sta_ip_mutex = NULL; char *wifi_manager_sta_ip = NULL; uint16_t ap_num = MAX_AP_NUM; +uint16_t ap_users = 0; wifi_ap_record_t *accessp_records; char *accessp_json = NULL; char *ip_info_json = NULL; @@ -685,6 +691,14 @@ static void wifi_manager_event_handler(void* arg, esp_event_base_t event_base, i * to do something, for example, to get the info of the connected STA, etc. */ case WIFI_EVENT_AP_STACONNECTED: ESP_LOGI(TAG, "WIFI_EVENT_AP_STACONNECTED"); + xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT); + + if(xTimerIsTimerActive(wifi_manager_retry_timer) == pdTRUE) + xTimerStop(wifi_manager_retry_timer, (TickType_t)0); + + xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT); + ap_users += 1; + ESP_LOGI(TAG, "WIFI_EVENT_AP_STACONNECTED, connected users: %d", ap_users); break; /* This event can happen in the following scenarios: @@ -695,6 +709,17 @@ static void wifi_manager_event_handler(void* arg, esp_event_base_t event_base, i * something, e.g., close the socket which is related to this station, etc. */ case WIFI_EVENT_AP_STADISCONNECTED: ESP_LOGI(TAG, "WIFI_EVENT_AP_STADISCONNECTED"); + if (ap_users > 0){ + ap_users -= 1; + } + if (ap_users == 0) { + xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT); + EventBits_t uxBits = xEventGroupGetBits(wifi_manager_event_group); + if(! (uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT) ) { + wifi_manager_send_message(WM_ORDER_LOAD_AND_RESTORE_STA, NULL); + } + } + ESP_LOGI(TAG, "WIFI_EVENT_AP_STADISCONNECTED: Remaining connected users: %d", ap_users); break; /* This event is disabled by default. The application can enable it via API esp_wifi_set_event_mask(). @@ -1171,7 +1196,7 @@ void wifi_manager( void * pvParameters ){ /* start SoftAP */ wifi_manager_send_message(WM_ORDER_START_AP, NULL); } - else{ + else if (! (uxBits & WIFI_MANAGER_AP_STA_CONNECTED_BIT)){ /* lost connection ? */ if(wifi_manager_lock_json_buffer( portMAX_DELAY )){ wifi_manager_generate_ip_info_json( UPDATE_LOST_CONNECTION ); @@ -1200,6 +1225,8 @@ void wifi_manager( void * pvParameters ){ wifi_manager_send_message(WM_ORDER_START_AP, NULL); } } + } else { + ESP_LOGI(TAG, "Disconnected"); } /* callback */ @@ -1218,7 +1245,9 @@ void wifi_manager( void * pvParameters ){ http_app_start(true); /* start DNS */ - dns_server_start(); + if(dns_server_enable) { + dns_server_start(); + } /* callback */ if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL); @@ -1240,7 +1269,9 @@ void wifi_manager( void * pvParameters ){ esp_wifi_set_mode(WIFI_MODE_STA); /* stop DNS */ - dns_server_stop(); + if(dns_server_enable) { + dns_server_stop(); + } /* restart HTTP daemon */ http_app_stop(); @@ -1283,7 +1314,9 @@ void wifi_manager( void * pvParameters ){ else { abort(); } /* bring down DNS hijack */ - dns_server_stop(); + if(dns_server_enable) { + dns_server_stop(); + } /* start the timer that will eventually shutdown the access point * We check first that it's actually running because in case of a boot and restore connection