controller-legacy/main.c

180 lines
4.4 KiB
C
Raw Normal View History

2020-01-07 01:23:16 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
2020-04-13 22:50:55 +00:00
#include <time.h>
2020-01-07 01:23:16 +00:00
#include <lmdb.h>
2020-02-23 00:13:27 +00:00
#include <unistd.h>
#include <sys/socket.h>
#include <poll.h>
2020-04-13 22:50:55 +00:00
#include <signal.h>
2020-01-07 01:23:16 +00:00
2020-02-08 12:50:54 +00:00
#include <logger.h>
2019-11-15 00:23:43 +00:00
#include <models/controller.h>
2020-02-09 23:58:17 +00:00
#include <database.h>
#include <config.h>
2020-04-13 22:50:55 +00:00
#include <handlers.h>
#include <drivers.h>
2020-02-23 00:13:27 +00:00
#include <enums.h>
2020-04-13 22:50:55 +00:00
#include <helpers.h>
#include <wiringPi.h>
#include <piFace.h>
#include <wiring_debug.h>
static MDB_env *mdb_env;
static int fd_discovery;
static int fd_command;
static controller_t *this_controller;
static void
terminate(int signum)
{
LOG_INFO("terminating controller (%d)", signum);
close(fd_discovery);
close(fd_command);
mdb_env_close(mdb_env);
controller_free(this_controller);
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);
}
}
}
2019-11-15 00:23:43 +00:00
2020-02-08 14:09:34 +00:00
/**
* @brief The main function
*
* @param argc UNUSED
* @param argv UNUSED
*
* @return Statuscode to indicate success (0) or failure (!0)
*/
2019-11-15 00:23:43 +00:00
int
main(int argc, char** argv)
{
(void)argc;
(void)argv;
2020-01-07 01:23:16 +00:00
2020-04-13 22:50:55 +00:00
signal(SIGINT, terminate);
signal(SIGABRT, terminate);
signal(SIGTERM, terminate);
if(sizeof(time_t) < 8)
{
LOG_WARN("this system is not using 8-bit time");
}
/******************** SETUP DATABASE AND THIS CONTROLLER ********************/
2020-02-09 23:58:17 +00:00
database_setup(&mdb_env);
2020-01-07 01:23:16 +00:00
2020-04-13 22:50:55 +00:00
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);
this_controller->command_port = helper_get_port(fd_command);
controller_save(this_controller, mdb_env);
/******************** SETUP WIRINGPI ********************/
2020-01-07 01:23:16 +00:00
2020-04-13 22:50:55 +00:00
wiringPiSetupSys();
piFaceSetup(200);
/******************** SETUP SOCKETS ********************/
2020-02-23 00:13:27 +00:00
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++;
2020-04-13 22:50:55 +00:00
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++;
/******************** START MAIN LOOP ********************/
2020-02-23 00:13:27 +00:00
2020-04-13 23:51:40 +00:00
for(;;)
2020-02-23 00:13:27 +00:00
{
ret = poll(fds, fd_count, timeout_msecs);
if(ret == 0)
{
2020-04-13 23:51:40 +00:00
LOG_DEBUG("===== IDLE LOOP START =====");
2020-04-13 22:50:55 +00:00
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)))
2020-02-23 00:13:27 +00:00
{
2020-04-13 22:50:55 +00:00
LOG_DEBUG("relay %d is active", i);
if(relay->number >= 2)
{
driver_gpio_set(relay, HIGH);
}
else
2020-02-23 00:13:27 +00:00
{
2020-04-13 22:50:55 +00:00
driver_piface_set(relay, HIGH);
2020-02-23 00:13:27 +00:00
}
}
2020-04-13 22:50:55 +00:00
else
2020-02-23 00:13:27 +00:00
{
2020-04-13 22:50:55 +00:00
if(relay->number >= 2)
{
driver_gpio_set(relay, LOW);
}
else
{
driver_piface_set(relay, LOW);
}
2020-02-23 00:13:27 +00:00
}
}
}
2020-04-13 22:50:55 +00:00
if(ret > 0)
{
handle_poll(fds, fd_count);
}
2020-02-23 00:13:27 +00:00
}
close(fd_discovery);
2020-01-07 01:23:16 +00:00
mdb_env_close(mdb_env);
2019-11-15 00:23:43 +00:00
return 0;
}