controller-legacy/handlers/command.c

119 lines
3.3 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>
#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);
2020-04-13 23:58:06 +00:00
relay_debug(target_relay);
2020-04-13 22:50:55 +00:00
}
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);
}
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;
}
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;
2020-04-14 00:05:34 +00:00
default:
LOG_ERROR("received invalid command");
2020-04-13 22:50:55 +00:00
}
free(payload);
close(client_fd);
}