diff --git a/Software/codegen/templates/struct2json.cpp.j2 b/Software/codegen/templates/struct2json.cpp.j2 index 1287082..38335c6 100644 --- a/Software/codegen/templates/struct2json.cpp.j2 +++ b/Software/codegen/templates/struct2json.cpp.j2 @@ -13,7 +13,7 @@ #include "struct2json.h" {% for var_name, var_info in structs.items() -%} -void generateJsonObject_{{ var_name }}(JsonObject& data) +void generateJsonObject_{{ var_name }}(JsonObject data) { {% for field_name, field_type in var_info['fields'].items() -%} data["{{ field_name }}"] = {{ var_name }}.{{ field_name }}; diff --git a/Software/codegen/templates/struct2json.h.j2 b/Software/codegen/templates/struct2json.h.j2 index d3f2521..baeb55f 100644 --- a/Software/codegen/templates/struct2json.h.j2 +++ b/Software/codegen/templates/struct2json.h.j2 @@ -18,7 +18,7 @@ #include "config.h" {% for var_name, var_info in structs.items() -%} -void generateJsonObject_{{ var_name }}(JsonObject& data); +void generateJsonObject_{{ var_name }}(JsonObject data); {% endfor %} #endif /* _STRUCT2JSON_H_ */ diff --git a/Software/data_src/index.htm b/Software/data_src/index.htm index 8b15922..41a9252 100644 --- a/Software/data_src/index.htm +++ b/Software/data_src/index.htm @@ -318,11 +318,6 @@ -
-
- -
-

@@ -430,7 +425,42 @@

- + + +
+

+

WiFi Einstellungen

+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+
+
+ +
+
+ + +
+
+
+

+

