Config file

This commit is contained in:
emanuel 2020-10-29 18:24:59 +00:00
parent aaf157b7ee
commit 4f2a6e6fea
6 changed files with 128 additions and 1 deletions

View File

@ -1,4 +1,5 @@
ADD_EXECUTABLE(it2s-itss-facilities ADD_EXECUTABLE(it2s-itss-facilities
config.c
queue.c queue.c
cam.c cam.c
denm.c denm.c
@ -9,6 +10,7 @@ TARGET_LINK_LIBRARIES(it2s-itss-facilities
-lit2s-asn-itss-facilities -lit2s-asn-itss-facilities
-lit2s-asn-itss-transport -lit2s-asn-itss-transport
-lzmq -lzmq
-ltoml
-lit2s-gps -lit2s-gps
-lpthread -lpthread
-lit2s-asn-camv2 -lit2s-asn-camv2

113
src/config.c Normal file
View File

@ -0,0 +1,113 @@
#include "facilities.h"
#include <syslog.h>
#include <string.h>
#include <toml.h>
#include <stdlib.h>
#include <it2s-gps.h>
#include <it2s-tiles.h>
#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;
}

6
src/config.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef ITSS_CONFIG_H
#define ITSS_CONFIG_H
int itss_config(void *itss_s, char * config_file);
#endif

View File

@ -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); pthread_mutex_unlock(&den->lock);

View File

@ -1,5 +1,6 @@
#include "facilities.h" #include "facilities.h"
#include "cam.h" #include "cam.h"
#include "config.h"
#include "denm.h" #include "denm.h"
#include <itss-transport/BTPDataRequest.h> #include <itss-transport/BTPDataRequest.h>
@ -346,6 +347,8 @@ int main() {
facilities_t facilities; facilities_t facilities;
facilities.exit = false; facilities.exit = false;
if (itss_config(&facilities, "/etc/it2s/itss.toml")) return 1;
void *context = zmq_ctx_new(); void *context = zmq_ctx_new();
facilities.ctx = context; facilities.ctx = context;
void *responder = zmq_socket(context, ZMQ_REP); void *responder = zmq_socket(context, ZMQ_REP);

View File

@ -19,11 +19,14 @@ typedef struct facilities {
// ZMQ // ZMQ
void* ctx; void* ctx;
// Transmitter
queue_t* tx_queue; queue_t* tx_queue;
// DENM // DENM
den_t *den; den_t *den;
int station_type;
bool exit; bool exit;
} facilities_t; } facilities_t;