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