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;
|
|
|
|
}
|
2020-04-18 14:34:43 +00:00
|
|
|
if(CONFINI_IS_KEY("controller", "database"))
|
|
|
|
{
|
2020-04-23 23:33:48 +00:00
|
|
|
config->database = malloc(sizeof(char) * (strlen(disp->value) + 1));
|
|
|
|
strcpy(config->database, disp->value);
|
2020-04-18 14:34:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-04-24 13:08:26 +00:00
|
|
|
if(CONFINI_IS_KEY("controller", "log-level"))
|
|
|
|
{
|
|
|
|
if(strcasecmp(disp->value, "trace") == 0)
|
|
|
|
{
|
|
|
|
config->log_level = LOG_LEVEL_TRACE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(strcasecmp(disp->value, "debug") == 0)
|
|
|
|
{
|
|
|
|
config->log_level = LOG_LEVEL_DEBUG;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(strcasecmp(disp->value, "info") == 0)
|
|
|
|
{
|
|
|
|
config->log_level = LOG_LEVEL_INFO;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(strcasecmp(disp->value, "warn") == 0)
|
|
|
|
{
|
|
|
|
config->log_level = LOG_LEVEL_WARN;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(strcasecmp(disp->value, "error") == 0)
|
|
|
|
{
|
|
|
|
config->log_level = LOG_LEVEL_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(strcasecmp(disp->value, "fatal") == 0)
|
|
|
|
{
|
|
|
|
config->log_level = LOG_LEVEL_FATAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
LOG_WARN("invalid log-level '%s'\n", disp->value);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-16 23:38:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_TRACE("config relay-count set to %u\n", config->relay_count);
|
2020-04-16 23:38:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_WARN("invalid driver '%s' in section '%s'\n", disp->value, relay_section_name);
|
2020-04-16 23:38:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|