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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
|
/*
BlueZ - Bluetooth protocol stack for Linux
Copyright (C) 2000-2001 Qualcomm Incorporated
Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation;
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM,
OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
USE OR PERFORMANCE OF THIS SOFTWARE.
ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS,
TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED.
*/
/*
* $Id$
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <netinet/in.h>
#include <bluetooth.h>
#include <hci.h>
#include <hci_lib.h>
extern int optind,opterr,optopt;
extern char *optarg;
static int ctl;
static void usage(void);
static int for_each_dev(int flag, int(*func)(int d, long arg), long arg)
{
struct hci_dev_list_req *dl;
struct hci_dev_req *dr;
int i;
dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t));
if (!dl) {
perror("Can't allocate memory");
return -1;
}
dl->dev_num = HCI_MAX_DEV;
dr = dl->dev_req;
if (ioctl(ctl, HCIGETDEVLIST, (void*)dl)) {
perror("Can't get device list");
return -1;
}
if (!dl->dev_num)
return -1;
for (i=0; i < dl->dev_num; i++, dr++) {
if (dr->dev_opt & (1<<flag)) {
if (!func || func(dr->dev_id, arg))
return dr->dev_id;
}
}
return -1;
}
static int other_bdaddr(int dev_id, long arg)
{
struct hci_dev_info di = {dev_id: dev_id};
if (ioctl(ctl, HCIGETDEVINFO, (void*) &di))
return 0;
return bacmp((bdaddr_t *)arg, &di.bdaddr);
}
static int get_route(bdaddr_t *bdaddr)
{
if (bdaddr)
return for_each_dev(HCI_UP, other_bdaddr, (long) bdaddr);
else
return for_each_dev(HCI_UP, NULL, 0);
}
static int dev_info(int dev_id, long arg)
{
struct hci_dev_info di = {dev_id: dev_id};
bdaddr_t bdaddr;
if (ioctl(ctl, HCIGETDEVINFO, (void*) &di))
return 0;
baswap(&bdaddr, &di.bdaddr);
printf("\t%s\t%s\n", di.name, batostr(&bdaddr));
return 0;
}
static int rev_info(int dev_id, long arg)
{
struct hci_version ver;
int dd;
struct hci_request rq;
unsigned char buf[102];
dd = hci_open_dev(dev_id);
if (dd < 0) {
printf("Can't open device hci%d. %s(%d)\n", dev_id, strerror(errno), errno);
return -1;
}
if (hci_read_local_version(dd, &ver, 1000) < 0) {
printf("Can't read version info hci%d. %s(%d)\n",
dev_id, strerror(errno), errno);
return -1;
}
printf("hci%d:", dev_id);
switch (ver.manufacturer) {
case 0:
memset(&rq, 0, sizeof(rq));
rq.ogf = 0x3f;
rq.ocf = 0x000f;
rq.cparam = NULL;
rq.clen = 0;
rq.rparam = &buf;
rq.rlen = sizeof(buf);
if (hci_send_req(dd, &rq, 1000) < 0) {
printf("\n Can't read revision info. %s(%d)\n",
strerror(errno), errno);
return -1;
}
printf("%s\n", buf + 1);
break;
default:
printf("\n Manufacturer not supported\n");
break;
}
printf("\n");
return 0;
}
static int conn_list(int dev_id, long arg)
{
struct hci_conn_list_req *cl;
struct hci_conn_info *ci;
int i;
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
perror("Can't allocate memory");
exit(1);
}
cl->dev_id = dev_id;
cl->conn_num = 10;
ci = cl->conn_info;
if (ioctl(ctl, HCIGETCONNLIST, (void*)cl)) {
perror("Can't get connection list");
exit(1);
}
for (i=0; i < cl->conn_num; i++, ci++) {
bdaddr_t bdaddr;
baswap(&bdaddr, &ci->bdaddr);
printf("\t%s %s %s handle %d state %d lm %s\n",
ci->out ? "<" : ">",
ci->type == ACL_LINK ? "ACL" : "SCO",
batostr(&bdaddr), ci->handle, ci->state,
hci_lmtostr(ci->link_mode));
}
return 0;
}
static int find_conn(int dev_id, long arg)
{
struct hci_conn_list_req *cl;
struct hci_conn_info *ci;
int i;
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
perror("Can't allocate memory");
exit(1);
}
cl->dev_id = dev_id;
cl->conn_num = 10;
ci = cl->conn_info;
if (ioctl(ctl, HCIGETCONNLIST, (void*)cl)) {
perror("Can't get connection list");
exit(1);
}
for (i=0; i < cl->conn_num; i++, ci++)
if (!bacmp((bdaddr_t *)arg, &ci->bdaddr))
return 1;
return 0;
}
static void hex_dump(char *pref, int width, unsigned char *buf, int len)
{
register int i,n;
for (i=0, n=1; i<len; i++, n++) {
if (n == 1)
printf("%s", pref);
printf("%2.2X ", buf[i]);
if (n == width) {
printf("\n");
n = 0;
}
}
if (i && n!=1)
printf("\n");
}
static void cmd_dev(int dev_id, char **opt, int nopt)
{
printf("Devices:\n");
for_each_dev(HCI_UP, dev_info, 0);
}
static void cmd_inq(int dev_id, char **opt, int nopt)
{
inquiry_info *info;
int i, num_rsp = 0, length, flags;
bdaddr_t bdaddr;
if (dev_id < 0)
dev_id = get_route(NULL);
if (nopt >= 1)
length = atoi(opt[0]);
else
length = 8; /* ~ 10 seconds */
flags = 0;
if (nopt >= 2)
flags |= !strncasecmp("f", opt[1], 1) ? IREQ_CACHE_FLUSH : 0;
printf("Inquiring ...\n");
info = hci_inquiry(dev_id, length, &num_rsp, NULL, flags);
if (!info) {
perror("Inquiry failed.");
exit(1);
}
for (i = 0; i < num_rsp; i++) {
baswap(&bdaddr, &(info+i)->bdaddr);
printf("\t%s\tclock offset: 0x%4.4x\tclass: 0x%2.2x%2.2x%2.2x\n",
batostr(&bdaddr), (info+i)->clock_offset,
(info+i)->dev_class[2],
(info+i)->dev_class[1],
(info+i)->dev_class[0]);
}
free(info);
}
static void cmd_scan(int dev_id, char **opt, int nopt)
{
inquiry_info *info;
int i, num_rsp = 0, length, flags;
bdaddr_t bdaddr;
char name[248];
int dd;
if (dev_id < 0)
dev_id = get_route(NULL);
if (nopt >= 1)
length = atoi(opt[0]);
else
length = 8; /* ~ 10 seconds */
flags = 0;
if (nopt >= 2)
flags |= !strncasecmp("f", opt[1], 1) ? IREQ_CACHE_FLUSH : 0;
printf("Scanning ...\n");
info = hci_inquiry(dev_id, length, &num_rsp, NULL, flags);
if (!info) {
perror("Inquiry failed.");
exit(1);
}
for (i = 0; i < num_rsp; i++) {
dd = hci_open_dev(dev_id);
memset(name, 0, sizeof(name));
if (hci_remote_name(dd, &(info+i)->bdaddr, sizeof(name), name, 100000) < 0)
strcpy(name, "n/a");
close(dd);
baswap(&bdaddr, &(info+i)->bdaddr);
printf("\t%s\t%s\n", batostr(&bdaddr), name);
}
free(info);
}
static void cmd_info(int dev_id, char **opt, int nopt)
{
bdaddr_t bdaddr;
uint16_t handle;
int dd;
char name[248];
unsigned char features[8];
struct hci_version version;
if (nopt < 1)
return;
baswap(&bdaddr, strtoba(opt[0]));
if (dev_id < 0) {
dev_id = get_route(&bdaddr);
if (dev_id < 0) {
fprintf(stderr, "Device is not available.\n");
exit(1);
}
}
printf("Requesting information ...\n");
dd = hci_open_dev(dev_id);
if (dd < 0) {
perror("HCI device open failed");
exit(1);
}
printf("\tBD Address: %s\n", opt[0]);
if (hci_create_connection(dd, &bdaddr, 0x0008 | 0x0010, 0, 0, &handle, 25000) < 0) {
close(dd);
exit(1);
}
if (hci_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
printf("\tDevice Name: %s\n", name);
if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
"\tManufacturer: %s (%d)\n",
lmp_vertostr(version.lmp_ver), version.lmp_ver, version.lmp_subver,
bt_compidtostr(version.manufacturer), version.manufacturer);
}
if (hci_read_remote_features(dd, handle, features, 20000) == 0) {
printf("\tFeatures:\n%s\n",
lmp_featurestostr(features, "\t\t", 3));
}
hci_disconnect(dd, handle, 0x13, 10000);
close(dd);
}
static void cmd_cmd(int dev_id, char **opt, int nopt)
{
char buf[HCI_MAX_EVENT_SIZE], *ptr = buf;
struct hci_filter flt;
hci_event_hdr *hdr;
int i, len, dd;
uint16_t ocf;
uint8_t ogf;
if (nopt < 2) {
usage();
return;
}
if (dev_id < 0)
dev_id = get_route(NULL);
errno = 0;
ogf = strtol(opt[0], NULL, 16);
ocf = strtol(opt[1], NULL, 16);
if (errno == ERANGE || (ogf > 0x3f) || (ocf > 0x3ff)) {
usage();
return;
}
for (i = 2, len = 0; i < nopt && len < sizeof(buf); i++, len++)
*ptr++ = (uint8_t) strtol(opt[i], NULL, 16);
dd = hci_open_dev(dev_id);
if (dd < 0) {
perror("Device open failed");
exit(EXIT_FAILURE);
}
/* Setup filter */
hci_filter_clear(&flt);
hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
hci_filter_all_events(&flt);
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
perror("HCI filter setup failed");
exit(EXIT_FAILURE);
}
printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);
hex_dump(" ", 20, buf, len); fflush(stdout);
if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
perror("Send failed");
exit(EXIT_FAILURE);
}
len = read(dd, buf, sizeof(buf));
if (len < 0) {
perror("Read failed");
exit(EXIT_FAILURE);
}
hdr = (void *)(buf + 1);
ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
len -= (1 + HCI_EVENT_HDR_SIZE);
printf("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);
hex_dump(" ", 20, ptr, len); fflush(stdout);
hci_close_dev(dd);
return;
}
static void cmd_rev(int dev_id, char **opt, int nopt)
{
if (dev_id < 0)
for_each_dev(HCI_UP, rev_info, 0);
else
rev_info(dev_id, 0);
}
static void cmd_con(int dev_id, char **opt, int nopt)
{
printf("Connections:\n");
if (dev_id < 0)
for_each_dev(HCI_UP, conn_list, 0);
else
conn_list(dev_id, 0);
}
static void cmd_cc(int dev_id, char **opt, int nopt)
{
bdaddr_t bdaddr;
int ptype, dd;
uint16_t handle;
uint8_t role;
if (nopt < 1)
return;
baswap(&bdaddr, strtoba(opt[0]));
if (dev_id < 0) {
dev_id = get_route(&bdaddr);
if (dev_id < 0) {
fprintf(stderr, "Device is not available.\n");
exit(1);
}
}
dd = hci_open_dev(dev_id);
if (dd < 0) {
perror("HCI device open failed");
exit(1);
}
if (nopt >= 2)
hci_strtoptype(opt[1], &ptype);
else
ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;
if (nopt >= 3)
role = !strncasecmp("m", opt[2], 1) ? 0 : 1;
else
role = 0;
hci_create_connection(dd, &bdaddr, ptype, 0, role, &handle, 1000);
hci_close_dev(dd);
}
static void cmd_dc(int dev_id, char **opt, int nopt)
{
struct hci_conn_info_req *cr;
bdaddr_t bdaddr;
int dd;
if (nopt < 1)
return;
baswap(&bdaddr, strtoba(*opt));
if (dev_id < 0) {
dev_id = for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
if (dev_id < 0) {
fprintf(stderr, "Not connected.\n");
exit(1);
}
}
dd = hci_open_dev(dev_id);
if (dd < 0) {
perror("HCI device open failed");
exit(1);
}
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
if (!cr)
return;
bacpy(&cr->bdaddr, &bdaddr);
cr->type = ACL_LINK;
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
perror("Get connection info failed");
exit(1);
}
hci_disconnect(dd, cr->conn_info->handle, 0x13, 100);
close(dd);
free(cr);
}
struct {
char *cmd;
void (*func)(int dev_id, char **opt, int nopt);
char *opt;
char *doc;
} command[] = {
{ "dev", cmd_dev, 0, "Display local devices" },
{ "rev", cmd_rev, 0, "Display revison information" },
{ "inq", cmd_inq, "[length] [flush]", "Inquire remote devices" },
{ "scan", cmd_scan, "[length] [flush]", "Scan for remote devices" },
{ "info", cmd_info, "<bdaddr>", "Get information from remote device" },
{ "cmd", cmd_cmd, "<ogf> <ocf> [param]", "Submit arbitrary HCI commands" },
{ "con", cmd_con, 0, "Display active connections" },
{ "cc", cmd_cc, "<bdaddr> [pkt type] [role]", "Create connection to remote device" },
{ "dc", cmd_dc, "<bdaddr>", "Disconnect from remote device" },
{ NULL, NULL, 0}
};
static void usage(void)
{
int i;
printf("hcitool - HCI Tool\n");
printf("Usage:\n"
"\thcitool [-i hciX] [command]\n");
printf("Commands:\n");
for (i=0; command[i].cmd; i++)
printf("\t%-4s %-20s\t%s\n", command[i].cmd,
command[i].opt ? command[i].opt : " ",
command[i].doc);
}
int main(int argc, char *argv[], char *env[])
{
int opt, i, dev_id = -1;
char *dev;
while ((opt=getopt(argc, argv, "i:h")) != EOF) {
switch(opt) {
case 'i':
dev = strdup(optarg);
dev_id = atoi(dev + 3);
break;
case 'h':
default:
usage();
exit(0);
}
}
if (argc - optind < 1) {
usage();
exit(0);
}
/* Open HCI socket */
if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
perror("Can't open HCI socket.");
exit(1);
}
for (i=0; command[i].cmd; i++) {
if (strncmp(command[i].cmd, argv[optind], 3))
continue;
optind++;
command[i].func(dev_id, argv + optind, argc - optind);
break;
}
close(ctl);
return 0;
}
|