controller-legacy/helpers/load_config.c

75 lines
2.4 KiB
C
Raw Normal View History

2020-04-16 23:38:25 +00:00
#include <stdlib.h>
#include <string.h>
#include <helpers.h>
#include <config.h>
#include <logger.h>
#include <confini.h>
#define CONFINI_IS_KEY(SECTION, KEY) \
(ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
ini_string_match_ii(KEY, disp->data, disp->format))
int
helper_load_config(IniDispatch *disp, void *config_void)
{
config_t *config = (config_t*)config_void;
char relay_section_name[10]; // "relay-255\0" is longest name
if(disp->type == INI_KEY)
{
if(CONFINI_IS_KEY("controller", "name"))
{
strncpy(config->name, disp->value, MAX_NAME_LENGTH);
config->name[MAX_NAME_LENGTH] = '\0';
return 0;
}
if(CONFINI_IS_KEY("controller", "discovery-port"))
{
config->discovery_port = atoi(disp->value);
return 0;
}
if(CONFINI_IS_KEY("controller", "relay-count"))
{
config->relay_count = atoi(disp->value);
config->relay_configs = malloc(sizeof(config_relay_t) * config->relay_count);
for(uint8_t i = 0; i < config->relay_count; ++i)
{
config->relay_configs[i].driver= RELAY_DRIVER_NONE;
}
LOG_TRACE("config relay-count set to %u", config->relay_count);
return 0;
}
for(uint8_t i = 0; i < config->relay_count; ++i)
{
sprintf(relay_section_name, "relay-%u", i);
if(CONFINI_IS_KEY(relay_section_name, "pin"))
{
config->relay_configs[i].pin = atoi(disp->value);
return 0;
}
2020-04-18 00:09:50 +00:00
if(CONFINI_IS_KEY(relay_section_name, "inverted"))
{
config->relay_configs[i].inverted = atoi(disp->value);
return 0;
}
2020-04-16 23:38:25 +00:00
if(CONFINI_IS_KEY(relay_section_name, "driver"))
{
if(strcasecmp(disp->value, "gpio") == 0)
{
config->relay_configs[i].driver = RELAY_DRIVER_GPIO;
return 0;
}
if(strcasecmp(disp->value, "piface") == 0)
{
config->relay_configs[i].driver = RELAY_DRIVER_PIFACE;
return 0;
}
LOG_WARN("invalid driver '%s' in section '%s'", disp->value, relay_section_name);
return 0;
}
}
}
return 0;
}