]> git.itanic.dy.fi Git - glucose/blob - main.c
449fa157873fe3aed9e0856854a5661d650f3db1
[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
111         trace(0, "Reading data ...\n");
112         if (opts->output_format == CSV)
113                 fprintf(opts->outf, "#,Time,Type,Value,Unit,\"Before meal\","
114                         "\"After meal\",Stress,Sick,\"Dont feel right\","
115                         "Activity,\"Control test\"\n");
116
117         while (1) {
118                 ret = contour_read_entry(fd, usage_code, &msg);
119                 if (ret < 45)
120                         return -1;
121
122                 ret = format_message(opts, &msg, ret);
123                 if (ret < 0)
124                         return ret;
125
126                 entries++;
127
128                 if ((opts->outf != stdout) || !isatty(fileno(stdout))) {
129                         trace(0, "\r%d entries", entries);
130                         fflush(stdout);
131                 }
132         }
133         trace(0, "\nDone.\n");
134
135         return 0;
136 }
137
138 int main(int argc, char *argv[])
139 {
140         struct user_options opts;
141         int fd, usage_code, ret;
142
143         bzero(&opts, sizeof(opts));
144         opts.output_format = CLEAN;
145
146         if ( read_args(argc, argv, &opts) )
147                 return -1;
148
149         trace_level = opts.trace_level;
150
151         if (opts.output_path) {
152                 opts.outf = fopen(opts.output_path, "w");
153                 if (opts.outf == NULL) {
154                         trace(0, "Failed to open output file \"%s\": %m\n",
155                                 opts.output_path);
156                         return 1;
157                 }
158         } else {
159                 opts.outf = stdout;
160         }
161
162         if (opts.usbdev == NULL)
163                 fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
164                                 CONTOUR_USB_PRODUCT_ID, &usage_code);
165         else
166                 fd = hiddev_open(opts.usbdev, &usage_code);
167         if (fd < 0)
168                 return 1;
169
170         trace(0, "Initializing ...\n");
171         contour_initialize(fd, usage_code);
172
173         ret = dump_entries(&opts, fd, usage_code);
174
175         return ret ? 1 : 0;
176 }