btconfig.c revision 1.1 1 /* $NetBSD: btconfig.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Iain Hibbert for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n"
36 "All rights reserved.\n");
37 __RCSID("$NetBSD: btconfig.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $");
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
44
45 #include <net/if.h>
46
47 #include <stdio.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <stdlib.h>
51 #include <util.h>
52 #include <unistd.h>
53 #include <errno.h>
54 #include <err.h>
55 #include <bluetooth.h>
56
57 int main(int, char *[]);
58 void badarg(const char *);
59 void badparam(const char *);
60 void usage(void);
61 int set_unit(unsigned long);
62 void config_unit(void);
63 void print_info(int);
64 void print_stats(void);
65 void print_class(const char *);
66 void print_voice(int);
67 void tag(const char *);
68 void print_features(const char *, uint8_t *);
69 void do_inquiry(void);
70 void print_result(int, hci_inquiry_response *);
71
72 void hci_req(uint16_t, uint8_t , void *, size_t, void *, size_t);
73 #define save_value(opcode, cbuf, clen) hci_req(opcode, 0, cbuf, clen, NULL, 0)
74 #define load_value(opcode, rbuf, rlen) hci_req(opcode, 0, NULL, 0, rbuf, rlen)
75 #define hci_cmd(opcode, cbuf, clen) hci_req(opcode, 0, cbuf, clen, NULL, 0)
76
77 #define MAX_STR_SIZE 0xff
78
79 /* print width */
80 int width = 0;
81 #define MAX_WIDTH 70
82
83 /* global variables */
84 int hci;
85 struct btreq btr;
86
87 /* command line flags */
88 int verbose = 0; /* more info */
89 int lflag = 0; /* list devices */
90 int sflag = 0; /* get/zero stats */
91
92 /* device up/down (flag) */
93 int opt_enable = 0;
94 int opt_reset = 0;
95 #define FLAGS_FMT "\20" \
96 "\001UP" \
97 "\002RUNNING" \
98 "\003XMIT_CMD" \
99 "\004XMIT_ACL" \
100 "\005XMIT_SCO" \
101 "\006INIT_BDADDR" \
102 "\007INIT_BUFFER_SIZE" \
103 "\010INIT_FEATURES"
104
105 /* authorisation (flag) */
106 int opt_auth = 0;
107
108 /* encryption (flag) */
109 int opt_encrypt = 0;
110
111 /* scan enable options (flags) */
112 int opt_pscan = 0;
113 int opt_iscan = 0;
114
115 /* link policy options (flags) */
116 int opt_switch = 0;
117 int opt_hold = 0;
118 int opt_sniff = 0;
119 int opt_park = 0;
120
121 /* class of device (hex value) */
122 int opt_class = 0;
123 uint32_t class;
124
125 /* packet type mask (hex value) */
126 int opt_ptype = 0;
127 uint32_t ptype;
128 #define PTYPE_FMT "\20" \
129 "\004DM1" \
130 "\005DH1" \
131 "\013DM3" \
132 "\014DH3" \
133 "\017DM5" \
134 "\020DH5"
135
136 /* unit name (string) */
137 int opt_name = 0;
138 char name[MAX_STR_SIZE];
139
140 /* pin type */
141 int opt_pin = 0;
142
143 /* Inquiry */
144 int opt_inquiry = 0;
145 #define INQUIRY_LENGTH 10 /* about 12 seconds */
146 #define INQUIRY_MAX_RESPONSES 10
147
148 /* Voice Settings */
149 int opt_voice = 0;
150 uint32_t voice;
151
152 struct parameter {
153 const char *name;
154 enum { P_SET, P_CLR, P_STR, P_HEX } type;
155 int *opt;
156 void *val;
157 } parameters[] = {
158 { "up", P_SET, &opt_enable, NULL },
159 { "enable", P_SET, &opt_enable, NULL },
160 { "down", P_CLR, &opt_enable, NULL },
161 { "disable", P_CLR, &opt_enable, NULL },
162 { "name", P_STR, &opt_name, name },
163 { "pscan", P_SET, &opt_pscan, NULL },
164 { "-pscan", P_CLR, &opt_pscan, NULL },
165 { "iscan", P_SET, &opt_iscan, NULL },
166 { "-iscan", P_CLR, &opt_iscan, NULL },
167 { "switch", P_SET, &opt_switch, NULL },
168 { "-switch", P_CLR, &opt_switch, NULL },
169 { "hold", P_SET, &opt_hold, NULL },
170 { "-hold", P_CLR, &opt_hold, NULL },
171 { "sniff", P_SET, &opt_sniff, NULL },
172 { "-sniff", P_CLR, &opt_sniff, NULL },
173 { "park", P_SET, &opt_park, NULL },
174 { "-park", P_CLR, &opt_park, NULL },
175 { "auth", P_SET, &opt_auth, NULL },
176 { "-auth", P_CLR, &opt_auth, NULL },
177 { "encrypt", P_SET, &opt_encrypt, NULL },
178 { "-encrypt", P_CLR, &opt_encrypt, NULL },
179 { "ptype", P_HEX, &opt_ptype, &ptype },
180 { "class", P_HEX, &opt_class, &class },
181 { "fixed", P_SET, &opt_pin, NULL },
182 { "variable", P_CLR, &opt_pin, NULL },
183 { "inq", P_SET, &opt_inquiry, NULL },
184 { "inquiry", P_SET, &opt_inquiry, NULL },
185 { "reset", P_SET, &opt_reset, NULL },
186 { "voice", P_HEX, &opt_voice, &voice },
187 { NULL }
188 };
189
190 int
191 main(int ac, char *av[])
192 {
193 int ch;
194 struct parameter *p;
195
196 while ((ch = getopt(ac, av, "hlsvz")) != -1) {
197 switch(ch) {
198 case 'l':
199 lflag = 1;
200 break;
201
202 case 's':
203 sflag = 1;
204 break;
205
206 case 'v':
207 verbose++;
208 break;
209
210 case 'z':
211 sflag = 2;
212 break;
213
214 case 'h':
215 default:
216 usage();
217 }
218 }
219 av += optind;
220 ac -= optind;
221
222 if (lflag && sflag)
223 usage();
224
225 hci = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
226 if (hci == -1)
227 err(EXIT_FAILURE, "socket");
228
229 if (ac == 0) {
230 verbose++;
231
232 memset(&btr, 0, sizeof(btr));
233 while (set_unit(SIOCNBTINFO) != -1) {
234 print_info(verbose);
235 print_stats();
236 }
237
238 tag(NULL);
239 } else {
240 strlcpy(btr.btr_name, *av, HCI_DEVNAME_SIZE);
241 av++, ac--;
242
243 if (set_unit(SIOCGBTINFO) < 0)
244 err(EXIT_FAILURE, "%s", btr.btr_name);
245
246 if (ac == 0)
247 verbose += 2;
248
249 while (ac > 0) {
250 for (p = parameters ; ; p++) {
251 if (p->name == NULL)
252 badparam(*av);
253
254 if (strcmp(*av, p->name) == 0)
255 break;
256 }
257
258 switch(p->type) {
259 case P_SET:
260 *(p->opt) = 1;
261 break;
262
263 case P_CLR:
264 *(p->opt) = -1;
265 break;
266
267 case P_STR:
268 if (--ac < 1) badarg(p->name);
269 strlcpy((char *)(p->val), *++av, MAX_STR_SIZE);
270 *(p->opt) = 1;
271 break;
272
273 case P_HEX:
274 if (--ac < 1) badarg(p->name);
275 *(uint32_t *)(p->val) = strtol(*++av, NULL, 16);
276 *(p->opt) = 1;
277 break;
278 }
279
280 av++, ac--;
281 }
282
283 config_unit();
284 print_info(verbose);
285 print_stats();
286 do_inquiry();
287 }
288
289 close(hci);
290 return EXIT_SUCCESS;
291 }
292
293 void
294 badparam(const char *param)
295 {
296
297 fprintf(stderr, "unknown parameter '%s'\n", param);
298 exit(EXIT_FAILURE);
299 }
300
301 void
302 badarg(const char *param)
303 {
304
305 fprintf(stderr, "parameter '%s' needs argument\n", param);
306 exit(EXIT_FAILURE);
307 }
308
309 void
310 usage(void)
311 {
312
313 fprintf(stderr, "usage: %s [-svz] [device [parameters]]\n", getprogname());
314 fprintf(stderr, " %s -l\n", getprogname());
315 exit(EXIT_FAILURE);
316 }
317
318 /*
319 * pretty printing feature
320 */
321 void
322 tag(const char *f)
323 {
324
325 if (f == NULL) {
326 if (width > 0)
327 printf("\n");
328
329 width = 0;
330 } else {
331 width += printf("%*s%s",
332 (width == 0 ? (lflag ? 0 : 8) : 1),
333 "", f);
334
335 if (width > MAX_WIDTH) {
336 printf("\n");
337 width = 0;
338 }
339 }
340 }
341
342 /*
343 * basic HCI cmd request function with argument return.
344 *
345 * Normally, this will return on COMMAND_STATUS or COMMAND_COMPLETE for the given
346 * opcode, but if event is given then it will ignore COMMAND_STATUS (unless error)
347 * and wait for the specified event.
348 *
349 * if rbuf/rlen is given, results will be copied into the result buffer for
350 * COMMAND_COMPLETE/event responses.
351 */
352 void
353 hci_req(uint16_t opcode, uint8_t event, void *cbuf, size_t clen, void *rbuf, size_t rlen)
354 {
355 uint8_t msg[sizeof(hci_cmd_hdr_t) + HCI_CMD_PKT_SIZE];
356 hci_event_hdr_t *ep;
357 hci_cmd_hdr_t *cp;
358
359 cp = (hci_cmd_hdr_t *)msg;
360 cp->type = HCI_CMD_PKT;
361 cp->opcode = opcode = htole16(opcode);
362 cp->length = clen = MIN(clen, sizeof(msg) - sizeof(hci_cmd_hdr_t));
363
364 if (clen) memcpy((cp + 1), cbuf, clen);
365
366 if (send(hci, msg, sizeof(hci_cmd_hdr_t) + clen, 0) < 0)
367 err(EXIT_FAILURE, "HCI Send");
368
369 ep = (hci_event_hdr_t *)msg;
370 for(;;) {
371 if (recv(hci, msg, sizeof(msg), 0) < 0) {
372 if (errno == EAGAIN || errno == EINTR)
373 continue;
374
375 err(EXIT_FAILURE, "HCI Recv");
376 }
377
378 if (ep->event == HCI_EVENT_COMMAND_STATUS) {
379 hci_command_status_ep *cs;
380
381 cs = (hci_command_status_ep *)(ep + 1);
382 if (cs->opcode != opcode)
383 continue;
384
385 if (cs->status)
386 errx(EXIT_FAILURE,
387 "HCI cmd (%4.4x) failed (status %d)",
388 opcode, cs->status);
389
390 if (event == 0)
391 break;
392
393 continue;
394 }
395
396 if (ep->event == HCI_EVENT_COMMAND_COMPL) {
397 hci_command_compl_ep *cc;
398 uint8_t *ptr;
399
400 cc = (hci_command_compl_ep *)(ep + 1);
401 if (cc->opcode != opcode)
402 continue;
403
404 if (rbuf == NULL)
405 break;
406
407 ptr = (uint8_t *)(cc + 1);
408 if (*ptr)
409 errx(EXIT_FAILURE,
410 "HCI cmd (%4.4x) failed (status %d)",
411 opcode, *ptr);
412
413 memcpy(rbuf, ++ptr, rlen);
414 break;
415 }
416
417 if (ep->event == event) {
418 if (rbuf == NULL)
419 break;
420
421 memcpy(rbuf, (ep + 1), rlen);
422 break;
423 }
424 }
425 }
426
427 int
428 set_unit(unsigned long cmd)
429 {
430
431 if (ioctl(hci, cmd, &btr) == -1)
432 return -1;
433
434 if (btr.btr_flags & BTF_UP) {
435 struct sockaddr_bt sa;
436
437 sa.bt_len = sizeof(sa);
438 sa.bt_family = AF_BLUETOOTH;
439 bdaddr_copy(&sa.bt_bdaddr, &btr.btr_bdaddr);
440
441 if (bind(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
442 err(EXIT_FAILURE, "bind");
443
444 if (connect(hci, (struct sockaddr *)&sa, sizeof(sa)) < 0)
445 err(EXIT_FAILURE, "connect");
446 }
447
448 return 0;
449 }
450
451 /*
452 * apply configuration parameters to unit
453 */
454 void
455 config_unit(void)
456 {
457
458 if (opt_enable) {
459 if (opt_enable > 0)
460 btr.btr_flags |= BTF_UP;
461 else
462 btr.btr_flags &= ~BTF_UP;
463
464 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
465 err(EXIT_FAILURE, "SIOCSBTFLAGS");
466
467 if (set_unit(SIOCGBTINFO) < 0)
468 err(EXIT_FAILURE, "%s", btr.btr_name);
469 }
470
471 if (opt_reset) {
472 hci_cmd(HCI_CMD_RESET, NULL, 0);
473
474 btr.btr_flags |= BTF_INIT;
475 if (ioctl(hci, SIOCSBTFLAGS, &btr) < 0)
476 err(EXIT_FAILURE, "SIOCSBTFLAGS");
477
478 /*
479 * although the reset command will automatically
480 * carry out these commands, we do them manually
481 * just so we can wait for completion.
482 */
483 hci_cmd(HCI_CMD_READ_BDADDR, NULL, 0);
484 hci_cmd(HCI_CMD_READ_BUFFER_SIZE, NULL, 0);
485 hci_cmd(HCI_CMD_READ_LOCAL_FEATURES, NULL, 0);
486
487 if (set_unit(SIOCGBTINFO) < 0)
488 err(EXIT_FAILURE, "%s", btr.btr_name);
489 }
490
491 if (opt_switch || opt_hold || opt_sniff || opt_park) {
492 uint16_t val = btr.btr_link_policy;
493
494 if (opt_switch > 0) val |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
495 if (opt_switch < 0) val &= ~HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
496 if (opt_hold > 0) val |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
497 if (opt_hold < 0) val &= ~HCI_LINK_POLICY_ENABLE_HOLD_MODE;
498 if (opt_sniff > 0) val |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
499 if (opt_sniff < 0) val &= ~HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
500 if (opt_park > 0) val |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
501 if (opt_park < 0) val &= ~HCI_LINK_POLICY_ENABLE_PARK_MODE;
502
503 btr.btr_link_policy = val;
504 if (ioctl(hci, SIOCSBTPOLICY, &btr) < 0)
505 err(EXIT_FAILURE, "SIOCSBTPOLICY");
506 }
507
508 if (opt_ptype) {
509 btr.btr_packet_type = ptype;
510 if (ioctl(hci, SIOCSBTPTYPE, &btr) < 0)
511 err(EXIT_FAILURE, "SIOCSBTPTYPE");
512 }
513
514 if (opt_pscan || opt_iscan) {
515 uint8_t val;
516
517 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
518 if (opt_pscan > 0) val |= HCI_PAGE_SCAN_ENABLE;
519 if (opt_pscan < 0) val &= ~HCI_PAGE_SCAN_ENABLE;
520 if (opt_iscan > 0) val |= HCI_INQUIRY_SCAN_ENABLE;
521 if (opt_iscan < 0) val &= ~HCI_INQUIRY_SCAN_ENABLE;
522 save_value(HCI_CMD_WRITE_SCAN_ENABLE, &val, sizeof(val));
523 }
524
525 if (opt_auth) {
526 uint8_t val = (opt_auth > 0 ? 1 : 0);
527
528 save_value(HCI_CMD_WRITE_AUTH_ENABLE, &val, sizeof(val));
529 }
530
531 if (opt_encrypt) {
532 uint8_t val = (opt_encrypt > 0 ? 1 : 0);
533
534 save_value(HCI_CMD_WRITE_ENCRYPTION_MODE, &val, sizeof(val));
535 }
536
537 if (opt_name)
538 save_value(HCI_CMD_WRITE_LOCAL_NAME, name, HCI_UNIT_NAME_SIZE);
539
540 if (opt_class) {
541 uint8_t val[HCI_CLASS_SIZE];
542
543 val[0] = (class >> 0) & 0xff;
544 val[1] = (class >> 8) & 0xff;
545 val[2] = (class >> 16) & 0xff;
546
547 save_value(HCI_CMD_WRITE_UNIT_CLASS, val, HCI_CLASS_SIZE);
548 }
549
550 if (opt_pin) {
551 uint8_t val;
552
553 if (opt_pin > 0) val = 1;
554 else val = 0;
555
556 save_value(HCI_CMD_WRITE_PIN_TYPE, &val, sizeof(val));
557 }
558
559 if (opt_voice) {
560 uint16_t val;
561
562 val = htole16(voice & 0x03ff);
563 save_value(HCI_CMD_WRITE_VOICE_SETTING, &val, sizeof(val));
564 }
565 }
566
567 /*
568 * Print info for Bluetooth Device with varying verbosity levels
569 */
570 void
571 print_info(int level)
572 {
573 uint8_t val, buf[MAX_STR_SIZE];
574
575 if (lflag) {
576 tag(btr.btr_name);
577 return;
578 }
579
580 if (level-- < 1)
581 return;
582
583 snprintb((char *)buf, MAX_STR_SIZE, FLAGS_FMT, btr.btr_flags);
584
585 printf("%s: bdaddr %s flags %s\n", btr.btr_name,
586 bt_ntoa(&btr.btr_bdaddr, NULL), buf);
587
588 if (level-- < 1)
589 return;
590
591 printf("\tnum_cmd = %d\n"
592 "\tnum_acl = %d, acl_mtu = %d\n"
593 "\tnum_sco = %d, sco_mtu = %d\n",
594 btr.btr_num_cmd,
595 btr.btr_num_acl, btr.btr_acl_mtu,
596 btr.btr_num_sco, btr.btr_sco_mtu);
597
598 if (level-- < 1 || (btr.btr_flags & BTF_UP) == 0)
599 return;
600
601 load_value(HCI_CMD_READ_UNIT_CLASS, buf, HCI_CLASS_SIZE);
602 class = (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
603 print_class("\t");
604
605 load_value(HCI_CMD_READ_LOCAL_NAME, buf, HCI_UNIT_NAME_SIZE);
606 printf("\tname: \"%s\"\n", buf);
607
608 load_value(HCI_CMD_READ_VOICE_SETTING, buf, sizeof(uint16_t));
609 voice = (buf[1] << 8) | buf[0];
610 print_voice(level);
611
612 load_value(HCI_CMD_READ_PIN_TYPE, &val, sizeof(val));
613 printf("\tpin: %s\n", val ? "fixed" : "variable");
614
615 width = printf("\toptions:");
616
617 load_value(HCI_CMD_READ_SCAN_ENABLE, &val, sizeof(val));
618 if (val & HCI_INQUIRY_SCAN_ENABLE) tag("iscan");
619 else if (level > 0) tag("-iscan");
620
621 if (val & HCI_PAGE_SCAN_ENABLE) tag("pscan");
622 else if (level > 0) tag("-pscan");
623
624 load_value(HCI_CMD_READ_AUTH_ENABLE, &val, sizeof(val));
625 if (val) tag("auth");
626 else if (level > 0) tag("-auth");
627
628 load_value(HCI_CMD_READ_ENCRYPTION_MODE, &val, sizeof(val));
629 if (val) tag("encrypt");
630 else if (level > 0) tag("-encrypt");
631
632 val = btr.btr_link_policy;
633 if (val & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH) tag("switch");
634 else if (level > 0) tag("-switch");
635 if (val & HCI_LINK_POLICY_ENABLE_HOLD_MODE) tag("hold");
636 else if (level > 0) tag("-hold");
637 if (val & HCI_LINK_POLICY_ENABLE_SNIFF_MODE) tag("sniff");
638 else if (level > 0) tag("-sniff");
639 if (val & HCI_LINK_POLICY_ENABLE_PARK_MODE) tag("park");
640 else if (level > 0) tag("-park");
641
642 tag(NULL);
643
644 if (level-- < 1)
645 return;
646
647 snprintb((char *)buf, MAX_STR_SIZE, PTYPE_FMT, btr.btr_packet_type);
648 printf("\tptype: %s\n", buf);
649
650 if (level-- < 1)
651 return;
652
653 load_value(HCI_CMD_READ_LOCAL_FEATURES, buf, HCI_FEATURES_SIZE);
654 print_features("\tfeatures:", buf);
655 }
656
657 void
658 print_stats(void)
659 {
660
661 if (sflag == 0)
662 return;
663
664 if (sflag == 1) {
665 if (ioctl(hci, SIOCGBTSTATS, &btr) < 0)
666 err(EXIT_FAILURE, "SIOCGBTSTATS");
667 } else {
668 if (ioctl(hci, SIOCZBTSTATS, &btr) < 0)
669 err(EXIT_FAILURE, "SIOCZBTSTATS");
670 }
671
672 printf( "\tTotal bytes sent %d, recieved %d\n"
673 "\tCommands sent %d, Events received %d\n"
674 "\tACL data packets sent %d, received %d\n"
675 "\tSCO data packets sent %d, received %d\n"
676 "\tInput errors %d, Output errors %d\n",
677 btr.btr_stats.byte_tx, btr.btr_stats.byte_rx,
678 btr.btr_stats.cmd_tx, btr.btr_stats.evt_rx,
679 btr.btr_stats.acl_tx, btr.btr_stats.acl_rx,
680 btr.btr_stats.sco_tx, btr.btr_stats.sco_rx,
681 btr.btr_stats.err_rx, btr.btr_stats.err_tx);
682 }
683
684 void
685 print_features(const char *str, uint8_t *f)
686 {
687
688 width = printf("%s", str);
689
690 /* ------------------- byte 0 --------------------*/
691 if (*f & HCI_LMP_3SLOT) tag("<3 slot>");
692 if (*f & HCI_LMP_5SLOT) tag("<5 slot>");
693 if (*f & HCI_LMP_ENCRYPTION) tag("<encryption>");
694 if (*f & HCI_LMP_SLOT_OFFSET) tag("<slot offset>");
695 if (*f & HCI_LMP_TIMIACCURACY) tag("<timing accuracy>");
696 if (*f & HCI_LMP_ROLE_SWITCH) tag("<role switch>");
697 if (*f & HCI_LMP_HOLD_MODE) tag("<hold mode>");
698 if (*f & HCI_LMP_SNIFF_MODE) tag("<sniff mode>");
699 f++;
700
701 /* ------------------- byte 1 --------------------*/
702 if (*f & HCI_LMP_PARK_MODE) tag("<park mode>");
703 if (*f & HCI_LMP_RSSI) tag("<RSSI>");
704 if (*f & HCI_LMP_CHANNEL_QUALITY) tag("<channel quality>");
705 if (*f & HCI_LMP_SCO_LINK) tag("<SCO link>");
706 if (*f & HCI_LMP_HV2_PKT) tag("<HV2>");
707 if (*f & HCI_LMP_HV3_PKT) tag("<HV3>");
708 if (*f & HCI_LMP_ULAW_LOG) tag("<u-Law log>");
709 if (*f & HCI_LMP_ALAW_LOG) tag("<A-Law log>");
710 f++;
711
712 /* ------------------- byte 1 --------------------*/
713 if (*f & HCI_LMP_CVSD) tag("<CVSD data>");
714 if (*f & HCI_LMP_PAGISCHEME) tag("<paging parameter>");
715 if (*f & HCI_LMP_POWER_CONTROL) tag("<power control>");
716 if (*f & HCI_LMP_TRANSPARENT_SCO) tag("<transparent SCO>");
717 if (*f & HCI_LMP_FLOW_CONTROL_LAG0) tag("<flow control lag 0>");
718 if (*f & HCI_LMP_FLOW_CONTROL_LAG1) tag("<flow control lag 1>");
719 if (*f & HCI_LMP_FLOW_CONTROL_LAG2) tag("<flow control lag 2>");
720 if (*f & HCI_LMP_BC_ENCRYPTION) tag("<broadcast encryption>");
721 f++;
722
723 /* ------------------- byte 3 --------------------*/
724 if (*f & HCI_LMP_EDR_ACL_2MBPS) tag("<EDR ACL 2Mbps>");
725 if (*f & HCI_LMP_EDR_ACL_3MBPS) tag("<EDR ACL 3Mbps>");
726 if (*f & HCI_LMP_ENHANCED_ISCAN) tag("<enhanced inquiry scan>");
727 if (*f & HCI_LMP_INTERLACED_ISCAN) tag("<interlaced inquiry scan>");
728 if (*f & HCI_LMP_INTERLACED_PSCAN) tag("<interlaced page scan>");
729 if (*f & HCI_LMP_RSSI_INQUIRY) tag("<RSSI with inquiry result>");
730 if (*f & HCI_LMP_EV3_PKT) tag("<EV3 packets>");
731 f++;
732
733 /* ------------------- byte 4 --------------------*/
734 if (*f & HCI_LMP_EV4_PKT) tag("<EV4 packets>");
735 if (*f & HCI_LMP_EV5_PKT) tag("<EV5 packets>");
736 if (*f & HCI_LMP_AFH_CAPABLE_SLAVE) tag("<AFH capable slave>");
737 if (*f & HCI_LMP_AFH_CLASS_SLAVE) tag("<AFH class slave>");
738 if (*f & HCI_LMP_3SLOT_EDR_ACL) tag("<3 slot EDR ACL>");
739 f++;
740
741 /* ------------------- byte 5 --------------------*/
742 if (*f & HCI_LMP_5SLOT_EDR_ACL) tag("<5 slot EDR ACL>");
743 if (*f & HCI_LMP_AFH_CAPABLE_MASTER)tag("<AFH capable master>");
744 if (*f & HCI_LMP_AFH_CLASS_MASTER) tag("<AFH class master>");
745 if (*f & HCI_LMP_EDR_eSCO_2MBPS) tag("<EDR eSCO 2Mbps>");
746 if (*f & HCI_LMP_EDR_eSCO_3MBPS) tag("<EDR eSCO 3Mbps>");
747 if (*f & HCI_LMP_3SLOT_EDR_eSCO) tag("<3 slot EDR eSCO>");
748 f++;
749
750 /* ------------------- byte 6 --------------------*/
751 f++;
752
753 /* ------------------- byte 7 --------------------*/
754 if (*f & HCI_LMP_EXTENDED_FEATURES) tag("<extended features>");
755
756 tag(NULL);
757 }
758
759 void
760 print_class(const char *str)
761 {
762 int major, minor;
763
764 major = (class & 0x1f00) >> 8;
765 minor = (class & 0x00fc) >> 2;
766
767 width = printf("%sclass: [0x%6.6x]", str, class);
768
769 switch (major) {
770 case 1: /* Computer */
771 switch (minor) {
772 case 1: tag("Desktop"); break;
773 case 2: tag("Server"); break;
774 case 3: tag("Laptop"); break;
775 case 4: tag("Handheld"); break;
776 case 5: tag("Palm Sized"); break;
777 case 6: tag("Wearable"); break;
778 }
779 tag("Computer");
780 break;
781
782 case 2: /* Phone */
783 switch (minor) {
784 case 1: tag("Cellular Phone"); break;
785 case 2: tag("Cordless Phone"); break;
786 case 3: tag("Smart Phone"); break;
787 case 4: tag("Wired Modem/Phone Gateway"); break;
788 case 5: tag("Common ISDN"); break;
789 default:tag("Phone"); break;
790 }
791 break;
792
793 case 3: /* LAN */
794 tag("LAN");
795 switch ((minor & 0x38) >> 3) {
796 case 0: tag("[Fully available]"); break;
797 case 1: tag("[1-17% utilised]"); break;
798 case 2: tag("[17-33% utilised]"); break;
799 case 3: tag("[33-50% utilised]"); break;
800 case 4: tag("[50-67% utilised]"); break;
801 case 5: tag("[67-83% utilised]"); break;
802 case 6: tag("[83-99% utilised]"); break;
803 case 7: tag("[No service available]"); break;
804 }
805 break;
806
807 case 4: /* Audio/Visual */
808 switch (minor) {
809 case 1: tag("Wearable Headset"); break;
810 case 2: tag("Hands-free Audio"); break;
811 case 4: tag("Microphone"); break;
812 case 5: tag("Loudspeaker"); break;
813 case 6: tag("Headphones"); break;
814 case 7: tag("Portable Audio"); break;
815 case 8: tag("Car Audio"); break;
816 case 9: tag("Set-top Box"); break;
817 case 10: tag("HiFi Audio"); break;
818 case 11: tag("VCR"); break;
819 case 12: tag("Video Camera"); break;
820 case 13: tag("Camcorder"); break;
821 case 14: tag("Video Monitor"); break;
822 case 15: tag("Video Display and Loudspeaker"); break;
823 case 16: tag("Video Conferencing"); break;
824 case 18: tag("A/V [Gaming/Toy]"); break;
825 default: tag("Audio/Visual"); break;
826 }
827 break;
828
829 case 5: /* Peripheral */
830 switch (minor & 0x0f) {
831 case 1: tag("Joystick"); break;
832 case 2: tag("Gamepad"); break;
833 case 3: tag("Remote Control"); break;
834 case 4: tag("Sensing Device"); break;
835 case 5: tag("Digitiser Tablet"); break;
836 case 6: tag("Card Reader"); break;
837 default: tag("Peripheral"); break;
838 }
839
840 if (minor & 0x10) tag("Keyboard");
841 if (minor & 0x20) tag("Mouse");
842 break;
843
844 case 6: /* Imaging */
845 if (minor & 0x20) tag("Printer");
846 if (minor & 0x10) tag("Scanner");
847 if (minor & 0x08) tag("Camera");
848 if (minor & 0x04) tag("Display");
849 if ((minor & 0x3c) == 0) tag("Imaging");
850 break;
851
852 case 7: /* Wearable */
853 switch (minor) {
854 case 1: tag("Wrist Watch"); break;
855 case 2: tag("Pager"); break;
856 case 3: tag("Jacket"); break;
857 case 4: tag("Helmet"); break;
858 case 5: tag("Glasses"); break;
859 default: tag("Wearable"); break;
860 }
861 break;
862
863 case 8: /* Toy */
864 switch (minor) {
865 case 1: tag("Robot"); break;
866 case 2: tag("Vehicle"); break;
867 case 3: tag("Doll / Action Figure"); break;
868 case 4: tag("Controller"); break;
869 case 5: tag("Game"); break;
870 default: tag("Toy"); break;
871 }
872 break;
873
874 default:
875 break;
876 }
877
878 if (class & 0x002000) tag("<Limited Discoverable>");
879 if (class & 0x010000) tag("<Positioning>");
880 if (class & 0x020000) tag("<Networking>");
881 if (class & 0x040000) tag("<Rendering>");
882 if (class & 0x080000) tag("<Capturing>");
883 if (class & 0x100000) tag("<Object Transfer>");
884 if (class & 0x200000) tag("<Audio>");
885 if (class & 0x400000) tag("<Telephony>");
886 if (class & 0x800000) tag("<Information>");
887 tag(NULL);
888 }
889
890 void
891 print_voice(int level)
892 {
893 printf("\tvoice: [0x%4.4x]\n", voice);
894
895 if (level == 0)
896 return;
897
898 printf("\t\tInput Coding: ");
899 switch ((voice & 0x0300) >> 8) {
900 case 0x00: printf("Linear PCM [%d-bit, pos %d]",
901 (voice & 0x0020 ? 16 : 8),
902 (voice & 0x001c) >> 2); break;
903 case 0x01: printf("u-Law"); break;
904 case 0x02: printf("A-Law"); break;
905 case 0x03: printf("unknown"); break;
906 }
907
908 switch ((voice & 0x00c0) >> 6) {
909 case 0x00: printf(", 1's complement"); break;
910 case 0x01: printf(", 2's complement"); break;
911 case 0x02: printf(", sign magnitude"); break;
912 case 0x03: printf(", unsigned"); break;
913 }
914
915 printf("\n\t\tAir Coding: ");
916 switch (voice & 0x0003) {
917 case 0x00: printf("CVSD"); break;
918 case 0x01: printf("u-Law"); break;
919 case 0x02: printf("A-Law"); break;
920 case 0x03: printf("Transparent"); break;
921 }
922
923 printf("\n");
924 }
925
926 void
927 print_result(int num, hci_inquiry_response *r)
928 {
929 hci_remote_name_req_cp ncp;
930 hci_remote_name_req_compl_ep nep;
931 #if 0
932 hci_read_remote_features_cp fcp;
933 hci_read_remote_features_compl_ep fep;
934 #endif
935 struct hostent *hp;
936
937 printf("%3d: bdaddr %s",
938 num,
939 bt_ntoa(&r->bdaddr, NULL));
940
941 hp = bt_gethostbyaddr((const char *)&r->bdaddr, sizeof(bdaddr_t), AF_BLUETOOTH);
942 if (hp != NULL)
943 printf(" (%s)", hp->h_name);
944
945 printf("\n");
946
947 bdaddr_copy(&ncp.bdaddr, &r->bdaddr);
948 ncp.page_scan_rep_mode = r->page_scan_rep_mode;
949 ncp.page_scan_mode = r->page_scan_mode;
950 ncp.clock_offset = r->clock_offset;
951
952 hci_req(HCI_CMD_REMOTE_NAME_REQ,
953 HCI_EVENT_REMOTE_NAME_REQ_COMPL,
954 &ncp, sizeof(ncp),
955 &nep, sizeof(nep));
956
957 printf(" : name \"%s\"\n", nep.name);
958
959 class = (r->uclass[2] << 16) | (r->uclass[1] << 8) | (r->uclass[0]);
960 print_class(" : ");
961
962 #if 0
963 hci_req(HCI_CMD_READ_REMOTE_FEATURES,
964 HCI_EVENT_READ_REMOTE_FEATURES_COMPL,
965 &fcp, sizeof(fcp),
966 &fep, sizeof(fep));
967
968 print_features(" : features", fep.features);
969 #endif
970
971 printf(" : page scan rep mode 0x%02x\n", r->page_scan_rep_mode);
972 printf(" : page scan period mode 0x%02x\n", r->page_scan_period_mode);
973 printf(" : page scan mode 0x%02x\n", r->page_scan_mode);
974 printf(" : clock offset %d\n", le16toh(r->clock_offset));
975
976 printf("\n");
977 }
978
979 void
980 do_inquiry(void)
981 {
982 uint8_t buf[HCI_EVENT_PKT_SIZE];
983 hci_inquiry_response result[INQUIRY_MAX_RESPONSES];
984 hci_inquiry_cp inq;
985 struct hci_filter f;
986 hci_event_hdr_t *hh;
987 hci_inquiry_result_ep *ep;
988 hci_inquiry_response *ir;
989 int i, j, num;
990
991 if (opt_inquiry == 0)
992 return;
993
994 printf("Device Discovery from device: %s ...", btr.btr_name);
995 fflush(stdout);
996
997 memset(&f, 0, sizeof(f));
998 hci_filter_set(HCI_EVENT_COMMAND_STATUS, &f);
999 hci_filter_set(HCI_EVENT_COMMAND_COMPL, &f);
1000 hci_filter_set(HCI_EVENT_INQUIRY_RESULT, &f);
1001 hci_filter_set(HCI_EVENT_INQUIRY_COMPL, &f);
1002 hci_filter_set(HCI_EVENT_REMOTE_NAME_REQ_COMPL, &f);
1003 hci_filter_set(HCI_EVENT_READ_REMOTE_FEATURES_COMPL, &f);
1004 if (setsockopt(hci, BTPROTO_HCI, SO_HCI_EVT_FILTER, &f, sizeof(f)) < 0)
1005 err(EXIT_FAILURE, "Can't set event filter");
1006
1007 /* General Inquiry LAP is 0x9e8b33 */
1008 inq.lap[0] = 0x33;
1009 inq.lap[1] = 0x8b;
1010 inq.lap[2] = 0x9e;
1011 inq.inquiry_length = INQUIRY_LENGTH;
1012 inq.num_responses = INQUIRY_MAX_RESPONSES;
1013
1014 hci_cmd(HCI_CMD_INQUIRY, &inq, sizeof(inq));
1015
1016 num = 0;
1017 hh = (hci_event_hdr_t *)buf;
1018 ep = (hci_inquiry_result_ep *)(hh + 1);
1019 ir = (hci_inquiry_response *)(ep + 1);
1020
1021 for (;;) {
1022 if (recv(hci, buf, sizeof(buf), 0) <= 0)
1023 err(EXIT_FAILURE, "recv");
1024
1025 if (hh->event == HCI_EVENT_INQUIRY_COMPL)
1026 break;
1027
1028 if (hh->event == HCI_EVENT_INQUIRY_RESULT) {
1029 for (i = 0 ; i < ep->num_responses ; i++) {
1030 if (num == INQUIRY_MAX_RESPONSES)
1031 break;
1032
1033 /* some devices keep responding, ignore dupes */
1034 for (j = 0 ; j < num ; j++)
1035 if (bdaddr_same(&result[j].bdaddr, &ir[i].bdaddr))
1036 break;
1037
1038 if (j < num)
1039 continue;
1040
1041 memcpy(&result[num++], &ir[i], sizeof(hci_inquiry_response));
1042 printf(".");
1043 fflush(stdout);
1044
1045 }
1046 continue;
1047 }
1048 }
1049
1050 printf(" %d response%s\n", num, (num == 1 ? "" : "s"));
1051
1052 for (i = 0 ; i < num ; i++)
1053 print_result(i + 1, &result[i]);
1054 }
1055