diff --git a/Software/data_src/static/js/script.js b/Software/data_src/static/js/script.js index f0d2018..51dadd2 100644 --- a/Software/data_src/static/js/script.js +++ b/Software/data_src/static/js/script.js @@ -1,6 +1,19 @@ -$(document).ready(function(){ - $('.navbar-nav a').on('click', function(){ - $('.navbar-collapse').collapse('hide'); +$(document).ready(function () { + $(".navbar-nav a").on("click", function () { + $(".navbar-collapse").collapse("hide"); + }); + + $("#show_hide_password a").on("click", function (event) { + event.preventDefault(); + if ($("#show_hide_password input").attr("type") == "text") { + $("#show_hide_password input").attr("type", "password"); + $("#show_hide_password i").addClass("fa-eye-slash"); + $("#show_hide_password i").removeClass("fa-eye"); + } else if ($("#show_hide_password input").attr("type") == "password") { + $("#show_hide_password input").attr("type", "text"); + $("#show_hide_password i").removeClass("fa-eye-slash"); + $("#show_hide_password i").addClass("fa-eye"); + } }); }); @@ -11,6 +24,3 @@ document var nextSibling = e.target.nextElementSibling; nextSibling.innerText = fileName; }); - - - diff --git a/Software/include/config.h b/Software/include/config.h index 67d6e4b..75a2d2c 100644 --- a/Software/include/config.h +++ b/Software/include/config.h @@ -79,6 +79,7 @@ typedef struct char wifi_ap_password[64]; char wifi_client_ssid[33]; char wifi_client_password[64]; + bool wifi_autoconnect; uint32_t checksum; } LubeConfig_t; @@ -94,6 +95,7 @@ const LubeConfig_t LubeConfig_defaults = { QUOTE(WIFI_AP_PASSWORD), QUOTE(WIFI_SSID_CLIENT), QUOTE(WIFI_PASSWORD_CLIENT), + true, 0}; void InitEEPROM(); @@ -108,6 +110,7 @@ uint32_t Checksum_EEPROM(uint8_t const *data, size_t len); void dumpEEPROM(uint16_t memoryAddress, uint16_t length); void MovePersistencePage_EEPROM(boolean reset); uint32_t ConfigSanityCheck(bool autocorrect = false); +bool validateWiFiString(char *string, size_t size); extern LubeConfig_t LubeConfig; extern persistenceData_t PersistenceData; diff --git a/Software/include/struct2json.h b/Software/include/struct2json.h index 515ba5d..83c5a03 100644 --- a/Software/include/struct2json.h +++ b/Software/include/struct2json.h @@ -3,10 +3,10 @@ * * @brief Header file for converting structs to JSON objects. * - * @note This file is auto-generated by a script on 2024-01-25 14:30:32. + * @note This file is auto-generated by a script on 2024-01-30 20:29:34. * * @author Marcel Peterkau - * @date 25.01.2024 + * @date 30.01.2024 */ #ifndef _STRUCT2JSON_H_ @@ -17,10 +17,10 @@ #include "config.h" -void generateJsonObject_LubeConfig(JsonObject& data); -void generateJsonObject_PersistenceData(JsonObject& data); +void generateJsonObject_LubeConfig(JsonObject data); +void generateJsonObject_PersistenceData(JsonObject data); #endif /* _STRUCT2JSON_H_ */ -// CODEGENERATOR_CHECKSUM: 4bb0dc037057aafd9688aacedfaae5c97c9de79dbbd0e139d982208053f74fa8 \ No newline at end of file +// CODEGENERATOR_CHECKSUM: 59f35aadffd0bbef253210ea2fbaaf9a515553a2e3cc9bf4cfa2819b63c969ce \ No newline at end of file diff --git a/Software/src/config.cpp b/Software/src/config.cpp index f6df9c3..1806ecb 100644 --- a/Software/src/config.cpp +++ b/Software/src/config.cpp @@ -492,6 +492,78 @@ uint32_t ConfigSanityCheck(bool autocorrect) if (autocorrect) LubeConfig.CANSource = LubeConfig_defaults.CANSource; } + + if (!validateWiFiString(LubeConfig.wifi_ap_ssid, sizeof(LubeConfig.wifi_ap_ssid))) + { + SET_BIT(setting_reset_bits, 14); + if (autocorrect) + strncpy(LubeConfig.wifi_ap_ssid, LubeConfig_defaults.wifi_ap_ssid, sizeof(LubeConfig.wifi_ap_ssid)); + } + + if (!validateWiFiString(LubeConfig.wifi_ap_password, sizeof(LubeConfig.wifi_ap_password))) + { + SET_BIT(setting_reset_bits, 15); + if (autocorrect) + strncpy(LubeConfig.wifi_ap_password, LubeConfig_defaults.wifi_ap_password, sizeof(LubeConfig.wifi_ap_password)); + } + + if (!validateWiFiString(LubeConfig.wifi_client_ssid, sizeof(LubeConfig.wifi_client_ssid))) + { + SET_BIT(setting_reset_bits, 16); + if (autocorrect) + strncpy(LubeConfig.wifi_client_ssid, LubeConfig_defaults.wifi_client_ssid, sizeof(LubeConfig.wifi_client_ssid)); + } + + if (!validateWiFiString(LubeConfig.wifi_client_password, sizeof(LubeConfig.wifi_client_password))) + { + SET_BIT(setting_reset_bits, 17); + if (autocorrect) + strncpy(LubeConfig.wifi_client_password, LubeConfig_defaults.wifi_client_password, sizeof(LubeConfig.wifi_client_password)); + } // Return the bitmask indicating which settings need to be reset return setting_reset_bits; -} \ No newline at end of file +} + +/** + * @brief Validates whether a given string contains only characters allowed in WiFi SSIDs and passwords. + * + * This function checks each character in the provided string to ensure + * that it contains only characters allowed in WiFi SSIDs and passwords. + * It considers characters from 'A' to 'Z', 'a' to 'z', '0' to '9', as well as + * the following special characters: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ + * + * @param string Pointer to the string to be validated. + * @param size Size of the string including the null-terminator. + * @return true if the string contains only allowed characters or is NULL, + * false otherwise. + */ +bool validateWiFiString(char *string, size_t size) +{ + if (string == NULL) + return false; + + for (size_t i = 0; i < size; i++) + { + char c = string[i]; + if (c == '\0') + { + // Reached the end of the string, all characters were valid WiFi characters. + return true; + } + if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || c == '!' || c == '"' || c == '#' || + c == '$' || c == '%' || c == '&' || c == '\'' || c == '(' || + c == ')' || c == '*' || c == '+' || c == ',' || c == '-' || + c == '.' || c == '/' || c == ':' || c == ';' || c == '<' || + c == '=' || c == '>' || c == '?' || c == '@' || c == '[' || + c == '\\' || c == ']' || c == '^' || c == '_' || c == '`' || + c == '{' || c == '|' || c == '}' || c == '~')) + { + // Found a character that is not a valid WiFi character. + return false; + } + } + // If the loop completes without finding a null terminator, the string is invalid. + return false; +} + diff --git a/Software/src/struct2json.cpp b/Software/src/struct2json.cpp index 03d51f7..1fc6e15 100644 --- a/Software/src/struct2json.cpp +++ b/Software/src/struct2json.cpp @@ -3,16 +3,16 @@ * * @brief Implementation file for converting structs to JSON objects. * - * @note This file is auto-generated by a script on 2024-01-25 14:30:32. + * @note This file is auto-generated by a script on 2024-01-30 20:29:34. * * @author Marcel Peterkau - * @date 25.01.2024 + * @date 30.01.2024 */ #include "struct2json.h" -void generateJsonObject_LubeConfig(JsonObject& data) +void generateJsonObject_LubeConfig(JsonObject data) { data["EEPROM_Version"] = LubeConfig.EEPROM_Version; data["DistancePerLube_Default"] = LubeConfig.DistancePerLube_Default; @@ -36,10 +36,11 @@ void generateJsonObject_LubeConfig(JsonObject& data) data["wifi_ap_password"] = LubeConfig.wifi_ap_password; data["wifi_client_ssid"] = LubeConfig.wifi_client_ssid; data["wifi_client_password"] = LubeConfig.wifi_client_password; + data["wifi_autoconnect"] = LubeConfig.wifi_autoconnect; data["checksum"] = LubeConfig.checksum; } -void generateJsonObject_PersistenceData(JsonObject& data) +void generateJsonObject_PersistenceData(JsonObject data) { data["writeCycleCounter"] = PersistenceData.writeCycleCounter; data["tankRemain_microL"] = PersistenceData.tankRemain_microL; @@ -51,4 +52,4 @@ void generateJsonObject_PersistenceData(JsonObject& data) -// CODEGENERATOR_CHECKSUM: 4bb0dc037057aafd9688aacedfaae5c97c9de79dbbd0e139d982208053f74fa8 \ No newline at end of file +// CODEGENERATOR_CHECKSUM: 59f35aadffd0bbef253210ea2fbaaf9a515553a2e3cc9bf4cfa2819b63c969ce \ No newline at end of file diff --git a/Software/src/webui.cpp b/Software/src/webui.cpp index 5a20aa3..aabb718 100644 --- a/Software/src/webui.cpp +++ b/Software/src/webui.cpp @@ -307,6 +307,10 @@ void WebserverEERestore_Callback(AsyncWebServerRequest *request, const String &f LubeConfig.LED_Mode_Flash = json["config"]["LED_Mode_Flash"].as(); LubeConfig.LED_Max_Brightness = json["config"]["LED_Max_Brightness"].as(); LubeConfig.LED_Min_Brightness = json["config"]["LED_Min_Brightness"].as(); + strncpy(LubeConfig.wifi_ap_ssid, json["config"]["wifi_ap_ssid"].as(), sizeof(LubeConfig.wifi_ap_ssid)); + strncpy(LubeConfig.wifi_ap_password, json["config"]["wifi_ap_password"].as(), sizeof(LubeConfig.wifi_ap_password)); + strncpy(LubeConfig.wifi_client_ssid, json["config"]["wifi_client_ssid"].as(), sizeof(LubeConfig.wifi_client_ssid)); + strncpy(LubeConfig.wifi_client_password, json["config"]["wifi_client_password"].as(), sizeof(LubeConfig.wifi_client_password)); PersistenceData.writeCycleCounter = json["persis"]["writeCycleCounter"].as(); PersistenceData.tankRemain_microL = json["persis"]["tankRemain_microL"].as(); @@ -346,7 +350,7 @@ void WebServerEEJSON_Callback(AsyncWebServerRequest *request) { AsyncResponseStream *response = request->beginResponseStream("application/json"); JsonDocument json; - JsonObject info = json.to(); + JsonObject info = json["info"].to(); char buffer[16]; @@ -357,12 +361,12 @@ void WebServerEEJSON_Callback(AsyncWebServerRequest *request) snprintf_P(buffer, sizeof(buffer), "%s", constants.GitHash); info["Git-Hash"] = buffer; - JsonObject config = json.to(); + JsonObject config = json["config"].to(); generateJsonObject_LubeConfig(config); - JsonObject persis = json.to(); + JsonObject persis = json["persis"].to(); generateJsonObject_PersistenceData(persis); - JsonObject eepart = json.to(); + JsonObject eepart = json["eepart"].to(); sprintf(buffer, "0x%04X", globals.eePersistanceAdress); eepart["PersistanceAddress"] = buffer; @@ -513,7 +517,7 @@ void Websocket_HandleButtons(uint8_t *data) void Websocket_HandleSettings(uint8_t *data) { char identifier[32]; - char value[32]; + char value[63]; parseWebsocketString((char *)data, identifier, sizeof(identifier), value, sizeof(value)); @@ -568,6 +572,14 @@ void Websocket_HandleSettings(uint8_t *data) { LubeConfig.LED_Mode_Flash = value[0] == '1' ? true : false; } + else if (strcmp(identifier, "wifi-ssid") == 0) + { + strncpy(LubeConfig.wifi_client_ssid, value, sizeof(LubeConfig.wifi_client_ssid)); + } + else if (strcmp(identifier, "wifi-password") == 0) + { + strncpy(LubeConfig.wifi_client_password, value, sizeof(LubeConfig.wifi_client_password)); + } else { Debug_pushMessage("Got unknown Settings-id and value '%s' from ws-client\n", identifier); @@ -704,7 +716,9 @@ void Websocket_RefreshClientData_Static(uint32_t client_id, bool send_mapping) "showimpulse;" "showgps;" "showcan;" - "bleedingpulses;"; + "bleedingpulses;" + "wifi-ssid;" + "wifi-password;"; if (client_id > 0) webSocket.text(client_id, mapping); @@ -733,6 +747,8 @@ void Websocket_RefreshClientData_Static(uint32_t client_id, bool send_mapping) temp.concat(String(LubeConfig.SpeedSource == SOURCE_GPS ? "1" : "0") + ";"); temp.concat(String(LubeConfig.SpeedSource == SOURCE_CAN ? "1" : "0") + ";"); temp.concat(String(LubeConfig.BleedingPulses) + ";"); + temp.concat(String(LubeConfig.wifi_client_ssid) + ";"); + temp.concat(String(LubeConfig.wifi_client_password) + ";"); for (uint32_t i = 0; i < SpeedSourceString_Elements; i++) {