diff --git a/examples/wificfg/wificfg.c b/examples/wificfg/wificfg.c index a90a9da..e7b3baa 100644 --- a/examples/wificfg/wificfg.c +++ b/examples/wificfg/wificfg.c @@ -89,6 +89,7 @@ void user_init(void) printf("SDK version:%s\n", sdk_system_get_sdk_version()); sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM); - - wificfg_init(80, dispatch_list); + if (!wificfg_init(80, dispatch_list)) { + printf("Failed to start AP\n"); + } } diff --git a/extras/wificfg/wificfg.c b/extras/wificfg/wificfg.c index 708af29..d9ed290 100644 --- a/extras/wificfg/wificfg.c +++ b/extras/wificfg/wificfg.c @@ -1775,24 +1775,60 @@ static void dns_task(void *pvParameters) } } +/** + * @brief Sanetize SSID. If configuring an AP containing the characters in + * 'illegals', the ESP will start an insecure AP named ESP_ + * Any illegal characters will be replaced by _ + * + * @param ssid the SSID + * + * @return true if name got sanetized. + */ +static bool sanetize_ssid(char *ssid) +{ + bool sanetized = false; + char *illegals = "+-<> "; // There might me more characters that are illegal + uint8_t num_illegals = strlen(illegals); + if (!ssid) { + return sanetized; + } + while(*ssid) { + for (uint32_t i = 0; i < num_illegals; i++) { + if (*ssid == illegals[i]) { + *ssid = '_'; + sanetized = true; + break; + } + } + ssid++; + } + return sanetized; +} -void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch) +bool wificfg_init(uint32_t port, const wificfg_dispatch *dispatch) { char *wifi_sta_ssid = NULL; char *wifi_sta_password = NULL; char *wifi_ap_ssid = NULL; char *wifi_ap_password = NULL; + bool ap_started = false; uint32_t base_addr; uint32_t num_sectors; if (sysparam_get_info(&base_addr, &num_sectors) != SYSPARAM_OK) { printf("Warning: WiFi config, sysparam not initialized\n"); - return; + return false; } sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid); + if (sanetize_ssid(wifi_ap_ssid)) { + sysparam_set_string("wifi_ap_ssid", wifi_ap_ssid); + } sysparam_get_string("wifi_ap_password", &wifi_ap_password); sysparam_get_string("wifi_sta_ssid", &wifi_sta_ssid); + if (sanetize_ssid(wifi_sta_ssid)) { + sysparam_set_string("wifi_sta_ssid", wifi_sta_ssid); + } sysparam_get_string("wifi_sta_password", &wifi_sta_password); int8_t wifi_sta_enable = 1; @@ -1819,7 +1855,7 @@ void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch) /* Validate the configuration. */ if (wifi_sta_enable && (!wifi_sta_ssid || !wifi_sta_password || - strlen(wifi_sta_ssid) < 1 || + strlen(wifi_sta_ssid) < 8 || strlen(wifi_sta_ssid) > 32 || !wifi_sta_password || strlen(wifi_sta_password) < 8 || @@ -1844,9 +1880,11 @@ void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch) } } - /* If the ssid and password are not valid then disable the AP interface. */ - if (!wifi_ap_ssid || strlen(wifi_ap_ssid) < 1 || strlen(wifi_ap_ssid) >= 32 || - !wifi_ap_password || strlen(wifi_ap_ssid) < 8 || strlen(wifi_ap_password) >= 64) { + /* If the ssid and password are not valid then disable the AP interface. + The SSID must be at least 8 characters, if not we will get an + insecure AP named ESP_ */ + if (!wifi_ap_ssid || strlen(wifi_ap_ssid) < 8 || strlen(wifi_ap_ssid) >= 32 || + !wifi_ap_password || strlen(wifi_ap_password) < 8 || strlen(wifi_ap_password) >= 64) { wifi_ap_enable = 0; } } @@ -1856,8 +1894,10 @@ void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch) wifi_mode = STATIONAP_MODE; else if (wifi_sta_enable) wifi_mode = STATION_MODE; - else + else if (wifi_ap_enable) wifi_mode = SOFTAP_MODE; + else + printf("Warning: No AP/STA enabled by wificfg.\n"); sdk_wifi_set_opmode(wifi_mode); if (wifi_sta_enable) { @@ -2013,10 +2053,13 @@ void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch) if (wifi_ap_ssid) free(wifi_ap_ssid); if (wifi_ap_password) free(wifi_ap_password); - server_params *params = malloc(sizeof(server_params)); - params->port = port; - params->wificfg_dispatch = wificfg_dispatch_list; - params->dispatch = dispatch; - - xTaskCreate(server_task, "WiFi Cfg HTTP", 464, params, 2, NULL); + if (wifi_mode != NULL_MODE) { + server_params *params = malloc(sizeof(server_params)); + params->port = port; + params->wificfg_dispatch = wificfg_dispatch_list; + params->dispatch = dispatch; + xTaskCreate(server_task, "WiFi Cfg HTTP", 464, params, 2, NULL); + ap_started = true; + } + return ap_started; } diff --git a/extras/wificfg/wificfg.h b/extras/wificfg/wificfg.h index 705b2f6..5dae061 100644 --- a/extras/wificfg/wificfg.h +++ b/extras/wificfg/wificfg.h @@ -88,9 +88,10 @@ typedef struct { /* * Start the Wifi Configuration http server task. The IP port number * and a path dispatch list are needed. The dispatch list can not be - * stack allocated as it is passed to another task. + * stack allocated as it is passed to another task. Returns true if the + * selected AP was successfully started. */ -void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch); +bool wificfg_init(uint32_t port, const wificfg_dispatch *dispatch); /* * Support for reading a form name or value from the socket. The name or value