wificfg bugfix and enhancements

This commit is contained in:
Johan Kanflo 2017-10-10 15:53:36 +02:00
parent e3403eb5db
commit d05a637c5d
3 changed files with 62 additions and 17 deletions

View file

@ -89,6 +89,7 @@ void user_init(void)
printf("SDK version:%s\n", sdk_system_get_sdk_version()); printf("SDK version:%s\n", sdk_system_get_sdk_version());
sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM); sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM);
if (!wificfg_init(80, dispatch_list)) {
wificfg_init(80, dispatch_list); printf("Failed to start AP\n");
}
} }

View file

@ -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_<macaddr>
* 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_ssid = NULL;
char *wifi_sta_password = NULL; char *wifi_sta_password = NULL;
char *wifi_ap_ssid = NULL; char *wifi_ap_ssid = NULL;
char *wifi_ap_password = NULL; char *wifi_ap_password = NULL;
bool ap_started = false;
uint32_t base_addr; uint32_t base_addr;
uint32_t num_sectors; uint32_t num_sectors;
if (sysparam_get_info(&base_addr, &num_sectors) != SYSPARAM_OK) { if (sysparam_get_info(&base_addr, &num_sectors) != SYSPARAM_OK) {
printf("Warning: WiFi config, sysparam not initialized\n"); printf("Warning: WiFi config, sysparam not initialized\n");
return; return false;
} }
sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid); 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_ap_password", &wifi_ap_password);
sysparam_get_string("wifi_sta_ssid", &wifi_sta_ssid); 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); sysparam_get_string("wifi_sta_password", &wifi_sta_password);
int8_t wifi_sta_enable = 1; int8_t wifi_sta_enable = 1;
@ -1819,7 +1855,7 @@ void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch)
/* Validate the configuration. */ /* Validate the configuration. */
if (wifi_sta_enable && (!wifi_sta_ssid || !wifi_sta_password || 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 || strlen(wifi_sta_ssid) > 32 ||
!wifi_sta_password || !wifi_sta_password ||
strlen(wifi_sta_password) < 8 || 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 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 || The SSID must be at least 8 characters, if not we will get an
!wifi_ap_password || strlen(wifi_ap_ssid) < 8 || strlen(wifi_ap_password) >= 64) { insecure AP named ESP_<macaddr> */
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; wifi_ap_enable = 0;
} }
} }
@ -1856,8 +1894,10 @@ void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch)
wifi_mode = STATIONAP_MODE; wifi_mode = STATIONAP_MODE;
else if (wifi_sta_enable) else if (wifi_sta_enable)
wifi_mode = STATION_MODE; wifi_mode = STATION_MODE;
else else if (wifi_ap_enable)
wifi_mode = SOFTAP_MODE; wifi_mode = SOFTAP_MODE;
else
printf("Warning: No AP/STA enabled by wificfg.\n");
sdk_wifi_set_opmode(wifi_mode); sdk_wifi_set_opmode(wifi_mode);
if (wifi_sta_enable) { 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_ssid) free(wifi_ap_ssid);
if (wifi_ap_password) free(wifi_ap_password); if (wifi_ap_password) free(wifi_ap_password);
if (wifi_mode != NULL_MODE) {
server_params *params = malloc(sizeof(server_params)); server_params *params = malloc(sizeof(server_params));
params->port = port; params->port = port;
params->wificfg_dispatch = wificfg_dispatch_list; params->wificfg_dispatch = wificfg_dispatch_list;
params->dispatch = dispatch; params->dispatch = dispatch;
xTaskCreate(server_task, "WiFi Cfg HTTP", 464, params, 2, NULL); xTaskCreate(server_task, "WiFi Cfg HTTP", 464, params, 2, NULL);
ap_started = true;
}
return ap_started;
} }

View file

@ -88,9 +88,10 @@ typedef struct {
/* /*
* Start the Wifi Configuration http server task. The IP port number * Start the Wifi Configuration http server task. The IP port number
* and a path dispatch list are needed. The dispatch list can not be * 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 * Support for reading a form name or value from the socket. The name or value