]> git.itanic.dy.fi Git - log-plotter/blob - data.c
b663cd50f5daed521764504e38f1f9646a00635f
[log-plotter] / data.c
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <time.h>
5 #include <stdlib.h>
6 #include <sys/epoll.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <string.h>
10 #include <math.h>
11
12 #include "data.h"
13 #include "trace.h"
14 #include "event.h"
15 #include "utils.h"
16 #include "event.h"
17
18 struct dataparser_struct {
19         struct eventhandler_entry evhandler;
20         int infd;
21         int outfd;
22         int offset;
23         time_t start_time;
24         char buf[256];
25 };
26
27 static int separate_entries(char *buf, char *entries[], int max_entries)
28 {
29         int i = 0;
30
31         entries[i] = buf;
32         i++;
33
34         while (*buf && i< max_entries) {
35                 if (*buf != ';') {
36                         buf++;
37                         continue;
38                 }
39
40                 *buf = 0;
41                 buf++;
42                 entries[i] = buf;
43                 i++;
44         }
45
46         return i;
47 }
48
49 static void init_data(struct charger_data *data)
50 {
51         int i;
52
53         bzero(data, sizeof(*data));
54         for (i = 0; i < MAX_CELLS; i++)
55                 data->cell_voltage[i] = NAN;
56
57         data->timestamp = NAN;
58         data->input_voltage = NAN;
59         data->charging_voltage = NAN;
60         data->charging_current = NAN;
61         data->total_charge = NAN;
62         data->int_temp = NAN;
63         data->ext_temp = NAN;
64 }
65
66 /*
67  * Convert one log line into charger_data structure. Supports at least
68  * iCharger 6 and 10 cell chargers. Others are not tested.
69  *
70  * Returns negative on incorrect data, zero on success
71  */
72 static int parse_logline(const char *buf, struct charger_data *data)
73 {
74         int i, j, entry_count;
75         int ret = -1;
76         int max_cells;
77         int d;
78         char *str = strdup(buf);
79         char *entries[64];
80
81         entry_count = separate_entries(str, entries, ARRAY_SIZE(entries));
82
83         init_data(data);
84
85         if (entries[0][0] != '$') {
86                 pr_debug("Discarding malformed data entry\n");
87                 goto out;
88         }
89
90         i = 0;
91         entries[i]++; /* discard the dollar sign */
92         data->channel = atoi(entries[i++]);
93
94         data->state = atoi(entries[i++]);
95
96         /* Timestamp is optional */
97         if (strlen(entries[1]) > 0)
98                 data->timestamp = atof(entries[i++]);
99
100         data->input_voltage = atof(entries[i++]) / 1000.0;
101         data->charging_voltage = atof(entries[i++]) / 1000.0;
102         data->charging_current = atof(entries[i++]) / 100.0;
103
104 #define ASSIGN_OR_NAN(data, val)                \
105         do {                                    \
106                 if ((val) == 0)                 \
107                         (data) = NAN;           \
108                 else                            \
109                         (data) = val;           \
110         } while(0);
111
112         max_cells = entry_count - 10;
113         pr_debug("max_cells: %d\n", max_cells);
114
115         for (j = 0; j < max_cells; j++, i++) {
116                 d = atoi(entries[i]);
117                 ASSIGN_OR_NAN(data->cell_voltage[j], d / 1000.0);
118         }
119
120         d = atoi(entries[i++]);
121         ASSIGN_OR_NAN(data->int_temp, d / 10.0);
122
123         d = atoi(entries[i++]);
124         ASSIGN_OR_NAN(data->ext_temp, d / 10.0);
125
126         data->total_charge = atof(entries[i++]);
127
128         ret = 0;
129 out:
130         free(str);
131         return ret;
132 }
133
134 static void dump_data(struct charger_data *data)
135 {
136         int i;
137
138         pr_debug("channel %d\n", data->channel);
139         pr_debug("state %d\n", data->state);
140         pr_debug("timestamp %.1f\n", data->timestamp);
141         pr_debug("input_voltage %.3f\n", data->input_voltage);
142         pr_debug("charging_voltage %.3f\n", data->charging_voltage);
143         pr_debug("charging_current %.3f\n", data->charging_current);
144
145         for (i = 0; i < MAX_CELLS; i++) {
146                 if (isnan(data->cell_voltage[i]))
147                         continue;
148
149                 pr_debug("cell_voltage[%d] %f\n", i,
150                         data->cell_voltage[i]);
151         }
152
153         pr_debug("total_charge %.0f\n", data->total_charge);
154         pr_debug("int_temp %.1f\n", data->int_temp);
155         pr_debug("ext_temp %.1f\n", data->ext_temp);
156 }
157
158 static void print_status_line(struct charger_data *data)
159 {
160         int i, active_cells = 0;
161         double cell_avg = 0;
162         char time_str[16];
163
164         if (data->timestamp > 3600) {
165                 snprintf(time_str, sizeof(time_str), "%d:%02d:%02d",
166                         (int)(data->timestamp / 3600),
167                         (int)((int)data->timestamp % 3600) / 60,
168                         (int)data->timestamp % 60);
169         } else {
170                 snprintf(time_str, sizeof(time_str), "%2d:%02d",
171                         ((int)data->timestamp % 3600) / 60,
172                         (int)data->timestamp % 60);
173         }
174
175         for (i = 0; i < MAX_CELLS; i++) {
176                 if (!isnan(data->cell_voltage[i])) {
177                         cell_avg += data->cell_voltage[i];
178                         active_cells++;
179                 }
180         }
181         cell_avg /= (double)active_cells;
182
183         pr_info("\r\033[K%8s Ubat: %.3fV Ucell avg: %.3fV "
184                 "Current: %.2fA Charge %.0fmAh ",
185                 time_str, data->charging_voltage, cell_avg,
186                 data->charging_current, data->total_charge);
187
188         fflush(stdout);
189
190 }
191
192 /**
193  * Read data from a slow device
194  *
195  * return 1 when a complete NULL terminated line has been read
196  *
197  * return 0 when a partial line has been read and appended to the
198  *               buffer at @offset
199  *
200  * return negative on error
201  */
202 static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
203 {
204         int ret;
205         int i;
206
207         ret = read(infd, buf + *offset, bufsize - *offset - 1);
208         if (ret < 0) {
209                 pr_err("read: %m\n");
210                 return -1;
211         }
212
213         if (ret == 0) {
214                 pr_err("Read EOF, stopping\n");
215                 return -1;
216         }
217         buf[*offset + ret] = 0;
218
219         for (i = 0; i < ret; i++) {
220                 if (buf[i + *offset] == '\n' ||
221                     buf[i + *offset] == '\r') {
222                         /*
223                          * Got a complete line when there is a newline
224                          * at the end. Remove the newline and possible
225                          * other junk, such as '\r'
226                          */
227                         buf[i + *offset] = 0;
228                         *offset = 0;
229
230                         return 1;
231                 }
232
233                 /*
234                  * Fixme! Nothing guarantees that there isn't actually
235                  * more data (a part of a new log entry perhaps) after
236                  * the newline. So in rare cases (we are prevented
237                  * from reading the serial line in very long time) we
238                  * might lose data from the stream..
239                  */
240         }
241
242         *offset += ret;
243         return 0;
244 }
245
246 static int read_data(struct eventhandler_entry *h)
247 {
248         struct dataparser_struct *dt;
249         time_t cur_time;
250         int ret;
251         struct charger_data data;
252         char str[320];
253         int len;
254
255         dt = container_of(h, struct dataparser_struct, evhandler);
256
257         ret = read_log_line(dt->infd, dt->buf, sizeof(dt->buf), &dt->offset);
258         if (ret <= 0)
259                 return ret;
260
261         if (ret == 0)
262                 pr_err("%s %s: EOF\n", __FILE__, __func__);
263
264         if (strlen(dt->buf) < 5) {
265                 pr_debug("discarding truncated log entry\n");
266                 dt->offset = 0;
267                 return 0;
268         }
269
270         if (!dt->start_time)
271                 dt->start_time = time(NULL);
272
273         cur_time = time(NULL);
274
275         parse_logline(dt->buf, &data);
276
277         /* Fill in possibly missing timestamp */
278         if (isnan(data.timestamp) || data.timestamp == 0)
279                 data.timestamp = cur_time - dt->start_time;
280
281         print_status_line(&data);
282
283         if (0)
284                 dump_data(&data);
285
286         if (!dt->outfd)
287                 return 0;
288
289         len = snprintf(str, sizeof(str),
290                 "%d;%d;%.1f;"
291                 "%.3f;%.3f;%.3f;"
292                 "%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;%.3f;"
293                 "%.f;%.1f;%.1f\n", /* mAh, and temp */
294                 data.channel,
295                 data.state,
296                 data.timestamp,
297
298                 data.input_voltage,
299                 data.charging_voltage,
300                 data.charging_current,
301
302                 data.cell_voltage[0],
303                 data.cell_voltage[1],
304                 data.cell_voltage[2],
305                 data.cell_voltage[3],
306                 data.cell_voltage[4],
307                 data.cell_voltage[5],
308                 data.cell_voltage[6],
309                 data.cell_voltage[7],
310                 data.cell_voltage[8],
311                 data.cell_voltage[9],
312                 data.int_temp,
313                 data.ext_temp,
314                 data.total_charge);
315
316         ret = write(dt->outfd, str, len);
317         if (ret < 0) {
318                 pr_err("write: %m\n");
319                 return -1;;
320         }
321
322         return 0;
323 }
324
325 static struct dataparser_struct dataparser = {
326         .evhandler.name = "OpenLog Parser",
327         .evhandler.events = EPOLLIN,
328         .evhandler.handle_event = read_data,
329 };
330
331 int init_data_parser(int infd, int outfd)
332 {
333         int ret;
334
335         dataparser.evhandler.fd = infd;
336         dataparser.infd = infd;
337         dataparser.outfd = outfd;
338
339         ret = register_event_handler(&dataparser.evhandler);
340         if (ret < 0)
341                 return 1;
342
343         return 0;
344 }