/* r8.c - Connect stdin and stdout to R8's serial port */ /* * Copyright (C) 1995 Jeffrey Chilton * * Permission is granted to anyone to make or distribute copies of * this program, in any medium, provided that the copyright notice * and permission notice are preserved, and that the distributor * grants the recipient permission for further redistribution as * permitted by this notice. * * Author's E-mail address: jwc@chilton.com * */ #include #include #include #include #include #include #define RADIO "/dev/tty00" #define LOCKFILE "/tmp/radio.lock" #define OPTIONS "fre:" extern char *optarg; main(argc, argv) int argc; char *argv[]; { register int i, j; int force; int ignore; int duration; int lfd, rfd; char rec[256]; time_t embargo; time_t current_time; struct termios rti; char buffer[80]; char c; int rc; /* * Get options: * * -r, ignore embargo (only going to read radio) * -f, ignore lock and embargo (manual operation) * -e , embargo for n seconds (for recording) */ force = 0; ignore = 0; duration = 0; rc = getopt(argc, argv, OPTIONS); while (rc != -1) { switch (rc) { case 'e': duration = atoi(optarg); break; case 'f': force = 1; break; case 'r': ignore = 1; break; } rc = getopt(argc, argv, OPTIONS); } /* Locking this file gives us exclusive use of the serial port */ if (!force) { lfd = open(LOCKFILE, O_RDWR | O_CREAT, (mode_t )0x1a0); if (lfd < 0) { perror("lockfile open fails"); printf("ERROR\n"); goto out; } rc = flock(lfd, LOCK_EX | LOCK_NB); if (rc < 0) { printf("LOCKED\n"); goto out; } /* The file contains the embargo-until time */ rc = read(lfd, rec, 256); if (rc < 0) { perror("lockfile read fails"); printf("ERROR\n"); goto out; } embargo = atol(rec); /* If under embargo, and not ignoring, print seconds to go */ if (!ignore) { time(¤t_time); if (embargo > current_time) { printf("%d\n", embargo - current_time); goto out; } } /* If placing embargo write lift time to file */ if (duration > 0) { sprintf(rec, "%d\n", current_time + duration); lseek(lfd, 0, SEEK_SET); rc = write(lfd, rec, strlen(rec)); if (rc < 0) { perror("lockfile write fails"); printf("ERROR\n"); goto out; } } } /* Ok to open and condition the serial port now */ rfd = open(RADIO, O_RDWR); if (rfd < 0) { perror("device open fails"); printf("ERROR\n"); goto out; } rti.c_iflag = IGNBRK; rti.c_oflag = 0; rti.c_cflag = CREAD | CS7 | PARENB | CRTSCTS; rti.c_lflag = 0; rti.c_cc[VMIN] = 0; rti.c_cc[VTIME] = 1; rti.c_ispeed = B9600; rti.c_ospeed = B9600; rc = tcsetattr(rfd, TCSAFLUSH, &rti); if (rc < 0) { perror("tcsetattr fails"); printf("ERROR\n"); goto out; } /* Send commands and collect responses */ fgets(buffer, 80, stdin); while (!feof(stdin)) { buffer[strlen(buffer) - 1] = '\r'; /* Make sure radio is quiet */ rc = read(rfd, &c, 1); while (rc > 0) { rc = read(rfd, &c, 1); } /* Send command */ i = 0; while (buffer[i]) { rc = ioctl(rfd, TIOCDRAIN, 0); if (rc == -1) { perror("TIOCDRAIN ioctl fails"); printf("ERROR\n"); goto out; } rc = write(rfd, &buffer[i], 1); if (rc < 0) { perror("device write fails"); printf("ERROR\n"); goto out; } i++; } /* Collect response */ strcpy(buffer, ""); j = 0; for (i = 0; i < 25; i++) { rc = read(rfd, &c, 1); if (rc == 1) { if (c == '\n' || c == '\r') { buffer[j] = '\0'; break; } buffer[j] = c; j++; } } printf("%s\n", buffer); fgets(buffer, 80, stdin); } /* Re-enables the knobs and buttons */ close(rfd); open(RADIO, O_RDWR); out: exit(0); }