1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2000-2001 Qualcomm Incorporated
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2002-2009 Marcel Holtmann <marcel@holtmann.org>
8 *
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <errno.h>
32 #include <ctype.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <getopt.h>
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41
42 #include <bluetooth/bluetooth.h>
43 #include <bluetooth/hci.h>
44 #include <bluetooth/hci_lib.h>
45
46 #include "textfile.h"
47 #include "oui.h"
48
49 #define for_each_opt(opt, long, short) while ((opt=getopt_long(argc, argv, short ? short:"+", long, NULL)) != -1)
50
51 static void usage(void);
52
dev_info(int s,int dev_id,long arg)53 static int dev_info(int s, int dev_id, long arg)
54 {
55 struct hci_dev_info di = { dev_id: dev_id };
56 char addr[18];
57
58 if (ioctl(s, HCIGETDEVINFO, (void *) &di))
59 return 0;
60
61 ba2str(&di.bdaddr, addr);
62 printf("\t%s\t%s\n", di.name, addr);
63 return 0;
64 }
65
type2str(uint8_t type)66 static char *type2str(uint8_t type)
67 {
68 switch (type) {
69 case SCO_LINK:
70 return "SCO";
71 case ACL_LINK:
72 return "ACL";
73 case ESCO_LINK:
74 return "eSCO";
75 default:
76 return "Unknown";
77 }
78 }
79
conn_list(int s,int dev_id,long arg)80 static int conn_list(int s, int dev_id, long arg)
81 {
82 struct hci_conn_list_req *cl;
83 struct hci_conn_info *ci;
84 int id = arg;
85 int i;
86
87 if (id != -1 && dev_id != id)
88 return 0;
89
90 if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
91 perror("Can't allocate memory");
92 exit(1);
93 }
94 cl->dev_id = dev_id;
95 cl->conn_num = 10;
96 ci = cl->conn_info;
97
98 if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
99 perror("Can't get connection list");
100 exit(1);
101 }
102
103 for (i = 0; i < cl->conn_num; i++, ci++) {
104 char addr[18];
105 char *str;
106 ba2str(&ci->bdaddr, addr);
107 str = hci_lmtostr(ci->link_mode);
108 printf("\t%s %s %s handle %d state %d lm %s\n",
109 ci->out ? "<" : ">", type2str(ci->type),
110 addr, ci->handle, ci->state, str);
111 bt_free(str);
112 }
113
114 return 0;
115 }
116
find_conn(int s,int dev_id,long arg)117 static int find_conn(int s, int dev_id, long arg)
118 {
119 struct hci_conn_list_req *cl;
120 struct hci_conn_info *ci;
121 int i;
122
123 if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
124 perror("Can't allocate memory");
125 exit(1);
126 }
127 cl->dev_id = dev_id;
128 cl->conn_num = 10;
129 ci = cl->conn_info;
130
131 if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
132 perror("Can't get connection list");
133 exit(1);
134 }
135
136 for (i = 0; i < cl->conn_num; i++, ci++)
137 if (!bacmp((bdaddr_t *) arg, &ci->bdaddr))
138 return 1;
139
140 return 0;
141 }
142
hex_dump(char * pref,int width,unsigned char * buf,int len)143 static void hex_dump(char *pref, int width, unsigned char *buf, int len)
144 {
145 register int i,n;
146
147 for (i = 0, n = 1; i < len; i++, n++) {
148 if (n == 1)
149 printf("%s", pref);
150 printf("%2.2X ", buf[i]);
151 if (n == width) {
152 printf("\n");
153 n = 0;
154 }
155 }
156 if (i && n!=1)
157 printf("\n");
158 }
159
get_minor_device_name(int major,int minor)160 static char *get_minor_device_name(int major, int minor)
161 {
162 switch (major) {
163 case 0: /* misc */
164 return "";
165 case 1: /* computer */
166 switch(minor) {
167 case 0:
168 return "Uncategorized";
169 case 1:
170 return "Desktop workstation";
171 case 2:
172 return "Server";
173 case 3:
174 return "Laptop";
175 case 4:
176 return "Handheld";
177 case 5:
178 return "Palm";
179 case 6:
180 return "Wearable";
181 }
182 break;
183 case 2: /* phone */
184 switch(minor) {
185 case 0:
186 return "Uncategorized";
187 case 1:
188 return "Cellular";
189 case 2:
190 return "Cordless";
191 case 3:
192 return "Smart phone";
193 case 4:
194 return "Wired modem or voice gateway";
195 case 5:
196 return "Common ISDN Access";
197 case 6:
198 return "Sim Card Reader";
199 }
200 break;
201 case 3: /* lan access */
202 if (minor == 0)
203 return "Uncategorized";
204 switch(minor / 8) {
205 case 0:
206 return "Fully available";
207 case 1:
208 return "1-17% utilized";
209 case 2:
210 return "17-33% utilized";
211 case 3:
212 return "33-50% utilized";
213 case 4:
214 return "50-67% utilized";
215 case 5:
216 return "67-83% utilized";
217 case 6:
218 return "83-99% utilized";
219 case 7:
220 return "No service available";
221 }
222 break;
223 case 4: /* audio/video */
224 switch(minor) {
225 case 0:
226 return "Uncategorized";
227 case 1:
228 return "Device conforms to the Headset profile";
229 case 2:
230 return "Hands-free";
231 /* 3 is reserved */
232 case 4:
233 return "Microphone";
234 case 5:
235 return "Loudspeaker";
236 case 6:
237 return "Headphones";
238 case 7:
239 return "Portable Audio";
240 case 8:
241 return "Car Audio";
242 case 9:
243 return "Set-top box";
244 case 10:
245 return "HiFi Audio Device";
246 case 11:
247 return "VCR";
248 case 12:
249 return "Video Camera";
250 case 13:
251 return "Camcorder";
252 case 14:
253 return "Video Monitor";
254 case 15:
255 return "Video Display and Loudspeaker";
256 case 16:
257 return "Video Conferencing";
258 /* 17 is reserved */
259 case 18:
260 return "Gaming/Toy";
261 }
262 break;
263 case 5: /* peripheral */ {
264 static char cls_str[48]; cls_str[0] = 0;
265
266 switch(minor & 48) {
267 case 16:
268 strncpy(cls_str, "Keyboard", sizeof(cls_str));
269 break;
270 case 32:
271 strncpy(cls_str, "Pointing device", sizeof(cls_str));
272 break;
273 case 48:
274 strncpy(cls_str, "Combo keyboard/pointing device", sizeof(cls_str));
275 break;
276 }
277 if((minor & 15) && (strlen(cls_str) > 0))
278 strcat(cls_str, "/");
279
280 switch(minor & 15) {
281 case 0:
282 break;
283 case 1:
284 strncat(cls_str, "Joystick", sizeof(cls_str) - strlen(cls_str));
285 break;
286 case 2:
287 strncat(cls_str, "Gamepad", sizeof(cls_str) - strlen(cls_str));
288 break;
289 case 3:
290 strncat(cls_str, "Remote control", sizeof(cls_str) - strlen(cls_str));
291 break;
292 case 4:
293 strncat(cls_str, "Sensing device", sizeof(cls_str) - strlen(cls_str));
294 break;
295 case 5:
296 strncat(cls_str, "Digitizer tablet", sizeof(cls_str) - strlen(cls_str));
297 break;
298 case 6:
299 strncat(cls_str, "Card reader", sizeof(cls_str) - strlen(cls_str));
300 break;
301 default:
302 strncat(cls_str, "(reserved)", sizeof(cls_str) - strlen(cls_str));
303 break;
304 }
305 if(strlen(cls_str) > 0)
306 return cls_str;
307 }
308 case 6: /* imaging */
309 if (minor & 4)
310 return "Display";
311 if (minor & 8)
312 return "Camera";
313 if (minor & 16)
314 return "Scanner";
315 if (minor & 32)
316 return "Printer";
317 break;
318 case 7: /* wearable */
319 switch(minor) {
320 case 1:
321 return "Wrist Watch";
322 case 2:
323 return "Pager";
324 case 3:
325 return "Jacket";
326 case 4:
327 return "Helmet";
328 case 5:
329 return "Glasses";
330 }
331 break;
332 case 8: /* toy */
333 switch(minor) {
334 case 1:
335 return "Robot";
336 case 2:
337 return "Vehicle";
338 case 3:
339 return "Doll / Action Figure";
340 case 4:
341 return "Controller";
342 case 5:
343 return "Game";
344 }
345 break;
346 case 63: /* uncategorised */
347 return "";
348 }
349 return "Unknown (reserved) minor device class";
350 }
351
352 static char *major_classes[] = {
353 "Miscellaneous", "Computer", "Phone", "LAN Access",
354 "Audio/Video", "Peripheral", "Imaging", "Uncategorized"
355 };
356
get_device_name(const bdaddr_t * local,const bdaddr_t * peer)357 static char *get_device_name(const bdaddr_t *local, const bdaddr_t *peer)
358 {
359 char filename[PATH_MAX + 1], addr[18];
360
361 ba2str(local, addr);
362 create_name(filename, PATH_MAX, STORAGEDIR, addr, "names");
363
364 ba2str(peer, addr);
365 return textfile_get(filename, addr);
366 }
367
368 /* Display local devices */
369
370 static struct option dev_options[] = {
371 { "help", 0, 0, 'h' },
372 {0, 0, 0, 0 }
373 };
374
375 static const char *dev_help =
376 "Usage:\n"
377 "\tdev\n";
378
cmd_dev(int dev_id,int argc,char ** argv)379 static void cmd_dev(int dev_id, int argc, char **argv)
380 {
381 int opt;
382
383 for_each_opt(opt, dev_options, NULL) {
384 switch (opt) {
385 default:
386 printf("%s", dev_help);
387 return;
388 }
389 }
390
391 printf("Devices:\n");
392
393 hci_for_each_dev(HCI_UP, dev_info, 0);
394 }
395
396 /* Inquiry */
397
398 static struct option inq_options[] = {
399 { "help", 0, 0, 'h' },
400 { "length", 1, 0, 'l' },
401 { "numrsp", 1, 0, 'n' },
402 { "iac", 1, 0, 'i' },
403 { "flush", 0, 0, 'f' },
404 { 0, 0, 0, 0 }
405 };
406
407 static const char *inq_help =
408 "Usage:\n"
409 "\tinq [--length=N] maximum inquiry duration in 1.28 s units\n"
410 "\t [--numrsp=N] specify maximum number of inquiry responses\n"
411 "\t [--iac=lap] specify the inquiry access code\n"
412 "\t [--flush] flush the inquiry cache\n";
413
cmd_inq(int dev_id,int argc,char ** argv)414 static void cmd_inq(int dev_id, int argc, char **argv)
415 {
416 inquiry_info *info = NULL;
417 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
418 int num_rsp, length, flags;
419 char addr[18];
420 int i, l, opt;
421
422 length = 8; /* ~10 seconds */
423 num_rsp = 0;
424 flags = 0;
425
426 for_each_opt(opt, inq_options, NULL) {
427 switch (opt) {
428 case 'l':
429 length = atoi(optarg);
430 break;
431
432 case 'n':
433 num_rsp = atoi(optarg);
434 break;
435
436 case 'i':
437 l = strtoul(optarg, 0, 16);
438 if (!strcasecmp(optarg, "giac")) {
439 l = 0x9e8b33;
440 } else if (!strcasecmp(optarg, "liac")) {
441 l = 0x9e8b00;
442 } if (l < 0x9e8b00 || l > 0x9e8b3f) {
443 printf("Invalid access code 0x%x\n", l);
444 exit(1);
445 }
446 lap[0] = (l & 0xff);
447 lap[1] = (l >> 8) & 0xff;
448 lap[2] = (l >> 16) & 0xff;
449 break;
450
451 case 'f':
452 flags |= IREQ_CACHE_FLUSH;
453 break;
454
455 default:
456 printf("%s", inq_help);
457 return;
458 }
459 }
460
461 printf("Inquiring ...\n");
462
463 num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
464 if (num_rsp < 0) {
465 perror("Inquiry failed.");
466 exit(1);
467 }
468
469 for (i = 0; i < num_rsp; i++) {
470 ba2str(&(info+i)->bdaddr, addr);
471 printf("\t%s\tclock offset: 0x%4.4x\tclass: 0x%2.2x%2.2x%2.2x\n",
472 addr, btohs((info+i)->clock_offset),
473 (info+i)->dev_class[2],
474 (info+i)->dev_class[1],
475 (info+i)->dev_class[0]);
476 }
477
478 bt_free(info);
479 }
480
481 /* Device scanning */
482
483 static struct option scan_options[] = {
484 { "help", 0, 0, 'h' },
485 { "length", 1, 0, 'l' },
486 { "numrsp", 1, 0, 'n' },
487 { "iac", 1, 0, 'i' },
488 { "flush", 0, 0, 'f' },
489 { "refresh", 0, 0, 'r' },
490 { "class", 0, 0, 'C' },
491 { "info", 0, 0, 'I' },
492 { "oui", 0, 0, 'O' },
493 { "all", 0, 0, 'A' },
494 { "ext", 0, 0, 'A' },
495 { 0, 0, 0, 0 }
496 };
497
498 static const char *scan_help =
499 "Usage:\n"
500 "\tscan [--length=N] [--numrsp=N] [--iac=lap] [--flush] [--class] [--info] [--oui] [--refresh]\n";
501
cmd_scan(int dev_id,int argc,char ** argv)502 static void cmd_scan(int dev_id, int argc, char **argv)
503 {
504 inquiry_info *info = NULL;
505 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
506 int num_rsp, length, flags;
507 uint8_t cls[3], features[8];
508 uint16_t handle;
509 char addr[18], name[249], oui[9], *comp, *tmp;
510 struct hci_version version;
511 struct hci_dev_info di;
512 struct hci_conn_info_req *cr;
513 int refresh = 0, extcls = 0, extinf = 0, extoui = 0;
514 int i, n, l, opt, dd, cc, nc;
515
516 length = 8; /* ~10 seconds */
517 num_rsp = 0;
518 flags = 0;
519
520 for_each_opt(opt, scan_options, NULL) {
521 switch (opt) {
522 case 'l':
523 length = atoi(optarg);
524 break;
525
526 case 'n':
527 num_rsp = atoi(optarg);
528 break;
529
530 case 'i':
531 l = strtoul(optarg, 0, 16);
532 if (!strcasecmp(optarg, "giac")) {
533 l = 0x9e8b33;
534 } else if (!strcasecmp(optarg, "liac")) {
535 l = 0x9e8b00;
536 } else if (l < 0x9e8b00 || l > 0x9e8b3f) {
537 printf("Invalid access code 0x%x\n", l);
538 exit(1);
539 }
540 lap[0] = (l & 0xff);
541 lap[1] = (l >> 8) & 0xff;
542 lap[2] = (l >> 16) & 0xff;
543 break;
544
545 case 'f':
546 flags |= IREQ_CACHE_FLUSH;
547 break;
548
549 case 'r':
550 refresh = 1;
551 break;
552
553 case 'C':
554 extcls = 1;
555 break;
556
557 case 'I':
558 extinf = 1;
559 break;
560
561 case 'O':
562 extoui = 1;
563 break;
564
565 case 'A':
566 extcls = 1;
567 extinf = 1;
568 extoui = 1;
569 break;
570
571 default:
572 printf("%s", scan_help);
573 return;
574 }
575 }
576
577 if (dev_id < 0) {
578 dev_id = hci_get_route(NULL);
579 if (dev_id < 0) {
580 perror("Device is not available");
581 exit(1);
582 }
583 }
584
585 if (hci_devinfo(dev_id, &di) < 0) {
586 perror("Can't get device info");
587 exit(1);
588 }
589
590 printf("Scanning ...\n");
591 num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
592 if (num_rsp < 0) {
593 perror("Inquiry failed");
594 exit(1);
595 }
596
597 dd = hci_open_dev(dev_id);
598 if (dd < 0) {
599 perror("HCI device open failed");
600 free(info);
601 exit(1);
602 }
603
604 if (extcls || extinf || extoui)
605 printf("\n");
606
607 for (i = 0; i < num_rsp; i++) {
608 if (!refresh) {
609 memset(name, 0, sizeof(name));
610 tmp = get_device_name(&di.bdaddr, &(info+i)->bdaddr);
611 if (tmp) {
612 strncpy(name, tmp, 249);
613 free(tmp);
614 nc = 1;
615 } else
616 nc = 0;
617 } else
618 nc = 0;
619
620 if (!extcls && !extinf && !extoui) {
621 ba2str(&(info+i)->bdaddr, addr);
622
623 if (nc) {
624 printf("\t%s\t%s\n", addr, name);
625 continue;
626 }
627
628 if (hci_read_remote_name_with_clock_offset(dd,
629 &(info+i)->bdaddr,
630 (info+i)->pscan_rep_mode,
631 (info+i)->clock_offset | 0x8000,
632 sizeof(name), name, 100000) < 0)
633 strcpy(name, "n/a");
634
635 for (n = 0; n < 248 && name[n]; n++) {
636 if ((unsigned char) name[i] < 32 || name[i] == 127)
637 name[i] = '.';
638 }
639
640 name[248] = '\0';
641
642 printf("\t%s\t%s\n", addr, name);
643 continue;
644 }
645
646 ba2str(&(info+i)->bdaddr, addr);
647 printf("BD Address:\t%s [mode %d, clkoffset 0x%4.4x]\n", addr,
648 (info+i)->pscan_rep_mode, btohs((info+i)->clock_offset));
649
650 if (extoui) {
651 ba2oui(&(info+i)->bdaddr, oui);
652 comp = ouitocomp(oui);
653 if (comp) {
654 printf("OUI company:\t%s (%s)\n", comp, oui);
655 free(comp);
656 }
657 }
658
659 cc = 0;
660
661 if (extinf) {
662 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
663 if (cr) {
664 bacpy(&cr->bdaddr, &(info+i)->bdaddr);
665 cr->type = ACL_LINK;
666 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
667 handle = 0;
668 cc = 1;
669 } else {
670 handle = htobs(cr->conn_info->handle);
671 cc = 0;
672 }
673 free(cr);
674 }
675
676 if (cc) {
677 if (hci_create_connection(dd, &(info+i)->bdaddr,
678 htobs(di.pkt_type & ACL_PTYPE_MASK),
679 (info+i)->clock_offset | 0x8000,
680 0x01, &handle, 25000) < 0) {
681 handle = 0;
682 cc = 0;
683 }
684 }
685 }
686
687 if (handle > 0 || !nc) {
688 if (hci_read_remote_name_with_clock_offset(dd,
689 &(info+i)->bdaddr,
690 (info+i)->pscan_rep_mode,
691 (info+i)->clock_offset | 0x8000,
692 sizeof(name), name, 100000) < 0) {
693 if (!nc)
694 strcpy(name, "n/a");
695 } else {
696 for (n = 0; n < 248 && name[n]; n++) {
697 if ((unsigned char) name[i] < 32 || name[i] == 127)
698 name[i] = '.';
699 }
700
701 name[248] = '\0';
702 nc = 0;
703 }
704 }
705
706 if (strlen(name) > 0)
707 printf("Device name:\t%s%s\n", name, nc ? " [cached]" : "");
708
709 if (extcls) {
710 memcpy(cls, (info+i)->dev_class, 3);
711 printf("Device class:\t");
712 if ((cls[1] & 0x1f) > sizeof(major_classes) / sizeof(char *))
713 printf("Invalid");
714 else
715 printf("%s, %s", major_classes[cls[1] & 0x1f],
716 get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));
717 printf(" (0x%2.2x%2.2x%2.2x)\n", cls[2], cls[1], cls[0]);
718 }
719
720 if (extinf && handle > 0) {
721 if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
722 char *ver = lmp_vertostr(version.lmp_ver);
723 printf("Manufacturer:\t%s (%d)\n",
724 bt_compidtostr(version.manufacturer),
725 version.manufacturer);
726 printf("LMP version:\t%s (0x%x) [subver 0x%x]\n",
727 ver ? ver : "n/a",
728 version.lmp_ver, version.lmp_subver);
729 if (ver)
730 bt_free(ver);
731 }
732
733 if (hci_read_remote_features(dd, handle, features, 20000) == 0) {
734 char *tmp = lmp_featurestostr(features, "\t\t", 63);
735 printf("LMP features:\t0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x"
736 " 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
737 features[0], features[1],
738 features[2], features[3],
739 features[4], features[5],
740 features[6], features[7]);
741 printf("%s\n", tmp);
742 bt_free(tmp);
743 }
744
745 if (cc) {
746 usleep(10000);
747 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
748 }
749 }
750
751 printf("\n");
752 }
753
754 bt_free(info);
755
756 hci_close_dev(dd);
757 }
758
759 /* Remote name */
760
761 static struct option name_options[] = {
762 { "help", 0, 0, 'h' },
763 { 0, 0, 0, 0 }
764 };
765
766 static const char *name_help =
767 "Usage:\n"
768 "\tname <bdaddr>\n";
769
cmd_name(int dev_id,int argc,char ** argv)770 static void cmd_name(int dev_id, int argc, char **argv)
771 {
772 bdaddr_t bdaddr;
773 char name[248];
774 int opt, dd;
775
776 for_each_opt(opt, name_options, NULL) {
777 switch (opt) {
778 default:
779 printf("%s", name_help);
780 return;
781 }
782 }
783 argc -= optind;
784 argv += optind;
785
786 if (argc < 1) {
787 printf("%s", name_help);
788 return;
789 }
790
791 str2ba(argv[0], &bdaddr);
792
793 if (dev_id < 0) {
794 dev_id = hci_get_route(&bdaddr);
795 if (dev_id < 0) {
796 fprintf(stderr, "Device is not available.\n");
797 exit(1);
798 }
799 }
800
801 dd = hci_open_dev(dev_id);
802 if (dd < 0) {
803 perror("HCI device open failed");
804 exit(1);
805 }
806
807 if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
808 printf("%s\n", name);
809
810 hci_close_dev(dd);
811 }
812
813 /* Info about remote device */
814
815 static struct option info_options[] = {
816 { "help", 0, 0, 'h' },
817 { 0, 0, 0, 0 }
818 };
819
820 static const char *info_help =
821 "Usage:\n"
822 "\tinfo <bdaddr>\n";
823
cmd_info(int dev_id,int argc,char ** argv)824 static void cmd_info(int dev_id, int argc, char **argv)
825 {
826 bdaddr_t bdaddr;
827 uint16_t handle;
828 uint8_t features[8], max_page = 0;
829 char name[249], oui[9], *comp, *tmp;
830 struct hci_version version;
831 struct hci_dev_info di;
832 struct hci_conn_info_req *cr;
833 int i, opt, dd, cc = 0;
834
835 for_each_opt(opt, info_options, NULL) {
836 switch (opt) {
837 default:
838 printf("%s", info_help);
839 return;
840 }
841 }
842 argc -= optind;
843 argv += optind;
844
845 if (argc < 1) {
846 printf("%s", info_help);
847 return;
848 }
849
850 str2ba(argv[0], &bdaddr);
851
852 if (dev_id < 0)
853 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
854
855 if (dev_id < 0)
856 dev_id = hci_get_route(&bdaddr);
857
858 if (dev_id < 0) {
859 fprintf(stderr, "Device is not available or not connected.\n");
860 exit(1);
861 }
862
863 if (hci_devinfo(dev_id, &di) < 0) {
864 perror("Can't get device info");
865 exit(1);
866 }
867
868 printf("Requesting information ...\n");
869
870 dd = hci_open_dev(dev_id);
871 if (dd < 0) {
872 perror("HCI device open failed");
873 exit(1);
874 }
875
876 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
877 if (!cr) {
878 perror("Can't get connection info");
879 close(dd);
880 exit(1);
881 }
882
883 bacpy(&cr->bdaddr, &bdaddr);
884 cr->type = ACL_LINK;
885 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
886 if (hci_create_connection(dd, &bdaddr,
887 htobs(di.pkt_type & ACL_PTYPE_MASK),
888 0, 0x01, &handle, 25000) < 0) {
889 perror("Can't create connection");
890 close(dd);
891 exit(1);
892 }
893 sleep(1);
894 cc = 1;
895 } else
896 handle = htobs(cr->conn_info->handle);
897
898 printf("\tBD Address: %s\n", argv[0]);
899
900 ba2oui(&bdaddr, oui);
901 comp = ouitocomp(oui);
902 if (comp) {
903 printf("\tOUI Company: %s (%s)\n", comp, oui);
904 free(comp);
905 }
906
907 if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
908 printf("\tDevice Name: %s\n", name);
909
910 if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
911 char *ver = lmp_vertostr(version.lmp_ver);
912 printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
913 "\tManufacturer: %s (%d)\n",
914 ver ? ver : "n/a",
915 version.lmp_ver,
916 version.lmp_subver,
917 bt_compidtostr(version.manufacturer),
918 version.manufacturer);
919 if (ver)
920 bt_free(ver);
921 }
922
923 memset(features, 0, sizeof(features));
924 hci_read_remote_features(dd, handle, features, 20000);
925
926 if ((di.features[7] & LMP_EXT_FEAT) && (features[7] & LMP_EXT_FEAT))
927 hci_read_remote_ext_features(dd, handle, 0, &max_page,
928 features, 20000);
929
930 printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
931 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
932 (max_page > 0) ? " page 0" : "",
933 features[0], features[1], features[2], features[3],
934 features[4], features[5], features[6], features[7]);
935
936 tmp = lmp_featurestostr(features, "\t\t", 63);
937 printf("%s\n", tmp);
938 bt_free(tmp);
939
940 for (i = 1; i <= max_page; i++) {
941 if (hci_read_remote_ext_features(dd, handle, i, NULL,
942 features, 20000) < 0)
943 continue;
944
945 printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
946 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i,
947 features[0], features[1], features[2], features[3],
948 features[4], features[5], features[6], features[7]);
949 }
950
951 if (cc) {
952 usleep(10000);
953 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
954 }
955
956 hci_close_dev(dd);
957 }
958
959 /* Start periodic inquiry */
960
961 static struct option spinq_options[] = {
962 { "help", 0, 0, 'h' },
963 { 0, 0, 0, 0 }
964 };
965
966 static const char *spinq_help =
967 "Usage:\n"
968 "\tspinq\n";
969
cmd_spinq(int dev_id,int argc,char ** argv)970 static void cmd_spinq(int dev_id, int argc, char **argv)
971 {
972 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
973 struct hci_request rq;
974 periodic_inquiry_cp cp;
975 int opt, dd;
976
977 for_each_opt(opt, spinq_options, NULL) {
978 switch (opt) {
979 default:
980 printf("%s", spinq_help);
981 return;
982 }
983 }
984
985 if (dev_id < 0)
986 dev_id = hci_get_route(NULL);
987
988 dd = hci_open_dev(dev_id);
989 if (dd < 0) {
990 perror("Device open failed");
991 exit(EXIT_FAILURE);
992 }
993
994 memset(&cp, 0, sizeof(cp));
995 memcpy(cp.lap, lap, 3);
996 cp.max_period = htobs(16);
997 cp.min_period = htobs(10);
998 cp.length = 8;
999 cp.num_rsp = 0;
1000
1001 memset(&rq, 0, sizeof(rq));
1002 rq.ogf = OGF_LINK_CTL;
1003 rq.ocf = OCF_PERIODIC_INQUIRY;
1004 rq.cparam = &cp;
1005 rq.clen = PERIODIC_INQUIRY_CP_SIZE;
1006
1007 if (hci_send_req(dd, &rq, 100) < 0) {
1008 perror("Periodic inquiry failed");
1009 exit(EXIT_FAILURE);
1010 }
1011
1012 hci_close_dev(dd);
1013 }
1014
1015 /* Exit periodic inquiry */
1016
1017 static struct option epinq_options[] = {
1018 { "help", 0, 0, 'h' },
1019 { 0, 0, 0, 0 }
1020 };
1021
1022 static const char *epinq_help =
1023 "Usage:\n"
1024 "\tspinq\n";
1025
cmd_epinq(int dev_id,int argc,char ** argv)1026 static void cmd_epinq(int dev_id, int argc, char **argv)
1027 {
1028 int opt, dd;
1029
1030 for_each_opt(opt, epinq_options, NULL) {
1031 switch (opt) {
1032 default:
1033 printf("%s", epinq_help);
1034 return;
1035 }
1036 }
1037
1038 if (dev_id < 0)
1039 dev_id = hci_get_route(NULL);
1040
1041 dd = hci_open_dev(dev_id);
1042 if (dd < 0) {
1043 perror("Device open failed");
1044 exit(EXIT_FAILURE);
1045 }
1046
1047 if (hci_send_cmd(dd, OGF_LINK_CTL,
1048 OCF_EXIT_PERIODIC_INQUIRY, 0, NULL) < 0) {
1049 perror("Exit periodic inquiry failed");
1050 exit(EXIT_FAILURE);
1051 }
1052
1053 hci_close_dev(dd);
1054 }
1055
1056 /* Send arbitrary HCI commands */
1057
1058 static struct option cmd_options[] = {
1059 { "help", 0, 0, 'h' },
1060 { 0, 0, 0, 0 }
1061 };
1062
1063 static const char *cmd_help =
1064 "Usage:\n"
1065 "\tcmd <ogf> <ocf> [parameters]\n"
1066 "Example:\n"
1067 "\tcmd 0x03 0x0013 0x41 0x42 0x43 0x44\n";
1068
cmd_cmd(int dev_id,int argc,char ** argv)1069 static void cmd_cmd(int dev_id, int argc, char **argv)
1070 {
1071 unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf;
1072 struct hci_filter flt;
1073 hci_event_hdr *hdr;
1074 int i, opt, len, dd;
1075 uint16_t ocf;
1076 uint8_t ogf;
1077
1078 for_each_opt(opt, cmd_options, NULL) {
1079 switch (opt) {
1080 default:
1081 printf("%s", cmd_help);
1082 return;
1083 }
1084 }
1085 argc -= optind;
1086 argv += optind;
1087
1088 if (argc < 2) {
1089 printf("%s", cmd_help);
1090 return;
1091 }
1092
1093 if (dev_id < 0)
1094 dev_id = hci_get_route(NULL);
1095
1096 errno = 0;
1097 ogf = strtol(argv[0], NULL, 16);
1098 ocf = strtol(argv[1], NULL, 16);
1099 if (errno == ERANGE || (ogf > 0x3f) || (ocf > 0x3ff)) {
1100 printf("%s", cmd_help);
1101 return;
1102 }
1103
1104 for (i = 2, len = 0; i < argc && len < (int) sizeof(buf); i++, len++)
1105 *ptr++ = (uint8_t) strtol(argv[i], NULL, 16);
1106
1107 dd = hci_open_dev(dev_id);
1108 if (dd < 0) {
1109 perror("Device open failed");
1110 exit(EXIT_FAILURE);
1111 }
1112
1113 /* Setup filter */
1114 hci_filter_clear(&flt);
1115 hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
1116 hci_filter_all_events(&flt);
1117 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1118 perror("HCI filter setup failed");
1119 exit(EXIT_FAILURE);
1120 }
1121
1122 printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);
1123 hex_dump(" ", 20, buf, len); fflush(stdout);
1124
1125 if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
1126 perror("Send failed");
1127 exit(EXIT_FAILURE);
1128 }
1129
1130 len = read(dd, buf, sizeof(buf));
1131 if (len < 0) {
1132 perror("Read failed");
1133 exit(EXIT_FAILURE);
1134 }
1135
1136 hdr = (void *)(buf + 1);
1137 ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
1138 len -= (1 + HCI_EVENT_HDR_SIZE);
1139
1140 printf("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);
1141 hex_dump(" ", 20, ptr, len); fflush(stdout);
1142
1143 hci_close_dev(dd);
1144 }
1145
1146 /* Display active connections */
1147
1148 static struct option con_options[] = {
1149 { "help", 0, 0, 'h' },
1150 { 0, 0, 0, 0 }
1151 };
1152
1153 static const char *con_help =
1154 "Usage:\n"
1155 "\tcon\n";
1156
cmd_con(int dev_id,int argc,char ** argv)1157 static void cmd_con(int dev_id, int argc, char **argv)
1158 {
1159 int opt;
1160
1161 for_each_opt(opt, con_options, NULL) {
1162 switch (opt) {
1163 default:
1164 printf("%s", con_help);
1165 return;
1166 }
1167 }
1168
1169 printf("Connections:\n");
1170
1171 hci_for_each_dev(HCI_UP, conn_list, dev_id);
1172 }
1173
1174 /* Create connection */
1175
1176 static struct option cc_options[] = {
1177 { "help", 0, 0, 'h' },
1178 { "role", 1, 0, 'r' },
1179 { "ptype", 1, 0, 'p' },
1180 { 0, 0, 0, 0 }
1181 };
1182
1183 static const char *cc_help =
1184 "Usage:\n"
1185 "\tcc [--role=m|s] [--ptype=pkt_types] <bdaddr>\n"
1186 "Example:\n"
1187 "\tcc --ptype=dm1,dh3,dh5 01:02:03:04:05:06\n"
1188 "\tcc --role=m 01:02:03:04:05:06\n";
1189
cmd_cc(int dev_id,int argc,char ** argv)1190 static void cmd_cc(int dev_id, int argc, char **argv)
1191 {
1192 bdaddr_t bdaddr;
1193 uint16_t handle;
1194 uint8_t role;
1195 unsigned int ptype;
1196 int dd, opt;
1197
1198 role = 0x01;
1199 ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;
1200
1201 for_each_opt(opt, cc_options, NULL) {
1202 switch (opt) {
1203 case 'p':
1204 hci_strtoptype(optarg, &ptype);
1205 break;
1206
1207 case 'r':
1208 role = optarg[0] == 'm' ? 0 : 1;
1209 break;
1210
1211 default:
1212 printf("%s", cc_help);
1213 return;
1214 }
1215 }
1216 argc -= optind;
1217 argv += optind;
1218
1219 if (argc < 1) {
1220 printf("%s", cc_help);
1221 return;
1222 }
1223
1224 str2ba(argv[0], &bdaddr);
1225
1226 if (dev_id < 0) {
1227 dev_id = hci_get_route(&bdaddr);
1228 if (dev_id < 0) {
1229 fprintf(stderr, "Device is not available.\n");
1230 exit(1);
1231 }
1232 }
1233
1234 dd = hci_open_dev(dev_id);
1235 if (dd < 0) {
1236 perror("HCI device open failed");
1237 exit(1);
1238 }
1239
1240 if (hci_create_connection(dd, &bdaddr, htobs(ptype),
1241 htobs(0x0000), role, &handle, 25000) < 0)
1242 perror("Can't create connection");
1243
1244 hci_close_dev(dd);
1245 }
1246
1247 /* Close connection */
1248
1249 static struct option dc_options[] = {
1250 { "help", 0, 0, 'h' },
1251 { 0, 0, 0, 0 }
1252 };
1253
1254 static const char *dc_help =
1255 "Usage:\n"
1256 "\tdc <bdaddr> [reason]\n";
1257
cmd_dc(int dev_id,int argc,char ** argv)1258 static void cmd_dc(int dev_id, int argc, char **argv)
1259 {
1260 struct hci_conn_info_req *cr;
1261 bdaddr_t bdaddr;
1262 uint8_t reason;
1263 int opt, dd;
1264
1265 for_each_opt(opt, dc_options, NULL) {
1266 switch (opt) {
1267 default:
1268 printf("%s", dc_help);
1269 return;
1270 }
1271 }
1272 argc -= optind;
1273 argv += optind;
1274
1275 if (argc < 1) {
1276 printf("%s", dc_help);
1277 return;
1278 }
1279
1280 str2ba(argv[0], &bdaddr);
1281 reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
1282
1283 if (dev_id < 0) {
1284 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1285 if (dev_id < 0) {
1286 fprintf(stderr, "Not connected.\n");
1287 exit(1);
1288 }
1289 }
1290
1291 dd = hci_open_dev(dev_id);
1292 if (dd < 0) {
1293 perror("HCI device open failed");
1294 exit(1);
1295 }
1296
1297 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1298 if (!cr) {
1299 perror("Can't allocate memory");
1300 exit(1);
1301 }
1302
1303 bacpy(&cr->bdaddr, &bdaddr);
1304 cr->type = ACL_LINK;
1305 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1306 perror("Get connection info failed");
1307 exit(1);
1308 }
1309
1310 if (hci_disconnect(dd, htobs(cr->conn_info->handle),
1311 reason, 10000) < 0)
1312 perror("Disconnect failed");
1313
1314 free(cr);
1315
1316 hci_close_dev(dd);
1317 }
1318
1319 /* Role switch */
1320
1321 static struct option sr_options[] = {
1322 { "help", 0, 0, 'h' },
1323 { 0, 0, 0, 0 }
1324 };
1325
1326 static const char *sr_help =
1327 "Usage:\n"
1328 "\tsr <bdaddr> <role>\n";
1329
cmd_sr(int dev_id,int argc,char ** argv)1330 static void cmd_sr(int dev_id, int argc, char **argv)
1331 {
1332 bdaddr_t bdaddr;
1333 uint8_t role;
1334 int opt, dd;
1335
1336 for_each_opt(opt, sr_options, NULL) {
1337 switch (opt) {
1338 default:
1339 printf("%s", sr_help);
1340 return;
1341 }
1342 }
1343 argc -= optind;
1344 argv += optind;
1345
1346 if (argc < 2) {
1347 printf("%s", sr_help);
1348 return;
1349 }
1350
1351 str2ba(argv[0], &bdaddr);
1352 switch (argv[1][0]) {
1353 case 'm':
1354 role = 0;
1355 break;
1356 case 's':
1357 role = 1;
1358 break;
1359 default:
1360 role = atoi(argv[1]);
1361 break;
1362 }
1363
1364 if (dev_id < 0) {
1365 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1366 if (dev_id < 0) {
1367 fprintf(stderr, "Not connected.\n");
1368 exit(1);
1369 }
1370 }
1371
1372 dd = hci_open_dev(dev_id);
1373 if (dd < 0) {
1374 perror("HCI device open failed");
1375 exit(1);
1376 }
1377
1378 if (hci_switch_role(dd, &bdaddr, role, 10000) < 0) {
1379 perror("Switch role request failed");
1380 exit(1);
1381 }
1382
1383 hci_close_dev(dd);
1384 }
1385
1386 /* Read RSSI */
1387
1388 static struct option rssi_options[] = {
1389 { "help", 0, 0, 'h' },
1390 { 0, 0, 0, 0 }
1391 };
1392
1393 static const char *rssi_help =
1394 "Usage:\n"
1395 "\trssi <bdaddr>\n";
1396
cmd_rssi(int dev_id,int argc,char ** argv)1397 static void cmd_rssi(int dev_id, int argc, char **argv)
1398 {
1399 struct hci_conn_info_req *cr;
1400 bdaddr_t bdaddr;
1401 int8_t rssi;
1402 int opt, dd;
1403
1404 for_each_opt(opt, rssi_options, NULL) {
1405 switch (opt) {
1406 default:
1407 printf("%s", rssi_help);
1408 return;
1409 }
1410 }
1411 argc -= optind;
1412 argv += optind;
1413
1414 if (argc < 1) {
1415 printf("%s", rssi_help);
1416 return;
1417 }
1418
1419 str2ba(argv[0], &bdaddr);
1420
1421 if (dev_id < 0) {
1422 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1423 if (dev_id < 0) {
1424 fprintf(stderr, "Not connected.\n");
1425 exit(1);
1426 }
1427 }
1428
1429 dd = hci_open_dev(dev_id);
1430 if (dd < 0) {
1431 perror("HCI device open failed");
1432 exit(1);
1433 }
1434
1435 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1436 if (!cr) {
1437 perror("Can't allocate memory");
1438 exit(1);
1439 }
1440
1441 bacpy(&cr->bdaddr, &bdaddr);
1442 cr->type = ACL_LINK;
1443 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1444 perror("Get connection info failed");
1445 exit(1);
1446 }
1447
1448 if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
1449 perror("Read RSSI failed");
1450 exit(1);
1451 }
1452
1453 printf("RSSI return value: %d\n", rssi);
1454
1455 free(cr);
1456
1457 hci_close_dev(dd);
1458 }
1459
1460 /* Get link quality */
1461
1462 static struct option lq_options[] = {
1463 { "help", 0, 0, 'h' },
1464 { 0, 0, 0, 0 }
1465 };
1466
1467 static const char *lq_help =
1468 "Usage:\n"
1469 "\tlq <bdaddr>\n";
1470
cmd_lq(int dev_id,int argc,char ** argv)1471 static void cmd_lq(int dev_id, int argc, char **argv)
1472 {
1473 struct hci_conn_info_req *cr;
1474 bdaddr_t bdaddr;
1475 uint8_t lq;
1476 int opt, dd;
1477
1478 for_each_opt(opt, lq_options, NULL) {
1479 switch (opt) {
1480 default:
1481 printf("%s", lq_help);
1482 return;
1483 }
1484 }
1485 argc -= optind;
1486 argv += optind;
1487
1488 if (argc < 1) {
1489 printf("%s", lq_help);
1490 return;
1491 }
1492
1493 str2ba(argv[0], &bdaddr);
1494
1495 if (dev_id < 0) {
1496 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1497 if (dev_id < 0) {
1498 fprintf(stderr, "Not connected.\n");
1499 exit(1);
1500 }
1501 }
1502
1503 dd = hci_open_dev(dev_id);
1504 if (dd < 0) {
1505 perror("HCI device open failed");
1506 exit(1);
1507 }
1508
1509 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1510 if (!cr) {
1511 perror("Can't allocate memory");
1512 exit(1);
1513 }
1514
1515 bacpy(&cr->bdaddr, &bdaddr);
1516 cr->type = ACL_LINK;
1517 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1518 perror("Get connection info failed");
1519 exit(1);
1520 }
1521
1522 if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), &lq, 1000) < 0) {
1523 perror("HCI read_link_quality request failed");
1524 exit(1);
1525 }
1526
1527 printf("Link quality: %d\n", lq);
1528
1529 free(cr);
1530
1531 hci_close_dev(dd);
1532 }
1533
1534 /* Get transmit power level */
1535
1536 static struct option tpl_options[] = {
1537 { "help", 0, 0, 'h' },
1538 { 0, 0, 0, 0 }
1539 };
1540
1541 static const char *tpl_help =
1542 "Usage:\n"
1543 "\ttpl <bdaddr> [type]\n";
1544
cmd_tpl(int dev_id,int argc,char ** argv)1545 static void cmd_tpl(int dev_id, int argc, char **argv)
1546 {
1547 struct hci_conn_info_req *cr;
1548 bdaddr_t bdaddr;
1549 uint8_t type;
1550 int8_t level;
1551 int opt, dd;
1552
1553 for_each_opt(opt, tpl_options, NULL) {
1554 switch (opt) {
1555 default:
1556 printf("%s", tpl_help);
1557 return;
1558 }
1559 }
1560 argc -= optind;
1561 argv += optind;
1562
1563 if (argc < 1) {
1564 printf("%s", tpl_help);
1565 return;
1566 }
1567
1568 str2ba(argv[0], &bdaddr);
1569 type = (argc > 1) ? atoi(argv[1]) : 0;
1570
1571 if (dev_id < 0) {
1572 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1573 if (dev_id < 0) {
1574 fprintf(stderr, "Not connected.\n");
1575 exit(1);
1576 }
1577 }
1578
1579 dd = hci_open_dev(dev_id);
1580 if (dd < 0) {
1581 perror("HCI device open failed");
1582 exit(1);
1583 }
1584
1585 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1586 if (!cr) {
1587 perror("Can't allocate memory");
1588 exit(1);
1589 }
1590
1591 bacpy(&cr->bdaddr, &bdaddr);
1592 cr->type = ACL_LINK;
1593 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1594 perror("Get connection info failed");
1595 exit(1);
1596 }
1597
1598 if (hci_read_transmit_power_level(dd, htobs(cr->conn_info->handle), type, &level, 1000) < 0) {
1599 perror("HCI read transmit power level request failed");
1600 exit(1);
1601 }
1602
1603 printf("%s transmit power level: %d\n",
1604 (type == 0) ? "Current" : "Maximum", level);
1605
1606 free(cr);
1607
1608 hci_close_dev(dd);
1609 }
1610
1611 /* Get AFH channel map */
1612
1613 static struct option afh_options[] = {
1614 { "help", 0, 0, 'h' },
1615 { 0, 0, 0, 0 }
1616 };
1617
1618 static const char *afh_help =
1619 "Usage:\n"
1620 "\tafh <bdaddr>\n";
1621
cmd_afh(int dev_id,int argc,char ** argv)1622 static void cmd_afh(int dev_id, int argc, char **argv)
1623 {
1624 struct hci_conn_info_req *cr;
1625 bdaddr_t bdaddr;
1626 uint16_t handle;
1627 uint8_t mode, map[10];
1628 int opt, dd;
1629
1630 for_each_opt(opt, afh_options, NULL) {
1631 switch (opt) {
1632 default:
1633 printf("%s", afh_help);
1634 return;
1635 }
1636 }
1637 argc -= optind;
1638 argv += optind;
1639
1640 if (argc < 1) {
1641 printf("%s", afh_help);
1642 return;
1643 }
1644
1645 str2ba(argv[0], &bdaddr);
1646
1647 if (dev_id < 0) {
1648 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1649 if (dev_id < 0) {
1650 fprintf(stderr, "Not connected.\n");
1651 exit(1);
1652 }
1653 }
1654
1655 dd = hci_open_dev(dev_id);
1656 if (dd < 0) {
1657 perror("HCI device open failed");
1658 exit(1);
1659 }
1660
1661 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1662 if (!cr) {
1663 perror("Can't allocate memory");
1664 exit(1);
1665 }
1666
1667 bacpy(&cr->bdaddr, &bdaddr);
1668 cr->type = ACL_LINK;
1669 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1670 perror("Get connection info failed");
1671 exit(1);
1672 }
1673
1674 handle = htobs(cr->conn_info->handle);
1675
1676 if (hci_read_afh_map(dd, handle, &mode, map, 1000) < 0) {
1677 perror("HCI read AFH map request failed");
1678 exit(1);
1679 }
1680
1681 if (mode == 0x01) {
1682 int i;
1683 printf("AFH map: 0x");
1684 for (i = 0; i < 10; i++)
1685 printf("%02x", map[i]);
1686 printf("\n");
1687 } else
1688 printf("AFH disabled\n");
1689
1690 free(cr);
1691
1692 hci_close_dev(dd);
1693 }
1694
1695 /* Set connection packet type */
1696
1697 static struct option cpt_options[] = {
1698 { "help", 0, 0, 'h' },
1699 { 0, 0, 0, 0 }
1700 };
1701
1702 static const char *cpt_help =
1703 "Usage:\n"
1704 "\tcpt <bdaddr> <packet_types>\n";
1705
cmd_cpt(int dev_id,int argc,char ** argv)1706 static void cmd_cpt(int dev_id, int argc, char **argv)
1707 {
1708 struct hci_conn_info_req *cr;
1709 struct hci_request rq;
1710 set_conn_ptype_cp cp;
1711 evt_conn_ptype_changed rp;
1712 bdaddr_t bdaddr;
1713 unsigned int ptype;
1714 int dd, opt;
1715
1716 for_each_opt(opt, cpt_options, NULL) {
1717 switch (opt) {
1718 default:
1719 printf("%s", cpt_help);
1720 return;
1721 }
1722 }
1723 argc -= optind;
1724 argv += optind;
1725
1726 if (argc < 2) {
1727 printf("%s", cpt_help);
1728 return;
1729 }
1730
1731 str2ba(argv[0], &bdaddr);
1732 hci_strtoptype(argv[1], &ptype);
1733
1734 if (dev_id < 0) {
1735 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1736 if (dev_id < 0) {
1737 fprintf(stderr, "Not connected.\n");
1738 exit(1);
1739 }
1740 }
1741
1742 dd = hci_open_dev(dev_id);
1743 if (dd < 0) {
1744 perror("HCI device open failed");
1745 exit(1);
1746 }
1747
1748 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1749 if (!cr) {
1750 perror("Can't allocate memory");
1751 exit(1);
1752 }
1753
1754 bacpy(&cr->bdaddr, &bdaddr);
1755 cr->type = ACL_LINK;
1756 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1757 perror("Get connection info failed");
1758 exit(1);
1759 }
1760
1761 cp.handle = htobs(cr->conn_info->handle);
1762 cp.pkt_type = ptype;
1763
1764 memset(&rq, 0, sizeof(rq));
1765 rq.ogf = OGF_LINK_CTL;
1766 rq.ocf = OCF_SET_CONN_PTYPE;
1767 rq.cparam = &cp;
1768 rq.clen = SET_CONN_PTYPE_CP_SIZE;
1769 rq.rparam = &rp;
1770 rq.rlen = EVT_CONN_PTYPE_CHANGED_SIZE;
1771 rq.event = EVT_CONN_PTYPE_CHANGED;
1772
1773 if (hci_send_req(dd, &rq, 100) < 0) {
1774 perror("Packet type change failed");
1775 exit(1);
1776 }
1777
1778 free(cr);
1779
1780 hci_close_dev(dd);
1781 }
1782
1783 /* Get/Set link policy settings */
1784
1785 static struct option lp_options[] = {
1786 { "help", 0, 0, 'h' },
1787 { 0, 0, 0, 0 }
1788 };
1789
1790 static const char *lp_help =
1791 "Usage:\n"
1792 "\tlp <bdaddr> [link policy]\n";
1793
cmd_lp(int dev_id,int argc,char ** argv)1794 static void cmd_lp(int dev_id, int argc, char **argv)
1795 {
1796 struct hci_conn_info_req *cr;
1797 bdaddr_t bdaddr;
1798 uint16_t policy;
1799 int opt, dd;
1800
1801 for_each_opt(opt, lp_options, NULL) {
1802 switch (opt) {
1803 default:
1804 printf("%s", lp_help);
1805 return;
1806 }
1807 }
1808 argc -= optind;
1809 argv += optind;
1810
1811 if (argc < 1) {
1812 printf("%s", lp_help);
1813 return;
1814 }
1815
1816 str2ba(argv[0], &bdaddr);
1817
1818 if (dev_id < 0) {
1819 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1820 if (dev_id < 0) {
1821 fprintf(stderr, "Not connected.\n");
1822 exit(1);
1823 }
1824 }
1825
1826 dd = hci_open_dev(dev_id);
1827 if (dd < 0) {
1828 perror("HCI device open failed");
1829 exit(1);
1830 }
1831
1832 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1833 if (!cr) {
1834 perror("Can't allocate memory");
1835 exit(1);
1836 }
1837
1838 bacpy(&cr->bdaddr, &bdaddr);
1839 cr->type = ACL_LINK;
1840 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1841 perror("Get connection info failed");
1842 exit(1);
1843 }
1844
1845 if (argc == 1) {
1846 char *str;
1847 if (hci_read_link_policy(dd, htobs(cr->conn_info->handle),
1848 &policy, 1000) < 0) {
1849 perror("HCI read_link_policy_settings request failed");
1850 exit(1);
1851 }
1852
1853 policy = btohs(policy);
1854 str = hci_lptostr(policy);
1855 if (str) {
1856 printf("Link policy settings: %s\n", str);
1857 bt_free(str);
1858 } else {
1859 fprintf(stderr, "Invalig settings\n");
1860 exit(1);
1861 }
1862 } else {
1863 unsigned int val;
1864 if (hci_strtolp(argv[1], &val) < 0) {
1865 fprintf(stderr, "Invalig arguments\n");
1866 exit(1);
1867 }
1868 policy = val;
1869
1870 if (hci_write_link_policy(dd, htobs(cr->conn_info->handle),
1871 htobs(policy), 1000) < 0) {
1872 perror("HCI write_link_policy_settings request failed");
1873 exit(1);
1874 }
1875 }
1876
1877 free(cr);
1878
1879 hci_close_dev(dd);
1880 }
1881
1882 /* Get/Set link supervision timeout */
1883
1884 static struct option lst_options[] = {
1885 { "help", 0, 0, 'h' },
1886 { 0, 0, 0, 0 }
1887 };
1888
1889 static const char *lst_help =
1890 "Usage:\n"
1891 "\tlst <bdaddr> [new value in slots]\n";
1892
cmd_lst(int dev_id,int argc,char ** argv)1893 static void cmd_lst(int dev_id, int argc, char **argv)
1894 {
1895 struct hci_conn_info_req *cr;
1896 bdaddr_t bdaddr;
1897 uint16_t timeout;
1898 int opt, dd;
1899
1900 for_each_opt(opt, lst_options, NULL) {
1901 switch (opt) {
1902 default:
1903 printf("%s", lst_help);
1904 return;
1905 }
1906 }
1907 argc -= optind;
1908 argv += optind;
1909
1910 if (argc < 1) {
1911 printf("%s", lst_help);
1912 return;
1913 }
1914
1915 str2ba(argv[0], &bdaddr);
1916
1917 if (dev_id < 0) {
1918 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1919 if (dev_id < 0) {
1920 fprintf(stderr, "Not connected.\n");
1921 exit(1);
1922 }
1923 }
1924
1925 dd = hci_open_dev(dev_id);
1926 if (dd < 0) {
1927 perror("HCI device open failed");
1928 exit(1);
1929 }
1930
1931 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1932 if (!cr) {
1933 perror("Can't allocate memory");
1934 exit(1);
1935 }
1936
1937 bacpy(&cr->bdaddr, &bdaddr);
1938 cr->type = ACL_LINK;
1939 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1940 perror("Get connection info failed");
1941 exit(1);
1942 }
1943
1944 if (argc == 1) {
1945 if (hci_read_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
1946 &timeout, 1000) < 0) {
1947 perror("HCI read_link_supervision_timeout request failed");
1948 exit(1);
1949 }
1950
1951 timeout = btohs(timeout);
1952
1953 if (timeout)
1954 printf("Link supervision timeout: %u slots (%.2f msec)\n",
1955 timeout, (float) timeout * 0.625);
1956 else
1957 printf("Link supervision timeout never expires\n");
1958 } else {
1959 timeout = strtol(argv[1], NULL, 10);
1960
1961 if (hci_write_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
1962 htobs(timeout), 1000) < 0) {
1963 perror("HCI write_link_supervision_timeout request failed");
1964 exit(1);
1965 }
1966 }
1967
1968 free(cr);
1969
1970 hci_close_dev(dd);
1971 }
1972
1973 /* Request authentication */
1974
1975 static struct option auth_options[] = {
1976 { "help", 0, 0, 'h' },
1977 { 0, 0, 0, 0 }
1978 };
1979
1980 static const char *auth_help =
1981 "Usage:\n"
1982 "\tauth <bdaddr>\n";
1983
cmd_auth(int dev_id,int argc,char ** argv)1984 static void cmd_auth(int dev_id, int argc, char **argv)
1985 {
1986 struct hci_conn_info_req *cr;
1987 bdaddr_t bdaddr;
1988 int opt, dd;
1989
1990 for_each_opt(opt, auth_options, NULL) {
1991 switch (opt) {
1992 default:
1993 printf("%s", auth_help);
1994 return;
1995 }
1996 }
1997 argc -= optind;
1998 argv += optind;
1999
2000 if (argc < 1) {
2001 printf("%s", auth_help);
2002 return;
2003 }
2004
2005 str2ba(argv[0], &bdaddr);
2006
2007 if (dev_id < 0) {
2008 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2009 if (dev_id < 0) {
2010 fprintf(stderr, "Not connected.\n");
2011 exit(1);
2012 }
2013 }
2014
2015 dd = hci_open_dev(dev_id);
2016 if (dd < 0) {
2017 perror("HCI device open failed");
2018 exit(1);
2019 }
2020
2021 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2022 if (!cr) {
2023 perror("Can't allocate memory");
2024 exit(1);
2025 }
2026
2027 bacpy(&cr->bdaddr, &bdaddr);
2028 cr->type = ACL_LINK;
2029 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2030 perror("Get connection info failed");
2031 exit(1);
2032 }
2033
2034 if (hci_authenticate_link(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2035 perror("HCI authentication request failed");
2036 exit(1);
2037 }
2038
2039 free(cr);
2040
2041 hci_close_dev(dd);
2042 }
2043
2044 /* Activate encryption */
2045
2046 static struct option enc_options[] = {
2047 { "help", 0, 0, 'h' },
2048 { 0, 0, 0, 0 }
2049 };
2050
2051 static const char *enc_help =
2052 "Usage:\n"
2053 "\tenc <bdaddr> [encrypt enable]\n";
2054
cmd_enc(int dev_id,int argc,char ** argv)2055 static void cmd_enc(int dev_id, int argc, char **argv)
2056 {
2057 struct hci_conn_info_req *cr;
2058 bdaddr_t bdaddr;
2059 uint8_t encrypt;
2060 int opt, dd;
2061
2062 for_each_opt(opt, enc_options, NULL) {
2063 switch (opt) {
2064 default:
2065 printf("%s", enc_help);
2066 return;
2067 }
2068 }
2069 argc -= optind;
2070 argv += optind;
2071
2072 if (argc < 1) {
2073 printf("%s", enc_help);
2074 return;
2075 }
2076
2077 str2ba(argv[0], &bdaddr);
2078
2079 if (dev_id < 0) {
2080 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2081 if (dev_id < 0) {
2082 fprintf(stderr, "Not connected.\n");
2083 exit(1);
2084 }
2085 }
2086
2087 dd = hci_open_dev(dev_id);
2088 if (dd < 0) {
2089 perror("HCI device open failed");
2090 exit(1);
2091 }
2092
2093 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2094 if (!cr) {
2095 perror("Can't allocate memory");
2096 exit(1);
2097 }
2098
2099 bacpy(&cr->bdaddr, &bdaddr);
2100 cr->type = ACL_LINK;
2101 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2102 perror("Get connection info failed");
2103 exit(1);
2104 }
2105
2106 encrypt = (argc > 1) ? atoi(argv[1]) : 1;
2107
2108 if (hci_encrypt_link(dd, htobs(cr->conn_info->handle), encrypt, 25000) < 0) {
2109 perror("HCI set encryption request failed");
2110 exit(1);
2111 }
2112
2113 free(cr);
2114
2115 hci_close_dev(dd);
2116 }
2117
2118 /* Change connection link key */
2119
2120 static struct option key_options[] = {
2121 { "help", 0, 0, 'h' },
2122 { 0, 0, 0, 0 }
2123 };
2124
2125 static const char *key_help =
2126 "Usage:\n"
2127 "\tkey <bdaddr>\n";
2128
cmd_key(int dev_id,int argc,char ** argv)2129 static void cmd_key(int dev_id, int argc, char **argv)
2130 {
2131 struct hci_conn_info_req *cr;
2132 bdaddr_t bdaddr;
2133 int opt, dd;
2134
2135 for_each_opt(opt, key_options, NULL) {
2136 switch (opt) {
2137 default:
2138 printf("%s", key_help);
2139 return;
2140 }
2141 }
2142 argc -= optind;
2143 argv += optind;
2144
2145 if (argc < 1) {
2146 printf("%s", key_help);
2147 return;
2148 }
2149
2150 str2ba(argv[0], &bdaddr);
2151
2152 if (dev_id < 0) {
2153 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2154 if (dev_id < 0) {
2155 fprintf(stderr, "Not connected.\n");
2156 exit(1);
2157 }
2158 }
2159
2160 dd = hci_open_dev(dev_id);
2161 if (dd < 0) {
2162 perror("HCI device open failed");
2163 exit(1);
2164 }
2165
2166 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2167 if (!cr) {
2168 perror("Can't allocate memory");
2169 exit(1);
2170 }
2171
2172 bacpy(&cr->bdaddr, &bdaddr);
2173 cr->type = ACL_LINK;
2174 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2175 perror("Get connection info failed");
2176 exit(1);
2177 }
2178
2179 if (hci_change_link_key(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2180 perror("Changing link key failed");
2181 exit(1);
2182 }
2183
2184 free(cr);
2185
2186 hci_close_dev(dd);
2187 }
2188
2189 /* Read clock offset */
2190
2191 static struct option clkoff_options[] = {
2192 { "help", 0, 0, 'h' },
2193 { 0, 0, 0, 0 }
2194 };
2195
2196 static const char *clkoff_help =
2197 "Usage:\n"
2198 "\tclkoff <bdaddr>\n";
2199
cmd_clkoff(int dev_id,int argc,char ** argv)2200 static void cmd_clkoff(int dev_id, int argc, char **argv)
2201 {
2202 struct hci_conn_info_req *cr;
2203 bdaddr_t bdaddr;
2204 uint16_t offset;
2205 int opt, dd;
2206
2207 for_each_opt(opt, clkoff_options, NULL) {
2208 switch (opt) {
2209 default:
2210 printf("%s", clkoff_help);
2211 return;
2212 }
2213 }
2214 argc -= optind;
2215 argv += optind;
2216
2217 if (argc < 1) {
2218 printf("%s", clkoff_help);
2219 return;
2220 }
2221
2222 str2ba(argv[0], &bdaddr);
2223
2224 if (dev_id < 0) {
2225 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2226 if (dev_id < 0) {
2227 fprintf(stderr, "Not connected.\n");
2228 exit(1);
2229 }
2230 }
2231
2232 dd = hci_open_dev(dev_id);
2233 if (dd < 0) {
2234 perror("HCI device open failed");
2235 exit(1);
2236 }
2237
2238 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2239 if (!cr) {
2240 perror("Can't allocate memory");
2241 exit(1);
2242 }
2243
2244 bacpy(&cr->bdaddr, &bdaddr);
2245 cr->type = ACL_LINK;
2246 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2247 perror("Get connection info failed");
2248 exit(1);
2249 }
2250
2251 if (hci_read_clock_offset(dd, htobs(cr->conn_info->handle), &offset, 1000) < 0) {
2252 perror("Reading clock offset failed");
2253 exit(1);
2254 }
2255
2256 printf("Clock offset: 0x%4.4x\n", btohs(offset));
2257
2258 free(cr);
2259
2260 hci_close_dev(dd);
2261 }
2262
2263 /* Read clock */
2264
2265 static struct option clock_options[] = {
2266 { "help", 0, 0, 'h' },
2267 { 0, 0, 0, 0 }
2268 };
2269
2270 static const char *clock_help =
2271 "Usage:\n"
2272 "\tclock [bdaddr] [which clock]\n";
2273
cmd_clock(int dev_id,int argc,char ** argv)2274 static void cmd_clock(int dev_id, int argc, char **argv)
2275 {
2276 struct hci_conn_info_req *cr;
2277 bdaddr_t bdaddr;
2278 uint8_t which;
2279 uint32_t handle, clock;
2280 uint16_t accuracy;
2281 int opt, dd;
2282
2283 for_each_opt(opt, clock_options, NULL) {
2284 switch (opt) {
2285 default:
2286 printf("%s", clock_help);
2287 return;
2288 }
2289 }
2290 argc -= optind;
2291 argv += optind;
2292
2293 if (argc > 0)
2294 str2ba(argv[0], &bdaddr);
2295 else
2296 bacpy(&bdaddr, BDADDR_ANY);
2297
2298 if (dev_id < 0 && !bacmp(&bdaddr, BDADDR_ANY))
2299 dev_id = hci_get_route(NULL);
2300
2301 if (dev_id < 0) {
2302 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2303 if (dev_id < 0) {
2304 fprintf(stderr, "Not connected.\n");
2305 exit(1);
2306 }
2307 }
2308
2309 dd = hci_open_dev(dev_id);
2310 if (dd < 0) {
2311 perror("HCI device open failed");
2312 exit(1);
2313 }
2314
2315 if (bacmp(&bdaddr, BDADDR_ANY)) {
2316 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2317 if (!cr) {
2318 perror("Can't allocate memory");
2319 exit(1);
2320 }
2321
2322 bacpy(&cr->bdaddr, &bdaddr);
2323 cr->type = ACL_LINK;
2324 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2325 perror("Get connection info failed");
2326 free(cr);
2327 exit(1);
2328 }
2329
2330 handle = htobs(cr->conn_info->handle);
2331 which = (argc > 1) ? atoi(argv[1]) : 0x01;
2332
2333 free(cr);
2334 } else {
2335 handle = 0x00;
2336 which = 0x00;
2337 }
2338
2339 if (hci_read_clock(dd, handle, which, &clock, &accuracy, 1000) < 0) {
2340 perror("Reading clock failed");
2341 exit(1);
2342 }
2343
2344 accuracy = btohs(accuracy);
2345
2346 printf("Clock: 0x%4.4x\n", btohl(clock));
2347 printf("Accuracy: %.2f msec\n", (float) accuracy * 0.3125);
2348
2349 hci_close_dev(dd);
2350 }
2351
2352 static struct {
2353 char *cmd;
2354 void (*func)(int dev_id, int argc, char **argv);
2355 char *doc;
2356 } command[] = {
2357 { "dev", cmd_dev, "Display local devices" },
2358 { "inq", cmd_inq, "Inquire remote devices" },
2359 { "scan", cmd_scan, "Scan for remote devices" },
2360 { "name", cmd_name, "Get name from remote device" },
2361 { "info", cmd_info, "Get information from remote device" },
2362 { "spinq", cmd_spinq, "Start periodic inquiry" },
2363 { "epinq", cmd_epinq, "Exit periodic inquiry" },
2364 { "cmd", cmd_cmd, "Submit arbitrary HCI commands" },
2365 { "con", cmd_con, "Display active connections" },
2366 { "cc", cmd_cc, "Create connection to remote device" },
2367 { "dc", cmd_dc, "Disconnect from remote device" },
2368 { "sr", cmd_sr, "Switch master/slave role" },
2369 { "cpt", cmd_cpt, "Change connection packet type" },
2370 { "rssi", cmd_rssi, "Display connection RSSI" },
2371 { "lq", cmd_lq, "Display link quality" },
2372 { "tpl", cmd_tpl, "Display transmit power level" },
2373 { "afh", cmd_afh, "Display AFH channel map" },
2374 { "lp", cmd_lp, "Set/display link policy settings" },
2375 { "lst", cmd_lst, "Set/display link supervision timeout" },
2376 { "auth", cmd_auth, "Request authentication" },
2377 { "enc", cmd_enc, "Set connection encryption" },
2378 { "key", cmd_key, "Change connection link key" },
2379 { "clkoff", cmd_clkoff, "Read clock offset" },
2380 { "clock", cmd_clock, "Read local or remote clock" },
2381 { NULL, NULL, 0 }
2382 };
2383
usage(void)2384 static void usage(void)
2385 {
2386 int i;
2387
2388 printf("hcitool - HCI Tool ver %s\n", VERSION);
2389 printf("Usage:\n"
2390 "\thcitool [options] <command> [command parameters]\n");
2391 printf("Options:\n"
2392 "\t--help\tDisplay help\n"
2393 "\t-i dev\tHCI device\n");
2394 printf("Commands:\n");
2395 for (i = 0; command[i].cmd; i++)
2396 printf("\t%-4s\t%s\n", command[i].cmd,
2397 command[i].doc);
2398 printf("\n"
2399 "For more information on the usage of each command use:\n"
2400 "\thcitool <command> --help\n" );
2401 }
2402
2403 static struct option main_options[] = {
2404 { "help", 0, 0, 'h' },
2405 { "device", 1, 0, 'i' },
2406 { 0, 0, 0, 0 }
2407 };
2408
main(int argc,char * argv[])2409 int main(int argc, char *argv[])
2410 {
2411 int opt, i, dev_id = -1;
2412 bdaddr_t ba;
2413
2414 while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
2415 switch (opt) {
2416 case 'i':
2417 dev_id = hci_devid(optarg);
2418 if (dev_id < 0) {
2419 perror("Invalid device");
2420 exit(1);
2421 }
2422 break;
2423
2424 case 'h':
2425 default:
2426 usage();
2427 exit(0);
2428 }
2429 }
2430
2431 argc -= optind;
2432 argv += optind;
2433 optind = 0;
2434
2435 if (argc < 1) {
2436 usage();
2437 exit(0);
2438 }
2439
2440 if (dev_id != -1 && hci_devba(dev_id, &ba) < 0) {
2441 perror("Device is not available");
2442 exit(1);
2443 }
2444
2445 for (i = 0; command[i].cmd; i++) {
2446 if (strncmp(command[i].cmd, argv[0], 3))
2447 continue;
2448 command[i].func(dev_id, argc, argv);
2449 break;
2450 }
2451 return 0;
2452 }
2453