From e7ce6c43fe34b6f5fd6b3abfeb3e121c6d9bbc0c Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 29 Dec 2013 17:15:33 +0200 Subject: [PATCH] data.c: Implement initial support for handling multiple data channels This initial implementation will simply open a separate log file for every data channel. User can refer to the channel name with $(channel) variable in the file name. Signed-off-by: Timo Kokkonen --- data.c | 30 ++++++++++++++++++++---------- data.h | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/data.c b/data.c index bd7c4d2..0db2394 100644 --- a/data.c +++ b/data.c @@ -18,11 +18,13 @@ #include "event.h" #include "plotter_status.h" +#define MAX_CHANNELS 4 + struct dataparser_struct { struct eventhandler_entry evhandler; - char *log_path; + struct plotter_config *cfg; int infd; - int outfd; + int outfd[MAX_CHANNELS]; int offset; time_t start_time; char buf[256]; @@ -94,6 +96,8 @@ static int parse_logline(const char *buf, struct charger_data *data) i = 0; entries[i]++; /* discard the dollar sign */ data->channel = atoi(entries[i++]); + if (data->channel > MAX_CHANNELS) + data->channel = 0; data->state = atoi(entries[i++]); @@ -251,19 +255,22 @@ static int read_log_line(int infd, char *buf, size_t bufsize, int *offset) return 0; } -static int open_new_logfile(struct dataparser_struct *dt) +static int open_new_logfile(struct dataparser_struct *dt, int channel) { char path[2048]; struct tm *tm; time_t cur_time = time(NULL); tm = localtime(&cur_time); - strftime(path, sizeof(path), dt->log_path, tm); + strftime(path, sizeof(path), dt->cfg->log_path, tm); + + store_int_variable_value_to_array("channel", channel, dt->cfg); + replace_variables_with_values(path, sizeof(path), dt->cfg); pr_debug("Opening %s for writing the log file\n", path); - dt->outfd = open(path, O_CREAT | O_APPEND | O_WRONLY, 0664); - if (dt->outfd < 0) { + dt->outfd[channel] = open(path, O_CREAT | O_APPEND | O_WRONLY, 0664); + if (dt->outfd[channel] < 0) { pr_err("Failed to open file %s for writing: %m\n", path); return -1; @@ -314,11 +321,11 @@ static int read_data(struct eventhandler_entry *h) if (0) dump_data(&data); - if (!dt->log_path) + if (!dt->cfg->log_path) return 0; if (state_has_changed()) { - ret = open_new_logfile(dt); + ret = open_new_logfile(dt, data.channel); if (ret < 0) return ret; } @@ -350,7 +357,10 @@ static int read_data(struct eventhandler_entry *h) data.ext_temp, data.total_charge); - ret = write(dt->outfd, str, len); + if (dt->outfd[data.channel] <= 0) + open_new_logfile(dt, data.channel); + + ret = write(dt->outfd[data.channel], str, len); if (ret < 0) { pr_err("write: %m\n"); return -1;; @@ -371,7 +381,7 @@ int init_data_parser(int infd, struct plotter_config *cfg) dataparser.evhandler.fd = infd; dataparser.infd = infd; - dataparser.log_path = cfg->log_path; + dataparser.cfg = cfg; ret = register_event_handler(&dataparser.evhandler); if (ret < 0) diff --git a/data.h b/data.h index 6e286fa..b9c04c9 100644 --- a/data.h +++ b/data.h @@ -6,7 +6,7 @@ #define MAX_CELLS 10 struct charger_data { - int channel; + unsigned int channel; int state; double timestamp; /* seconds since beginning of the charging */ double input_voltage; -- 2.45.0