controller-legacy/models/controller.c

73 lines
1.9 KiB
C
Raw Normal View History

2019-11-15 00:23:43 +00:00
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>
2019-12-08 22:39:10 +00:00
#include <models/controller.h>
2019-11-15 00:23:43 +00:00
#include <macros.h>
2020-04-16 23:38:25 +00:00
#include <config.h>
#include <constants.h>
2019-11-15 00:23:43 +00:00
2020-04-13 22:50:55 +00:00
controller_t*
2019-11-15 00:23:43 +00:00
controller_create(void)
{
2020-04-13 22:50:55 +00:00
controller_t *new_controller = malloc(sizeof(*new_controller));
uuid_generate(new_controller->id);
2019-11-15 00:23:43 +00:00
2020-04-16 23:38:25 +00:00
strncpy(new_controller->name, global_config.name, MAX_NAME_LENGTH);
new_controller->name[MAX_NAME_LENGTH] = '\0';
2020-04-13 22:50:55 +00:00
new_controller->command_port = 0;
2020-04-16 23:38:25 +00:00
new_controller->discovery_port = global_config.discovery_port;
new_controller->relay_count = global_config.relay_count;
2019-11-15 00:23:43 +00:00
2020-04-13 22:50:55 +00:00
new_controller->relays = malloc(sizeof(relay_t) * new_controller->relay_count);
2019-11-15 00:23:43 +00:00
uint8_t i;
2020-04-13 22:50:55 +00:00
for(i = 0; i < new_controller->relay_count; ++i)
2019-11-15 00:23:43 +00:00
{
2020-04-13 22:50:55 +00:00
new_controller->relays[i] = relay_create(i);
2019-11-15 00:23:43 +00:00
}
2020-04-13 22:50:55 +00:00
return new_controller;
2019-11-15 00:23:43 +00:00
}
2020-01-07 01:23:16 +00:00
void
2020-04-23 23:33:48 +00:00
controller_set_name(controller_t *controller, const char *name)
2020-01-07 01:23:16 +00:00
{
2020-04-13 22:50:55 +00:00
strncpy(controller->name, name, MAX_NAME_LENGTH);
controller->name[MAX_NAME_LENGTH] = '\0';
}
void
controller_free(controller_t *controller)
{
for(int i = 0; i < controller->relay_count; ++i)
{
relay_free(controller->relays[i]);
}
free(controller->relays);
free(controller);
}
void
controller_debug(controller_t *controller)
{
if(controller == NULL)
2020-02-09 23:58:17 +00:00
{
LOG_DEBUG("controller is NULL");
2020-04-13 22:50:55 +00:00
return;
2020-02-09 23:58:17 +00:00
}
2020-01-07 01:23:16 +00:00
char uuid_str[37];
2020-04-13 22:50:55 +00:00
uuid_unparse(controller->id, uuid_str);
LOG_DEBUG("(1/5) %s @ %p", uuid_str, (void*)controller);
2020-04-13 22:50:55 +00:00
LOG_DEBUG("(2/5) name: %s", controller->name);
LOG_DEBUG("(3/5) command_port: %5d discovery_port: %5d", controller->command_port, controller->discovery_port);
LOG_DEBUG("(4/5) relay count: %3d", controller->relay_count);
LOG_DEBUG("(5/5) relays @ %p:", (void*)controller->relays);
2020-04-13 22:50:55 +00:00
for(uint8_t i = 0; i < controller->relay_count; ++i)
{
relay_debug(controller->relays[i]);
}
2020-01-07 01:23:16 +00:00
}