]> git.itanic.dy.fi Git - glucose/blob - main.c
Have extra 20ms delay after every 16th read entry
[glucose] / main.c
1 /*
2  * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
3  *
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.
8  *
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.
13  *
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
17  * USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <linux/types.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include "hiddev.h"
31 #include "utils.h"
32 #include "options.h"
33 #include "contour-protocol.h"
34
35 static char *token(char **str, char sep)
36 {
37         char *start = *str;
38         char *cur;
39
40         for (cur = start; *cur && (*cur != sep); ++cur);
41
42         *cur = 0;
43         *str = cur+1;
44
45         return start;
46 }
47
48 static void format_csv(struct user_options *opts, struct msg *msg)
49 {
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');
60
61         unit[strlen(unit)-2] = 0;
62         fprintf(opts->outf,
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],
66                 &type[3], val, unit,
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" : ""
74                 );
75 }
76
77 static int format_message(struct user_options *opts, struct msg *msg, int len)
78 {
79         switch (opts->output_format)
80         {
81         case CSV:
82                 format_csv(opts, msg);
83                 break;
84
85         case CLEAN:
86                 sanitize_ascii(msg->data, len);
87                 fprintf(opts->outf, "%s\n", msg->data);
88                 break;
89
90         case RAW:
91                 fprintf(opts->outf, "%s", msg->data);
92                 break;
93
94         default:
95                 trace(0, "BUG: Invalid message format %d\n",
96                         opts->output_format);
97                 return -1;
98         }
99
100         fflush(opts->outf);
101
102         return 0;
103 }
104
105 static int dump_entries(struct user_options *opts, int fd, int usage_code)
106 {
107         struct msg msg;
108         int ret;
109         int entries = 0;
110         int extra_delay = 0;
111
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");
117
118         while (1) {
119                 ret = contour_read_entry(fd, usage_code, &msg, extra_delay);
120                 if (ret < 45)
121                         break;
122
123                 ret = format_message(opts, &msg, ret);
124                 if (ret < 0)
125                         return ret;
126
127                 entries++;
128
129                 /*
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
134                  * 16th read.
135                  */
136                 if (!(entries % 16))
137                         extra_delay = 20;
138                 else
139                         extra_delay = 0;
140
141                 if ((opts->outf != stdout) || !isatty(fileno(stdout))) {
142                         trace(0, "\r%d entries", entries);
143                         fflush(stdout);
144                 }
145         }
146         trace(0, "\nDone.\n");
147
148         return 0;
149 }
150
151 int main(int argc, char *argv[])
152 {
153         struct user_options opts;
154         int fd, usage_code, ret;
155
156         bzero(&opts, sizeof(opts));
157         opts.output_format = CLEAN;
158
159         if ( read_args(argc, argv, &opts) )
160                 return -1;
161
162         trace_level = opts.trace_level;
163
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",
168                                 opts.output_path);
169                         return 1;
170                 }
171         } else {
172                 opts.outf = stdout;
173         }
174
175         if (opts.usbdev == NULL)
176                 fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
177                                 CONTOUR_USB_PRODUCT_ID, &usage_code);
178         else
179                 fd = hiddev_open(opts.usbdev, &usage_code);
180         if (fd < 0)
181                 return 1;
182
183         trace(0, "Initializing ...\n");
184         contour_initialize(fd, usage_code);
185
186         ret = dump_entries(&opts, fd, usage_code);
187
188         return ret ? 1 : 0;
189 }