source: software/tty/readtty_timer.c @ 1

Last change on this file since 1 was 1, checked in by bouyer, 7 years ago

Initial commit of soc-conso files

File size: 3.2 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <signal.h>
7#include <termios.h>
8#include <sys/ioctl.h>
9
10volatile int quit;
11
12static void
13set_quit(int s)
14{
15        quit = 1;
16}
17
18int
19main(int argc, const char *argv[])
20{
21        int d;
22        int bytes;
23        int nfds;
24        char buf[256];
25        int i;
26        fd_set rfds;
27        char gpio[2], current[4], tension[4];
28        char *endp;
29        int currenti, tensioni;
30        int val;
31
32        enum {
33                INIT,
34                GPIO,
35                CURRENT,
36                TENSION,
37                MARK
38        } state;
39
40        struct termios t, ot;
41
42        state = INIT;
43        currenti = tensioni = 0;
44
45        if (argc != 3) {
46                errx(1, "usage: %s <device> time", argv[0]);
47        }
48
49        d = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK, 0);
50        if (d < 0) {
51                err(1, "open %s", argv[1]);
52        }
53        fprintf(stderr, "o\n");
54
55        if (tcgetattr(d, &ot) < 0) {
56                err(1, "tcgetattr");
57        }
58        fprintf(stderr, "tg\n");
59
60        quit = 0;
61
62        if (signal(SIGINT, set_quit) == SIG_ERR) {
63                err(1, "signal(SIGINT)");
64        }
65        if (signal(SIGQUIT, set_quit) == SIG_ERR) {
66                err(1, "signal(SIGQUIT)");
67        }
68        if (signal(SIGPIPE, set_quit) == SIG_ERR) {
69                err(1, "signal(SIGPIPE)");
70        }
71        if (signal(SIGTERM, set_quit) == SIG_ERR) {
72                err(1, "signal(SIGTERM)");
73        }
74
75        if (signal(SIGALRM, set_quit) == SIG_ERR) {
76                err(1, "signal(SIGALRM)");
77        }
78
79        t = ot;
80        cfmakeraw(&t);
81        t.c_cflag |= CLOCAL;
82        t.c_cflag &= ~CRTSCTS;
83        cfsetspeed(&t, B921600);
84
85        if (tcsetattr(d, TCSANOW | TCSAFLUSH, &t) < 0) {
86                err(1, "tcsetattr");
87        }
88        fprintf(stderr, "tty ready\n");
89        alarm(atoi(argv[2]));
90        FD_ZERO(&rfds);
91        while (quit == 0) {
92                FD_SET(d, &rfds);
93                nfds = select(d+1, &rfds, NULL, NULL, NULL);
94                if (nfds > 0 && FD_ISSET(d, &rfds)) {
95                        bytes = read(d, buf, sizeof(buf));
96                        if (bytes < 0 && errno != EAGAIN) {
97                                warn("read");
98                        }
99                        for (i = 0; i < bytes; i++) {
100                                switch(state) {
101                                case INIT:
102                                        /* look for an X mark */
103                                        if (buf[i] == 'X')
104                                                state = GPIO;
105                                        break;
106                                case GPIO:
107                                        gpio[0] = buf[i];
108                                        gpio[1] = '\0';
109                                        val = strtol(gpio, &endp, 16);
110                                        if (*endp != '\0') {
111                                                fprintf(stderr,
112                                                    "gpio error: %c\n",
113                                                    *endp);
114                                                state = INIT;
115                                        } else {
116                                                state = CURRENT;
117                                                currenti = 0;
118                                                printf("%2d ", val);
119                                        }
120                                        break;
121                                case CURRENT:
122                                        current[currenti] = buf[i];
123                                        currenti++;
124                                        if (currenti < 3)
125                                                break;
126                                        current[currenti] = '\0';
127                                        val = strtol(current, &endp, 16);
128                                        if (*endp != '\0') {
129                                                fprintf(stderr,
130                                                    "current error: %c\n",
131                                                    *endp);
132                                                state = INIT;
133                                        } else {
134                                                state = TENSION;
135                                                tensioni = 0;
136                                                printf("%4d ", val);
137                                        }
138                                        break;
139                                case TENSION:
140                                        tension[tensioni] = buf[i];
141                                        tensioni++;
142                                        if (tensioni < 3)
143                                                break;
144                                        tension[tensioni] = '\0';
145                                        val = strtol(tension, &endp, 16);
146                                        if (*endp != '\0') {
147                                                fprintf(stderr,
148                                                    "tension error: %c\n",
149                                                    *endp);
150                                                state = INIT;
151                                        } else {
152                                                state = MARK;
153                                                printf("%4d\n", val);
154                                        }
155                                        break;
156                                case MARK:
157                                        if (buf[i] != 'X') {
158                                                fprintf(stderr,
159                                                    "mark error: %c\n",
160                                                    buf[i]);
161                                                state = INIT;
162                                        } else {
163                                                state = GPIO;
164                                        }
165                                        break;
166                                }
167                        }
168                } else if (nfds < 0) {
169                        warn("select");
170                }
171        }
172        if (tcsetattr(d, TCSANOW, &ot) < 0) {
173                err(1, "restore tcsetattr");
174        }
175        exit(0);
176}
177
178
Note: See TracBrowser for help on using the repository browser.