xref: /netbsd-src/usr.sbin/irdaattach/irdaattach.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: irdaattach.c,v 1.8 2009/04/16 02:11:44 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*
32  * Inspired by slattach.c.
33  */
34 
35 #include <sys/ioctl.h>
36 #include <dev/ir/irdaio.h>
37 
38 #include <err.h>
39 #include <fcntl.h>
40 #include <paths.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <termios.h>
46 #include <unistd.h>
47 #include <util.h>
48 
49 int	main(int, char **);
50 void	usage(void);
51 
52 int
53 main(int argc, char **argv)
54 {
55 	int fd;
56 	char *dev, devbuf[100];
57 	const char *donglename = "none";
58 	struct termios tty;
59 	tcflag_t cflag = HUPCL;
60 	int ch;
61 	sigset_t nsigset;
62 	int opt_detach = 1;
63 	int pr_pid = 0;
64 	int pr_frame = 0;
65 	int frdev;
66 	int dongle;
67 
68 	while ((ch = getopt(argc, argv, "d:fhHlmnp")) != -1) {
69 		switch (ch) {
70 		case 'd':
71 			donglename = optarg;
72 			break;
73 		case 'f':
74 			pr_frame = 1;
75 			break;
76 		case 'h':
77 			cflag |= CRTSCTS;
78 			break;
79 		case 'H':
80 			cflag |= CDTRCTS;
81 			break;
82 		case 'l':
83 			cflag |= CLOCAL;
84 			break;
85 		case 'm':
86 			cflag &= ~HUPCL;
87 			break;
88 		case 'n':
89 			opt_detach = 0;
90 			break;
91 		case 'p':
92 			pr_pid = 1;
93 			break;
94 		case '?':
95 		default:
96 			usage();
97 		}
98 	}
99 	argc -= optind;
100 	argv += optind;
101 
102 	if (argc != 1)
103 		usage();
104 
105 	dev = *argv;
106 	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
107 		(void)snprintf(devbuf, sizeof(devbuf),
108 		    "%s%s", _PATH_DEV, dev);
109 		dev = devbuf;
110 	}
111 	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
112 		err(1, "%s", dev);
113 	tty.c_cflag = CREAD | CS8 | cflag;
114 	tty.c_iflag = 0;
115 	tty.c_lflag = 0;
116 	tty.c_oflag = 0;
117 	tty.c_cc[VMIN] = 1;
118 	tty.c_cc[VTIME] = 0;
119 	cfsetspeed(&tty, 9600);
120 	if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
121 		err(1, "tcsetattr");
122 	if (ioctl(fd, TIOCSDTR, 0) < 0)
123 		err(1, "TIOCSDTR");
124 	if (ioctl(fd, TIOCSLINED, "irframe") < 0)
125 		err(1, "TIOCSLINED");
126 	if (pr_frame) {
127 		if (ioctl(fd, IRFRAMETTY_GET_DEVICE, &frdev) < 0)
128 			err(1, "IRFRAMETTY_GET_DEVICE");
129 		printf("%sirframe%d\n", _PATH_DEV, frdev);
130 	}
131 	if (strcmp(donglename, "none") == 0)
132 		dongle = DONGLE_NONE;
133 	else if (strcmp(donglename, "tekram") == 0)
134 		dongle = DONGLE_TEKRAM;
135 	else if (strcmp(donglename, "jeteye") == 0)
136 		dongle = DONGLE_JETEYE;
137 	else if (strcmp(donglename, "actisys") == 0)
138 		dongle = DONGLE_ACTISYS;
139 	else if (strcmp(donglename, "actisys+") == 0)
140 		dongle = DONGLE_ACTISYS_PLUS;
141 	else if (strcmp(donglename, "litelink") == 0)
142 		dongle = DONGLE_LITELINK;
143 	else if (strcmp(donglename, "girbil") == 0)
144 		dongle = DONGLE_GIRBIL;
145 	else
146 		errx(1, "Unknown dongle");
147 	if (ioctl(fd, IRFRAMETTY_SET_DONGLE, &dongle) < 0)
148 		err(1, "IRFRAMETTY_SET_DONGLE");
149 
150 	fflush(stdout);
151 	if (opt_detach && daemon(0, 0) != 0)
152 		err(1, "couldn't detach");
153 	if (pr_pid)
154 		pidfile(NULL);
155 	sigemptyset(&nsigset);
156 	for (;;)
157 		sigsuspend(&nsigset);
158 }
159 
160 void
161 usage()
162 {
163 
164 	fprintf(stderr, "usage: %s [-d donglename] [-fhHlmnp] ttyname\n",
165 		getprogname());
166 	exit(1);
167 }
168