controller-legacy/handlers/command.c

129 lines
3.6 KiB
C
Raw Normal View History

2020-04-13 22:50:55 +00:00
#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>
2020-04-23 23:33:48 +00:00
#include <mpack.h>
2020-04-13 22:50:55 +00:00
#include <models/schedule.h>
static void
2020-04-23 23:33:48 +00:00
handler_command_set_schedule(mpack_node_t map, controller_t *controller)
2020-04-13 22:50:55 +00:00
{
2020-04-23 23:33:48 +00:00
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
int uid_size = sizeof(uuid_t);
2020-04-13 22:50:55 +00:00
uuid_t schedule_id;
2020-04-23 23:33:48 +00:00
memcpy(schedule_id, mpack_node_data(mpack_node_map_uint(map, COMMAND_MAPPING_SCHEDULE_ID)), uid_size);
uint16_t periods_count = mpack_node_u16(mpack_node_map_uint(map, COMMAND_MAPPING_PERIODS_COUNT));
uint16_t *periods = (uint16_t*)mpack_node_bin_data(mpack_node_map_uint(map, COMMAND_MAPPING_PERIODS_BLOB));
2020-04-13 22:50:55 +00:00
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);
2020-04-13 23:58:06 +00:00
relay_debug(target_relay);
2020-04-13 22:50:55 +00:00
}
static void
2020-04-23 23:33:48 +00:00
handler_command_set_relay_name(mpack_node_t map, controller_t *controller)
2020-04-13 22:50:55 +00:00
{
2020-04-23 23:33:48 +00:00
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
const char *relay_name = mpack_node_str(mpack_node_map_uint(map, COMMAND_MAPPING_NAME));
2020-04-13 22:50:55 +00:00
if(relay_num < controller->relay_count)
{
relay_set_name(controller->relays[relay_num], relay_name);
}
2020-04-13 23:58:06 +00:00
relay_debug(controller->relays[relay_num]);
2020-04-13 22:50:55 +00:00
}
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;
}
uint32_t payload_length;
2020-04-13 22:50:55 +00:00
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;
}
2020-04-23 23:33:48 +00:00
mpack_tree_t tree;
mpack_tree_init_data(&tree, payload, payload_length);
mpack_tree_parse(&tree);
mpack_node_t root = mpack_tree_root(&tree);
uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
2020-04-13 22:50:55 +00:00
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:
2020-04-23 23:33:48 +00:00
controller_set_name(controller, mpack_node_str(mpack_node_map_uint(root, COMMAND_MAPPING_NAME)));
2020-04-13 22:50:55 +00:00
break;
case COMMAND_CODE_GET_NAME:
break;
case COMMAND_CODE_SET_SCHEDULE:
2020-04-23 23:33:48 +00:00
handler_command_set_schedule(root, controller);
2020-04-13 22:50:55 +00:00
break;
case COMMAND_CODE_GET_SCHEDULE:
break;
case COMMAND_CODE_SET_RELAY_NAME:
2020-04-23 23:33:48 +00:00
handler_command_set_relay_name(root, controller);
2020-04-13 22:50:55 +00:00
break;
case COMMAND_CODE_GET_RELAY_NAME:
break;
2020-04-14 00:05:34 +00:00
default:
LOG_ERROR("received invalid command");
2020-04-13 22:50:55 +00:00
}
2020-04-23 23:33:48 +00:00
if(mpack_tree_destroy(&tree) != mpack_ok)
{
LOG_WARN("error when destroying mpack tree");
}
2020-04-13 22:50:55 +00:00
free(payload);
close(client_fd);
}