Home | History | Annotate | Line # | Download | only in btdevctl
btdevctl.c revision 1.1.2.2
      1  1.1.2.2     riz /*	$NetBSD: btdevctl.c,v 1.1.2.2 2006/09/14 21:16:31 riz Exp $	*/
      2      1.1  plunky 
      3      1.1  plunky /*-
      4      1.1  plunky  * Copyright (c) 2006 Itronix Inc.
      5      1.1  plunky  * All rights reserved.
      6      1.1  plunky  *
      7      1.1  plunky  * Written by Iain Hibbert for Itronix Inc.
      8      1.1  plunky  *
      9      1.1  plunky  * Redistribution and use in source and binary forms, with or without
     10      1.1  plunky  * modification, are permitted provided that the following conditions
     11      1.1  plunky  * are met:
     12      1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     13      1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     14      1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     16      1.1  plunky  *    documentation and/or other materials provided with the distribution.
     17      1.1  plunky  * 3. The name of Itronix Inc. may not be used to endorse
     18      1.1  plunky  *    or promote products derived from this software without specific
     19      1.1  plunky  *    prior written permission.
     20      1.1  plunky  *
     21      1.1  plunky  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22      1.1  plunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23      1.1  plunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24      1.1  plunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25      1.1  plunky  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26      1.1  plunky  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27      1.1  plunky  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28      1.1  plunky  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29      1.1  plunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30      1.1  plunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31      1.1  plunky  * POSSIBILITY OF SUCH DAMAGE.
     32      1.1  plunky  */
     33      1.1  plunky 
     34      1.1  plunky #include <sys/cdefs.h>
     35      1.1  plunky __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n"
     36      1.1  plunky 	    "All rights reserved.\n");
     37  1.1.2.2     riz __RCSID("$NetBSD: btdevctl.c,v 1.1.2.2 2006/09/14 21:16:31 riz Exp $");
     38      1.1  plunky 
     39      1.1  plunky #include <prop/proplib.h>
     40  1.1.2.2     riz #include <sys/ioctl.h>
     41      1.1  plunky 
     42  1.1.2.2     riz #include <bluetooth.h>
     43  1.1.2.2     riz #include <ctype.h>
     44      1.1  plunky #include <err.h>
     45      1.1  plunky #include <fcntl.h>
     46      1.1  plunky #include <stdlib.h>
     47      1.1  plunky #include <string.h>
     48      1.1  plunky #include <unistd.h>
     49      1.1  plunky 
     50  1.1.2.2     riz #include <dev/bluetooth/btdev.h>
     51  1.1.2.2     riz 
     52      1.1  plunky #include "btdevctl.h"
     53      1.1  plunky 
     54  1.1.2.2     riz #define BTHUB_PATH		"/dev/bthub"
     55      1.1  plunky 
     56  1.1.2.2     riz int main(int, char *[]);
     57  1.1.2.2     riz void usage(void);
     58  1.1.2.2     riz char *uppercase(const char *);
     59  1.1.2.2     riz int bthub_pioctl(unsigned long, prop_dictionary_t);
     60      1.1  plunky 
     61  1.1.2.2     riz int
     62  1.1.2.2     riz main(int argc, char *argv[])
     63      1.1  plunky {
     64  1.1.2.2     riz 	prop_dictionary_t dev;
     65  1.1.2.2     riz 	prop_object_t obj;
     66  1.1.2.2     riz 	bdaddr_t laddr, raddr;
     67  1.1.2.2     riz 	const char *service;
     68  1.1.2.2     riz 	int ch, query, verbose, attach, detach;
     69  1.1.2.2     riz 
     70  1.1.2.2     riz 	bdaddr_copy(&laddr, BDADDR_ANY);
     71  1.1.2.2     riz 	bdaddr_copy(&raddr, BDADDR_ANY);
     72  1.1.2.2     riz 	service = NULL;
     73  1.1.2.2     riz 	query = FALSE;
     74  1.1.2.2     riz 	verbose = FALSE;
     75  1.1.2.2     riz 	attach = FALSE;
     76  1.1.2.2     riz 	detach = FALSE;
     77  1.1.2.2     riz 
     78  1.1.2.2     riz 	while ((ch = getopt(argc, argv, "Aa:Dd:hqs:v")) != -1) {
     79  1.1.2.2     riz 		switch (ch) {
     80  1.1.2.2     riz 		case 'A': /* Attach device */
     81  1.1.2.2     riz 			attach = TRUE;
     82  1.1.2.2     riz 			break;
     83  1.1.2.2     riz 
     84  1.1.2.2     riz 		case 'a': /* remote address */
     85  1.1.2.2     riz 			if (!bt_aton(optarg, &raddr)) {
     86  1.1.2.2     riz 				struct hostent  *he = NULL;
     87  1.1.2.2     riz 
     88  1.1.2.2     riz 				if ((he = bt_gethostbyname(optarg)) == NULL)
     89  1.1.2.2     riz 					errx(EXIT_FAILURE, "%s: %s",
     90  1.1.2.2     riz 						optarg, hstrerror(h_errno));
     91  1.1.2.2     riz 
     92  1.1.2.2     riz 				bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
     93  1.1.2.2     riz 			}
     94  1.1.2.2     riz 			break;
     95  1.1.2.2     riz 
     96  1.1.2.2     riz 		case 'D': /* Detach device */
     97  1.1.2.2     riz 			detach = TRUE;
     98  1.1.2.2     riz 			break;
     99  1.1.2.2     riz 
    100  1.1.2.2     riz 		case 'd': /* local device address */
    101  1.1.2.2     riz 			if (!bt_devaddr(optarg, &laddr))
    102  1.1.2.2     riz 				err(EXIT_FAILURE, "%s", optarg);
    103  1.1.2.2     riz 
    104  1.1.2.2     riz 			break;
    105  1.1.2.2     riz 
    106  1.1.2.2     riz 		case 'q':
    107  1.1.2.2     riz 			query = TRUE;
    108  1.1.2.2     riz 			break;
    109  1.1.2.2     riz 
    110  1.1.2.2     riz 		case 's': /* service */
    111  1.1.2.2     riz 			service = uppercase(optarg);
    112  1.1.2.2     riz 			break;
    113  1.1.2.2     riz 
    114  1.1.2.2     riz 		case 'v': /* verbose */
    115  1.1.2.2     riz 			verbose = TRUE;
    116  1.1.2.2     riz 			break;
    117  1.1.2.2     riz 
    118  1.1.2.2     riz 		case 'h':
    119  1.1.2.2     riz 		default:
    120  1.1.2.2     riz 			usage();
    121  1.1.2.2     riz 		}
    122  1.1.2.2     riz 	}
    123  1.1.2.2     riz 
    124  1.1.2.2     riz 	argc -= optind;
    125  1.1.2.2     riz 	argv += optind;
    126  1.1.2.2     riz 
    127  1.1.2.2     riz 	if (argc > 0
    128  1.1.2.2     riz 	    || (attach == TRUE && detach == TRUE)
    129  1.1.2.2     riz 	    || bdaddr_any(&laddr)
    130  1.1.2.2     riz 	    || bdaddr_any(&raddr)
    131  1.1.2.2     riz 	    || service == NULL)
    132  1.1.2.2     riz 		usage();
    133      1.1  plunky 
    134  1.1.2.2     riz 	if (attach == FALSE && detach == FALSE)
    135  1.1.2.2     riz 		verbose = TRUE;
    136      1.1  plunky 
    137  1.1.2.2     riz 	dev = db_get(&laddr, &raddr, service);
    138  1.1.2.2     riz 	if (dev == NULL || query == TRUE) {
    139  1.1.2.2     riz 		if (verbose == TRUE)
    140  1.1.2.2     riz 			printf("Performing SDP query for service '%s'..\n", service);
    141      1.1  plunky 
    142  1.1.2.2     riz 		dev = cfg_query(&laddr, &raddr, service);
    143  1.1.2.2     riz 		if (dev == NULL)
    144  1.1.2.2     riz 			errx(EXIT_FAILURE, "%s/%s not found", bt_ntoa(&raddr, NULL), service);
    145      1.1  plunky 
    146  1.1.2.2     riz 		if (!db_set(dev, &laddr, &raddr, service))
    147  1.1.2.2     riz 			errx(EXIT_FAILURE, "service store failed");
    148  1.1.2.2     riz 	}
    149      1.1  plunky 
    150  1.1.2.2     riz 	/* add binary local-bdaddr */
    151  1.1.2.2     riz 	obj = prop_data_create_data(&laddr, sizeof(laddr));
    152  1.1.2.2     riz 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVladdr, obj))
    153  1.1.2.2     riz 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
    154      1.1  plunky 
    155  1.1.2.2     riz 	prop_object_release(obj);
    156      1.1  plunky 
    157  1.1.2.2     riz 	/* add binary remote-bdaddr */
    158  1.1.2.2     riz 	obj = prop_data_create_data(&raddr, sizeof(raddr));
    159  1.1.2.2     riz 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVraddr, obj))
    160  1.1.2.2     riz 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
    161  1.1.2.2     riz 
    162  1.1.2.2     riz 	prop_object_release(obj);
    163  1.1.2.2     riz 
    164  1.1.2.2     riz 	/* add service name */
    165  1.1.2.2     riz 	obj = prop_string_create_cstring(service);
    166  1.1.2.2     riz 	if (obj == NULL || !prop_dictionary_set(dev, BTDEVservice, obj))
    167  1.1.2.2     riz 		errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
    168  1.1.2.2     riz 
    169  1.1.2.2     riz 	prop_object_release(obj);
    170  1.1.2.2     riz 
    171  1.1.2.2     riz 	if (verbose == TRUE)
    172  1.1.2.2     riz 		cfg_print(dev);
    173      1.1  plunky 
    174  1.1.2.2     riz 	if (attach == TRUE)
    175  1.1.2.2     riz 		bthub_pioctl(BTDEV_ATTACH, dev);
    176  1.1.2.2     riz 
    177  1.1.2.2     riz 	if (detach == TRUE)
    178  1.1.2.2     riz 		bthub_pioctl(BTDEV_DETACH, dev);
    179  1.1.2.2     riz 
    180  1.1.2.2     riz 	exit(EXIT_SUCCESS);
    181      1.1  plunky }
    182      1.1  plunky 
    183  1.1.2.2     riz void
    184  1.1.2.2     riz usage(void)
    185      1.1  plunky {
    186      1.1  plunky 
    187  1.1.2.2     riz 	fprintf(stderr,
    188  1.1.2.2     riz 		"usage: %s [-A | -D] [-qv] -a address -d device -s service\n"
    189  1.1.2.2     riz 		"Where:\n"
    190  1.1.2.2     riz 		"\t-A           attach device\n"
    191  1.1.2.2     riz 		"\t-a address   remote device address\n"
    192  1.1.2.2     riz 		"\t-D           detach device\n"
    193  1.1.2.2     riz 		"\t-d device    local device address\n"
    194  1.1.2.2     riz 		"\t-q           force SDP query\n"
    195  1.1.2.2     riz 		"\t-s service   remote service\n"
    196  1.1.2.2     riz 		"\t-v           verbose\n"
    197  1.1.2.2     riz 		"", getprogname());
    198      1.1  plunky 
    199  1.1.2.2     riz 	exit(EXIT_FAILURE);
    200  1.1.2.2     riz }
    201      1.1  plunky 
    202  1.1.2.2     riz char *
    203  1.1.2.2     riz uppercase(const char *arg)
    204  1.1.2.2     riz {
    205  1.1.2.2     riz 	char *str, *ptr;
    206      1.1  plunky 
    207  1.1.2.2     riz 	str = strdup(arg);
    208  1.1.2.2     riz 	if (str == NULL)
    209  1.1.2.2     riz 		err(EXIT_FAILURE, "strdup");
    210      1.1  plunky 
    211  1.1.2.2     riz 	for (ptr = str ; *ptr ; ptr++)
    212  1.1.2.2     riz 		*ptr = (char)toupper((int)*ptr);
    213      1.1  plunky 
    214  1.1.2.2     riz 	return str;
    215      1.1  plunky }
    216      1.1  plunky 
    217      1.1  plunky int
    218  1.1.2.2     riz bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
    219      1.1  plunky {
    220      1.1  plunky 	int fd;
    221      1.1  plunky 
    222  1.1.2.2     riz 	fd = open(BTHUB_PATH, O_WRONLY, 0);
    223      1.1  plunky 	if (fd < 0)
    224  1.1.2.2     riz 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
    225      1.1  plunky 
    226  1.1.2.2     riz 	if (prop_dictionary_send_ioctl(dict, fd, cmd))
    227  1.1.2.2     riz 		err(EXIT_FAILURE, "%s", BTHUB_PATH);
    228      1.1  plunky 
    229      1.1  plunky 	close(fd);
    230  1.1.2.2     riz 	return EXIT_SUCCESS;
    231      1.1  plunky }
    232