From d234b47349e62924d10b91d2e3475484d22689cf Mon Sep 17 00:00:00 2001 From: fvanroie Date: Sun, 5 Apr 2020 21:51:13 +0200 Subject: [PATCH] Add wifi validation check --- src/hasp_http.cpp | 16 ++++---- src/hasp_oobe.cpp | 101 +++++++++++++++++++++++++++++----------------- src/hasp_wifi.cpp | 21 +++++++++- src/hasp_wifi.h | 1 + 4 files changed, 93 insertions(+), 46 deletions(-) diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index 1b76337f..f000081a 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -333,7 +333,7 @@ void webHandleFirmware() //////////////////////////////////////////////////////////////////////////////////////////////////// void httpSetup() { - if(WiFi.getMode() == WIFI_AP) { + if(WiFi.getMode() != WIFI_STA) { debugPrintln(F("HTTP: Wifi access point")); // webServer.on(F("/"), webHandleWifiConfig); } else { @@ -360,10 +360,10 @@ void httpReconnect() webServer.end(); webServerStarted = false; debugPrintln(F("HTTP: Server stoped")); - } else if(WiFi.status() == WL_CONNECTED || WiFi.getMode() == WIFI_AP) { + } else if(WiFi.status() == WL_CONNECTED || WiFi.getMode() != WIFI_STA) { /* - if(WiFi.getMode() == WIFI_AP) { + if(WiFi.getMode() != WIFI_STA) { webServer.on(F("/"), webHandleWifiConfig); webServer.on(F("/config"), webHandleConfig); webServer.onNotFound(httpHandleNotFound); @@ -374,7 +374,7 @@ void httpReconnect() webServerStarted = true; debugPrintln(String(F("HTTP: Server started @ http://")) + - (WiFi.getMode() == WIFI_AP ? WiFi.softAPIP().toString() : WiFi.localIP().toString())); + (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString() : WiFi.localIP().toString())); } } @@ -1253,7 +1253,7 @@ void webHandleConfig() } } - if(WiFi.getMode() == WIFI_AP) { + if(WiFi.getMode() != WIFI_STA) { httpHandleReboot(); } @@ -1780,7 +1780,7 @@ void webStart() webServer.begin(); webServerStarted = true; Log.notice(F("HTTP: Server started @ http://%s"), - (WiFi.getMode() == WIFI_AP ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); + (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); } void webStop() @@ -1795,7 +1795,7 @@ void httpSetup() { // httpSetConfig(settings); - if(WiFi.getMode() == WIFI_AP) { + if(WiFi.getMode() != WIFI_STA) { Log.notice(F("HTTP: Wifi access point")); webServer.on(F("/"), webHandleWifiConfig); } else { @@ -1877,7 +1877,7 @@ void httpReconnect() if(webServerStarted) { webStop(); - } else if(WiFi.status() == WL_CONNECTED || WiFi.getMode() == WIFI_AP) { + } else if(WiFi.status() == WL_CONNECTED || WiFi.getMode() != WIFI_STA) { webStart(); } } diff --git a/src/hasp_oobe.cpp b/src/hasp_oobe.cpp index 215a6213..4a061822 100644 --- a/src/hasp_oobe.cpp +++ b/src/hasp_oobe.cpp @@ -12,6 +12,7 @@ #include "hasp_gui.h" #include "hasp_wifi.h" #include "hasp_config.h" +#include "hasp_dispatch.h" #if HASP_USE_QRCODE != 0 #include "lv_qrcode.h" @@ -22,6 +23,8 @@ static lv_obj_t * oobekb; extern lv_font_t * defaultFont; static bool oobeAutoCalibrate = true; +lv_obj_t * pwd_ta; + void oobeSetAutoCalibrate(bool cal) { oobeAutoCalibrate = cal; @@ -40,6 +43,15 @@ void gotoPage1_cb(lv_obj_t * event_kb, lv_event_t event) } } +static void peek_password_cb(lv_obj_t * obj, lv_event_t event) +{ + if(event == LV_EVENT_VALUE_CHANGED) { + lv_obj_set_style_local_value_str(obj, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, + lv_btn_get_state(obj) ? LV_SYMBOL_EYE_OPEN : LV_SYMBOL_EYE_CLOSE); + lv_textarea_set_pwd_mode(pwd_ta, !lv_btn_get_state(obj)); + } +} + static void kb_event_cb(lv_obj_t * event_kb, lv_event_t event) { if(event == LV_EVENT_APPLY) { @@ -71,7 +83,20 @@ static void kb_event_cb(lv_obj_t * event_kb, lv_event_t event) if(strlen(ssid) > 0) { wifiSetConfig(settings.as()); - // dispatchReboot(true); + lv_obj_t * msgb = lv_cont_create(oobepage[1], NULL); + lv_obj_set_size(msgb, 120, 30); + lv_obj_align(msgb, oobepage[1], LV_ALIGN_CENTER, 0, 0); + + lv_obj_t * lbl = lv_label_create(msgb, NULL); + char buffer[128]; + snprintf(buffer, sizeof(buffer), PSTR("Connecting to %s..."), ssid); + lv_label_set_text(lbl, buffer); + + if(wifiTestConnection()) { + // dispatchReboot(true); + } + + // lv_obj_del(msgb); } } else if(event == LV_EVENT_CANCEL) { @@ -95,7 +120,18 @@ static void ta_event_cb(lv_obj_t * ta, lv_event_t event) else if(event == LV_EVENT_INSERT) { const char * str = (const char *)lv_event_get_data(); if(str[0] == '\n') { - // printf("Ready\n"); + lv_obj_t * child; + child = lv_obj_get_child(oobepage[1], NULL); + while(child) { + if(child->user_data && child->user_data > 0 && child->user_data != ta->user_data) { + if(oobekb != NULL) { + lv_keyboard_set_textarea(oobekb, child); + } + } + + /* next sibling */ + child = lv_obj_get_child(oobepage[1], child); + } } else { // printf("%s\n", lv_ta_get_text(ta)); } @@ -106,29 +142,21 @@ static void oobeSetupQR(const char * ssid, const char * pass) { lv_disp_t * disp = lv_disp_get_default(); oobepage[0] = lv_obj_create(NULL, NULL); - + char buffer[128]; lv_obj_t * container = lv_cont_create(oobepage[0], NULL); lv_obj_set_pos(container, 5, 5); lv_obj_set_style_local_bg_opa(container, LV_ARC_PART_BG, LV_STATE_DEFAULT, 0); lv_obj_set_style_local_border_opa(container, LV_ARC_PART_BG, LV_STATE_DEFAULT, 0); - Log.verbose(F("OOBE: line %d"), __LINE__); - #if HASP_USE_QRCODE != 0 - char buffer[128]; snprintf_P(buffer, sizeof(buffer), PSTR("WIFI:S:%s;T:WPA;P:%s;;"), ssid, pass); - Log.verbose(F("OOBE: line %d"), __LINE__); - lv_obj_t * qr = lv_qrcode_create(oobepage[0], 120, LV_COLOR_BLACK, LV_COLOR_WHITE); lv_qrcode_update(qr, buffer, strlen(buffer)); - Log.verbose(F("OOBE: line %d"), __LINE__); - lv_obj_t * qrlabel = lv_label_create(oobepage[0], NULL); - lv_label_set_text(qrlabel, "Scan to connect:"); - - Log.verbose(F("OOBE: line %d"), __LINE__); + snprintf(buffer, sizeof(buffer), PSTR("Scan to connect:")); + lv_label_set_text(qrlabel, buffer); if(disp->driver.hor_res <= disp->driver.ver_res) { lv_obj_align(qr, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -5); @@ -140,40 +168,31 @@ static void oobeSetupQR(const char * ssid, const char * pass) lv_obj_align(qrlabel, qr, LV_ALIGN_OUT_BOTTOM_MID, 0, 5); } - Log.verbose(F("OOBE: line %d"), __LINE__); - #else - Log.verbose(F("OOBE: line %d"), __LINE__); + lv_obj_set_size(container, disp->driver.hor_res, disp->driver.ver_res); #endif - Log.verbose(F("OOBE: line %d"), __LINE__); - lv_obj_t * aplabel = lv_label_create(container, NULL); - lv_label_set_text(aplabel, "Tap the screen to setup WiFi or\nconnect to this Access Point:"); + snprintf(buffer, sizeof(buffer), PSTR("Tap the screen to setup WiFi or\nconnect to this Access Point:")); + lv_label_set_text(aplabel, buffer); lv_label_set_long_mode(aplabel, LV_LABEL_LONG_BREAK); - Log.verbose(F("OOBE: line %d"), __LINE__); lv_obj_set_width(aplabel, lv_obj_get_width(container)); lv_label_set_align(aplabel, LV_LABEL_ALIGN_CENTER); - Log.verbose(F("OOBE: line %d"), __LINE__); lv_obj_align(aplabel, container, LV_ALIGN_IN_TOP_MID, 0, 0); lv_obj_t * panel = lv_cont_create(container, NULL); lv_obj_align(panel, aplabel, LV_ALIGN_OUT_BOTTOM_MID, 0, 10); - Log.verbose(F("OOBE: line %d"), __LINE__); lv_cont_set_fit(panel, LV_FIT_TIGHT); - Log.verbose(F("OOBE: line %d"), __LINE__); lv_cont_set_layout(panel, LV_LAYOUT_COLUMN_MID); - Log.verbose(F("OOBE: line %d"), __LINE__); String txt((char *)0); txt.reserve(64); - Log.verbose(F("OOBE: line %d"), __LINE__); txt = String(LV_SYMBOL_WIFI) + " " + String(ssid); lv_obj_t * network = lv_label_create(panel, NULL); lv_label_set_text(network, txt.c_str()); @@ -226,9 +245,10 @@ static void oobeSetupSsid(void) // lv_style_set_text_font(&robotofont, LV_STATE_DEFAULT, defaultfont); oobepage[1] = lv_obj_create(NULL, NULL); + char buffer[32]; /* Create the password box */ - lv_obj_t * pwd_ta = lv_textarea_create(oobepage[1], NULL); + pwd_ta = lv_textarea_create(oobepage[1], NULL); lv_obj_set_style_local_text_font(pwd_ta, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, defaultfont); lv_textarea_set_text(pwd_ta, ""); @@ -237,11 +257,19 @@ static void oobeSetupSsid(void) lv_textarea_set_one_line(pwd_ta, true); lv_textarea_set_cursor_hidden(pwd_ta, true); lv_obj_set_user_data(pwd_ta, 20); - lv_obj_set_width(pwd_ta, disp->driver.hor_res - leftmargin - 20); + lv_obj_set_width(pwd_ta, disp->driver.hor_res - leftmargin - 20 - lv_obj_get_height(pwd_ta)); lv_obj_set_event_cb(pwd_ta, ta_event_cb); - lv_obj_align(pwd_ta, NULL, LV_ALIGN_CENTER, leftmargin / 2, topmargin - voffset); + lv_obj_align(pwd_ta, NULL, LV_ALIGN_CENTER, leftmargin / 2 - lv_obj_get_height(pwd_ta) / 2, topmargin - voffset); + + lv_obj_t * pwd_icon = lv_btn_create(oobepage[1], NULL); + lv_obj_set_size(pwd_icon, lv_obj_get_height(pwd_ta), lv_obj_get_height(pwd_ta)); + lv_obj_set_pos(pwd_icon, lv_obj_get_x(pwd_ta) + lv_obj_get_width(pwd_ta), lv_obj_get_y(pwd_ta)); + lv_obj_set_style_local_value_str(pwd_icon, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_EYE_CLOSE); + lv_obj_set_style_local_radius(pwd_icon, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, + lv_obj_get_style_radius(pwd_ta, LV_TEXTAREA_PART_BG)); + lv_obj_set_event_cb(pwd_icon, peek_password_cb); + lv_btn_set_checkable(pwd_icon, true); - Log.notice(F("Create the one-line mode text area")); /* Create the one-line mode text area */ lv_obj_t * oneline_ta = lv_textarea_create(oobepage[1], pwd_ta); @@ -251,37 +279,37 @@ static void oobeSetupSsid(void) lv_obj_set_user_data(oneline_ta, 10); lv_obj_align(oneline_ta, pwd_ta, LV_ALIGN_OUT_TOP_MID, 0, topmargin); - Log.notice(F("Create a label and position it above the text box")); /* Create a label and position it above the text box */ lv_obj_t * pwd_label = lv_label_create(oobepage[1], NULL); - lv_label_set_text(pwd_label, "Password:"); + snprintf(buffer, sizeof(buffer), PSTR("Password:")); + lv_label_set_text(pwd_label, buffer); lv_obj_align(pwd_label, pwd_ta, labelpos, 0, 0); - Log.notice(F("Create a label and position it above the text box")); /* Create a label and position it above the text box */ lv_obj_t * oneline_label = lv_label_create(oobepage[1], NULL); - lv_label_set_text(oneline_label, "Ssid:"); + snprintf(buffer, sizeof(buffer), PSTR("Ssid:")); + lv_label_set_text(oneline_label, buffer); lv_obj_align(oneline_label, oneline_ta, labelpos, 0, 0); - Log.notice(F("Create a keyboard")); /* Create a keyboard and make it fill the width of the above text areas */ oobekb = lv_keyboard_create(oobepage[1], NULL); lv_obj_set_style_local_text_font(oobekb, LV_KEYBOARD_PART_BG, LV_STATE_DEFAULT, defaultfont); lv_obj_set_style_local_radius(oobekb, LV_KEYBOARD_PART_BG, LV_STATE_DEFAULT, 0); + lv_obj_set_style_local_border_color(oobekb, LV_KEYBOARD_PART_BTN, LV_STATE_DEFAULT, LV_COLOR_SILVER); + lv_obj_set_style_local_border_color(oobekb, LV_KEYBOARD_PART_BTN, LV_STATE_PRESSED, LV_COLOR_PURPLE); + lv_obj_set_style_local_bg_color(oobekb, LV_KEYBOARD_PART_BTN, LV_BTN_STATE_PRESSED, LV_COLOR_PURPLE); lv_obj_set_style_local_pad_inner(oobekb, LV_KEYBOARD_PART_BG, LV_STATE_DEFAULT, 1); lv_obj_set_style_local_pad_left(oobekb, LV_KEYBOARD_PART_BG, LV_STATE_DEFAULT, 0); lv_obj_set_style_local_pad_right(oobekb, LV_KEYBOARD_PART_BG, LV_STATE_DEFAULT, 0); // lv_obj_set_pos(oobekb, 5, 90); - Log.notice(F("Create a keyboard CB")); lv_obj_set_event_cb(oobekb, kb_event_cb); /* Setting a custom event handler stops the keyboard from closing automatically */ // lv_keybard_add_(oobekb, LV_KEYBOARD_PART_BG, &lv_style_transp_tight); // lv_keyboard_set_style(oobekb, LV_keyboard_STYLE_BTN_REL, &rel_style); // lv_keyboard_set_style(oobekb, LV_keyboard_STYLE_BTN_PR, &pr_style); - Log.notice(F("Create a keyboard TA link")); lv_keyboard_set_textarea(oobekb, oneline_ta); /* Focus it on one of the text areas to start */ lv_keyboard_set_cursor_manage(oobekb, true); /* Automatically show/hide cursors on text areas */ } @@ -306,6 +334,7 @@ void oobeSetup() { char ssid[32]; char pass[32]; + if(wifiShowAP(ssid, pass)) { guiSetDim(100); oobeSetupQR(ssid, pass); diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index 6ae20b6f..0168a63c 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -161,7 +161,7 @@ bool wifiShowAP(char * ssid, char * pass) void wifiSetup() { if(wifiShowAP()) { - WiFi.mode(WIFI_AP); + WiFi.mode(WIFI_AP_STA); } else { WiFi.mode(WIFI_STA); @@ -182,7 +182,9 @@ void wifiSetup() bool wifiEvery5Seconds() { - if(WiFi.getMode() == WIFI_AP || WiFi.status() == WL_CONNECTED) { + if(WiFi.getMode() != WIFI_STA) { + return false; + } else if(WiFi.status() == WL_CONNECTED) { return true; } else { wifiReconnectCounter++; @@ -232,6 +234,21 @@ bool wifiSetConfig(const JsonObject & settings) return changed; } +bool wifiTestConnection() +{ + uint8_t attempt = 0; + WiFi.begin(wifiSsid, wifiPassword); + while(attempt < 10 && WiFi.localIP().toString() == F("0.0.0.0")) { + attempt++; + Log.verbose(F("WIFI: Trying to connect to %s... %u"), wifiSsid, attempt); + delay(1000); + } + if(WiFi.localIP().toString() != F("0.0.0.0")) return true; + + WiFi.disconnect(); + return false; +} + void wifiStop() { Log.warning(F("WIFI: Stopped")); diff --git a/src/hasp_wifi.h b/src/hasp_wifi.h index 57d33a51..3ee27d27 100644 --- a/src/hasp_wifi.h +++ b/src/hasp_wifi.h @@ -8,6 +8,7 @@ bool wifiShowAP(); bool wifiShowAP(char * ssid, char * pass); bool wifiEvery5Seconds(void); void wifiStop(void); +bool wifiTestConnection(); bool wifiGetConfig(const JsonObject & settings); bool wifiSetConfig(const JsonObject & settings);