19 #include "plotter_status.h"
21 #define MAX_CHANNELS 4
23 struct dataparser_struct {
24 struct eventhandler_entry evhandler;
25 struct plotter_config *cfg;
27 int outfd[MAX_CHANNELS];
33 static int separate_entries(char *buf, char *entries[], int max_entries)
40 while (*buf && i< max_entries) {
55 static void init_data(struct charger_data *data)
59 bzero(data, sizeof(*data));
60 for (i = 0; i < MAX_CELLS; i++)
61 data->cell_voltage[i] = NAN;
63 data->timestamp = NAN;
64 data->input_voltage = NAN;
65 data->charging_voltage = NAN;
66 data->charging_current = NAN;
67 data->total_charge = NAN;
73 * Convert one log line into charger_data structure. Supports at least
74 * iCharger 6 and 10 cell chargers. Others are not tested.
76 * Returns negative on incorrect data, zero on success
78 static int parse_logline(const char *buf, struct charger_data *data)
80 int i, j, entry_count;
84 char *str = strdup(buf);
87 entry_count = separate_entries(str, entries, ARRAY_SIZE(entries));
91 if (entries[0][0] != '$') {
92 pr_debug("Discarding malformed data entry\n");
97 entries[i]++; /* discard the dollar sign */
98 data->channel = atoi(entries[i++]);
99 if (data->channel > MAX_CHANNELS)
102 data->state = atoi(entries[i++]);
104 /* Timestamp is optional */
105 if (strlen(entries[1]) > 0)
106 data->timestamp = atof(entries[i++]);
108 data->input_voltage = atof(entries[i++]) / 1000.0;
109 data->charging_voltage = atof(entries[i++]) / 1000.0;
110 data->charging_current = atof(entries[i++]) / 100.0;
112 #define ASSIGN_OR_NAN(data, val) \
120 max_cells = entry_count - 10;
121 pr_debug("max_cells: %d\n", max_cells);
123 for (j = 0; j < max_cells; j++, i++) {
124 d = atoi(entries[i]);
125 ASSIGN_OR_NAN(data->cell_voltage[j], d / 1000.0);
131 d = atoi(entries[i++]);
132 ASSIGN_OR_NAN(data->int_temp, d / 10.0);
134 d = atoi(entries[i++]);
135 ASSIGN_OR_NAN(data->ext_temp, d / 10.0);
137 data->total_charge = atof(entries[i++]);
145 static void dump_data(struct charger_data *data)
149 pr_debug("channel %d\n", data->channel);
150 pr_debug("state %d\n", data->state);
151 pr_debug("timestamp %.1f\n", data->timestamp);
152 pr_debug("input_voltage %.3f\n", data->input_voltage);
153 pr_debug("charging_voltage %.3f\n", data->charging_voltage);
154 pr_debug("charging_current %.3f\n", data->charging_current);
156 for (i = 0; i < MAX_CELLS; i++) {
157 if (isnan(data->cell_voltage[i]))
160 pr_debug("cell_voltage[%d] %f\n", i,
161 data->cell_voltage[i]);
164 pr_debug("total_charge %.0f\n", data->total_charge);
165 pr_debug("int_temp %.1f\n", data->int_temp);
166 pr_debug("ext_temp %.1f\n", data->ext_temp);
169 static void print_status_line(struct charger_data *data)
171 int i, active_cells = 0;
175 if (data->timestamp > 3600) {
176 snprintf(time_str, sizeof(time_str), "%d:%02d:%02d",
177 (int)(data->timestamp / 3600),
178 (int)((int)data->timestamp % 3600) / 60,
179 (int)data->timestamp % 60);
181 snprintf(time_str, sizeof(time_str), "%2d:%02d",
182 ((int)data->timestamp % 3600) / 60,
183 (int)data->timestamp % 60);
186 for (i = 0; i < MAX_CELLS; i++) {
187 if (!isnan(data->cell_voltage[i])) {
188 cell_avg += data->cell_voltage[i];
192 cell_avg /= (double)active_cells;
194 pr_info("\r\033[K%s [%s] Ubat: %.3fV Ucell avg: %.3fV "
195 "Current: %.2fA Charge %.0fmAh ",
197 state_to_str(data->state),
198 data->charging_voltage, cell_avg,
199 data->charging_current, data->total_charge);
206 * Read data from a slow device
208 * return 1 when a complete NULL terminated line has been read
210 * return 0 when a partial line has been read and appended to the
213 * return negative on error
215 static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
220 ret = read(infd, buf + *offset, bufsize - *offset - 1);
222 pr_err("read: %m\n");
227 pr_err("Read EOF, stopping\n");
229 set_plotter_system_status(SYSTEM_STATUS_NO_USB);
232 buf[*offset + ret] = 0;
234 for (i = 0; i < ret; i++) {
235 if (buf[i + *offset] == '\n' ||
236 buf[i + *offset] == '\r') {
238 * Got a complete line when there is a newline
239 * at the end. Remove the newline and possible
240 * other junk, such as '\r'
242 buf[i + *offset] = 0;
249 * Fixme! Nothing guarantees that there isn't actually
250 * more data (a part of a new log entry perhaps) after
251 * the newline. So in rare cases (we are prevented
252 * from reading the serial line in very long time) we
253 * might lose data from the stream..
261 static int open_new_logfile(struct dataparser_struct *dt, int channel)
265 time_t cur_time = time(NULL);
267 tm = localtime(&cur_time);
268 strftime(path, sizeof(path), dt->cfg->log_path, tm);
270 store_int_variable_value_to_array("channel", channel, dt->cfg);
271 replace_variables_with_values(path, sizeof(path), dt->cfg);
273 pr_debug("Opening %s for writing the log file\n", path);
275 dt->outfd[channel] = open(path, O_CREAT | O_APPEND | O_WRONLY, 0664);
276 if (dt->outfd[channel] < 0) {
277 pr_err("Failed to open file %s for writing: %m\n",
285 static int read_data(struct eventhandler_entry *h)
287 struct dataparser_struct *dt;
290 struct charger_data data;
294 dt = container_of(h, struct dataparser_struct, evhandler);
296 ret = read_log_line(dt->infd, dt->buf, sizeof(dt->buf), &dt->offset);
301 pr_err("%s %s: EOF\n", __FILE__, __func__);
303 if (strlen(dt->buf) < 5) {
304 pr_debug("discarding truncated log entry\n");
310 dt->start_time = time(NULL);
312 cur_time = time(NULL);
314 parse_logline(dt->buf, &data);
316 set_plotter_system_status(data.state);
318 /* Fill in possibly missing timestamp */
319 if (isnan(data.timestamp) || data.timestamp == 0)
320 data.timestamp = cur_time - dt->start_time;
322 print_status_line(&data);
327 if (!dt->cfg->log_path)
330 if (state_has_changed()) {
331 store_str_variable_value_to_array("status",
332 state_to_str(plotter_state.system_status), dt->cfg);
334 store_int_variable_value_to_array("cell_count",
335 data.cell_count, dt->cfg);
337 ret = open_new_logfile(dt, data.channel);
342 len = snprintf(str, sizeof(str),
345 "%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;"
346 "%.f;%.1f;%.1f\n", /* mAh, and temp */
352 data.charging_voltage,
353 data.charging_current,
355 data.cell_voltage[0],
356 data.cell_voltage[1],
357 data.cell_voltage[2],
358 data.cell_voltage[3],
359 data.cell_voltage[4],
360 data.cell_voltage[5],
361 data.cell_voltage[6],
362 data.cell_voltage[7],
363 data.cell_voltage[8],
364 data.cell_voltage[9],
369 if (dt->outfd[data.channel] <= 0)
370 open_new_logfile(dt, data.channel);
372 ret = write(dt->outfd[data.channel], str, len);
374 pr_err("write: %m\n");
381 static struct dataparser_struct dataparser = {
382 .evhandler.name = "OpenLog Parser",
383 .evhandler.events = EPOLLIN,
384 .evhandler.handle_event = read_data,
387 int init_data_parser(int infd, struct plotter_config *cfg)
391 dataparser.evhandler.fd = infd;
392 dataparser.infd = infd;
393 dataparser.cfg = cfg;
395 ret = register_event_handler(&dataparser.evhandler);