add: relay/piface support
This commit is contained in:
parent
fa6ceb2bf4
commit
db64e4f820
34 changed files with 1259 additions and 313 deletions
141
main.c
141
main.c
|
@ -1,17 +1,72 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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 <models/controller.h>
|
||||
#include <database.h>
|
||||
#include <config.h>
|
||||
#include <discovery.h>
|
||||
#include <handlers.h>
|
||||
#include <drivers.h>
|
||||
#include <enums.h>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The main function
|
||||
|
@ -27,18 +82,40 @@ main(int argc, char** argv)
|
|||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
MDB_env *mdb_env;
|
||||
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 ********************/
|
||||
|
||||
database_setup(&mdb_env);
|
||||
|
||||
controller *this_cntrlr = controller_load(mdb_env);
|
||||
controller_save(this_cntrlr, mdb_env);
|
||||
this_controller = controller_load(mdb_env);
|
||||
|
||||
int fd_discovery = discovery_socket_open(this_cntrlr->discovery_port);
|
||||
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 ********************/
|
||||
|
||||
wiringPiSetupSys();
|
||||
piFaceSetup(200);
|
||||
|
||||
|
||||
/******************** SETUP SOCKETS ********************/
|
||||
|
||||
struct pollfd fds[2];
|
||||
int timeout_msecs = ACCEPT_TIMEOUT_MSECONDS;
|
||||
int ret;
|
||||
int i;
|
||||
int fd_count = 0;
|
||||
|
||||
/* Open STREAMS device. */
|
||||
|
@ -46,6 +123,12 @@ main(int argc, char** argv)
|
|||
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++;
|
||||
|
||||
/******************** START MAIN LOOP ********************/
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
@ -54,27 +137,37 @@ main(int argc, char** argv)
|
|||
if(ret == 0)
|
||||
{
|
||||
LOG_TRACE("idle loop");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(ret > 0)
|
||||
{
|
||||
/* An event on one of the fds has occurred. */
|
||||
for(i = 0; i < fd_count; i++) {
|
||||
if(fds[i].revents & POLLIN)
|
||||
{
|
||||
/* Priority data may be read on device number i. */
|
||||
LOG_DEBUG("fd %i may read data", fds[i].fd);
|
||||
switch(i)
|
||||
{
|
||||
case POLL_FGS_DISCOVERY:
|
||||
discovery_handle_discover(fd_discovery, this_cntrlr);
|
||||
}
|
||||
}
|
||||
if(fds[i].revents & POLLHUP)
|
||||
{
|
||||
/* A hangup has occurred on device number i. */
|
||||
LOG_DEBUG("fd %i got closed", fds[i].fd);
|
||||
}
|
||||
}
|
||||
handle_poll(fds, fd_count);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue