df05088cadaf2e4cb33d0b61482eac66f5210a5e
[fw/altos] / ao-tools / ao-dump-up / ao-dump-up.c
1 /*
2  * Copyright © 2009 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; either version 2 of the License, or
7  * (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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <string.h>
24 #include "cc-usb.h"
25 #include "cc.h"
26
27 #define NUM_BLOCK       512
28
29 static const struct option options[] = {
30         { .name = "tty", .has_arg = 1, .val = 'T' },
31         { .name = "device", .has_arg = 1, .val = 'D' },
32         { .name = "wait", .has_arg = 0, .val = 'w' },
33         { 0, 0, 0, 0},
34 };
35
36 static void usage(char *program)
37 {
38         fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--wait]\n", program);
39         exit(1);
40 }
41
42 static uint8_t
43 log_checksum(int d[8])
44 {
45         uint8_t sum = 0x5a;
46         int     i;
47
48         for (i = 0; i < 8; i++)
49                 sum += (uint8_t) d[i];
50         return -sum;
51 }
52
53 static int get_nonwhite(struct cc_usb *cc, int timeout)
54 {
55         int     c;
56
57         for (;;) {
58                 c = cc_usb_getchar_timeout(cc, timeout);
59                 putchar(c);
60                 if (!isspace(c))
61                         return c;
62         }
63 }
64
65 static uint8_t
66 get_hexc(struct cc_usb *cc)
67 {
68         int     c = get_nonwhite(cc, 1000);
69
70         if ('0' <= c && c <= '9')
71                 return c - '0';
72         if ('a' <= c && c <= 'f')
73                 return c - 'a' + 10;
74         if ('A' <= c && c <= 'F')
75                 return c - 'A' + 10;
76         fprintf(stderr, "Non-hex char '%c'\n", c);
77         exit(1);
78 }
79
80 static int file_crc;
81
82 static const int POLY = 0x8408;
83
84 static int
85 log_crc(int crc, int b)
86 {
87         int     i;
88
89         for (i = 0; i < 8; i++) {
90                 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
91                         crc = (crc >> 1) ^ POLY;
92                 else
93                         crc = crc >> 1;
94                 b >>= 1;
95         }
96         return crc & 0xffff;
97 }
98
99 static uint8_t
100 get_hex(struct cc_usb *cc)
101 {
102         int     a = get_hexc(cc);
103         int     b = get_hexc(cc);
104         int     h = (a << 4) + b;
105
106         file_crc = log_crc(file_crc, h);
107         return h;
108 }
109
110 static int get_32(struct cc_usb *cc)
111 {
112         int     v = 0;
113         int     i;
114         for (i = 0; i < 4; i++) {
115                 v += get_hex(cc) << (i * 8);
116         }
117         return v;
118 }
119
120 static int get_16(struct cc_usb *cc)
121 {
122         int     v = 0;
123         int     i;
124         for (i = 0; i < 2; i++) {
125                 v += get_hex(cc) << (i * 8);
126         }
127         return v;
128 }
129
130 static int swap16(int i)
131 {
132         return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
133 }
134
135 static int find_header(struct cc_usb *cc)
136 {
137         for (;;) {
138                 if (get_nonwhite(cc, -1) == 'M' && get_nonwhite(cc, 1000) == 'P')
139                         return 1;
140         }
141 }
142
143 int
144 main (int argc, char **argv)
145 {
146         struct cc_usb   *cc;
147         char            *tty = NULL;
148         char            *device = NULL;
149         int             c;
150         char            line[8192];
151         int             nsamples;
152         int             i;
153         int             crc;
154         int             current_crc;
155         int             wait = 0;
156
157         while ((c = getopt_long(argc, argv, "wT:D:", options, NULL)) != -1) {
158                 switch (c) {
159                 case 'w':
160                         wait = 1;
161                         break;
162                 case 'T':
163                         tty = optarg;
164                         break;
165                 case 'D':
166                         device = optarg;
167                         break;
168                 default:
169                         usage(argv[0]);
170                         break;
171                 }
172         }
173         if (!tty) {
174                 for (;;) {
175                         tty = cc_usbdevs_find_by_arg(device, "FT230X Basic UART");
176                         if (tty) {
177                                 if (wait) {
178                                         printf("tty is %s\n", tty);
179                                         sleep(1);
180                                 }
181                                 break;
182                         }
183                         if (!wait)
184                                 break;
185                         sleep(1);
186                 }
187         }
188         if (!tty)
189                 tty = getenv("ALTOS_TTY");
190         if (!tty)
191                 tty="/dev/ttyUSB0";
192         cc = cc_usb_open(tty);
193         if (!cc)
194                 exit(1);
195         find_header(cc);
196         file_crc = 0xffff;
197         get_32(cc);     /* ground pressure */
198         get_32(cc);     /* min pressure */
199         nsamples = get_16(cc);  /* nsamples */
200         for (i = 0; i < nsamples; i++)
201                 get_16(cc);     /* sample i */
202         current_crc = swap16(~file_crc & 0xffff);
203         crc = get_16(cc);       /* crc */
204         putchar ('\n');
205         if (crc == current_crc)
206                 printf("CRC valid\n");
207         else
208                 printf("CRC invalid\n");
209         cc_usb_close(cc);
210         exit (0);
211 }