]> rtime.felk.cvut.cz Git - linux-lin.git/commitdiff
Add pty testing program
authorMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 22 Nov 2011 00:21:42 +0000 (01:21 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 22 Nov 2011 00:21:42 +0000 (01:21 +0100)
This program allows testing serial line disciplines on computers without
any serial port (like my laptop outside of docking station).

misc/ptytest/Makefile [new file with mode: 0644]
misc/ptytest/ptytest.c [new file with mode: 0644]

diff --git a/misc/ptytest/Makefile b/misc/ptytest/Makefile
new file mode 100644 (file)
index 0000000..d5b7062
--- /dev/null
@@ -0,0 +1,4 @@
+CFLAGS = -Wall -O2
+
+all: ptytest
+       ./ptytest | hexdump -C
diff --git a/misc/ptytest/ptytest.c b/misc/ptytest/ptytest.c
new file mode 100644 (file)
index 0000000..2f46bf9
--- /dev/null
@@ -0,0 +1,52 @@
+#define _XOPEN_SOURCE 600
+#include <stdlib.h>
+#include <fcntl.h>
+#include <error.h>
+#include <errno.h>
+#include <stdio.h>
+#include <poll.h>
+#include <unistd.h>
+
+/*
+ * How I use this program:
+ * - on terminal 1: ./ptytest | hexdump -C
+ * - on root terminal: rmmod slcan; insmod ./slcan.ko; ( sleep 1; ip l set up dev slcan0) & slcan_attach -w /dev/pts/12
+ */
+
+int main()
+{
+       int master_fd;
+       master_fd = posix_openpt(O_RDWR | O_NOCTTY);
+       if (master_fd == -1)
+               error(1, errno, "posix_openpt");
+       if (grantpt(master_fd) == -1)
+               error(1, errno, "grantpt");
+       if (unlockpt(master_fd) == -1)
+               error(1, errno, "unlockpt");
+       fprintf(stderr, "%s\n", ptsname(master_fd));
+
+       struct pollfd fds[2] = {
+               { .fd = 0, .events = POLLIN },
+               { .fd = master_fd, .events = POLLIN },
+       };
+
+       while (1) {
+               int ret = poll(fds, 2, -1);
+               char buffer[100];
+               if (ret == -1)
+                       error(1, errno, "poll");
+               if (fds[0].revents) {
+                       ret = read(fds[0].fd, buffer, sizeof(buffer));
+                       if (ret == -1) error(1, errno, "read(stdin)");
+                       ret = write(fds[1].fd, buffer, ret);
+                       if (ret == -1) error(1, errno, "write(tty)");
+               }
+               if (fds[1].revents) {
+                       ret = read(fds[1].fd, buffer, sizeof(buffer));
+                       if (ret == -1) error(1, errno, "read(tty)");
+                       ret = write(1, buffer, ret);
+                       if (ret == -1) error(1, errno, "write(stdout)");
+               }
+       }
+       return 0;
+}