Fixed a ton of bugs and made almost everything work

This commit is contained in:
2017-07-24 00:35:04 +02:00
parent 314239842e
commit 28a170faa3
39 changed files with 1985 additions and 390 deletions
+8 -5
View File
@@ -8,13 +8,14 @@ Cgi/template routines for configuring non-wifi settings
#include "screen.h"
#include "helpers.h"
#define SET_REDIR_SUC "/cfg/term"
#define SET_REDIR_SUC "/cfg/app"
#define SET_REDIR_ERR SET_REDIR_SUC"?err="
/**
* Universal CGI endpoint to set Terminal params.
*/
httpd_cgi_state ICACHE_FLASH_ATTR cgiAppCfgSet(HttpdConnData *connData)
httpd_cgi_state ICACHE_FLASH_ATTR
cgiAppCfgSetParams(HttpdConnData *connData)
{
char buff[50];
@@ -49,7 +50,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiAppCfgSet(HttpdConnData *connData)
redir_url += sprintf(redir_url, "term_width,");
}
} else {
warn("Missing height arg", buff);
warn("Missing height arg!");
// this wont happen normally when the form is used
redir_url += sprintf(redir_url, "term_width,term_height,");
}
@@ -115,6 +116,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiAppCfgSet(HttpdConnData *connData)
// All was OK
info("Set app params - success, saving...");
terminal_apply_settings();
persist_store();
httpdRedirect(connData, SET_REDIR_SUC);
@@ -127,7 +129,8 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiAppCfgSet(HttpdConnData *connData)
}
httpd_cgi_state ICACHE_FLASH_ATTR tplAppCfg(HttpdConnData *connData, char *token, void **arg)
httpd_cgi_state ICACHE_FLASH_ATTR
tplAppCfg(HttpdConnData *connData, char *token, void **arg)
{
#define BUFLEN 100
char buff[BUFLEN];
@@ -149,7 +152,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR tplAppCfg(HttpdConnData *connData, char *token
sprintf(buff, "%d", termconf->default_bg);
}
else if (streq(token, "default_fg")) {
sprintf(buff, "%d", termconf->default_bg);
sprintf(buff, "%d", termconf->default_fg);
}
else if (streq(token, "term_title")) {
strncpy_safe(buff, termconf->title, BUFLEN);
+1 -1
View File
@@ -3,7 +3,7 @@
#include "httpd.h"
httpd_cgi_state cgiAppCfgSet(HttpdConnData *connData);
httpd_cgi_state cgiAppCfgSetParams(HttpdConnData *connData);
httpd_cgi_state tplAppCfg(HttpdConnData *connData, char *token, void **arg);
#endif
+28 -4
View File
@@ -5,9 +5,7 @@
#include "cgi_main.h"
#include "screen.h"
#include "user_main.h"
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#include "helpers.h"
/**
* Main page template substitution
@@ -28,7 +26,33 @@ httpd_cgi_state ICACHE_FLASH_ATTR tplScreen(HttpdConnData *connData, char *token
const int bufsiz = 512;
char buff[bufsiz];
if (streq(token, "screenData")) {
if (streq(token, "term_title")) {
httpdSend(connData, termconf->title, -1);
}
else if (streq(token, "btn1")) {
httpdSend(connData, termconf->btn1, -1);
}
else if (streq(token, "btn2")) {
httpdSend(connData, termconf->btn2, -1);
}
else if (streq(token, "btn3")) {
httpdSend(connData, termconf->btn3, -1);
}
else if (streq(token, "btn4")) {
httpdSend(connData, termconf->btn4, -1);
}
else if (streq(token, "btn5")) {
httpdSend(connData, termconf->btn5, -1);
}
else if (streq(token, "default_bg")) {
sprintf(buff, "%d", termconf->default_bg);
httpdSend(connData, buff, -1);
}
else if (streq(token, "default_fg")) {
sprintf(buff, "%d", termconf->default_fg);
httpdSend(connData, buff, -1);
}
else if (streq(token, "screenData")) {
httpd_cgi_state cont = screenSerializeToBuffer(buff, bufsiz, arg);
httpdSend(connData, buff, -1);
return cont;
+2 -2
View File
@@ -231,10 +231,10 @@ httpd_cgi_state ICACHE_FLASH_ATTR tplNetwork(HttpdConnData *connData, char *toke
else if (streq(token, "sta_addr_ip")) {
sprintf(buff, IPSTR, GOOD_IP2STR(wificonf->sta_addr.ip.addr));
}
else if (streq(token, "ap_addr_mask")) {
else if (streq(token, "sta_addr_mask")) {
sprintf(buff, IPSTR, GOOD_IP2STR(wificonf->sta_addr.netmask.addr));
}
else if (streq(token, "ap_addr_gw")) {
else if (streq(token, "sta_addr_gw")) {
sprintf(buff, IPSTR, GOOD_IP2STR(wificonf->sta_addr.gw.addr));
}
else if (streq(token, "sta_mac")) {
+74
View File
@@ -0,0 +1,74 @@
/*
Cgi/template routines for configuring non-wifi settings
*/
#include <esp8266.h>
#include "cgi_persist.h"
#include "persist.h"
#include "helpers.h"
#define SET_REDIR_SUC "/cfg/admin"
static bool ICACHE_FLASH_ATTR
verify_admin_pw(const char *pw)
{
// This is not really for security, but to prevent someone who
// shouldn't touch those settings from fucking it up.
return streq(pw, STR(ADMIN_PASSWORD)); // the PW comes from the makefile
}
httpd_cgi_state ICACHE_FLASH_ATTR
cgiPersistWriteDefaults(HttpdConnData *connData)
{
char buff[50];
if (connData->conn == NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
// width and height must always go together so we can do max size validation
if (GET_ARG("pw")) {
dbg("Entered password for admin: %s", buff);
if (verify_admin_pw(buff)) {
dbg("pw is OK");
persist_set_as_default();
httpdRedirect(connData, SET_REDIR_SUC);
return HTTPD_CGI_DONE;
}
// if pw failed, show the same error as if it's wrong
}
httpdRedirect(connData, SET_REDIR_SUC "?err=Password"); // this will show in the "validation errors" box
return HTTPD_CGI_DONE;
}
httpd_cgi_state ICACHE_FLASH_ATTR
cgiPersistRestoreDefaults(HttpdConnData *connData)
{
if (connData->conn == NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
persist_restore_default();
httpdRedirect(connData, SET_REDIR_SUC);
return HTTPD_CGI_DONE;
}
httpd_cgi_state ICACHE_FLASH_ATTR
cgiPersistRestoreHard(HttpdConnData *connData)
{
if (connData->conn == NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
persist_restore_hard_default();
httpdRedirect(connData, SET_REDIR_SUC);
return HTTPD_CGI_DONE;
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef CGIPERSIST_H
#define CGIPERSIST_H
#include "httpd.h"
httpd_cgi_state cgiPersistWriteDefaults(HttpdConnData *connData);
httpd_cgi_state cgiPersistRestoreDefaults(HttpdConnData *connData);
httpd_cgi_state cgiPersistRestoreHard(HttpdConnData *connData);
#endif
+6 -4
View File
@@ -571,10 +571,12 @@ httpd_cgi_state ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token,
else {
struct ip_info info;
wifi_get_ip_info(STATION_IF, &info);
sprintf(buff, "ip: "IPSTR", mask: "IPSTR", gw: "IPSTR,
GOOD_IP2STR(info.ip.addr),
GOOD_IP2STR(info.netmask.addr),
GOOD_IP2STR(info.gw.addr));
sprintf(buff, IPSTR, GOOD_IP2STR(info.ip.addr));
// sprintf(buff, "ip: "IPSTR", mask: "IPSTR", gw: "IPSTR,
// GOOD_IP2STR(info.ip.addr),
// GOOD_IP2STR(info.netmask.addr),
// GOOD_IP2STR(info.gw.addr));
}
}
+9 -7
View File
@@ -11,9 +11,6 @@ PersistBlock persist;
#define PERSIST_SECTOR_ID 0x3D
// This is used to force-erase the config area (when it's changed)
#define CHECKSUM_SALT 0x02
//region Persist and restore individual modules
static void ICACHE_FLASH_ATTR
@@ -81,6 +78,11 @@ persist_load(void)
{
info("[Persist] Loading stored settings from FLASH...");
dbg("sizeof(AppConfigBundle) = %d bytes", sizeof(AppConfigBundle));
dbg("sizeof(PersistBlock) = %d bytes", sizeof(PersistBlock));
dbg("sizeof(WiFiConfigBundle) = %d bytes", sizeof(WiFiConfigBundle));
dbg("sizeof(TerminalConfigBundle) = %d bytes", sizeof(TerminalConfigBundle));
bool hard_reset = false;
// Try to load
@@ -90,15 +92,15 @@ persist_load(void)
if (hard_reset ||
(compute_checksum(&persist.defaults) != persist.defaults.checksum) ||
(compute_checksum(&persist.current) != persist.current.checksum)) {
dbg("[Persist] Checksum verification: FAILED");
error("[Persist] Checksum verification: FAILED");
hard_reset = true;
} else {
dbg("[Persist] Checksum verification: PASSED");
info("[Persist] Checksum verification: PASSED");
}
if (hard_reset) {
persist_restore_hard_default();
// this also stores them to flash and applies to modues
// this also stores them to flash and applies to modules
} else {
apply_live_settings();
}
@@ -161,8 +163,8 @@ persist_set_as_default(void)
{
info("[Persist] Storing live settings as defaults..");
// current -> defaults
memcpy(&persist.defaults, &persist.current, sizeof(AppConfigBundle));
persist_store();
info("[Persist] Default settings updated.");
+19 -3
View File
@@ -13,15 +13,31 @@
#include "wifimgr.h"
#include "screen.h"
// Changing this could be used to force-erase the config area
// after a firmware upgrade
#define CHECKSUM_SALT 0x5F5F5F5F
/** Struct for current or default settings */
typedef struct {
WiFiConfigBundle wificonf;
TerminalConfigBundle termconf;
// ...
// other settings here
// ...
// --- Space for future settings ---
// Original size: 1024
//
// The size must be appropriately reduced each time something is added,
// and boolean flags defaulting to 0 should be used to detect unpopulated
// sections that must be restored to defaults on load.
//
// This ensures user settings are not lost each time they upgrade the firmware,
// which would lead to a checksum mismatch if the structure was changed and
// it grew to a different memory area.
uint8_t filler[1024];
uint32_t checksum; // computed before write and tested on load. If it doesn't match, values are reset to hard defaults.
} AppConfigBundle;
/** This is the entire data block stored in FLASH */
typedef struct {
AppConfigBundle defaults; // defaults are stored here
AppConfigBundle current; // active settings adjusted by the user
+25 -12
View File
@@ -10,6 +10,9 @@
#include "cgi_ping.h"
#include "cgi_main.h"
#include "cgi_sockets.h"
#include "cgi_network.h"
#include "cgi_appcfg.h"
#include "cgi_persist.h"
#define WIFI_PROTECT 0
#define WIFI_AUTH_NAME "wifi"
@@ -26,28 +29,38 @@ HttpdBuiltInUrl routes[] = {
// --- Web pages ---
ROUTE_TPL_FILE("/", tplScreen, "/term.tpl"),
ROUTE_TPL_FILE("/about", tplAbout, "/about.tpl"),
ROUTE_FILE("/help", "/help.tpl"),
ROUTE_TPL_FILE("/about/?", tplAbout, "/about.tpl"),
ROUTE_FILE("/help/?", "/help.tpl"),
// --- Sockets ---
ROUTE_WS(URL_WS_UPDATE, updateSockConnect),
// --- System control ---
ROUTE_CGI("/system/reset", cgiResetDevice),
ROUTE_CGI("/system/ping", cgiPing),
ROUTE_CGI("/system/reset/?", cgiResetDevice),
ROUTE_CGI("/system/ping/?", cgiPing),
// --- WiFi config ---
// --- WiFi config --- (TODO make this conditional and configurable)
#if WIFI_PROTECT
ROUTE_AUTH("/wifi*", wifiPassFn),
#endif
ROUTE_REDIRECT("/wifi/", "/wifi"),
ROUTE_TPL_FILE("/wifi", tplWlan, "/wifi.tpl"),
ROUTE_REDIRECT("/cfg/?", "/cfg/wifi"),
ROUTE_CGI("/wifi/scan", cgiWiFiScan),
ROUTE_CGI("/wifi/connect", cgiWiFiConnect),
ROUTE_CGI("/wifi/connstatus", cgiWiFiConnStatus),
ROUTE_FILE("/wifi/connecting", "/wifi_conn.tpl"),
ROUTE_CGI("/wifi/set", cgiWiFiSetParams),
ROUTE_TPL_FILE("/cfg/wifi/?", tplWlan, "/cfg_wifi.tpl"),
ROUTE_FILE("/cfg/wifi/connecting/?", "/cfg_wifi_conn.tpl"),
ROUTE_CGI("/cfg/wifi/scan", cgiWiFiScan),
ROUTE_CGI("/cfg/wifi/connstatus", cgiWiFiConnStatus),
ROUTE_CGI("/cfg/wifi/set", cgiWiFiSetParams),
ROUTE_TPL_FILE("/cfg/network/?", tplNetwork, "/cfg_network.tpl"),
ROUTE_CGI("/cfg/network/set", cgiNetworkSetParams),
ROUTE_TPL_FILE("/cfg/app/?", tplAppCfg, "/cfg_app.tpl"),
ROUTE_CGI("/cfg/app/set", cgiAppCfgSetParams),
ROUTE_FILE("/cfg/admin/?", "/cfg_admin.tpl"),
ROUTE_CGI("/cfg/admin/write_defaults", cgiPersistWriteDefaults),
ROUTE_CGI("/cfg/admin/restore_defaults", cgiPersistRestoreDefaults),
ROUTE_CGI("/cfg/admin/restore_hard", cgiPersistRestoreHard),
ROUTE_FILESYSTEM(),
ROUTE_END(),
+1 -1
View File
@@ -17,7 +17,7 @@ void terminal_restore_defaults(void)
termconf->default_fg = 7;
termconf->width = 26;
termconf->height = 10;
sprintf(termconf->title, "ESP8266 Wireless Terminal");
sprintf(termconf->title, "ESPTerm");
sprintf(termconf->btn1, "1");
sprintf(termconf->btn2, "2");
sprintf(termconf->btn3, "3");
+5
View File
@@ -43,6 +43,11 @@ wifimgr_restore_defaults(void)
IP4_ADDR(&wificonf->sta_addr.ip, 192, 168, 0, (mac[5] == 1 ? 2 : mac[5])); // avoid being the same as "default gw"
IP4_ADDR(&wificonf->sta_addr.netmask, 255, 255, 255, 0);
IP4_ADDR(&wificonf->sta_addr.gw, 192, 168, 0, 1);
// DEBUG ONLY - TODO remove for release
wificonf->opmode = STATION_MODE;
sprintf((char*)wificonf->sta_ssid, "Chlivek");
sprintf((char*)wificonf->sta_password, "prase chrochta");
}
static void ICACHE_FLASH_ATTR