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