add: config loading
This commit is contained in:
parent
3cd6668f9c
commit
25ba0ff723
22 changed files with 5799 additions and 75 deletions
helpers
69
helpers/load_config.c
Normal file
69
helpers/load_config.c
Normal file
|
@ -0,0 +1,69 @@
|
|||
#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;
|
||||
}
|
||||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue