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-2008 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>
42
#include <bluetooth/bluetooth.h>
43
#include <bluetooth/hci.h>
44
#include <bluetooth/hci_lib.h>
49
#define for_each_opt(opt, long, short) while ((opt=getopt_long(argc, argv, short ? short:"+", long, NULL)) != -1)
51
static void usage(void);
53
static int dev_info(int s, int dev_id, long arg)
55
struct hci_dev_info di = { dev_id: dev_id };
58
if (ioctl(s, HCIGETDEVINFO, (void *) &di))
61
ba2str(&di.bdaddr, addr);
62
printf("\t%s\t%s\n", di.name, addr);
66
static char *type2str(uint8_t type)
80
static int conn_list(int s, int dev_id, long arg)
82
struct hci_conn_list_req *cl;
83
struct hci_conn_info *ci;
87
if (id != -1 && dev_id != id)
90
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
91
perror("Can't allocate memory");
98
if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
99
perror("Can't get connection list");
103
for (i = 0; i < cl->conn_num; i++, ci++) {
105
ba2str(&ci->bdaddr, addr);
106
printf("\t%s %s %s handle %d state %d lm %s\n",
107
ci->out ? "<" : ">", type2str(ci->type),
108
addr, ci->handle, ci->state,
109
hci_lmtostr(ci->link_mode));
115
static int find_conn(int s, int dev_id, long arg)
117
struct hci_conn_list_req *cl;
118
struct hci_conn_info *ci;
121
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
122
perror("Can't allocate memory");
129
if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
130
perror("Can't get connection list");
134
for (i = 0; i < cl->conn_num; i++, ci++)
135
if (!bacmp((bdaddr_t *) arg, &ci->bdaddr))
141
static void hex_dump(char *pref, int width, unsigned char *buf, int len)
145
for (i = 0, n = 1; i < len; i++, n++) {
148
printf("%2.2X ", buf[i]);
158
static char *get_minor_device_name(int major, int minor)
163
case 1: /* computer */
166
return "Uncategorized";
168
return "Desktop workstation";
184
return "Uncategorized";
190
return "Smart phone";
192
return "Wired modem or voice gateway";
194
return "Common ISDN Access";
196
return "Sim Card Reader";
199
case 3: /* lan access */
201
return "Uncategorized";
204
return "Fully available";
206
return "1-17% utilized";
208
return "17-33% utilized";
210
return "33-50% utilized";
212
return "50-67% utilized";
214
return "67-83% utilized";
216
return "83-99% utilized";
218
return "No service available";
221
case 4: /* audio/video */
224
return "Uncategorized";
226
return "Device conforms to the Headset profile";
233
return "Loudspeaker";
237
return "Portable Audio";
241
return "Set-top box";
243
return "HiFi Audio Device";
247
return "Video Camera";
251
return "Video Monitor";
253
return "Video Display and Loudspeaker";
255
return "Video Conferencing";
261
case 5: /* peripheral */ {
262
static char cls_str[48]; cls_str[0] = 0;
266
strncpy(cls_str, "Keyboard", sizeof(cls_str));
269
strncpy(cls_str, "Pointing device", sizeof(cls_str));
272
strncpy(cls_str, "Combo keyboard/pointing device", sizeof(cls_str));
275
if((minor & 15) && (strlen(cls_str) > 0))
276
strcat(cls_str, "/");
282
strncat(cls_str, "Joystick", sizeof(cls_str) - strlen(cls_str));
285
strncat(cls_str, "Gamepad", sizeof(cls_str) - strlen(cls_str));
288
strncat(cls_str, "Remote control", sizeof(cls_str) - strlen(cls_str));
291
strncat(cls_str, "Sensing device", sizeof(cls_str) - strlen(cls_str));
294
strncat(cls_str, "Digitizer tablet", sizeof(cls_str) - strlen(cls_str));
297
strncat(cls_str, "Card reader", sizeof(cls_str) - strlen(cls_str));
300
strncat(cls_str, "(reserved)", sizeof(cls_str) - strlen(cls_str));
303
if(strlen(cls_str) > 0)
306
case 6: /* imaging */
316
case 7: /* wearable */
319
return "Wrist Watch";
337
return "Doll / Action Figure";
344
case 63: /* uncategorised */
347
return "Unknown (reserved) minor device class";
350
static char *major_classes[] = {
351
"Miscellaneous", "Computer", "Phone", "LAN Access",
352
"Audio/Video", "Peripheral", "Imaging", "Uncategorized"
355
static char *get_device_name(const bdaddr_t *local, const bdaddr_t *peer)
357
char filename[PATH_MAX + 1], addr[18];
360
create_name(filename, PATH_MAX, STORAGEDIR, addr, "names");
363
return textfile_get(filename, addr);
366
/* Display local devices */
368
static struct option dev_options[] = {
369
{ "help", 0, 0, 'h' },
373
static const char *dev_help =
377
static void cmd_dev(int dev_id, int argc, char **argv)
381
for_each_opt(opt, dev_options, NULL) {
389
printf("Devices:\n");
391
hci_for_each_dev(HCI_UP, dev_info, 0);
396
static struct option inq_options[] = {
397
{ "help", 0, 0, 'h' },
398
{ "length", 1, 0, 'l' },
399
{ "numrsp", 1, 0, 'n' },
400
{ "iac", 1, 0, 'i' },
401
{ "flush", 0, 0, 'f' },
405
static const char *inq_help =
407
"\tinq [--length=N] maximum inquiry duration in 1.28 s units\n"
408
"\t [--numrsp=N] specify maximum number of inquiry responses\n"
409
"\t [--iac=lap] specify the inquiry access code\n"
410
"\t [--flush] flush the inquiry cache\n";
412
static void cmd_inq(int dev_id, int argc, char **argv)
414
inquiry_info *info = NULL;
415
uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
416
int num_rsp, length, flags;
420
length = 8; /* ~10 seconds */
424
for_each_opt(opt, inq_options, NULL) {
427
length = atoi(optarg);
431
num_rsp = atoi(optarg);
435
l = strtoul(optarg, 0, 16);
436
if (!strcasecmp(optarg, "giac")) {
438
} else if (!strcasecmp(optarg, "liac")) {
440
} if (l < 0x9e8b00 || l > 0x9e8b3f) {
441
printf("Invalid access code 0x%x\n", l);
445
lap[1] = (l >> 8) & 0xff;
446
lap[2] = (l >> 16) & 0xff;
450
flags |= IREQ_CACHE_FLUSH;
459
printf("Inquiring ...\n");
461
num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
463
perror("Inquiry failed.");
467
for (i = 0; i < num_rsp; i++) {
468
ba2str(&(info+i)->bdaddr, addr);
469
printf("\t%s\tclock offset: 0x%4.4x\tclass: 0x%2.2x%2.2x%2.2x\n",
470
addr, btohs((info+i)->clock_offset),
471
(info+i)->dev_class[2],
472
(info+i)->dev_class[1],
473
(info+i)->dev_class[0]);
479
/* Device scanning */
481
static struct option scan_options[] = {
482
{ "help", 0, 0, 'h' },
483
{ "length", 1, 0, 'l' },
484
{ "numrsp", 1, 0, 'n' },
485
{ "iac", 1, 0, 'i' },
486
{ "flush", 0, 0, 'f' },
487
{ "refresh", 0, 0, 'r' },
488
{ "class", 0, 0, 'C' },
489
{ "info", 0, 0, 'I' },
490
{ "oui", 0, 0, 'O' },
491
{ "all", 0, 0, 'A' },
492
{ "ext", 0, 0, 'A' },
496
static const char *scan_help =
498
"\tscan [--length=N] [--numrsp=N] [--iac=lap] [--flush] [--class] [--info] [--oui] [--refresh]\n";
500
static void cmd_scan(int dev_id, int argc, char **argv)
502
inquiry_info *info = NULL;
503
uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
504
int num_rsp, length, flags;
505
uint8_t cls[3], features[8];
507
char addr[18], name[249], oui[9], *comp, *tmp;
508
struct hci_version version;
509
struct hci_dev_info di;
510
struct hci_conn_info_req *cr;
511
int refresh = 0, extcls = 0, extinf = 0, extoui = 0;
512
int i, n, l, opt, dd, cc, nc;
514
length = 8; /* ~10 seconds */
518
for_each_opt(opt, scan_options, NULL) {
521
length = atoi(optarg);
525
num_rsp = atoi(optarg);
529
l = strtoul(optarg, 0, 16);
530
if (!strcasecmp(optarg, "giac")) {
532
} else if (!strcasecmp(optarg, "liac")) {
534
} else if (l < 0x9e8b00 || l > 0x9e8b3f) {
535
printf("Invalid access code 0x%x\n", l);
539
lap[1] = (l >> 8) & 0xff;
540
lap[2] = (l >> 16) & 0xff;
544
flags |= IREQ_CACHE_FLUSH;
576
dev_id = hci_get_route(NULL);
578
perror("Device is not available");
583
if (hci_devinfo(dev_id, &di) < 0) {
584
perror("Can't get device info");
588
printf("Scanning ...\n");
589
num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
591
perror("Inquiry failed");
595
dd = hci_open_dev(dev_id);
597
perror("HCI device open failed");
602
if (extcls || extinf || extoui)
605
for (i = 0; i < num_rsp; i++) {
607
memset(name, 0, sizeof(name));
608
tmp = get_device_name(&di.bdaddr, &(info+i)->bdaddr);
610
strncpy(name, tmp, 249);
618
if (!extcls && !extinf && !extoui) {
619
ba2str(&(info+i)->bdaddr, addr);
622
printf("\t%s\t%s\n", addr, name);
626
if (hci_read_remote_name_with_clock_offset(dd,
628
(info+i)->pscan_rep_mode,
629
(info+i)->clock_offset | 0x8000,
630
sizeof(name), name, 100000) < 0)
633
for (n = 0; n < 248 && name[n]; n++) {
634
if ((unsigned char) name[i] < 32 || name[i] == 127)
640
printf("\t%s\t%s\n", addr, name);
644
ba2str(&(info+i)->bdaddr, addr);
645
printf("BD Address:\t%s [mode %d, clkoffset 0x%4.4x]\n", addr,
646
(info+i)->pscan_rep_mode, btohs((info+i)->clock_offset));
649
ba2oui(&(info+i)->bdaddr, oui);
650
comp = ouitocomp(oui);
652
printf("OUI company:\t%s (%s)\n", comp, oui);
660
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
662
bacpy(&cr->bdaddr, &(info+i)->bdaddr);
664
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
668
handle = htobs(cr->conn_info->handle);
675
if (hci_create_connection(dd, &(info+i)->bdaddr,
676
htobs(di.pkt_type & ACL_PTYPE_MASK),
677
(info+i)->clock_offset | 0x8000,
678
0x01, &handle, 25000) < 0) {
685
if (handle > 0 || !nc) {
686
if (hci_read_remote_name_with_clock_offset(dd,
688
(info+i)->pscan_rep_mode,
689
(info+i)->clock_offset | 0x8000,
690
sizeof(name), name, 100000) < 0) {
694
for (n = 0; n < 248 && name[n]; n++) {
695
if ((unsigned char) name[i] < 32 || name[i] == 127)
704
if (strlen(name) > 0)
705
printf("Device name:\t%s%s\n", name, nc ? " [cached]" : "");
708
memcpy(cls, (info+i)->dev_class, 3);
709
printf("Device class:\t");
710
if ((cls[1] & 0x1f) > sizeof(major_classes) / sizeof(char *))
713
printf("%s, %s", major_classes[cls[1] & 0x1f],
714
get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));
715
printf(" (0x%2.2x%2.2x%2.2x)\n", cls[2], cls[1], cls[0]);
718
if (extinf && handle > 0) {
719
if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
720
char *ver = lmp_vertostr(version.lmp_ver);
721
printf("Manufacturer:\t%s (%d)\n",
722
bt_compidtostr(version.manufacturer),
723
version.manufacturer);
724
printf("LMP version:\t%s (0x%x) [subver 0x%x]\n",
726
version.lmp_ver, version.lmp_subver);
731
if (hci_read_remote_features(dd, handle, features, 20000) == 0) {
732
char *tmp = lmp_featurestostr(features, "\t\t", 63);
733
printf("LMP features:\t0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x"
734
" 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
735
features[0], features[1],
736
features[2], features[3],
737
features[4], features[5],
738
features[6], features[7]);
745
hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
759
static struct option name_options[] = {
760
{ "help", 0, 0, 'h' },
764
static const char *name_help =
768
static void cmd_name(int dev_id, int argc, char **argv)
774
for_each_opt(opt, name_options, NULL) {
789
str2ba(argv[0], &bdaddr);
792
dev_id = hci_get_route(&bdaddr);
794
fprintf(stderr, "Device is not available.\n");
799
dd = hci_open_dev(dev_id);
801
perror("HCI device open failed");
805
if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
806
printf("%s\n", name);
811
/* Info about remote device */
813
static struct option info_options[] = {
814
{ "help", 0, 0, 'h' },
818
static const char *info_help =
822
static void cmd_info(int dev_id, int argc, char **argv)
826
uint8_t features[8], max_page = 0;
827
char name[249], oui[9], *comp, *tmp;
828
struct hci_version version;
829
struct hci_dev_info di;
830
struct hci_conn_info_req *cr;
831
int i, opt, dd, cc = 0;
833
for_each_opt(opt, info_options, NULL) {
848
str2ba(argv[0], &bdaddr);
851
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
854
dev_id = hci_get_route(&bdaddr);
857
fprintf(stderr, "Device is not available or not connected.\n");
861
if (hci_devinfo(dev_id, &di) < 0) {
862
perror("Can't get device info");
866
printf("Requesting information ...\n");
868
dd = hci_open_dev(dev_id);
870
perror("HCI device open failed");
874
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
876
perror("Can't get connection info");
881
bacpy(&cr->bdaddr, &bdaddr);
883
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
884
if (hci_create_connection(dd, &bdaddr,
885
htobs(di.pkt_type & ACL_PTYPE_MASK),
886
0, 0x01, &handle, 25000) < 0) {
887
perror("Can't create connection");
893
handle = htobs(cr->conn_info->handle);
895
printf("\tBD Address: %s\n", argv[0]);
897
ba2oui(&bdaddr, oui);
898
comp = ouitocomp(oui);
900
printf("\tOUI Company: %s (%s)\n", comp, oui);
904
if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
905
printf("\tDevice Name: %s\n", name);
907
if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
908
char *ver = lmp_vertostr(version.lmp_ver);
909
printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
910
"\tManufacturer: %s (%d)\n",
914
bt_compidtostr(version.manufacturer),
915
version.manufacturer);
920
memset(features, 0, sizeof(features));
921
hci_read_remote_features(dd, handle, features, 20000);
923
if ((di.features[7] & LMP_EXT_FEAT) && (features[7] & LMP_EXT_FEAT))
924
hci_read_remote_ext_features(dd, handle, 0, &max_page,
927
printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
928
"0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
929
(max_page > 0) ? " page 0" : "",
930
features[0], features[1], features[2], features[3],
931
features[4], features[5], features[6], features[7]);
933
tmp = lmp_featurestostr(features, "\t\t", 63);
937
for (i = 1; i <= max_page; i++) {
938
if (hci_read_remote_ext_features(dd, handle, i, NULL,
939
features, 20000) < 0)
942
printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
943
"0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i,
944
features[0], features[1], features[2], features[3],
945
features[4], features[5], features[6], features[7]);
950
hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
956
/* Start periodic inquiry */
958
static struct option spinq_options[] = {
959
{ "help", 0, 0, 'h' },
963
static const char *spinq_help =
967
static void cmd_spinq(int dev_id, int argc, char **argv)
969
uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
970
struct hci_request rq;
971
periodic_inquiry_cp cp;
974
for_each_opt(opt, spinq_options, NULL) {
983
dev_id = hci_get_route(NULL);
985
dd = hci_open_dev(dev_id);
987
perror("Device open failed");
991
memset(&cp, 0, sizeof(cp));
992
memcpy(cp.lap, lap, 3);
993
cp.max_period = htobs(16);
994
cp.min_period = htobs(10);
998
memset(&rq, 0, sizeof(rq));
999
rq.ogf = OGF_LINK_CTL;
1000
rq.ocf = OCF_PERIODIC_INQUIRY;
1002
rq.clen = PERIODIC_INQUIRY_CP_SIZE;
1004
if (hci_send_req(dd, &rq, 100) < 0) {
1005
perror("Periodic inquiry failed");
1012
/* Exit periodic inquiry */
1014
static struct option epinq_options[] = {
1015
{ "help", 0, 0, 'h' },
1019
static const char *epinq_help =
1023
static void cmd_epinq(int dev_id, int argc, char **argv)
1027
for_each_opt(opt, epinq_options, NULL) {
1036
dev_id = hci_get_route(NULL);
1038
dd = hci_open_dev(dev_id);
1040
perror("Device open failed");
1044
if (hci_send_cmd(dd, OGF_LINK_CTL,
1045
OCF_EXIT_PERIODIC_INQUIRY, 0, NULL) < 0) {
1046
perror("Exit periodic inquiry failed");
1053
/* Send arbitrary HCI commands */
1055
static struct option cmd_options[] = {
1056
{ "help", 0, 0, 'h' },
1060
static const char *cmd_help =
1062
"\tcmd <ogf> <ocf> [parameters]\n"
1064
"\tcmd 0x03 0x0013 0x41 0x42 0x43 0x44\n";
1066
static void cmd_cmd(int dev_id, int argc, char **argv)
1068
unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf;
1069
struct hci_filter flt;
1071
int i, opt, len, dd;
1075
for_each_opt(opt, cmd_options, NULL) {
1091
dev_id = hci_get_route(NULL);
1094
ogf = strtol(argv[0], NULL, 16);
1095
ocf = strtol(argv[1], NULL, 16);
1096
if (errno == ERANGE || (ogf > 0x3f) || (ocf > 0x3ff)) {
1101
for (i = 2, len = 0; i < argc && len < sizeof(buf); i++, len++)
1102
*ptr++ = (uint8_t) strtol(argv[i], NULL, 16);
1104
dd = hci_open_dev(dev_id);
1106
perror("Device open failed");
1111
hci_filter_clear(&flt);
1112
hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
1113
hci_filter_all_events(&flt);
1114
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1115
perror("HCI filter setup failed");
1119
printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);
1120
hex_dump(" ", 20, buf, len); fflush(stdout);
1122
if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
1123
perror("Send failed");
1127
len = read(dd, buf, sizeof(buf));
1129
perror("Read failed");
1133
hdr = (void *)(buf + 1);
1134
ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
1135
len -= (1 + HCI_EVENT_HDR_SIZE);
1137
printf("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);
1138
hex_dump(" ", 20, ptr, len); fflush(stdout);
1143
/* Display active connections */
1145
static struct option con_options[] = {
1146
{ "help", 0, 0, 'h' },
1150
static const char *con_help =
1154
static void cmd_con(int dev_id, int argc, char **argv)
1158
for_each_opt(opt, con_options, NULL) {
1166
printf("Connections:\n");
1168
hci_for_each_dev(HCI_UP, conn_list, dev_id);
1171
/* Create connection */
1173
static struct option cc_options[] = {
1174
{ "help", 0, 0, 'h' },
1175
{ "role", 1, 0, 'r' },
1176
{ "ptype", 1, 0, 'p' },
1180
static const char *cc_help =
1182
"\tcc [--role=m|s] [--ptype=pkt_types] <bdaddr>\n"
1184
"\tcc --ptype=dm1,dh3,dh5 01:02:03:04:05:06\n"
1185
"\tcc --role=m 01:02:03:04:05:06\n";
1187
static void cmd_cc(int dev_id, int argc, char **argv)
1196
ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;
1198
for_each_opt(opt, cc_options, NULL) {
1201
hci_strtoptype(optarg, &ptype);
1205
role = optarg[0] == 'm' ? 0 : 1;
1221
str2ba(argv[0], &bdaddr);
1224
dev_id = hci_get_route(&bdaddr);
1226
fprintf(stderr, "Device is not available.\n");
1231
dd = hci_open_dev(dev_id);
1233
perror("HCI device open failed");
1237
if (hci_create_connection(dd, &bdaddr, htobs(ptype),
1238
htobs(0x0000), role, &handle, 25000) < 0)
1239
perror("Can't create connection");
1244
/* Close connection */
1246
static struct option dc_options[] = {
1247
{ "help", 0, 0, 'h' },
1251
static const char *dc_help =
1253
"\tdc <bdaddr> [reason]\n";
1255
static void cmd_dc(int dev_id, int argc, char **argv)
1257
struct hci_conn_info_req *cr;
1262
for_each_opt(opt, dc_options, NULL) {
1277
str2ba(argv[0], &bdaddr);
1278
reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
1281
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1283
fprintf(stderr, "Not connected.\n");
1288
dd = hci_open_dev(dev_id);
1290
perror("HCI device open failed");
1294
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1296
perror("Can't allocate memory");
1300
bacpy(&cr->bdaddr, &bdaddr);
1301
cr->type = ACL_LINK;
1302
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1303
perror("Get connection info failed");
1307
if (hci_disconnect(dd, htobs(cr->conn_info->handle),
1309
perror("Disconnect failed");
1318
static struct option sr_options[] = {
1319
{ "help", 0, 0, 'h' },
1323
static const char *sr_help =
1325
"\tsr <bdaddr> <role>\n";
1327
static void cmd_sr(int dev_id, int argc, char **argv)
1333
for_each_opt(opt, sr_options, NULL) {
1348
str2ba(argv[0], &bdaddr);
1349
switch (argv[1][0]) {
1357
role = atoi(argv[1]);
1362
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1364
fprintf(stderr, "Not connected.\n");
1369
dd = hci_open_dev(dev_id);
1371
perror("HCI device open failed");
1375
if (hci_switch_role(dd, &bdaddr, role, 10000) < 0) {
1376
perror("Switch role request failed");
1385
static struct option rssi_options[] = {
1386
{ "help", 0, 0, 'h' },
1390
static const char *rssi_help =
1392
"\trssi <bdaddr>\n";
1394
static void cmd_rssi(int dev_id, int argc, char **argv)
1396
struct hci_conn_info_req *cr;
1401
for_each_opt(opt, rssi_options, NULL) {
1416
str2ba(argv[0], &bdaddr);
1419
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1421
fprintf(stderr, "Not connected.\n");
1426
dd = hci_open_dev(dev_id);
1428
perror("HCI device open failed");
1432
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1434
perror("Can't allocate memory");
1438
bacpy(&cr->bdaddr, &bdaddr);
1439
cr->type = ACL_LINK;
1440
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1441
perror("Get connection info failed");
1445
if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
1446
perror("Read RSSI failed");
1450
printf("RSSI return value: %d\n", rssi);
1457
/* Get link quality */
1459
static struct option lq_options[] = {
1460
{ "help", 0, 0, 'h' },
1464
static const char *lq_help =
1468
static void cmd_lq(int dev_id, int argc, char **argv)
1470
struct hci_conn_info_req *cr;
1475
for_each_opt(opt, lq_options, NULL) {
1490
str2ba(argv[0], &bdaddr);
1493
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1495
fprintf(stderr, "Not connected.\n");
1500
dd = hci_open_dev(dev_id);
1502
perror("HCI device open failed");
1506
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1508
perror("Can't allocate memory");
1512
bacpy(&cr->bdaddr, &bdaddr);
1513
cr->type = ACL_LINK;
1514
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1515
perror("Get connection info failed");
1519
if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), &lq, 1000) < 0) {
1520
perror("HCI read_link_quality request failed");
1524
printf("Link quality: %d\n", lq);
1531
/* Get transmit power level */
1533
static struct option tpl_options[] = {
1534
{ "help", 0, 0, 'h' },
1538
static const char *tpl_help =
1540
"\ttpl <bdaddr> [type]\n";
1542
static void cmd_tpl(int dev_id, int argc, char **argv)
1544
struct hci_conn_info_req *cr;
1550
for_each_opt(opt, tpl_options, NULL) {
1565
str2ba(argv[0], &bdaddr);
1566
type = (argc > 1) ? atoi(argv[1]) : 0;
1569
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1571
fprintf(stderr, "Not connected.\n");
1576
dd = hci_open_dev(dev_id);
1578
perror("HCI device open failed");
1582
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1584
perror("Can't allocate memory");
1588
bacpy(&cr->bdaddr, &bdaddr);
1589
cr->type = ACL_LINK;
1590
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1591
perror("Get connection info failed");
1595
if (hci_read_transmit_power_level(dd, htobs(cr->conn_info->handle), type, &level, 1000) < 0) {
1596
perror("HCI read transmit power level request failed");
1600
printf("%s transmit power level: %d\n",
1601
(type == 0) ? "Current" : "Maximum", level);
1608
/* Get AFH channel map */
1610
static struct option afh_options[] = {
1611
{ "help", 0, 0, 'h' },
1615
static const char *afh_help =
1619
static void cmd_afh(int dev_id, int argc, char **argv)
1621
struct hci_conn_info_req *cr;
1624
uint8_t mode, map[10];
1627
for_each_opt(opt, afh_options, NULL) {
1642
str2ba(argv[0], &bdaddr);
1645
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1647
fprintf(stderr, "Not connected.\n");
1652
dd = hci_open_dev(dev_id);
1654
perror("HCI device open failed");
1658
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1660
perror("Can't allocate memory");
1664
bacpy(&cr->bdaddr, &bdaddr);
1665
cr->type = ACL_LINK;
1666
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1667
perror("Get connection info failed");
1671
handle = htobs(cr->conn_info->handle);
1673
if (hci_read_afh_map(dd, handle, &mode, map, 1000) < 0) {
1674
perror("HCI read AFH map request failed");
1680
printf("AFH map: 0x");
1681
for (i = 0; i < 10; i++)
1682
printf("%02x", map[i]);
1685
printf("AFH disabled\n");
1692
/* Set connection packet type */
1694
static struct option cpt_options[] = {
1695
{ "help", 0, 0, 'h' },
1699
static const char *cpt_help =
1701
"\tcpt <bdaddr> <packet_types>\n";
1703
static void cmd_cpt(int dev_id, int argc, char **argv)
1705
struct hci_conn_info_req *cr;
1706
struct hci_request rq;
1707
set_conn_ptype_cp cp;
1708
evt_conn_ptype_changed rp;
1713
for_each_opt(opt, cpt_options, NULL) {
1728
str2ba(argv[0], &bdaddr);
1729
hci_strtoptype(argv[1], &ptype);
1732
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1734
fprintf(stderr, "Not connected.\n");
1739
dd = hci_open_dev(dev_id);
1741
perror("HCI device open failed");
1745
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1747
perror("Can't allocate memory");
1751
bacpy(&cr->bdaddr, &bdaddr);
1752
cr->type = ACL_LINK;
1753
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1754
perror("Get connection info failed");
1758
cp.handle = htobs(cr->conn_info->handle);
1759
cp.pkt_type = ptype;
1761
memset(&rq, 0, sizeof(rq));
1762
rq.ogf = OGF_LINK_CTL;
1763
rq.ocf = OCF_SET_CONN_PTYPE;
1765
rq.clen = SET_CONN_PTYPE_CP_SIZE;
1767
rq.rlen = EVT_CONN_PTYPE_CHANGED_SIZE;
1768
rq.event = EVT_CONN_PTYPE_CHANGED;
1770
if (hci_send_req(dd, &rq, 100) < 0) {
1771
perror("Packet type change failed");
1780
/* Get/Set link policy settings */
1782
static struct option lp_options[] = {
1783
{ "help", 0, 0, 'h' },
1787
static const char *lp_help =
1789
"\tlp <bdaddr> [link policy]\n";
1791
static void cmd_lp(int dev_id, int argc, char **argv)
1793
struct hci_conn_info_req *cr;
1798
for_each_opt(opt, lp_options, NULL) {
1813
str2ba(argv[0], &bdaddr);
1816
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1818
fprintf(stderr, "Not connected.\n");
1823
dd = hci_open_dev(dev_id);
1825
perror("HCI device open failed");
1829
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1831
perror("Can't allocate memory");
1835
bacpy(&cr->bdaddr, &bdaddr);
1836
cr->type = ACL_LINK;
1837
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1838
perror("Get connection info failed");
1844
if (hci_read_link_policy(dd, htobs(cr->conn_info->handle),
1845
&policy, 1000) < 0) {
1846
perror("HCI read_link_policy_settings request failed");
1850
policy = btohs(policy);
1851
str = hci_lptostr(policy);
1853
printf("Link policy settings: %s\n", str);
1856
fprintf(stderr, "Invalig settings\n");
1861
if (hci_strtolp(argv[1], &val) < 0) {
1862
fprintf(stderr, "Invalig arguments\n");
1867
if (hci_write_link_policy(dd, htobs(cr->conn_info->handle),
1868
htobs(policy), 1000) < 0) {
1869
perror("HCI write_link_policy_settings request failed");
1879
/* Get/Set link supervision timeout */
1881
static struct option lst_options[] = {
1882
{ "help", 0, 0, 'h' },
1886
static const char *lst_help =
1888
"\tlst <bdaddr> [new value in slots]\n";
1890
static void cmd_lst(int dev_id, int argc, char **argv)
1892
struct hci_conn_info_req *cr;
1897
for_each_opt(opt, lst_options, NULL) {
1912
str2ba(argv[0], &bdaddr);
1915
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1917
fprintf(stderr, "Not connected.\n");
1922
dd = hci_open_dev(dev_id);
1924
perror("HCI device open failed");
1928
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1930
perror("Can't allocate memory");
1934
bacpy(&cr->bdaddr, &bdaddr);
1935
cr->type = ACL_LINK;
1936
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1937
perror("Get connection info failed");
1942
if (hci_read_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
1943
&timeout, 1000) < 0) {
1944
perror("HCI read_link_supervision_timeout request failed");
1948
timeout = btohs(timeout);
1951
printf("Link supervision timeout: %u slots (%.2f msec)\n",
1952
timeout, (float) timeout * 0.625);
1954
printf("Link supervision timeout never expires\n");
1956
timeout = strtol(argv[1], NULL, 10);
1958
if (hci_write_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
1959
htobs(timeout), 1000) < 0) {
1960
perror("HCI write_link_supervision_timeout request failed");
1970
/* Request authentication */
1972
static struct option auth_options[] = {
1973
{ "help", 0, 0, 'h' },
1977
static const char *auth_help =
1979
"\tauth <bdaddr>\n";
1981
static void cmd_auth(int dev_id, int argc, char **argv)
1983
struct hci_conn_info_req *cr;
1987
for_each_opt(opt, auth_options, NULL) {
2002
str2ba(argv[0], &bdaddr);
2005
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2007
fprintf(stderr, "Not connected.\n");
2012
dd = hci_open_dev(dev_id);
2014
perror("HCI device open failed");
2018
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2020
perror("Can't allocate memory");
2024
bacpy(&cr->bdaddr, &bdaddr);
2025
cr->type = ACL_LINK;
2026
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2027
perror("Get connection info failed");
2031
if (hci_authenticate_link(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2032
perror("HCI authentication request failed");
2041
/* Activate encryption */
2043
static struct option enc_options[] = {
2044
{ "help", 0, 0, 'h' },
2048
static const char *enc_help =
2050
"\tenc <bdaddr> [encrypt enable]\n";
2052
static void cmd_enc(int dev_id, int argc, char **argv)
2054
struct hci_conn_info_req *cr;
2059
for_each_opt(opt, enc_options, NULL) {
2074
str2ba(argv[0], &bdaddr);
2077
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2079
fprintf(stderr, "Not connected.\n");
2084
dd = hci_open_dev(dev_id);
2086
perror("HCI device open failed");
2090
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2092
perror("Can't allocate memory");
2096
bacpy(&cr->bdaddr, &bdaddr);
2097
cr->type = ACL_LINK;
2098
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2099
perror("Get connection info failed");
2103
encrypt = (argc > 1) ? atoi(argv[1]) : 1;
2105
if (hci_encrypt_link(dd, htobs(cr->conn_info->handle), encrypt, 25000) < 0) {
2106
perror("HCI set encryption request failed");
2115
/* Change connection link key */
2117
static struct option key_options[] = {
2118
{ "help", 0, 0, 'h' },
2122
static const char *key_help =
2126
static void cmd_key(int dev_id, int argc, char **argv)
2128
struct hci_conn_info_req *cr;
2132
for_each_opt(opt, key_options, NULL) {
2147
str2ba(argv[0], &bdaddr);
2150
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2152
fprintf(stderr, "Not connected.\n");
2157
dd = hci_open_dev(dev_id);
2159
perror("HCI device open failed");
2163
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2165
perror("Can't allocate memory");
2169
bacpy(&cr->bdaddr, &bdaddr);
2170
cr->type = ACL_LINK;
2171
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2172
perror("Get connection info failed");
2176
if (hci_change_link_key(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2177
perror("Changing link key failed");
2186
/* Read clock offset */
2188
static struct option clkoff_options[] = {
2189
{ "help", 0, 0, 'h' },
2193
static const char *clkoff_help =
2195
"\tclkoff <bdaddr>\n";
2197
static void cmd_clkoff(int dev_id, int argc, char **argv)
2199
struct hci_conn_info_req *cr;
2204
for_each_opt(opt, clkoff_options, NULL) {
2207
printf(clkoff_help);
2215
printf(clkoff_help);
2219
str2ba(argv[0], &bdaddr);
2222
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2224
fprintf(stderr, "Not connected.\n");
2229
dd = hci_open_dev(dev_id);
2231
perror("HCI device open failed");
2235
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2237
perror("Can't allocate memory");
2241
bacpy(&cr->bdaddr, &bdaddr);
2242
cr->type = ACL_LINK;
2243
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2244
perror("Get connection info failed");
2248
if (hci_read_clock_offset(dd, htobs(cr->conn_info->handle), &offset, 1000) < 0) {
2249
perror("Reading clock offset failed");
2253
printf("Clock offset: 0x%4.4x\n", btohs(offset));
2262
static struct option clock_options[] = {
2263
{ "help", 0, 0, 'h' },
2267
static const char *clock_help =
2269
"\tclock [bdaddr] [which clock]\n";
2271
static void cmd_clock(int dev_id, int argc, char **argv)
2273
struct hci_conn_info_req *cr;
2276
uint32_t handle, clock;
2280
for_each_opt(opt, clock_options, NULL) {
2291
str2ba(argv[0], &bdaddr);
2293
bacpy(&bdaddr, BDADDR_ANY);
2295
if (dev_id < 0 && !bacmp(&bdaddr, BDADDR_ANY))
2296
dev_id = hci_get_route(NULL);
2299
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2301
fprintf(stderr, "Not connected.\n");
2306
dd = hci_open_dev(dev_id);
2308
perror("HCI device open failed");
2312
if (bacmp(&bdaddr, BDADDR_ANY)) {
2313
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2315
perror("Can't allocate memory");
2319
bacpy(&cr->bdaddr, &bdaddr);
2320
cr->type = ACL_LINK;
2321
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2322
perror("Get connection info failed");
2327
handle = htobs(cr->conn_info->handle);
2328
which = (argc > 1) ? atoi(argv[1]) : 0x01;
2336
if (hci_read_clock(dd, handle, which, &clock, &accuracy, 1000) < 0) {
2337
perror("Reading clock failed");
2341
accuracy = btohs(accuracy);
2343
printf("Clock: 0x%4.4x\n", btohl(clock));
2344
printf("Accuracy: %.2f msec\n", (float) accuracy * 0.3125);
2351
void (*func)(int dev_id, int argc, char **argv);
2354
{ "dev", cmd_dev, "Display local devices" },
2355
{ "inq", cmd_inq, "Inquire remote devices" },
2356
{ "scan", cmd_scan, "Scan for remote devices" },
2357
{ "name", cmd_name, "Get name from remote device" },
2358
{ "info", cmd_info, "Get information from remote device" },
2359
{ "spinq", cmd_spinq, "Start periodic inquiry" },
2360
{ "epinq", cmd_epinq, "Exit periodic inquiry" },
2361
{ "cmd", cmd_cmd, "Submit arbitrary HCI commands" },
2362
{ "con", cmd_con, "Display active connections" },
2363
{ "cc", cmd_cc, "Create connection to remote device" },
2364
{ "dc", cmd_dc, "Disconnect from remote device" },
2365
{ "sr", cmd_sr, "Switch master/slave role" },
2366
{ "cpt", cmd_cpt, "Change connection packet type" },
2367
{ "rssi", cmd_rssi, "Display connection RSSI" },
2368
{ "lq", cmd_lq, "Display link quality" },
2369
{ "tpl", cmd_tpl, "Display transmit power level" },
2370
{ "afh", cmd_afh, "Display AFH channel map" },
2371
{ "lp", cmd_lp, "Set/display link policy settings" },
2372
{ "lst", cmd_lst, "Set/display link supervision timeout" },
2373
{ "auth", cmd_auth, "Request authentication" },
2374
{ "enc", cmd_enc, "Set connection encryption" },
2375
{ "key", cmd_key, "Change connection link key" },
2376
{ "clkoff", cmd_clkoff, "Read clock offset" },
2377
{ "clock", cmd_clock, "Read local or remote clock" },
2381
static void usage(void)
2385
printf("hcitool - HCI Tool ver %s\n", VERSION);
2387
"\thcitool [options] <command> [command parameters]\n");
2389
"\t--help\tDisplay help\n"
2390
"\t-i dev\tHCI device\n");
2391
printf("Commands:\n");
2392
for (i = 0; command[i].cmd; i++)
2393
printf("\t%-4s\t%s\n", command[i].cmd,
2396
"For more information on the usage of each command use:\n"
2397
"\thcitool <command> --help\n" );
2400
static struct option main_options[] = {
2401
{ "help", 0, 0, 'h' },
2402
{ "device", 1, 0, 'i' },
2406
int main(int argc, char *argv[])
2408
int opt, i, dev_id = -1;
2411
while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
2414
dev_id = hci_devid(optarg);
2416
perror("Invalid device");
2437
if (dev_id != -1 && hci_devba(dev_id, &ba) < 0) {
2438
perror("Device is not available");
2442
for (i = 0; command[i].cmd; i++) {
2443
if (strncmp(command[i].cmd, argv[0], 3))
2445
command[i].func(dev_id, argc, argv);