add: relay/piface support
This commit is contained in:
parent
fa6ceb2bf4
commit
db64e4f820
34 changed files with 1255 additions and 309 deletions
|
|
@ -5,7 +5,7 @@ add_executable(controller main.c)
|
|||
|
||||
option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool" OFF)
|
||||
|
||||
SET(CMAKE_C_FLAGS "-Wall -Wextra -lwiringPi -luuid -llmdb -g")
|
||||
SET(CMAKE_C_FLAGS "-Wall -Wextra -lwiringPi -lwiringPiDev -luuid -llmdb -g")
|
||||
|
||||
string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
|
||||
add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")
|
||||
|
|
@ -16,14 +16,26 @@ if(WIRING_PI_DEBUG)
|
|||
endif(WIRING_PI_DEBUG)
|
||||
|
||||
aux_source_directory(. SRC_DIR)
|
||||
aux_source_directory(models MODEL_SRC)
|
||||
aux_source_directory(helpers HELPER_SRC)
|
||||
aux_source_directory(models MODELS_SRC)
|
||||
aux_source_directory(helpers HELPERS_SRC)
|
||||
aux_source_directory(handlers HANDLERS_SRC)
|
||||
aux_source_directory(drivers DRIVERS_SRC)
|
||||
|
||||
target_sources(controller PRIVATE ${SRC_DIR} ${MODEL_SRC} ${HELPER_SRC})
|
||||
target_sources(controller PRIVATE ${SRC_DIR} ${MODELS_SRC} ${HELPERS_SRC} ${HANDLERS_SRC} ${DRIVERS_SRC})
|
||||
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
add_custom_target(run
|
||||
COMMAND controller
|
||||
COMMAND ./controller
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
||||
)
|
||||
add_custom_target(debug
|
||||
COMMAND valgrind ./controller
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
||||
)
|
||||
add_custom_target(debug-full
|
||||
COMMAND valgrind --leak-check=full --show-leak-kinds=all ./controller
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
||||
)
|
||||
|
|
|
|||
125
discovery.c
125
discovery.c
|
|
@ -1,125 +0,0 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <uuid/uuid.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <discovery.h>
|
||||
#include <helper.h>
|
||||
#include <binn.h>
|
||||
|
||||
enum DISCOVERY_MAPPING
|
||||
{
|
||||
DISCOVERY_MAPPING_ID = 0,
|
||||
DISCOVERY_MAPPING_NAME = 1,
|
||||
DISCOVERY_MAPPING_COMMAND_PORT = 2,
|
||||
DISCOVERY_MAPPING_RELAY_COUNT = 3,
|
||||
};
|
||||
|
||||
int
|
||||
discovery_socket_open(uint16_t discovery_port)
|
||||
{
|
||||
struct addrinfo hints, *res;
|
||||
int fd, status;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; // use ipv4
|
||||
hints.ai_socktype = SOCK_DGRAM; //set socket flag
|
||||
hints.ai_flags = AI_PASSIVE; // get my IP
|
||||
|
||||
char* discovery_port_str = malloc(6 * sizeof(char));
|
||||
sprintf(discovery_port_str, "%u", discovery_port);
|
||||
|
||||
//get connection info for our computer
|
||||
if ((status = getaddrinfo(NULL, discovery_port_str, &hints, &res)) != 0)
|
||||
{
|
||||
LOG_FATAL("getaddrinfo: %s", gai_strerror(status));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
//creating socket
|
||||
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
int yes = 1;
|
||||
|
||||
// lose the pesky "Address already in use" error message
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
|
||||
{
|
||||
LOG_FATAL("setsockopt: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1)
|
||||
{
|
||||
LOG_FATAL("bind: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
LOG_DEBUG("opened discovery socket on port %u", discovery_port);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
void
|
||||
discovery_handle_discover(int fd, controller *cntrlr)
|
||||
{
|
||||
ssize_t bytes_transferred;
|
||||
uint16_t discovery_answer_port;
|
||||
struct sockaddr_in si_other;
|
||||
socklen_t slen = sizeof(si_other);
|
||||
|
||||
if((bytes_transferred = recvfrom(fd, &discovery_answer_port, sizeof(discovery_answer_port), 0, (struct sockaddr *) &si_other, &slen)) <= 0)
|
||||
{
|
||||
LOG_ERROR("received invalid discovery from %s", inet_ntoa(si_other.sin_addr));
|
||||
return;
|
||||
}
|
||||
LOG_DEBUG("received discovery from %s for port %d", inet_ntoa(si_other.sin_addr), discovery_answer_port);
|
||||
|
||||
if(discovery_answer_port == 0)
|
||||
{
|
||||
LOG_ERROR("invalid port received");
|
||||
return;
|
||||
}
|
||||
|
||||
binn *map = binn_map();
|
||||
|
||||
binn_map_set_blob(map, DISCOVERY_MAPPING_ID, &cntrlr->id, sizeof(uuid_t));
|
||||
binn_map_set_str(map, DISCOVERY_MAPPING_NAME, cntrlr->name);
|
||||
binn_map_set_uint32(map, DISCOVERY_MAPPING_COMMAND_PORT, cntrlr->command_port);
|
||||
binn_map_set_uint8(map, DISCOVERY_MAPPING_RELAY_COUNT, cntrlr->relay_count);
|
||||
|
||||
void *payload = binn_ptr(map);
|
||||
size_t payload_size = binn_size(map);
|
||||
|
||||
char discover_answer_port_str[6];
|
||||
sprintf(discover_answer_port_str, "%d", discovery_answer_port);
|
||||
int fd_answer = helper_connect_server(inet_ntoa(si_other.sin_addr), discover_answer_port_str);
|
||||
|
||||
LOG_DEBUG("size: %ld (%ld)", payload_size, sizeof(payload_size));
|
||||
|
||||
if((bytes_transferred = send(fd_answer, &payload_size, sizeof(payload_size), 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR("error during sending");
|
||||
binn_free(map);
|
||||
return;
|
||||
}
|
||||
if((bytes_transferred = send(fd_answer, payload, payload_size, 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR("error during sending");
|
||||
binn_free(map);
|
||||
return;
|
||||
}
|
||||
|
||||
close(fd_answer);
|
||||
}
|
||||
11
drivers/gpio.c
Normal file
11
drivers/gpio.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <wiringPi.h>
|
||||
#include <piFace.h>
|
||||
#include <wiring_debug.h>
|
||||
|
||||
#include <drivers.h>
|
||||
|
||||
void
|
||||
driver_gpio_set(relay_t *relay, int value)
|
||||
{
|
||||
digitalWrite(relay->number, value);
|
||||
}
|
||||
11
drivers/piface.c
Normal file
11
drivers/piface.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <wiringPi.h>
|
||||
#include <piFace.h>
|
||||
#include <wiring_debug.h>
|
||||
|
||||
#include <drivers.h>
|
||||
|
||||
void
|
||||
driver_piface_set(relay_t *relay, int value)
|
||||
{
|
||||
digitalWrite(DRIVER_PIFACE_GPIO_BASE + relay->number, value);
|
||||
}
|
||||
113
handlers/command.c
Normal file
113
handlers/command.c
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <uuid/uuid.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <handlers.h>
|
||||
#include <helpers.h>
|
||||
#include <enums.h>
|
||||
#include <binn.h>
|
||||
#include <models/schedule.h>
|
||||
|
||||
static void
|
||||
handler_command_set_schedule(binn *map, controller_t *controller)
|
||||
{
|
||||
uint8_t relay_num = binn_map_uint8(map, COMMAND_MAPPING_RELAY_NUM);
|
||||
int uuid_size = sizeof(uuid_t);
|
||||
uuid_t schedule_id;
|
||||
memmove(schedule_id, binn_map_blob(map, COMMAND_MAPPING_SCHEDULE_ID, &uuid_size), uuid_size);
|
||||
uint16_t periods_count = binn_map_uint16(map, COMMAND_MAPPING_PERIODS_COUNT);
|
||||
int periods_size = sizeof(uint16_t) * (periods_count * 2);
|
||||
uint16_t *periods = binn_map_blob(map, COMMAND_MAPPING_PERIODS_BLOB, &periods_size);
|
||||
|
||||
relay_t *target_relay = controller->relays[relay_num];
|
||||
|
||||
if(target_relay->schedule)
|
||||
{
|
||||
schedule_free(target_relay->schedule);
|
||||
}
|
||||
target_relay->schedule = schedule_create(schedule_id, periods_count, periods);
|
||||
}
|
||||
|
||||
static void
|
||||
handler_command_set_relay_name(binn *map, controller_t *controller)
|
||||
{
|
||||
uint8_t relay_num = binn_map_uint8(map, COMMAND_MAPPING_RELAY_NUM);
|
||||
char *relay_name = binn_map_str(map, COMMAND_MAPPING_NAME);
|
||||
|
||||
if(relay_num < controller->relay_count)
|
||||
{
|
||||
relay_set_name(controller->relays[relay_num], relay_name);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
handler_command(int fd, controller_t *controller)
|
||||
{
|
||||
struct sockaddr_storage their_addr;
|
||||
socklen_t addr_size;
|
||||
int client_fd;
|
||||
|
||||
addr_size = sizeof(their_addr);
|
||||
|
||||
if((client_fd = accept(fd, (struct sockaddr *) &their_addr, &addr_size)) < 0)
|
||||
{
|
||||
LOG_ERROR("could not accept client: %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
size_t payload_length;
|
||||
|
||||
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
|
||||
{
|
||||
LOG_ERROR("unable to receive header: %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
void *payload = malloc((payload_length + 1));
|
||||
ssize_t bytes_transferred;
|
||||
|
||||
if((bytes_transferred = recv(client_fd, payload, payload_length, 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR("unable to receive payload: %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t command_code = binn_map_uint8(payload, COMMAND_MAPPING_CODE);
|
||||
|
||||
LOG_INFO("received command %d", command_code);
|
||||
|
||||
switch(command_code)
|
||||
{
|
||||
case COMMAND_CODE_GET_TIME:
|
||||
break;
|
||||
case COMMAND_CODE_GET_ID:
|
||||
break;
|
||||
case COMMAND_CODE_SET_NAME:
|
||||
controller_set_name(controller, binn_map_str(payload, COMMAND_MAPPING_NAME));
|
||||
break;
|
||||
case COMMAND_CODE_GET_NAME:
|
||||
break;
|
||||
case COMMAND_CODE_SET_SCHEDULE:
|
||||
handler_command_set_schedule(payload, controller);
|
||||
break;
|
||||
case COMMAND_CODE_GET_SCHEDULE:
|
||||
break;
|
||||
case COMMAND_CODE_SET_RELAY_NAME:
|
||||
handler_command_set_relay_name(payload, controller);
|
||||
break;
|
||||
case COMMAND_CODE_GET_RELAY_NAME:
|
||||
break;
|
||||
}
|
||||
|
||||
free(payload);
|
||||
close(client_fd);
|
||||
}
|
||||
72
handlers/discovery.c
Normal file
72
handlers/discovery.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <uuid/uuid.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <handlers.h>
|
||||
#include <helpers.h>
|
||||
#include <binn.h>
|
||||
#include <enums.h>
|
||||
|
||||
void
|
||||
handler_discovery(int fd, controller_t *controller)
|
||||
{
|
||||
ssize_t bytes_transferred;
|
||||
uint16_t discovery_answer_port;
|
||||
struct sockaddr_in si_other;
|
||||
socklen_t slen = sizeof(si_other);
|
||||
|
||||
if((bytes_transferred = recvfrom(fd, &discovery_answer_port, sizeof(discovery_answer_port), 0, (struct sockaddr *) &si_other, &slen)) <= 0)
|
||||
{
|
||||
LOG_ERROR("received invalid discovery from %s", inet_ntoa(si_other.sin_addr));
|
||||
return;
|
||||
}
|
||||
LOG_DEBUG("received discovery from %s for port %d", inet_ntoa(si_other.sin_addr), discovery_answer_port);
|
||||
|
||||
if(discovery_answer_port == 0)
|
||||
{
|
||||
LOG_ERROR("invalid port received");
|
||||
return;
|
||||
}
|
||||
|
||||
binn *map = binn_map();
|
||||
|
||||
binn_map_set_blob(map, DISCOVERY_MAPPING_ID, &controller->id, sizeof(uuid_t));
|
||||
binn_map_set_str(map, DISCOVERY_MAPPING_NAME, controller->name);
|
||||
binn_map_set_uint32(map, DISCOVERY_MAPPING_COMMAND_PORT, controller->command_port);
|
||||
binn_map_set_uint8(map, DISCOVERY_MAPPING_RELAY_COUNT, controller->relay_count);
|
||||
|
||||
void *payload = binn_ptr(map);
|
||||
size_t payload_size = binn_size(map);
|
||||
|
||||
int fd_answer = helper_connect_tcp_server(inet_ntoa(si_other.sin_addr), discovery_answer_port);
|
||||
if(fd_answer == -1)
|
||||
{
|
||||
LOG_ERROR("error during connecting");
|
||||
binn_free(map);
|
||||
return;
|
||||
}
|
||||
|
||||
if((bytes_transferred = send(fd_answer, &payload_size, sizeof(payload_size), 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR("error during sending");
|
||||
binn_free(map);
|
||||
return;
|
||||
}
|
||||
if((bytes_transferred = send(fd_answer, payload, payload_size, 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR("error during sending");
|
||||
binn_free(map);
|
||||
return;
|
||||
}
|
||||
|
||||
close(fd_answer);
|
||||
}
|
||||
51
helpers/bind_server.c
Normal file
51
helpers/bind_server.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <helpers.h>
|
||||
|
||||
int
|
||||
helper_bind_tcp_server(char* addr, uint16_t port, int max_client_backlog)
|
||||
{
|
||||
char port_str[6];
|
||||
sprintf(port_str, "%d", port);
|
||||
|
||||
struct addrinfo hints, *res;
|
||||
int fd;
|
||||
int status;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
if ((status = getaddrinfo(addr, port_str, &hints, &res)) != 0)
|
||||
{
|
||||
LOG_ERROR("getaddrinfo: %s", gai_strerror(status));
|
||||
return -1;
|
||||
}
|
||||
|
||||
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
|
||||
if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1)
|
||||
{
|
||||
LOG_ERROR("error binding socket: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((status = listen(fd, max_client_backlog)) == -1)
|
||||
{
|
||||
LOG_ERROR("error setting up listener: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
|
@ -5,33 +5,35 @@
|
|||
#include <sys/socket.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <helper.h>
|
||||
#include <helpers.h>
|
||||
|
||||
int
|
||||
helper_connect_server(char* host, char* port)
|
||||
helper_connect_tcp_server(char* host, uint16_t port)
|
||||
{
|
||||
int s, status;
|
||||
struct addrinfo hints, *res;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; //set IP Protocol flag (IPv4 or IPv6 - we don't care)
|
||||
hints.ai_socktype = SOCK_STREAM; //set socket flag
|
||||
char port_str[6];
|
||||
sprintf(port_str, "%d", port);
|
||||
|
||||
if ((status = getaddrinfo(host, port, &hints, &res)) != 0) { //getaddrinfo() will evaluate the given address, using the hints-flags and port, and return an IP address and other server infos
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int s, status;
|
||||
struct addrinfo hints, *res;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; //set IP Protocol flag (IPv4 or IPv6 - we don't care)
|
||||
hints.ai_socktype = SOCK_STREAM; //set socket flag
|
||||
|
||||
//res got filled out by getaddrinfo() for us
|
||||
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket
|
||||
if ((status = getaddrinfo(host, port_str, &hints, &res)) != 0) { //getaddrinfo() will evaluate the given address, using the hints-flags and port, and return an IP address and other server infos
|
||||
LOG_ERROR("getaddrinfo: %s\n", gai_strerror(status));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0) {
|
||||
fprintf(stderr, "Keine Verbindung mit dem Netzwerk möglich.\n");
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//res got filled out by getaddrinfo() for us
|
||||
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket
|
||||
|
||||
freeaddrinfo(res);
|
||||
if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0) {
|
||||
LOG_ERROR("connect() failed");
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return s;
|
||||
freeaddrinfo(res);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
|
|
|||
24
helpers/get_port.c
Normal file
24
helpers/get_port.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <helpers.h>
|
||||
#include <logger.h>
|
||||
|
||||
uint16_t
|
||||
helper_get_port(int sock)
|
||||
{
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
|
||||
{
|
||||
LOG_ERROR("could not get socket name for port: %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ntohs(sin.sin_port);
|
||||
}
|
||||
}
|
||||
57
helpers/open_discovery_socket.c
Normal file
57
helpers/open_discovery_socket.c
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <helpers.h>
|
||||
|
||||
int
|
||||
helper_open_discovery_socket(uint16_t discovery_port)
|
||||
{
|
||||
struct addrinfo hints, *res;
|
||||
int fd, status;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; // use ipv4
|
||||
hints.ai_socktype = SOCK_DGRAM; //set socket flag
|
||||
hints.ai_flags = AI_PASSIVE; // get my IP
|
||||
|
||||
char discovery_port_str[6];
|
||||
sprintf(discovery_port_str, "%u", discovery_port);
|
||||
|
||||
//get connection info for our computer
|
||||
if ((status = getaddrinfo(NULL, discovery_port_str, &hints, &res)) != 0)
|
||||
{
|
||||
LOG_FATAL("getaddrinfo: %s", gai_strerror(status));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
//creating socket
|
||||
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
int yes = 1;
|
||||
|
||||
// lose the pesky "Address already in use" error message
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
|
||||
{
|
||||
LOG_FATAL("setsockopt: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1)
|
||||
{
|
||||
LOG_FATAL("bind: %s", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
LOG_INFO("opened discovery socket on port %u", discovery_port);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
|
@ -4,11 +4,11 @@
|
|||
#include <log_levels.h>
|
||||
|
||||
/**
|
||||
* @brief Limit the maximum length of a controller name
|
||||
* @brief Limit the maximum length of a controller/relay/etc name
|
||||
*
|
||||
* The NULL terminator is not included. Arrays of length #CONTROLLER_NAME_LENGTH + 1 are required.
|
||||
* The NULL terminator is not included. Arrays of length #MAX_NAME_LENGTH + 1 are required.
|
||||
*/
|
||||
#define CONTROLLER_NAME_LENGTH 128
|
||||
#define MAX_NAME_LENGTH 128
|
||||
|
||||
/**
|
||||
* @brief Maximum number of dbs for the databases for the MDB_env
|
||||
|
|
|
|||
3
include/constants.h
Normal file
3
include/constants.h
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#define SECONDS_PER_DAY 86400 // 60 * 60 * 24
|
||||
|
||||
#define SECONDS_PER_MINUTE 60
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
#ifndef CONTROLLER_DISCOVERY_H
|
||||
#define CONTROLLER_DISCOVERY_H
|
||||
|
||||
#include <models/controller.h>
|
||||
|
||||
/**
|
||||
* @brief Open socket for discovery
|
||||
*
|
||||
* Will exit program when unable to open socket.
|
||||
*
|
||||
* @param discovery_port Port number to listen on for discovery broadcasts
|
||||
*
|
||||
* @return Open socket to accept discovery broadcasts on
|
||||
*/
|
||||
int
|
||||
discovery_socket_open(uint16_t discovery_port);
|
||||
|
||||
/**
|
||||
* @brief Handle the discovery processing
|
||||
*
|
||||
* @param fd File descriptor to receive initial data from
|
||||
* @param cntrlr Controller to use for answering discovery
|
||||
*/
|
||||
void
|
||||
discovery_handle_discover(int fd, controller *cntrlr);
|
||||
|
||||
#endif /* CONTROLLER_DISCOVERY_H */
|
||||
14
include/drivers.h
Normal file
14
include/drivers.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef CONTROLLER_DRIVERS_H
|
||||
#define CONTROLLER_DRIVERS_H
|
||||
|
||||
#include <models/relay.h>
|
||||
|
||||
#define DRIVER_PIFACE_GPIO_BASE 200
|
||||
|
||||
void
|
||||
driver_piface_set(relay_t *relay, int value);
|
||||
|
||||
void
|
||||
driver_gpio_set(relay_t *relay, int value);
|
||||
|
||||
#endif /* CONTROLLER_DRIVERS_H */
|
||||
|
|
@ -1,8 +1,40 @@
|
|||
#ifndef CONTROLLER_ENUMS_H
|
||||
#define CONTROLLER_ENUMS_H
|
||||
|
||||
enum poll_fgs {
|
||||
POLL_FGS_DISCOVERY
|
||||
enum poll_fgs
|
||||
{
|
||||
POLL_FGS_DISCOVERY,
|
||||
POLL_FGS_COMMAND
|
||||
};
|
||||
|
||||
enum discovery_mapping
|
||||
{
|
||||
DISCOVERY_MAPPING_ID = 0,
|
||||
DISCOVERY_MAPPING_NAME = 1,
|
||||
DISCOVERY_MAPPING_COMMAND_PORT = 2,
|
||||
DISCOVERY_MAPPING_RELAY_COUNT = 3,
|
||||
};
|
||||
|
||||
enum control_mapping
|
||||
{
|
||||
COMMAND_MAPPING_CODE = 0,
|
||||
COMMAND_MAPPING_NAME = 1,
|
||||
COMMAND_MAPPING_RELAY_NUM = 2,
|
||||
COMMAND_MAPPING_SCHEDULE_ID = 3,
|
||||
COMMAND_MAPPING_PERIODS_COUNT = 4,
|
||||
COMMAND_MAPPING_PERIODS_BLOB = 5,
|
||||
};
|
||||
|
||||
enum command_code
|
||||
{
|
||||
COMMAND_CODE_GET_TIME = 1,
|
||||
COMMAND_CODE_GET_ID = 2,
|
||||
COMMAND_CODE_SET_NAME = 100,
|
||||
COMMAND_CODE_GET_NAME = 101,
|
||||
COMMAND_CODE_SET_SCHEDULE = 102,
|
||||
COMMAND_CODE_GET_SCHEDULE = 103,
|
||||
COMMAND_CODE_SET_RELAY_NAME = 104,
|
||||
COMMAND_CODE_GET_RELAY_NAME = 105,
|
||||
};
|
||||
|
||||
#endif /* CONTROLLER_ENUMS_H */
|
||||
|
|
|
|||
24
include/handlers.h
Normal file
24
include/handlers.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef CONTROLLER_HANDLERS_H
|
||||
#define CONTROLLER_HANDLERS_H
|
||||
|
||||
#include <models/controller.h>
|
||||
|
||||
/**
|
||||
* @brief Handle the command processing
|
||||
*
|
||||
* @param fd File descriptor to receive initial data from
|
||||
* @param controller Controller to use for answering command
|
||||
*/
|
||||
void
|
||||
handler_command(int fd, controller_t *controller);
|
||||
|
||||
/**
|
||||
* @brief Handle the discovery processing
|
||||
*
|
||||
* @param fd File descriptor to receive initial data from
|
||||
* @param controller Controller to use for answering discovery
|
||||
*/
|
||||
void
|
||||
handler_discovery(int fd, controller_t *controller);
|
||||
|
||||
#endif /* CONTROLLER_HANDLERS_H */
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
#ifndef CONTROLLER_HELPER_H
|
||||
#define CONTROLLER_HELPER_H
|
||||
|
||||
int
|
||||
helper_connect_server(char* host, char* port);
|
||||
|
||||
#endif /* CONTROLLER_HELPER_H */
|
||||
25
include/helpers.h
Normal file
25
include/helpers.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef CONTROLLER_HELPERS_H
|
||||
#define CONTROLLER_HELPERS_H
|
||||
|
||||
int
|
||||
helper_connect_tcp_server(char* host, uint16_t port);
|
||||
|
||||
int
|
||||
helper_bind_tcp_server(char* addr, uint16_t port, int max_client_backlog);
|
||||
|
||||
uint16_t
|
||||
helper_get_port(int sock);
|
||||
|
||||
/**
|
||||
* @brief Open socket for discovery
|
||||
*
|
||||
* Will exit program when unable to open socket.
|
||||
*
|
||||
* @param discovery_port Port number to listen on for discovery broadcasts
|
||||
*
|
||||
* @return Open socket to accept discovery broadcasts on
|
||||
*/
|
||||
int
|
||||
helper_open_discovery_socket(uint16_t discovery_port);
|
||||
|
||||
#endif /* CONTROLLER_HELPERS_H */
|
||||
|
|
@ -2,45 +2,52 @@
|
|||
#define CONTROLLER_LOGGER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <colors.h>
|
||||
#include <config.h>
|
||||
#include <macros.h>
|
||||
|
||||
#define _LOGGER_MESSAGE(msg) COLOR_NONE " %s:%s:%d: " msg "\n", __FILENAME__, __func__, __LINE__
|
||||
#define _LOGGER_TIMESTAMP_SIZE 32
|
||||
char _LOGGER_TIMESTAMP[_LOGGER_TIMESTAMP_SIZE];
|
||||
|
||||
char*
|
||||
logger_get_timestamp();
|
||||
|
||||
#define _LOGGER_MESSAGE(msg) COLOR_NONE " %s %s:%d:%s: " msg "\n", logger_get_timestamp(), __FILENAME__, __LINE__, __func__
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_TRACE
|
||||
#define LOG_TRACE(msg, ...) printf(COLOR_GREEN "[TRACE]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_TRACE(msg, ...) fprintf(stdout, COLOR_GREEN "[TRACE]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_TRACE(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
|
||||
#define LOG_DEBUG(msg, ...) printf(COLOR_BLUE "[DEBUG]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_DEBUG(msg, ...) fprintf(stdout, COLOR_BLUE "[DEBUG]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_DEBUG(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_INFO
|
||||
#define LOG_INFO(msg, ...) printf(COLOR_CYAN "[ INFO] " _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_INFO(msg, ...) fprintf(stdout, COLOR_CYAN "[ INFO]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_INFO(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_WARN
|
||||
#define LOG_WARN(msg, ...) printf(COLOR_YELLOW "[ WARN] " _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_WARN(msg, ...) fprintf(stdout, COLOR_YELLOW "[ WARN]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_WARN(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_ERROR
|
||||
#define LOG_ERROR(msg, ...) printf(COLOR_RED "[ERROR]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_ERROR(msg, ...) fprintf(stderr, COLOR_RED "[ERROR]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_ERROR(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_FATAL
|
||||
#define LOG_FATAL(msg, ...) printf(COLOR_MAGENTA "[FATAL]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_FATAL(msg, ...) fprintf(stderr, COLOR_MAGENTA "[FATAL]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_FATAL(msg, ...)
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
/**
|
||||
* @brief Information about this controller
|
||||
*/
|
||||
typedef struct controller
|
||||
typedef struct
|
||||
{
|
||||
/**
|
||||
* @brief A unique UUID for this controller
|
||||
|
|
@ -22,7 +22,7 @@ typedef struct controller
|
|||
*
|
||||
* Includes a \0 terminator.
|
||||
*/
|
||||
char name[CONTROLLER_NAME_LENGTH + 1];
|
||||
char name[MAX_NAME_LENGTH + 1];
|
||||
/**
|
||||
* @brief The command port the controller was bound to
|
||||
*/
|
||||
|
|
@ -35,22 +35,22 @@ typedef struct controller
|
|||
* @brief Amount of relays available to this controller
|
||||
*/
|
||||
uint8_t relay_count;
|
||||
relay **relays;
|
||||
relay_t **relays;
|
||||
|
||||
} controller;
|
||||
} controller_t;
|
||||
|
||||
/**
|
||||
* @brief Key to save controller information in database
|
||||
*/
|
||||
typedef enum controller_db_key
|
||||
typedef enum
|
||||
{
|
||||
KEY_META_ID = 0,
|
||||
KEY_META_NAME = 1,
|
||||
KEY_META_COMMAND_PORT = 2,
|
||||
KEY_META_DISCOVERY_PORT = 3,
|
||||
KEY_META_RELAY_COUNT = 4,
|
||||
KEY_META_RELAYS = 5,
|
||||
} controller_db_key;
|
||||
DB_KEY_CONTROLLER_ID = 0,
|
||||
DB_KEY_CONTROLLER_NAME = 1,
|
||||
DB_KEY_CONTROLLER_COMMAND_PORT = 2,
|
||||
DB_KEY_CONTROLLER_DISCOVERY_PORT = 3,
|
||||
DB_KEY_CONTROLLER_RELAY_COUNT = 4,
|
||||
DB_KEY_CONTROLLER_RELAYS = 5,
|
||||
} db_key_controller_e;
|
||||
|
||||
/**
|
||||
* @brief Create a new instance of controller
|
||||
|
|
@ -59,7 +59,7 @@ typedef enum controller_db_key
|
|||
*
|
||||
* @return A new instance of #controller
|
||||
*/
|
||||
controller*
|
||||
controller_t*
|
||||
controller_create(void);
|
||||
|
||||
|
||||
|
|
@ -72,20 +72,32 @@ controller_create(void);
|
|||
*
|
||||
* @return A loaded or new instance of controller or NULL on database error
|
||||
*/
|
||||
controller*
|
||||
controller_t*
|
||||
controller_load(MDB_env *mdb_env);
|
||||
|
||||
/**
|
||||
* @brief Save a controller to the database
|
||||
*
|
||||
* @param cntrlr Instance of a controller
|
||||
* @param controller Instance of a controller
|
||||
* @param mdb_env Already created MDB_env
|
||||
*
|
||||
* @return Indicator to show success (0) or failure (!0)
|
||||
*/
|
||||
int
|
||||
controller_save(controller *cntrlr, MDB_env *mdb_env);
|
||||
controller_save(controller_t *controller, MDB_env *mdb_env);
|
||||
|
||||
/**
|
||||
* @brief Sets a name to a controller.
|
||||
* This function won't perform any checks (e.g. no NULL checks)
|
||||
*
|
||||
* @param controller Set the name to this controller
|
||||
* @param name Name to be set
|
||||
*/
|
||||
void
|
||||
controller_set_name(controller_t *controller, char *name);
|
||||
|
||||
void
|
||||
controller_free(controller_t *controller);
|
||||
|
||||
/**
|
||||
* @brief Debug an instance of #controller
|
||||
|
|
@ -95,6 +107,6 @@ controller_save(controller *cntrlr, MDB_env *mdb_env);
|
|||
* @param cntrlr #controller to debug
|
||||
*/
|
||||
void
|
||||
controller_debug(controller *cntrlr);
|
||||
controller_debug(controller_t *controller);
|
||||
|
||||
#endif //CONTROLLER_CONTROLLER_H
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue