add: mongoose for connections and mqtt
This commit is contained in:
parent
f5f9be803c
commit
1d1ae61310
14 changed files with 22620 additions and 233 deletions
src
81
src/main.c
81
src/main.c
|
@ -3,12 +3,10 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <lmdb.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <mongoose.h>
|
||||
#include <models/controller.h>
|
||||
#include <database.h>
|
||||
#include <config.h>
|
||||
|
@ -24,26 +22,28 @@
|
|||
#include <confini.h>
|
||||
|
||||
config_t global_config;
|
||||
controller_t *global_controller;
|
||||
MDB_env *global_mdb_env;
|
||||
|
||||
static MDB_env *mdb_env;
|
||||
static controller_t *this_controller;
|
||||
static struct pollfd poll_fds[POLL_FDS_COUNT];
|
||||
static struct mg_mgr mgr;
|
||||
|
||||
static void
|
||||
terminate(int signum)
|
||||
{
|
||||
LOG_INFO("terminating controller (%d)\n", signum);
|
||||
|
||||
for(int i = 0; i < POLL_FDS_COUNT; ++i)
|
||||
{
|
||||
close(poll_fds[i].fd);
|
||||
}
|
||||
LOG_DEBUG("freeing mongoose manager\n");
|
||||
mg_mgr_free(&mgr);
|
||||
|
||||
mdb_env_close(mdb_env);
|
||||
LOG_DEBUG("closing database\n");
|
||||
mdb_env_close(global_mdb_env);
|
||||
|
||||
controller_free(this_controller);
|
||||
LOG_DEBUG("freeing global controller\n");
|
||||
controller_free(global_controller);
|
||||
|
||||
LOG_DEBUG("freeing database config\n");
|
||||
free(global_config.database);
|
||||
LOG_DEBUG("freeing relay configs config\n");
|
||||
free(global_config.relay_configs);
|
||||
|
||||
exit(signum);
|
||||
|
@ -93,18 +93,27 @@ main(int argc, const char** argv)
|
|||
LOG_WARN("this system is not using 8-bit time\n");
|
||||
}
|
||||
|
||||
/******************** SETUP DATABASE AND THIS CONTROLLER ********************/
|
||||
|
||||
database_setup(&mdb_env, &global_config);
|
||||
/******************** SETUP DATABASE, SOCKETS AND THIS CONTROLLER ********************/
|
||||
|
||||
this_controller = controller_load(mdb_env);
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
|
||||
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);
|
||||
database_setup(&global_mdb_env, &global_config);
|
||||
|
||||
this_controller->command_port = helper_get_port(fd_command);
|
||||
global_controller = controller_load(global_mdb_env);
|
||||
|
||||
controller_save(this_controller, mdb_env);
|
||||
char address[100];
|
||||
sprintf(address, "udp://0.0.0.0:%u", global_controller->discovery_port);
|
||||
struct mg_connection *c_discovery = mg_bind(&mgr, address, handler_discovery);
|
||||
sprintf(address, "tcp://0.0.0.0:%u", global_controller->command_port);
|
||||
struct mg_connection *c_command = mg_bind(&mgr, address, handler_command);
|
||||
sprintf(address, "tcp://localhost:%u", 1883);
|
||||
struct mg_connection *c_mqtt = mg_connect(&mgr, address, handler_mqtt);
|
||||
|
||||
(void)c_discovery; // still unused
|
||||
global_controller->command_port = helper_get_port(c_command->sock);
|
||||
|
||||
controller_save(global_controller, global_mdb_env);
|
||||
|
||||
|
||||
/******************** SETUP WIRINGPI ********************/
|
||||
|
@ -112,7 +121,7 @@ main(int argc, const char** argv)
|
|||
wiringPiSetup();
|
||||
piFaceSetup(PIFACE_GPIO_BASE);
|
||||
|
||||
for(uint_fast8_t i = 0; i < this_controller->relay_count; ++i)
|
||||
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
|
||||
{
|
||||
if(global_config.relay_configs[i].driver == RELAY_DRIVER_GPIO)
|
||||
{
|
||||
|
@ -121,25 +130,11 @@ main(int argc, const char** argv)
|
|||
}
|
||||
|
||||
|
||||
/******************** SETUP SOCKETS ********************/
|
||||
|
||||
int timeout_msecs = ACCEPT_TIMEOUT_MSECONDS;
|
||||
int ret;
|
||||
|
||||
/* Open STREAMS device. */
|
||||
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\n", 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\n", fd_command, POLL_FDS_COMMAND);
|
||||
|
||||
|
||||
/******************** CHECK FOR TESTING RUN ********************/
|
||||
|
||||
if(global_config.run_type == RUN_TYPE_TEST)
|
||||
{
|
||||
runner_test(this_controller);
|
||||
runner_test(global_controller);
|
||||
terminate(0);
|
||||
}
|
||||
|
||||
|
@ -148,21 +143,11 @@ main(int argc, const char** argv)
|
|||
|
||||
for(;;)
|
||||
{
|
||||
ret = poll(poll_fds, POLL_FDS_COUNT, timeout_msecs);
|
||||
|
||||
if(ret == 0)
|
||||
{
|
||||
handler_loop(this_controller);
|
||||
}
|
||||
if(ret > 0)
|
||||
{
|
||||
handler_poll(poll_fds, this_controller, mdb_env);
|
||||
}
|
||||
mg_mgr_poll(&mgr, 1000);
|
||||
handler_loop(c_mqtt);
|
||||
}
|
||||
|
||||
close(fd_discovery);
|
||||
|
||||
mdb_env_close(mdb_env);
|
||||
terminate(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue