altos/test: Adjust CRC error rate after FEC fix
[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 <stdbool.h>
25 #include <ctype.h>
26 #include "cc-usb.h"
27 #include "cc.h"
28
29 #define NUM_BLOCK       512
30
31 static const struct option options[] = {
32         { .name = "tty", .has_arg = 1, .val = 'T' },
33         { .name = "device", .has_arg = 1, .val = 'D' },
34         { .name = "wait", .has_arg = 0, .val = 'w' },
35         { 0, 0, 0, 0},
36 };
37
38 static void usage(char *program)
39 {
40         fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--wait]\n", program);
41         exit(1);
42 }
43
44 static int get_nonwhite(struct cc_usb *cc, int timeout)
45 {
46         int     c;
47
48         for (;;) {
49                 c = cc_usb_getchar_timeout(cc, timeout);
50                 putchar(c);
51                 if (!isspace(c))
52                         return c;
53         }
54 }
55
56 static const uint8_t test_data[] = {
57         0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0x40, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
58         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
59         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
60         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
61         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
62         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
63         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
64         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
65         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
66         0x17, 0x08,
67 };
68
69 static bool test_failed;
70 static int test_pos;
71
72 static void
73 check_test(uint8_t b)
74 {
75         if (test_pos >= sizeof (test_data) || test_data[test_pos++] != b)
76                 test_failed = true;
77 }
78
79 static uint8_t
80 get_hexc(struct cc_usb *cc)
81 {
82         int     c = get_nonwhite(cc, 1000);
83
84         if ('0' <= c && c <= '9')
85                 return c - '0';
86         if ('a' <= c && c <= 'f')
87                 return c - 'a' + 10;
88         if ('A' <= c && c <= 'F')
89                 return c - 'A' + 10;
90         fprintf(stderr, "Non-hex char '%c'\n", c);
91         exit(1);
92 }
93
94 static int file_crc;
95
96 static const int POLY = 0x8408;
97
98 static int
99 log_crc(int crc, int b)
100 {
101         int     i;
102
103         for (i = 0; i < 8; i++) {
104                 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
105                         crc = (crc >> 1) ^ POLY;
106                 else
107                         crc = crc >> 1;
108                 b >>= 1;
109         }
110         return crc & 0xffff;
111 }
112
113 static uint8_t
114 get_hex(struct cc_usb *cc)
115 {
116         int     a = get_hexc(cc);
117         int     b = get_hexc(cc);
118         int     h = (a << 4) + b;
119
120         file_crc = log_crc(file_crc, h);
121         check_test(h);
122         return h;
123 }
124
125 static int get_32(struct cc_usb *cc)
126 {
127         int     v = 0;
128         int     i;
129         for (i = 0; i < 4; i++) {
130                 v += get_hex(cc) << (i * 8);
131         }
132         return v;
133 }
134
135 static int get_16(struct cc_usb *cc)
136 {
137         int     v = 0;
138         int     i;
139         for (i = 0; i < 2; i++) {
140                 v += get_hex(cc) << (i * 8);
141         }
142         return v;
143 }
144
145 static int swap16(int i)
146 {
147         return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
148 }
149
150 static int find_header(struct cc_usb *cc)
151 {
152         for (;;) {
153                 if (get_nonwhite(cc, -1) == 'M' && get_nonwhite(cc, 1000) == 'P')
154                         return 1;
155         }
156 }
157
158 int
159 main (int argc, char **argv)
160 {
161         struct cc_usb   *cc;
162         char            *tty = NULL;
163         char            *device = NULL;
164         int             c;
165         int             nsamples;
166         int             i;
167         int             crc;
168         int             current_crc;
169         int             wait = 0;
170
171         while ((c = getopt_long(argc, argv, "wT:D:", options, NULL)) != -1) {
172                 switch (c) {
173                 case 'w':
174                         wait = 1;
175                         break;
176                 case 'T':
177                         tty = optarg;
178                         break;
179                 case 'D':
180                         device = optarg;
181                         break;
182                 default:
183                         usage(argv[0]);
184                         break;
185                 }
186         }
187         if (!tty) {
188                 for (;;) {
189                         tty = cc_usbdevs_find_by_arg(device, "FT230X Basic UART");
190                         if (tty) {
191                                 if (wait) {
192                                         printf("tty is %s\n", tty);
193                                         sleep(1);
194                                 }
195                                 break;
196                         }
197                         if (!wait)
198                                 break;
199                         sleep(1);
200                 }
201         }
202         if (!tty)
203                 tty = getenv("ALTOS_TTY");
204         if (!tty)
205                 tty="/dev/ttyUSB0";
206         cc = cc_usb_open(tty);
207         if (!cc)
208                 exit(1);
209         find_header(cc);
210         file_crc = 0xffff;
211         get_32(cc);     /* ground pressure */
212         get_32(cc);     /* min pressure */
213         nsamples = get_16(cc);  /* nsamples */
214         for (i = 0; i < nsamples; i++)
215                 get_16(cc);     /* sample i */
216         current_crc = swap16(~file_crc & 0xffff);
217         crc = get_16(cc);       /* crc */
218         putchar ('\n');
219         if (crc == current_crc) {
220                 if (!test_failed)
221                         printf("\033[32mValid MicroTest Data\033[39m\n");
222                 else
223                         printf("CRC valid\n");
224         }
225         else
226                 printf("CRC invalid\n");
227         cc_usb_close(cc);
228         exit (0);
229 }