controller-legacy/main.c

167 lines
3.8 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-16 19:19:56 +00:00
#include <constants.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>
#include <runners.h>
2020-04-13 22:50:55 +00:00
#include <helpers.h>
#include <wiringPi.h>
#include <piFace.h>
#include <wiring_debug.h>
2020-04-16 23:38:25 +00:00
#include <confini.h>
config_t global_config;
2020-04-13 22:50:55 +00:00
static MDB_env *mdb_env;
static controller_t *this_controller;
2020-04-16 19:19:56 +00:00
static struct pollfd poll_fds[POLL_FDS_COUNT];
2020-04-13 22:50:55 +00:00
static void
terminate(int signum)
{
LOG_INFO("terminating controller (%d)", signum);
2020-04-16 19:19:56 +00:00
for(int i = 0; i < POLL_FDS_COUNT; ++i)
{
close(poll_fds[i].fd);
}
2020-04-13 22:50:55 +00:00
mdb_env_close(mdb_env);
controller_free(this_controller);
2020-04-16 23:38:25 +00:00
free(global_config.relay_configs);
2020-04-13 22:50:55 +00:00
exit(signum);
}
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, const char** argv)
2019-11-15 00:23:43 +00:00
{
(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);
2020-04-16 23:38:25 +00:00
/******************** LOAD CONFIG ********************/
global_config.file = "controller.ini";
helpers_parse_cli(argc, argv, &global_config);
FILE * const ini_file = fopen(global_config.file, "rb");
2020-04-16 23:38:25 +00:00
if(ini_file == NULL)
{
LOG_FATAL("config file '%s' was not found", global_config.file);
2020-04-16 23:38:25 +00:00
exit(1);
}
if(load_ini_file( ini_file, INI_DEFAULT_FORMAT, NULL, helper_load_config, &global_config))
{
LOG_FATAL("unable to parse ini file");
2020-04-16 23:38:25 +00:00
exit(1);
}
fclose(ini_file);
2020-04-13 22:50:55 +00:00
if(sizeof(time_t) < 8)
{
LOG_WARN("this system is not using 8-bit time");
}
/******************** SETUP DATABASE AND THIS CONTROLLER ********************/
database_setup(&mdb_env, &global_config);
2020-01-07 01:23:16 +00:00
2020-04-13 22:50:55 +00:00
this_controller = controller_load(mdb_env);
2020-04-16 19:19:56 +00:00
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);
2020-04-13 22:50:55 +00:00
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-17 23:50:36 +00:00
wiringPiSetup();
piFaceSetup(PIFACE_GPIO_BASE);
for(uint_fast8_t i = 0; i < this_controller->relay_count; ++i)
{
if(global_config.relay_configs[i].driver == RELAY_DRIVER_GPIO)
{
pinMode(global_config.relay_configs[i].pin, OUTPUT);
}
}
2020-04-13 22:50:55 +00:00
/******************** SETUP SOCKETS ********************/
2020-02-23 00:13:27 +00:00
int timeout_msecs = ACCEPT_TIMEOUT_MSECONDS;
int ret;
/* Open STREAMS device. */
2020-04-16 19:19:56 +00:00
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);
2020-04-13 22:50:55 +00:00
/******************** CHECK FOR TESTING RUN ********************/
if(global_config.run_type == RUN_TYPE_TEST)
{
runner_test(this_controller);
2020-04-17 23:28:24 +00:00
terminate(0);
}
2020-04-13 22:50:55 +00:00
/******************** 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
{
2020-04-16 19:19:56 +00:00
ret = poll(poll_fds, POLL_FDS_COUNT, timeout_msecs);
2020-02-23 00:13:27 +00:00
if(ret == 0)
{
2020-04-16 19:19:56 +00:00
handler_loop(this_controller);
2020-02-23 00:13:27 +00:00
}
2020-04-13 22:50:55 +00:00
if(ret > 0)
{
2020-04-16 19:19:56 +00:00
handler_poll(poll_fds, this_controller, mdb_env);
2020-04-13 22:50:55 +00:00
}
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;
}