added Wifi-Settings to WebUI and Config
This commit is contained in:
		| @@ -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; | ||||
| } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @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; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
| // CODEGENERATOR_CHECKSUM: 59f35aadffd0bbef253210ea2fbaaf9a515553a2e3cc9bf4cfa2819b63c969ce | ||||
| @@ -307,6 +307,10 @@ void WebserverEERestore_Callback(AsyncWebServerRequest *request, const String &f | ||||
|         LubeConfig.LED_Mode_Flash = json["config"]["LED_Mode_Flash"].as<bool>(); | ||||
|         LubeConfig.LED_Max_Brightness = json["config"]["LED_Max_Brightness"].as<uint8_t>(); | ||||
|         LubeConfig.LED_Min_Brightness = json["config"]["LED_Min_Brightness"].as<uint8_t>(); | ||||
|         strncpy(LubeConfig.wifi_ap_ssid, json["config"]["wifi_ap_ssid"].as<const char *>(), sizeof(LubeConfig.wifi_ap_ssid)); | ||||
|         strncpy(LubeConfig.wifi_ap_password, json["config"]["wifi_ap_password"].as<const char *>(), sizeof(LubeConfig.wifi_ap_password)); | ||||
|         strncpy(LubeConfig.wifi_client_ssid, json["config"]["wifi_client_ssid"].as<const char *>(), sizeof(LubeConfig.wifi_client_ssid)); | ||||
|         strncpy(LubeConfig.wifi_client_password, json["config"]["wifi_client_password"].as<const char *>(), sizeof(LubeConfig.wifi_client_password)); | ||||
|  | ||||
|         PersistenceData.writeCycleCounter = json["persis"]["writeCycleCounter"].as<uint16_t>(); | ||||
|         PersistenceData.tankRemain_microL = json["persis"]["tankRemain_microL"].as<uint32_t>(); | ||||
| @@ -346,7 +350,7 @@ void WebServerEEJSON_Callback(AsyncWebServerRequest *request) | ||||
| { | ||||
|   AsyncResponseStream *response = request->beginResponseStream("application/json"); | ||||
|   JsonDocument json; | ||||
|   JsonObject info = json.to<JsonObject>(); | ||||
|   JsonObject info = json["info"].to<JsonObject>(); | ||||
|  | ||||
|   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>(); | ||||
|   JsonObject config = json["config"].to<JsonObject>(); | ||||
|   generateJsonObject_LubeConfig(config); | ||||
|   JsonObject persis = json.to<JsonObject>(); | ||||
|   JsonObject persis = json["persis"].to<JsonObject>(); | ||||
|   generateJsonObject_PersistenceData(persis); | ||||
|  | ||||
|   JsonObject eepart = json.to<JsonObject>(); | ||||
|   JsonObject eepart = json["eepart"].to<JsonObject>(); | ||||
|   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++) | ||||
|   { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user