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