Support for the new GPIO control web interface

master
Ondřej Hruška 6 years ago
parent d3130c43da
commit 7e4d25272a
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      front-end
  2. 121
      user/cgi_system.c
  3. 1
      user/cgi_system.h
  4. 3
      user/config_xmacros.h
  5. 1
      user/routes.c
  6. 2
      user/version.h

@ -1 +1 @@
Subproject commit f729d91651fda22ebdd94d41ebc7e0aa47a2c2e6 Subproject commit 9aae724df2ce60a144eb9f170bb09bdbd02297df

@ -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,39 +212,54 @@ cgiSystemCfgSetParams(HttpdConnData *connData)
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
static void tplSystemCfgFill(char *token, char *buff)
httpd_cgi_state ICACHE_FLASH_ATTR
tplSystemCfg(HttpdConnData *connData, char *token, void **arg)
{ {
#define BUFLEN 100 buff[0] = '\0';
char buff[BUFLEN];
if (token == NULL) {
// We're done
return HTTPD_CGI_DONE;
}
strcpy(buff, ""); // fallback
const bool admin = false; const bool admin = false;
const bool tpl=true; const bool tpl=true;
#define XSTRUCT sysconf #define XSTRUCT sysconf
#define X XGET_CGI_FUNC #define X XGET_CGI_FUNC_RETURN
XTABLE_SYSCONF XTABLE_SYSCONF
#undef X #undef X
#undef XSTRUCT #undef XSTRUCT
if (streq(token, "def_access_name")) { if (streq(token, "def_access_name")) {
sprintf(buff, "%s", DEF_ACCESS_NAME); sprintf(buff, "%s", DEF_ACCESS_NAME);
return;
} }
else if (streq(token, "def_access_pw")) {
if (streq(token, "def_access_pw")) {
sprintf(buff, "%s", DEF_ACCESS_PW); sprintf(buff, "%s", DEF_ACCESS_PW);
return;
} }
else if (streq(token, "def_admin_pw")) {
if (streq(token, "def_admin_pw")) {
sprintf(buff, "%s", DEFAULT_ADMIN_PW); sprintf(buff, "%s", DEFAULT_ADMIN_PW);
return;
}
if (streq(token, "gpio_initial")) {
buildInputsJson(buff);
return;
}
}
httpd_cgi_state ICACHE_FLASH_ATTR
tplSystemCfg(HttpdConnData *connData, char *token, void **arg)
{
#define BUFLEN 100
char buff[BUFLEN];
if (token == NULL) {
// We're done
return HTTPD_CGI_DONE;
} }
tplSystemCfgFill(token, buff);
tplSend(connData, buff, -1); tplSend(connData, buff, -1);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
@ -247,6 +276,19 @@ static void ICACHE_FLASH_ATTR tmrPulseCb(void *arg)
gpio_output_set(set_off, set_on, 0, 0); // the args are swapped here to achieve the opposite 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 * API to set GPIOs
* *
@ -255,8 +297,7 @@ static void ICACHE_FLASH_ATTR tmrPulseCb(void *arg)
*/ */
httpd_cgi_state ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData) httpd_cgi_state ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData)
{ {
#define BUFLEN 100 char buff[32];
char buff[BUFLEN];
if (connData->conn==NULL) { if (connData->conn==NULL) {
//Connection aborted. Clean up. //Connection aborted. Clean up.
@ -266,9 +307,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData)
bool set = 0; bool set = 0;
u32 inputs = gpio_input_get(); u32 inputs = gpio_input_get();
// TODO pulse option // args are do2, do4, do5, pulse. Values: 0 - off, 1 - on, t - toggle. pulse is in ms
// args are do2, do4, do5. Values: 0 - off, 1 - on, -1 - toggle
s8 set_d2 = -1, set_d4 = -1, set_d5 = -1; s8 set_d2 = -1, set_d4 = -1, set_d5 = -1;
@ -314,27 +353,15 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData)
if (duration > 0) { if (duration > 0) {
u32 cmd = 0; u32 cmd = 0;
if (set_d2 != -1) { if (set_d2 != -1) {
if (set_d2) { if (set_d2) { cmd |= 1<<2; } else { cmd |= 1<<(16+2); }
cmd |= 1<<2;
} else {
cmd |= 1<<(16+2);
}
} }
if (set_d4 != -1) { if (set_d4 != -1) {
if (set_d4) { if (set_d4) { cmd |= 1<<4; } else { cmd |= 1<<(16+4); }
cmd |= 1<<4;
} else {
cmd |= 1<<(16+4);
}
} }
if (set_d5 != -1) { if (set_d5 != -1) {
if (set_d5) { if (set_d5) { cmd |= 1<<5; } else { cmd |= 1<<(16+5); }
cmd |= 1<<5;
} else {
cmd |= 1<<(16+5);
}
} }
os_timer_disarm(&tmrPulse); os_timer_disarm(&tmrPulse);
@ -348,15 +375,15 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData)
httpdEndHeaders(connData); httpdEndHeaders(connData);
// refresh inputs // refresh inputs
inputs = gpio_input_get(); buildInputsJson(buff);
sprintf(buff, "{\"io2\":%d,\"io4\":%d,\"io5\":%d}",
((inputs&(1<<2)) != 0),
((inputs&(1<<4)) != 0),
((inputs&(1<<5)) != 0)
);
httpdSend(connData, buff, -1); 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);
}

@ -10,5 +10,6 @@ httpd_cgi_state cgiSystemCfgSetParams(HttpdConnData *connData);
httpd_cgi_state tplSystemCfg(HttpdConnData *connData, char *token, void **arg); httpd_cgi_state tplSystemCfg(HttpdConnData *connData, char *token, void **arg);
httpd_cgi_state cgiResetScreen(HttpdConnData *connData); httpd_cgi_state cgiResetScreen(HttpdConnData *connData);
httpd_cgi_state cgiGPIO(HttpdConnData *connData); httpd_cgi_state cgiGPIO(HttpdConnData *connData);
httpd_cgi_state tplGpio(HttpdConnData *connData, char *token, void **arg);
#endif // CGI_PING_H #endif // CGI_PING_H

@ -88,6 +88,9 @@ enum xset_result xset_ustring(const char *name, u8 *field, const char *buff, con
#define XGET_CGI_FUNC(type, name, suffix, deref, xget, xset, xsarg, xnotify, allow) \ #define XGET_CGI_FUNC(type, name, suffix, deref, xget, xset, xsarg, xnotify, allow) \
if ((allow) && streq(token, #name)) xget(buff, deref XSTRUCT->name); if ((allow) && streq(token, #name)) xget(buff, deref XSTRUCT->name);
#define XGET_CGI_FUNC_RETURN(type, name, suffix, deref, xget, xset, xsarg, xnotify, allow) \
if ((allow) && streq(token, #name)) { xget(buff, deref XSTRUCT->name); return; }
#define XSTRUCT_FIELD(type, name, suffix, deref, xget, xset, xsarg, xnotify, allow) \ #define XSTRUCT_FIELD(type, name, suffix, deref, xget, xset, xsarg, xnotify, allow) \
type name suffix; type name suffix;

@ -103,6 +103,7 @@ const HttpdBuiltInUrl routes[] ESP_CONST_DATA = {
ROUTE_TPL_FILE("/", tplScreen, "/term.tpl"), ROUTE_TPL_FILE("/", tplScreen, "/term.tpl"),
ROUTE_TPL_FILE("/about/?", tplAbout, "/about.tpl"), ROUTE_TPL_FILE("/about/?", tplAbout, "/about.tpl"),
ROUTE_FILE("/help/?", "/help.html"), ROUTE_FILE("/help/?", "/help.html"),
ROUTE_TPL_FILE("/cfg/gpio/?", tplGpio, "/cfg_gpio.tpl"),
// --- Sockets --- // --- Sockets ---
ROUTE_WS(URL_WS_UPDATE, updateSockConnect), ROUTE_WS(URL_WS_UPDATE, updateSockConnect),

@ -11,7 +11,7 @@
#define FW_V_MAJOR 2 #define FW_V_MAJOR 2
#define FW_V_MINOR 4 #define FW_V_MINOR 4
#define FW_V_PATCH 0 #define FW_V_PATCH 0
#define FW_V_SUFFIX "-pre1" #define FW_V_SUFFIX "-pre2"
#define FW_CODENAME "Damselfly" #define FW_CODENAME "Damselfly"
#define FW_CODENAME_QUOTED "\""FW_CODENAME"\"" #define FW_CODENAME_QUOTED "\""FW_CODENAME"\""

Loading…
Cancel
Save