3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2000-2001 Qualcomm Incorporated
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2002-2009 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/hci.h>
43 #include <bluetooth/hci_lib.h>
48 static struct hci_dev_info di;
51 static void print_dev_hdr(struct hci_dev_info *di);
52 static void print_dev_info(int ctl, struct hci_dev_info *di);
54 static void print_dev_list(int ctl, int flags)
56 struct hci_dev_list_req *dl;
57 struct hci_dev_req *dr;
60 if (!(dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)))) {
61 perror("Can't allocate memory");
64 dl->dev_num = HCI_MAX_DEV;
67 if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) {
68 perror("Can't get device list");
72 for (i = 0; i< dl->dev_num; i++) {
73 di.dev_id = (dr+i)->dev_id;
74 if (ioctl(ctl, HCIGETDEVINFO, (void *) &di) < 0)
76 if (hci_test_bit(HCI_RAW, &di.flags) &&
77 !bacmp(&di.bdaddr, BDADDR_ANY)) {
78 int dd = hci_open_dev(di.dev_id);
79 hci_read_bd_addr(dd, &di.bdaddr, 1000);
82 print_dev_info(ctl, &di);
86 static void print_pkt_type(struct hci_dev_info *di)
89 str = hci_ptypetostr(di->pkt_type);
90 printf("\tPacket type: %s\n", str);
94 static void print_link_policy(struct hci_dev_info *di)
96 printf("\tLink policy: %s\n", hci_lptostr(di->link_policy));
99 static void print_link_mode(struct hci_dev_info *di)
102 str = hci_lmtostr(di->link_mode);
103 printf("\tLink mode: %s\n", str);
107 static void print_dev_features(struct hci_dev_info *di, int format)
109 printf("\tFeatures: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
110 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
111 di->features[0], di->features[1], di->features[2],
112 di->features[3], di->features[4], di->features[5],
113 di->features[6], di->features[7]);
116 char *tmp = lmp_featurestostr(di->features, "\t\t", 63);
122 static void cmd_rstat(int ctl, int hdev, char *opt)
124 /* Reset HCI device stat counters */
125 if (ioctl(ctl, HCIDEVRESTAT, hdev) < 0) {
126 fprintf(stderr, "Can't reset stats counters hci%d: %s (%d)\n",
127 hdev, strerror(errno), errno);
132 static void cmd_scan(int ctl, int hdev, char *opt)
134 struct hci_dev_req dr;
137 dr.dev_opt = SCAN_DISABLED;
138 if (!strcmp(opt, "iscan"))
139 dr.dev_opt = SCAN_INQUIRY;
140 else if (!strcmp(opt, "pscan"))
141 dr.dev_opt = SCAN_PAGE;
142 else if (!strcmp(opt, "piscan"))
143 dr.dev_opt = SCAN_PAGE | SCAN_INQUIRY;
145 if (ioctl(ctl, HCISETSCAN, (unsigned long) &dr) < 0) {
146 fprintf(stderr, "Can't set scan mode on hci%d: %s (%d)\n",
147 hdev, strerror(errno), errno);
152 static void cmd_iac(int ctl, int hdev, char *opt)
154 int s = hci_open_dev(hdev);
157 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
158 hdev, strerror(errno), errno);
162 int l = strtoul(opt, 0, 16);
164 if (!strcasecmp(opt, "giac")) {
166 } else if (!strcasecmp(opt, "liac")) {
168 } else if (l < 0x9e8b00 || l > 0x9e8b3f) {
169 printf("Invalid access code 0x%x\n", l);
173 lap[1] = (l >> 8) & 0xff;
174 lap[2] = (l >> 16) & 0xff;
175 if (hci_write_current_iac_lap(s, 1, lap, 1000) < 0) {
176 printf("Failed to set IAC on hci%d: %s\n", hdev, strerror(errno));
180 uint8_t lap[3 * MAX_IAC_LAP];
183 if (hci_read_current_iac_lap(s, &n, lap, 1000) < 0) {
184 printf("Failed to read IAC from hci%d: %s\n", hdev, strerror(errno));
189 for (i = 0; i < n; i++) {
192 printf("%02x", lap[j + 3 * i]);
201 static void cmd_auth(int ctl, int hdev, char *opt)
203 struct hci_dev_req dr;
206 if (!strcmp(opt, "auth"))
207 dr.dev_opt = AUTH_ENABLED;
209 dr.dev_opt = AUTH_DISABLED;
211 if (ioctl(ctl, HCISETAUTH, (unsigned long) &dr) < 0) {
212 fprintf(stderr, "Can't set auth on hci%d: %s (%d)\n",
213 hdev, strerror(errno), errno);
218 static void cmd_encrypt(int ctl, int hdev, char *opt)
220 struct hci_dev_req dr;
223 if (!strcmp(opt, "encrypt"))
224 dr.dev_opt = ENCRYPT_P2P;
226 dr.dev_opt = ENCRYPT_DISABLED;
228 if (ioctl(ctl, HCISETENCRYPT, (unsigned long) &dr) < 0) {
229 fprintf(stderr, "Can't set encrypt on hci%d: %s (%d)\n",
230 hdev, strerror(errno), errno);
235 static void cmd_up(int ctl, int hdev, char *opt)
237 /* Start HCI device */
238 if (ioctl(ctl, HCIDEVUP, hdev) < 0) {
239 if (errno == EALREADY)
241 fprintf(stderr, "Can't init device hci%d: %s (%d)\n",
242 hdev, strerror(errno), errno);
247 static void cmd_down(int ctl, int hdev, char *opt)
249 /* Stop HCI device */
250 if (ioctl(ctl, HCIDEVDOWN, hdev) < 0) {
251 fprintf(stderr, "Can't down device hci%d: %s (%d)\n",
252 hdev, strerror(errno), errno);
257 static void cmd_reset(int ctl, int hdev, char *opt)
259 /* Reset HCI device */
261 if (ioctl(ctl, HCIDEVRESET, hdev) < 0 ){
262 fprintf(stderr, "Reset failed for device hci%d: %s (%d)\n",
263 hdev, strerror(errno), errno);
267 cmd_down(ctl, hdev, "down");
268 cmd_up(ctl, hdev, "up");
271 static void cmd_ptype(int ctl, int hdev, char *opt)
273 struct hci_dev_req dr;
277 if (hci_strtoptype(opt, &dr.dev_opt)) {
278 if (ioctl(ctl, HCISETPTYPE, (unsigned long) &dr) < 0) {
279 fprintf(stderr, "Can't set pkttype on hci%d: %s (%d)\n",
280 hdev, strerror(errno), errno);
289 static void cmd_lp(int ctl, int hdev, char *opt)
291 struct hci_dev_req dr;
295 if (hci_strtolp(opt, &dr.dev_opt)) {
296 if (ioctl(ctl, HCISETLINKPOL, (unsigned long) &dr) < 0) {
297 fprintf(stderr, "Can't set link policy on hci%d: %s (%d)\n",
298 hdev, strerror(errno), errno);
303 print_link_policy(&di);
307 static void cmd_lm(int ctl, int hdev, char *opt)
309 struct hci_dev_req dr;
313 if (hci_strtolm(opt, &dr.dev_opt)) {
314 if (ioctl(ctl, HCISETLINKMODE, (unsigned long) &dr) < 0) {
315 fprintf(stderr, "Can't set default link mode on hci%d: %s (%d)\n",
316 hdev, strerror(errno), errno);
321 print_link_mode(&di);
325 static void cmd_aclmtu(int ctl, int hdev, char *opt)
327 struct hci_dev_req dr = { dev_id: hdev };
333 if (sscanf(opt, "%4hu:%4hu", &mtu, &mpkt) != 2)
336 dr.dev_opt = htobl(htobs(mpkt) | (htobs(mtu) << 16));
338 if (ioctl(ctl, HCISETACLMTU, (unsigned long) &dr) < 0) {
339 fprintf(stderr, "Can't set ACL mtu on hci%d: %s(%d)\n",
340 hdev, strerror(errno), errno);
345 static void cmd_scomtu(int ctl, int hdev, char *opt)
347 struct hci_dev_req dr = { dev_id: hdev };
353 if (sscanf(opt, "%4hu:%4hu", &mtu, &mpkt) != 2)
356 dr.dev_opt = htobl(htobs(mpkt) | (htobs(mtu) << 16));
358 if (ioctl(ctl, HCISETSCOMTU, (unsigned long) &dr) < 0) {
359 fprintf(stderr, "Can't set SCO mtu on hci%d: %s (%d)\n",
360 hdev, strerror(errno), errno);
365 static void cmd_features(int ctl, int hdev, char *opt)
367 uint8_t features[8], max_page = 0;
371 if (!(di.features[7] & LMP_EXT_FEAT)) {
373 print_dev_features(&di, 1);
377 dd = hci_open_dev(hdev);
379 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
380 hdev, strerror(errno), errno);
384 if (hci_read_local_ext_features(dd, 0, &max_page, features, 1000) < 0) {
385 fprintf(stderr, "Can't read extended features hci%d: %s (%d)\n",
386 hdev, strerror(errno), errno);
391 printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
392 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
393 (max_page > 0) ? " page 0" : "",
394 features[0], features[1], features[2], features[3],
395 features[4], features[5], features[6], features[7]);
397 tmp = lmp_featurestostr(di.features, "\t\t", 63);
401 for (i = 1; i <= max_page; i++) {
402 if (hci_read_local_ext_features(dd, i, NULL,
406 printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
407 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i,
408 features[0], features[1], features[2], features[3],
409 features[4], features[5], features[6], features[7]);
415 static void cmd_name(int ctl, int hdev, char *opt)
419 dd = hci_open_dev(hdev);
421 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
422 hdev, strerror(errno), errno);
427 if (hci_write_local_name(dd, opt, 2000) < 0) {
428 fprintf(stderr, "Can't change local name on hci%d: %s (%d)\n",
429 hdev, strerror(errno), errno);
436 if (hci_read_local_name(dd, sizeof(name), name, 1000) < 0) {
437 fprintf(stderr, "Can't read local name on hci%d: %s (%d)\n",
438 hdev, strerror(errno), errno);
442 for (i = 0; i < 248 && name[i]; i++) {
443 if ((unsigned char) name[i] < 32 || name[i] == 127)
450 printf("\tName: '%s'\n", name);
457 * see http://www.bluetooth.org/assigned-numbers/baseband.htm --- all
458 * strings are reproduced verbatim
460 static char *get_minor_device_name(int major, int minor)
465 case 1: /* computer */
468 return "Uncategorized";
470 return "Desktop workstation";
486 return "Uncategorized";
492 return "Smart phone";
494 return "Wired modem or voice gateway";
496 return "Common ISDN Access";
498 return "Sim Card Reader";
501 case 3: /* lan access */
503 return "Uncategorized";
506 return "Fully available";
508 return "1-17% utilized";
510 return "17-33% utilized";
512 return "33-50% utilized";
514 return "50-67% utilized";
516 return "67-83% utilized";
518 return "83-99% utilized";
520 return "No service available";
523 case 4: /* audio/video */
526 return "Uncategorized";
528 return "Device conforms to the Headset profile";
535 return "Loudspeaker";
539 return "Portable Audio";
543 return "Set-top box";
545 return "HiFi Audio Device";
549 return "Video Camera";
553 return "Video Monitor";
555 return "Video Display and Loudspeaker";
557 return "Video Conferencing";
563 case 5: /* peripheral */ {
564 static char cls_str[48];
570 strncpy(cls_str, "Keyboard", sizeof(cls_str));
573 strncpy(cls_str, "Pointing device", sizeof(cls_str));
576 strncpy(cls_str, "Combo keyboard/pointing device", sizeof(cls_str));
579 if((minor & 15) && (strlen(cls_str) > 0))
580 strcat(cls_str, "/");
586 strncat(cls_str, "Joystick", sizeof(cls_str) - strlen(cls_str));
589 strncat(cls_str, "Gamepad", sizeof(cls_str) - strlen(cls_str));
592 strncat(cls_str, "Remote control", sizeof(cls_str) - strlen(cls_str));
595 strncat(cls_str, "Sensing device", sizeof(cls_str) - strlen(cls_str));
598 strncat(cls_str, "Digitizer tablet", sizeof(cls_str) - strlen(cls_str));
601 strncat(cls_str, "Card reader", sizeof(cls_str) - strlen(cls_str));
604 strncat(cls_str, "(reserved)", sizeof(cls_str) - strlen(cls_str));
607 if(strlen(cls_str) > 0)
610 case 6: /* imaging */
620 case 7: /* wearable */
623 return "Wrist Watch";
641 return "Doll / Action Figure";
648 case 63: /* uncategorised */
651 return "Unknown (reserved) minor device class";
654 static void cmd_class(int ctl, int hdev, char *opt)
656 static const char *services[] = { "Positioning",
664 static const char *major_devices[] = { "Miscellaneous",
672 int s = hci_open_dev(hdev);
675 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
676 hdev, strerror(errno), errno);
680 uint32_t cod = strtoul(opt, NULL, 16);
681 if (hci_write_class_of_dev(s, cod, 2000) < 0) {
682 fprintf(stderr, "Can't write local class of device on hci%d: %s (%d)\n",
683 hdev, strerror(errno), errno);
688 if (hci_read_class_of_dev(s, cls, 1000) < 0) {
689 fprintf(stderr, "Can't read class of device on hci%d: %s (%d)\n",
690 hdev, strerror(errno), errno);
694 printf("\tClass: 0x%02x%02x%02x\n", cls[2], cls[1], cls[0]);
695 printf("\tService Classes: ");
699 for (i = 0; i < (sizeof(services) / sizeof(*services)); i++)
700 if (cls[2] & (1 << i)) {
703 printf("%s", services[i]);
707 printf("Unspecified");
708 printf("\n\tDevice Class: ");
709 if ((cls[1] & 0x1f) >= sizeof(major_devices) / sizeof(*major_devices))
710 printf("Invalid Device Class!\n");
712 printf("%s, %s\n", major_devices[cls[1] & 0x1f],
713 get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));
717 static void cmd_voice(int ctl, int hdev, char *opt)
719 static char *icf[] = { "Linear", "u-Law", "A-Law", "Reserved" };
720 static char *idf[] = { "1's complement", "2's complement", "Sign-Magnitude", "Reserved" };
721 static char *iss[] = { "8 bit", "16 bit" };
722 static char *acf[] = { "CVSD", "u-Law", "A-Law", "Reserved" };
723 int s = hci_open_dev(hdev);
726 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
727 hdev, strerror(errno), errno);
731 uint16_t vs = htobs(strtoul(opt, NULL, 16));
732 if (hci_write_voice_setting(s, vs, 2000) < 0) {
733 fprintf(stderr, "Can't write voice setting on hci%d: %s (%d)\n",
734 hdev, strerror(errno), errno);
740 if (hci_read_voice_setting(s, &vs, 1000) < 0) {
741 fprintf(stderr, "Can't read voice setting on hci%d: %s (%d)\n",
742 hdev, strerror(errno), errno);
746 ic = (vs & 0x0300) >> 8;
748 printf("\tVoice setting: 0x%04x%s\n", vs,
749 ((vs & 0x03fc) == 0x0060) ? " (Default Condition)" : "");
750 printf("\tInput Coding: %s\n", icf[ic]);
751 printf("\tInput Data Format: %s\n", idf[(vs & 0xc0) >> 6]);
753 printf("\tInput Sample Size: %s\n", iss[(vs & 0x20) >> 5]);
754 printf("\t# of bits padding at MSB: %d\n", (vs & 0x1c) >> 2);
756 printf("\tAir Coding Format: %s\n", acf[vs & 0x03]);
760 static int get_link_key(const bdaddr_t *local, const bdaddr_t *peer, uint8_t *key)
762 char filename[PATH_MAX + 1], addr[18], tmp[3], *str;
766 create_name(filename, PATH_MAX, STORAGEDIR, addr, "linkkeys");
769 str = textfile_get(filename, addr);
773 memset(tmp, 0, sizeof(tmp));
774 for (i = 0; i < 16; i++) {
775 memcpy(tmp, str + (i * 2), 2);
776 key[i] = (uint8_t) strtol(tmp, NULL, 16);
784 static void cmd_putkey(int ctl, int hdev, char *opt)
786 struct hci_dev_info di;
794 dd = hci_open_dev(hdev);
796 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
797 hdev, strerror(errno), errno);
801 if (hci_devinfo(hdev, &di) < 0) {
802 fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n",
803 hdev, strerror(errno), errno);
807 str2ba(opt, &bdaddr);
808 if (get_link_key(&di.bdaddr, &bdaddr, key) < 0) {
809 fprintf(stderr, "Can't find link key for %s on hci%d\n", opt, hdev);
813 if (hci_write_stored_link_key(dd, &bdaddr, key, 1000) < 0) {
814 fprintf(stderr, "Can't write stored link key on hci%d: %s (%d)\n",
815 hdev, strerror(errno), errno);
822 static void cmd_delkey(int ctl, int hdev, char *opt)
831 dd = hci_open_dev(hdev);
833 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
834 hdev, strerror(errno), errno);
838 if (!strcasecmp(opt, "all")) {
839 bacpy(&bdaddr, BDADDR_ANY);
842 str2ba(opt, &bdaddr);
846 if (hci_delete_stored_link_key(dd, &bdaddr, all, 1000) < 0) {
847 fprintf(stderr, "Can't delete stored link key on hci%d: %s (%d)\n",
848 hdev, strerror(errno), errno);
855 static void cmd_oob_data(int ctl, int hdev, char *opt)
857 uint8_t hash[16], randomizer[16];
860 dd = hci_open_dev(hdev);
862 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
863 hdev, strerror(errno), errno);
867 if (hci_read_local_oob_data(dd, hash, randomizer, 1000) < 0) {
868 fprintf(stderr, "Can't read local OOB data on hci%d: %s (%d)\n",
869 hdev, strerror(errno), errno);
874 printf("\tOOB Hash: ");
875 for (i = 0; i < 16; i++)
876 printf(" %02x", hash[i]);
877 printf("\n\tRandomizer:");
878 for (i = 0; i < 16; i++)
879 printf(" %02x", randomizer[i]);
885 static void cmd_commands(int ctl, int hdev, char *opt)
891 dd = hci_open_dev(hdev);
893 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
894 hdev, strerror(errno), errno);
898 if (hci_read_local_commands(dd, cmds, 1000) < 0) {
899 fprintf(stderr, "Can't read support commands on hci%d: %s (%d)\n",
900 hdev, strerror(errno), errno);
905 for (i = 0; i < 64; i++) {
909 printf("%s Octet %-2d = 0x%02x (Bit",
910 i ? "\t\t ": "\tCommands:", i, cmds[i]);
911 for (n = 0; n < 8; n++)
912 if (cmds[i] & (1 << n))
917 str = hci_commandstostr(cmds, "\t", 71);
924 static void cmd_version(int ctl, int hdev, char *opt)
926 struct hci_version ver;
927 char *hciver, *lmpver;
930 dd = hci_open_dev(hdev);
932 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
933 hdev, strerror(errno), errno);
937 if (hci_read_local_version(dd, &ver, 1000) < 0) {
938 fprintf(stderr, "Can't read version info hci%d: %s (%d)\n",
939 hdev, strerror(errno), errno);
943 hciver = hci_vertostr(ver.hci_ver);
944 lmpver = lmp_vertostr(ver.hci_ver);
947 printf("\tHCI Ver: %s (0x%x) HCI Rev: 0x%x LMP Ver: %s (0x%x) LMP Subver: 0x%x\n"
948 "\tManufacturer: %s (%d)\n",
949 hciver ? hciver : "n/a", ver.hci_ver, ver.hci_rev,
950 lmpver ? lmpver : "n/a", ver.lmp_ver, ver.lmp_subver,
951 bt_compidtostr(ver.manufacturer), ver.manufacturer);
961 static void cmd_inq_tpl(int ctl, int hdev, char *opt)
965 dd = hci_open_dev(hdev);
967 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
968 hdev, strerror(errno), errno);
973 int8_t level = atoi(opt);
975 if (hci_write_inquiry_transmit_power_level(dd, level, 2000) < 0) {
976 fprintf(stderr, "Can't set inquiry transmit power level on hci%d: %s (%d)\n",
977 hdev, strerror(errno), errno);
983 if (hci_read_inquiry_transmit_power_level(dd, &level, 1000) < 0) {
984 fprintf(stderr, "Can't read inquiry transmit power level on hci%d: %s (%d)\n",
985 hdev, strerror(errno), errno);
990 printf("\tInquiry transmit power level: %d\n", level);
996 static void cmd_inq_mode(int ctl, int hdev, char *opt)
1000 dd = hci_open_dev(hdev);
1002 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1003 hdev, strerror(errno), errno);
1008 uint8_t mode = atoi(opt);
1010 if (hci_write_inquiry_mode(dd, mode, 2000) < 0) {
1011 fprintf(stderr, "Can't set inquiry mode on hci%d: %s (%d)\n",
1012 hdev, strerror(errno), errno);
1018 if (hci_read_inquiry_mode(dd, &mode, 1000) < 0) {
1019 fprintf(stderr, "Can't read inquiry mode on hci%d: %s (%d)\n",
1020 hdev, strerror(errno), errno);
1025 printf("\tInquiry mode: ");
1028 printf("Standard Inquiry\n");
1031 printf("Inquiry with RSSI\n");
1034 printf("Inquiry with RSSI or Extended Inquiry\n");
1037 printf("Unknown (0x%02x)\n", mode);
1045 static void cmd_inq_data(int ctl, int hdev, char *opt)
1049 dd = hci_open_dev(hdev);
1051 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1052 hdev, strerror(errno), errno);
1057 uint8_t fec = 0, data[240];
1061 memset(data, 0, sizeof(data));
1063 memset(tmp, 0, sizeof(tmp));
1064 size = (strlen(opt) + 1) / 2;
1068 for (i = 0; i < size; i++) {
1069 memcpy(tmp, opt + (i * 2), 2);
1070 data[i] = strtol(tmp, NULL, 16);
1073 if (hci_write_ext_inquiry_response(dd, fec, data, 2000) < 0) {
1074 fprintf(stderr, "Can't set extended inquiry response on hci%d: %s (%d)\n",
1075 hdev, strerror(errno), errno);
1079 uint8_t fec, data[240], len, type, *ptr;
1082 if (hci_read_ext_inquiry_response(dd, &fec, data, 1000) < 0) {
1083 fprintf(stderr, "Can't read extended inquiry response on hci%d: %s (%d)\n",
1084 hdev, strerror(errno), errno);
1089 printf("\tFEC %s\n\t\t", fec ? "enabled" : "disabled");
1090 for (i = 0; i < 240; i++)
1091 printf("%02x%s%s", data[i], (i + 1) % 8 ? "" : " ",
1092 (i + 1) % 16 ? " " : (i < 239 ? "\n\t\t" : "\n"));
1101 for (i = 0; i < len - 1; i++)
1102 printf(" 0x%2.2x", *((uint8_t *) (ptr + i)));
1107 printf("\t%s service classes:",
1108 type == 0x02 ? "Shortened" : "Complete");
1109 for (i = 0; i < (len - 1) / 2; i++) {
1110 uint16_t val = btohs(bt_get_unaligned((uint16_t *) (ptr + (i * 2))));
1111 printf(" 0x%4.4x", val);
1119 snprintf(str, len, "%s", ptr);
1120 for (i = 0; i < len - 1; i++) {
1121 if ((unsigned char) str[i] < 32 || str[i] == 127)
1124 printf("\t%s local name: \'%s\'\n",
1125 type == 0x08 ? "Shortened" : "Complete", str);
1130 printf("\tTX power level: %d\n", *((uint8_t *) ptr));
1133 printf("\tUnknown type 0x%02x with %d bytes data\n",
1147 static void cmd_inq_type(int ctl, int hdev, char *opt)
1151 dd = hci_open_dev(hdev);
1153 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1154 hdev, strerror(errno), errno);
1159 uint8_t type = atoi(opt);
1161 if (hci_write_inquiry_scan_type(dd, type, 2000) < 0) {
1162 fprintf(stderr, "Can't set inquiry scan type on hci%d: %s (%d)\n",
1163 hdev, strerror(errno), errno);
1169 if (hci_read_inquiry_scan_type(dd, &type, 1000) < 0) {
1170 fprintf(stderr, "Can't read inquiry scan type on hci%d: %s (%d)\n",
1171 hdev, strerror(errno), errno);
1176 printf("\tInquiry scan type: %s\n",
1177 type == 1 ? "Interlaced Inquiry Scan" : "Standard Inquiry Scan");
1183 static void cmd_inq_parms(int ctl, int hdev, char *opt)
1185 struct hci_request rq;
1188 if ((s = hci_open_dev(hdev)) < 0) {
1189 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1190 hdev, strerror(errno), errno);
1194 memset(&rq, 0, sizeof(rq));
1197 unsigned int window, interval;
1198 write_inq_activity_cp cp;
1200 if (sscanf(opt,"%4u:%4u", &window, &interval) != 2) {
1201 printf("Invalid argument format\n");
1205 rq.ogf = OGF_HOST_CTL;
1206 rq.ocf = OCF_WRITE_INQ_ACTIVITY;
1208 rq.clen = WRITE_INQ_ACTIVITY_CP_SIZE;
1210 cp.window = htobs((uint16_t) window);
1211 cp.interval = htobs((uint16_t) interval);
1213 if (window < 0x12 || window > 0x1000)
1214 printf("Warning: inquiry window out of range!\n");
1216 if (interval < 0x12 || interval > 0x1000)
1217 printf("Warning: inquiry interval out of range!\n");
1219 if (hci_send_req(s, &rq, 2000) < 0) {
1220 fprintf(stderr, "Can't set inquiry parameters name on hci%d: %s (%d)\n",
1221 hdev, strerror(errno), errno);
1225 uint16_t window, interval;
1226 read_inq_activity_rp rp;
1228 rq.ogf = OGF_HOST_CTL;
1229 rq.ocf = OCF_READ_INQ_ACTIVITY;
1231 rq.rlen = READ_INQ_ACTIVITY_RP_SIZE;
1233 if (hci_send_req(s, &rq, 1000) < 0) {
1234 fprintf(stderr, "Can't read inquiry parameters on hci%d: %s (%d)\n",
1235 hdev, strerror(errno), errno);
1239 printf("Read inquiry parameters on hci%d returned status %d\n",
1245 window = btohs(rp.window);
1246 interval = btohs(rp.interval);
1247 printf("\tInquiry interval: %u slots (%.2f ms), window: %u slots (%.2f ms)\n",
1248 interval, (float)interval * 0.625, window, (float)window * 0.625);
1252 static void cmd_page_parms(int ctl, int hdev, char *opt)
1254 struct hci_request rq;
1257 if ((s = hci_open_dev(hdev)) < 0) {
1258 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1259 hdev, strerror(errno), errno);
1263 memset(&rq, 0, sizeof(rq));
1266 unsigned int window, interval;
1267 write_page_activity_cp cp;
1269 if (sscanf(opt,"%4u:%4u", &window, &interval) != 2) {
1270 printf("Invalid argument format\n");
1274 rq.ogf = OGF_HOST_CTL;
1275 rq.ocf = OCF_WRITE_PAGE_ACTIVITY;
1277 rq.clen = WRITE_PAGE_ACTIVITY_CP_SIZE;
1279 cp.window = htobs((uint16_t) window);
1280 cp.interval = htobs((uint16_t) interval);
1282 if (window < 0x12 || window > 0x1000)
1283 printf("Warning: page window out of range!\n");
1285 if (interval < 0x12 || interval > 0x1000)
1286 printf("Warning: page interval out of range!\n");
1288 if (hci_send_req(s, &rq, 2000) < 0) {
1289 fprintf(stderr, "Can't set page parameters name on hci%d: %s (%d)\n",
1290 hdev, strerror(errno), errno);
1294 uint16_t window, interval;
1295 read_page_activity_rp rp;
1297 rq.ogf = OGF_HOST_CTL;
1298 rq.ocf = OCF_READ_PAGE_ACTIVITY;
1300 rq.rlen = READ_PAGE_ACTIVITY_RP_SIZE;
1302 if (hci_send_req(s, &rq, 1000) < 0) {
1303 fprintf(stderr, "Can't read page parameters on hci%d: %s (%d)\n",
1304 hdev, strerror(errno), errno);
1308 printf("Read page parameters on hci%d returned status %d\n",
1314 window = btohs(rp.window);
1315 interval = btohs(rp.interval);
1316 printf("\tPage interval: %u slots (%.2f ms), window: %u slots (%.2f ms)\n",
1317 interval, (float)interval * 0.625, window, (float)window * 0.625);
1321 static void cmd_page_to(int ctl, int hdev, char *opt)
1323 struct hci_request rq;
1326 if ((s = hci_open_dev(hdev)) < 0) {
1327 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1328 hdev, strerror(errno), errno);
1332 memset(&rq, 0, sizeof(rq));
1335 unsigned int timeout;
1336 write_page_timeout_cp cp;
1338 if (sscanf(opt,"%5u", &timeout) != 1) {
1339 printf("Invalid argument format\n");
1343 rq.ogf = OGF_HOST_CTL;
1344 rq.ocf = OCF_WRITE_PAGE_TIMEOUT;
1346 rq.clen = WRITE_PAGE_TIMEOUT_CP_SIZE;
1348 cp.timeout = htobs((uint16_t) timeout);
1350 if (timeout < 0x01 || timeout > 0xFFFF)
1351 printf("Warning: page timeout out of range!\n");
1353 if (hci_send_req(s, &rq, 2000) < 0) {
1354 fprintf(stderr, "Can't set page timeout on hci%d: %s (%d)\n",
1355 hdev, strerror(errno), errno);
1360 read_page_timeout_rp rp;
1362 rq.ogf = OGF_HOST_CTL;
1363 rq.ocf = OCF_READ_PAGE_TIMEOUT;
1365 rq.rlen = READ_PAGE_TIMEOUT_RP_SIZE;
1367 if (hci_send_req(s, &rq, 1000) < 0) {
1368 fprintf(stderr, "Can't read page timeout on hci%d: %s (%d)\n",
1369 hdev, strerror(errno), errno);
1373 printf("Read page timeout on hci%d returned status %d\n",
1379 timeout = btohs(rp.timeout);
1380 printf("\tPage timeout: %u slots (%.2f ms)\n",
1381 timeout, (float)timeout * 0.625);
1385 static void cmd_afh_mode(int ctl, int hdev, char *opt)
1389 dd = hci_open_dev(hdev);
1391 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1392 hdev, strerror(errno), errno);
1397 uint8_t mode = atoi(opt);
1399 if (hci_write_afh_mode(dd, mode, 2000) < 0) {
1400 fprintf(stderr, "Can't set AFH mode on hci%d: %s (%d)\n",
1401 hdev, strerror(errno), errno);
1407 if (hci_read_afh_mode(dd, &mode, 1000) < 0) {
1408 fprintf(stderr, "Can't read AFH mode on hci%d: %s (%d)\n",
1409 hdev, strerror(errno), errno);
1414 printf("\tAFH mode: %s\n", mode == 1 ? "Enabled" : "Disabled");
1418 static void cmd_ssp_mode(int ctl, int hdev, char *opt)
1422 dd = hci_open_dev(hdev);
1424 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1425 hdev, strerror(errno), errno);
1430 uint8_t mode = atoi(opt);
1432 if (hci_write_simple_pairing_mode(dd, mode, 2000) < 0) {
1433 fprintf(stderr, "Can't set Simple Pairing mode on hci%d: %s (%d)\n",
1434 hdev, strerror(errno), errno);
1440 if (hci_read_simple_pairing_mode(dd, &mode, 1000) < 0) {
1441 fprintf(stderr, "Can't read Simple Pairing mode on hci%d: %s (%d)\n",
1442 hdev, strerror(errno), errno);
1447 printf("\tSimple Pairing mode: %s\n", mode == 1 ? "Enabled" : "Disabled");
1451 static void print_rev_ericsson(int dd)
1453 struct hci_request rq;
1454 unsigned char buf[102];
1456 memset(&rq, 0, sizeof(rq));
1457 rq.ogf = OGF_VENDOR_CMD;
1462 rq.rlen = sizeof(buf);
1464 if (hci_send_req(dd, &rq, 1000) < 0) {
1465 printf("\nCan't read revision info: %s (%d)\n", strerror(errno), errno);
1469 printf("\t%s\n", buf + 1);
1472 static void print_rev_csr(int dd, uint16_t rev)
1474 uint16_t buildid, chipver, chiprev, maxkeylen, mapsco;
1476 if (csr_read_varid_uint16(dd, 0, CSR_VARID_BUILDID, &buildid) < 0) {
1477 printf("\t%s\n", csr_buildidtostr(rev));
1481 printf("\t%s\n", csr_buildidtostr(buildid));
1483 if (!csr_read_varid_uint16(dd, 1, CSR_VARID_CHIPVER, &chipver)) {
1484 if (csr_read_varid_uint16(dd, 2, CSR_VARID_CHIPREV, &chiprev) < 0)
1486 printf("\tChip version: %s\n", csr_chipvertostr(chipver, chiprev));
1489 if (!csr_read_varid_uint16(dd, 3, CSR_VARID_MAX_CRYPT_KEY_LENGTH, &maxkeylen))
1490 printf("\tMax key size: %d bit\n", maxkeylen * 8);
1492 if (!csr_read_pskey_uint16(dd, 4, CSR_PSKEY_HOSTIO_MAP_SCO_PCM, 0x0000, &mapsco))
1493 printf("\tSCO mapping: %s\n", mapsco ? "PCM" : "HCI");
1496 static void print_rev_digianswer(int dd)
1498 struct hci_request rq;
1499 unsigned char req[] = { 0x07 };
1500 unsigned char buf[102];
1502 memset(&rq, 0, sizeof(rq));
1503 rq.ogf = OGF_VENDOR_CMD;
1506 rq.clen = sizeof(req);
1508 rq.rlen = sizeof(buf);
1510 if (hci_send_req(dd, &rq, 1000) < 0) {
1511 printf("\nCan't read revision info: %s (%d)\n", strerror(errno), errno);
1515 printf("\t%s\n", buf + 1);
1518 static void print_rev_broadcom(uint16_t hci_rev, uint16_t lmp_subver)
1520 printf("\tFirmware %d.%d / %d\n", hci_rev & 0xff, lmp_subver >> 8, lmp_subver & 0xff);
1523 static void print_rev_avm(uint16_t hci_rev, uint16_t lmp_subver)
1525 if (lmp_subver == 0x01)
1526 printf("\tFirmware 03.%d.%d\n", hci_rev >> 8, hci_rev & 0xff);
1528 printf("\tUnknown type\n");
1531 static void cmd_revision(int ctl, int hdev, char *opt)
1533 struct hci_version ver;
1536 dd = hci_open_dev(hdev);
1538 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
1539 hdev, strerror(errno), errno);
1543 if (hci_read_local_version(dd, &ver, 1000) < 0) {
1544 fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n",
1545 hdev, strerror(errno), errno);
1550 switch (ver.manufacturer) {
1554 print_rev_ericsson(dd);
1557 print_rev_csr(dd, ver.hci_rev);
1560 print_rev_digianswer(dd);
1563 print_rev_broadcom(ver.hci_rev, ver.lmp_subver);
1566 print_rev_avm(ver.hci_rev, ver.lmp_subver);
1569 printf("\tUnsupported manufacturer\n");
1575 static void print_dev_hdr(struct hci_dev_info *di)
1577 static int hdr = -1;
1580 if (hdr == di->dev_id)
1584 ba2str(&di->bdaddr, addr);
1586 printf("%s:\tType: %s\n", di->name, hci_dtypetostr(di->type) );
1587 printf("\tBD Address: %s ACL MTU: %d:%d SCO MTU: %d:%d\n",
1588 addr, di->acl_mtu, di->acl_pkts,
1589 di->sco_mtu, di->sco_pkts);
1592 static void print_dev_info(int ctl, struct hci_dev_info *di)
1594 struct hci_dev_stats *st = &di->stat;
1599 str = hci_dflagstostr(di->flags);
1600 printf("\t%s\n", str);
1603 printf("\tRX bytes:%d acl:%d sco:%d events:%d errors:%d\n",
1604 st->byte_rx, st->acl_rx, st->sco_rx, st->evt_rx, st->err_rx);
1606 printf("\tTX bytes:%d acl:%d sco:%d commands:%d errors:%d\n",
1607 st->byte_tx, st->acl_tx, st->sco_tx, st->cmd_tx, st->err_tx);
1609 if (all && !hci_test_bit(HCI_RAW, &di->flags) &&
1610 bacmp(&di->bdaddr, BDADDR_ANY)) {
1611 print_dev_features(di, 0);
1613 print_link_policy(di);
1614 print_link_mode(di);
1616 if (hci_test_bit(HCI_UP, &di->flags)) {
1617 cmd_name(ctl, di->dev_id, NULL);
1618 cmd_class(ctl, di->dev_id, NULL);
1619 cmd_version(ctl, di->dev_id, NULL);
1628 void (*func)(int ctl, int hdev, char *opt);
1632 { "up", cmd_up, 0, "Open and initialize HCI device" },
1633 { "down", cmd_down, 0, "Close HCI device" },
1634 { "reset", cmd_reset, 0, "Reset HCI device" },
1635 { "rstat", cmd_rstat, 0, "Reset statistic counters" },
1636 { "auth", cmd_auth, 0, "Enable Authentication" },
1637 { "noauth", cmd_auth, 0, "Disable Authentication" },
1638 { "encrypt", cmd_encrypt, 0, "Enable Encryption" },
1639 { "noencrypt", cmd_encrypt, 0, "Disable Encryption" },
1640 { "piscan", cmd_scan, 0, "Enable Page and Inquiry scan" },
1641 { "noscan", cmd_scan, 0, "Disable scan" },
1642 { "iscan", cmd_scan, 0, "Enable Inquiry scan" },
1643 { "pscan", cmd_scan, 0, "Enable Page scan" },
1644 { "ptype", cmd_ptype, "[type]", "Get/Set default packet type" },
1645 { "lm", cmd_lm, "[mode]", "Get/Set default link mode" },
1646 { "lp", cmd_lp, "[policy]", "Get/Set default link policy" },
1647 { "name", cmd_name, "[name]", "Get/Set local name" },
1648 { "class", cmd_class, "[class]", "Get/Set class of device" },
1649 { "voice", cmd_voice, "[voice]", "Get/Set voice setting" },
1650 { "iac", cmd_iac, "[iac]", "Get/Set inquiry access code" },
1651 { "inqtpl", cmd_inq_tpl, "[level]", "Get/Set inquiry transmit power level" },
1652 { "inqmode", cmd_inq_mode, "[mode]", "Get/Set inquiry mode" },
1653 { "inqdata", cmd_inq_data, "[data]", "Get/Set inquiry data" },
1654 { "inqtype", cmd_inq_type, "[type]", "Get/Set inquiry scan type" },
1655 { "inqparms", cmd_inq_parms, "[win:int]", "Get/Set inquiry scan window and interval" },
1656 { "pageparms", cmd_page_parms, "[win:int]", "Get/Set page scan window and interval" },
1657 { "pageto", cmd_page_to, "[to]", "Get/Set page timeout" },
1658 { "afhmode", cmd_afh_mode, "[mode]", "Get/Set AFH mode" },
1659 { "sspmode", cmd_ssp_mode, "[mode]", "Get/Set Simple Pairing Mode" },
1660 { "aclmtu", cmd_aclmtu, "<mtu:pkt>", "Set ACL MTU and number of packets" },
1661 { "scomtu", cmd_scomtu, "<mtu:pkt>", "Set SCO MTU and number of packets" },
1662 { "putkey", cmd_putkey, "<bdaddr>", "Store link key on the device" },
1663 { "delkey", cmd_delkey, "<bdaddr>", "Delete link key from the device" },
1664 { "oobdata", cmd_oob_data, 0, "Display local OOB data" },
1665 { "commands", cmd_commands, 0, "Display supported commands" },
1666 { "features", cmd_features, 0, "Display device features" },
1667 { "version", cmd_version, 0, "Display version information" },
1668 { "revision", cmd_revision, 0, "Display revision information" },
1672 static void usage(void)
1676 printf("hciconfig - HCI device configuration utility\n");
1679 "\thciconfig [-a] hciX [command]\n");
1680 printf("Commands:\n");
1681 for (i=0; command[i].cmd; i++)
1682 printf("\t%-10s %-8s\t%s\n", command[i].cmd,
1683 command[i].opt ? command[i].opt : " ",
1687 static struct option main_options[] = {
1688 { "help", 0, 0, 'h' },
1689 { "all", 0, 0, 'a' },
1693 int main(int argc, char *argv[])
1695 int opt, ctl, i, cmd=0;
1697 while ((opt=getopt_long(argc, argv, "ah", main_options, NULL)) != -1) {
1714 /* Open HCI socket */
1715 if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
1716 perror("Can't open HCI socket.");
1721 print_dev_list(ctl, 0);
1725 di.dev_id = atoi(argv[0] + 3);
1728 if (ioctl(ctl, HCIGETDEVINFO, (void *) &di)) {
1729 perror("Can't get device info");
1733 if (hci_test_bit(HCI_RAW, &di.flags) &&
1734 !bacmp(&di.bdaddr, BDADDR_ANY)) {
1735 int dd = hci_open_dev(di.dev_id);
1736 hci_read_bd_addr(dd, &di.bdaddr, 1000);
1741 for (i = 0; command[i].cmd; i++) {
1742 if (strncmp(command[i].cmd, *argv, 5))
1745 if (command[i].opt) {
1749 command[i].func(ctl, di.dev_id, *argv);
1757 print_dev_info(ctl, &di);