1 /* $NetBSD: irdaattach.c,v 1.9 2011/08/30 19:07:07 joerg 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 __dead static void usage(void);
50
51 int
main(int argc,char ** argv)52 main(int argc, char **argv)
53 {
54 int fd;
55 char *dev, devbuf[100];
56 const char *donglename = "none";
57 struct termios tty;
58 tcflag_t cflag = HUPCL;
59 int ch;
60 sigset_t nsigset;
61 int opt_detach = 1;
62 int pr_pid = 0;
63 int pr_frame = 0;
64 int frdev;
65 int dongle;
66
67 while ((ch = getopt(argc, argv, "d:fhHlmnp")) != -1) {
68 switch (ch) {
69 case 'd':
70 donglename = optarg;
71 break;
72 case 'f':
73 pr_frame = 1;
74 break;
75 case 'h':
76 cflag |= CRTSCTS;
77 break;
78 case 'H':
79 cflag |= CDTRCTS;
80 break;
81 case 'l':
82 cflag |= CLOCAL;
83 break;
84 case 'm':
85 cflag &= ~HUPCL;
86 break;
87 case 'n':
88 opt_detach = 0;
89 break;
90 case 'p':
91 pr_pid = 1;
92 break;
93 case '?':
94 default:
95 usage();
96 }
97 }
98 argc -= optind;
99 argv += optind;
100
101 if (argc != 1)
102 usage();
103
104 dev = *argv;
105 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
106 (void)snprintf(devbuf, sizeof(devbuf),
107 "%s%s", _PATH_DEV, dev);
108 dev = devbuf;
109 }
110 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
111 err(1, "%s", dev);
112 tty.c_cflag = CREAD | CS8 | cflag;
113 tty.c_iflag = 0;
114 tty.c_lflag = 0;
115 tty.c_oflag = 0;
116 tty.c_cc[VMIN] = 1;
117 tty.c_cc[VTIME] = 0;
118 cfsetspeed(&tty, 9600);
119 if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
120 err(1, "tcsetattr");
121 if (ioctl(fd, TIOCSDTR, 0) < 0)
122 err(1, "TIOCSDTR");
123 if (ioctl(fd, TIOCSLINED, "irframe") < 0)
124 err(1, "TIOCSLINED");
125 if (pr_frame) {
126 if (ioctl(fd, IRFRAMETTY_GET_DEVICE, &frdev) < 0)
127 err(1, "IRFRAMETTY_GET_DEVICE");
128 printf("%sirframe%d\n", _PATH_DEV, frdev);
129 }
130 if (strcmp(donglename, "none") == 0)
131 dongle = DONGLE_NONE;
132 else if (strcmp(donglename, "tekram") == 0)
133 dongle = DONGLE_TEKRAM;
134 else if (strcmp(donglename, "jeteye") == 0)
135 dongle = DONGLE_JETEYE;
136 else if (strcmp(donglename, "actisys") == 0)
137 dongle = DONGLE_ACTISYS;
138 else if (strcmp(donglename, "actisys+") == 0)
139 dongle = DONGLE_ACTISYS_PLUS;
140 else if (strcmp(donglename, "litelink") == 0)
141 dongle = DONGLE_LITELINK;
142 else if (strcmp(donglename, "girbil") == 0)
143 dongle = DONGLE_GIRBIL;
144 else
145 errx(1, "Unknown dongle");
146 if (ioctl(fd, IRFRAMETTY_SET_DONGLE, &dongle) < 0)
147 err(1, "IRFRAMETTY_SET_DONGLE");
148
149 fflush(stdout);
150 if (opt_detach && daemon(0, 0) != 0)
151 err(1, "couldn't detach");
152 if (pr_pid)
153 pidfile(NULL);
154 sigemptyset(&nsigset);
155 for (;;)
156 sigsuspend(&nsigset);
157 }
158
159 static void
usage(void)160 usage(void)
161 {
162
163 fprintf(stderr, "usage: %s [-d donglename] [-fhHlmnp] ttyname\n",
164 getprogname());
165 exit(1);
166 }
167