From 66f1703bf0e780708715d60584a7263c9beac4d3 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 27 Mar 2012 22:36:45 +1300 Subject: Add str_to_mac() and str_to_ip() * Process all IPs and ethernet MAC in /config/set handler Signed-off-by: Mike Beattie --- HouseControl.ino | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/HouseControl.ino b/HouseControl.ino index 8d293f9..9035a29 100644 --- a/HouseControl.ino +++ b/HouseControl.ino @@ -144,6 +144,31 @@ char* time_to_str(void *buf, time_t t) { return (char*)buf; } +bool str_to_bytearray(byte *buf, byte *dest, byte base, byte n, char delim) { + byte i; + char *cur = (char*)buf, *next; + unsigned long int v; + for (i=0;i 255) || (next[0] != delim) || (next == cur)) + return false; + buf[i] = v; + cur = ++next; + } + memcpy(dest,buf,n); + return true; +} + +bool str_to_ip(byte *buf, byte *ip) { + return str_to_bytearray(buf,ip,10,4,'.'); +} + +bool str_to_mac(byte *buf, byte *mac) { + return str_to_bytearray(buf,mac,16,6,':'); +} + + /*********************************************************************************/ /* NTP/Time functions */ @@ -326,12 +351,30 @@ void configSetHandler(WebServer &server, WebServer::ConnectionType type, char *u if (type == WebServer::POST) { server.httpSuccess(); while (server.readPOSTparam((char*)buf2, BUF2_SIZE, (char*)buf1, BUF1_SIZE)) { + if ((strcmp((const char*)buf2, "mac") == 0)) + if (!str_to_mac(buf1, config.mac)) { server.print("Invalid MAC Address"); return; } + + if ((strcmp((const char*)buf2, "def_ip") == 0)) + if (!str_to_ip(buf1, config.def_ip)) { server.print("Invalid IP Address"); return; } + + if ((strcmp((const char*)buf2, "def_nm") == 0)) + if (!str_to_ip(buf1, config.def_netmask)) { server.print("Invalid Netmask"); return; } + + if ((strcmp((const char*)buf2, "def_gw") == 0)) + if (!str_to_ip(buf1, config.def_gateway)) { server.print("Invalid Gateway"); return; } + + if ((strcmp((const char*)buf2, "ntpserver") == 0)) + if (!str_to_ip(buf1, config.ntpServer)) { server.print("Invalid NTP Server"); return; } + + if ((strcmp((const char*)buf2, "stathost") == 0)) + if (!str_to_ip(buf1, config.notifyHost)) { server.print("Invalid Status target"); return; } + server.print((char*)buf2); server.print(" = '"); server.print((char*)buf1); server.print("'
\n"); } - server.print("OK"); + server.print("Settings Saved Successfully"); return; } -- cgit v1.2.3