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