diff --git a/drivers/gpio.c b/drivers/gpio.c index fdff05e..11c370e 100644 --- a/drivers/gpio.c +++ b/drivers/gpio.c @@ -7,5 +7,8 @@ void driver_gpio_set(relay_t *relay, int value) { + // disable "unused parameter" warning (happens when using wiring_debug) + (void)relay; + (void)value; digitalWrite(relay->number, value); } diff --git a/drivers/piface.c b/drivers/piface.c index 2cea3bb..ecc8c2f 100644 --- a/drivers/piface.c +++ b/drivers/piface.c @@ -7,5 +7,8 @@ void driver_piface_set(relay_t *relay, int value) { + // disable "unused parameter" warning (happens when using wiring_debug) + (void)relay; + (void)value; digitalWrite(DRIVER_PIFACE_GPIO_BASE + relay->number, value); } diff --git a/handlers/loop.c b/handlers/loop.c new file mode 100644 index 0000000..be7042e --- /dev/null +++ b/handlers/loop.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +void +handler_loop(controller_t *controller) +{ + LOG_DEBUG("===== IDLE LOOP START ====="); + for(uint_fast8_t i = 0; i < controller->relay_count; ++i) + { + relay_t *relay = controller->relays[i]; + if(relay_is_active(relay, time(NULL))) + { + LOG_DEBUG("relay %d is active", i); + if(relay->number >= 2) + { + driver_gpio_set(relay, HIGH); + } + else + { + driver_piface_set(relay, HIGH); + } + } + else + { + if(relay->number >= 2) + { + driver_gpio_set(relay, LOW); + } + else + { + driver_piface_set(relay, LOW); + } + } + } +} diff --git a/handlers/poll.c b/handlers/poll.c new file mode 100644 index 0000000..d75f9d7 --- /dev/null +++ b/handlers/poll.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void +handler_poll(struct pollfd *fds, controller_t *controller, MDB_env *mdb_env) +{ + /* An event on one of the fds has occurred. */ + for(int i = 0; i < POLL_FDS_COUNT; i++) { + if(fds[i].revents & POLLIN) + { + /* data may be read on device number i. */ + LOG_DEBUG("fd %i may read data", fds[i].fd); + switch(i) + { + case POLL_FDS_DISCOVERY: + handler_discovery(fds[i].fd, controller); + break; + case POLL_FDS_COMMAND: + handler_command(fds[i].fd, controller); + controller_save(controller, mdb_env); + break; + } + } + if(fds[i].revents & POLLHUP) + { + /* A hangup has occurred on device number i. */ + LOG_DEBUG("fd %i got closed", fds[i].fd); + } + } +} diff --git a/include/constants.h b/include/constants.h index 5bfcb5b..dcc9a1e 100644 --- a/include/constants.h +++ b/include/constants.h @@ -1,3 +1,5 @@ #define SECONDS_PER_DAY 86400 // 60 * 60 * 24 #define SECONDS_PER_MINUTE 60 + +#define POLL_FDS_COUNT 2 diff --git a/include/enums.h b/include/enums.h index 9a03afe..c009ada 100644 --- a/include/enums.h +++ b/include/enums.h @@ -3,8 +3,8 @@ enum poll_fgs { - POLL_FGS_DISCOVERY, - POLL_FGS_COMMAND + POLL_FDS_DISCOVERY, + POLL_FDS_COMMAND }; enum discovery_mapping diff --git a/include/handlers.h b/include/handlers.h index 0b52d64..9e45b35 100644 --- a/include/handlers.h +++ b/include/handlers.h @@ -1,6 +1,8 @@ #ifndef CONTROLLER_HANDLERS_H #define CONTROLLER_HANDLERS_H +#include + #include /** @@ -21,4 +23,10 @@ handler_command(int fd, controller_t *controller); void handler_discovery(int fd, controller_t *controller); +void +handler_loop(controller_t *this_controller); + +void +handler_poll(struct pollfd *fds, controller_t *controller, MDB_env *mdb_env); + #endif /* CONTROLLER_HANDLERS_H */ diff --git a/main.c b/main.c index 9d39f47..0e1752e 100644 --- a/main.c +++ b/main.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -21,17 +22,18 @@ #include static MDB_env *mdb_env; -static int fd_discovery; -static int fd_command; static controller_t *this_controller; +static struct pollfd poll_fds[POLL_FDS_COUNT]; static void terminate(int signum) { LOG_INFO("terminating controller (%d)", signum); - close(fd_discovery); - close(fd_command); + for(int i = 0; i < POLL_FDS_COUNT; ++i) + { + close(poll_fds[i].fd); + } mdb_env_close(mdb_env); @@ -40,34 +42,6 @@ terminate(int signum) exit(signum); } -static void -handle_poll(struct pollfd *fds, int fd_count) -{ - /* An event on one of the fds has occurred. */ - for(int i = 0; i < fd_count; i++) { - if(fds[i].revents & POLLIN) - { - /* data may be read on device number i. */ - LOG_DEBUG("fd %i may read data", fds[i].fd); - switch(i) - { - case POLL_FGS_DISCOVERY: - handler_discovery(fd_discovery, this_controller); - break; - case POLL_FGS_COMMAND: - handler_command(fd_command, this_controller); - controller_save(this_controller, mdb_env); - break; - } - } - if(fds[i].revents & POLLHUP) - { - /* A hangup has occurred on device number i. */ - LOG_DEBUG("fd %i got closed", fds[i].fd); - } - } -} - /** * @brief The main function * @@ -97,8 +71,8 @@ main(int argc, char** argv) this_controller = controller_load(mdb_env); - fd_discovery = helper_open_discovery_socket(this_controller->discovery_port); - fd_command = helper_bind_tcp_server("0.0.0.0", this_controller->command_port, 128); + int fd_discovery = helper_open_discovery_socket(this_controller->discovery_port); + int fd_command = helper_bind_tcp_server("0.0.0.0", this_controller->command_port, 128); this_controller->command_port = helper_get_port(fd_command); @@ -113,61 +87,30 @@ main(int argc, char** argv) /******************** SETUP SOCKETS ********************/ - struct pollfd fds[2]; int timeout_msecs = ACCEPT_TIMEOUT_MSECONDS; int ret; - int fd_count = 0; /* Open STREAMS device. */ - fds[POLL_FGS_DISCOVERY].fd = fd_discovery; - fds[POLL_FGS_DISCOVERY].events = POLLIN; - LOG_DEBUG("setup fd_discovery as %i on index %i", fd_discovery, fd_count); - fd_count++; - fds[POLL_FGS_COMMAND].fd = fd_command; - fds[POLL_FGS_COMMAND].events = POLLIN; - LOG_DEBUG("setup fd_command as %i on index %i", fd_command, fd_count); - fd_count++; + poll_fds[POLL_FDS_DISCOVERY].fd = fd_discovery; + poll_fds[POLL_FDS_DISCOVERY].events = POLLIN; + LOG_DEBUG("setup fd_discovery as %i on index %i", fd_discovery, POLL_FDS_DISCOVERY); + poll_fds[POLL_FDS_COMMAND].fd = fd_command; + poll_fds[POLL_FDS_COMMAND].events = POLLIN; + LOG_DEBUG("setup fd_command as %i on index %i", fd_command, POLL_FDS_COMMAND); /******************** START MAIN LOOP ********************/ for(;;) { - ret = poll(fds, fd_count, timeout_msecs); + ret = poll(poll_fds, POLL_FDS_COUNT, timeout_msecs); if(ret == 0) { - LOG_DEBUG("===== IDLE LOOP START ====="); - for(uint_fast8_t i = 0; i < this_controller->relay_count; ++i) - { - relay_t *relay = this_controller->relays[i]; - if(relay_is_active(relay, time(NULL))) - { - LOG_DEBUG("relay %d is active", i); - if(relay->number >= 2) - { - driver_gpio_set(relay, HIGH); - } - else - { - driver_piface_set(relay, HIGH); - } - } - else - { - if(relay->number >= 2) - { - driver_gpio_set(relay, LOW); - } - else - { - driver_piface_set(relay, LOW); - } - } - } + handler_loop(this_controller); } if(ret > 0) { - handle_poll(fds, fd_count); + handler_poll(poll_fds, this_controller, mdb_env); } }