2020-05-05 23:05:36 +00:00
|
|
|
#include <cJSON.h>
|
|
|
|
#include <constants.h>
|
|
|
|
#include <endpoints/api_v1_controllers.h>
|
|
|
|
#include <models/controller.h>
|
|
|
|
#include <logger.h>
|
|
|
|
#include <mpack.h>
|
|
|
|
|
|
|
|
#define DISCOVERY_TIMEOUT_MS 2000
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
DISCOVERY_MAPPING_ID = 0,
|
|
|
|
DISCOVERY_MAPPING_NAME = 1,
|
|
|
|
DISCOVERY_MAPPING_COMMAND_PORT = 2,
|
|
|
|
DISCOVERY_MAPPING_RELAY_COUNT = 3,
|
|
|
|
} discovery_mapping_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
bind_tcp_server(const char *addr, const char *port, int max_client_backlog)
|
|
|
|
{
|
|
|
|
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, &hints, &res)) != 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error getting address info: %s\n", gai_strerror(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
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\n", status);
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = listen(fd, max_client_backlog)) == -1)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error setting up listener: %s\n", status);
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_server_port(int fd)
|
|
|
|
{
|
|
|
|
if(fd == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
socklen_t addr_len = sizeof(sin);
|
|
|
|
if(getsockname(fd, (struct sockaddr *)&sin, &addr_len) == 0)
|
|
|
|
{
|
|
|
|
return ntohs(sin.sin_port);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length)
|
|
|
|
{
|
|
|
|
struct sockaddr_in their_addr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error creating socket\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int broadcast = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) < 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error setting broadcast\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&their_addr, 0, sizeof(their_addr));
|
|
|
|
their_addr.sin_family = AF_INET;
|
|
|
|
their_addr.sin_port = htons(port);
|
|
|
|
their_addr.sin_addr.s_addr = inet_addr(addr);
|
|
|
|
|
|
|
|
if(sendto(fd, message, length, 0, (struct sockaddr *)&their_addr, sizeof(their_addr)) < 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error sending broadcast (%d): '%s'\n", errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
|
|
|
{
|
|
|
|
int discover_server_socket = bind_tcp_server("0.0.0.0", "0", 20);
|
|
|
|
int discover_server_port = get_server_port(discover_server_socket);
|
|
|
|
|
|
|
|
if(discover_server_port == -1)
|
|
|
|
{
|
|
|
|
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
|
|
|
mg_printf(c, "[]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t payload[1];
|
|
|
|
payload[0] = discover_server_port;
|
|
|
|
|
|
|
|
if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0)
|
|
|
|
{
|
|
|
|
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
|
|
|
mg_printf(c, "[]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_storage their_addr;
|
|
|
|
socklen_t addr_size;
|
|
|
|
int client_fd, s_ret;
|
|
|
|
fd_set accept_fds;
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
uint8_t discover_answer_buf[1];
|
|
|
|
|
|
|
|
controller_t **known_controllers = controller_get_all();
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
addr_size = sizeof(their_addr);
|
|
|
|
|
|
|
|
FD_ZERO(&accept_fds);
|
|
|
|
FD_SET(discover_server_socket, &accept_fds); // NOLINT(hicpp-signed-bitwise)
|
|
|
|
|
|
|
|
timeout.tv_sec = DISCOVERY_TIMEOUT_MS / 1000;
|
|
|
|
timeout.tv_usec = (DISCOVERY_TIMEOUT_MS % 1000) * 1000;
|
|
|
|
|
|
|
|
s_ret = select(discover_server_socket + 1, &accept_fds, NULL, NULL, &timeout);
|
|
|
|
if(s_ret == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if((client_fd = accept(discover_server_socket, (struct sockaddr *) &their_addr, &addr_size)) < 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error accepting client %s\n", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t payload_length;
|
|
|
|
|
|
|
|
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error receiving header from client\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *answer_payload = (char*)malloc((payload_length));
|
|
|
|
ssize_t bytes_transferred;
|
|
|
|
|
|
|
|
if((bytes_transferred = recv(client_fd, answer_payload, payload_length, 0)) <= 0)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error receiving payload from client\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t client_addr_size = sizeof(struct sockaddr_in);
|
|
|
|
if(getpeername(client_fd, (struct sockaddr *)&addr, &client_addr_size) != 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
LOG_ERROR("error receiving payload from client\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid_t discovered_id;
|
|
|
|
|
|
|
|
mpack_tree_t tree;
|
|
|
|
mpack_tree_init_data(&tree, answer_payload, payload_length);
|
|
|
|
mpack_tree_parse(&tree);
|
|
|
|
mpack_node_t root = mpack_tree_root(&tree);
|
|
|
|
|
|
|
|
memcpy(discovered_id, mpack_node_data(mpack_node_map_uint(root, DISCOVERY_MAPPING_ID)), sizeof(uuid_t));
|
|
|
|
|
|
|
|
uint16_t discovered_command_port = mpack_node_u16(mpack_node_map_uint(root, DISCOVERY_MAPPING_COMMAND_PORT));
|
|
|
|
uint8_t discovered_relay_count = mpack_node_u8(mpack_node_map_uint(root, DISCOVERY_MAPPING_RELAY_COUNT));
|
|
|
|
const char *discovered_name = mpack_node_str(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
|
2020-05-05 23:16:43 +00:00
|
|
|
size_t discovered_name_len = mpack_node_strlen(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
|
|
|
|
|
|
|
|
if(discovered_name_len > MAX_NAME_LENGTH)
|
|
|
|
{
|
|
|
|
discovered_name_len = MAX_NAME_LENGTH;
|
|
|
|
}
|
2020-05-05 23:05:36 +00:00
|
|
|
|
|
|
|
bool found_discovered_in_list = 0;
|
|
|
|
|
|
|
|
for(int i = 0; known_controllers[i] != NULL; i++)
|
|
|
|
{
|
|
|
|
if(!found_discovered_in_list)
|
|
|
|
{
|
|
|
|
if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0)
|
|
|
|
{
|
|
|
|
known_controllers[i]->active = 1;
|
2020-05-05 23:16:43 +00:00
|
|
|
strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
|
2020-05-06 20:49:22 +00:00
|
|
|
known_controllers[i]->name[discovered_name_len] = '\0';
|
2020-05-05 23:05:36 +00:00
|
|
|
known_controllers[i]->port = discovered_command_port;
|
|
|
|
known_controllers[i]->relay_count = discovered_relay_count;
|
|
|
|
|
|
|
|
controller_save(known_controllers[i]);
|
|
|
|
controller_free(known_controllers[i]);
|
|
|
|
|
|
|
|
found_discovered_in_list = 1;
|
|
|
|
known_controllers[i] = known_controllers[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
known_controllers[i] = known_controllers[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found_discovered_in_list)
|
|
|
|
{
|
|
|
|
controller_t *discovered_controller = malloc(sizeof(controller_t));
|
|
|
|
discovered_controller->id = 0;
|
|
|
|
strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr));
|
|
|
|
memcpy(discovered_controller->uid, discovered_id, sizeof(uuid_t));
|
2020-05-06 20:49:22 +00:00
|
|
|
strncpy(discovered_controller->name, discovered_name, discovered_name_len);
|
|
|
|
discovered_controller->name[discovered_name_len] = '\0';
|
2020-05-05 23:05:36 +00:00
|
|
|
discovered_controller->relay_count = discovered_relay_count;
|
|
|
|
discovered_controller->port = discovered_command_port;
|
|
|
|
discovered_controller->active = 1;
|
|
|
|
|
2020-05-11 20:36:47 +00:00
|
|
|
controller_save(discovered_controller);
|
|
|
|
|
|
|
|
// TODO get relays during discovery and don't create empty ones
|
|
|
|
relay_t **discovered_relays = malloc(sizeof(relay_t*) * (discovered_controller->relay_count + 1));
|
|
|
|
for(int i = 0; i < discovered_controller->relay_count; ++i)
|
|
|
|
{
|
|
|
|
relay_t *new_relay = malloc(sizeof(relay_t));
|
|
|
|
new_relay->id = 0;
|
|
|
|
sprintf(new_relay->name, "Relay %d", i + 1);
|
|
|
|
new_relay->number = i;
|
|
|
|
new_relay->controller_id = discovered_controller->id;
|
|
|
|
|
|
|
|
uuid_t tmp_uuid;
|
|
|
|
memset(tmp_uuid, 0, sizeof(uuid_t));
|
|
|
|
memcpy(tmp_uuid, "off", 3);
|
|
|
|
|
|
|
|
for(int i = 0; i < 7; ++i)
|
|
|
|
{
|
|
|
|
new_relay->schedules[i] = schedule_get_by_uid(tmp_uuid);
|
|
|
|
}
|
|
|
|
time_t timestamp = time(NULL);
|
|
|
|
struct tm *time_struct = localtime(×tamp);
|
|
|
|
new_relay->active_schedule = new_relay->schedules[helper_get_weekday(time_struct)];
|
|
|
|
|
|
|
|
relay_save(new_relay);
|
|
|
|
|
|
|
|
discovered_relays[i] = new_relay;
|
|
|
|
}
|
|
|
|
discovered_relays[discovered_controller->relay_count] = NULL;
|
2020-05-10 20:37:55 +00:00
|
|
|
discovered_controller->relays = discovered_relays;
|
|
|
|
|
2020-05-06 23:38:13 +00:00
|
|
|
controller_free(discovered_controller);
|
2020-05-05 23:05:36 +00:00
|
|
|
}
|
|
|
|
mpack_tree_destroy(&tree);
|
|
|
|
free(answer_payload);
|
|
|
|
|
|
|
|
discover_answer_buf[0] = 0; // TODO add discovery return codes
|
|
|
|
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
|
|
|
|
close(client_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(int i = 0; known_controllers[i] != NULL; i++)
|
|
|
|
{
|
|
|
|
known_controllers[i]->active = false;
|
|
|
|
controller_save(known_controllers[i]);
|
|
|
|
LOG_DEBUG("lost: %s\n", known_controllers[i]->name);
|
|
|
|
}
|
|
|
|
controller_free_list(known_controllers);
|
|
|
|
|
|
|
|
api_v1_controllers_GET(c, args, hm);
|
|
|
|
}
|