From 5d5e7c4a46bbc2789edc4263ddf2573cd2dabc06 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Mon, 17 Jun 2024 15:10:51 +0300 Subject: [PATCH] seems like it works --- schlagbaum/client.c | 4 +- schlagbaum/gpio.c | 107 ++++++++++++++++------------- schlagbaum/gpio.h | 8 ++- schlagbaum/schlagbaum.config | 2 + schlagbaum/schlagbaum.creator.user | 2 +- schlagbaum/server.c | 13 ++-- schlagbaum/sslsock.c | 24 ++++--- 7 files changed, 92 insertions(+), 68 deletions(-) diff --git a/schlagbaum/client.c b/schlagbaum/client.c index d1cdaa9..78c97a3 100644 --- a/schlagbaum/client.c +++ b/schlagbaum/client.c @@ -69,7 +69,7 @@ static void readssl(SSL *ssl){ if(bytes > 0){ buf[bytes] = 0; verbose(1, "Received: \"%s\"", buf); - handle_message(buf, client_out_gpios); + if(!G.commands) handle_message(buf, client_out_gpios); // don't react on incoming messages if just send commands }else if(bytes < 0){ LOGWARN("Server disconnected or other error"); ERRX("Disconnected"); @@ -114,7 +114,7 @@ void clientproc(SSL_CTX *ctx, int fd){ } while(1){ #ifdef __arm__ - poll_gpio(&ssl, 1, client_in_gpios); + poll_gpio(&ssl, -1, client_in_gpios); #endif readssl(ssl); } diff --git a/schlagbaum/gpio.c b/schlagbaum/gpio.c index 85e659f..bb11eff 100644 --- a/schlagbaum/gpio.c +++ b/schlagbaum/gpio.c @@ -39,7 +39,10 @@ static const int gpio_inputs[GPIO_IN_NUMBER] = {18, 23, 24, 25, 8, 7}; static const int gpio_outputs[GPIO_OUT_NUMBER] = {17, 27, 22, 10, 9, 11}; // last time GPIO was activated -static double gpio_set_time[GPIO_OUT_NUMBER] = {-1., -1., -1., -1., -1., -1.}; +static double gpio_clear_time[GPIO_OUT_NUMBER] = {1., 1., 1., 1., 1., 1.}; +// last GPIO event times & event values +static double gpio_in_time[GPIO_IN_NUMBER] = {0.}; +static enum gpio_v2_line_event_id gpio_in_event_id[GPIO_IN_NUMBER] = {0}; /** * @brief gpio_chkclr - clear outputs by timeout @@ -47,9 +50,9 @@ static double gpio_set_time[GPIO_OUT_NUMBER] = {-1., -1., -1., -1., -1., -1.}; static void gpio_chkclr(){ double tnow = dtime(); for(int i = 0; i < GPIO_OUT_NUMBER; ++i){ - if(t[i] < 0.) continue; - if(tnow - t[i] < GPIO_TIMEOUT) continue; - gpio_clear_output(gpio_outputs[i]); + if(gpio_clear_time[i] < 0.) continue; + if(tnow - gpio_clear_time[i] < GPIO_TIMEOUT) continue; + gpio_set_output(gpio_outputs[i]); } } @@ -94,30 +97,32 @@ int gpio_setup_outputs(){ rq_out.offsets[i] = gpio_outputs[i]; snprintf(rq_out.consumer, GPIO_MAX_NAME_SIZE-1, "outputs"); rq_out.num_lines = GPIO_OUT_NUMBER; - rq_out.config.flags = GPIO_V2_LINE_FLAG_OUTPUT | GPIO_V2_LINE_FLAG_OPEN_DRAIN | GPIO_V2_LINE_FLAG_ACTIVE_LOW | GPIO_V2_LINE_FLAG_BIAS_DISABLED; + rq_out.config.flags = GPIO_V2_LINE_FLAG_OUTPUT | GPIO_V2_LINE_FLAG_BIAS_DISABLED; rq_out.config.num_attrs = 0; if(-1 == ioctl(gpiofd, GPIO_V2_GET_LINE_IOCTL, &rq_out)){ LOGERR("Unable setup outputs: %s", strerror(errno)); WARNX("Can't setup outputs"); return -1; } + gpio_chkclr(); // set all outputs + DBG("Outputs are ready"); return rq_out.fd; } -static int gpio_setreset(int input, int set){ +static int gpio_setreset(int output, int set){ int idx = -1; - for(int i = 0; i < GPIO_IN_NUMBER; ++i){ - if(gpio_inputs[i] == input){ + for(int i = 0; i < GPIO_OUT_NUMBER; ++i){ + if(gpio_outputs[i] == output){ idx = i; break; } } - DBG("idx = %d", idx); if(idx < 0 || idx > GPIO_OUT_NUMBER) return FALSE; + if(set == 0 && (dtime() - gpio_clear_time[idx] < GPIO_SETTMOUT)) return FALSE; // time! struct gpio_v2_line_values values; bzero(&values, sizeof(values)); uint64_t val = (1< - + EnvironmentId diff --git a/schlagbaum/server.c b/schlagbaum/server.c index 9784f50..409c84e 100644 --- a/schlagbaum/server.c +++ b/schlagbaum/server.c @@ -23,10 +23,10 @@ #ifdef __arm__ #include "gpio.h" -// GPIO25 - LEDopen, 8 - LEDclose +// GPIO7 - LEDopen, 8 - LEDclose static cmd_t server_in_gpios[] = { - {25, CMD_LED0}, - {8, CMD_LED1}, + {8, CMD_LED0}, + {7, CMD_LED1}, {0, NULL} }; @@ -56,9 +56,10 @@ static int handle_connection(SSL *ssl){ LOGDBG("fd=%d, message=%s", sd, buf); const char *ans = "FAIL"; #ifdef __arm__ - if(strcmp(buf, CMD_OPEN) || strcmp(buf, CMD_CLOSE)){ // shut both 17/27 channels before run cmd - gpio_clear_output(17); - gpio_clear_output(27); + if(0 == strcmp(buf, CMD_OPEN) || 0 == strcmp(buf, CMD_CLOSE)){ // shut both 17/27 channels before run cmd + DBG("Got cmd %s -> 1st close all", buf); + gpio_set_output(17); + gpio_set_output(27); usleep(100000); // wait a little } #endif diff --git a/schlagbaum/sslsock.c b/schlagbaum/sslsock.c index a700d1c..4038f39 100644 --- a/schlagbaum/sslsock.c +++ b/schlagbaum/sslsock.c @@ -30,6 +30,7 @@ #else #include "client.h" #endif +#include "gpio.h" #ifdef SERVER static int OpenConn(int port){ @@ -203,7 +204,7 @@ int read_string(SSL *ssl, char *buf, int l){ /** * @brief poll_gpio - GPIO polling * @param ssl - ssl array to write - * @param nfd - amount of descriptors (+1 - starting frol ssls[1]) + * @param nfd - amount of descriptors (+1 - starting frol ssls[1]) for server or -1 for client */ void poll_gpio(SSL **ssls, int nfd, cmd_t *commands){ static double t0 = 0.; @@ -211,15 +212,22 @@ void poll_gpio(SSL **ssls, int nfd, cmd_t *commands){ char buf[64]; uint32_t up, down; t0 = dtime(); - if(gpio_poll(&up, &down) <= 0 || !up) return; + if(gpio_poll(&up, &down) <= 0 || !down) return; + DBG("DOWN=%d, nfd=%d", down, nfd); for(cmd_t *c = commands; c->cmd; ++c){ - if(c->gpio != up) continue; + DBG("Test %d - %s", c->gpio, c->cmd); + if(c->gpio != down) continue; + DBG("Got event %s", c->cmd); sprintf(buf, "%s\n", c->cmd); int l = strlen(buf); - if(nfd == 1){ + if(!ssls) return; + if(nfd == -1){ + DBG("Write to server"); if(SSL_write(ssls[0], buf, l) <= 0) WARNX("SSL write error"); }else{ + DBG("Write to all %d clients", nfd-1); for(int i = nfd-1; i > 0; --i){ + DBG("ssls[%d]", i); if(SSL_write(ssls[i], buf, l) <= 0){ WARNX("SSL write error"); } @@ -237,12 +245,12 @@ int handle_message(const char *msg, cmd_t *gpios){ int ret = FALSE; for(cmd_t *c = gpios; c->cmd; ++c){ if(strcmp(msg, c->cmd)) continue; - DBG("set pin %d", c->gpio); + DBG("set pin %d (to 0)", c->gpio); #ifdef __arm__ - if(!gpio_set_output(pin)) LOGERR("Can't change state according to pin %d", pin); + if(!gpio_clear_output(c->gpio)) LOGERR("Can't change state according to pin %d", c->gpio); else{ - LOGMSG("%s gpio %d", act == 1 ? "Set" : "Reset", pin); - verbose(1, "%s gpio %d", act == 1 ? "Set" : "Reset", pin); + LOGMSG("RESET gpio %d", c->gpio); + verbose(1, "RESET gpio %d", c->gpio); ret = TRUE; } #endif