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);
128 d = atoi(entries[i++]);
129 ASSIGN_OR_NAN(data->int_temp, d / 10.0);
131 d = atoi(entries[i++]);
132 ASSIGN_OR_NAN(data->ext_temp, d / 10.0);
134 data->total_charge = atof(entries[i++]);
142 static void dump_data(struct charger_data *data)
146 pr_debug("channel %d\n", data->channel);
147 pr_debug("state %d\n", data->state);
148 pr_debug("timestamp %.1f\n", data->timestamp);
149 pr_debug("input_voltage %.3f\n", data->input_voltage);
150 pr_debug("charging_voltage %.3f\n", data->charging_voltage);
151 pr_debug("charging_current %.3f\n", data->charging_current);
153 for (i = 0; i < MAX_CELLS; i++) {
154 if (isnan(data->cell_voltage[i]))
157 pr_debug("cell_voltage[%d] %f\n", i,
158 data->cell_voltage[i]);
161 pr_debug("total_charge %.0f\n", data->total_charge);
162 pr_debug("int_temp %.1f\n", data->int_temp);
163 pr_debug("ext_temp %.1f\n", data->ext_temp);
166 static void print_status_line(struct charger_data *data)
168 int i, active_cells = 0;
172 if (data->timestamp > 3600) {
173 snprintf(time_str, sizeof(time_str), "%d:%02d:%02d",
174 (int)(data->timestamp / 3600),
175 (int)((int)data->timestamp % 3600) / 60,
176 (int)data->timestamp % 60);
178 snprintf(time_str, sizeof(time_str), "%2d:%02d",
179 ((int)data->timestamp % 3600) / 60,
180 (int)data->timestamp % 60);
183 for (i = 0; i < MAX_CELLS; i++) {
184 if (!isnan(data->cell_voltage[i])) {
185 cell_avg += data->cell_voltage[i];
189 cell_avg /= (double)active_cells;
191 pr_info("\r\033[K%s [%s] Ubat: %.3fV Ucell avg: %.3fV "
192 "Current: %.2fA Charge %.0fmAh ",
194 state_to_str(data->state),
195 data->charging_voltage, cell_avg,
196 data->charging_current, data->total_charge);
203 * Read data from a slow device
205 * return 1 when a complete NULL terminated line has been read
207 * return 0 when a partial line has been read and appended to the
210 * return negative on error
212 static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
217 ret = read(infd, buf + *offset, bufsize - *offset - 1);
219 pr_err("read: %m\n");
224 pr_err("Read EOF, stopping\n");
226 set_plotter_system_status(SYSTEM_STATUS_NO_USB);
229 buf[*offset + ret] = 0;
231 for (i = 0; i < ret; i++) {
232 if (buf[i + *offset] == '\n' ||
233 buf[i + *offset] == '\r') {
235 * Got a complete line when there is a newline
236 * at the end. Remove the newline and possible
237 * other junk, such as '\r'
239 buf[i + *offset] = 0;
246 * Fixme! Nothing guarantees that there isn't actually
247 * more data (a part of a new log entry perhaps) after
248 * the newline. So in rare cases (we are prevented
249 * from reading the serial line in very long time) we
250 * might lose data from the stream..
258 static int open_new_logfile(struct dataparser_struct *dt, int channel)
262 time_t cur_time = time(NULL);
264 tm = localtime(&cur_time);
265 strftime(path, sizeof(path), dt->cfg->log_path, tm);
267 store_int_variable_value_to_array("channel", channel, dt->cfg);
268 replace_variables_with_values(path, sizeof(path), dt->cfg);
270 pr_debug("Opening %s for writing the log file\n", path);
272 dt->outfd[channel] = open(path, O_CREAT | O_APPEND | O_WRONLY, 0664);
273 if (dt->outfd[channel] < 0) {
274 pr_err("Failed to open file %s for writing: %m\n",
282 static int read_data(struct eventhandler_entry *h)
284 struct dataparser_struct *dt;
287 struct charger_data data;
291 dt = container_of(h, struct dataparser_struct, evhandler);
293 ret = read_log_line(dt->infd, dt->buf, sizeof(dt->buf), &dt->offset);
298 pr_err("%s %s: EOF\n", __FILE__, __func__);
300 if (strlen(dt->buf) < 5) {
301 pr_debug("discarding truncated log entry\n");
307 dt->start_time = time(NULL);
309 cur_time = time(NULL);
311 parse_logline(dt->buf, &data);
313 set_plotter_system_status(data.state);
315 /* Fill in possibly missing timestamp */
316 if (isnan(data.timestamp) || data.timestamp == 0)
317 data.timestamp = cur_time - dt->start_time;
319 print_status_line(&data);
324 if (!dt->cfg->log_path)
327 if (state_has_changed()) {
328 ret = open_new_logfile(dt, data.channel);
333 len = snprintf(str, sizeof(str),
336 "%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;"
337 "%.f;%.1f;%.1f\n", /* mAh, and temp */
343 data.charging_voltage,
344 data.charging_current,
346 data.cell_voltage[0],
347 data.cell_voltage[1],
348 data.cell_voltage[2],
349 data.cell_voltage[3],
350 data.cell_voltage[4],
351 data.cell_voltage[5],
352 data.cell_voltage[6],
353 data.cell_voltage[7],
354 data.cell_voltage[8],
355 data.cell_voltage[9],
360 if (dt->outfd[data.channel] <= 0)
361 open_new_logfile(dt, data.channel);
363 ret = write(dt->outfd[data.channel], str, len);
365 pr_err("write: %m\n");
372 static struct dataparser_struct dataparser = {
373 .evhandler.name = "OpenLog Parser",
374 .evhandler.events = EPOLLIN,
375 .evhandler.handle_event = read_data,
378 int init_data_parser(int infd, struct plotter_config *cfg)
382 dataparser.evhandler.fd = infd;
383 dataparser.infd = infd;
384 dataparser.cfg = cfg;
386 ret = register_event_handler(&dataparser.evhandler);