PERFORCE change 37478 for review
Marcel Moolenaar
marcel at FreeBSD.org
Wed Sep 3 22:16:50 PDT 2003
http://perforce.freebsd.org/chv.cgi?CH=37478
Change 37478 by marcel at marcel_nfs on 2003/09/03 22:16:00
Simplify the test program. It doesn't try to handle
both sides of the communication anymore. You either
start it as DTE (-t) or as DCE (-c). A loopback (-l)
option has been added to remind me that we can also
take advantage of loopback features...
It has again been verified that flow control without
hardware support works. The SAB 82532 works as sender
(ie CTS_OFLOW works with hardware support), but does
not work as receiver (ie RTS_IFLOW does not work).
Affected files ...
.. //depot/projects/uart/dev/uart/uarttest.c#2 edit
Differences ...
==== //depot/projects/uart/dev/uart/uarttest.c#2 (text+ko) ====
@@ -37,14 +37,21 @@
#include <stdlib.h>
#include <sysexits.h>
#include <termios.h>
+#include <unistd.h>
+
+static void sethow(int);
+static void usage(void);
-static int open_uart(const char*dev);
-static int usage(void);
+#define AS_DCE 1
+#define AS_DTE 2
+#define AS_LOOP 3
+
+int fd, how;
char buffer[16384*4];
static int
-getsig(int fd, const char *dev, size_t count, int oldsig)
+getsig(const char *dev, size_t count, int oldsig)
{
int newsig;
@@ -72,152 +79,144 @@
return (newsig);
}
-static void*
-dte(void *arg)
+static void
+dte(void)
{
- struct termios t0, t1;
ssize_t count, wc;
- int fd, sig;
+ int sig;
- fd = (intptr_t)arg;
- tcgetattr(fd, &t0);
- t1 = t0;
- cfmakeraw(&t1);
- cfsetspeed(&t1, B115200);
- t1.c_cflag |= CCTS_OFLOW;
- tcsetattr(fd, TCSAFLUSH|TCSANOW, &t1);
-
- sleep(1);
count = 0;
errno = 0;
sig = 0;
while (count < sizeof(buffer)) {
- sig = getsig(fd, "dte", count, sig);
+ sig = getsig("dte", count, sig);
wc = write(fd, buffer + count, sizeof(buffer) - count);
if (wc > 0) {
count += wc;
errno = 0;
- } else
- pthread_yield();
+ }
}
-
printf("dte: %u bytes transmitted\n", count);
-
- tcsetattr(fd, TCSADRAIN, &t0);
- return (NULL);
}
-static void*
-dce(void *arg)
+static void
+dce(void)
{
- struct termios t0, t1;
ssize_t count, rc;
- int fd, sig;
+ int sig;
- fd = (intptr_t)arg;
- tcgetattr(fd, &t0);
- t1 = t0;
- cfmakeraw(&t1);
- cfsetspeed(&t1, B115200);
- t1.c_cflag |= CRTS_IFLOW;
- tcsetattr(fd, TCSAFLUSH|TCSANOW, &t1);
-
- sleep(2);
count = 0;
errno = 0;
sig = 0;
- /* fcntl(fd, F_SETFL, 0); */
while (count < sizeof(buffer)) {
- sig = getsig(fd, "dce", count, sig);
- pthread_yield();
+ sig = getsig("dce", count, sig);
rc = MIN(16, sizeof(buffer) - count);
rc = read(fd, buffer + count, rc);
if (rc > 0) {
count += rc;
errno = 0;
- } else
+ } else {
+ printf("sleeping after %u bytes...\n", count);
sleep(1);
+ }
}
printf("dce: %u bytes received\n", count);
-
- tcsetattr(fd, TCSADRAIN, &t0);
- return (NULL);
}
-static void
-run(int dte_fd, int dce_fd)
+int
+main(int argc, char *argv[])
{
- pthread_t dte_thr, dce_thr;
- int error;
+ char buf[PATH_MAX];
+ struct termios t0, t1;
+ int ch;
- error = pthread_create(&dte_thr, NULL, dte, (void*)(intptr_t)dte_fd);
- if (error) {
- warnx("%s: pthread_create: %s (%d)", getprogname(),
- strerror(error), error);
- return;
+ while ((ch = getopt(argc, argv, "clt")) != -1) {
+ switch (ch) {
+ case 'c': /* DCE mode */
+ sethow(AS_DCE);
+ break;
+ case 'l': /* Loopback mode */
+ sethow(AS_LOOP);
+ break;
+ case 't': /* DTE mode */
+ sethow(AS_DTE);
+ break;
+ default:
+ usage();
+ }
}
- error = pthread_create(&dce_thr, NULL, dce, (void*)(intptr_t)dce_fd);
- if (error) {
- warnx("%s: pthread_create: %s (%d)", getprogname(),
- strerror(error), error);
- return;
- }
+ if (how == 0)
+ usage();
- pthread_join(dce_thr, NULL);
- pthread_join(dte_thr, NULL);
-}
+ argc -= optind;
+ argv += optind;
-int
-main(int argc, char *argv[])
-{
- int dce_fd, dte_fd;
+ if (argc != 1)
+ usage();
- if (argc != 3)
- return (usage());
+ fd = open(argv[0], O_RDWR | O_NONBLOCK);
+ if (fd == -1) {
+ if (errno == ENOENT) {
+ snprintf(buf, PATH_MAX, "/dev/%s", argv[0]);
+ fd = open(buf, O_RDWR | O_NONBLOCK);
+ if (fd == -1)
+ warn("opening `%s' and `%s'", argv[0], buf);
+ } else
+ warn("opening `%s'", argv[0]);
+ }
+ if (fd == -1)
+ exit(EX_NOINPUT);
- dte_fd = open_uart(argv[1]);
- if (dte_fd == -1)
- return (EX_NOINPUT);
- dce_fd = open_uart(argv[2]);
- if (dce_fd == -1) {
- close(dte_fd);
- return (EX_NOINPUT);
+ tcgetattr(fd, &t0);
+ t1 = t0;
+ cfmakeraw(&t1);
+ cfsetspeed(&t1, B115200);
+ switch (how) {
+ case AS_DCE:
+ t1.c_cflag |= CRTS_IFLOW;
+ break;
+ case AS_DTE:
+ t1.c_cflag |= CCTS_OFLOW;
+ break;
+ default:
+ t1.c_cflag |= CCTS_OFLOW | CRTS_IFLOW;
+ break;
}
+ tcsetattr(fd, TCSAFLUSH|TCSANOW, &t1);
- printf("%s: starting tests...\n", getprogname());
- run(dte_fd, dce_fd);
+ switch (how) {
+ case AS_DCE:
+ dce();
+ break;
+ case AS_DTE:
+ dte();
+ break;
+ default:
+ warn("not today");
+ break;
+ }
- close(dte_fd);
- close(dce_fd);
+ tcsetattr(fd, TCSADRAIN, &t0);
+ sleep(1);
+ close(fd);
return (EX_OK);
}
-static int
-open_uart(const char *dev)
+static void
+sethow(int h)
{
- char buf[PATH_MAX];
- int fd;
- fd = open(dev, O_RDWR | O_NONBLOCK);
- if (fd == -1) {
- if (errno == ENOENT) {
- snprintf(buf, PATH_MAX, "/dev/%s", dev);
- fd = open(buf, O_RDWR | O_NONBLOCK);
- if (fd == -1)
- warn("opening `%s' and `%s'", dev, buf);
- } else
- warn("opening `%s'", dev);
- }
- return (fd);
+ if (how != 0)
+ usage();
+ how = h;
}
-static int
+static void
usage(void)
{
- fprintf(stderr, "usage: %s <dte-device> <dce-device>\n",
- getprogname());
- return (EX_USAGE);
+ fprintf(stderr, "usage: %s -[c|l|t] <device>\n", getprogname());
+ exit(EX_USAGE);
}
More information about the p4-projects
mailing list