| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <unistd.h> |
|---|
| 4 | #include <string.h> |
|---|
| 5 | #include <errno.h> |
|---|
| 6 | #include <err.h> |
|---|
| 7 | #include <fcntl.h> |
|---|
| 8 | #include <signal.h> |
|---|
| 9 | #include <termios.h> |
|---|
| 10 | #include <sys/ioctl.h> |
|---|
| 11 | #include <sys/select.h> |
|---|
| 12 | |
|---|
| 13 | volatile int quit; |
|---|
| 14 | const char *prog; |
|---|
| 15 | |
|---|
| 16 | static void |
|---|
| 17 | set_quit(int s) |
|---|
| 18 | { |
|---|
| 19 | quit = 1; |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | static void |
|---|
| 23 | usage() |
|---|
| 24 | { |
|---|
| 25 | errx(1, "usage: %s <device> <cmd>", prog); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | static void |
|---|
| 29 | readline(int d, char *buf, int l) |
|---|
| 30 | { |
|---|
| 31 | int i; |
|---|
| 32 | int bytes; |
|---|
| 33 | do { |
|---|
| 34 | for (i = 0; i < l; i++) { |
|---|
| 35 | do { |
|---|
| 36 | bytes = read(d, &buf[i], 1); |
|---|
| 37 | if (quit) |
|---|
| 38 | return; |
|---|
| 39 | } while ((bytes < 0 && errno == EAGAIN) || buf[i] == '\r'); |
|---|
| 40 | if (bytes != 1) { |
|---|
| 41 | fprintf(stderr, "readline: read %d\n", bytes); |
|---|
| 42 | err(1, "read line"); |
|---|
| 43 | } |
|---|
| 44 | if (buf[i] == '\n') |
|---|
| 45 | break; |
|---|
| 46 | } |
|---|
| 47 | } while (i == 0); |
|---|
| 48 | if (buf[i] != '\n') { |
|---|
| 49 | warnx("realine: end of line not found"); |
|---|
| 50 | } |
|---|
| 51 | buf[i] = '\0'; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | int |
|---|
| 55 | main(int argc, char * const argv[]) |
|---|
| 56 | { |
|---|
| 57 | int d; |
|---|
| 58 | int bytes; |
|---|
| 59 | int nfds; |
|---|
| 60 | int lineno = 0; |
|---|
| 61 | char buf[256]; |
|---|
| 62 | int i; |
|---|
| 63 | fd_set rfds; |
|---|
| 64 | char *endp; |
|---|
| 65 | int gpioi, currenti, tensioni; |
|---|
| 66 | float current_a, tension_v; |
|---|
| 67 | int val, gpio_val; |
|---|
| 68 | int c_opt = 0, v_opt = 0; |
|---|
| 69 | extern char *optarg; |
|---|
| 70 | extern int optind; |
|---|
| 71 | int ch; |
|---|
| 72 | |
|---|
| 73 | struct termios t, ot; |
|---|
| 74 | |
|---|
| 75 | prog = argv[0]; |
|---|
| 76 | argc -= 1; |
|---|
| 77 | argv += 1; |
|---|
| 78 | |
|---|
| 79 | if (argc != 2) |
|---|
| 80 | usage(); |
|---|
| 81 | |
|---|
| 82 | d = open(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK, 0); |
|---|
| 83 | if (d < 0) { |
|---|
| 84 | err(1, "open %s", argv[0]); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | if (tcgetattr(d, &ot) < 0) { |
|---|
| 88 | err(1, "tcgetattr"); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | quit = 0; |
|---|
| 92 | |
|---|
| 93 | if (signal(SIGINT, set_quit) == SIG_ERR) { |
|---|
| 94 | err(1, "signal(SIGINT)"); |
|---|
| 95 | } |
|---|
| 96 | if (signal(SIGQUIT, set_quit) == SIG_ERR) { |
|---|
| 97 | err(1, "signal(SIGQUIT)"); |
|---|
| 98 | } |
|---|
| 99 | if (signal(SIGPIPE, set_quit) == SIG_ERR) { |
|---|
| 100 | err(1, "signal(SIGPIPE)"); |
|---|
| 101 | } |
|---|
| 102 | if (signal(SIGTERM, set_quit) == SIG_ERR) { |
|---|
| 103 | err(1, "signal(SIGTERM)"); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | t = ot; |
|---|
| 107 | cfmakeraw(&t); |
|---|
| 108 | t.c_cflag |= CLOCAL; |
|---|
| 109 | t.c_cflag &= ~CRTSCTS; |
|---|
| 110 | cfsetspeed(&t, B921600); |
|---|
| 111 | |
|---|
| 112 | if (tcsetattr(d, TCSANOW | TCSAFLUSH, &t) < 0) { |
|---|
| 113 | err(1, "tcsetattr"); |
|---|
| 114 | } |
|---|
| 115 | do { |
|---|
| 116 | write(d, "M0\n", 3); |
|---|
| 117 | readline(d, buf, sizeof(buf)); |
|---|
| 118 | if (quit) |
|---|
| 119 | goto quit; |
|---|
| 120 | } while (memcmp(buf, "OK", 2) != 0); |
|---|
| 121 | |
|---|
| 122 | snprintf(buf, sizeof(buf) - 1, "%s\n", argv[1]); |
|---|
| 123 | write(d, buf, strlen(buf)); |
|---|
| 124 | readline(d, buf, sizeof(buf)); |
|---|
| 125 | if (quit) |
|---|
| 126 | goto quit; |
|---|
| 127 | printf("%s\n", buf); |
|---|
| 128 | quit: |
|---|
| 129 | if (fflush(stdout) == EOF) { |
|---|
| 130 | warn("fflush"); |
|---|
| 131 | } |
|---|
| 132 | if (tcsetattr(d, TCSANOW, &ot) < 0) { |
|---|
| 133 | err(1, "restore tcsetattr"); |
|---|
| 134 | } |
|---|
| 135 | exit(0); |
|---|
| 136 | } |
|---|