From f57347c10e434da2b721d7ac77f9c5c542040d1f Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Tue, 22 Nov 2011 01:21:42 +0100 Subject: [PATCH] Add pty testing program This program allows testing serial line disciplines on computers without any serial port (like my laptop outside of docking station). --- misc/ptytest/Makefile | 4 ++++ misc/ptytest/ptytest.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 misc/ptytest/Makefile create mode 100644 misc/ptytest/ptytest.c diff --git a/misc/ptytest/Makefile b/misc/ptytest/Makefile new file mode 100644 index 0000000..d5b7062 --- /dev/null +++ b/misc/ptytest/Makefile @@ -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 index 0000000..2f46bf9 --- /dev/null +++ b/misc/ptytest/ptytest.c @@ -0,0 +1,52 @@ +#define _XOPEN_SOURCE 600 +#include +#include +#include +#include +#include +#include +#include + +/* + * 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; +} -- 2.39.2