xref: /netbsd-src/share/examples/rump/tipsy/tipsy.c (revision 64fbb2e2b8046ae14ae6bb548cf56dbf666df23e)
1*64fbb2e2Spooka /*	$NetBSD: tipsy.c,v 1.2 2010/02/18 16:14:55 pooka Exp $	*/
2ea5f9a0fSpooka 
3ea5f9a0fSpooka /*
4ea5f9a0fSpooka  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
5ea5f9a0fSpooka  *
6ea5f9a0fSpooka  * Redistribution and use in source and binary forms, with or without
7ea5f9a0fSpooka  * modification, are permitted provided that the following conditions
8ea5f9a0fSpooka  * are met:
9ea5f9a0fSpooka  * 1. Redistributions of source code must retain the above copyright
10ea5f9a0fSpooka  *    notice, this list of conditions and the following disclaimer.
11ea5f9a0fSpooka  * 2. Redistributions in binary form must reproduce the above copyright
12ea5f9a0fSpooka  *    notice, this list of conditions and the following disclaimer in the
13ea5f9a0fSpooka  *    documentation and/or other materials provided with the distribution.
14ea5f9a0fSpooka  *
15ea5f9a0fSpooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16ea5f9a0fSpooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17ea5f9a0fSpooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18ea5f9a0fSpooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ea5f9a0fSpooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ea5f9a0fSpooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21ea5f9a0fSpooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ea5f9a0fSpooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ea5f9a0fSpooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ea5f9a0fSpooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ea5f9a0fSpooka  * SUCH DAMAGE.
26ea5f9a0fSpooka  */
27ea5f9a0fSpooka 
28ea5f9a0fSpooka /*
29ea5f9a0fSpooka  * Experimental proof-of-concept program:
30ea5f9a0fSpooka  *
31ea5f9a0fSpooka  * tip-on-booze.  Uses rump kernel for ucom driver + underlying
32ea5f9a0fSpooka  * hardware.  Just shovels bits back and forth between the user
33ea5f9a0fSpooka  * terminal and the ucom device.
34ea5f9a0fSpooka  *
35ea5f9a0fSpooka  * Seems to drop an occasional character for output here and there.
36ea5f9a0fSpooka  * Haven't pinpointed the problem yet.  It happens commonly for certain
37ea5f9a0fSpooka  * patterns such as ctrl-U followed by immediate typing.  Tipsy is quite
38ea5f9a0fSpooka  * sober despite this "feature".
39ea5f9a0fSpooka  */
40ea5f9a0fSpooka 
41ea5f9a0fSpooka #include <sys/types.h>
42ea5f9a0fSpooka #include <sys/ioctl.h>
43ea5f9a0fSpooka 
44ea5f9a0fSpooka #include <rump/rump.h>
45ea5f9a0fSpooka #include <rump/rump_syscalls.h>
46ea5f9a0fSpooka 
47ea5f9a0fSpooka #include <err.h>
48ea5f9a0fSpooka #include <errno.h>
49ea5f9a0fSpooka #include <fcntl.h>
50ea5f9a0fSpooka #include <pthread.h>
51ea5f9a0fSpooka #include <stdio.h>
52ea5f9a0fSpooka #include <stdlib.h>
53ea5f9a0fSpooka #include <string.h>
54ea5f9a0fSpooka #include <unistd.h>
55ea5f9a0fSpooka #include <termios.h>
56ea5f9a0fSpooka 
57ea5f9a0fSpooka /*
58ea5f9a0fSpooka  * We use a shovel thread to get the bits from the rump kernel and
59ea5f9a0fSpooka  * print them on the terminal.  The reason for a thread instead of
60ea5f9a0fSpooka  * polling is that we currently have no way to poll two fd's when
61ea5f9a0fSpooka  * they are in different kernels (stdin in the host, ucom in rump).
62ea5f9a0fSpooka  */
63ea5f9a0fSpooka static void *
shovel(void * arg)64ea5f9a0fSpooka shovel(void *arg)
65ea5f9a0fSpooka {
66ea5f9a0fSpooka 	char buf[64];
67ea5f9a0fSpooka 	ssize_t n;
68ea5f9a0fSpooka 	int fd = (int)(intptr_t)arg;
69ea5f9a0fSpooka 
70ea5f9a0fSpooka 	for (;;) {
71ea5f9a0fSpooka 		n = rump_sys_read(fd, buf, sizeof(buf));
72ea5f9a0fSpooka 		if (__predict_false(n <= 0)) {
73ea5f9a0fSpooka 			if (n == 0)
74ea5f9a0fSpooka 				errx(1, "ucom EOF");
75ea5f9a0fSpooka 			if (n == -1)
76ea5f9a0fSpooka 				err(1, "ucom read");
77ea5f9a0fSpooka 		}
78ea5f9a0fSpooka 		if (write(STDOUT_FILENO, buf, n) != n)
79ea5f9a0fSpooka 			err(1, "write to console");
80ea5f9a0fSpooka 	}
81ea5f9a0fSpooka }
82ea5f9a0fSpooka 
83ea5f9a0fSpooka int
main(int argc,char * argv[])84ea5f9a0fSpooka main(int argc, char *argv[])
85ea5f9a0fSpooka {
86ea5f9a0fSpooka 	pthread_t pt;
87ea5f9a0fSpooka         struct termios tios;
88ea5f9a0fSpooka 	int probeonly = 0;
89ea5f9a0fSpooka 	int com;
90ea5f9a0fSpooka 
91ea5f9a0fSpooka 	if (argc > 1) {
92ea5f9a0fSpooka 		if (argc == 2 && strcmp(argv[1], "probe") == 0) {
93ea5f9a0fSpooka 			probeonly = 1;
94ea5f9a0fSpooka 		} else {
95ea5f9a0fSpooka 			fprintf(stderr, "mind the usage\n");
96ea5f9a0fSpooka 			exit(1);
97ea5f9a0fSpooka 		}
98ea5f9a0fSpooka 	}
99ea5f9a0fSpooka 
100ea5f9a0fSpooka 	if (probeonly)
101ea5f9a0fSpooka 		rump_boot_sethowto(RUMP_AB_VERBOSE);
102ea5f9a0fSpooka 	rump_init();
103*64fbb2e2Spooka 	if (probeonly) {
104*64fbb2e2Spooka 		pause();
105ea5f9a0fSpooka 		exit(0);
106*64fbb2e2Spooka 	}
107ea5f9a0fSpooka 
108ea5f9a0fSpooka 	com = rump_sys_open("/dev/dtyU0", O_RDWR);
109ea5f9a0fSpooka 	if (com == -1)
110ea5f9a0fSpooka 		err(1, "rump ucom open failed");
111ea5f9a0fSpooka 
112ea5f9a0fSpooka 	/*
113ea5f9a0fSpooka 	 * Setup the com port.  You might need to tweak this.
114ea5f9a0fSpooka 	 */
115ea5f9a0fSpooka 	if (rump_sys_ioctl(com, TIOCGETA, &tios) == -1)
116ea5f9a0fSpooka 		err(1, "rump get term");
117ea5f9a0fSpooka 	tios.c_cflag &= ~(CSIZE|PARENB);
118ea5f9a0fSpooka 	tios.c_cflag |= CS8;
119ea5f9a0fSpooka 	tios.c_cflag |= CLOCAL;
120ea5f9a0fSpooka 	tios.c_iflag &= ~(ISTRIP|ICRNL);
121ea5f9a0fSpooka 	tios.c_oflag &= ~OPOST;
122ea5f9a0fSpooka 	tios.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
123ea5f9a0fSpooka 	tios.c_cc[VMIN] = 1;
124ea5f9a0fSpooka 	tios.c_cc[VTIME] = 0;
125ea5f9a0fSpooka 	if (rump_sys_ioctl(com, TIOCSETA, &tios) == -1)
126ea5f9a0fSpooka 		err(1, "rump set term");
127ea5f9a0fSpooka 
128ea5f9a0fSpooka 	/* setup stdin */
129ea5f9a0fSpooka 	if (tcgetattr(STDIN_FILENO, &tios) == -1)
130ea5f9a0fSpooka 		err(1, "host get term");
131ea5f9a0fSpooka 	tios.c_oflag &= ~OPOST;
132ea5f9a0fSpooka 	tios.c_iflag &= ~(INPCK|ICRNL);
133ea5f9a0fSpooka 	tios.c_lflag &= ~(ICANON|IEXTEN|ECHO);
134ea5f9a0fSpooka 	tios.c_cc[VINTR] = tios.c_cc[VQUIT] = tios.c_cc[VSUSP]
135ea5f9a0fSpooka 	    = tios.c_cc[VDSUSP] = tios.c_cc[VDISCARD] = tios.c_cc[VREPRINT]
136ea5f9a0fSpooka 	    = tios.c_cc[VLNEXT] = _POSIX_VDISABLE;
137ea5f9a0fSpooka         tios.c_cc[VMIN] = 1;
138ea5f9a0fSpooka         tios.c_cc[VTIME] = 0;
139ea5f9a0fSpooka         if (tcsetattr(STDIN_FILENO, TCSADRAIN, &tios) == -1)
140ea5f9a0fSpooka 		err(1, "host set term");
141ea5f9a0fSpooka 
142ea5f9a0fSpooka 	if (pthread_create(&pt, NULL, shovel, (void *)(intptr_t)com) == -1)
143ea5f9a0fSpooka 		err(1, "pthread create");
144ea5f9a0fSpooka 
145ea5f9a0fSpooka 	/* read stdin, feed that into the rump kernel */
146ea5f9a0fSpooka 	for (;;) {
147ea5f9a0fSpooka 		char ch;
148ea5f9a0fSpooka 
149ea5f9a0fSpooka 		ch = getchar();
150ea5f9a0fSpooka 		if (rump_sys_write(com, &ch, 1) != 1)
151ea5f9a0fSpooka 			err(1, "rump write");
152ea5f9a0fSpooka 	}
153ea5f9a0fSpooka }
154