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-2010 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
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
43 #include "lib/bluetooth.h"
45 #include "lib/hci_lib.h"
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 /* Unofficial value, might still change */
56 #define FLAGS_AD_TYPE 0x01
57 #define FLAGS_LIMITED_MODE_BIT 0x01
58 #define FLAGS_GENERAL_MODE_BIT 0x02
60 #define EIR_FLAGS 0x01 /* flags */
61 #define EIR_UUID16_SOME 0x02 /* 16-bit UUID, more available */
62 #define EIR_UUID16_ALL 0x03 /* 16-bit UUID, all listed */
63 #define EIR_UUID32_SOME 0x04 /* 32-bit UUID, more available */
64 #define EIR_UUID32_ALL 0x05 /* 32-bit UUID, all listed */
65 #define EIR_UUID128_SOME 0x06 /* 128-bit UUID, more available */
66 #define EIR_UUID128_ALL 0x07 /* 128-bit UUID, all listed */
67 #define EIR_NAME_SHORT 0x08 /* shortened local name */
68 #define EIR_NAME_COMPLETE 0x09 /* complete local name */
69 #define EIR_TX_POWER 0x0A /* transmit power level */
70 #define EIR_DEVICE_ID 0x10 /* device ID */
72 #define for_each_opt(opt, long, short) while ((opt=getopt_long(argc, argv, short ? short:"+", long, NULL)) != -1)
74 static volatile int signal_received = 0;
76 static void usage(void);
78 static int str2buf(const char *str, uint8_t *buf, size_t blen)
87 dlen = MIN((strlen(str) / 2), blen);
89 for (i = 0; i < dlen; i++)
90 sscanf(str + (i * 2), "%02hhX", &buf[i]);
95 static int dev_info(int s, int dev_id, long arg)
97 struct hci_dev_info di = { .dev_id = dev_id };
100 if (ioctl(s, HCIGETDEVINFO, (void *) &di))
103 ba2str(&di.bdaddr, addr);
104 printf("\t%s\t%s\n", di.name, addr);
108 static void helper_arg(int min_num_arg, int max_num_arg, int *argc,
109 char ***argv, const char *usage)
112 /* too many arguments, but when "max_num_arg < min_num_arg" then no
113 limiting (prefer "max_num_arg=-1" to gen infinity)
115 if ( (*argc > max_num_arg) && (max_num_arg >= min_num_arg ) ) {
116 fprintf(stderr, "%s: too many arguments (maximal: %i)\n",
117 *argv[0], max_num_arg);
123 if (*argc < min_num_arg) {
124 fprintf(stderr, "%s: too few arguments (minimal: %i)\n",
125 *argv[0], min_num_arg);
133 static char *type2str(uint8_t type)
149 static int conn_list(int s, int dev_id, long arg)
151 struct hci_conn_list_req *cl;
152 struct hci_conn_info *ci;
156 if (id != -1 && dev_id != id)
159 if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
160 perror("Can't allocate memory");
167 if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
168 perror("Can't get connection list");
172 for (i = 0; i < cl->conn_num; i++, ci++) {
175 ba2str(&ci->bdaddr, addr);
176 str = hci_lmtostr(ci->link_mode);
177 printf("\t%s %s %s handle %d state %d lm %s\n",
178 ci->out ? "<" : ">", type2str(ci->type),
179 addr, ci->handle, ci->state, str);
187 static int find_conn(int s, int dev_id, long arg)
189 struct hci_conn_list_req *cl;
190 struct hci_conn_info *ci;
193 if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
194 perror("Can't allocate memory");
201 if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
202 perror("Can't get connection list");
206 for (i = 0; i < cl->conn_num; i++, ci++)
207 if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
216 static void hex_dump(char *pref, int width, unsigned char *buf, int len)
220 for (i = 0, n = 1; i < len; i++, n++) {
223 printf("%2.2X ", buf[i]);
233 static char *get_minor_device_name(int major, int minor)
238 case 1: /* computer */
241 return "Uncategorized";
243 return "Desktop workstation";
259 return "Uncategorized";
265 return "Smart phone";
267 return "Wired modem or voice gateway";
269 return "Common ISDN Access";
271 return "Sim Card Reader";
274 case 3: /* lan access */
276 return "Uncategorized";
279 return "Fully available";
281 return "1-17% utilized";
283 return "17-33% utilized";
285 return "33-50% utilized";
287 return "50-67% utilized";
289 return "67-83% utilized";
291 return "83-99% utilized";
293 return "No service available";
296 case 4: /* audio/video */
299 return "Uncategorized";
301 return "Device conforms to the Headset profile";
308 return "Loudspeaker";
312 return "Portable Audio";
316 return "Set-top box";
318 return "HiFi Audio Device";
322 return "Video Camera";
326 return "Video Monitor";
328 return "Video Display and Loudspeaker";
330 return "Video Conferencing";
336 case 5: /* peripheral */ {
337 static char cls_str[48]; cls_str[0] = 0;
339 switch (minor & 48) {
341 strncpy(cls_str, "Keyboard", sizeof(cls_str));
344 strncpy(cls_str, "Pointing device", sizeof(cls_str));
347 strncpy(cls_str, "Combo keyboard/pointing device", sizeof(cls_str));
350 if ((minor & 15) && (strlen(cls_str) > 0))
351 strcat(cls_str, "/");
353 switch (minor & 15) {
357 strncat(cls_str, "Joystick",
358 sizeof(cls_str) - strlen(cls_str) - 1);
361 strncat(cls_str, "Gamepad",
362 sizeof(cls_str) - strlen(cls_str) - 1);
365 strncat(cls_str, "Remote control",
366 sizeof(cls_str) - strlen(cls_str) - 1);
369 strncat(cls_str, "Sensing device",
370 sizeof(cls_str) - strlen(cls_str) - 1);
373 strncat(cls_str, "Digitizer tablet",
374 sizeof(cls_str) - strlen(cls_str) - 1);
377 strncat(cls_str, "Card reader",
378 sizeof(cls_str) - strlen(cls_str) - 1);
381 strncat(cls_str, "(reserved)",
382 sizeof(cls_str) - strlen(cls_str) - 1);
385 if (strlen(cls_str) > 0)
389 case 6: /* imaging */
399 case 7: /* wearable */
402 return "Wrist Watch";
420 return "Doll / Action Figure";
427 case 63: /* uncategorised */
430 return "Unknown (reserved) minor device class";
433 static char *major_classes[] = {
434 "Miscellaneous", "Computer", "Phone", "LAN Access",
435 "Audio/Video", "Peripheral", "Imaging", "Uncategorized"
438 /* Display local devices */
440 static struct option dev_options[] = {
441 { "help", 0, 0, 'h' },
445 static const char *dev_help =
449 static void cmd_dev(int dev_id, int argc, char **argv)
453 for_each_opt(opt, dev_options, NULL) {
456 printf("%s", dev_help);
460 helper_arg(0, 0, &argc, &argv, dev_help);
462 printf("Devices:\n");
464 hci_for_each_dev(HCI_UP, dev_info, 0);
469 static struct option inq_options[] = {
470 { "help", 0, 0, 'h' },
471 { "length", 1, 0, 'l' },
472 { "numrsp", 1, 0, 'n' },
473 { "iac", 1, 0, 'i' },
474 { "flush", 0, 0, 'f' },
478 static const char *inq_help =
480 "\tinq [--length=N] maximum inquiry duration in 1.28 s units\n"
481 "\t [--numrsp=N] specify maximum number of inquiry responses\n"
482 "\t [--iac=lap] specify the inquiry access code\n"
483 "\t [--flush] flush the inquiry cache\n";
485 static void cmd_inq(int dev_id, int argc, char **argv)
487 inquiry_info *info = NULL;
488 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
489 int num_rsp, length, flags;
493 length = 8; /* ~10 seconds */
497 for_each_opt(opt, inq_options, NULL) {
500 length = atoi(optarg);
504 num_rsp = atoi(optarg);
508 l = strtoul(optarg, 0, 16);
509 if (!strcasecmp(optarg, "giac")) {
511 } else if (!strcasecmp(optarg, "liac")) {
513 } if (l < 0x9e8b00 || l > 0x9e8b3f) {
514 printf("Invalid access code 0x%x\n", l);
518 lap[1] = (l >> 8) & 0xff;
519 lap[2] = (l >> 16) & 0xff;
523 flags |= IREQ_CACHE_FLUSH;
527 printf("%s", inq_help);
531 helper_arg(0, 0, &argc, &argv, inq_help);
533 printf("Inquiring ...\n");
535 num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
537 perror("Inquiry failed.");
541 for (i = 0; i < num_rsp; i++) {
542 ba2str(&(info+i)->bdaddr, addr);
543 printf("\t%s\tclock offset: 0x%4.4x\tclass: 0x%2.2x%2.2x%2.2x\n",
544 addr, btohs((info+i)->clock_offset),
545 (info+i)->dev_class[2],
546 (info+i)->dev_class[1],
547 (info+i)->dev_class[0]);
553 /* Device scanning */
555 static struct option scan_options[] = {
556 { "help", 0, 0, 'h' },
557 { "length", 1, 0, 'l' },
558 { "numrsp", 1, 0, 'n' },
559 { "iac", 1, 0, 'i' },
560 { "flush", 0, 0, 'f' },
561 { "class", 0, 0, 'C' },
562 { "info", 0, 0, 'I' },
563 { "oui", 0, 0, 'O' },
564 { "all", 0, 0, 'A' },
565 { "ext", 0, 0, 'A' },
569 static const char *scan_help =
571 "\tscan [--length=N] [--numrsp=N] [--iac=lap] [--flush] [--class] [--info] [--oui] [--refresh]\n";
573 static void cmd_scan(int dev_id, int argc, char **argv)
575 inquiry_info *info = NULL;
576 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
577 int num_rsp, length, flags;
578 uint8_t cls[3], features[8];
579 char addr[18], name[249], *comp;
580 struct hci_version version;
581 struct hci_dev_info di;
582 struct hci_conn_info_req *cr;
583 int extcls = 0, extinf = 0, extoui = 0;
584 int i, n, l, opt, dd, cc;
586 length = 8; /* ~10 seconds */
590 for_each_opt(opt, scan_options, NULL) {
593 length = atoi(optarg);
597 num_rsp = atoi(optarg);
601 l = strtoul(optarg, 0, 16);
602 if (!strcasecmp(optarg, "giac")) {
604 } else if (!strcasecmp(optarg, "liac")) {
606 } else if (l < 0x9e8b00 || l > 0x9e8b3f) {
607 printf("Invalid access code 0x%x\n", l);
611 lap[1] = (l >> 8) & 0xff;
612 lap[2] = (l >> 16) & 0xff;
616 flags |= IREQ_CACHE_FLUSH;
638 printf("%s", scan_help);
642 helper_arg(0, 0, &argc, &argv, scan_help);
645 dev_id = hci_get_route(NULL);
647 perror("Device is not available");
652 if (hci_devinfo(dev_id, &di) < 0) {
653 perror("Can't get device info");
657 printf("Scanning ...\n");
658 num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
660 perror("Inquiry failed");
664 dd = hci_open_dev(dev_id);
666 perror("HCI device open failed");
671 if (extcls || extinf || extoui)
674 for (i = 0; i < num_rsp; i++) {
677 if (!extcls && !extinf && !extoui) {
678 ba2str(&(info+i)->bdaddr, addr);
680 if (hci_read_remote_name_with_clock_offset(dd,
682 (info+i)->pscan_rep_mode,
683 (info+i)->clock_offset | 0x8000,
684 sizeof(name), name, 100000) < 0)
687 for (n = 0; n < 248 && name[n]; n++) {
688 if ((unsigned char) name[i] < 32 || name[i] == 127)
694 printf("\t%s\t%s\n", addr, name);
698 ba2str(&(info+i)->bdaddr, addr);
699 printf("BD Address:\t%s [mode %d, clkoffset 0x%4.4x]\n", addr,
700 (info+i)->pscan_rep_mode, btohs((info+i)->clock_offset));
703 comp = batocomp(&(info+i)->bdaddr);
706 ba2oui(&(info+i)->bdaddr, oui);
707 printf("OUI company:\t%s (%s)\n", comp, oui);
715 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
717 bacpy(&cr->bdaddr, &(info+i)->bdaddr);
719 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
723 handle = htobs(cr->conn_info->handle);
730 if (hci_create_connection(dd, &(info+i)->bdaddr,
731 htobs(di.pkt_type & ACL_PTYPE_MASK),
732 (info+i)->clock_offset | 0x8000,
733 0x01, &handle, 25000) < 0) {
740 if (hci_read_remote_name_with_clock_offset(dd,
742 (info+i)->pscan_rep_mode,
743 (info+i)->clock_offset | 0x8000,
744 sizeof(name), name, 100000) < 0) {
746 for (n = 0; n < 248 && name[n]; n++) {
747 if ((unsigned char) name[i] < 32 || name[i] == 127)
754 if (strlen(name) > 0)
755 printf("Device name:\t%s\n", name);
758 memcpy(cls, (info+i)->dev_class, 3);
759 printf("Device class:\t");
760 if ((cls[1] & 0x1f) > sizeof(major_classes) / sizeof(char *))
763 printf("%s, %s", major_classes[cls[1] & 0x1f],
764 get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));
765 printf(" (0x%2.2x%2.2x%2.2x)\n", cls[2], cls[1], cls[0]);
768 if (extinf && handle > 0) {
769 if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
770 char *ver = lmp_vertostr(version.lmp_ver);
771 printf("Manufacturer:\t%s (%d)\n",
772 bt_compidtostr(version.manufacturer),
773 version.manufacturer);
774 printf("LMP version:\t%s (0x%x) [subver 0x%x]\n",
776 version.lmp_ver, version.lmp_subver);
781 if (hci_read_remote_features(dd, handle, features, 20000) == 0) {
782 char *tmp = lmp_featurestostr(features, "\t\t", 63);
783 printf("LMP features:\t0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x"
784 " 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
785 features[0], features[1],
786 features[2], features[3],
787 features[4], features[5],
788 features[6], features[7]);
795 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
809 static struct option name_options[] = {
810 { "help", 0, 0, 'h' },
814 static const char *name_help =
818 static void cmd_name(int dev_id, int argc, char **argv)
824 for_each_opt(opt, name_options, NULL) {
827 printf("%s", name_help);
831 helper_arg(1, 1, &argc, &argv, name_help);
833 str2ba(argv[0], &bdaddr);
836 dev_id = hci_get_route(&bdaddr);
838 fprintf(stderr, "Device is not available.\n");
843 dd = hci_open_dev(dev_id);
845 perror("HCI device open failed");
849 if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
850 printf("%s\n", name);
855 /* Info about remote device */
857 static struct option info_options[] = {
858 { "help", 0, 0, 'h' },
862 static const char *info_help =
866 static void cmd_info(int dev_id, int argc, char **argv)
870 uint8_t features[8], max_page = 0;
871 char name[249], *comp, *tmp;
872 struct hci_version version;
873 struct hci_dev_info di;
874 struct hci_conn_info_req *cr;
875 int i, opt, dd, cc = 0;
877 for_each_opt(opt, info_options, NULL) {
880 printf("%s", info_help);
884 helper_arg(1, 1, &argc, &argv, info_help);
886 str2ba(argv[0], &bdaddr);
889 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
892 dev_id = hci_get_route(&bdaddr);
895 fprintf(stderr, "Device is not available or not connected.\n");
899 if (hci_devinfo(dev_id, &di) < 0) {
900 perror("Can't get device info");
904 printf("Requesting information ...\n");
906 dd = hci_open_dev(dev_id);
908 perror("HCI device open failed");
912 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
914 perror("Can't get connection info");
919 bacpy(&cr->bdaddr, &bdaddr);
921 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
922 if (hci_create_connection(dd, &bdaddr,
923 htobs(di.pkt_type & ACL_PTYPE_MASK),
924 0, 0x01, &handle, 25000) < 0) {
925 perror("Can't create connection");
933 handle = htobs(cr->conn_info->handle);
937 printf("\tBD Address: %s\n", argv[0]);
939 comp = batocomp(&bdaddr);
942 ba2oui(&bdaddr, oui);
943 printf("\tOUI Company: %s (%s)\n", comp, oui);
947 if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
948 printf("\tDevice Name: %s\n", name);
950 if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
951 char *ver = lmp_vertostr(version.lmp_ver);
952 printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
953 "\tManufacturer: %s (%d)\n",
957 bt_compidtostr(version.manufacturer),
958 version.manufacturer);
963 memset(features, 0, sizeof(features));
964 hci_read_remote_features(dd, handle, features, 20000);
966 if ((di.features[7] & LMP_EXT_FEAT) && (features[7] & LMP_EXT_FEAT))
967 hci_read_remote_ext_features(dd, handle, 0, &max_page,
970 if (max_page < 1 && (features[6] & LMP_SIMPLE_PAIR))
973 printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
974 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
975 (max_page > 0) ? " page 0" : "",
976 features[0], features[1], features[2], features[3],
977 features[4], features[5], features[6], features[7]);
979 tmp = lmp_featurestostr(features, "\t\t", 63);
983 for (i = 1; i <= max_page; i++) {
984 if (hci_read_remote_ext_features(dd, handle, i, NULL,
985 features, 20000) < 0)
988 printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
989 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i,
990 features[0], features[1], features[2], features[3],
991 features[4], features[5], features[6], features[7]);
996 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
1002 /* Start periodic inquiry */
1004 static struct option spinq_options[] = {
1005 { "help", 0, 0, 'h' },
1009 static const char *spinq_help =
1013 static void cmd_spinq(int dev_id, int argc, char **argv)
1015 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
1016 struct hci_request rq;
1017 periodic_inquiry_cp cp;
1020 for_each_opt(opt, spinq_options, NULL) {
1023 printf("%s", spinq_help);
1027 helper_arg(0, 0, &argc, &argv, spinq_help);
1030 dev_id = hci_get_route(NULL);
1032 dd = hci_open_dev(dev_id);
1034 perror("Device open failed");
1038 memset(&cp, 0, sizeof(cp));
1039 memcpy(cp.lap, lap, 3);
1040 cp.max_period = htobs(16);
1041 cp.min_period = htobs(10);
1045 memset(&rq, 0, sizeof(rq));
1046 rq.ogf = OGF_LINK_CTL;
1047 rq.ocf = OCF_PERIODIC_INQUIRY;
1049 rq.clen = PERIODIC_INQUIRY_CP_SIZE;
1051 if (hci_send_req(dd, &rq, 100) < 0) {
1052 perror("Periodic inquiry failed");
1059 /* Exit periodic inquiry */
1061 static struct option epinq_options[] = {
1062 { "help", 0, 0, 'h' },
1066 static const char *epinq_help =
1070 static void cmd_epinq(int dev_id, int argc, char **argv)
1074 for_each_opt(opt, epinq_options, NULL) {
1077 printf("%s", epinq_help);
1081 helper_arg(0, 0, &argc, &argv, epinq_help);
1084 dev_id = hci_get_route(NULL);
1086 dd = hci_open_dev(dev_id);
1088 perror("Device open failed");
1092 if (hci_send_cmd(dd, OGF_LINK_CTL,
1093 OCF_EXIT_PERIODIC_INQUIRY, 0, NULL) < 0) {
1094 perror("Exit periodic inquiry failed");
1101 #ifdef __TIZEN_PATCH__
1102 /* Send arbitrary ACL data */
1103 static struct option data_options[] = {
1104 { "help", 0, 0, 'h' },
1108 static const char *data_help =
1110 "\tcmd <handle> <data>\n"
1112 "\tcmd 0x0064 0x41 0x42 0x43 0x44\n";
1114 static void cmd_data(int dev_id, int argc, char **argv)
1116 unsigned char buf[HCI_MAX_ACL_SIZE], *ptr = buf;
1117 struct hci_filter flt;
1118 int i, opt, len, dd;
1121 for_each_opt(opt, data_options, NULL) {
1124 printf("%s", data_help);
1128 helper_arg(2, -1, &argc, &argv, data_help);
1131 dev_id = hci_get_route(NULL);
1133 handle = atoi(argv[0]);
1135 for (i = 1, len = 0; i < argc && len < (int) sizeof(buf); i++, len++)
1136 *ptr++ = (uint8_t) strtol(argv[i], NULL, 16);
1138 dd = hci_open_dev(dev_id);
1140 perror("Device open failed");
1145 hci_filter_clear(&flt);
1146 hci_filter_all_events(&flt);
1147 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1148 perror("HCI filter setup failed");
1152 if (hci_send_data(dd, handle, len, buf) < 0) {
1153 perror("Send failed");
1161 /* Send arbitrary HCI commands */
1163 static struct option cmd_options[] = {
1164 { "help", 0, 0, 'h' },
1168 static const char *cmd_help =
1170 "\tcmd <ogf> <ocf> [parameters]\n"
1172 "\tcmd 0x03 0x0013 0x41 0x42 0x43 0x44\n";
1174 static void cmd_cmd(int dev_id, int argc, char **argv)
1176 unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf;
1177 struct hci_filter flt;
1179 int i, opt, len, dd;
1183 for_each_opt(opt, cmd_options, NULL) {
1186 printf("%s", cmd_help);
1190 helper_arg(2, -1, &argc, &argv, cmd_help);
1193 dev_id = hci_get_route(NULL);
1196 ogf = strtol(argv[0], NULL, 16);
1197 ocf = strtol(argv[1], NULL, 16);
1198 if (errno == ERANGE || (ogf > 0x3f) || (ocf > 0x3ff)) {
1199 printf("%s", cmd_help);
1203 for (i = 2, len = 0; i < argc && len < (int) sizeof(buf); i++, len++)
1204 *ptr++ = (uint8_t) strtol(argv[i], NULL, 16);
1206 dd = hci_open_dev(dev_id);
1208 perror("Device open failed");
1213 hci_filter_clear(&flt);
1214 hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
1215 hci_filter_all_events(&flt);
1216 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1217 perror("HCI filter setup failed");
1221 printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);
1222 hex_dump(" ", 20, buf, len); fflush(stdout);
1224 if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
1225 perror("Send failed");
1229 len = read(dd, buf, sizeof(buf));
1231 perror("Read failed");
1235 hdr = (void *)(buf + 1);
1236 ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
1237 len -= (1 + HCI_EVENT_HDR_SIZE);
1239 printf("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);
1240 hex_dump(" ", 20, ptr, len); fflush(stdout);
1245 /* Display active connections */
1247 static struct option con_options[] = {
1248 { "help", 0, 0, 'h' },
1252 static const char *con_help =
1256 static void cmd_con(int dev_id, int argc, char **argv)
1260 for_each_opt(opt, con_options, NULL) {
1263 printf("%s", con_help);
1267 helper_arg(0, 0, &argc, &argv, con_help);
1269 printf("Connections:\n");
1271 hci_for_each_dev(HCI_UP, conn_list, dev_id);
1274 /* Create connection */
1276 static struct option cc_options[] = {
1277 { "help", 0, 0, 'h' },
1278 { "role", 1, 0, 'r' },
1279 { "ptype", 1, 0, 'p' },
1283 static const char *cc_help =
1285 "\tcc [--role=m|s] [--ptype=pkt_types] <bdaddr>\n"
1287 "\tcc --ptype=dm1,dh3,dh5 01:02:03:04:05:06\n"
1288 "\tcc --role=m 01:02:03:04:05:06\n";
1290 static void cmd_cc(int dev_id, int argc, char **argv)
1299 ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;
1301 for_each_opt(opt, cc_options, NULL) {
1304 hci_strtoptype(optarg, &ptype);
1308 role = optarg[0] == 'm' ? 0 : 1;
1312 printf("%s", cc_help);
1316 helper_arg(1, 1, &argc, &argv, cc_help);
1318 str2ba(argv[0], &bdaddr);
1321 dev_id = hci_get_route(&bdaddr);
1323 fprintf(stderr, "Device is not available.\n");
1328 dd = hci_open_dev(dev_id);
1330 perror("HCI device open failed");
1334 if (hci_create_connection(dd, &bdaddr, htobs(ptype),
1335 htobs(0x0000), role, &handle, 25000) < 0)
1336 perror("Can't create connection");
1341 /* Close connection */
1343 static struct option dc_options[] = {
1344 { "help", 0, 0, 'h' },
1348 static const char *dc_help =
1350 "\tdc <bdaddr> [reason]\n";
1352 static void cmd_dc(int dev_id, int argc, char **argv)
1354 struct hci_conn_info_req *cr;
1359 for_each_opt(opt, dc_options, NULL) {
1362 printf("%s", dc_help);
1366 helper_arg(1, 2, &argc, &argv, dc_help);
1368 str2ba(argv[0], &bdaddr);
1369 reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
1372 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1374 fprintf(stderr, "Not connected.\n");
1379 dd = hci_open_dev(dev_id);
1381 perror("HCI device open failed");
1385 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1387 perror("Can't allocate memory");
1391 bacpy(&cr->bdaddr, &bdaddr);
1392 cr->type = ACL_LINK;
1393 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1394 perror("Get connection info failed");
1398 if (hci_disconnect(dd, htobs(cr->conn_info->handle),
1400 perror("Disconnect failed");
1409 static struct option sr_options[] = {
1410 { "help", 0, 0, 'h' },
1414 static const char *sr_help =
1416 "\tsr <bdaddr> <role>\n";
1418 static void cmd_sr(int dev_id, int argc, char **argv)
1424 for_each_opt(opt, sr_options, NULL) {
1427 printf("%s", sr_help);
1431 helper_arg(2, 2, &argc, &argv, sr_help);
1433 str2ba(argv[0], &bdaddr);
1434 switch (argv[1][0]) {
1442 role = atoi(argv[1]);
1447 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1449 fprintf(stderr, "Not connected.\n");
1454 dd = hci_open_dev(dev_id);
1456 perror("HCI device open failed");
1460 if (hci_switch_role(dd, &bdaddr, role, 10000) < 0) {
1461 perror("Switch role request failed");
1470 static struct option rssi_options[] = {
1471 { "help", 0, 0, 'h' },
1475 static const char *rssi_help =
1477 "\trssi <bdaddr>\n";
1479 static void cmd_rssi(int dev_id, int argc, char **argv)
1481 struct hci_conn_info_req *cr;
1486 for_each_opt(opt, rssi_options, NULL) {
1489 printf("%s", rssi_help);
1493 helper_arg(1, 1, &argc, &argv, rssi_help);
1495 str2ba(argv[0], &bdaddr);
1498 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1500 fprintf(stderr, "Not connected.\n");
1505 dd = hci_open_dev(dev_id);
1507 perror("HCI device open failed");
1511 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1513 perror("Can't allocate memory");
1517 bacpy(&cr->bdaddr, &bdaddr);
1518 cr->type = ACL_LINK;
1519 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1520 perror("Get connection info failed");
1524 if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
1525 perror("Read RSSI failed");
1529 printf("RSSI return value: %d\n", rssi);
1536 /* Get link quality */
1538 static struct option lq_options[] = {
1539 { "help", 0, 0, 'h' },
1543 static const char *lq_help =
1547 static void cmd_lq(int dev_id, int argc, char **argv)
1549 struct hci_conn_info_req *cr;
1554 for_each_opt(opt, lq_options, NULL) {
1557 printf("%s", lq_help);
1561 helper_arg(1, 1, &argc, &argv, lq_help);
1563 str2ba(argv[0], &bdaddr);
1566 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1568 fprintf(stderr, "Not connected.\n");
1573 dd = hci_open_dev(dev_id);
1575 perror("HCI device open failed");
1579 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1581 perror("Can't allocate memory");
1585 bacpy(&cr->bdaddr, &bdaddr);
1586 cr->type = ACL_LINK;
1587 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1588 perror("Get connection info failed");
1592 if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), &lq, 1000) < 0) {
1593 perror("HCI read_link_quality request failed");
1597 printf("Link quality: %d\n", lq);
1604 /* Get transmit power level */
1606 static struct option tpl_options[] = {
1607 { "help", 0, 0, 'h' },
1611 static const char *tpl_help =
1613 "\ttpl <bdaddr> [type]\n";
1615 static void cmd_tpl(int dev_id, int argc, char **argv)
1617 struct hci_conn_info_req *cr;
1623 for_each_opt(opt, tpl_options, NULL) {
1626 printf("%s", tpl_help);
1630 helper_arg(1, 2, &argc, &argv, tpl_help);
1632 str2ba(argv[0], &bdaddr);
1633 type = (argc > 1) ? atoi(argv[1]) : 0;
1636 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1638 fprintf(stderr, "Not connected.\n");
1643 dd = hci_open_dev(dev_id);
1645 perror("HCI device open failed");
1649 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1651 perror("Can't allocate memory");
1655 bacpy(&cr->bdaddr, &bdaddr);
1656 cr->type = ACL_LINK;
1657 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1658 perror("Get connection info failed");
1662 if (hci_read_transmit_power_level(dd, htobs(cr->conn_info->handle), type, &level, 1000) < 0) {
1663 perror("HCI read transmit power level request failed");
1667 printf("%s transmit power level: %d\n",
1668 (type == 0) ? "Current" : "Maximum", level);
1675 /* Get AFH channel map */
1677 static struct option afh_options[] = {
1678 { "help", 0, 0, 'h' },
1682 static const char *afh_help =
1686 static void cmd_afh(int dev_id, int argc, char **argv)
1688 struct hci_conn_info_req *cr;
1691 uint8_t mode, map[10];
1694 for_each_opt(opt, afh_options, NULL) {
1697 printf("%s", afh_help);
1701 helper_arg(1, 1, &argc, &argv, afh_help);
1703 str2ba(argv[0], &bdaddr);
1706 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1708 fprintf(stderr, "Not connected.\n");
1713 dd = hci_open_dev(dev_id);
1715 perror("HCI device open failed");
1719 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1721 perror("Can't allocate memory");
1725 bacpy(&cr->bdaddr, &bdaddr);
1726 cr->type = ACL_LINK;
1727 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1728 perror("Get connection info failed");
1732 handle = htobs(cr->conn_info->handle);
1734 if (hci_read_afh_map(dd, handle, &mode, map, 1000) < 0) {
1735 perror("HCI read AFH map request failed");
1741 printf("AFH map: 0x");
1742 for (i = 0; i < 10; i++)
1743 printf("%02x", map[i]);
1746 printf("AFH disabled\n");
1753 /* Set connection packet type */
1755 static struct option cpt_options[] = {
1756 { "help", 0, 0, 'h' },
1760 static const char *cpt_help =
1762 "\tcpt <bdaddr> <packet_types>\n";
1764 static void cmd_cpt(int dev_id, int argc, char **argv)
1766 struct hci_conn_info_req *cr;
1767 struct hci_request rq;
1768 set_conn_ptype_cp cp;
1769 evt_conn_ptype_changed rp;
1774 for_each_opt(opt, cpt_options, NULL) {
1777 printf("%s", cpt_help);
1781 helper_arg(2, 2, &argc, &argv, cpt_help);
1783 str2ba(argv[0], &bdaddr);
1784 hci_strtoptype(argv[1], &ptype);
1787 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1789 fprintf(stderr, "Not connected.\n");
1794 dd = hci_open_dev(dev_id);
1796 perror("HCI device open failed");
1800 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1802 perror("Can't allocate memory");
1806 bacpy(&cr->bdaddr, &bdaddr);
1807 cr->type = ACL_LINK;
1808 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1809 perror("Get connection info failed");
1813 cp.handle = htobs(cr->conn_info->handle);
1814 cp.pkt_type = ptype;
1816 memset(&rq, 0, sizeof(rq));
1817 rq.ogf = OGF_LINK_CTL;
1818 rq.ocf = OCF_SET_CONN_PTYPE;
1820 rq.clen = SET_CONN_PTYPE_CP_SIZE;
1822 rq.rlen = EVT_CONN_PTYPE_CHANGED_SIZE;
1823 rq.event = EVT_CONN_PTYPE_CHANGED;
1825 if (hci_send_req(dd, &rq, 100) < 0) {
1826 perror("Packet type change failed");
1835 /* Get/Set link policy settings */
1837 static struct option lp_options[] = {
1838 { "help", 0, 0, 'h' },
1842 static const char *lp_help =
1844 "\tlp <bdaddr> [link policy]\n";
1846 static void cmd_lp(int dev_id, int argc, char **argv)
1848 struct hci_conn_info_req *cr;
1853 for_each_opt(opt, lp_options, NULL) {
1856 printf("%s", lp_help);
1860 helper_arg(1, 2, &argc, &argv, lp_help);
1862 str2ba(argv[0], &bdaddr);
1865 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1867 fprintf(stderr, "Not connected.\n");
1872 dd = hci_open_dev(dev_id);
1874 perror("HCI device open failed");
1878 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1880 perror("Can't allocate memory");
1884 bacpy(&cr->bdaddr, &bdaddr);
1885 cr->type = ACL_LINK;
1886 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1887 perror("Get connection info failed");
1893 if (hci_read_link_policy(dd, htobs(cr->conn_info->handle),
1894 &policy, 1000) < 0) {
1895 perror("HCI read_link_policy_settings request failed");
1899 policy = btohs(policy);
1900 str = hci_lptostr(policy);
1902 printf("Link policy settings: %s\n", str);
1905 fprintf(stderr, "Invalig settings\n");
1910 if (hci_strtolp(argv[1], &val) < 0) {
1911 fprintf(stderr, "Invalig arguments\n");
1916 if (hci_write_link_policy(dd, htobs(cr->conn_info->handle),
1917 htobs(policy), 1000) < 0) {
1918 perror("HCI write_link_policy_settings request failed");
1928 /* Get/Set link supervision timeout */
1930 static struct option lst_options[] = {
1931 { "help", 0, 0, 'h' },
1935 static const char *lst_help =
1937 "\tlst <bdaddr> [new value in slots]\n";
1939 static void cmd_lst(int dev_id, int argc, char **argv)
1941 struct hci_conn_info_req *cr;
1946 for_each_opt(opt, lst_options, NULL) {
1949 printf("%s", lst_help);
1953 helper_arg(1, 2, &argc, &argv, lst_help);
1955 str2ba(argv[0], &bdaddr);
1958 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1960 fprintf(stderr, "Not connected.\n");
1965 dd = hci_open_dev(dev_id);
1967 perror("HCI device open failed");
1971 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1973 perror("Can't allocate memory");
1977 bacpy(&cr->bdaddr, &bdaddr);
1978 cr->type = ACL_LINK;
1979 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1980 perror("Get connection info failed");
1985 if (hci_read_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
1986 &timeout, 1000) < 0) {
1987 perror("HCI read_link_supervision_timeout request failed");
1991 timeout = btohs(timeout);
1994 printf("Link supervision timeout: %u slots (%.2f msec)\n",
1995 timeout, (float) timeout * 0.625);
1997 printf("Link supervision timeout never expires\n");
1999 timeout = strtol(argv[1], NULL, 10);
2001 if (hci_write_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
2002 htobs(timeout), 1000) < 0) {
2003 perror("HCI write_link_supervision_timeout request failed");
2013 /* Request authentication */
2015 static struct option auth_options[] = {
2016 { "help", 0, 0, 'h' },
2020 static const char *auth_help =
2022 "\tauth <bdaddr>\n";
2024 static void cmd_auth(int dev_id, int argc, char **argv)
2026 struct hci_conn_info_req *cr;
2030 for_each_opt(opt, auth_options, NULL) {
2033 printf("%s", auth_help);
2037 helper_arg(1, 1, &argc, &argv, auth_help);
2039 str2ba(argv[0], &bdaddr);
2042 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2044 fprintf(stderr, "Not connected.\n");
2049 dd = hci_open_dev(dev_id);
2051 perror("HCI device open failed");
2055 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2057 perror("Can't allocate memory");
2061 bacpy(&cr->bdaddr, &bdaddr);
2062 cr->type = ACL_LINK;
2063 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2064 perror("Get connection info failed");
2068 if (hci_authenticate_link(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2069 perror("HCI authentication request failed");
2078 /* Activate encryption */
2080 static struct option enc_options[] = {
2081 { "help", 0, 0, 'h' },
2085 static const char *enc_help =
2087 "\tenc <bdaddr> [encrypt enable]\n";
2089 static void cmd_enc(int dev_id, int argc, char **argv)
2091 struct hci_conn_info_req *cr;
2096 for_each_opt(opt, enc_options, NULL) {
2099 printf("%s", enc_help);
2103 helper_arg(1, 2, &argc, &argv, enc_help);
2105 str2ba(argv[0], &bdaddr);
2108 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2110 fprintf(stderr, "Not connected.\n");
2115 dd = hci_open_dev(dev_id);
2117 perror("HCI device open failed");
2121 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2123 perror("Can't allocate memory");
2127 bacpy(&cr->bdaddr, &bdaddr);
2128 cr->type = ACL_LINK;
2129 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2130 perror("Get connection info failed");
2134 encrypt = (argc > 1) ? atoi(argv[1]) : 1;
2136 if (hci_encrypt_link(dd, htobs(cr->conn_info->handle), encrypt, 25000) < 0) {
2137 perror("HCI set encryption request failed");
2146 /* Change connection link key */
2148 static struct option key_options[] = {
2149 { "help", 0, 0, 'h' },
2153 static const char *key_help =
2157 static void cmd_key(int dev_id, int argc, char **argv)
2159 struct hci_conn_info_req *cr;
2163 for_each_opt(opt, key_options, NULL) {
2166 printf("%s", key_help);
2170 helper_arg(1, 1, &argc, &argv, key_help);
2172 str2ba(argv[0], &bdaddr);
2175 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2177 fprintf(stderr, "Not connected.\n");
2182 dd = hci_open_dev(dev_id);
2184 perror("HCI device open failed");
2188 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2190 perror("Can't allocate memory");
2194 bacpy(&cr->bdaddr, &bdaddr);
2195 cr->type = ACL_LINK;
2196 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2197 perror("Get connection info failed");
2201 if (hci_change_link_key(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2202 perror("Changing link key failed");
2211 /* Read clock offset */
2213 static struct option clkoff_options[] = {
2214 { "help", 0, 0, 'h' },
2218 static const char *clkoff_help =
2220 "\tclkoff <bdaddr>\n";
2222 static void cmd_clkoff(int dev_id, int argc, char **argv)
2224 struct hci_conn_info_req *cr;
2229 for_each_opt(opt, clkoff_options, NULL) {
2232 printf("%s", clkoff_help);
2236 helper_arg(1, 1, &argc, &argv, clkoff_help);
2238 str2ba(argv[0], &bdaddr);
2241 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2243 fprintf(stderr, "Not connected.\n");
2248 dd = hci_open_dev(dev_id);
2250 perror("HCI device open failed");
2254 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2256 perror("Can't allocate memory");
2260 bacpy(&cr->bdaddr, &bdaddr);
2261 cr->type = ACL_LINK;
2262 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2263 perror("Get connection info failed");
2267 if (hci_read_clock_offset(dd, htobs(cr->conn_info->handle), &offset, 1000) < 0) {
2268 perror("Reading clock offset failed");
2272 printf("Clock offset: 0x%4.4x\n", btohs(offset));
2281 static struct option clock_options[] = {
2282 { "help", 0, 0, 'h' },
2286 static const char *clock_help =
2288 "\tclock [bdaddr] [which clock]\n";
2290 static void cmd_clock(int dev_id, int argc, char **argv)
2292 struct hci_conn_info_req *cr;
2295 uint32_t handle, clock;
2299 for_each_opt(opt, clock_options, NULL) {
2302 printf("%s", clock_help);
2306 helper_arg(0, 2, &argc, &argv, clock_help);
2309 str2ba(argv[0], &bdaddr);
2311 bacpy(&bdaddr, BDADDR_ANY);
2313 if (dev_id < 0 && !bacmp(&bdaddr, BDADDR_ANY))
2314 dev_id = hci_get_route(NULL);
2317 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2319 fprintf(stderr, "Not connected.\n");
2324 dd = hci_open_dev(dev_id);
2326 perror("HCI device open failed");
2330 if (bacmp(&bdaddr, BDADDR_ANY)) {
2331 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2333 perror("Can't allocate memory");
2337 bacpy(&cr->bdaddr, &bdaddr);
2338 cr->type = ACL_LINK;
2339 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2340 perror("Get connection info failed");
2345 handle = htobs(cr->conn_info->handle);
2346 which = (argc > 1) ? atoi(argv[1]) : 0x01;
2354 if (hci_read_clock(dd, handle, which, &clock, &accuracy, 1000) < 0) {
2355 perror("Reading clock failed");
2359 accuracy = btohs(accuracy);
2361 printf("Clock: 0x%4.4x\n", btohl(clock));
2362 printf("Accuracy: %.2f msec\n", (float) accuracy * 0.3125);
2367 static int read_flags(uint8_t *flags, const uint8_t *data, size_t size)
2371 if (!flags || !data)
2375 while (offset < size) {
2376 uint8_t len = data[offset];
2379 /* Check if it is the end of the significant part */
2383 if (len + offset > size)
2386 type = data[offset + 1];
2388 if (type == FLAGS_AD_TYPE) {
2389 *flags = data[offset + 2];
2399 static int check_report_filter(uint8_t procedure, le_advertising_info *info)
2403 /* If no discovery procedure is set, all reports are treat as valid */
2407 /* Read flags AD type value from the advertising report if it exists */
2408 if (read_flags(&flags, info->data, info->length))
2411 switch (procedure) {
2412 case 'l': /* Limited Discovery Procedure */
2413 if (flags & FLAGS_LIMITED_MODE_BIT)
2416 case 'g': /* General Discovery Procedure */
2417 if (flags & (FLAGS_LIMITED_MODE_BIT | FLAGS_GENERAL_MODE_BIT))
2421 fprintf(stderr, "Unknown discovery procedure\n");
2427 static void sigint_handler(int sig)
2429 signal_received = sig;
2432 static void eir_parse_name(uint8_t *eir, size_t eir_len,
2433 char *buf, size_t buf_len)
2438 while (offset < eir_len) {
2439 uint8_t field_len = eir[0];
2442 /* Check for the end of EIR */
2446 if (offset + field_len > eir_len)
2450 case EIR_NAME_SHORT:
2451 case EIR_NAME_COMPLETE:
2452 name_len = field_len - 1;
2453 if (name_len > buf_len)
2456 memcpy(buf, &eir[2], name_len);
2460 offset += field_len + 1;
2461 eir += field_len + 1;
2465 snprintf(buf, buf_len, "(unknown)");
2468 static int print_advertising_devices(int dd, uint8_t filter_type)
2470 unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
2471 struct hci_filter nf, of;
2472 struct sigaction sa;
2477 if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &olen) < 0) {
2478 printf("Could not get socket options\n");
2482 hci_filter_clear(&nf);
2483 hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
2484 hci_filter_set_event(EVT_LE_META_EVENT, &nf);
2486 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
2487 printf("Could not set socket options\n");
2491 memset(&sa, 0, sizeof(sa));
2492 sa.sa_flags = SA_NOCLDSTOP;
2493 sa.sa_handler = sigint_handler;
2494 sigaction(SIGINT, &sa, NULL);
2497 evt_le_meta_event *meta;
2498 le_advertising_info *info;
2501 while ((len = read(dd, buf, sizeof(buf))) < 0) {
2502 if (errno == EINTR && signal_received == SIGINT) {
2507 if (errno == EAGAIN || errno == EINTR)
2512 ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
2513 len -= (1 + HCI_EVENT_HDR_SIZE);
2515 meta = (void *) ptr;
2517 if (meta->subevent != 0x02)
2520 /* Ignoring multiple reports */
2521 info = (le_advertising_info *) (meta->data + 1);
2522 if (check_report_filter(filter_type, info)) {
2525 memset(name, 0, sizeof(name));
2527 ba2str(&info->bdaddr, addr);
2528 eir_parse_name(info->data, info->length,
2529 name, sizeof(name) - 1);
2531 printf("%s %s\n", addr, name);
2536 setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
2544 static struct option lescan_options[] = {
2545 { "help", 0, 0, 'h' },
2546 { "static", 0, 0, 's' },
2547 { "privacy", 0, 0, 'p' },
2548 { "passive", 0, 0, 'P' },
2549 { "whitelist", 0, 0, 'w' },
2550 { "discovery", 1, 0, 'd' },
2551 { "duplicates", 0, 0, 'D' },
2555 static const char *lescan_help =
2557 "\tlescan [--privacy] enable privacy\n"
2558 "\tlescan [--passive] set scan type passive (default active)\n"
2559 "\tlescan [--whitelist] scan for address in the whitelist only\n"
2560 "\tlescan [--discovery=g|l] enable general or limited discovery"
2562 "\tlescan [--duplicates] don't filter duplicates\n";
2564 static void cmd_lescan(int dev_id, int argc, char **argv)
2567 uint8_t own_type = LE_PUBLIC_ADDRESS;
2568 uint8_t scan_type = 0x01;
2569 uint8_t filter_type = 0;
2570 uint8_t filter_policy = 0x00;
2571 uint16_t interval = htobs(0x0010);
2572 uint16_t window = htobs(0x0010);
2573 uint8_t filter_dup = 0x01;
2575 for_each_opt(opt, lescan_options, NULL) {
2578 own_type = LE_RANDOM_ADDRESS;
2581 own_type = LE_RANDOM_ADDRESS;
2584 scan_type = 0x00; /* Passive */
2587 filter_policy = 0x01; /* Whitelist */
2590 filter_type = optarg[0];
2591 if (filter_type != 'g' && filter_type != 'l') {
2592 fprintf(stderr, "Unknown discovery procedure\n");
2596 interval = htobs(0x0012);
2597 window = htobs(0x0012);
2603 printf("%s", lescan_help);
2607 helper_arg(0, 1, &argc, &argv, lescan_help);
2610 dev_id = hci_get_route(NULL);
2612 dd = hci_open_dev(dev_id);
2614 perror("Could not open device");
2618 err = hci_le_set_scan_parameters(dd, scan_type, interval, window,
2619 own_type, filter_policy, 10000);
2621 perror("Set scan parameters failed");
2625 err = hci_le_set_scan_enable(dd, 0x01, filter_dup, 10000);
2627 perror("Enable scan failed");
2631 printf("LE Scan ...\n");
2633 err = print_advertising_devices(dd, filter_type);
2635 perror("Could not receive advertising events");
2639 err = hci_le_set_scan_enable(dd, 0x00, filter_dup, 10000);
2641 perror("Disable scan failed");
2648 static struct option leinfo_options[] = {
2649 { "help", 0, 0, 'h' },
2650 { "static", 0, 0, 's' },
2651 { "random", 0, 0, 'r' },
2655 static const char *leinfo_help =
2657 "\tleinfo [--static] [--random] <bdaddr>\n";
2659 static void cmd_leinfo(int dev_id, int argc, char **argv)
2663 uint8_t features[8];
2664 struct hci_version version;
2665 uint16_t interval, latency, max_ce_length, max_interval, min_ce_length;
2666 uint16_t min_interval, supervision_timeout, window;
2667 uint8_t initiator_filter, own_bdaddr_type, peer_bdaddr_type;
2670 own_bdaddr_type = LE_PUBLIC_ADDRESS;
2671 peer_bdaddr_type = LE_PUBLIC_ADDRESS;
2673 for_each_opt(opt, leinfo_options, NULL) {
2676 own_bdaddr_type = LE_RANDOM_ADDRESS;
2679 peer_bdaddr_type = LE_RANDOM_ADDRESS;
2682 printf("%s", leinfo_help);
2686 helper_arg(1, 1, &argc, &argv, leinfo_help);
2688 str2ba(argv[0], &bdaddr);
2690 printf("Requesting information ...\n");
2693 dev_id = hci_get_route(NULL);
2695 dd = hci_open_dev(dev_id);
2697 perror("Could not open device");
2701 interval = htobs(0x0004);
2702 window = htobs(0x0004);
2703 initiator_filter = 0;
2704 min_interval = htobs(0x000F);
2705 max_interval = htobs(0x000F);
2706 latency = htobs(0x0000);
2707 supervision_timeout = htobs(0x0C80);
2708 min_ce_length = htobs(0x0000);
2709 max_ce_length = htobs(0x0000);
2711 err = hci_le_create_conn(dd, interval, window, initiator_filter,
2712 peer_bdaddr_type, bdaddr, own_bdaddr_type, min_interval,
2713 max_interval, latency, supervision_timeout,
2714 min_ce_length, max_ce_length, &handle, 25000);
2716 perror("Could not create connection");
2720 printf("\tHandle: %d (0x%04x)\n", handle, handle);
2722 if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
2723 char *ver = lmp_vertostr(version.lmp_ver);
2724 printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
2725 "\tManufacturer: %s (%d)\n",
2729 bt_compidtostr(version.manufacturer),
2730 version.manufacturer);
2735 memset(features, 0, sizeof(features));
2736 hci_le_read_remote_features(dd, handle, features, 20000);
2738 printf("\tFeatures: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
2739 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
2740 features[0], features[1], features[2], features[3],
2741 features[4], features[5], features[6], features[7]);
2744 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
2749 static struct option lecc_options[] = {
2750 { "help", 0, 0, 'h' },
2751 { "static", 0, 0, 's' },
2752 { "random", 0, 0, 'r' },
2753 { "whitelist", 0, 0, 'w' },
2757 static const char *lecc_help =
2759 "\tlecc [--static] [--random] <bdaddr>\n"
2760 "\tlecc --whitelist\n";
2762 static void cmd_lecc(int dev_id, int argc, char **argv)
2766 uint16_t interval, latency, max_ce_length, max_interval, min_ce_length;
2767 uint16_t min_interval, supervision_timeout, window, handle;
2768 uint8_t initiator_filter, own_bdaddr_type, peer_bdaddr_type;
2770 own_bdaddr_type = LE_PUBLIC_ADDRESS;
2771 peer_bdaddr_type = LE_PUBLIC_ADDRESS;
2772 initiator_filter = 0; /* Use peer address */
2774 for_each_opt(opt, lecc_options, NULL) {
2777 own_bdaddr_type = LE_RANDOM_ADDRESS;
2780 peer_bdaddr_type = LE_RANDOM_ADDRESS;
2783 initiator_filter = 0x01; /* Use white list */
2786 printf("%s", lecc_help);
2790 helper_arg(0, 1, &argc, &argv, lecc_help);
2793 dev_id = hci_get_route(NULL);
2795 dd = hci_open_dev(dev_id);
2797 perror("Could not open device");
2801 memset(&bdaddr, 0, sizeof(bdaddr_t));
2803 str2ba(argv[0], &bdaddr);
2805 interval = htobs(0x0004);
2806 window = htobs(0x0004);
2807 min_interval = htobs(0x000F);
2808 max_interval = htobs(0x000F);
2809 latency = htobs(0x0000);
2810 supervision_timeout = htobs(0x0C80);
2811 min_ce_length = htobs(0x0001);
2812 max_ce_length = htobs(0x0001);
2814 err = hci_le_create_conn(dd, interval, window, initiator_filter,
2815 peer_bdaddr_type, bdaddr, own_bdaddr_type, min_interval,
2816 max_interval, latency, supervision_timeout,
2817 min_ce_length, max_ce_length, &handle, 25000);
2819 perror("Could not create connection");
2823 printf("Connection handle %d\n", handle);
2828 static struct option lewladd_options[] = {
2829 { "help", 0, 0, 'h' },
2830 { "random", 0, 0, 'r' },
2834 static const char *lewladd_help =
2836 "\tlewladd [--random] <bdaddr>\n";
2838 static void cmd_lewladd(int dev_id, int argc, char **argv)
2842 uint8_t bdaddr_type = LE_PUBLIC_ADDRESS;
2844 for_each_opt(opt, lewladd_options, NULL) {
2847 bdaddr_type = LE_RANDOM_ADDRESS;
2850 printf("%s", lewladd_help);
2855 helper_arg(1, 1, &argc, &argv, lewladd_help);
2858 dev_id = hci_get_route(NULL);
2860 dd = hci_open_dev(dev_id);
2862 perror("Could not open device");
2866 str2ba(argv[0], &bdaddr);
2868 err = hci_le_add_white_list(dd, &bdaddr, bdaddr_type, 1000);
2873 fprintf(stderr, "Can't add to white list: %s(%d)\n",
2874 strerror(-err), -err);
2879 static struct option lewlrm_options[] = {
2880 { "help", 0, 0, 'h' },
2884 static const char *lewlrm_help =
2886 "\tlewlrm <bdaddr>\n";
2888 static void cmd_lewlrm(int dev_id, int argc, char **argv)
2893 for_each_opt(opt, lewlrm_options, NULL) {
2896 printf("%s", lewlrm_help);
2901 helper_arg(1, 1, &argc, &argv, lewlrm_help);
2904 dev_id = hci_get_route(NULL);
2906 dd = hci_open_dev(dev_id);
2908 perror("Could not open device");
2912 str2ba(argv[0], &bdaddr);
2914 err = hci_le_rm_white_list(dd, &bdaddr, LE_PUBLIC_ADDRESS, 1000);
2919 fprintf(stderr, "Can't remove from white list: %s(%d)\n",
2920 strerror(err), err);
2925 static struct option lewlsz_options[] = {
2926 { "help", 0, 0, 'h' },
2930 static const char *lewlsz_help =
2934 static void cmd_lewlsz(int dev_id, int argc, char **argv)
2939 for_each_opt(opt, lewlsz_options, NULL) {
2942 printf("%s", lewlsz_help);
2947 helper_arg(0, 0, &argc, &argv, lewlsz_help);
2950 dev_id = hci_get_route(NULL);
2952 dd = hci_open_dev(dev_id);
2954 perror("Could not open device");
2958 err = hci_le_read_white_list_size(dd, &size, 1000);
2963 fprintf(stderr, "Can't read white list size: %s(%d)\n",
2964 strerror(-err), -err);
2968 printf("White list size: %d\n", size);
2971 static struct option lewlclr_options[] = {
2972 { "help", 0, 0, 'h' },
2976 static const char *lewlclr_help =
2980 static void cmd_lewlclr(int dev_id, int argc, char **argv)
2984 for_each_opt(opt, lewlclr_options, NULL) {
2987 printf("%s", lewlclr_help);
2992 helper_arg(0, 0, &argc, &argv, lewlclr_help);
2995 dev_id = hci_get_route(NULL);
2997 dd = hci_open_dev(dev_id);
2999 perror("Could not open device");
3003 err = hci_le_clear_white_list(dd, 1000);
3008 fprintf(stderr, "Can't clear white list: %s(%d)\n",
3009 strerror(-err), -err);
3014 static struct option lerladd_options[] = {
3015 { "help", 0, 0, 'h' },
3016 { "random", 0, 0, 'r' },
3017 { "local", 1, 0, 'l' },
3018 { "peer", 1, 0, 'p' },
3022 static const char *lerladd_help =
3024 "\tlerladd [--local irk] [--peer irk] [--random] <bdaddr>\n";
3026 static void cmd_lerladd(int dev_id, int argc, char **argv)
3030 uint8_t bdaddr_type = LE_PUBLIC_ADDRESS;
3031 uint8_t local_irk[16], peer_irk[16];
3033 memset(local_irk, 0, 16);
3034 memset(peer_irk, 0, 16);
3036 for_each_opt(opt, lerladd_options, NULL) {
3039 bdaddr_type = LE_RANDOM_ADDRESS;
3042 str2buf(optarg, local_irk, 16);
3045 str2buf(optarg, peer_irk, 16);
3048 printf("%s", lerladd_help);
3053 helper_arg(1, 1, &argc, &argv, lerladd_help);
3056 dev_id = hci_get_route(NULL);
3058 dd = hci_open_dev(dev_id);
3060 perror("Could not open device");
3064 str2ba(argv[0], &bdaddr);
3066 err = hci_le_add_resolving_list(dd, &bdaddr, bdaddr_type,
3067 peer_irk, local_irk, 1000);
3072 fprintf(stderr, "Can't add to resolving list: %s(%d)\n",
3073 strerror(-err), -err);
3078 static struct option lerlrm_options[] = {
3079 { "help", 0, 0, 'h' },
3083 static const char *lerlrm_help =
3085 "\tlerlrm <bdaddr>\n";
3087 static void cmd_lerlrm(int dev_id, int argc, char **argv)
3092 for_each_opt(opt, lerlrm_options, NULL) {
3095 printf("%s", lerlrm_help);
3100 helper_arg(1, 1, &argc, &argv, lerlrm_help);
3103 dev_id = hci_get_route(NULL);
3105 dd = hci_open_dev(dev_id);
3107 perror("Could not open device");
3111 str2ba(argv[0], &bdaddr);
3113 err = hci_le_rm_resolving_list(dd, &bdaddr, LE_PUBLIC_ADDRESS, 1000);
3118 fprintf(stderr, "Can't remove from resolving list: %s(%d)\n",
3119 strerror(err), err);
3124 static struct option lerlclr_options[] = {
3125 { "help", 0, 0, 'h' },
3129 static const char *lerlclr_help =
3133 static void cmd_lerlclr(int dev_id, int argc, char **argv)
3137 for_each_opt(opt, lerlclr_options, NULL) {
3140 printf("%s", lerlclr_help);
3145 helper_arg(0, 0, &argc, &argv, lerlclr_help);
3148 dev_id = hci_get_route(NULL);
3150 dd = hci_open_dev(dev_id);
3152 perror("Could not open device");
3156 err = hci_le_clear_resolving_list(dd, 1000);
3161 fprintf(stderr, "Can't clear resolving list: %s(%d)\n",
3162 strerror(-err), -err);
3167 static struct option lerlsz_options[] = {
3168 { "help", 0, 0, 'h' },
3172 static const char *lerlsz_help =
3176 static void cmd_lerlsz(int dev_id, int argc, char **argv)
3181 for_each_opt(opt, lerlsz_options, NULL) {
3184 printf("%s", lerlsz_help);
3189 helper_arg(0, 0, &argc, &argv, lerlsz_help);
3192 dev_id = hci_get_route(NULL);
3194 dd = hci_open_dev(dev_id);
3196 perror("Could not open device");
3200 err = hci_le_read_resolving_list_size(dd, &size, 1000);
3205 fprintf(stderr, "Can't read resolving list size: %s(%d)\n",
3206 strerror(-err), -err);
3210 printf("Resolving list size: %d\n", size);
3213 static struct option lerlon_options[] = {
3214 { "help", 0, 0, 'h' },
3218 static const char *lerlon_help =
3222 static void cmd_lerlon(int dev_id, int argc, char **argv)
3226 for_each_opt(opt, lerlon_options, NULL) {
3229 printf("%s", lerlon_help);
3234 helper_arg(0, 0, &argc, &argv, lerlon_help);
3237 dev_id = hci_get_route(NULL);
3239 dd = hci_open_dev(dev_id);
3241 perror("Could not open device");
3245 err = hci_le_set_address_resolution_enable(dd, 0x01, 1000);
3250 fprintf(stderr, "Can't set address resolution enable: %s(%d)\n",
3251 strerror(-err), -err);
3256 static struct option lerloff_options[] = {
3257 { "help", 0, 0, 'h' },
3261 static const char *lerloff_help =
3265 static void cmd_lerloff(int dev_id, int argc, char **argv)
3269 for_each_opt(opt, lerloff_options, NULL) {
3272 printf("%s", lerloff_help);
3277 helper_arg(0, 0, &argc, &argv, lerloff_help);
3280 dev_id = hci_get_route(NULL);
3282 dd = hci_open_dev(dev_id);
3284 perror("Could not open device");
3288 err = hci_le_set_address_resolution_enable(dd, 0x00, 1000);
3293 fprintf(stderr, "Can't set address resolution enable: %s(%d)\n",
3294 strerror(-err), -err);
3299 static struct option ledc_options[] = {
3300 { "help", 0, 0, 'h' },
3304 static const char *ledc_help =
3306 "\tledc <handle> [reason]\n";
3308 static void cmd_ledc(int dev_id, int argc, char **argv)
3314 for_each_opt(opt, ledc_options, NULL) {
3317 printf("%s", ledc_help);
3321 helper_arg(1, 2, &argc, &argv, ledc_help);
3324 dev_id = hci_get_route(NULL);
3326 dd = hci_open_dev(dev_id);
3328 perror("Could not open device");
3332 handle = atoi(argv[0]);
3334 reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
3336 err = hci_disconnect(dd, handle, reason, 10000);
3338 perror("Could not disconnect");
3345 static struct option lecup_options[] = {
3346 { "help", 0, 0, 'h' },
3347 { "handle", 1, 0, 'H' },
3348 { "min", 1, 0, 'm' },
3349 { "max", 1, 0, 'M' },
3350 { "latency", 1, 0, 'l' },
3351 { "timeout", 1, 0, 't' },
3355 static const char *lecup_help =
3357 "\tlecup <handle> <min> <max> <latency> <timeout>\n"
3359 "\t --handle=<0xXXXX> LE connection handle\n"
3360 "\t --min=<interval> Range: 0x0006 to 0x0C80\n"
3361 "\t --max=<interval> Range: 0x0006 to 0x0C80\n"
3362 "\t --latency=<range> Slave latency. Range: 0x0000 to 0x03E8\n"
3363 "\t --timeout=<time> N * 10ms. Range: 0x000A to 0x0C80\n"
3364 "\n\t min/max range: 7.5ms to 4s. Multiply factor: 1.25ms"
3365 "\n\t timeout range: 100ms to 32.0s. Larger than max interval\n";
3367 static void cmd_lecup(int dev_id, int argc, char **argv)
3369 uint16_t handle = 0, min, max, latency, timeout;
3373 /* Aleatory valid values */
3379 for_each_opt(opt, lecup_options, NULL) {
3382 handle = strtoul(optarg, NULL, 0);
3385 min = strtoul(optarg, NULL, 0);
3388 max = strtoul(optarg, NULL, 0);
3391 latency = strtoul(optarg, NULL, 0);
3394 timeout = strtoul(optarg, NULL, 0);
3397 printf("%s", lecup_help);
3405 helper_arg(5, 5, &argc, &argv, lecup_help);
3407 handle = strtoul(argv[0], NULL, 0);
3408 min = strtoul(argv[1], NULL, 0);
3409 max = strtoul(argv[2], NULL, 0);
3410 latency = strtoul(argv[3], NULL, 0);
3411 timeout = strtoul(argv[4], NULL, 0);
3415 printf("%s", lecup_help);
3420 dev_id = hci_get_route(NULL);
3422 dd = hci_open_dev(dev_id);
3424 fprintf(stderr, "HCI device open failed\n");
3428 if (hci_le_conn_update(dd, htobs(handle), htobs(min), htobs(max),
3429 htobs(latency), htobs(timeout), 5000) < 0) {
3431 fprintf(stderr, "Could not change connection params: %s(%d)\n",
3432 strerror(-err), -err);
3440 void (*func)(int dev_id, int argc, char **argv);
3443 { "dev", cmd_dev, "Display local devices" },
3444 { "inq", cmd_inq, "Inquire remote devices" },
3445 { "scan", cmd_scan, "Scan for remote devices" },
3446 { "name", cmd_name, "Get name from remote device" },
3447 { "info", cmd_info, "Get information from remote device" },
3448 { "spinq", cmd_spinq, "Start periodic inquiry" },
3449 { "epinq", cmd_epinq, "Exit periodic inquiry" },
3450 { "cmd", cmd_cmd, "Submit arbitrary HCI commands" },
3451 #ifdef __TIZEN_PATCH__
3452 { "acl", cmd_data, "Submit arbitrary ACL data" },
3454 { "con", cmd_con, "Display active connections" },
3455 { "cc", cmd_cc, "Create connection to remote device" },
3456 { "dc", cmd_dc, "Disconnect from remote device" },
3457 { "sr", cmd_sr, "Switch master/slave role" },
3458 { "cpt", cmd_cpt, "Change connection packet type" },
3459 { "rssi", cmd_rssi, "Display connection RSSI" },
3460 { "lq", cmd_lq, "Display link quality" },
3461 { "tpl", cmd_tpl, "Display transmit power level" },
3462 { "afh", cmd_afh, "Display AFH channel map" },
3463 { "lp", cmd_lp, "Set/display link policy settings" },
3464 { "lst", cmd_lst, "Set/display link supervision timeout" },
3465 { "auth", cmd_auth, "Request authentication" },
3466 { "enc", cmd_enc, "Set connection encryption" },
3467 { "key", cmd_key, "Change connection link key" },
3468 { "clkoff", cmd_clkoff, "Read clock offset" },
3469 { "clock", cmd_clock, "Read local or remote clock" },
3470 { "lescan", cmd_lescan, "Start LE scan" },
3471 { "leinfo", cmd_leinfo, "Get LE remote information" },
3472 { "lewladd", cmd_lewladd, "Add device to LE White List" },
3473 { "lewlrm", cmd_lewlrm, "Remove device from LE White List" },
3474 { "lewlsz", cmd_lewlsz, "Read size of LE White List" },
3475 { "lewlclr", cmd_lewlclr, "Clear LE White List" },
3476 { "lerladd", cmd_lerladd, "Add device to LE Resolving List" },
3477 { "lerlrm", cmd_lerlrm, "Remove device from LE Resolving List" },
3478 { "lerlclr", cmd_lerlclr, "Clear LE Resolving List" },
3479 { "lerlsz", cmd_lerlsz, "Read size of LE Resolving List" },
3480 { "lerlon", cmd_lerlon, "Enable LE Address Resolution" },
3481 { "lerloff", cmd_lerloff, "Disable LE Address Resolution" },
3482 { "lecc", cmd_lecc, "Create a LE Connection" },
3483 { "ledc", cmd_ledc, "Disconnect a LE Connection" },
3484 { "lecup", cmd_lecup, "LE Connection Update" },
3488 static void usage(void)
3492 printf("hcitool - HCI Tool ver %s\n", VERSION);
3494 "\thcitool [options] <command> [command parameters]\n");
3496 "\t--help\tDisplay help\n"
3497 "\t-i dev\tHCI device\n");
3498 printf("Commands:\n");
3499 for (i = 0; command[i].cmd; i++)
3500 printf("\t%-4s\t%s\n", command[i].cmd,
3503 "For more information on the usage of each command use:\n"
3504 "\thcitool <command> --help\n" );
3507 static struct option main_options[] = {
3508 { "help", 0, 0, 'h' },
3509 { "device", 1, 0, 'i' },
3513 int main(int argc, char *argv[])
3515 int opt, i, dev_id = -1;
3518 while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
3521 dev_id = hci_devid(optarg);
3523 perror("Invalid device");
3544 if (dev_id != -1 && hci_devba(dev_id, &ba) < 0) {
3545 perror("Device is not available");
3549 for (i = 0; command[i].cmd; i++) {
3550 if (strncmp(command[i].cmd,
3551 argv[0], strlen(command[i].cmd)))
3554 command[i].func(dev_id, argc, argv);
3558 if (command[i].cmd == 0) {
3559 fprintf(stderr, "Unknown command - \"%s\"\n", *argv);