From 4f2a6e6fea9c414f8f451ac4f9548e2df5b6dd5d Mon Sep 17 00:00:00 2001 From: emanuel Date: Thu, 29 Oct 2020 18:24:59 +0000 Subject: [PATCH] Config file --- src/CMakeLists.txt | 2 + src/config.c | 113 +++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 6 +++ src/denm.c | 2 +- src/facilities.c | 3 ++ src/facilities.h | 3 ++ 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/config.c create mode 100644 src/config.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0c4a1a0..0ab98c1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,5 @@ ADD_EXECUTABLE(it2s-itss-facilities + config.c queue.c cam.c denm.c @@ -9,6 +10,7 @@ TARGET_LINK_LIBRARIES(it2s-itss-facilities -lit2s-asn-itss-facilities -lit2s-asn-itss-transport -lzmq + -ltoml -lit2s-gps -lpthread -lit2s-asn-camv2 diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..59d556b --- /dev/null +++ b/src/config.c @@ -0,0 +1,113 @@ +#include "facilities.h" + +#include +#include +#include +#include +#include +#include + +#define syslog_emerg(msg, ...) syslog(LOG_EMERG, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) +#define syslog_err(msg, ...) syslog(LOG_ERR, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) + +#ifndef NDEBUG +#define syslog_debug(msg, ...) syslog(LOG_DEBUG, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) +#else +#define syslog_debug(msg, ...) +#endif + +static int extract_val_string(char ** output, toml_table_t *table, char* name) { + toml_raw_t raw; + if (0 == (raw = toml_raw_in(table, name))) { + syslog_err("[itss] [config] error extracting %s from file", name); + return 1; + } + + if (toml_rtos(raw, (char**) output)) { + syslog_err("[itss] [config] error extracting %s to string", name); + return 1; + } + + return 0; +} + +static int extract_val_bool(int * output, toml_table_t *table, char* name) { + toml_raw_t raw; + if (0 == (raw = toml_raw_in(table, name))) { + syslog_err("[itss] [config] error extracting %s from file", name); + return 1; + } + + if (toml_rtob(raw, output)) { + syslog_err("[itss] [config] error extracting %s to bool", name); + return 1; + } + + return 0; +} + +static int extract_val_int(int64_t * output, toml_table_t *table, char* name) { + toml_raw_t raw; + if (0 == (raw = toml_raw_in(table, name))) { + syslog_err("[itss] [config] error extracting %s from file", name); + return 1; + } + + if (toml_rtoi(raw, output)) { + syslog_err("[itss] [config] error extracting %s to int", name); + return 1; + } + + return 0; +} + +int itss_config(void* facilities_s, char* config_file) { + int rv = 0; + + facilities_t *facilities = (facilities_t*) facilities_s; + FILE* fp; + toml_table_t* conf; + + char errbuf[200]; + + /* Open the file. */ + if (0 == (fp = fopen(config_file, "r"))) { + syslog_err("[facilities] [config] couldn't open config file"); + return 1; + } + + /* Run the file through the parser. */ + conf = toml_parse_file(fp, errbuf, sizeof(errbuf)); + if (0 == conf) { + syslog_err("[facilities] [config] error while parsing config file"); + return 1; + } + + fclose(fp); + + // Tables + toml_table_t *general; + + if (0 == (general = toml_table_in(conf, "general"))) {syslog_err("[itss] [config] failed locating [general] table"); return 1;} + + // Values + // General + char *itss_type = NULL; + rv = extract_val_string(&itss_type, general, "itss-type"); + if (strcmp("obu", itss_type) == 0) { + facilities->station_type = 5; + } else if (strcmp("rsu", itss_type) == 0) { + facilities->station_type = 15; + } else { + syslog_err("[itss] [config] Unrecognized ITSS type, running as unkown"); + facilities->station_type = 0; + } + free(itss_type); + + + toml_free(conf); + + + return rv; +} + diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..144afbb --- /dev/null +++ b/src/config.h @@ -0,0 +1,6 @@ +#ifndef ITSS_CONFIG_H +#define ITSS_CONFIG_H + +int itss_config(void *itss_s, char * config_file); + +#endif diff --git a/src/denm.c b/src/denm.c index 23c76a3..b335c3f 100644 --- a/src/denm.c +++ b/src/denm.c @@ -366,7 +366,7 @@ void* den_service(void *fc) { } } - syslog_debug("[facilities] [den] events :: [ %d active | %d cancelled | %d negated ]", den->no_active_events, den->no_cancelled_events, den->no_negated_events); + syslog_info("[facilities] [den] events :: [ %d active | %d cancelled | %d negated ]", den->no_active_events, den->no_cancelled_events, den->no_negated_events); pthread_mutex_unlock(&den->lock); diff --git a/src/facilities.c b/src/facilities.c index 96b8fcf..c23a9b9 100644 --- a/src/facilities.c +++ b/src/facilities.c @@ -1,5 +1,6 @@ #include "facilities.h" #include "cam.h" +#include "config.h" #include "denm.h" #include @@ -346,6 +347,8 @@ int main() { facilities_t facilities; facilities.exit = false; + if (itss_config(&facilities, "/etc/it2s/itss.toml")) return 1; + void *context = zmq_ctx_new(); facilities.ctx = context; void *responder = zmq_socket(context, ZMQ_REP); diff --git a/src/facilities.h b/src/facilities.h index f02a913..94b69a5 100644 --- a/src/facilities.h +++ b/src/facilities.h @@ -19,11 +19,14 @@ typedef struct facilities { // ZMQ void* ctx; + // Transmitter queue_t* tx_queue; // DENM den_t *den; + int station_type; + bool exit; } facilities_t;