2 * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 #include <linux/types.h>
24 #include <sys/types.h>
33 #include "contour-protocol.h"
35 static char *token(char **str, char sep)
40 for (cur = start; *cur && (*cur != sep); ++cur);
48 static void format_csv(struct user_options *opts, struct msg *msg)
50 char *tok = (char *) msg->data;
51 token(&tok, '|'); // unknown
52 char *seq = token(&tok, '|');
53 char *type = token(&tok, '|');
54 char *val = token(&tok, '|');
55 char *unit = token(&tok, '|');
56 token(&tok, '|');// unknown
57 char *notes = token(&tok, '|');
58 token(&tok, '|');// unknown
59 char *time = token(&tok, '\r');
61 unit[strlen(unit)-2] = 0;
63 "%s,\"%.4s-%.2s-%.2s %.2s:%.2s\",\"%s\",\"%s\",\"%s\","
64 "%s,%s,%s,%s,%s,%s,%s\n",
65 seq, &time[0], &time[4], &time[6], &time[8], &time[10],
67 strchr(notes, 'B') ? "X" : "",
68 strchr(notes, 'A') ? "X" : "",
69 strchr(notes, 'S') ? "X" : "",
70 strchr(notes, 'I') ? "X" : "",
71 strchr(notes, 'D') ? "X" : "",
72 strchr(notes, 'X') ? "X" : "",
73 strchr(notes, 'C') ? "X" : ""
77 static int format_message(struct user_options *opts, struct msg *msg, int len)
79 switch (opts->output_format)
82 format_csv(opts, msg);
86 sanitize_ascii(msg->data, len);
87 fprintf(opts->outf, "%s\n", msg->data);
91 fprintf(opts->outf, "%s", msg->data);
95 trace(0, "BUG: Invalid message format %d\n",
105 static int dump_entries(struct user_options *opts, int fd, int usage_code)
112 trace(0, "Reading data ...\n");
113 if (opts->output_format == CSV)
114 fprintf(opts->outf, "#,Time,Type,Value,Unit,\"Before meal\","
115 "\"After meal\",Stress,Sick,\"Dont feel right\","
116 "Activity,\"Control test\"\n");
119 ret = contour_read_entry(fd, usage_code, &msg, extra_delay);
123 ret = format_message(opts, &msg, ret);
130 * Add 20ms magic sleep. This is needed especially for
131 * meters that have reached the internal limit of 2000
132 * glucose readings. We don't want to delay any other
133 * reads as it appears to be needed only for every
141 if ((opts->outf != stdout) || !isatty(fileno(stdout))) {
142 trace(0, "\r%d entries", entries);
146 trace(0, "\nDone.\n");
151 int main(int argc, char *argv[])
153 struct user_options opts;
154 int fd, usage_code, ret;
156 bzero(&opts, sizeof(opts));
157 opts.output_format = CLEAN;
159 if ( read_args(argc, argv, &opts) )
162 trace_level = opts.trace_level;
164 if (opts.output_path) {
165 opts.outf = fopen(opts.output_path, "w");
166 if (opts.outf == NULL) {
167 trace(0, "Failed to open output file \"%s\": %m\n",
175 if (opts.usbdev == NULL)
176 fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
177 CONTOUR_USB_PRODUCT_ID, &usage_code);
179 fd = hiddev_open(opts.usbdev, &usage_code);
183 trace(0, "Initializing ...\n");
184 contour_initialize(fd, usage_code);
186 ret = dump_entries(&opts, fd, usage_code);