[1] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <unistd.h> |
---|
| 4 | #include <errno.h> |
---|
[12] | 5 | #include <err.h> |
---|
[1] | 6 | #include <fcntl.h> |
---|
| 7 | #include <signal.h> |
---|
[12] | 8 | #include <string.h> |
---|
[1] | 9 | #include <termios.h> |
---|
| 10 | #include <sys/ioctl.h> |
---|
[12] | 11 | #include <sys/select.h> |
---|
[1] | 12 | |
---|
| 13 | volatile int quit; |
---|
| 14 | |
---|
| 15 | static void |
---|
| 16 | set_quit(int s) |
---|
| 17 | { |
---|
| 18 | quit = 1; |
---|
| 19 | } |
---|
| 20 | |
---|
[12] | 21 | static void |
---|
| 22 | readline(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 | |
---|
[1] | 47 | int |
---|
| 48 | main(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; |
---|
[12] | 56 | char gpio[3], current[4], tension[4]; |
---|
[1] | 57 | char *endp; |
---|
[12] | 58 | int gpioi, currenti, tensioni; |
---|
| 59 | int gpioval, currentval, tensionval; |
---|
| 60 | int exitst = 0; |
---|
[1] | 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; |
---|
[12] | 73 | gpioi = currenti = tensioni = 0; |
---|
[1] | 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"); |
---|
[12] | 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 | |
---|
[1] | 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)); |
---|
[12] | 136 | if (bytes > sizeof(buf)) { |
---|
| 137 | errx(1, "read %d/0x%x bytes", bytes, bytes); |
---|
| 138 | } |
---|
[1] | 139 | if (bytes < 0 && errno != EAGAIN) { |
---|
| 140 | warn("read"); |
---|
[12] | 141 | continue; |
---|
[1] | 142 | } |
---|
| 143 | for (i = 0; i < bytes; i++) { |
---|
[12] | 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(); |
---|
[1] | 154 | switch(state) { |
---|
| 155 | case INIT: |
---|
| 156 | /* look for an X mark */ |
---|
[12] | 157 | if (buf[i] == 'X') { |
---|
[1] | 158 | state = GPIO; |
---|
[12] | 159 | gpioi = 0; |
---|
| 160 | } |
---|
[1] | 161 | break; |
---|
| 162 | case GPIO: |
---|
[12] | 163 | gpio[gpioi] = buf[i]; |
---|
| 164 | gpioi++; |
---|
| 165 | if (gpioi < 2) |
---|
| 166 | break; |
---|
| 167 | gpio[gpioi] = '\0'; |
---|
| 168 | gpioval = strtol(gpio, &endp, 16); |
---|
[1] | 169 | if (*endp != '\0') { |
---|
| 170 | fprintf(stderr, |
---|
| 171 | "gpio error: %c\n", |
---|
| 172 | *endp); |
---|
[12] | 173 | exitst = 1; |
---|
[1] | 174 | state = INIT; |
---|
| 175 | } else { |
---|
[12] | 176 | state = TENSION; |
---|
| 177 | tensioni = 0; |
---|
[1] | 178 | } |
---|
| 179 | break; |
---|
| 180 | case CURRENT: |
---|
| 181 | current[currenti] = buf[i]; |
---|
| 182 | currenti++; |
---|
| 183 | if (currenti < 3) |
---|
| 184 | break; |
---|
| 185 | current[currenti] = '\0'; |
---|
[12] | 186 | currentval = strtol(current, &endp, 16); |
---|
[1] | 187 | if (*endp != '\0') { |
---|
| 188 | fprintf(stderr, |
---|
| 189 | "current error: %c\n", |
---|
| 190 | *endp); |
---|
[12] | 191 | exitst = 1; |
---|
[1] | 192 | state = INIT; |
---|
| 193 | } else { |
---|
[12] | 194 | state = MARK; |
---|
[1] | 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'; |
---|
[12] | 204 | tensionval = strtol(tension, &endp, 16); |
---|
[1] | 205 | if (*endp != '\0') { |
---|
| 206 | fprintf(stderr, |
---|
| 207 | "tension error: %c\n", |
---|
| 208 | *endp); |
---|
[12] | 209 | exitst = 1; |
---|
[1] | 210 | state = INIT; |
---|
| 211 | } else { |
---|
[12] | 212 | state = CURRENT; |
---|
| 213 | currenti = 0; |
---|
[1] | 214 | } |
---|
| 215 | break; |
---|
| 216 | case MARK: |
---|
| 217 | if (buf[i] != 'X') { |
---|
| 218 | fprintf(stderr, |
---|
| 219 | "mark error: %c\n", |
---|
| 220 | buf[i]); |
---|
[12] | 221 | exitst = 1; |
---|
[1] | 222 | state = INIT; |
---|
| 223 | } else { |
---|
[12] | 224 | printf("%3d ", gpioval); |
---|
| 225 | printf("%4d ", tensionval); |
---|
| 226 | printf("%4d\n", currentval); |
---|
[1] | 227 | state = GPIO; |
---|
[12] | 228 | gpioi = 0; |
---|
[1] | 229 | } |
---|
| 230 | break; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } else if (nfds < 0) { |
---|
| 234 | warn("select"); |
---|
| 235 | } |
---|
| 236 | } |
---|
[12] | 237 | quit: |
---|
| 238 | /* stop measures */ |
---|
| 239 | write(d, "M0\n", 3); |
---|
| 240 | if (fflush(stdout) == EOF) { |
---|
| 241 | warn("fflush"); |
---|
| 242 | } |
---|
| 243 | |
---|
[1] | 244 | if (tcsetattr(d, TCSANOW, &ot) < 0) { |
---|
| 245 | err(1, "restore tcsetattr"); |
---|
| 246 | } |
---|
[12] | 247 | exit(exitst); |
---|
[1] | 248 | } |
---|
| 249 | |
---|
| 250 | |
---|