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 <unistd.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <getopt.h>
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/hci.h>
43 #include <bluetooth/hci_lib.h>
44
45 #include "textfile.h"
46 #include "csr.h"
47
48 static struct hci_dev_info di;
49 static int all;
50
51 static void print_dev_hdr(struct hci_dev_info *di);
52 static void print_dev_info(int ctl, struct hci_dev_info *di);
53
print_dev_list(int ctl,int flags)54 static void print_dev_list(int ctl, int flags)
55 {
56 struct hci_dev_list_req *dl;
57 struct hci_dev_req *dr;
58 int i;
59
60 if (!(dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) +
61 sizeof(uint16_t)))) {
62 perror("Can't allocate memory");
63 exit(1);
64 }
65 dl->dev_num = HCI_MAX_DEV;
66 dr = dl->dev_req;
67
68 if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) {
69 perror("Can't get device list");
70 exit(1);
71 }
72
73 for (i = 0; i< dl->dev_num; i++) {
74 di.dev_id = (dr+i)->dev_id;
75 if (ioctl(ctl, HCIGETDEVINFO, (void *) &di) < 0)
76 continue;
77 if (hci_test_bit(HCI_RAW, &di.flags) &&
78 !bacmp(&di.bdaddr, BDADDR_ANY)) {
79 int dd = hci_open_dev(di.dev_id);
80 hci_read_bd_addr(dd, &di.bdaddr, 1000);
81 hci_close_dev(dd);
82 }
83 print_dev_info(ctl, &di);
84 }
85 }
86
print_pkt_type(struct hci_dev_info * di)87 static void print_pkt_type(struct hci_dev_info *di)
88 {
89 char *str;
90 str = hci_ptypetostr(di->pkt_type);
91 printf("\tPacket type: %s\n", str);
92 bt_free(str);
93 }
94
print_link_policy(struct hci_dev_info * di)95 static void print_link_policy(struct hci_dev_info *di)
96 {
97 printf("\tLink policy: %s\n", hci_lptostr(di->link_policy));
98 }
99
print_link_mode(struct hci_dev_info * di)100 static void print_link_mode(struct hci_dev_info *di)
101 {
102 char *str;
103 str = hci_lmtostr(di->link_mode);
104 printf("\tLink mode: %s\n", str);
105 bt_free(str);
106 }
107
print_dev_features(struct hci_dev_info * di,int format)108 static void print_dev_features(struct hci_dev_info *di, int format)
109 {
110 printf("\tFeatures: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
111 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
112 di->features[0], di->features[1], di->features[2],
113 di->features[3], di->features[4], di->features[5],
114 di->features[6], di->features[7]);
115
116 if (format) {
117 char *tmp = lmp_featurestostr(di->features, "\t\t", 63);
118 printf("%s\n", tmp);
119 bt_free(tmp);
120 }
121 }
122
print_le_states(uint64_t states)123 static void print_le_states(uint64_t states)
124 {
125 int i;
126 const char *le_states[] = {
127 "Non-connectable Advertising State" ,
128 "Scannable Advertising State",
129 "Connectable Advertising State",
130 "Directed Advertising State",
131 "Passive Scanning State",
132 "Active Scanning State",
133 "Initiating State/Connection State in Master Role",
134 "Connection State in the Slave Role",
135 "Non-connectable Advertising State and Passive Scanning State combination",
136 "Scannable Advertising State and Passive Scanning State combination",
137 "Connectable Advertising State and Passive Scanning State combination",
138 "Directed Advertising State and Passive Scanning State combination",
139 "Non-connectable Advertising State and Active Scanning State combination",
140 "Scannable Advertising State and Active Scanning State combination",
141 "Connectable Advertising State and Active Scanning State combination",
142 "Directed Advertising State and Active Scanning State combination",
143 "Non-connectable Advertising State and Initiating State combination",
144 "Scannable Advertising State and Initiating State combination",
145 "Non-connectable Advertising State and Master Role combination",
146 "Scannable Advertising State and Master Role combination",
147 "Non-connectable Advertising State and Slave Role combination",
148 "Scannable Advertising State and Slave Role combination",
149 "Passive Scanning State and Initiating State combination",
150 "Active Scanning State and Initiating State combination",
151 "Passive Scanning State and Master Role combination",
152 "Active Scanning State and Master Role combination",
153 "Passive Scanning State and Slave Role combination",
154 "Active Scanning State and Slave Role combination",
155 "Initiating State and Master Role combination/Master Role and Master Role combination",
156 NULL
157 };
158
159 printf("Supported link layer states:\n");
160 for (i = 0; le_states[i]; i++) {
161 const char *status;
162
163 status = states & (1 << i) ? "YES" : "NO ";
164 printf("\t%s %s\n", status, le_states[i]);
165 }
166 }
167
cmd_rstat(int ctl,int hdev,char * opt)168 static void cmd_rstat(int ctl, int hdev, char *opt)
169 {
170 /* Reset HCI device stat counters */
171 if (ioctl(ctl, HCIDEVRESTAT, hdev) < 0) {
172 fprintf(stderr, "Can't reset stats counters hci%d: %s (%d)\n",
173 hdev, strerror(errno), errno);
174 exit(1);
175 }
176 }
177
cmd_scan(int ctl,int hdev,char * opt)178 static void cmd_scan(int ctl, int hdev, char *opt)
179 {
180 struct hci_dev_req dr;
181
182 dr.dev_id = hdev;
183 dr.dev_opt = SCAN_DISABLED;
184 if (!strcmp(opt, "iscan"))
185 dr.dev_opt = SCAN_INQUIRY;
186 else if (!strcmp(opt, "pscan"))
187 dr.dev_opt = SCAN_PAGE;
188 else if (!strcmp(opt, "piscan"))
189 dr.dev_opt = SCAN_PAGE | SCAN_INQUIRY;
190
191 if (ioctl(ctl, HCISETSCAN, (unsigned long) &dr) < 0) {
192 fprintf(stderr, "Can't set scan mode on hci%d: %s (%d)\n",
193 hdev, strerror(errno), errno);
194 exit(1);
195 }
196 }
197
cmd_le_addr(int ctl,int hdev,char * opt)198 static void cmd_le_addr(int ctl, int hdev, char *opt)
199 {
200 struct hci_request rq;
201 le_set_random_address_cp cp;
202 uint8_t status;
203 int dd, err, ret;
204
205 if (!opt)
206 return;
207
208 if (hdev < 0)
209 hdev = hci_get_route(NULL);
210
211 dd = hci_open_dev(hdev);
212 if (dd < 0) {
213 err = errno;
214 fprintf(stderr, "Could not open device: %s(%d)\n",
215 strerror(err), err);
216 exit(1);
217 }
218
219 memset(&cp, 0, sizeof(cp));
220
221 str2ba(opt, &cp.bdaddr);
222
223 memset(&rq, 0, sizeof(rq));
224 rq.ogf = OGF_LE_CTL;
225 rq.ocf = OCF_LE_SET_RANDOM_ADDRESS;
226 rq.cparam = &cp;
227 rq.clen = LE_SET_RANDOM_ADDRESS_CP_SIZE;
228 rq.rparam = &status;
229 rq.rlen = 1;
230
231 ret = hci_send_req(dd, &rq, 1000);
232 if (status || ret < 0) {
233 err = errno;
234 fprintf(stderr, "Can't set random address for hci%d: "
235 "%s (%d)\n", hdev, strerror(err), err);
236 }
237
238 hci_close_dev(dd);
239 }
240
cmd_le_adv(int ctl,int hdev,char * opt)241 static void cmd_le_adv(int ctl, int hdev, char *opt)
242 {
243 struct hci_request rq;
244 le_set_advertise_enable_cp advertise_cp;
245 uint8_t status;
246 int dd, ret;
247
248 if (hdev < 0)
249 hdev = hci_get_route(NULL);
250
251 dd = hci_open_dev(hdev);
252 if (dd < 0) {
253 perror("Could not open device");
254 exit(1);
255 }
256
257 memset(&advertise_cp, 0, sizeof(advertise_cp));
258 if (strcmp(opt, "noleadv") == 0)
259 advertise_cp.enable = 0x00;
260 else
261 advertise_cp.enable = 0x01;
262
263 memset(&rq, 0, sizeof(rq));
264 rq.ogf = OGF_LE_CTL;
265 rq.ocf = OCF_LE_SET_ADVERTISE_ENABLE;
266 rq.cparam = &advertise_cp;
267 rq.clen = LE_SET_ADVERTISE_ENABLE_CP_SIZE;
268 rq.rparam = &status;
269 rq.rlen = 1;
270
271 ret = hci_send_req(dd, &rq, 1000);
272
273 hci_close_dev(dd);
274
275 if (ret < 0) {
276 fprintf(stderr, "Can't set advertise mode on hci%d: %s (%d)\n",
277 hdev, strerror(errno), errno);
278 exit(1);
279 }
280
281 if (status) {
282 fprintf(stderr, "LE set advertise enable on hci%d returned status %d\n",
283 hdev, status);
284 exit(1);
285 }
286 }
287
cmd_le_states(int ctl,int hdev,char * opt)288 static void cmd_le_states(int ctl, int hdev, char *opt)
289 {
290 le_read_supported_states_rp rp;
291 struct hci_request rq;
292 int err, dd;
293
294 if (hdev < 0)
295 hdev = hci_get_route(NULL);
296
297 dd = hci_open_dev(hdev);
298 if (dd < 0) {
299 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
300 hdev, strerror(errno), errno);
301 exit(1);
302 }
303
304 memset(&rp, 0, sizeof(rp));
305 memset(&rq, 0, sizeof(rq));
306
307 rq.ogf = OGF_LE_CTL;
308 rq.ocf = OCF_LE_READ_SUPPORTED_STATES;
309 rq.rparam = &rp;
310 rq.rlen = LE_READ_SUPPORTED_STATES_RP_SIZE;
311
312 err = hci_send_req(dd, &rq, 1000);
313
314 hci_close_dev(dd);
315
316 if (err < 0) {
317 fprintf(stderr, "Can't read LE supported states on hci%d:"
318 " %s(%d)\n", hdev, strerror(errno), errno);
319 exit(1);
320 }
321
322 if (rp.status) {
323 fprintf(stderr, "Read LE supported states on hci%d"
324 " returned status %d\n", hdev, rp.status);
325 exit(1);
326 }
327
328 print_le_states(rp.states);
329 }
330
cmd_iac(int ctl,int hdev,char * opt)331 static void cmd_iac(int ctl, int hdev, char *opt)
332 {
333 int s = hci_open_dev(hdev);
334
335 if (s < 0) {
336 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
337 hdev, strerror(errno), errno);
338 exit(1);
339 }
340 if (opt) {
341 int l = strtoul(opt, 0, 16);
342 uint8_t lap[3];
343 if (!strcasecmp(opt, "giac")) {
344 l = 0x9e8b33;
345 } else if (!strcasecmp(opt, "liac")) {
346 l = 0x9e8b00;
347 } else if (l < 0x9e8b00 || l > 0x9e8b3f) {
348 printf("Invalid access code 0x%x\n", l);
349 exit(1);
350 }
351 lap[0] = (l & 0xff);
352 lap[1] = (l >> 8) & 0xff;
353 lap[2] = (l >> 16) & 0xff;
354 if (hci_write_current_iac_lap(s, 1, lap, 1000) < 0) {
355 printf("Failed to set IAC on hci%d: %s\n", hdev, strerror(errno));
356 exit(1);
357 }
358 } else {
359 uint8_t lap[3 * MAX_IAC_LAP];
360 int i, j;
361 uint8_t n;
362 if (hci_read_current_iac_lap(s, &n, lap, 1000) < 0) {
363 printf("Failed to read IAC from hci%d: %s\n", hdev, strerror(errno));
364 exit(1);
365 }
366 print_dev_hdr(&di);
367 printf("\tIAC: ");
368 for (i = 0; i < n; i++) {
369 printf("0x");
370 for (j = 3; j--; )
371 printf("%02x", lap[j + 3 * i]);
372 if (i < n - 1)
373 printf(", ");
374 }
375 printf("\n");
376 }
377 close(s);
378 }
379
cmd_auth(int ctl,int hdev,char * opt)380 static void cmd_auth(int ctl, int hdev, char *opt)
381 {
382 struct hci_dev_req dr;
383
384 dr.dev_id = hdev;
385 if (!strcmp(opt, "auth"))
386 dr.dev_opt = AUTH_ENABLED;
387 else
388 dr.dev_opt = AUTH_DISABLED;
389
390 if (ioctl(ctl, HCISETAUTH, (unsigned long) &dr) < 0) {
391 fprintf(stderr, "Can't set auth on hci%d: %s (%d)\n",
392 hdev, strerror(errno), errno);
393 exit(1);
394 }
395 }
396
cmd_encrypt(int ctl,int hdev,char * opt)397 static void cmd_encrypt(int ctl, int hdev, char *opt)
398 {
399 struct hci_dev_req dr;
400
401 dr.dev_id = hdev;
402 if (!strcmp(opt, "encrypt"))
403 dr.dev_opt = ENCRYPT_P2P;
404 else
405 dr.dev_opt = ENCRYPT_DISABLED;
406
407 if (ioctl(ctl, HCISETENCRYPT, (unsigned long) &dr) < 0) {
408 fprintf(stderr, "Can't set encrypt on hci%d: %s (%d)\n",
409 hdev, strerror(errno), errno);
410 exit(1);
411 }
412 }
413
cmd_up(int ctl,int hdev,char * opt)414 static void cmd_up(int ctl, int hdev, char *opt)
415 {
416 /* Start HCI device */
417 if (ioctl(ctl, HCIDEVUP, hdev) < 0) {
418 if (errno == EALREADY)
419 return;
420 fprintf(stderr, "Can't init device hci%d: %s (%d)\n",
421 hdev, strerror(errno), errno);
422 exit(1);
423 }
424 }
425
cmd_down(int ctl,int hdev,char * opt)426 static void cmd_down(int ctl, int hdev, char *opt)
427 {
428 /* Stop HCI device */
429 if (ioctl(ctl, HCIDEVDOWN, hdev) < 0) {
430 fprintf(stderr, "Can't down device hci%d: %s (%d)\n",
431 hdev, strerror(errno), errno);
432 exit(1);
433 }
434 }
435
cmd_reset(int ctl,int hdev,char * opt)436 static void cmd_reset(int ctl, int hdev, char *opt)
437 {
438 /* Reset HCI device */
439 #if 0
440 if (ioctl(ctl, HCIDEVRESET, hdev) < 0 ){
441 fprintf(stderr, "Reset failed for device hci%d: %s (%d)\n",
442 hdev, strerror(errno), errno);
443 exit(1);
444 }
445 #endif
446 cmd_down(ctl, hdev, "down");
447 cmd_up(ctl, hdev, "up");
448 }
449
cmd_ptype(int ctl,int hdev,char * opt)450 static void cmd_ptype(int ctl, int hdev, char *opt)
451 {
452 struct hci_dev_req dr;
453
454 dr.dev_id = hdev;
455
456 if (hci_strtoptype(opt, &dr.dev_opt)) {
457 if (ioctl(ctl, HCISETPTYPE, (unsigned long) &dr) < 0) {
458 fprintf(stderr, "Can't set pkttype on hci%d: %s (%d)\n",
459 hdev, strerror(errno), errno);
460 exit(1);
461 }
462 } else {
463 print_dev_hdr(&di);
464 print_pkt_type(&di);
465 }
466 }
467
cmd_lp(int ctl,int hdev,char * opt)468 static void cmd_lp(int ctl, int hdev, char *opt)
469 {
470 struct hci_dev_req dr;
471
472 dr.dev_id = hdev;
473
474 if (hci_strtolp(opt, &dr.dev_opt)) {
475 if (ioctl(ctl, HCISETLINKPOL, (unsigned long) &dr) < 0) {
476 fprintf(stderr, "Can't set link policy on hci%d: %s (%d)\n",
477 hdev, strerror(errno), errno);
478 exit(1);
479 }
480 } else {
481 print_dev_hdr(&di);
482 print_link_policy(&di);
483 }
484 }
485
cmd_lm(int ctl,int hdev,char * opt)486 static void cmd_lm(int ctl, int hdev, char *opt)
487 {
488 struct hci_dev_req dr;
489
490 dr.dev_id = hdev;
491
492 if (hci_strtolm(opt, &dr.dev_opt)) {
493 if (ioctl(ctl, HCISETLINKMODE, (unsigned long) &dr) < 0) {
494 fprintf(stderr, "Can't set default link mode on hci%d: %s (%d)\n",
495 hdev, strerror(errno), errno);
496 exit(1);
497 }
498 } else {
499 print_dev_hdr(&di);
500 print_link_mode(&di);
501 }
502 }
503
cmd_aclmtu(int ctl,int hdev,char * opt)504 static void cmd_aclmtu(int ctl, int hdev, char *opt)
505 {
506 struct hci_dev_req dr = { dev_id: hdev };
507 uint16_t mtu, mpkt;
508
509 if (!opt)
510 return;
511
512 if (sscanf(opt, "%4hu:%4hu", &mtu, &mpkt) != 2)
513 return;
514
515 dr.dev_opt = htobl(htobs(mpkt) | (htobs(mtu) << 16));
516
517 if (ioctl(ctl, HCISETACLMTU, (unsigned long) &dr) < 0) {
518 fprintf(stderr, "Can't set ACL mtu on hci%d: %s(%d)\n",
519 hdev, strerror(errno), errno);
520 exit(1);
521 }
522 }
523
cmd_scomtu(int ctl,int hdev,char * opt)524 static void cmd_scomtu(int ctl, int hdev, char *opt)
525 {
526 struct hci_dev_req dr = { dev_id: hdev };
527 uint16_t mtu, mpkt;
528
529 if (!opt)
530 return;
531
532 if (sscanf(opt, "%4hu:%4hu", &mtu, &mpkt) != 2)
533 return;
534
535 dr.dev_opt = htobl(htobs(mpkt) | (htobs(mtu) << 16));
536
537 if (ioctl(ctl, HCISETSCOMTU, (unsigned long) &dr) < 0) {
538 fprintf(stderr, "Can't set SCO mtu on hci%d: %s (%d)\n",
539 hdev, strerror(errno), errno);
540 exit(1);
541 }
542 }
543
cmd_features(int ctl,int hdev,char * opt)544 static void cmd_features(int ctl, int hdev, char *opt)
545 {
546 uint8_t features[8], max_page = 0;
547 char *tmp;
548 int i, dd;
549
550 if (!(di.features[7] & LMP_EXT_FEAT)) {
551 print_dev_hdr(&di);
552 print_dev_features(&di, 1);
553 return;
554 }
555
556 dd = hci_open_dev(hdev);
557 if (dd < 0) {
558 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
559 hdev, strerror(errno), errno);
560 exit(1);
561 }
562
563 if (hci_read_local_ext_features(dd, 0, &max_page, features, 1000) < 0) {
564 fprintf(stderr, "Can't read extended features hci%d: %s (%d)\n",
565 hdev, strerror(errno), errno);
566 exit(1);
567 }
568
569 print_dev_hdr(&di);
570 printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
571 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
572 (max_page > 0) ? " page 0" : "",
573 features[0], features[1], features[2], features[3],
574 features[4], features[5], features[6], features[7]);
575
576 tmp = lmp_featurestostr(di.features, "\t\t", 63);
577 printf("%s\n", tmp);
578 bt_free(tmp);
579
580 for (i = 1; i <= max_page; i++) {
581 if (hci_read_local_ext_features(dd, i, NULL,
582 features, 1000) < 0)
583 continue;
584
585 printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
586 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i,
587 features[0], features[1], features[2], features[3],
588 features[4], features[5], features[6], features[7]);
589 }
590
591 hci_close_dev(dd);
592 }
593
cmd_name(int ctl,int hdev,char * opt)594 static void cmd_name(int ctl, int hdev, char *opt)
595 {
596 int dd;
597
598 dd = hci_open_dev(hdev);
599 if (dd < 0) {
600 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
601 hdev, strerror(errno), errno);
602 exit(1);
603 }
604
605 if (opt) {
606 if (hci_write_local_name(dd, opt, 2000) < 0) {
607 fprintf(stderr, "Can't change local name on hci%d: %s (%d)\n",
608 hdev, strerror(errno), errno);
609 exit(1);
610 }
611 } else {
612 char name[249];
613 int i;
614
615 if (hci_read_local_name(dd, sizeof(name), name, 1000) < 0) {
616 fprintf(stderr, "Can't read local name on hci%d: %s (%d)\n",
617 hdev, strerror(errno), errno);
618 exit(1);
619 }
620
621 for (i = 0; i < 248 && name[i]; i++) {
622 if ((unsigned char) name[i] < 32 || name[i] == 127)
623 name[i] = '.';
624 }
625
626 name[248] = '\0';
627
628 print_dev_hdr(&di);
629 printf("\tName: '%s'\n", name);
630 }
631
632 hci_close_dev(dd);
633 }
634
635 /*
636 * see http://www.bluetooth.org/assigned-numbers/baseband.htm --- all
637 * strings are reproduced verbatim
638 */
get_minor_device_name(int major,int minor)639 static char *get_minor_device_name(int major, int minor)
640 {
641 switch (major) {
642 case 0: /* misc */
643 return "";
644 case 1: /* computer */
645 switch (minor) {
646 case 0:
647 return "Uncategorized";
648 case 1:
649 return "Desktop workstation";
650 case 2:
651 return "Server";
652 case 3:
653 return "Laptop";
654 case 4:
655 return "Handheld";
656 case 5:
657 return "Palm";
658 case 6:
659 return "Wearable";
660 }
661 break;
662 case 2: /* phone */
663 switch (minor) {
664 case 0:
665 return "Uncategorized";
666 case 1:
667 return "Cellular";
668 case 2:
669 return "Cordless";
670 case 3:
671 return "Smart phone";
672 case 4:
673 return "Wired modem or voice gateway";
674 case 5:
675 return "Common ISDN Access";
676 case 6:
677 return "Sim Card Reader";
678 }
679 break;
680 case 3: /* lan access */
681 if (minor == 0)
682 return "Uncategorized";
683 switch (minor / 8) {
684 case 0:
685 return "Fully available";
686 case 1:
687 return "1-17% utilized";
688 case 2:
689 return "17-33% utilized";
690 case 3:
691 return "33-50% utilized";
692 case 4:
693 return "50-67% utilized";
694 case 5:
695 return "67-83% utilized";
696 case 6:
697 return "83-99% utilized";
698 case 7:
699 return "No service available";
700 }
701 break;
702 case 4: /* audio/video */
703 switch (minor) {
704 case 0:
705 return "Uncategorized";
706 case 1:
707 return "Device conforms to the Headset profile";
708 case 2:
709 return "Hands-free";
710 /* 3 is reserved */
711 case 4:
712 return "Microphone";
713 case 5:
714 return "Loudspeaker";
715 case 6:
716 return "Headphones";
717 case 7:
718 return "Portable Audio";
719 case 8:
720 return "Car Audio";
721 case 9:
722 return "Set-top box";
723 case 10:
724 return "HiFi Audio Device";
725 case 11:
726 return "VCR";
727 case 12:
728 return "Video Camera";
729 case 13:
730 return "Camcorder";
731 case 14:
732 return "Video Monitor";
733 case 15:
734 return "Video Display and Loudspeaker";
735 case 16:
736 return "Video Conferencing";
737 /* 17 is reserved */
738 case 18:
739 return "Gaming/Toy";
740 }
741 break;
742 case 5: /* peripheral */ {
743 static char cls_str[48];
744
745 cls_str[0] = '\0';
746
747 switch (minor & 48) {
748 case 16:
749 strncpy(cls_str, "Keyboard", sizeof(cls_str));
750 break;
751 case 32:
752 strncpy(cls_str, "Pointing device", sizeof(cls_str));
753 break;
754 case 48:
755 strncpy(cls_str, "Combo keyboard/pointing device", sizeof(cls_str));
756 break;
757 }
758 if ((minor & 15) && (strlen(cls_str) > 0))
759 strcat(cls_str, "/");
760
761 switch (minor & 15) {
762 case 0:
763 break;
764 case 1:
765 strncat(cls_str, "Joystick", sizeof(cls_str) - strlen(cls_str));
766 break;
767 case 2:
768 strncat(cls_str, "Gamepad", sizeof(cls_str) - strlen(cls_str));
769 break;
770 case 3:
771 strncat(cls_str, "Remote control", sizeof(cls_str) - strlen(cls_str));
772 break;
773 case 4:
774 strncat(cls_str, "Sensing device", sizeof(cls_str) - strlen(cls_str));
775 break;
776 case 5:
777 strncat(cls_str, "Digitizer tablet", sizeof(cls_str) - strlen(cls_str));
778 break;
779 case 6:
780 strncat(cls_str, "Card reader", sizeof(cls_str) - strlen(cls_str));
781 break;
782 default:
783 strncat(cls_str, "(reserved)", sizeof(cls_str) - strlen(cls_str));
784 break;
785 }
786 if (strlen(cls_str) > 0)
787 return cls_str;
788 }
789 case 6: /* imaging */
790 if (minor & 4)
791 return "Display";
792 if (minor & 8)
793 return "Camera";
794 if (minor & 16)
795 return "Scanner";
796 if (minor & 32)
797 return "Printer";
798 break;
799 case 7: /* wearable */
800 switch (minor) {
801 case 1:
802 return "Wrist Watch";
803 case 2:
804 return "Pager";
805 case 3:
806 return "Jacket";
807 case 4:
808 return "Helmet";
809 case 5:
810 return "Glasses";
811 }
812 break;
813 case 8: /* toy */
814 switch (minor) {
815 case 1:
816 return "Robot";
817 case 2:
818 return "Vehicle";
819 case 3:
820 return "Doll / Action Figure";
821 case 4:
822 return "Controller";
823 case 5:
824 return "Game";
825 }
826 break;
827 case 63: /* uncategorised */
828 return "";
829 }
830 return "Unknown (reserved) minor device class";
831 }
832
cmd_class(int ctl,int hdev,char * opt)833 static void cmd_class(int ctl, int hdev, char *opt)
834 {
835 static const char *services[] = { "Positioning",
836 "Networking",
837 "Rendering",
838 "Capturing",
839 "Object Transfer",
840 "Audio",
841 "Telephony",
842 "Information" };
843 static const char *major_devices[] = { "Miscellaneous",
844 "Computer",
845 "Phone",
846 "LAN Access",
847 "Audio/Video",
848 "Peripheral",
849 "Imaging",
850 "Uncategorized" };
851 int s = hci_open_dev(hdev);
852
853 if (s < 0) {
854 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
855 hdev, strerror(errno), errno);
856 exit(1);
857 }
858 if (opt) {
859 uint32_t cod = strtoul(opt, NULL, 16);
860 if (hci_write_class_of_dev(s, cod, 2000) < 0) {
861 fprintf(stderr, "Can't write local class of device on hci%d: %s (%d)\n",
862 hdev, strerror(errno), errno);
863 exit(1);
864 }
865 } else {
866 uint8_t cls[3];
867 if (hci_read_class_of_dev(s, cls, 1000) < 0) {
868 fprintf(stderr, "Can't read class of device on hci%d: %s (%d)\n",
869 hdev, strerror(errno), errno);
870 exit(1);
871 }
872 print_dev_hdr(&di);
873 printf("\tClass: 0x%02x%02x%02x\n", cls[2], cls[1], cls[0]);
874 printf("\tService Classes: ");
875 if (cls[2]) {
876 unsigned int i;
877 int first = 1;
878 for (i = 0; i < (sizeof(services) / sizeof(*services)); i++)
879 if (cls[2] & (1 << i)) {
880 if (!first)
881 printf(", ");
882 printf("%s", services[i]);
883 first = 0;
884 }
885 } else
886 printf("Unspecified");
887 printf("\n\tDevice Class: ");
888 if ((cls[1] & 0x1f) >= sizeof(major_devices) / sizeof(*major_devices))
889 printf("Invalid Device Class!\n");
890 else
891 printf("%s, %s\n", major_devices[cls[1] & 0x1f],
892 get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));
893 }
894 }
895
cmd_voice(int ctl,int hdev,char * opt)896 static void cmd_voice(int ctl, int hdev, char *opt)
897 {
898 static char *icf[] = { "Linear",
899 "u-Law",
900 "A-Law",
901 "Reserved" };
902
903 static char *idf[] = { "1's complement",
904 "2's complement",
905 "Sign-Magnitude",
906 "Reserved" };
907
908 static char *iss[] = { "8 bit",
909 "16 bit" };
910
911 static char *acf[] = { "CVSD",
912 "u-Law",
913 "A-Law",
914 "Reserved" };
915
916 int s = hci_open_dev(hdev);
917
918 if (s < 0) {
919 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
920 hdev, strerror(errno), errno);
921 exit(1);
922 }
923 if (opt) {
924 uint16_t vs = htobs(strtoul(opt, NULL, 16));
925 if (hci_write_voice_setting(s, vs, 2000) < 0) {
926 fprintf(stderr, "Can't write voice setting on hci%d: %s (%d)\n",
927 hdev, strerror(errno), errno);
928 exit(1);
929 }
930 } else {
931 uint16_t vs;
932 uint8_t ic;
933 if (hci_read_voice_setting(s, &vs, 1000) < 0) {
934 fprintf(stderr, "Can't read voice setting on hci%d: %s (%d)\n",
935 hdev, strerror(errno), errno);
936 exit(1);
937 }
938 vs = htobs(vs);
939 ic = (vs & 0x0300) >> 8;
940 print_dev_hdr(&di);
941 printf("\tVoice setting: 0x%04x%s\n", vs,
942 ((vs & 0x03fc) == 0x0060) ? " (Default Condition)" : "");
943 printf("\tInput Coding: %s\n", icf[ic]);
944 printf("\tInput Data Format: %s\n", idf[(vs & 0xc0) >> 6]);
945
946 if (!ic) {
947 printf("\tInput Sample Size: %s\n",
948 iss[(vs & 0x20) >> 5]);
949 printf("\t# of bits padding at MSB: %d\n",
950 (vs & 0x1c) >> 2);
951 }
952 printf("\tAir Coding Format: %s\n", acf[vs & 0x03]);
953 }
954 }
955
get_link_key(const bdaddr_t * local,const bdaddr_t * peer,uint8_t * key)956 static int get_link_key(const bdaddr_t *local, const bdaddr_t *peer,
957 uint8_t *key)
958 {
959 char filename[PATH_MAX + 1], addr[18], tmp[3], *str;
960 int i;
961
962 ba2str(local, addr);
963 create_name(filename, PATH_MAX, STORAGEDIR, addr, "linkkeys");
964
965 ba2str(peer, addr);
966 str = textfile_get(filename, addr);
967 if (!str)
968 return -EIO;
969
970 memset(tmp, 0, sizeof(tmp));
971 for (i = 0; i < 16; i++) {
972 memcpy(tmp, str + (i * 2), 2);
973 key[i] = (uint8_t) strtol(tmp, NULL, 16);
974 }
975
976 free(str);
977
978 return 0;
979 }
980
cmd_putkey(int ctl,int hdev,char * opt)981 static void cmd_putkey(int ctl, int hdev, char *opt)
982 {
983 struct hci_dev_info di;
984 bdaddr_t bdaddr;
985 uint8_t key[16];
986 int dd;
987
988 if (!opt)
989 return;
990
991 dd = hci_open_dev(hdev);
992 if (dd < 0) {
993 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
994 hdev, strerror(errno), errno);
995 exit(1);
996 }
997
998 if (hci_devinfo(hdev, &di) < 0) {
999 fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n",
1000 hdev, strerror(errno), errno);
1001 exit(1);
1002 }
1003
1004 str2ba(opt, &bdaddr);
1005 if (get_link_key(&di.bdaddr, &bdaddr, key) < 0) {
1006 fprintf(stderr, "Can't find link key for %s on hci%d\n", opt, hdev);
1007 exit(1);
1008 }
1009
1010 if (hci_write_stored_link_key(dd, &bdaddr, key, 1000) < 0) {
1011 fprintf(stderr, "Can't write stored link key on hci%d: %s (%d)\n",
1012 hdev, strerror(errno), errno);
1013 exit(1);
1014 }
1015
1016 hci_close_dev(dd);
1017 }
1018
cmd_delkey(int ctl,int hdev,char * opt)1019 static void cmd_delkey(int ctl, int hdev, char *opt)
1020 {
1021 bdaddr_t bdaddr;
1022 uint8_t all;
1023 int dd;
1024
1025 if (!opt)
1026 return;
1027
1028 dd = hci_open_dev(hdev);
1029 if (dd < 0) {
1030 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1031 hdev, strerror(errno), errno);
1032 exit(1);
1033 }
1034
1035 if (!strcasecmp(opt, "all")) {
1036 bacpy(&bdaddr, BDADDR_ANY);
1037 all = 1;
1038 } else {
1039 str2ba(opt, &bdaddr);
1040 all = 0;
1041 }
1042
1043 if (hci_delete_stored_link_key(dd, &bdaddr, all, 1000) < 0) {
1044 fprintf(stderr, "Can't delete stored link key on hci%d: %s (%d)\n",
1045 hdev, strerror(errno), errno);
1046 exit(1);
1047 }
1048
1049 hci_close_dev(dd);
1050 }
1051
cmd_oob_data(int ctl,int hdev,char * opt)1052 static void cmd_oob_data(int ctl, int hdev, char *opt)
1053 {
1054 uint8_t hash[16], randomizer[16];
1055 int i, dd;
1056
1057 dd = hci_open_dev(hdev);
1058 if (dd < 0) {
1059 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1060 hdev, strerror(errno), errno);
1061 exit(1);
1062 }
1063
1064 if (hci_read_local_oob_data(dd, hash, randomizer, 1000) < 0) {
1065 fprintf(stderr, "Can't read local OOB data on hci%d: %s (%d)\n",
1066 hdev, strerror(errno), errno);
1067 exit(1);
1068 }
1069
1070 print_dev_hdr(&di);
1071 printf("\tOOB Hash: ");
1072 for (i = 0; i < 16; i++)
1073 printf(" %02x", hash[i]);
1074 printf("\n\tRandomizer:");
1075 for (i = 0; i < 16; i++)
1076 printf(" %02x", randomizer[i]);
1077 printf("\n");
1078
1079 hci_close_dev(dd);
1080 }
1081
cmd_commands(int ctl,int hdev,char * opt)1082 static void cmd_commands(int ctl, int hdev, char *opt)
1083 {
1084 uint8_t cmds[64];
1085 char *str;
1086 int i, n, dd;
1087
1088 dd = hci_open_dev(hdev);
1089 if (dd < 0) {
1090 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1091 hdev, strerror(errno), errno);
1092 exit(1);
1093 }
1094
1095 if (hci_read_local_commands(dd, cmds, 1000) < 0) {
1096 fprintf(stderr, "Can't read support commands on hci%d: %s (%d)\n",
1097 hdev, strerror(errno), errno);
1098 exit(1);
1099 }
1100
1101 print_dev_hdr(&di);
1102 for (i = 0; i < 64; i++) {
1103 if (!cmds[i])
1104 continue;
1105
1106 printf("%s Octet %-2d = 0x%02x (Bit",
1107 i ? "\t\t ": "\tCommands:", i, cmds[i]);
1108 for (n = 0; n < 8; n++)
1109 if (cmds[i] & (1 << n))
1110 printf(" %d", n);
1111 printf(")\n");
1112 }
1113
1114 str = hci_commandstostr(cmds, "\t", 71);
1115 printf("%s\n", str);
1116 bt_free(str);
1117
1118 hci_close_dev(dd);
1119 }
1120
cmd_version(int ctl,int hdev,char * opt)1121 static void cmd_version(int ctl, int hdev, char *opt)
1122 {
1123 struct hci_version ver;
1124 char *hciver, *lmpver;
1125 int dd;
1126
1127 dd = hci_open_dev(hdev);
1128 if (dd < 0) {
1129 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1130 hdev, strerror(errno), errno);
1131 exit(1);
1132 }
1133
1134 if (hci_read_local_version(dd, &ver, 1000) < 0) {
1135 fprintf(stderr, "Can't read version info hci%d: %s (%d)\n",
1136 hdev, strerror(errno), errno);
1137 exit(1);
1138 }
1139
1140 hciver = hci_vertostr(ver.hci_ver);
1141 lmpver = lmp_vertostr(ver.lmp_ver);
1142
1143 print_dev_hdr(&di);
1144 printf("\tHCI Version: %s (0x%x) Revision: 0x%x\n"
1145 "\tLMP Version: %s (0x%x) Subversion: 0x%x\n"
1146 "\tManufacturer: %s (%d)\n",
1147 hciver ? hciver : "n/a", ver.hci_ver, ver.hci_rev,
1148 lmpver ? lmpver : "n/a", ver.lmp_ver, ver.lmp_subver,
1149 bt_compidtostr(ver.manufacturer), ver.manufacturer);
1150
1151 if (hciver)
1152 bt_free(hciver);
1153 if (lmpver)
1154 bt_free(lmpver);
1155
1156 hci_close_dev(dd);
1157 }
1158
cmd_inq_tpl(int ctl,int hdev,char * opt)1159 static void cmd_inq_tpl(int ctl, int hdev, char *opt)
1160 {
1161 int dd;
1162
1163 dd = hci_open_dev(hdev);
1164 if (dd < 0) {
1165 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1166 hdev, strerror(errno), errno);
1167 exit(1);
1168 }
1169
1170 if (opt) {
1171 int8_t level = atoi(opt);
1172
1173 if (hci_write_inquiry_transmit_power_level(dd, level, 2000) < 0) {
1174 fprintf(stderr, "Can't set inquiry transmit power level on hci%d: %s (%d)\n",
1175 hdev, strerror(errno), errno);
1176 exit(1);
1177 }
1178 } else {
1179 int8_t level;
1180
1181 if (hci_read_inq_response_tx_power_level(dd, &level, 1000) < 0) {
1182 fprintf(stderr, "Can't read inquiry transmit power level on hci%d: %s (%d)\n",
1183 hdev, strerror(errno), errno);
1184 exit(1);
1185 }
1186
1187 print_dev_hdr(&di);
1188 printf("\tInquiry transmit power level: %d\n", level);
1189 }
1190
1191 hci_close_dev(dd);
1192 }
1193
cmd_inq_mode(int ctl,int hdev,char * opt)1194 static void cmd_inq_mode(int ctl, int hdev, char *opt)
1195 {
1196 int dd;
1197
1198 dd = hci_open_dev(hdev);
1199 if (dd < 0) {
1200 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1201 hdev, strerror(errno), errno);
1202 exit(1);
1203 }
1204
1205 if (opt) {
1206 uint8_t mode = atoi(opt);
1207
1208 if (hci_write_inquiry_mode(dd, mode, 2000) < 0) {
1209 fprintf(stderr, "Can't set inquiry mode on hci%d: %s (%d)\n",
1210 hdev, strerror(errno), errno);
1211 exit(1);
1212 }
1213 } else {
1214 uint8_t mode;
1215
1216 if (hci_read_inquiry_mode(dd, &mode, 1000) < 0) {
1217 fprintf(stderr, "Can't read inquiry mode on hci%d: %s (%d)\n",
1218 hdev, strerror(errno), errno);
1219 exit(1);
1220 }
1221
1222 print_dev_hdr(&di);
1223 printf("\tInquiry mode: ");
1224 switch (mode) {
1225 case 0:
1226 printf("Standard Inquiry\n");
1227 break;
1228 case 1:
1229 printf("Inquiry with RSSI\n");
1230 break;
1231 case 2:
1232 printf("Inquiry with RSSI or Extended Inquiry\n");
1233 break;
1234 default:
1235 printf("Unknown (0x%02x)\n", mode);
1236 break;
1237 }
1238 }
1239
1240 hci_close_dev(dd);
1241 }
1242
cmd_inq_data(int ctl,int hdev,char * opt)1243 static void cmd_inq_data(int ctl, int hdev, char *opt)
1244 {
1245 int i, dd;
1246
1247 dd = hci_open_dev(hdev);
1248 if (dd < 0) {
1249 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1250 hdev, strerror(errno), errno);
1251 exit(1);
1252 }
1253
1254 if (opt) {
1255 uint8_t fec = 0, data[HCI_MAX_EIR_LENGTH];
1256 char tmp[3];
1257 int i, size;
1258
1259 memset(data, 0, sizeof(data));
1260
1261 memset(tmp, 0, sizeof(tmp));
1262 size = (strlen(opt) + 1) / 2;
1263 if (size > HCI_MAX_EIR_LENGTH)
1264 size = HCI_MAX_EIR_LENGTH;
1265
1266 for (i = 0; i < size; i++) {
1267 memcpy(tmp, opt + (i * 2), 2);
1268 data[i] = strtol(tmp, NULL, 16);
1269 }
1270
1271 if (hci_write_ext_inquiry_response(dd, fec, data, 2000) < 0) {
1272 fprintf(stderr, "Can't set extended inquiry response on hci%d: %s (%d)\n",
1273 hdev, strerror(errno), errno);
1274 exit(1);
1275 }
1276 } else {
1277 uint8_t fec, data[HCI_MAX_EIR_LENGTH], len, type, *ptr;
1278 char *str;
1279
1280 if (hci_read_ext_inquiry_response(dd, &fec, data, 1000) < 0) {
1281 fprintf(stderr, "Can't read extended inquiry response on hci%d: %s (%d)\n",
1282 hdev, strerror(errno), errno);
1283 exit(1);
1284 }
1285
1286 print_dev_hdr(&di);
1287 printf("\tFEC %s\n\t\t", fec ? "enabled" : "disabled");
1288 for (i = 0; i < HCI_MAX_EIR_LENGTH; i++)
1289 printf("%02x%s%s", data[i], (i + 1) % 8 ? "" : " ",
1290 (i + 1) % 16 ? " " : (i < 239 ? "\n\t\t" : "\n"));
1291
1292 ptr = data;
1293 while (*ptr) {
1294 len = *ptr++;
1295 type = *ptr++;
1296 switch (type) {
1297 case 0x01:
1298 printf("\tFlags:");
1299 for (i = 0; i < len - 1; i++)
1300 printf(" 0x%2.2x", *((uint8_t *) (ptr + i)));
1301 printf("\n");
1302 break;
1303 case 0x02:
1304 case 0x03:
1305 printf("\t%s service classes:",
1306 type == 0x02 ? "Shortened" : "Complete");
1307 for (i = 0; i < (len - 1) / 2; i++) {
1308 uint16_t val = btohs(bt_get_unaligned((uint16_t *) (ptr + (i * 2))));
1309 printf(" 0x%4.4x", val);
1310 }
1311 printf("\n");
1312 break;
1313 case 0x08:
1314 case 0x09:
1315 str = malloc(len);
1316 if (str) {
1317 snprintf(str, len, "%s", ptr);
1318 for (i = 0; i < len - 1; i++) {
1319 if ((unsigned char) str[i] < 32 || str[i] == 127)
1320 str[i] = '.';
1321 }
1322 printf("\t%s local name: \'%s\'\n",
1323 type == 0x08 ? "Shortened" : "Complete", str);
1324 free(str);
1325 }
1326 break;
1327 case 0x0a:
1328 printf("\tTX power level: %d\n", *((int8_t *) ptr));
1329 break;
1330 case 0x10:
1331 printf("\tDevice ID with %d bytes data\n",
1332 len - 1);
1333 break;
1334 default:
1335 printf("\tUnknown type 0x%02x with %d bytes data\n",
1336 type, len - 1);
1337 break;
1338 }
1339
1340 ptr += (len - 1);
1341 }
1342
1343 printf("\n");
1344 }
1345
1346 hci_close_dev(dd);
1347 }
1348
cmd_inq_type(int ctl,int hdev,char * opt)1349 static void cmd_inq_type(int ctl, int hdev, char *opt)
1350 {
1351 int dd;
1352
1353 dd = hci_open_dev(hdev);
1354 if (dd < 0) {
1355 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1356 hdev, strerror(errno), errno);
1357 exit(1);
1358 }
1359
1360 if (opt) {
1361 uint8_t type = atoi(opt);
1362
1363 if (hci_write_inquiry_scan_type(dd, type, 2000) < 0) {
1364 fprintf(stderr, "Can't set inquiry scan type on hci%d: %s (%d)\n",
1365 hdev, strerror(errno), errno);
1366 exit(1);
1367 }
1368 } else {
1369 uint8_t type;
1370
1371 if (hci_read_inquiry_scan_type(dd, &type, 1000) < 0) {
1372 fprintf(stderr, "Can't read inquiry scan type on hci%d: %s (%d)\n",
1373 hdev, strerror(errno), errno);
1374 exit(1);
1375 }
1376
1377 print_dev_hdr(&di);
1378 printf("\tInquiry scan type: %s\n",
1379 type == 1 ? "Interlaced Inquiry Scan" : "Standard Inquiry Scan");
1380 }
1381
1382 hci_close_dev(dd);
1383 }
1384
cmd_inq_parms(int ctl,int hdev,char * opt)1385 static void cmd_inq_parms(int ctl, int hdev, char *opt)
1386 {
1387 struct hci_request rq;
1388 int s;
1389
1390 if ((s = hci_open_dev(hdev)) < 0) {
1391 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1392 hdev, strerror(errno), errno);
1393 exit(1);
1394 }
1395
1396 memset(&rq, 0, sizeof(rq));
1397
1398 if (opt) {
1399 unsigned int window, interval;
1400 write_inq_activity_cp cp;
1401
1402 if (sscanf(opt,"%4u:%4u", &window, &interval) != 2) {
1403 printf("Invalid argument format\n");
1404 exit(1);
1405 }
1406
1407 rq.ogf = OGF_HOST_CTL;
1408 rq.ocf = OCF_WRITE_INQ_ACTIVITY;
1409 rq.cparam = &cp;
1410 rq.clen = WRITE_INQ_ACTIVITY_CP_SIZE;
1411
1412 cp.window = htobs((uint16_t) window);
1413 cp.interval = htobs((uint16_t) interval);
1414
1415 if (window < 0x12 || window > 0x1000)
1416 printf("Warning: inquiry window out of range!\n");
1417
1418 if (interval < 0x12 || interval > 0x1000)
1419 printf("Warning: inquiry interval out of range!\n");
1420
1421 if (hci_send_req(s, &rq, 2000) < 0) {
1422 fprintf(stderr, "Can't set inquiry parameters name on hci%d: %s (%d)\n",
1423 hdev, strerror(errno), errno);
1424 exit(1);
1425 }
1426 } else {
1427 uint16_t window, interval;
1428 read_inq_activity_rp rp;
1429
1430 rq.ogf = OGF_HOST_CTL;
1431 rq.ocf = OCF_READ_INQ_ACTIVITY;
1432 rq.rparam = &rp;
1433 rq.rlen = READ_INQ_ACTIVITY_RP_SIZE;
1434
1435 if (hci_send_req(s, &rq, 1000) < 0) {
1436 fprintf(stderr, "Can't read inquiry parameters on hci%d: %s (%d)\n",
1437 hdev, strerror(errno), errno);
1438 exit(1);
1439 }
1440 if (rp.status) {
1441 printf("Read inquiry parameters on hci%d returned status %d\n",
1442 hdev, rp.status);
1443 exit(1);
1444 }
1445 print_dev_hdr(&di);
1446
1447 window = btohs(rp.window);
1448 interval = btohs(rp.interval);
1449 printf("\tInquiry interval: %u slots (%.2f ms), window: %u slots (%.2f ms)\n",
1450 interval, (float)interval * 0.625, window, (float)window * 0.625);
1451 }
1452 }
1453
cmd_page_parms(int ctl,int hdev,char * opt)1454 static void cmd_page_parms(int ctl, int hdev, char *opt)
1455 {
1456 struct hci_request rq;
1457 int s;
1458
1459 if ((s = hci_open_dev(hdev)) < 0) {
1460 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1461 hdev, strerror(errno), errno);
1462 exit(1);
1463 }
1464
1465 memset(&rq, 0, sizeof(rq));
1466
1467 if (opt) {
1468 unsigned int window, interval;
1469 write_page_activity_cp cp;
1470
1471 if (sscanf(opt,"%4u:%4u", &window, &interval) != 2) {
1472 printf("Invalid argument format\n");
1473 exit(1);
1474 }
1475
1476 rq.ogf = OGF_HOST_CTL;
1477 rq.ocf = OCF_WRITE_PAGE_ACTIVITY;
1478 rq.cparam = &cp;
1479 rq.clen = WRITE_PAGE_ACTIVITY_CP_SIZE;
1480
1481 cp.window = htobs((uint16_t) window);
1482 cp.interval = htobs((uint16_t) interval);
1483
1484 if (window < 0x12 || window > 0x1000)
1485 printf("Warning: page window out of range!\n");
1486
1487 if (interval < 0x12 || interval > 0x1000)
1488 printf("Warning: page interval out of range!\n");
1489
1490 if (hci_send_req(s, &rq, 2000) < 0) {
1491 fprintf(stderr, "Can't set page parameters name on hci%d: %s (%d)\n",
1492 hdev, strerror(errno), errno);
1493 exit(1);
1494 }
1495 } else {
1496 uint16_t window, interval;
1497 read_page_activity_rp rp;
1498
1499 rq.ogf = OGF_HOST_CTL;
1500 rq.ocf = OCF_READ_PAGE_ACTIVITY;
1501 rq.rparam = &rp;
1502 rq.rlen = READ_PAGE_ACTIVITY_RP_SIZE;
1503
1504 if (hci_send_req(s, &rq, 1000) < 0) {
1505 fprintf(stderr, "Can't read page parameters on hci%d: %s (%d)\n",
1506 hdev, strerror(errno), errno);
1507 exit(1);
1508 }
1509 if (rp.status) {
1510 printf("Read page parameters on hci%d returned status %d\n",
1511 hdev, rp.status);
1512 exit(1);
1513 }
1514 print_dev_hdr(&di);
1515
1516 window = btohs(rp.window);
1517 interval = btohs(rp.interval);
1518 printf("\tPage interval: %u slots (%.2f ms), "
1519 "window: %u slots (%.2f ms)\n",
1520 interval, (float)interval * 0.625,
1521 window, (float)window * 0.625);
1522 }
1523 }
1524
cmd_page_to(int ctl,int hdev,char * opt)1525 static void cmd_page_to(int ctl, int hdev, char *opt)
1526 {
1527 struct hci_request rq;
1528 int s;
1529
1530 if ((s = hci_open_dev(hdev)) < 0) {
1531 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1532 hdev, strerror(errno), errno);
1533 exit(1);
1534 }
1535
1536 memset(&rq, 0, sizeof(rq));
1537
1538 if (opt) {
1539 unsigned int timeout;
1540 write_page_timeout_cp cp;
1541
1542 if (sscanf(opt,"%5u", &timeout) != 1) {
1543 printf("Invalid argument format\n");
1544 exit(1);
1545 }
1546
1547 rq.ogf = OGF_HOST_CTL;
1548 rq.ocf = OCF_WRITE_PAGE_TIMEOUT;
1549 rq.cparam = &cp;
1550 rq.clen = WRITE_PAGE_TIMEOUT_CP_SIZE;
1551
1552 cp.timeout = htobs((uint16_t) timeout);
1553
1554 if (timeout < 0x01 || timeout > 0xFFFF)
1555 printf("Warning: page timeout out of range!\n");
1556
1557 if (hci_send_req(s, &rq, 2000) < 0) {
1558 fprintf(stderr, "Can't set page timeout on hci%d: %s (%d)\n",
1559 hdev, strerror(errno), errno);
1560 exit(1);
1561 }
1562 } else {
1563 uint16_t timeout;
1564 read_page_timeout_rp rp;
1565
1566 rq.ogf = OGF_HOST_CTL;
1567 rq.ocf = OCF_READ_PAGE_TIMEOUT;
1568 rq.rparam = &rp;
1569 rq.rlen = READ_PAGE_TIMEOUT_RP_SIZE;
1570
1571 if (hci_send_req(s, &rq, 1000) < 0) {
1572 fprintf(stderr, "Can't read page timeout on hci%d: %s (%d)\n",
1573 hdev, strerror(errno), errno);
1574 exit(1);
1575 }
1576 if (rp.status) {
1577 printf("Read page timeout on hci%d returned status %d\n",
1578 hdev, rp.status);
1579 exit(1);
1580 }
1581 print_dev_hdr(&di);
1582
1583 timeout = btohs(rp.timeout);
1584 printf("\tPage timeout: %u slots (%.2f ms)\n",
1585 timeout, (float)timeout * 0.625);
1586 }
1587 }
1588
cmd_afh_mode(int ctl,int hdev,char * opt)1589 static void cmd_afh_mode(int ctl, int hdev, char *opt)
1590 {
1591 int dd;
1592
1593 dd = hci_open_dev(hdev);
1594 if (dd < 0) {
1595 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1596 hdev, strerror(errno), errno);
1597 exit(1);
1598 }
1599
1600 if (opt) {
1601 uint8_t mode = atoi(opt);
1602
1603 if (hci_write_afh_mode(dd, mode, 2000) < 0) {
1604 fprintf(stderr, "Can't set AFH mode on hci%d: %s (%d)\n",
1605 hdev, strerror(errno), errno);
1606 exit(1);
1607 }
1608 } else {
1609 uint8_t mode;
1610
1611 if (hci_read_afh_mode(dd, &mode, 1000) < 0) {
1612 fprintf(stderr, "Can't read AFH mode on hci%d: %s (%d)\n",
1613 hdev, strerror(errno), errno);
1614 exit(1);
1615 }
1616
1617 print_dev_hdr(&di);
1618 printf("\tAFH mode: %s\n", mode == 1 ? "Enabled" : "Disabled");
1619 }
1620 }
1621
cmd_ssp_mode(int ctl,int hdev,char * opt)1622 static void cmd_ssp_mode(int ctl, int hdev, char *opt)
1623 {
1624 int dd;
1625
1626 dd = hci_open_dev(hdev);
1627 if (dd < 0) {
1628 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1629 hdev, strerror(errno), errno);
1630 exit(1);
1631 }
1632
1633 if (opt) {
1634 uint8_t mode = atoi(opt);
1635
1636 if (hci_write_simple_pairing_mode(dd, mode, 2000) < 0) {
1637 fprintf(stderr, "Can't set Simple Pairing mode on hci%d: %s (%d)\n",
1638 hdev, strerror(errno), errno);
1639 exit(1);
1640 }
1641 } else {
1642 uint8_t mode;
1643
1644 if (hci_read_simple_pairing_mode(dd, &mode, 1000) < 0) {
1645 fprintf(stderr, "Can't read Simple Pairing mode on hci%d: %s (%d)\n",
1646 hdev, strerror(errno), errno);
1647 exit(1);
1648 }
1649
1650 print_dev_hdr(&di);
1651 printf("\tSimple Pairing mode: %s\n",
1652 mode == 1 ? "Enabled" : "Disabled");
1653 }
1654 }
1655
print_rev_ericsson(int dd)1656 static void print_rev_ericsson(int dd)
1657 {
1658 struct hci_request rq;
1659 unsigned char buf[102];
1660
1661 memset(&rq, 0, sizeof(rq));
1662 rq.ogf = OGF_VENDOR_CMD;
1663 rq.ocf = 0x000f;
1664 rq.cparam = NULL;
1665 rq.clen = 0;
1666 rq.rparam = &buf;
1667 rq.rlen = sizeof(buf);
1668
1669 if (hci_send_req(dd, &rq, 1000) < 0) {
1670 printf("\nCan't read revision info: %s (%d)\n",
1671 strerror(errno), errno);
1672 return;
1673 }
1674
1675 printf("\t%s\n", buf + 1);
1676 }
1677
print_rev_csr(int dd,uint16_t rev)1678 static void print_rev_csr(int dd, uint16_t rev)
1679 {
1680 uint16_t buildid, chipver, chiprev, maxkeylen, mapsco;
1681
1682 if (csr_read_varid_uint16(dd, 0, CSR_VARID_BUILDID, &buildid) < 0) {
1683 printf("\t%s\n", csr_buildidtostr(rev));
1684 return;
1685 }
1686
1687 printf("\t%s\n", csr_buildidtostr(buildid));
1688
1689 if (!csr_read_varid_uint16(dd, 1, CSR_VARID_CHIPVER, &chipver)) {
1690 if (csr_read_varid_uint16(dd, 2, CSR_VARID_CHIPREV, &chiprev) < 0)
1691 chiprev = 0;
1692 printf("\tChip version: %s\n", csr_chipvertostr(chipver, chiprev));
1693 }
1694
1695 if (!csr_read_varid_uint16(dd, 3, CSR_VARID_MAX_CRYPT_KEY_LENGTH, &maxkeylen))
1696 printf("\tMax key size: %d bit\n", maxkeylen * 8);
1697
1698 if (!csr_read_pskey_uint16(dd, 4, CSR_PSKEY_HOSTIO_MAP_SCO_PCM, 0x0000, &mapsco))
1699 printf("\tSCO mapping: %s\n", mapsco ? "PCM" : "HCI");
1700 }
1701
print_rev_digianswer(int dd)1702 static void print_rev_digianswer(int dd)
1703 {
1704 struct hci_request rq;
1705 unsigned char req[] = { 0x07 };
1706 unsigned char buf[102];
1707
1708 memset(&rq, 0, sizeof(rq));
1709 rq.ogf = OGF_VENDOR_CMD;
1710 rq.ocf = 0x000e;
1711 rq.cparam = req;
1712 rq.clen = sizeof(req);
1713 rq.rparam = &buf;
1714 rq.rlen = sizeof(buf);
1715
1716 if (hci_send_req(dd, &rq, 1000) < 0) {
1717 printf("\nCan't read revision info: %s (%d)\n",
1718 strerror(errno), errno);
1719 return;
1720 }
1721
1722 printf("\t%s\n", buf + 1);
1723 }
1724
print_rev_broadcom(uint16_t hci_rev,uint16_t lmp_subver)1725 static void print_rev_broadcom(uint16_t hci_rev, uint16_t lmp_subver)
1726 {
1727 printf("\tFirmware %d.%d / %d\n",
1728 hci_rev & 0xff, lmp_subver >> 8, lmp_subver & 0xff);
1729 }
1730
print_rev_avm(uint16_t hci_rev,uint16_t lmp_subver)1731 static void print_rev_avm(uint16_t hci_rev, uint16_t lmp_subver)
1732 {
1733 if (lmp_subver == 0x01)
1734 printf("\tFirmware 03.%d.%d\n", hci_rev >> 8, hci_rev & 0xff);
1735 else
1736 printf("\tUnknown type\n");
1737 }
1738
cmd_revision(int ctl,int hdev,char * opt)1739 static void cmd_revision(int ctl, int hdev, char *opt)
1740 {
1741 struct hci_version ver;
1742 int dd;
1743
1744 dd = hci_open_dev(hdev);
1745 if (dd < 0) {
1746 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1747 hdev, strerror(errno), errno);
1748 return;
1749 }
1750
1751 if (hci_read_local_version(dd, &ver, 1000) < 0) {
1752 fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n",
1753 hdev, strerror(errno), errno);
1754 return;
1755 }
1756
1757 print_dev_hdr(&di);
1758 switch (ver.manufacturer) {
1759 case 0:
1760 case 37:
1761 case 48:
1762 print_rev_ericsson(dd);
1763 break;
1764 case 10:
1765 print_rev_csr(dd, ver.hci_rev);
1766 break;
1767 case 12:
1768 print_rev_digianswer(dd);
1769 break;
1770 case 15:
1771 print_rev_broadcom(ver.hci_rev, ver.lmp_subver);
1772 break;
1773 case 31:
1774 print_rev_avm(ver.hci_rev, ver.lmp_subver);
1775 break;
1776 default:
1777 printf("\tUnsupported manufacturer\n");
1778 break;
1779 }
1780 return;
1781 }
1782
cmd_block(int ctl,int hdev,char * opt)1783 static void cmd_block(int ctl, int hdev, char *opt)
1784 {
1785 bdaddr_t bdaddr;
1786 int dd;
1787
1788 if (!opt)
1789 return;
1790
1791 dd = hci_open_dev(hdev);
1792 if (dd < 0) {
1793 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1794 hdev, strerror(errno), errno);
1795 exit(1);
1796 }
1797
1798 str2ba(opt, &bdaddr);
1799
1800 if (ioctl(dd, HCIBLOCKADDR, &bdaddr) < 0) {
1801 perror("ioctl(HCIBLOCKADDR)");
1802 exit(1);
1803 }
1804
1805 hci_close_dev(dd);
1806 }
1807
cmd_unblock(int ctl,int hdev,char * opt)1808 static void cmd_unblock(int ctl, int hdev, char *opt)
1809 {
1810 bdaddr_t bdaddr;
1811 int dd;
1812
1813 if (!opt)
1814 return;
1815
1816 dd = hci_open_dev(hdev);
1817 if (dd < 0) {
1818 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1819 hdev, strerror(errno), errno);
1820 exit(1);
1821 }
1822
1823 if (!strcasecmp(opt, "all"))
1824 bacpy(&bdaddr, BDADDR_ANY);
1825 else
1826 str2ba(opt, &bdaddr);
1827
1828 if (ioctl(dd, HCIUNBLOCKADDR, &bdaddr) < 0) {
1829 perror("ioctl(HCIUNBLOCKADDR)");
1830 exit(1);
1831 }
1832
1833 hci_close_dev(dd);
1834 }
1835
print_dev_hdr(struct hci_dev_info * di)1836 static void print_dev_hdr(struct hci_dev_info *di)
1837 {
1838 static int hdr = -1;
1839 char addr[18];
1840
1841 if (hdr == di->dev_id)
1842 return;
1843 hdr = di->dev_id;
1844
1845 ba2str(&di->bdaddr, addr);
1846
1847 printf("%s:\tType: %s Bus: %s\n", di->name,
1848 hci_typetostr(di->type >> 4),
1849 hci_bustostr(di->type & 0x0f));
1850 printf("\tBD Address: %s ACL MTU: %d:%d SCO MTU: %d:%d\n",
1851 addr, di->acl_mtu, di->acl_pkts,
1852 di->sco_mtu, di->sco_pkts);
1853 }
1854
print_dev_info(int ctl,struct hci_dev_info * di)1855 static void print_dev_info(int ctl, struct hci_dev_info *di)
1856 {
1857 struct hci_dev_stats *st = &di->stat;
1858 char *str;
1859
1860 print_dev_hdr(di);
1861
1862 str = hci_dflagstostr(di->flags);
1863 printf("\t%s\n", str);
1864 bt_free(str);
1865
1866 printf("\tRX bytes:%d acl:%d sco:%d events:%d errors:%d\n",
1867 st->byte_rx, st->acl_rx, st->sco_rx, st->evt_rx, st->err_rx);
1868
1869 printf("\tTX bytes:%d acl:%d sco:%d commands:%d errors:%d\n",
1870 st->byte_tx, st->acl_tx, st->sco_tx, st->cmd_tx, st->err_tx);
1871
1872 if (all && !hci_test_bit(HCI_RAW, &di->flags) &&
1873 bacmp(&di->bdaddr, BDADDR_ANY)) {
1874 print_dev_features(di, 0);
1875 print_pkt_type(di);
1876 print_link_policy(di);
1877 print_link_mode(di);
1878
1879 if (hci_test_bit(HCI_UP, &di->flags)) {
1880 cmd_name(ctl, di->dev_id, NULL);
1881 cmd_class(ctl, di->dev_id, NULL);
1882 cmd_version(ctl, di->dev_id, NULL);
1883 }
1884 }
1885
1886 printf("\n");
1887 }
1888
1889 static struct {
1890 char *cmd;
1891 void (*func)(int ctl, int hdev, char *opt);
1892 char *opt;
1893 char *doc;
1894 } command[] = {
1895 { "up", cmd_up, 0, "Open and initialize HCI device" },
1896 { "down", cmd_down, 0, "Close HCI device" },
1897 { "reset", cmd_reset, 0, "Reset HCI device" },
1898 { "rstat", cmd_rstat, 0, "Reset statistic counters" },
1899 { "auth", cmd_auth, 0, "Enable Authentication" },
1900 { "noauth", cmd_auth, 0, "Disable Authentication" },
1901 { "encrypt", cmd_encrypt, 0, "Enable Encryption" },
1902 { "noencrypt", cmd_encrypt, 0, "Disable Encryption" },
1903 { "piscan", cmd_scan, 0, "Enable Page and Inquiry scan" },
1904 { "noscan", cmd_scan, 0, "Disable scan" },
1905 { "iscan", cmd_scan, 0, "Enable Inquiry scan" },
1906 { "pscan", cmd_scan, 0, "Enable Page scan" },
1907 { "ptype", cmd_ptype, "[type]", "Get/Set default packet type" },
1908 { "lm", cmd_lm, "[mode]", "Get/Set default link mode" },
1909 { "lp", cmd_lp, "[policy]", "Get/Set default link policy" },
1910 { "name", cmd_name, "[name]", "Get/Set local name" },
1911 { "class", cmd_class, "[class]", "Get/Set class of device" },
1912 { "voice", cmd_voice, "[voice]", "Get/Set voice setting" },
1913 { "iac", cmd_iac, "[iac]", "Get/Set inquiry access code" },
1914 { "inqtpl", cmd_inq_tpl, "[level]", "Get/Set inquiry transmit power level" },
1915 { "inqmode", cmd_inq_mode, "[mode]", "Get/Set inquiry mode" },
1916 { "inqdata", cmd_inq_data, "[data]", "Get/Set inquiry data" },
1917 { "inqtype", cmd_inq_type, "[type]", "Get/Set inquiry scan type" },
1918 { "inqparms", cmd_inq_parms, "[win:int]", "Get/Set inquiry scan window and interval" },
1919 { "pageparms", cmd_page_parms, "[win:int]", "Get/Set page scan window and interval" },
1920 { "pageto", cmd_page_to, "[to]", "Get/Set page timeout" },
1921 { "afhmode", cmd_afh_mode, "[mode]", "Get/Set AFH mode" },
1922 { "sspmode", cmd_ssp_mode, "[mode]", "Get/Set Simple Pairing Mode" },
1923 { "aclmtu", cmd_aclmtu, "<mtu:pkt>", "Set ACL MTU and number of packets" },
1924 { "scomtu", cmd_scomtu, "<mtu:pkt>", "Set SCO MTU and number of packets" },
1925 { "putkey", cmd_putkey, "<bdaddr>", "Store link key on the device" },
1926 { "delkey", cmd_delkey, "<bdaddr>", "Delete link key from the device" },
1927 { "oobdata", cmd_oob_data, 0, "Display local OOB data" },
1928 { "commands", cmd_commands, 0, "Display supported commands" },
1929 { "features", cmd_features, 0, "Display device features" },
1930 { "version", cmd_version, 0, "Display version information" },
1931 { "revision", cmd_revision, 0, "Display revision information" },
1932 { "block", cmd_block, "<bdaddr>", "Add a device to the blacklist" },
1933 { "unblock", cmd_unblock, "<bdaddr>", "Remove a device from the blacklist" },
1934 { "lerandaddr", cmd_le_addr, "<bdaddr>", "Set LE Random Address" },
1935 { "leadv", cmd_le_adv, 0, "Enable LE advertising" },
1936 { "noleadv", cmd_le_adv, 0, "Disable LE advertising" },
1937 { "lestates", cmd_le_states, 0, "Display the supported LE states" },
1938 { NULL, NULL, 0 }
1939 };
1940
usage(void)1941 static void usage(void)
1942 {
1943 int i;
1944
1945 printf("hciconfig - HCI device configuration utility\n");
1946 printf("Usage:\n"
1947 "\thciconfig\n"
1948 "\thciconfig [-a] hciX [command ...]\n");
1949 printf("Commands:\n");
1950 for (i = 0; command[i].cmd; i++)
1951 printf("\t%-10s %-8s\t%s\n", command[i].cmd,
1952 command[i].opt ? command[i].opt : " ",
1953 command[i].doc);
1954 }
1955
1956 static struct option main_options[] = {
1957 { "help", 0, 0, 'h' },
1958 { "all", 0, 0, 'a' },
1959 { 0, 0, 0, 0 }
1960 };
1961
main(int argc,char * argv[])1962 int main(int argc, char *argv[])
1963 {
1964 int opt, ctl, i, cmd = 0;
1965
1966 while ((opt = getopt_long(argc, argv, "ah", main_options, NULL)) != -1) {
1967 switch (opt) {
1968 case 'a':
1969 all = 1;
1970 break;
1971
1972 case 'h':
1973 default:
1974 usage();
1975 exit(0);
1976 }
1977 }
1978
1979 argc -= optind;
1980 argv += optind;
1981 optind = 0;
1982
1983 /* Open HCI socket */
1984 if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
1985 perror("Can't open HCI socket.");
1986 exit(1);
1987 }
1988
1989 if (argc < 1) {
1990 print_dev_list(ctl, 0);
1991 exit(0);
1992 }
1993
1994 di.dev_id = atoi(argv[0] + 3);
1995 argc--; argv++;
1996
1997 if (ioctl(ctl, HCIGETDEVINFO, (void *) &di)) {
1998 perror("Can't get device info");
1999 exit(1);
2000 }
2001
2002 if (hci_test_bit(HCI_RAW, &di.flags) &&
2003 !bacmp(&di.bdaddr, BDADDR_ANY)) {
2004 int dd = hci_open_dev(di.dev_id);
2005 hci_read_bd_addr(dd, &di.bdaddr, 1000);
2006 hci_close_dev(dd);
2007 }
2008
2009 while (argc > 0) {
2010 for (i = 0; command[i].cmd; i++) {
2011 if (strncmp(command[i].cmd,
2012 *argv, strlen(command[i].cmd)))
2013 continue;
2014
2015 if (command[i].opt) {
2016 argc--; argv++;
2017 }
2018
2019 command[i].func(ctl, di.dev_id, *argv);
2020 cmd = 1;
2021 break;
2022 }
2023
2024 if (command[i].cmd == 0)
2025 fprintf(stderr, "Warning: unknown command - \"%s\"\n",
2026 *argv);
2027
2028 argc--; argv++;
2029 }
2030
2031 if (!cmd)
2032 print_dev_info(ctl, &di);
2033
2034 close(ctl);
2035 return 0;
2036 }
2037