Files
ACSreader/config.c
2019-08-05 12:16:11 +02:00

121 lines
3.3 KiB
C

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>
#include <unistd.h>
#include <confuse.h>
#include <ctype.h>
#include "config.h"
#include "log.h"
int get_device_id(char *device){
int fd;
ssize_t count;
if((fd = open("/sys/class/net/eth0/address", O_RDONLY)) < 0){
writesyslog(LOG_ERR, "error opening /sys/class/net/eth0/address");
return -1;
}
char mac[18];
count = read(fd, &mac, sizeof(mac));
if(count == 0){
writesyslog(LOG_ERR, "error reading mac address");
return -1;
}
mac[count] = '\0';
device[0] = (char) toupper(mac[9]);
device[1] = (char) toupper(mac[10]);
device[2] = (char) toupper(mac[12]);
device[3] = (char) toupper(mac[13]);
device[4] = (char) toupper(mac[15]);
device[5] = (char) toupper(mac[16]);
device[6] = '\0';
return 0;
}
int config_read(struct config_t *config, char *filepath){
if(access(filepath, F_OK) < 0 ) {
//no config found
char *log;
if(asprintf(&log, "No config on path %s", filepath) > 0 ){
writesyslog(LOG_ERR, log);
}
free(log);
return 0;
}
get_device_id(config->device);
config->serialport1 = NULL;
config->serialport2 = NULL;
config->serveraddress = NULL;
config->name = NULL;
config->manualinput = 0;
config->inputlevel = 0;
config->relay = 0;
config->relayinvert = cfg_false;
cfg_opt_t opts[] = {
//CFG_SIMPLE_BOOL("verbose", &verbose),
//CFG_SIMPLE_STR("server", &server),
//CFG_SIMPLE_STR("user", &username),
//CFG_SIMPLE_INT("debug", &debug),
//CFG_SIMPLE_FLOAT("delay", &delay),
//CFG_SIMPLE_STR("serialport", &serialport),
CFG_SIMPLE_STR("serialport1", &config->serialport1),
CFG_SIMPLE_STR("serialport2", &config->serialport2),
CFG_SIMPLE_STR("serveraddress", &config->serveraddress),
CFG_SIMPLE_STR("devicename", &config->name),
CFG_SIMPLE_INT("productclass", &config->ProductClass),
CFG_SIMPLE_INT("manualinput", &config->manualinput),
CFG_SIMPLE_INT("inputlevel", &config->inputlevel),
CFG_SIMPLE_INT("relay", &config->relay),
CFG_SIMPLE_BOOL("relayinvert", &config->relayinvert),
CFG_END()
};
cfg_t *cfg;
cfg = cfg_init(opts, CFGF_NONE);
int ret = cfg_parse(cfg, filepath);
cfg_free(cfg);
if(ret == CFG_PARSE_ERROR){
char *log;
if(asprintf(&log, "%s parsing error", filepath) > 0 ){
writesyslog(LOG_ERR, log);
}
free(log);
return 0;
}
else if (ret == CFG_FILE_ERROR){
char *log;
if(asprintf(&log, "%s not found", filepath) > 0 ){
writesyslog(LOG_ERR, log);
}
free(log);
return 0;
}
return 1;
}
void config_print(struct config_t *config, char *filepath){
printf("------------- configfile ----------\n");
printf("filepath: %s\n", filepath);
printf("serialport 1: %s\n", config->serialport1);
printf("serialport 2: %s\n", config->serialport2);
printf("serveraddress: %s\n", config->serveraddress);
printf("device: %s\n", config->device);
printf("device name: %s\n", config->name);
printf("manualinput: %ld\n", config->manualinput);
printf("inputlevel: %ld\n", config->inputlevel);
printf("relay: %ld\n", config->relay);
printf("relayinvert: %s\n", config->relayinvert ? "true" : "false");
printf("-----------------------------------\n\n");
}