2011-08-28 02:22:01 +04:00
|
|
|
/* $NetBSD: btdevctl.c,v 1.10 2011/08/27 22:24:14 joerg Exp $ */
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Itronix Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Iain Hibbert for Itronix Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of Itronix Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific
|
|
|
|
* prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2009-05-12 22:39:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 2006 The NetBSD Foundation, Inc.\
|
|
|
|
@(#) Copyright (c) 2006 Itronix, Inc.\
|
|
|
|
All rights reserved.");
|
2011-08-28 02:22:01 +04:00
|
|
|
__RCSID("$NetBSD: btdevctl.c,v 1.10 2011/08/27 22:24:14 joerg Exp $");
|
2006-07-26 14:30:59 +04:00
|
|
|
|
|
|
|
#include <prop/proplib.h>
|
2006-09-10 19:45:55 +04:00
|
|
|
#include <sys/ioctl.h>
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
#include <bluetooth.h>
|
|
|
|
#include <ctype.h>
|
2006-06-19 19:44:33 +04:00
|
|
|
#include <err.h>
|
2006-07-26 14:30:59 +04:00
|
|
|
#include <fcntl.h>
|
2006-06-19 19:44:33 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
#include <dev/bluetooth/btdev.h>
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
#include "btdevctl.h"
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
#define BTHUB_PATH "/dev/bthub"
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2011-08-28 02:22:01 +04:00
|
|
|
__dead static void usage(void);
|
|
|
|
static char *uppercase(const char *);
|
|
|
|
static int bthub_pioctl(unsigned long, prop_dictionary_t);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2006-09-10 19:45:55 +04:00
|
|
|
prop_dictionary_t dev;
|
|
|
|
prop_object_t obj;
|
|
|
|
bdaddr_t laddr, raddr;
|
2007-04-21 10:15:22 +04:00
|
|
|
const char *service, *mode;
|
|
|
|
int ch, query, verbose, attach, detach, set, none;
|
2006-09-10 19:45:55 +04:00
|
|
|
|
|
|
|
bdaddr_copy(&laddr, BDADDR_ANY);
|
|
|
|
bdaddr_copy(&raddr, BDADDR_ANY);
|
|
|
|
service = NULL;
|
2007-04-21 10:15:22 +04:00
|
|
|
mode = NULL;
|
2007-08-17 21:59:15 +04:00
|
|
|
query = false;
|
|
|
|
verbose = false;
|
|
|
|
attach = false;
|
|
|
|
detach = false;
|
|
|
|
set = false;
|
|
|
|
none = false;
|
2006-09-10 19:45:55 +04:00
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
while ((ch = getopt(argc, argv, "Aa:Dd:hm:qs:v")) != -1) {
|
2006-09-10 19:45:55 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'A': /* Attach device */
|
2007-08-17 21:59:15 +04:00
|
|
|
attach = true;
|
2006-09-10 19:45:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a': /* remote address */
|
|
|
|
if (!bt_aton(optarg, &raddr)) {
|
|
|
|
struct hostent *he = NULL;
|
|
|
|
|
|
|
|
if ((he = bt_gethostbyname(optarg)) == NULL)
|
|
|
|
errx(EXIT_FAILURE, "%s: %s",
|
|
|
|
optarg, hstrerror(h_errno));
|
|
|
|
|
|
|
|
bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D': /* Detach device */
|
2007-08-17 21:59:15 +04:00
|
|
|
detach = true;
|
2006-09-10 19:45:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd': /* local device address */
|
|
|
|
if (!bt_devaddr(optarg, &laddr))
|
|
|
|
err(EXIT_FAILURE, "%s", optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
case 'm': /* link mode */
|
|
|
|
if (strcasecmp(optarg, "none") == 0)
|
2007-08-17 21:59:15 +04:00
|
|
|
none = true;
|
2007-04-21 10:15:22 +04:00
|
|
|
else if (strcasecmp(optarg, BTDEVauth) == 0)
|
|
|
|
mode = BTDEVauth;
|
|
|
|
else if (strcasecmp(optarg, BTDEVencrypt) == 0)
|
|
|
|
mode = BTDEVencrypt;
|
|
|
|
else if (strcasecmp(optarg, BTDEVsecure) == 0)
|
|
|
|
mode = BTDEVsecure;
|
|
|
|
else
|
2007-11-19 22:49:58 +03:00
|
|
|
errx(EXIT_FAILURE, "%s: unknown mode", optarg);
|
2007-04-21 10:15:22 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
case 'q':
|
2007-08-17 21:59:15 +04:00
|
|
|
query = true;
|
2006-09-10 19:45:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 's': /* service */
|
|
|
|
service = uppercase(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* verbose */
|
2007-08-17 21:59:15 +04:00
|
|
|
verbose = true;
|
2006-09-10 19:45:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
if (argc > 0
|
2007-08-17 21:59:15 +04:00
|
|
|
|| (attach == true && detach == true)
|
2006-09-10 19:45:55 +04:00
|
|
|
|| bdaddr_any(&laddr)
|
|
|
|
|| bdaddr_any(&raddr)
|
|
|
|
|| service == NULL)
|
2006-07-26 14:30:59 +04:00
|
|
|
usage();
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-08-17 21:59:15 +04:00
|
|
|
if (attach == false && detach == false)
|
|
|
|
verbose = true;
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
dev = db_get(&laddr, &raddr, service);
|
2007-08-17 21:59:15 +04:00
|
|
|
if (dev == NULL || query == true) {
|
|
|
|
if (verbose == true)
|
2006-09-10 19:45:55 +04:00
|
|
|
printf("Performing SDP query for service '%s'..\n", service);
|
|
|
|
|
|
|
|
dev = cfg_query(&laddr, &raddr, service);
|
2007-08-17 21:59:15 +04:00
|
|
|
set = true;
|
2006-07-26 14:30:59 +04:00
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
if (mode != NULL) {
|
|
|
|
obj = prop_string_create_cstring_nocopy(mode);
|
|
|
|
if (obj == NULL || !prop_dictionary_set(dev, BTDEVmode, obj))
|
|
|
|
errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVmode);
|
|
|
|
|
|
|
|
prop_object_release(obj);
|
2007-08-17 21:59:15 +04:00
|
|
|
set = true;
|
2007-04-21 10:15:22 +04:00
|
|
|
}
|
|
|
|
|
2007-08-17 21:59:15 +04:00
|
|
|
if (none == true) {
|
2007-04-21 10:15:22 +04:00
|
|
|
prop_dictionary_remove(dev, BTDEVmode);
|
2007-08-17 21:59:15 +04:00
|
|
|
set = true;
|
2007-04-21 10:15:22 +04:00
|
|
|
}
|
|
|
|
|
2007-08-17 21:59:15 +04:00
|
|
|
if (set == true && !db_set(dev, &laddr, &raddr, service))
|
2007-04-21 10:15:22 +04:00
|
|
|
errx(EXIT_FAILURE, "service store failed");
|
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
/* add binary local-bdaddr */
|
|
|
|
obj = prop_data_create_data(&laddr, sizeof(laddr));
|
|
|
|
if (obj == NULL || !prop_dictionary_set(dev, BTDEVladdr, obj))
|
|
|
|
errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVladdr);
|
|
|
|
|
|
|
|
prop_object_release(obj);
|
|
|
|
|
|
|
|
/* add binary remote-bdaddr */
|
|
|
|
obj = prop_data_create_data(&raddr, sizeof(raddr));
|
|
|
|
if (obj == NULL || !prop_dictionary_set(dev, BTDEVraddr, obj))
|
|
|
|
errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVraddr);
|
|
|
|
|
|
|
|
prop_object_release(obj);
|
|
|
|
|
2006-09-11 22:30:27 +04:00
|
|
|
/* add service name */
|
|
|
|
obj = prop_string_create_cstring(service);
|
|
|
|
if (obj == NULL || !prop_dictionary_set(dev, BTDEVservice, obj))
|
|
|
|
errx(EXIT_FAILURE, "proplib failure (%s)", BTDEVservice);
|
|
|
|
|
|
|
|
prop_object_release(obj);
|
|
|
|
|
2007-08-17 21:59:15 +04:00
|
|
|
if (verbose == true)
|
2006-09-10 19:45:55 +04:00
|
|
|
cfg_print(dev);
|
|
|
|
|
2007-08-17 21:59:15 +04:00
|
|
|
if (attach == true)
|
2006-09-10 19:45:55 +04:00
|
|
|
bthub_pioctl(BTDEV_ATTACH, dev);
|
|
|
|
|
2007-08-17 21:59:15 +04:00
|
|
|
if (detach == true)
|
2006-09-10 19:45:55 +04:00
|
|
|
bthub_pioctl(BTDEV_DETACH, dev);
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
2006-07-26 14:30:59 +04:00
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2011-08-28 02:22:01 +04:00
|
|
|
static void
|
2006-09-10 19:45:55 +04:00
|
|
|
usage(void)
|
2006-07-26 14:30:59 +04:00
|
|
|
{
|
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
fprintf(stderr,
|
2007-04-21 10:15:22 +04:00
|
|
|
"usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
|
2006-09-10 19:45:55 +04:00
|
|
|
"Where:\n"
|
|
|
|
"\t-A attach device\n"
|
|
|
|
"\t-a address remote device address\n"
|
|
|
|
"\t-D detach device\n"
|
|
|
|
"\t-d device local device address\n"
|
2007-04-21 10:15:22 +04:00
|
|
|
"\t-m mode link mode\n"
|
2006-09-10 19:45:55 +04:00
|
|
|
"\t-q force SDP query\n"
|
|
|
|
"\t-s service remote service\n"
|
|
|
|
"\t-v verbose\n"
|
|
|
|
"", getprogname());
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2011-08-28 02:22:01 +04:00
|
|
|
static char *
|
2006-09-10 19:45:55 +04:00
|
|
|
uppercase(const char *arg)
|
|
|
|
{
|
|
|
|
char *str, *ptr;
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
str = strdup(arg);
|
|
|
|
if (str == NULL)
|
|
|
|
err(EXIT_FAILURE, "strdup");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
for (ptr = str ; *ptr ; ptr++)
|
|
|
|
*ptr = (char)toupper((int)*ptr);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
return str;
|
2006-07-26 14:30:59 +04:00
|
|
|
}
|
|
|
|
|
2011-08-28 02:22:01 +04:00
|
|
|
static int
|
2006-09-10 19:45:55 +04:00
|
|
|
bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
|
2006-07-26 14:30:59 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
fd = open(BTHUB_PATH, O_WRONLY, 0);
|
2006-07-26 14:30:59 +04:00
|
|
|
if (fd < 0)
|
2006-09-10 19:45:55 +04:00
|
|
|
err(EXIT_FAILURE, "%s", BTHUB_PATH);
|
2006-07-26 14:30:59 +04:00
|
|
|
|
2006-09-10 19:45:55 +04:00
|
|
|
if (prop_dictionary_send_ioctl(dict, fd, cmd))
|
|
|
|
err(EXIT_FAILURE, "%s", BTHUB_PATH);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2006-07-26 14:30:59 +04:00
|
|
|
close(fd);
|
2006-09-10 19:45:55 +04:00
|
|
|
return EXIT_SUCCESS;
|
2006-06-19 19:44:33 +04:00
|
|
|
}
|