source: software/tty/readtty_raw.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.1 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 != 2) {
46                errx(1, "usage: %s <device>", 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        t = ot;
76        cfmakeraw(&t);
77        t.c_cflag |= CLOCAL;
78        t.c_cflag &= ~CRTSCTS;
79        cfsetspeed(&t, B921600);
80
81        if (tcsetattr(d, TCSANOW | TCSAFLUSH, &t) < 0) {
82                err(1, "tcsetattr");
83        }
84        fprintf(stderr, "tty ready\n");
85        FD_ZERO(&rfds);
86        while (quit == 0) {
87                FD_SET(d, &rfds);
88                nfds = select(d+1, &rfds, NULL, NULL, NULL);
89                if (nfds > 0 && FD_ISSET(d, &rfds)) {
90                        bytes = read(d, buf, sizeof(buf));
91                        if (bytes < 0 && errno != EAGAIN) {
92                                warn("read");
93                        }
94                        for (i = 0; i < bytes; i++) {
95                                switch(state) {
96                                case INIT:
97                                        /* look for an X mark */
98                                        if (buf[i] == 'X')
99                                                state = GPIO;
100                                        break;
101                                case GPIO:
102                                        gpio[0] = buf[i];
103                                        gpio[1] = '\0';
104                                        val = strtol(gpio, &endp, 16);
105                                        if (*endp != '\0') {
106                                                fprintf(stderr,
107                                                    "gpio error: %c\n",
108                                                    *endp);
109                                                state = INIT;
110                                        } else {
111                                                state = CURRENT;
112                                                currenti = 0;
113                                                printf("%2d ", val);
114                                        }
115                                        break;
116                                case CURRENT:
117                                        current[currenti] = buf[i];
118                                        currenti++;
119                                        if (currenti < 3)
120                                                break;
121                                        current[currenti] = '\0';
122                                        val = strtol(current, &endp, 16);
123                                        if (*endp != '\0') {
124                                                fprintf(stderr,
125                                                    "current error: %c\n",
126                                                    *endp);
127                                                state = INIT;
128                                        } else {
129                                                state = TENSION;
130                                                tensioni = 0;
131                                                printf("%4d ", val);
132                                        }
133                                        break;
134                                case TENSION:
135                                        tension[tensioni] = buf[i];
136                                        tensioni++;
137                                        if (tensioni < 3)
138                                                break;
139                                        tension[tensioni] = '\0';
140                                        val = strtol(tension, &endp, 16);
141                                        if (*endp != '\0') {
142                                                fprintf(stderr,
143                                                    "tension error: %c\n",
144                                                    *endp);
145                                                state = INIT;
146                                        } else {
147                                                state = MARK;
148                                                printf("%4d\n", val);
149                                        }
150                                        break;
151                                case MARK:
152                                        if (buf[i] != 'X') {
153                                                fprintf(stderr,
154                                                    "mark error: %c\n",
155                                                    buf[i]);
156                                                state = INIT;
157                                        } else {
158                                                state = GPIO;
159                                        }
160                                        break;
161                                }
162                        }
163                } else if (nfds < 0) {
164                        warn("select");
165                }
166        }
167        if (tcsetattr(d, TCSANOW, &ot) < 0) {
168                err(1, "restore tcsetattr");
169        }
170        exit(0);
171}
172
173
Note: See TracBrowser for help on using the repository browser.