#include <string.h>
#include <time.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "event.h"
#include "utils.h"
#include "event.h"
+#include "plotter_status.h"
struct dataparser_struct {
struct eventhandler_entry evhandler;
+ char *log_path;
int infd;
int outfd;
int offset;
if (ret == 0) {
pr_err("Read EOF, stopping\n");
+
+ set_plotter_system_status(SYSTEM_STATUS_NO_USB);
return -1;
}
buf[*offset + ret] = 0;
return 0;
}
+static int open_new_logfile(struct dataparser_struct *dt)
+{
+ char path[2048];
+ struct tm *tm;
+ time_t cur_time = time(NULL);
+
+ tm = localtime(&cur_time);
+ strftime(path, sizeof(path), dt->log_path, tm);
+
+ 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) {
+ pr_err("Failed to open file %s for writing: %m\n",
+ path);
+ return -1;
+ }
+
+ return 0;
+}
+
static int read_data(struct eventhandler_entry *h)
{
struct dataparser_struct *dt;
parse_logline(dt->buf, &data);
+ set_plotter_system_status(data.state);
+
/* Fill in possibly missing timestamp */
if (isnan(data.timestamp) || data.timestamp == 0)
data.timestamp = cur_time - dt->start_time;
if (0)
dump_data(&data);
- if (!dt->outfd)
+ if (!dt->log_path)
return 0;
+ if (state_has_changed()) {
+ ret = open_new_logfile(dt);
+ if (ret < 0)
+ return ret;
+ }
+
len = snprintf(str, sizeof(str),
"%d;%d;%.1f;"
"%.3f;%.3f;%.3f;"
.evhandler.handle_event = read_data,
};
-int init_data_parser(int infd, int outfd)
+int init_data_parser(int infd, struct plotter_config *cfg)
{
int ret;
dataparser.evhandler.fd = infd;
dataparser.infd = infd;
- dataparser.outfd = outfd;
+ dataparser.log_path = cfg->log_path;
ret = register_event_handler(&dataparser.evhandler);
if (ret < 0)
#ifndef _DATA_H_
#define _DATA_H_
+#include "config.h"
+
#define MAX_CELLS 10
struct charger_data {
double ext_temp;
};
-int init_data_parser(int infd, int outfd);
+int init_data_parser(int infd, struct plotter_config *cfg);
#endif
#include <stdio.h>
#include <unistd.h>
-#include <fcntl.h>
#include <string.h>
#include "options.h"
#include "trace.h"
#include "data.h"
#include "event.h"
+#include "plotter_status.h"
+
+struct log_plotter_status plotter_state;
int main(int argc, char *argv[])
{
struct plotter_options options;
struct plotter_config cfg;
- int fd, baud, ret = 0, out_fd = 0;
+ int fd, baud, ret = 0;
bzero(&cfg, sizeof(cfg));
goto out;
}
- if (options.output_path) {
- pr_debug("Opening %s for writing the log file\n",
- options.output_path);
-
- out_fd = open(options.output_path,
- O_CREAT | O_APPEND | O_WRONLY, 0664);
- if (out_fd < 0) {
- pr_err("Failed to open file %s for writing: %m\n",
- options.output_path);
- ret = 1;
- goto out;
- }
- }
+ init_data_parser(fd, &cfg);
+ while (plotter_state.system_status != SYSTEM_STATUS_NO_USB) {
+ poll_events(10000);
- init_data_parser(fd, out_fd);
+ if (plotter_state.old_system_status !=
+ plotter_state.system_status) {
+ pr_debug("Status changing from %s to %s",
+ state_to_str(plotter_state.old_system_status),
+ state_to_str(plotter_state.system_status));
- while (1) {
- poll_events(10000);
+ plotter_state.old_system_status =
+ plotter_state.system_status;
+ }
}
out:
--- /dev/null
+#ifndef _PLOTTER_STATE_
+#define _PLOTTER_STATE_
+
+enum system_status {
+ SYSTEM_STATUS_NO_USB = -1,
+ SYSTEM_STATUS_UNKNOWN,
+ SYSTEM_STATUS_CHARGING,
+ SYSTEM_STATUS_DISCHARGING,
+ SYSTEM_STATUS_MONITORING,
+ SYSTEM_STATUS_COMPLETED = 6,
+};
+
+struct log_plotter_status {
+ int old_system_status;
+ int system_status;
+};
+
+extern struct log_plotter_status plotter_state;
+
+static inline void set_plotter_system_status(int new_status)
+{
+ plotter_state.system_status = new_status;
+}
+
+static inline char *state_to_str(int state)
+{
+ switch (state) {
+ case SYSTEM_STATUS_NO_USB:
+ return "No USB";
+
+ case SYSTEM_STATUS_CHARGING:
+ return "Charging";
+
+ case SYSTEM_STATUS_DISCHARGING:
+ return "Discharging";
+
+ case SYSTEM_STATUS_MONITORING:
+ return "Monitoring";
+
+ case SYSTEM_STATUS_COMPLETED:
+ return "Completed";
+
+ default:
+ return "Unknown";
+ }
+}
+
+static inline int state_has_changed(void)
+{
+ return plotter_state.old_system_status != plotter_state.system_status;
+}
+
+#endif