]> git.itanic.dy.fi Git - glucose/blob - main.c
Contour Next protocol, work in progress
[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-next-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, unsigned char *msg)
49 {
50         char *tok = (char *)msg;
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,
78                         unsigned char *msg, int len)
79 {
80         switch (opts->output_format)
81         {
82         case CSV:
83                 format_csv(opts, msg);
84                 break;
85
86         case CLEAN:
87                 sanitize_ascii(msg, len);
88                 fprintf(opts->outf, "%s\n", msg);
89                 break;
90
91         case RAW:
92                 fprintf(opts->outf, "%s", msg);
93                 break;
94
95         default:
96                 trace(0, "BUG: Invalid message format %d\n",
97                         opts->output_format);
98                 return -1;
99         }
100
101         fflush(opts->outf);
102
103         return 0;
104 }
105
106 static int dump_entries(struct user_options *opts)
107 {
108         unsigned char msg[64 + 1];
109         int ret;
110         int entries = 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_next_read_entry(msg);
120                 msg[MSG_LEN] = 0;
121                 if (ret < 45)
122                         break;
123
124                 ret = format_message(opts, msg, ret);
125                 if (ret < 0)
126                         return ret;
127
128                 entries++;
129
130                 if ((opts->outf != stdout) || !isatty(fileno(stdout))) {
131                         trace(0, "\r%d entries", entries);
132                         fflush(stdout);
133                 }
134         }
135         trace(0, "\nDone.\n");
136
137         return 0;
138 }
139
140 int main(int argc, char *argv[])
141 {
142         struct user_options opts;
143         int ret;
144
145         bzero(&opts, sizeof(opts));
146         opts.output_format = CLEAN;
147
148         if (read_args(argc, argv, &opts))
149                 return -1;
150
151         trace_level = opts.trace_level;
152
153         if (opts.output_path) {
154                 opts.outf = fopen(opts.output_path, "w");
155                 if (opts.outf == NULL) {
156                         trace(0, "Failed to open output file \"%s\": %m\n",
157                                 opts.output_path);
158                         return 1;
159                 }
160         } else {
161                 opts.outf = stdout;
162         }
163
164         trace(0, "Initializing ...\n");
165         contour_next_initialize();
166
167         ret = dump_entries(&opts);
168
169         return ret ? 1 : 0;
170 }