it2s-itss-facilities/src/config.c

114 lines
2.9 KiB
C

#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;
}