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