20bfd6081627f0e5ad903fa845da68867f7fe9e2
[fw/altos] / ao-tools / ao-sendfake / ao-sendfake.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <err.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <sysexits.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <string.h>
27 #include <stdbool.h>
28 #include "ccdbg.h"
29 #include "cc-usb.h"
30 #include "cc.h"
31 #include "ao-verbose.h"
32
33
34 static const struct option options[] = {
35         { .name = "tty", .has_arg = 1, .val = 'T' },
36         { .name = "device", .has_arg = 1, .val = 'D' },
37         { .name = "verbose", .has_arg = 0, .val = 'v' },
38         { 0, 0, 0, 0},
39 };
40
41 static void usage(char *program)
42 {
43         fprintf(stderr, "usage: %s [--device=<device>] [-tty=<tty>] [--verbose] file\n", program);
44         exit(1);
45 }
46
47 void
48 done(struct cc_usb *cc, int code)
49 {
50 /*      cc_usb_printf(cc, "a\n"); */
51         cc_usb_close(cc);
52         exit (code);
53 }
54
55 int
56 main (int argc, char **argv)
57 {
58         char                    *device = NULL;
59         char                    *filename;
60         unsigned int            s;
61         int                     i;
62         int                     string_num;
63         int                     c;
64         int                     tries;
65         struct cc_usb           *cc = NULL;
66         char                    *tty = NULL;
67         int                     success;
68         int                     verbose = 0;
69         FILE                    *file;
70         char                    buf[1024];
71
72         while ((c = getopt_long(argc, argv, "vT:D:", options, NULL)) != -1) {
73                 switch (c) {
74                 case 'T':
75                         tty = optarg;
76                         break;
77                 case 'D':
78                         device = optarg;
79                         break;
80                 case 'v':
81                         verbose++;
82                         break;
83                 default:
84                         usage(argv[0]);
85                         break;
86                 }
87         }
88
89         ao_verbose = verbose;
90
91         if (verbose > 1)
92                 ccdbg_add_debug(CC_DEBUG_BITBANG);
93
94         filename = argv[optind];
95         if (filename == NULL)
96                 usage(argv[0]);
97
98         file = fopen(filename, "r");
99         if (!file) {
100                 perror (filename);
101                 usage(argv[0]);
102         }
103
104         if (!tty)
105                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
106         if (!tty)
107                 tty = getenv("ALTOS_TTY");
108         if (!tty)
109                 tty="/dev/ttyACM0";
110
111         cc = cc_usb_open(tty);
112
113         if (!cc)
114                 exit(1);
115
116         /* And dump the resulting bits to the device
117          */
118
119         while ((c = fread(buf, 1, sizeof(buf), file)) > 0)
120                 cc_usb_write(cc, buf, c);
121
122         done(cc, 0);
123 }