]> git.itanic.dy.fi Git - glucose/blob - main.c
main: Print how many entries we have read from the meter
[glucose] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <linux/types.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <errno.h>
10
11 #include "hiddev.h"
12 #include "utils.h"
13 #include "options.h"
14 #include "contour-protocol.h"
15
16 int main(int argc, char *argv[])
17 {
18         FILE *outf;
19         struct user_options opts;
20         struct msg msg;
21         int fd, usage_code, ret, error;
22         int entries = 0;
23
24         read_args(argc, argv, &opts);
25
26         trace_level = opts.trace_level;
27
28         if (opts.output_path) {
29                 outf = fopen(opts.output_path, "w");
30                 if (outf == NULL) {
31                         error = errno;
32                         trace(0, "Failed to open output file %s: %s\n",
33                                 opts.output_path, strerror(error));
34                         return 1;
35                 }
36         } else {
37                 outf = stdout;
38         }
39
40         if (opts.usbdev == NULL)
41                 fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
42                                 CONTOUR_USB_PRODUCT_ID, &usage_code);
43         else
44                 fd = hiddev_open(opts.usbdev, &usage_code);
45         if (fd < 0)
46                 return 1;
47
48         trace(0, "Initializing\n");
49         contour_initialize(fd, usage_code);
50
51         trace(0, "Done! Reading data\n");
52         while (1) {
53                 ret = contour_read_entry(fd, usage_code, &msg);
54                 sanitize_ascii(msg.data, ret);
55
56                 if (ret < 45)
57                         break;
58
59                 fprintf(outf, "%s\n", msg.data);
60
61                 entries++;
62                 if (outf != stdout) {
63                         trace(0, "\r%d", entries);
64                         fflush(stdout);
65                 }
66         }
67         trace(0, "\n");
68
69         return 0;
70 }