source: trunk/software/tty/readtty_timer.c @ 12

Last change on this file since 12 was 12, checked in by bouyer, 5 years ago

Host software for the v2 firmware

File size: 4.6 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <err.h>
6#include <fcntl.h>
7#include <signal.h>
8#include <string.h>
9#include <termios.h>
10#include <sys/ioctl.h>
11#include <sys/select.h>
12
13volatile int quit;
14
15static void
16set_quit(int s)
17{
18        quit = 1;
19}
20
21static void
22readline(int d, char *buf, int l)
23{
24        int i;
25        int bytes;
26        do {
27                for (i = 0; i < l; i++) {
28                        do {
29                                bytes = read(d, &buf[i], 1);
30                                if (quit)
31                                        return;
32                        } while ((bytes < 0 && errno == EAGAIN) || buf[i] == '\r');
33                        if (bytes != 1) {
34                                fprintf(stderr, "readline: read %d\n", bytes);
35                                err(1, "read line");
36                        }
37                        if (buf[i] == '\n')
38                                break;
39                }
40        } while (i == 0);
41        if (buf[i] != '\n') {
42                errx(1, "realine: end of line not found");
43        }
44        buf[i] = '\0';
45}
46
47int
48main(int argc, const char *argv[])
49{
50        int d;
51        int bytes;
52        int nfds;
53        char buf[256];
54        int i;
55        fd_set rfds;
56        char gpio[3], current[4], tension[4];
57        char *endp;
58        int gpioi, currenti, tensioni;
59        int gpioval, currentval, tensionval;
60        int exitst = 0;
61
62        enum {
63                INIT,
64                GPIO,
65                CURRENT,
66                TENSION,
67                MARK
68        } state;
69
70        struct termios t, ot;
71
72        state = INIT;
73        gpioi = currenti = tensioni = 0;
74
75        if (argc != 3) {
76                errx(1, "usage: %s <device> time", argv[0]);
77        }
78
79        d = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK, 0);
80        if (d < 0) {
81                err(1, "open %s", argv[1]);
82        }
83        fprintf(stderr, "o\n");
84
85        if (tcgetattr(d, &ot) < 0) {
86                err(1, "tcgetattr");
87        }
88        fprintf(stderr, "tg\n");
89
90        quit = 0;
91
92        if (signal(SIGINT, set_quit) == SIG_ERR) {
93                err(1, "signal(SIGINT)");
94        }
95        if (signal(SIGQUIT, set_quit) == SIG_ERR) {
96                err(1, "signal(SIGQUIT)");
97        }
98        if (signal(SIGPIPE, set_quit) == SIG_ERR) {
99                err(1, "signal(SIGPIPE)");
100        }
101        if (signal(SIGTERM, set_quit) == SIG_ERR) {
102                err(1, "signal(SIGTERM)");
103        }
104
105        if (signal(SIGALRM, set_quit) == SIG_ERR) {
106                err(1, "signal(SIGALRM)");
107        }
108
109        t = ot;
110        cfmakeraw(&t);
111        t.c_cflag |= CLOCAL;
112        t.c_cflag &= ~CRTSCTS;
113        cfsetspeed(&t, B921600);
114
115        if (tcsetattr(d, TCSANOW | TCSAFLUSH, &t) < 0) {
116                err(1, "tcsetattr");
117        }
118        fprintf(stderr, "tty ready\n");
119        write(d, "M0\n", 3);
120        write(d, "M0\n", 3);
121        do {
122                readline(d, buf, sizeof(buf));
123                if (quit)
124                        goto quit;
125        } while (memcmp(buf, "OK", 2) != 0);
126        fprintf(stderr, "starting\n");
127        write(d, "M1\n", 3);
128
129        alarm(atoi(argv[2]));
130        FD_ZERO(&rfds);
131        while (quit == 0) {
132                FD_SET(d, &rfds);
133                nfds = select(d+1, &rfds, NULL, NULL, NULL);
134                if (nfds > 0 && FD_ISSET(d, &rfds)) {
135                        bytes = read(d, buf, sizeof(buf));
136                        if (bytes > sizeof(buf)) {
137                                errx(1, "read %d/0x%x bytes", bytes, bytes);
138                        }
139                        if (bytes < 0 && errno != EAGAIN) {
140                                warn("read");
141                                continue;
142                        }
143                        for (i = 0; i < bytes; i++) {
144                                if (i > sizeof(buf))
145                                        abort();
146                                if (bytes > sizeof(buf))
147                                        abort();
148                                if (gpioi > sizeof(buf))
149                                        abort();
150                                if (tensioni > sizeof(buf))
151                                        abort();
152                                if (currenti > sizeof(buf))
153                                        abort();
154                                switch(state) {
155                                case INIT:
156                                        /* look for an X mark */
157                                        if (buf[i] == 'X') {
158                                                state = GPIO;
159                                                gpioi = 0;
160                                        }
161                                        break;
162                                case GPIO:
163                                        gpio[gpioi] = buf[i];
164                                        gpioi++;
165                                        if (gpioi < 2)
166                                                break;
167                                        gpio[gpioi] = '\0';
168                                        gpioval = strtol(gpio, &endp, 16);
169                                        if (*endp != '\0') {
170                                                fprintf(stderr,
171                                                    "gpio error: %c\n",
172                                                    *endp);
173                                                exitst = 1;
174                                                state = INIT;
175                                        } else {
176                                                state = TENSION;
177                                                tensioni = 0;
178                                        }
179                                        break;
180                                case CURRENT:
181                                        current[currenti] = buf[i];
182                                        currenti++;
183                                        if (currenti < 3)
184                                                break;
185                                        current[currenti] = '\0';
186                                        currentval = strtol(current, &endp, 16);
187                                        if (*endp != '\0') {
188                                                fprintf(stderr,
189                                                    "current error: %c\n",
190                                                    *endp);
191                                                exitst = 1;
192                                                state = INIT;
193                                        } else {
194                                                state = MARK;
195                                                tensioni = 0;
196                                        }
197                                        break;
198                                case TENSION:
199                                        tension[tensioni] = buf[i];
200                                        tensioni++;
201                                        if (tensioni < 3)
202                                                break;
203                                        tension[tensioni] = '\0';
204                                        tensionval = strtol(tension, &endp, 16);
205                                        if (*endp != '\0') {
206                                                fprintf(stderr,
207                                                    "tension error: %c\n",
208                                                    *endp);
209                                                exitst = 1;
210                                                state = INIT;
211                                        } else {
212                                                state = CURRENT;
213                                                currenti = 0;
214                                        }
215                                        break;
216                                case MARK:
217                                        if (buf[i] != 'X') {
218                                                fprintf(stderr,
219                                                    "mark error: %c\n",
220                                                    buf[i]);
221                                                exitst = 1;
222                                                state = INIT;
223                                        } else {
224                                                printf("%3d ", gpioval);
225                                                printf("%4d ", tensionval);
226                                                printf("%4d\n", currentval);
227                                                state = GPIO;
228                                                gpioi = 0;
229                                        }
230                                        break;
231                                }
232                        }
233                } else if (nfds < 0) {
234                        warn("select");
235                }
236        }
237quit:
238        /* stop measures */
239        write(d, "M0\n", 3);
240        if (fflush(stdout) == EOF) { 
241                warn("fflush");       
242        }
243
244        if (tcsetattr(d, TCSANOW, &ot) < 0) {
245                err(1, "restore tcsetattr");
246        }
247        exit(exitst);
248}
249
250
Note: See TracBrowser for help on using the repository browser.