Replace confini with toml
This commit is contained in:
parent
25eab5d38e
commit
79b1f3b2cf
27 changed files with 3071 additions and 5916 deletions
|
|
@ -12,11 +12,17 @@ target_link_libraries(controller -luuid)
|
|||
|
||||
option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool (OFF)" OFF)
|
||||
|
||||
set(CMAKE_C_FLAGS "$ENV{CFLAGS}")
|
||||
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -std=gnu99 -Wpedantic -Werror -Wall -Wextra -ffile-prefix-map=${CMAKE_SOURCE_DIR}/src/=")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D'__FILENAME__=\"$(subst $(realpath ${CMAKE_SOURCE_DIR}/src/)/,,$(abspath $<))\"'")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -std=gnu99 -Wpedantic -Werror -Wall -Wextra")
|
||||
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage")
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
|
||||
file(GLOB_RECURSE ALL_SOURCE_FILES src/*.c)
|
||||
|
||||
if(WIRING_PI_DEBUG)
|
||||
message("Showing wiringPi calls as debug")
|
||||
add_definitions("-DWIRING_PI_DEBUG")
|
||||
|
|
@ -32,7 +38,6 @@ aux_source_directory(vendor VENDOR_SRC)
|
|||
|
||||
add_dependencies(controller sql)
|
||||
|
||||
configure_file("controller.ini" "controller.ini" COPYONLY)
|
||||
configure_file("version.h.in" "version.h" @ONLY)
|
||||
|
||||
|
||||
|
|
@ -47,21 +52,43 @@ add_custom_target(sql
|
|||
)
|
||||
|
||||
add_custom_target(run
|
||||
COMMAND ./controller start
|
||||
COMMAND ${CMAKE_BINARY_DIR}/controller
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
)
|
||||
add_custom_target(debug
|
||||
COMMAND valgrind ./controller start
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
)
|
||||
add_custom_target(debug-full
|
||||
COMMAND valgrind --leak-check=full --show-leak-kinds=all ./controller start
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
)
|
||||
|
||||
add_custom_target(docs
|
||||
COMMAND doxygen
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
)
|
||||
|
||||
IF(CMAKE_BUILD_TYPE MATCHES Debug)
|
||||
message(STATUS "loading debug targets")
|
||||
add_custom_target(debug
|
||||
COMMAND gdb ${CMAKE_BINARY_DIR}/controller
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
)
|
||||
add_custom_target(valgrind
|
||||
COMMAND valgrind -s ${CMAKE_BINARY_DIR}/controller
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
)
|
||||
add_custom_target(valgrind-leak
|
||||
COMMAND valgrind --leak-check=full --show-leak-kinds=all ${CMAKE_BINARY_DIR}/controller
|
||||
DEPENDS controller
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
)
|
||||
|
||||
add_custom_target(
|
||||
clang-tidy
|
||||
COMMAND /usr/bin/clang-tidy
|
||||
${ALL_SOURCE_FILES}
|
||||
--header-filter=${CMAKE_SOURCE_DIR}/include/*
|
||||
--
|
||||
-std=gnu99
|
||||
-I${CMAKE_SOURCE_DIR}/include
|
||||
-I${CMAKE_SOURCE_DIR}/vendor
|
||||
-I${CMAKE_BINARY_DIR}
|
||||
)
|
||||
ENDIF(CMAKE_BUILD_TYPE MATCHES Debug)
|
||||
|
|
|
|||
|
|
@ -1,69 +0,0 @@
|
|||
[controller]
|
||||
name = new emgauwa device
|
||||
|
||||
: 4422 for testing; 4421 for dev-env; 4420 for testing-env; 4419 for prod-env
|
||||
discovery-port = 4421
|
||||
: 1886 for testing; 1885 for dev-env; 1884 for testing-env; 1883 for prod-env
|
||||
mqtt-port = 1885
|
||||
mqtt-host = localhost
|
||||
|
||||
relay-count = 10
|
||||
relays-init = 1
|
||||
|
||||
database = controller.sqlite
|
||||
log-level = debug
|
||||
log-file = stdout
|
||||
|
||||
[relay-0]
|
||||
driver = piface
|
||||
pin = 0
|
||||
inverted = 0
|
||||
init = 0
|
||||
|
||||
[relay-1]
|
||||
driver = piface
|
||||
pin = 1
|
||||
inverted = 0
|
||||
init = 1
|
||||
|
||||
[relay-2]
|
||||
driver = gpio
|
||||
pin = 5
|
||||
inverted = 1
|
||||
|
||||
[relay-3]
|
||||
driver = gpio
|
||||
pin = 4
|
||||
inverted = 1
|
||||
|
||||
[relay-4]
|
||||
driver = gpio
|
||||
pin = 3
|
||||
inverted = 1
|
||||
|
||||
[relay-5]
|
||||
driver = gpio
|
||||
pin = 2
|
||||
inverted = 1
|
||||
|
||||
[relay-6]
|
||||
driver = gpio
|
||||
pin = 1
|
||||
inverted = 1
|
||||
pulse-duration = 3
|
||||
|
||||
[relay-7]
|
||||
driver = gpio
|
||||
pin = 0
|
||||
inverted = 1
|
||||
pulse-duration = 3
|
||||
|
||||
[relay-8]
|
||||
driver = gpio
|
||||
pin = 16
|
||||
inverted = 1
|
||||
|
||||
[relay-9]
|
||||
driver = gpio
|
||||
pin = 15
|
||||
inverted = 1
|
||||
66
emgauwa-controller.conf
Normal file
66
emgauwa-controller.conf
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
[controller]
|
||||
database = "emgauwa-controller.sqlite"
|
||||
|
||||
mqtt-host = "127.0.0.1"
|
||||
|
||||
[ports]
|
||||
# 4422 for testing; 4421 for dev-env; 4420 for testing-env; 4419 for prod-env
|
||||
discovery = 4421
|
||||
# 1886 for testing; 1885 for dev-env; 1884 for testing-env; 1883 for prod-env
|
||||
mqtt = 1885
|
||||
|
||||
[logging]
|
||||
level = "debug"
|
||||
file = "stdout"
|
||||
|
||||
[[relays]]
|
||||
driver = "piface"
|
||||
pin = 0
|
||||
inverted = 0
|
||||
|
||||
[[relays]]
|
||||
driver = "piface"
|
||||
pin = 1
|
||||
inverted = 0
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 5
|
||||
inverted = 1
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 4
|
||||
inverted = 1
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 3
|
||||
inverted = 1
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 2
|
||||
inverted = 1
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 1
|
||||
inverted = 1
|
||||
pulse-duration = 3
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 0
|
||||
inverted = 1
|
||||
pulse-duration = 3
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 16
|
||||
inverted = 1
|
||||
|
||||
[[relays]]
|
||||
driver = "gpio"
|
||||
pin = 15
|
||||
inverted = 1
|
||||
BIN
emgauwa-controller.sqlite
Normal file
BIN
emgauwa-controller.sqlite
Normal file
Binary file not shown.
13
include/cli.h
Normal file
13
include/cli.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef CONTROLLER_CLI_H
|
||||
#define CONTROLLER_CLI_H
|
||||
|
||||
typedef struct cli
|
||||
{
|
||||
const char *config_file;
|
||||
int demo_mode;
|
||||
} cli_t;
|
||||
|
||||
void
|
||||
cli_parse(int argc, const char **argv, cli_t *cli);
|
||||
|
||||
#endif /* CONTROLLER_CLI_H */
|
||||
|
|
@ -2,7 +2,8 @@
|
|||
#define CONTROLLER_CONFIG_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <confini.h>
|
||||
|
||||
#include <toml.h>
|
||||
|
||||
#include <constants.h>
|
||||
#include <enums.h>
|
||||
|
|
@ -16,27 +17,50 @@ typedef struct
|
|||
uint8_t pulse_duration;
|
||||
} config_relay_t;
|
||||
|
||||
int
|
||||
config_load(IniDispatch *disp, void *config_void);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *file;
|
||||
char database[256];
|
||||
char user[256];
|
||||
char group[256];
|
||||
int log_level;
|
||||
FILE *log_file;
|
||||
run_type_t run_type;
|
||||
char name[MAX_NAME_LENGTH + 1];
|
||||
uint16_t discovery_port;
|
||||
uint16_t mqtt_port;
|
||||
char mqtt_host[256];
|
||||
uint8_t relay_count;
|
||||
char *name;
|
||||
char *database;
|
||||
char *user;
|
||||
char *group;
|
||||
char *mqtt_host;
|
||||
char *include;
|
||||
int relays_init;
|
||||
uint8_t relay_count;
|
||||
|
||||
struct
|
||||
{
|
||||
int level;
|
||||
FILE *file;
|
||||
} logging;
|
||||
|
||||
struct
|
||||
{
|
||||
uint16_t discovery;
|
||||
uint16_t mqtt;
|
||||
} ports;
|
||||
|
||||
config_relay_t *relay_configs;
|
||||
} config_t;
|
||||
|
||||
extern config_t global_config;
|
||||
extern config_t *global_config;
|
||||
|
||||
void
|
||||
config_init();
|
||||
|
||||
void
|
||||
config_free();
|
||||
|
||||
void
|
||||
config_load_string(char **holder, const char *value);
|
||||
|
||||
void
|
||||
config_load(config_t *config, const char *cli_config_file);
|
||||
|
||||
void
|
||||
config_load_file(config_t *config, const char *file_name);
|
||||
|
||||
void
|
||||
config_load_directory(config_t *config, const char *directory_name);
|
||||
|
||||
#endif //CONTROLLER_CONFIG_H
|
||||
|
|
|
|||
|
|
@ -14,13 +14,6 @@
|
|||
*/
|
||||
#define MAX_NAME_LENGTH 128
|
||||
|
||||
/**
|
||||
* @brief Maximum number of dbs for the databases for the MDB_env
|
||||
*
|
||||
* Used when calling mdb_env_set_maxdbs() in database_setup()
|
||||
*/
|
||||
#define MDB_MAXDBS 8
|
||||
|
||||
/**
|
||||
* @brief How many milli seconds to wait until poll timeout in main loop
|
||||
*/
|
||||
|
|
@ -28,4 +21,12 @@
|
|||
|
||||
#define PIFACE_GPIO_BASE 200
|
||||
|
||||
#define DEFAULT_CONTROLLER_NAME "emgauwa-controller"
|
||||
#define DEFAULT_CONFIG_PATH "emgauwa-controller.conf"
|
||||
#define DEFAULT_GLOBAL_CONFIG_PATH "/etc/emgauwa/controller.conf"
|
||||
#define DEFAULT_DATABASE_PATH "emgauwa-controller.sqlite"
|
||||
#define DEFAULT_DISCOVERY_PORT 4421
|
||||
#define DEFAULT_MQTT_PORT 1885
|
||||
#define DEFAULT_MQTT_HOST "127.0.0.1"
|
||||
|
||||
#endif /* CONTROLLER_CONTANTS_H */
|
||||
|
|
|
|||
|
|
@ -22,10 +22,4 @@ typedef enum
|
|||
RELAY_DRIVER_PIFACE,
|
||||
} relay_driver_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RUN_TYPE_START,
|
||||
RUN_TYPE_TEST,
|
||||
} run_type_t;
|
||||
|
||||
#endif /* CONTROLLER_ENUMS_H */
|
||||
|
|
|
|||
|
|
@ -25,9 +25,6 @@ helper_get_port(int sock);
|
|||
int
|
||||
helper_open_discovery_socket(uint16_t discovery_port);
|
||||
|
||||
void
|
||||
helper_parse_cli(int argc, const char **argv, config_t *config);
|
||||
|
||||
int
|
||||
helper_get_weekday(const struct tm *time_struct);
|
||||
|
||||
|
|
|
|||
49
src/cli.c
Normal file
49
src/cli.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <argparse.h>
|
||||
|
||||
#include <cli.h>
|
||||
#include <config.h>
|
||||
#include <logger.h>
|
||||
#include <helpers.h>
|
||||
#include <version.h>
|
||||
|
||||
static const char *const usage[] = {
|
||||
"controller [options]",
|
||||
NULL,
|
||||
};
|
||||
|
||||
void
|
||||
cli_parse(int argc, const char **argv, cli_t *cli)
|
||||
{
|
||||
cli->config_file = NULL;
|
||||
cli->demo_mode = 0;
|
||||
|
||||
int version = 0;
|
||||
struct argparse_option options[] =
|
||||
{
|
||||
OPT_HELP(),
|
||||
OPT_GROUP("Basic options"),
|
||||
OPT_STRING('c', "config", &cli->config_file, "path to config file", NULL, 0, OPT_NONEG),
|
||||
OPT_BOOLEAN('d', "demo", &cli->demo_mode, "demo mode", NULL, 0, OPT_NONEG),
|
||||
OPT_BOOLEAN('v', "version", &version, "print version", NULL, 0, OPT_NONEG),
|
||||
OPT_END(),
|
||||
};
|
||||
|
||||
struct argparse argparse;
|
||||
argparse_init(&argparse, options, usage, 0);
|
||||
argparse_describe(
|
||||
&argparse,
|
||||
"\nA brief description of what the program does and how it works.",
|
||||
"\nAdditional description of the program after the description of the arguments."
|
||||
);
|
||||
argparse_parse(&argparse, argc, argv);
|
||||
|
||||
if(version)
|
||||
{
|
||||
printf("%s\n", EMGAUWA_CONTROLLER_VERSION);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
505
src/config.c
505
src/config.c
|
|
@ -1,191 +1,442 @@
|
|||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <helpers.h>
|
||||
#include <config.h>
|
||||
#include <constants.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))
|
||||
config_t *global_config;
|
||||
|
||||
static int
|
||||
config_load_log_level(IniDispatch *disp, config_t *config)
|
||||
config_load_log_level(config_t *config, char *value)
|
||||
{
|
||||
if(strcasecmp(disp->value, "debug") == 0)
|
||||
if(strcmp(value, "debug") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_DEBUG));
|
||||
config->log_level = LOG_DEBUG;
|
||||
config->logging.level = LOG_DEBUG;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "info") == 0)
|
||||
if(strcmp(value, "info") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_INFO));
|
||||
config->log_level = LOG_INFO;
|
||||
config->logging.level = LOG_INFO;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "notice") == 0)
|
||||
if(strcmp(value, "notice") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_NOTICE));
|
||||
config->log_level = LOG_NOTICE;
|
||||
config->logging.level = LOG_NOTICE;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "warning") == 0)
|
||||
if(strcmp(value, "warning") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_WARNING));
|
||||
config->log_level = LOG_WARNING;
|
||||
config->logging.level = LOG_WARNING;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "err") == 0)
|
||||
if(strcmp(value, "err") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_ERR));
|
||||
config->log_level = LOG_ERR;
|
||||
config->logging.level = LOG_ERR;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "crit") == 0)
|
||||
if(strcmp(value, "crit") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_CRIT));
|
||||
config->log_level = LOG_CRIT;
|
||||
config->logging.level = LOG_CRIT;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "emerg") == 0)
|
||||
if(strcmp(value, "emerg") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_EMERG));
|
||||
config->log_level = LOG_EMERG;
|
||||
config->logging.level = LOG_EMERG;
|
||||
return 0;
|
||||
}
|
||||
LOGGER_WARNING("invalid log-level '%s'\n", disp->value);
|
||||
LOGGER_WARNING("invalid log-level '%s'\n", value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
config_load_log_file(IniDispatch *disp, config_t *config)
|
||||
config_load_log_file(config_t *config, char *value)
|
||||
{
|
||||
if(strcasecmp(disp->value, "stdout") == 0)
|
||||
if(strcmp(value, "stdout") == 0)
|
||||
{
|
||||
config->log_file = stdout;
|
||||
config->logging.file = stdout;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "stderr") == 0)
|
||||
if(strcmp(value, "stderr") == 0)
|
||||
{
|
||||
config->log_file = stderr;
|
||||
config->logging.file = stderr;
|
||||
return 0;
|
||||
}
|
||||
config->log_file = fopen(disp->value, "a+");
|
||||
config->logging.file = fopen(value, "a+");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
config_load(IniDispatch *disp, void *config_void)
|
||||
static void
|
||||
config_init_relay_configs(config_t *config)
|
||||
{
|
||||
config_t *config = (config_t*)config_void;
|
||||
char relay_section_name[10]; // "relay-255\0" is longest name
|
||||
|
||||
if(disp->type == INI_KEY)
|
||||
config->relay_configs = malloc(sizeof(config_relay_t) * config->relay_count);
|
||||
for(uint8_t i = 0; i < config->relay_count; ++i)
|
||||
{
|
||||
if(CONFINI_IS_KEY("controller", "name"))
|
||||
config->relay_configs[i].driver = RELAY_DRIVER_NONE;
|
||||
config->relay_configs[i].inverted = 0;
|
||||
config->relay_configs[i].init = -1;
|
||||
config->relay_configs[i].pin = 0;
|
||||
config->relay_configs[i].pulse_duration = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
config_load_section_controller(config_t *config, toml_table_t* controller)
|
||||
{
|
||||
toml_datum_t config_entry;
|
||||
|
||||
config_entry = toml_string_in(controller, "name");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_string(&config->name, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
|
||||
config_entry = toml_string_in(controller, "database");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_string(&config->database, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
|
||||
config_entry = toml_string_in(controller, "user");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_string(&config->user, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
|
||||
config_entry = toml_string_in(controller, "group");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_string(&config->group, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
|
||||
config_entry = toml_string_in(controller, "mqtt-host");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_string(&config->mqtt_host, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
|
||||
config_entry = toml_int_in(controller, "relays-init");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config->relays_init = config_entry.u.i;
|
||||
}
|
||||
|
||||
|
||||
config_entry = toml_string_in(controller, "include");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_string(&config->include, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
config_load_section_logging(config_t *config, toml_table_t* logging)
|
||||
{
|
||||
toml_datum_t config_entry;
|
||||
|
||||
config_entry = toml_string_in(logging, "level");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_log_level(config, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
|
||||
config_entry = toml_string_in(logging, "file");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config_load_log_file(config, config_entry.u.s);
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
config_load_section_ports(config_t *config, toml_table_t* ports)
|
||||
{
|
||||
toml_datum_t config_entry;
|
||||
|
||||
config_entry = toml_int_in(ports, "discovery");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config->ports.discovery = config_entry.u.i;
|
||||
}
|
||||
|
||||
config_entry = toml_int_in(ports, "mqtt");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config->ports.mqtt = config_entry.u.i;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
config_load_section_relay(config_t *config, toml_table_t* relay, int relay_num)
|
||||
{
|
||||
toml_datum_t config_entry;
|
||||
|
||||
config_entry = toml_int_in(relay, "pin");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config->relay_configs[relay_num].pin = config_entry.u.i;
|
||||
}
|
||||
|
||||
config_entry = toml_int_in(relay, "inverted");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config->relay_configs[relay_num].inverted = config_entry.u.i;
|
||||
}
|
||||
|
||||
config_entry = toml_int_in(relay, "init");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config->relay_configs[relay_num].init = config_entry.u.i;
|
||||
}
|
||||
|
||||
config_entry = toml_int_in(relay, "pulse-duration");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
config->relay_configs[relay_num].pulse_duration = config_entry.u.i;
|
||||
}
|
||||
|
||||
config_entry = toml_string_in(relay, "driver");
|
||||
if(config_entry.ok)
|
||||
{
|
||||
for(int i = 0; config_entry.u.s[i] != '\0'; ++i)
|
||||
{
|
||||
strncpy(config->name, disp->value, MAX_NAME_LENGTH);
|
||||
config->name[MAX_NAME_LENGTH] = '\0';
|
||||
return 0;
|
||||
config_entry.u.s[i] = tolower(config_entry.u.s[i]);
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "database"))
|
||||
|
||||
if(strcmp(config_entry.u.s, "gpio") == 0)
|
||||
{
|
||||
strcpy(config->database, disp->value);
|
||||
return 0;
|
||||
config->relay_configs[relay_num].driver = RELAY_DRIVER_GPIO;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "user"))
|
||||
else if(strcmp(config_entry.u.s, "piface") == 0)
|
||||
{
|
||||
strcpy(config->user, disp->value);
|
||||
return 0;
|
||||
config->relay_configs[relay_num].driver = RELAY_DRIVER_PIFACE;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "group"))
|
||||
else
|
||||
{
|
||||
strcpy(config->group, disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "log-level"))
|
||||
{
|
||||
return config_load_log_level(disp, config);
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "log-file"))
|
||||
{
|
||||
return config_load_log_file(disp, config);
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "discovery-port"))
|
||||
{
|
||||
config->discovery_port = atoi(disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "mqtt-port"))
|
||||
{
|
||||
config->mqtt_port = atoi(disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "mqtt-host"))
|
||||
{
|
||||
strcpy(config->mqtt_host, disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "relays-init"))
|
||||
{
|
||||
config->relays_init = 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;
|
||||
config->relay_configs[i].inverted = 0;
|
||||
config->relay_configs[i].init = -1;
|
||||
config->relay_configs[i].pin = 0;
|
||||
config->relay_configs[i].pulse_duration = 0;
|
||||
}
|
||||
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, "inverted"))
|
||||
{
|
||||
config->relay_configs[i].inverted = atoi(disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY(relay_section_name, "init"))
|
||||
{
|
||||
config->relay_configs[i].init = atoi(disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY(relay_section_name, "pulse-duration"))
|
||||
{
|
||||
config->relay_configs[i].pulse_duration = 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;
|
||||
}
|
||||
LOGGER_WARNING("invalid driver '%s' in section '%s'\n", disp->value, relay_section_name);
|
||||
return 0;
|
||||
}
|
||||
LOGGER_WARNING("invalid driver '%s' in section for relay %d\n", config_entry.u.s, relay_num);
|
||||
}
|
||||
free(config_entry.u.s);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
config_init()
|
||||
{
|
||||
global_config = calloc(1, sizeof(config_t));
|
||||
|
||||
config_load_string(&global_config->name, DEFAULT_CONTROLLER_NAME);
|
||||
|
||||
config_load_string(&global_config->database, DEFAULT_DATABASE_PATH);
|
||||
|
||||
config_load_string(&global_config->mqtt_host, DEFAULT_MQTT_HOST);
|
||||
|
||||
|
||||
global_config->user = NULL;
|
||||
global_config->group = NULL;
|
||||
|
||||
global_config->include = NULL;
|
||||
|
||||
global_config->relays_init = 0;
|
||||
global_config->relay_count = 0;
|
||||
|
||||
config_load_string(&global_config->mqtt_host, "127.0.0.1");
|
||||
|
||||
global_config->ports.mqtt = DEFAULT_MQTT_PORT;
|
||||
global_config->ports.discovery = DEFAULT_DISCOVERY_PORT;
|
||||
|
||||
global_config->logging.level = LOG_DEBUG;
|
||||
global_config->logging.file = stdout;
|
||||
}
|
||||
|
||||
void
|
||||
config_free()
|
||||
{
|
||||
free(global_config->name);
|
||||
free(global_config->database);
|
||||
free(global_config->user);
|
||||
free(global_config->group);
|
||||
free(global_config->mqtt_host);
|
||||
free(global_config->include);
|
||||
|
||||
free(global_config);
|
||||
}
|
||||
|
||||
void
|
||||
config_load_string(char **holder, const char *value)
|
||||
{
|
||||
if(*holder)
|
||||
{
|
||||
free(*holder);
|
||||
}
|
||||
size_t value_len = strlen(value);
|
||||
|
||||
char *new_holder = malloc(sizeof(char) * (value_len + 1));
|
||||
strcpy(new_holder, value);
|
||||
new_holder[value_len] = '\0';
|
||||
|
||||
*holder = new_holder;
|
||||
}
|
||||
|
||||
static int
|
||||
config_try_file(const char *path)
|
||||
{
|
||||
if(access(path, F_OK) != 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if(access(path, R_OK) != 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
config_load(config_t *config, const char *cli_config_file)
|
||||
{
|
||||
if(cli_config_file)
|
||||
{
|
||||
if(config_try_file(cli_config_file) == 0)
|
||||
{
|
||||
config_load_file(config, cli_config_file);
|
||||
return;
|
||||
}
|
||||
LOGGER_CRIT("unable to open the passed config file '%s'\n", cli_config_file);
|
||||
exit(1);
|
||||
}
|
||||
if(config_try_file(DEFAULT_CONFIG_PATH) == 0)
|
||||
{
|
||||
config_load_file(config, DEFAULT_CONFIG_PATH);
|
||||
return;
|
||||
}
|
||||
if(config_try_file(DEFAULT_GLOBAL_CONFIG_PATH) == 0)
|
||||
{
|
||||
config_load_file(config, DEFAULT_GLOBAL_CONFIG_PATH);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
config_load_file(config_t *config, const char *file_name)
|
||||
{
|
||||
FILE *fp;
|
||||
toml_table_t* config_toml;
|
||||
char errbuf[256];
|
||||
|
||||
/* Open the file and parse content */
|
||||
fp = fopen(file_name, "r");
|
||||
if(fp == NULL) {
|
||||
LOGGER_CRIT("unable to open config file '%s'\n", file_name);
|
||||
exit(1);
|
||||
}
|
||||
config_toml = toml_parse_file(fp, errbuf, sizeof(errbuf));
|
||||
fclose(fp);
|
||||
if(config_toml == NULL) {
|
||||
LOGGER_CRIT("unable to parse config file '%s': %s\n", file_name, errbuf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
toml_table_t* controller = toml_table_in(config_toml, "controller");
|
||||
if(controller)
|
||||
{
|
||||
config_load_section_controller(config, controller);
|
||||
}
|
||||
|
||||
toml_table_t* logging = toml_table_in(config_toml, "logging");
|
||||
if(logging)
|
||||
{
|
||||
config_load_section_logging(config, logging);
|
||||
}
|
||||
|
||||
toml_table_t* ports = toml_table_in(config_toml, "ports");
|
||||
if(ports)
|
||||
{
|
||||
config_load_section_ports(config, ports);
|
||||
}
|
||||
|
||||
toml_array_t* relays = toml_array_in(config_toml, "relays");
|
||||
if(relays)
|
||||
{
|
||||
config->relay_count = toml_array_nelem(relays);
|
||||
config_init_relay_configs(config);
|
||||
|
||||
for(int i = 0; i < config->relay_count; ++i)
|
||||
{
|
||||
config_load_section_relay(config, toml_table_at(relays, i), i);
|
||||
}
|
||||
}
|
||||
|
||||
toml_free(config_toml);
|
||||
|
||||
LOGGER_DEBUG("Loaded config from %s\n", file_name);
|
||||
}
|
||||
|
||||
void
|
||||
config_load_directory(config_t *config, const char *directory_name)
|
||||
{
|
||||
struct dirent *directory_entry;
|
||||
DIR *directory;
|
||||
|
||||
(void)config;
|
||||
|
||||
directory = opendir(directory_name);
|
||||
if(directory == NULL)
|
||||
{
|
||||
LOGGER_CRIT("cannot open directory '%s': %s\n", directory_name, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while((directory_entry = readdir(directory)) != NULL)
|
||||
{
|
||||
struct stat sb;
|
||||
const char *entry_name = directory_entry->d_name;
|
||||
|
||||
size_t copied = 0;
|
||||
|
||||
// Add 2 for '/' and '\0'.
|
||||
size_t path_len = strlen(directory_name) + strlen(entry_name) + 1;
|
||||
char *path = malloc(sizeof(char) * (path_len + 1));
|
||||
path[0] = '\0';
|
||||
|
||||
strncat(path + copied, directory_name, path_len - copied);
|
||||
copied = strlen(path);
|
||||
if(path[copied - 1] != '/')
|
||||
{
|
||||
strncat(path + copied, "/", path_len - copied);
|
||||
copied = strlen(path);
|
||||
}
|
||||
strncat(path + copied, entry_name, path_len - copied);
|
||||
|
||||
if(stat(path, &sb))
|
||||
{
|
||||
LOGGER_WARNING("failed to get info for '%s': %s\n", path, strerror(errno));
|
||||
}
|
||||
if(S_ISREG(sb.st_mode))
|
||||
{
|
||||
config_load_file(config, path);
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
|
||||
closedir(directory);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ struct mg_connection*
|
|||
connection_discovery_bind(struct mg_mgr *mgr)
|
||||
{
|
||||
char address[32];
|
||||
sprintf(address, "udp://0.0.0.0:%u", global_config.discovery_port);
|
||||
sprintf(address, "udp://0.0.0.0:%u", global_config->ports.discovery);
|
||||
struct mg_connection *c = mg_bind(mgr, address, handler_discovery);
|
||||
return c;
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ struct mg_connection*
|
|||
connection_mqtt_connect(struct mg_mgr *mgr)
|
||||
{
|
||||
char address[512];
|
||||
sprintf(address, "tcp://%s:%u", global_config.mqtt_host, global_config.mqtt_port);
|
||||
sprintf(address, "tcp://%s:%u", global_config->mqtt_host, global_config->ports.mqtt);
|
||||
struct mg_connection *c = mg_connect(mgr, address, handler_mqtt);
|
||||
return c;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ static int in_transaction;
|
|||
void
|
||||
database_init()
|
||||
{
|
||||
int rc = sqlite3_open(global_config.database, &global_database);
|
||||
int rc = sqlite3_open(global_config->database, &global_database);
|
||||
|
||||
if(rc)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -53,9 +53,9 @@ handler_command_relay_pulse(mpack_node_t map)
|
|||
{
|
||||
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
|
||||
|
||||
if(relay_num > global_config.relay_count)
|
||||
if(relay_num > global_config->relay_count)
|
||||
{
|
||||
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config.relay_count);
|
||||
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config->relay_count);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ handler_command_relay_pulse(mpack_node_t map)
|
|||
int duration = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_PULSE_DURATION));
|
||||
if(duration == 0)
|
||||
{
|
||||
duration = global_config.relay_configs[relay_num].pulse_duration;
|
||||
duration = global_config->relay_configs[relay_num].pulse_duration;
|
||||
}
|
||||
target_relay->pulse_timer = duration;
|
||||
LOGGER_DEBUG("pulsing relay %d for %ds\n", relay_num, duration);
|
||||
|
|
@ -87,9 +87,9 @@ handler_command_relay_name_set(mpack_node_t map)
|
|||
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
|
||||
const char *relay_name = mpack_node_str(mpack_node_map_uint(map, COMMAND_MAPPING_NAME));
|
||||
|
||||
if(relay_num > global_config.relay_count)
|
||||
if(relay_num > global_config->relay_count)
|
||||
{
|
||||
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config.relay_count);
|
||||
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config->relay_count);
|
||||
return;
|
||||
}
|
||||
relay_set_name(global_controller->relays[relay_num], relay_name);
|
||||
|
|
@ -120,7 +120,7 @@ handler_command_schedule_update(mpack_node_t map)
|
|||
|
||||
int *relay_ids = junction_relay_schedule_get_relay_ids_with_schedule(new_schedule->id);
|
||||
|
||||
for(int i = 0; i < global_config.relay_count; ++i)
|
||||
for(int i = 0; i < global_config->relay_count; ++i)
|
||||
{
|
||||
for(int j = 0; relay_ids[j] != 0; ++j)
|
||||
{
|
||||
|
|
@ -140,9 +140,9 @@ handler_command_relay_schedules_set(mpack_node_t map)
|
|||
{
|
||||
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
|
||||
|
||||
if(relay_num > global_config.relay_count)
|
||||
if(relay_num > global_config->relay_count)
|
||||
{
|
||||
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config.relay_count);
|
||||
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config->relay_count);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ handler_discovery(struct mg_connection *c, int ev, void *ev_data)
|
|||
mpack_write_uint(&writer, DISCOVERY_MAPPING_COMMAND_PORT);
|
||||
mpack_write_u16(&writer, global_controller->command_port);
|
||||
mpack_write_uint(&writer, DISCOVERY_MAPPING_RELAY_COUNT);
|
||||
mpack_write_u8(&writer, global_config.relay_count);
|
||||
mpack_write_u8(&writer, global_config->relay_count);
|
||||
mpack_write_uint(&writer, DISCOVERY_MAPPING_NAME);
|
||||
mpack_write_cstr(&writer, global_controller->name);
|
||||
mpack_finish_map(&writer);
|
||||
|
|
|
|||
|
|
@ -28,13 +28,13 @@ handler_loop(struct mg_connection *c_mqtt)
|
|||
localtime_r(×tamp, &time_last);
|
||||
timestamp = time(NULL);
|
||||
localtime_r(×tamp, &time_now);
|
||||
for(uint_fast8_t i = 0; i < global_config.relay_count; ++i)
|
||||
for(uint_fast8_t i = 0; i < global_config->relay_count; ++i)
|
||||
{
|
||||
relay_t *relay = global_controller->relays[i];
|
||||
int is_on = 0;
|
||||
|
||||
int is_on_schedule = relay_is_on_schedule(relay, &time_now);
|
||||
int pulse_relay = global_config.relay_configs[i].pulse_duration;
|
||||
int pulse_relay = global_config->relay_configs[i].pulse_duration;
|
||||
|
||||
if(is_on_schedule)
|
||||
{
|
||||
|
|
@ -73,17 +73,17 @@ handler_loop(struct mg_connection *c_mqtt)
|
|||
}
|
||||
relay->is_on = is_on;
|
||||
|
||||
if(global_config.relay_configs[i].inverted)
|
||||
if(global_config->relay_configs[i].inverted)
|
||||
{
|
||||
is_on = !is_on;
|
||||
}
|
||||
switch(global_config.relay_configs[i].driver)
|
||||
switch(global_config->relay_configs[i].driver)
|
||||
{
|
||||
case RELAY_DRIVER_GPIO:
|
||||
driver_gpio_set(global_config.relay_configs[i].pin, is_on);
|
||||
driver_gpio_set(global_config->relay_configs[i].pin, is_on);
|
||||
break;
|
||||
case RELAY_DRIVER_PIFACE:
|
||||
driver_piface_set(global_config.relay_configs[i].pin, is_on);
|
||||
driver_piface_set(global_config->relay_configs[i].pin, is_on);
|
||||
break;
|
||||
default:
|
||||
LOGGER_WARNING("relay %d is not using a driver\n", i);
|
||||
|
|
|
|||
|
|
@ -59,8 +59,8 @@ get_gid_for_group(char *group)
|
|||
int
|
||||
helper_drop_privileges()
|
||||
{
|
||||
uid_t uid = get_uid_for_user(global_config.user);
|
||||
gid_t gid = get_gid_for_group(global_config.group);
|
||||
uid_t uid = get_uid_for_user(global_config->user);
|
||||
gid_t gid = get_gid_for_group(global_config->group);
|
||||
|
||||
LOGGER_DEBUG("drop privileges to %lu:%lu\n", uid, gid);
|
||||
|
||||
|
|
|
|||
29
src/logger.c
29
src/logger.c
|
|
@ -17,10 +17,11 @@ const char *COLOR_EMERG = COLOR_MAGENTA;
|
|||
void
|
||||
logger_log(int level, const char *filename, int line, const char *func, const char *msg, ...)
|
||||
{
|
||||
if(global_config.log_level < level)
|
||||
if(global_config->logging.level < level)
|
||||
{
|
||||
return;
|
||||
}
|
||||
va_list args;
|
||||
const char *level_str;
|
||||
const char *color;
|
||||
|
||||
|
|
@ -62,24 +63,28 @@ logger_log(int level, const char *filename, int line, const char *func, const ch
|
|||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
|
||||
size_t timestamp_len = strlen(timestamp_str);
|
||||
|
||||
char *buffer = malloc(sizeof(char) * (128 + strlen(msg)));
|
||||
sprintf(buffer, "%s[%5s] %s:%d:%s " COLOR_NONE "%s", color, level_str, filename, line, func, msg);
|
||||
char *buffer = malloc(sizeof(char) * (128 + strlen(msg) + timestamp_len));
|
||||
sprintf(buffer, "%s %s[%5s] %s:%d:%s " COLOR_NONE "%s", timestamp_str, color, level_str, filename, line, func, msg);
|
||||
|
||||
//fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func);
|
||||
|
||||
va_list args;
|
||||
// start arg va_list and find log_len
|
||||
va_start(args, msg);
|
||||
vsyslog(level, buffer, args);
|
||||
size_t log_len = vsnprintf(NULL, 0, buffer, args); // NOLINT(clang-analyzer-valist.Uninitialized): clang-tidy bug
|
||||
va_end(args);
|
||||
|
||||
char *buffer_timed = malloc(sizeof(char) * (strlen(timestamp_str) + strlen(buffer) + 2));
|
||||
sprintf(buffer_timed, "%s %s", timestamp_str, buffer);
|
||||
char *log_line = malloc(sizeof(char) * (log_len + 1));
|
||||
|
||||
// start arg va_list again and write log_line
|
||||
va_start(args, msg);
|
||||
vfprintf(global_config.log_file, buffer_timed, args);
|
||||
fflush(global_config.log_file);
|
||||
vsprintf(log_line, buffer, args); // NOLINT(clang-analyzer-valist.Uninitialized): clang-tidy bug
|
||||
va_end(args);
|
||||
|
||||
syslog(level, "%s", log_line + timestamp_len + 1);
|
||||
|
||||
fprintf(global_config->logging.file, "%s", log_line);
|
||||
fflush(global_config->logging.file);
|
||||
|
||||
free(buffer);
|
||||
free(buffer_timed);
|
||||
free(log_line);
|
||||
}
|
||||
|
|
|
|||
61
src/main.c
61
src/main.c
|
|
@ -6,6 +6,7 @@
|
|||
#include <signal.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <cli.h>
|
||||
#include <logger.h>
|
||||
#include <mongoose.h>
|
||||
#include <models/controller.h>
|
||||
|
|
@ -21,9 +22,6 @@
|
|||
#include <wiringPi.h>
|
||||
#include <piFace.h>
|
||||
#include <wiring_debug.h>
|
||||
#include <confini.h>
|
||||
|
||||
config_t global_config;
|
||||
|
||||
static struct mg_mgr mgr;
|
||||
|
||||
|
|
@ -43,7 +41,7 @@ terminate(int signum)
|
|||
controller_free(global_controller);
|
||||
|
||||
LOGGER_DEBUG("freeing relay configs config\n");
|
||||
free(global_config.relay_configs);
|
||||
free(global_config->relay_configs);
|
||||
|
||||
exit(signum);
|
||||
}
|
||||
|
|
@ -66,45 +64,28 @@ main(int argc, const char** argv)
|
|||
signal(SIGABRT, terminate);
|
||||
signal(SIGTERM, terminate);
|
||||
|
||||
openlog("emgauwa-controller", 0, LOG_USER);
|
||||
setlogmask(LOG_UPTO(LOG_INFO));
|
||||
|
||||
|
||||
/******************** LOAD CONFIG ********************/
|
||||
|
||||
global_config.file = "controller.ini";
|
||||
global_config.discovery_port = 4421;
|
||||
global_config.mqtt_port = 1885;
|
||||
config_init();
|
||||
|
||||
global_config.relay_count = 0;
|
||||
global_config.relays_init = -1;
|
||||
cli_t cli;
|
||||
cli_parse(argc, argv, &cli);
|
||||
|
||||
global_config.log_level = LOG_INFO;
|
||||
global_config.log_file = stdout;
|
||||
config_load(global_config, cli.config_file);
|
||||
|
||||
strcpy(global_config.user, "");
|
||||
strcpy(global_config.group, "");
|
||||
|
||||
helper_parse_cli(argc, argv, &global_config);
|
||||
|
||||
FILE * const ini_file = fopen(global_config.file, "rb");
|
||||
if(ini_file == NULL)
|
||||
if(global_config->logging.file == NULL)
|
||||
{
|
||||
LOGGER_CRIT("config file '%s' was not found\n", global_config.file);
|
||||
exit(1);
|
||||
global_config->logging.file = stdout;
|
||||
}
|
||||
if(load_ini_file( ini_file, INI_DEFAULT_FORMAT, NULL, config_load, &global_config))
|
||||
{
|
||||
LOGGER_CRIT("unable to parse ini file\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fclose(ini_file);
|
||||
|
||||
if(global_config.log_file == NULL)
|
||||
if(global_config->include)
|
||||
{
|
||||
global_config.log_file = stdout;
|
||||
config_load_directory(global_config, global_config->include);
|
||||
}
|
||||
openlog("emgauwa-controller", 0, LOG_USER);
|
||||
|
||||
if(sizeof(time_t) < 8)
|
||||
{
|
||||
|
|
@ -146,38 +127,38 @@ main(int argc, const char** argv)
|
|||
|
||||
int piface_setup = 0;
|
||||
|
||||
for(uint_fast8_t i = 0; i < global_config.relay_count; ++i)
|
||||
for(uint_fast8_t i = 0; i < global_config->relay_count; ++i)
|
||||
{
|
||||
int relay_default = global_config.relay_configs[i].init;
|
||||
int relay_default = global_config->relay_configs[i].init;
|
||||
if(relay_default == -1)
|
||||
{
|
||||
relay_default = global_config.relays_init;
|
||||
relay_default = global_config->relays_init;
|
||||
}
|
||||
if(relay_default == -1)
|
||||
{
|
||||
relay_default = global_config.relay_configs[i].inverted;
|
||||
relay_default = global_config->relay_configs[i].inverted;
|
||||
}
|
||||
|
||||
if(global_config.relay_configs[i].driver == RELAY_DRIVER_GPIO)
|
||||
if(global_config->relay_configs[i].driver == RELAY_DRIVER_GPIO)
|
||||
{
|
||||
pinMode(global_config.relay_configs[i].pin, OUTPUT);
|
||||
driver_gpio_set(global_config.relay_configs[i].pin, relay_default);
|
||||
pinMode(global_config->relay_configs[i].pin, OUTPUT);
|
||||
driver_gpio_set(global_config->relay_configs[i].pin, relay_default);
|
||||
}
|
||||
if(global_config.relay_configs[i].driver == RELAY_DRIVER_PIFACE)
|
||||
if(global_config->relay_configs[i].driver == RELAY_DRIVER_PIFACE)
|
||||
{
|
||||
if(!piface_setup)
|
||||
{
|
||||
piFaceSetup(PIFACE_GPIO_BASE);
|
||||
piface_setup = 1;
|
||||
}
|
||||
driver_piface_set(global_config.relay_configs[i].pin, relay_default);
|
||||
driver_piface_set(global_config->relay_configs[i].pin, relay_default);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/******************** CHECK FOR TESTING RUN ********************/
|
||||
|
||||
if(global_config.run_type == RUN_TYPE_TEST)
|
||||
if(cli.demo_mode)
|
||||
{
|
||||
runner_test(global_controller);
|
||||
terminate(0);
|
||||
|
|
|
|||
|
|
@ -56,9 +56,9 @@ controller_db_select_mapper(sqlite3_stmt *stmt)
|
|||
break;
|
||||
}
|
||||
}
|
||||
new_controller->relays = malloc(sizeof(relay_t) * global_config.relay_count);
|
||||
new_controller->relays = malloc(sizeof(relay_t) * global_config->relay_count);
|
||||
uint8_t i;
|
||||
for(i = 0; i < global_config.relay_count; ++i)
|
||||
for(i = 0; i < global_config->relay_count; ++i)
|
||||
{
|
||||
new_controller->relays[i] = relay_load(i);
|
||||
if(!new_controller->relays[i])
|
||||
|
|
@ -198,14 +198,14 @@ controller_create(void)
|
|||
new_controller->id = 0;
|
||||
uuid_generate(new_controller->uid);
|
||||
|
||||
strncpy(new_controller->name, global_config.name, MAX_NAME_LENGTH);
|
||||
strncpy(new_controller->name, global_config->name, MAX_NAME_LENGTH);
|
||||
new_controller->name[MAX_NAME_LENGTH] = '\0';
|
||||
|
||||
new_controller->command_port = 0;
|
||||
|
||||
new_controller->relays = malloc(sizeof(relay_t) * global_config.relay_count);
|
||||
new_controller->relays = malloc(sizeof(relay_t) * global_config->relay_count);
|
||||
uint8_t i;
|
||||
for(i = 0; i < global_config.relay_count; ++i)
|
||||
for(i = 0; i < global_config->relay_count; ++i)
|
||||
{
|
||||
new_controller->relays[i] = relay_load(i);
|
||||
if(!new_controller->relays[i])
|
||||
|
|
@ -228,7 +228,7 @@ controller_set_name(controller_t *controller, const char *name)
|
|||
void
|
||||
controller_free(controller_t *controller)
|
||||
{
|
||||
for(int i = 0; i < global_config.relay_count; ++i)
|
||||
for(int i = 0; i < global_config->relay_count; ++i)
|
||||
{
|
||||
relay_free(controller->relays[i]);
|
||||
}
|
||||
|
|
@ -249,7 +249,7 @@ controller_debug(controller_t *controller)
|
|||
LOGGER_DEBUG("(1/3) %s @ %p\n", uuid_str, (void*)controller);
|
||||
LOGGER_DEBUG("(2/3) name: %s\n", controller->name);
|
||||
LOGGER_DEBUG("(3/3) relays @ %p:\n", (void*)controller->relays);
|
||||
for(int i = 0; i < global_config.relay_count; ++i)
|
||||
for(int i = 0; i < global_config->relay_count; ++i)
|
||||
{
|
||||
relay_debug(controller->relays[i]);
|
||||
}
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue