summaryrefslogtreecommitdiff
path: root/usr.sbin/btdevctl/btdevctl.c
blob: 97ba534e2a4b73014fcc4421619a9214cee91ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*	$NetBSD: btdevctl.c,v 1.10 2011/08/27 22:24:14 joerg Exp $	*/

/*-
 * 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>
__COPYRIGHT("@(#) Copyright (c) 2006 The NetBSD Foundation, Inc.\
  @(#) Copyright (c) 2006 Itronix, Inc.\
  All rights reserved.");
__RCSID("$NetBSD: btdevctl.c,v 1.10 2011/08/27 22:24:14 joerg Exp $");

#include <prop/proplib.h>
#include <sys/ioctl.h>

#include <bluetooth.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <dev/bluetooth/btdev.h>

#include "btdevctl.h"

#define BTHUB_PATH		"/dev/bthub"

__dead static void usage(void);
static char *uppercase(const char *);
static int bthub_pioctl(unsigned long, prop_dictionary_t);

int
main(int argc, char *argv[])
{
	prop_dictionary_t dev;
	prop_object_t obj;
	bdaddr_t laddr, raddr;
	const char *service, *mode;
	int ch, query, verbose, attach, detach, set, none;

	bdaddr_copy(&laddr, BDADDR_ANY);
	bdaddr_copy(&raddr, BDADDR_ANY);
	service = NULL;
	mode = NULL;
	query = false;
	verbose = false;
	attach = false;
	detach = false;
	set = false;
	none = false;

	while ((ch = getopt(argc, argv, "Aa:Dd:hm:qs:v")) != -1) {
		switch (ch) {
		case 'A': /* Attach device */
			attach = true;
			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 */
			detach = true;
			break;

		case 'd': /* local device address */
			if (!bt_devaddr(optarg, &laddr))
				err(EXIT_FAILURE, "%s", optarg);

			break;

		case 'm': /* link mode */
			if (strcasecmp(optarg, "none") == 0)
				none = true;
			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
				errx(EXIT_FAILURE, "%s: unknown mode", optarg);

			break;

		case 'q':
			query = true;
			break;

		case 's': /* service */
			service = uppercase(optarg);
			break;

		case 'v': /* verbose */
			verbose = true;
			break;

		case 'h':
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;

	if (argc > 0
	    || (attach == true && detach == true)
	    || bdaddr_any(&laddr)
	    || bdaddr_any(&raddr)
	    || service == NULL)
		usage();

	if (attach == false && detach == false)
		verbose = true;

	dev = db_get(&laddr, &raddr, service);
	if (dev == NULL || query == true) {
		if (verbose == true)
			printf("Performing SDP query for service '%s'..\n", service);

		dev = cfg_query(&laddr, &raddr, service);
		set = true;
	}

	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);
		set = true;
	}

	if (none == true) {
		prop_dictionary_remove(dev, BTDEVmode);
		set = true;
	}

	if (set == true && !db_set(dev, &laddr, &raddr, service))
		errx(EXIT_FAILURE, "service store failed");

	/* 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);

	/* 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);

	if (verbose == true)
		cfg_print(dev);

	if (attach == true)
		bthub_pioctl(BTDEV_ATTACH, dev);

	if (detach == true)
		bthub_pioctl(BTDEV_DETACH, dev);

	exit(EXIT_SUCCESS);
}

static void
usage(void)
{

	fprintf(stderr,
		"usage: %s [-A | -D] [-qv] [-m mode] -a address -d device -s service\n"
		"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"
		"\t-m mode      link mode\n"
		"\t-q           force SDP query\n"
		"\t-s service   remote service\n"
		"\t-v           verbose\n"
		"", getprogname());

	exit(EXIT_FAILURE);
}

static char *
uppercase(const char *arg)
{
	char *str, *ptr;

	str = strdup(arg);
	if (str == NULL)
		err(EXIT_FAILURE, "strdup");

	for (ptr = str ; *ptr ; ptr++)
		*ptr = (char)toupper((int)*ptr);

	return str;
}

static int
bthub_pioctl(unsigned long cmd, prop_dictionary_t dict)
{
	int fd;

	fd = open(BTHUB_PATH, O_WRONLY, 0);
	if (fd < 0)
		err(EXIT_FAILURE, "%s", BTHUB_PATH);

	if (prop_dictionary_send_ioctl(dict, fd, cmd))
		err(EXIT_FAILURE, "%s", BTHUB_PATH);

	close(fd);
	return EXIT_SUCCESS;
}