From 297f229c12e05d90045a598f5f5093468a238260 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Sun, 17 Apr 2022 21:40:47 +0200 Subject: [PATCH] Implemented WebAPI to set wifi credentials --- include/helper.h | 4 ++ src/WebApi.cpp | 149 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 123 insertions(+), 30 deletions(-) create mode 100644 include/helper.h diff --git a/include/helper.h b/include/helper.h new file mode 100644 index 0000000..e75fa8f --- /dev/null +++ b/include/helper.h @@ -0,0 +1,4 @@ +#pragma once + +#define STR_HELPER(x) #x +#define STR(x) STR_HELPER(x) \ No newline at end of file diff --git a/src/WebApi.cpp b/src/WebApi.cpp index 4cb8abb..8824aea 100644 --- a/src/WebApi.cpp +++ b/src/WebApi.cpp @@ -4,6 +4,7 @@ #include "Configuration.h" #include "WiFiSettings.h" #include "defaults.h" +#include "helper.h" #include #include @@ -123,36 +124,11 @@ void WebApiClass::onNetworkAdminGet(AsyncWebServerRequest* request) root[F("hostname")] = config.WiFi_Hostname; root[F("dhcp")] = config.WiFi_Dhcp; - root[F("ipaddress")] = IPAddress( - config.WiFi_Ip[0], - config.WiFi_Ip[1], - config.WiFi_Ip[2], - config.WiFi_Ip[3]) - .toString(); - root[F("netmask")] = IPAddress( - config.WiFi_Netmask[0], - config.WiFi_Netmask[1], - config.WiFi_Netmask[2], - config.WiFi_Netmask[3]) - .toString(); - root[F("gateway")] = IPAddress( - config.WiFi_Gateway[0], - config.WiFi_Gateway[1], - config.WiFi_Gateway[2], - config.WiFi_Gateway[3]) - .toString(); - root[F("dns1")] = IPAddress( - config.WiFi_Dns1[0], - config.WiFi_Dns1[1], - config.WiFi_Dns1[2], - config.WiFi_Dns1[3]) - .toString(); - root[F("dns2")] = IPAddress( - config.WiFi_Dns2[0], - config.WiFi_Dns2[1], - config.WiFi_Dns2[2], - config.WiFi_Dns2[3]) - .toString(); + root[F("ipaddress")] = IPAddress(config.WiFi_Ip).toString(); + root[F("netmask")] = IPAddress(config.WiFi_Netmask).toString(); + root[F("gateway")] = IPAddress(config.WiFi_Gateway).toString(); + root[F("dns1")] = IPAddress(config.WiFi_Dns1).toString(); + root[F("dns2")] = IPAddress(config.WiFi_Dns2).toString(); root[F("ssid")] = config.WiFi_Ssid; root[F("password")] = config.WiFi_Password; @@ -175,11 +151,124 @@ void WebApiClass::onNetworkAdminPost(AsyncWebServerRequest* request) String json = request->getParam("data", true)->value(); + if (json.length() > 1024) { + retMsg[F("message")] = F("Data too large!"); + response->setLength(); + request->send(response); + return; + } + + DynamicJsonDocument root(1024); + DeserializationError error = deserializeJson(root, json); + + if (error) { + retMsg[F("message")] = F("Failed to parse data!"); + response->setLength(); + request->send(response); + return; + } + + if (!(root.containsKey("ssid") && root.containsKey("password") && root.containsKey("hostname") && root.containsKey("dhcp") && root.containsKey("ipaddress") && root.containsKey("netmask") && root.containsKey("gateway") && root.containsKey("dns1") && root.containsKey("dns2"))) { + retMsg[F("message")] = F("Values are missing!"); + response->setLength(); + request->send(response); + return; + } + + IPAddress ipaddress; + if (!ipaddress.fromString(root[F("ipaddress")].as())) { + retMsg[F("message")] = F("IP address is invalid!"); + response->setLength(); + request->send(response); + return; + } + IPAddress netmask; + if (!netmask.fromString(root[F("netmask")].as())) { + retMsg[F("message")] = F("Netmask is invalid!"); + response->setLength(); + request->send(response); + return; + } + IPAddress gateway; + if (!gateway.fromString(root[F("gateway")].as())) { + retMsg[F("message")] = F("Gateway is invalid!"); + response->setLength(); + request->send(response); + return; + } + IPAddress dns1; + if (!dns1.fromString(root[F("dns1")].as())) { + retMsg[F("message")] = F("DNS Server IP 1 is invalid!"); + response->setLength(); + request->send(response); + return; + } + IPAddress dns2; + if (!dns2.fromString(root[F("dns2")].as())) { + retMsg[F("message")] = F("DNS Server IP 2 is invalid!"); + response->setLength(); + request->send(response); + return; + } + + if (root[F("hostname")].as().length() == 0 || root[F("hostname")].as().length() > WIFI_MAX_HOSTNAME_STRLEN) { + retMsg[F("message")] = F("Hostname must between 1 and " STR(WIFI_MAX_HOSTNAME_STRLEN) " characters long!"); + response->setLength(); + request->send(response); + return; + } + if (root[F("ssid")].as().length() == 0 || root[F("ssid")].as().length() > WIFI_MAX_SSID_STRLEN) { + retMsg[F("message")] = F("SSID must between 1 and " STR(WIFI_MAX_SSID_STRLEN) " characters long!"); + response->setLength(); + request->send(response); + return; + } + if (root[F("password")].as().length() > WIFI_MAX_PASSWORD_STRLEN - 1) { + retMsg[F("message")] = F("Password must not be longer than " STR(WIFI_MAX_PASSWORD_STRLEN) " characters long!"); + response->setLength(); + request->send(response); + return; + } + + CONFIG_T& config = Configuration.get(); + config.WiFi_Ip[0] = ipaddress[0]; + config.WiFi_Ip[1] = ipaddress[1]; + config.WiFi_Ip[2] = ipaddress[2]; + config.WiFi_Ip[3] = ipaddress[3]; + config.WiFi_Netmask[0] = netmask[0]; + config.WiFi_Netmask[1] = netmask[1]; + config.WiFi_Netmask[2] = netmask[2]; + config.WiFi_Netmask[3] = netmask[3]; + config.WiFi_Gateway[0] = gateway[0]; + config.WiFi_Gateway[1] = gateway[1]; + config.WiFi_Gateway[2] = gateway[2]; + config.WiFi_Gateway[3] = gateway[3]; + config.WiFi_Dns1[0] = dns1[0]; + config.WiFi_Dns1[1] = dns1[1]; + config.WiFi_Dns1[2] = dns1[2]; + config.WiFi_Dns1[3] = dns1[3]; + config.WiFi_Dns2[0] = dns2[0]; + config.WiFi_Dns2[1] = dns2[1]; + config.WiFi_Dns2[2] = dns2[2]; + config.WiFi_Dns2[3] = dns2[3]; + strcpy(config.WiFi_Ssid, root[F("ssid")].as().c_str()); + strcpy(config.WiFi_Password, root[F("password")].as().c_str()); + strcpy(config.WiFi_Hostname, root[F("hostname")].as().c_str()); + if (root[F("dhcp")].as()) { + config.WiFi_Dhcp = true; + } else { + config.WiFi_Dhcp = false; + } + Configuration.write(); + retMsg[F("type")] = F("success"); retMsg[F("message")] = F("Settings saved!"); response->setLength(); request->send(response); + + WiFiSettings.enableAdminMode(); + WiFiSettings.applyConfig(); } WebApiClass WebApi; \ No newline at end of file