|
|
@ -10,7 +10,8 @@ |
|
|
|
#include "cgi_logging.h" |
|
|
|
#include "cgi_logging.h" |
|
|
|
|
|
|
|
|
|
|
|
#define SET_REDIR_SUC "/cfg/system" |
|
|
|
#define SET_REDIR_SUC "/cfg/system" |
|
|
|
#define SET_REDIR_ERR SET_REDIR_SUC"?err=" |
|
|
|
|
|
|
|
|
|
|
|
void buildInputsJson(char *buff); |
|
|
|
|
|
|
|
|
|
|
|
static ETSTimer tmr; |
|
|
|
static ETSTimer tmr; |
|
|
|
|
|
|
|
|
|
|
@ -82,7 +83,18 @@ cgiSystemCfgSetParams(HttpdConnData *connData) |
|
|
|
char redir_url_buf[100]; |
|
|
|
char redir_url_buf[100]; |
|
|
|
|
|
|
|
|
|
|
|
char *redir_url = redir_url_buf; |
|
|
|
char *redir_url = redir_url_buf; |
|
|
|
redir_url += sprintf(redir_url, SET_REDIR_ERR); |
|
|
|
|
|
|
|
|
|
|
|
if (GET_ARG("redir")) { |
|
|
|
|
|
|
|
strncpy(redir_url, buff, 40); |
|
|
|
|
|
|
|
u32 len = strlen(buff); |
|
|
|
|
|
|
|
if (len > 40) len = 40; |
|
|
|
|
|
|
|
redir_url += len; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
redir_url += sprintf(redir_url, SET_REDIR_SUC); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
char *end_of_redir_url = redir_url; |
|
|
|
|
|
|
|
redir_url += sprintf(redir_url, "?err="); |
|
|
|
|
|
|
|
char *end_of_failed_redir_url = redir_url; |
|
|
|
// we'll test if anything was printed by looking for \0 in failed_keys_buf
|
|
|
|
// we'll test if anything was printed by looking for \0 in failed_keys_buf
|
|
|
|
|
|
|
|
|
|
|
|
if (connData->conn == NULL) { |
|
|
|
if (connData->conn == NULL) { |
|
|
@ -174,14 +186,16 @@ cgiSystemCfgSetParams(HttpdConnData *connData) |
|
|
|
(void)redir_url; |
|
|
|
(void)redir_url; |
|
|
|
(void)uart_changed; // unused
|
|
|
|
(void)uart_changed; // unused
|
|
|
|
|
|
|
|
|
|
|
|
if (redir_url_buf[strlen(SET_REDIR_ERR)] == 0) { |
|
|
|
if (*end_of_failed_redir_url == '\0') { |
|
|
|
// All was OK
|
|
|
|
// All was OK
|
|
|
|
cgi_info("Set system params - success, saving..."); |
|
|
|
cgi_info("Set system params - success, saving..."); |
|
|
|
|
|
|
|
|
|
|
|
sysconf_apply_settings(); |
|
|
|
sysconf_apply_settings(); |
|
|
|
persist_store(); |
|
|
|
persist_store(); |
|
|
|
|
|
|
|
|
|
|
|
httpdRedirect(connData, SET_REDIR_SUC "?msg=Settings%20saved%20and%20applied."); |
|
|
|
strcpy(end_of_redir_url, "?msg=Settings%20saved%20and%20applied."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpdRedirect(connData, redir_url_buf); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
cgi_warn("Some settings did not validate, asking for correction"); |
|
|
|
cgi_warn("Some settings did not validate, asking for correction"); |
|
|
|
|
|
|
|
|
|
|
@ -198,6 +212,40 @@ cgiSystemCfgSetParams(HttpdConnData *connData) |
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void tplSystemCfgFill(char *token, char *buff) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
buff[0] = '\0'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const bool admin = false; |
|
|
|
|
|
|
|
const bool tpl=true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define XSTRUCT sysconf |
|
|
|
|
|
|
|
#define X XGET_CGI_FUNC_RETURN |
|
|
|
|
|
|
|
XTABLE_SYSCONF |
|
|
|
|
|
|
|
#undef X |
|
|
|
|
|
|
|
#undef XSTRUCT |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (streq(token, "def_access_name")) { |
|
|
|
|
|
|
|
sprintf(buff, "%s", DEF_ACCESS_NAME); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (streq(token, "def_access_pw")) { |
|
|
|
|
|
|
|
sprintf(buff, "%s", DEF_ACCESS_PW); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (streq(token, "def_admin_pw")) { |
|
|
|
|
|
|
|
sprintf(buff, "%s", DEFAULT_ADMIN_PW); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (streq(token, "gpio_initial")) { |
|
|
|
|
|
|
|
buildInputsJson(buff); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpd_cgi_state ICACHE_FLASH_ATTR |
|
|
|
httpd_cgi_state ICACHE_FLASH_ATTR |
|
|
|
tplSystemCfg(HttpdConnData *connData, char *token, void **arg) |
|
|
|
tplSystemCfg(HttpdConnData *connData, char *token, void **arg) |
|
|
@ -210,27 +258,132 @@ tplSystemCfg(HttpdConnData *connData, char *token, void **arg) |
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
strcpy(buff, ""); // fallback
|
|
|
|
tplSystemCfgFill(token, buff); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tplSend(connData, buff, -1); |
|
|
|
|
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const bool admin = false; |
|
|
|
static ETSTimer tmrPulse; |
|
|
|
const bool tpl=true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define XSTRUCT sysconf |
|
|
|
static void ICACHE_FLASH_ATTR tmrPulseCb(void *arg) |
|
|
|
#define X XGET_CGI_FUNC |
|
|
|
{ |
|
|
|
XTABLE_SYSCONF |
|
|
|
u32 mask = sysconf->gpio2_conf==GPIOCONF_OFF ? 0x30 : 0x34; |
|
|
|
#undef X |
|
|
|
u32 argu = (u32) arg; |
|
|
|
#undef XSTRUCT |
|
|
|
u32 set_on = argu & mask; |
|
|
|
|
|
|
|
u32 set_off = (argu >> 16) & mask; |
|
|
|
|
|
|
|
gpio_output_set(set_off, set_on, 0, 0); // the args are swapped here to achieve the opposite
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ICACHE_FLASH_ATTR |
|
|
|
|
|
|
|
buildInputsJson(char *buff) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
u32 inputs = gpio_input_get(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sprintf(buff, "{\"io2\":%d,\"io4\":%d,\"io5\":%d}", |
|
|
|
|
|
|
|
((inputs&(1<<2)) != 0), |
|
|
|
|
|
|
|
((inputs&(1<<4)) != 0), |
|
|
|
|
|
|
|
((inputs&(1<<5)) != 0) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* API to set GPIOs |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param connData |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
httpd_cgi_state ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char buff[32]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (connData->conn==NULL) { |
|
|
|
|
|
|
|
//Connection aborted. Clean up.
|
|
|
|
|
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool set = 0; |
|
|
|
|
|
|
|
u32 inputs = gpio_input_get(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// args are do2, do4, do5, pulse. Values: 0 - off, 1 - on, t - toggle. pulse is in ms
|
|
|
|
|
|
|
|
|
|
|
|
if (streq(token, "def_access_name")) { |
|
|
|
s8 set_d2 = -1, set_d4 = -1, set_d5 = -1; |
|
|
|
sprintf(buff, "%s", DEF_ACCESS_NAME); |
|
|
|
|
|
|
|
|
|
|
|
if (sysconf->gpio2_conf == GPIOCONF_OUT_START_0 || sysconf->gpio2_conf == GPIOCONF_OUT_START_1) { |
|
|
|
|
|
|
|
if (GET_ARG("do2")) { |
|
|
|
|
|
|
|
if (buff[0] == 't') { |
|
|
|
|
|
|
|
set = ((inputs & (1<<2)) == 0); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
set = buff[0] == '1'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
set_d2 = set; |
|
|
|
|
|
|
|
gpio_output_set((uint32) (set << 2), (uint32) ((!set) << 2), 0, 0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (streq(token, "def_access_pw")) { |
|
|
|
|
|
|
|
sprintf(buff, "%s", DEF_ACCESS_PW); |
|
|
|
if (sysconf->gpio4_conf == GPIOCONF_OUT_START_0 || sysconf->gpio4_conf == GPIOCONF_OUT_START_1) { |
|
|
|
|
|
|
|
if (GET_ARG("do4")) { |
|
|
|
|
|
|
|
if (buff[0] == 't') { |
|
|
|
|
|
|
|
set = ((inputs & (1<<4)) == 0); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
set = buff[0] == '1'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
set_d4 = set; |
|
|
|
|
|
|
|
gpio_output_set((uint32) (set << 4), (uint32) ((!set) << 4), 0, 0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (streq(token, "def_admin_pw")) { |
|
|
|
|
|
|
|
sprintf(buff, "%s", DEFAULT_ADMIN_PW); |
|
|
|
if (sysconf->gpio5_conf == GPIOCONF_OUT_START_0 || sysconf->gpio5_conf == GPIOCONF_OUT_START_1) { |
|
|
|
|
|
|
|
if (GET_ARG("do5")) { |
|
|
|
|
|
|
|
if (buff[0] == 't') { |
|
|
|
|
|
|
|
set = ((inputs & (1<<5)) == 0); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
set = buff[0] == '1'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
set_d5 = set; |
|
|
|
|
|
|
|
gpio_output_set((uint32) (set << 5), (uint32) ((!set) << 5), 0, 0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
tplSend(connData, buff, -1); |
|
|
|
if (GET_ARG("pulse")) { |
|
|
|
|
|
|
|
int duration = atoi(buff); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (duration > 0) { |
|
|
|
|
|
|
|
u32 cmd = 0; |
|
|
|
|
|
|
|
if (set_d2 != -1) { |
|
|
|
|
|
|
|
if (set_d2) { cmd |= 1<<2; } else { cmd |= 1<<(16+2); } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (set_d4 != -1) { |
|
|
|
|
|
|
|
if (set_d4) { cmd |= 1<<4; } else { cmd |= 1<<(16+4); } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (set_d5 != -1) { |
|
|
|
|
|
|
|
if (set_d5) { cmd |= 1<<5; } else { cmd |= 1<<(16+5); } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
os_timer_disarm(&tmrPulse); |
|
|
|
|
|
|
|
os_timer_setfn(&tmrPulse, tmrPulseCb, (void*) cmd); |
|
|
|
|
|
|
|
os_timer_arm(&tmrPulse, duration, false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpdStartResponse(connData, 200); |
|
|
|
|
|
|
|
httpdHeader(connData, "Content-Type", "application/json"); |
|
|
|
|
|
|
|
httpdEndHeaders(connData); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// refresh inputs
|
|
|
|
|
|
|
|
buildInputsJson(buff); |
|
|
|
|
|
|
|
httpdSend(connData, buff, -1); |
|
|
|
|
|
|
|
|
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
return HTTPD_CGI_DONE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** "GPIO" page */ |
|
|
|
|
|
|
|
httpd_cgi_state ICACHE_FLASH_ATTR |
|
|
|
|
|
|
|
tplGpio(HttpdConnData *connData, char *token, void **arg) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return tplSystemCfg(connData, token, arg); |
|
|
|
|
|
|
|
} |
|
|
|