ao-tools: new ao-test-pressure utility for bench testing EasyMotor
[fw/altos] / ao-tools / ao-test-pressure / ao-test-pressure.c
1 /*
2  * Copyright © 2022 Bdale Garbee <bdale@gag.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 <err.h>
20 #include <fcntl.h>
21 #include <gelf.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sysexits.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include "ao-elf.h"
31 #include "ccdbg.h"
32 #include "cc-usb.h"
33 #include "cc.h"
34 #include "ao-verbose.h"
35
36 static const struct option options[] = {
37         { .name = "tty", .has_arg = 1, .val = 'T' },
38         { .name = "device", .has_arg = 1, .val = 'D' },
39         { .name = "raw", .has_arg = 0, .val = 'r' },
40         { .name = "verbose", .has_arg = 1, .val = 'v' },
41         { 0, 0, 0, 0},
42 };
43
44 static void usage(char *program)
45 {
46         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] main|drogue\n", program);
47         exit(1);
48 }
49
50 static void
51 done(struct cc_usb *cc, int code)
52 {
53 /*      cc_usb_printf(cc, "a\n"); */
54         cc_usb_close(cc);
55         exit (code);
56 }
57
58 static char **
59 tok(char *line) {
60         char    **strs = malloc (sizeof (char *)), *str;
61         int     n = 0;
62
63         while ((str = strtok(line, " \t"))) {
64                 line = NULL;
65                 strs = realloc(strs, (n + 2) * sizeof (char *));
66                 strs[n] = strdup(str);
67                 n++;
68         }
69         strs[n] = '\0';
70         return strs;
71 }
72
73 static void
74 free_strs(char **strs) {
75         char    *str;
76         int     i;
77
78         for (i = 0; (str = strs[i]) != NULL; i++)
79                 free(str);
80         free(strs);
81 }
82
83 struct pressure {
84         struct pressure *next;
85         char            **strs;
86 };
87
88 static struct pressure *
89 pressure(struct cc_usb *usb)
90 {
91         struct pressure *head = NULL, **tail = &head;
92         cc_usb_printf(usb, "a\n");
93         for (;;) {
94                 char    line[512];
95                 struct pressure *b;
96
97                 cc_usb_getline(usb, line, sizeof (line));
98                 b = malloc (sizeof (struct pressure));
99                 b->strs = tok(line);
100                 b->next = NULL;
101                 *tail = b;
102                 tail = &b->next;
103                 if (strstr(line, "tick:"))
104                         break;
105         }
106         return head;
107 }
108
109 static void
110 free_pressure(struct pressure *b) {
111         struct pressure *n;
112
113         while (b) {
114                 n = b->next;
115                 free_strs(b->strs);
116                 free(b);
117                 b = n;
118         }
119 }
120
121 static int
122 do_pressure(struct cc_usb *usb) {
123         struct pressure *b = pressure(usb);
124         char **alt = b->strs;
125
126         if (!alt) {
127                 printf("no response\n");
128                 free_pressure(b);
129                 return 0;
130         }
131
132         double pressure = strtod(alt[5], NULL);
133
134         if (pressure < 15000 || pressure > 16000) {
135                 printf ("weird pressure %f\n", pressure);
136                 free_pressure(b);
137                 return 0;
138         }
139
140         printf ("pressure %f\n", pressure);
141
142         return 1;
143 }
144
145 int
146 main (int argc, char **argv)
147 {
148         char                    *device = NULL;
149         int                     c;
150         struct cc_usb           *cc = NULL;
151         char                    *tty = NULL;
152         int                     verbose = 0;
153         int                     ret = 0;
154
155         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
156                 switch (c) {
157                 case 'T':
158                         tty = optarg;
159                         break;
160                 case 'D':
161                         device = optarg;
162                         break;
163                 case 'v':
164                         verbose++;
165                         break;
166                 default:
167                         usage(argv[0]);
168                         break;
169                 }
170         }
171
172         ao_verbose = verbose;
173
174         if (verbose > 1)
175                 ccdbg_add_debug(CC_DEBUG_BITBANG);
176
177         if (!tty)
178                 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
179         if (!tty)
180                 tty = cc_usbdevs_find_by_arg(device, "EasyMotor");
181         if (!tty)
182                 tty = getenv("ALTOS_TTY");
183         if (!tty)
184                 tty="/dev/ttyACM0";
185
186         cc = cc_usb_open(tty);
187
188         if (!cc)
189                 exit(1);
190
191         if (!do_pressure(cc))
192                 ret = 1;
193         done(cc, ret);
194 }