From 5327e84a4c0f07e44d8c3b23410c355465b094e5 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 4 Nov 2004 11:23:01 +0000 Subject: Add support for changing the link key of a connection --- tools/hcitool.1 | 6 ++++- tools/hcitool.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/tools/hcitool.1 b/tools/hcitool.1 index 1d87b9a0..c4c69b87 100644 --- a/tools/hcitool.1 +++ b/tools/hcitool.1 @@ -166,7 +166,11 @@ Request authentication for the device with Bluetooth address .BI enc " [encrypt enable]" Enable or disable the encryption for the device with Bluetooth address .IR bdaddr . +.TP +.BI key " " +Change the connection link key for the device with Bluetooth address +.IR bdaddr . .SH AUTHORS -Written by Maxim Krasnyansky +Written by Maxim Krasnyansky and Marcel Holtmann .PP man page by Fabrizio Gennari diff --git a/tools/hcitool.c b/tools/hcitool.c index 6c41483b..4ecead2f 100644 --- a/tools/hcitool.c +++ b/tools/hcitool.c @@ -1506,6 +1506,74 @@ static void cmd_enc(int dev_id, int argc, char **argv) free(cr); } +/* Change connection link key */ + +static struct option key_options[] = { + {"help", 0,0, 'h'}, + {0, 0, 0, 0} +}; + +static char *key_help = + "Usage:\n" + "\tkey \n"; + +static void cmd_key(int dev_id, int argc, char **argv) +{ + struct hci_conn_info_req *cr; + bdaddr_t bdaddr; + int opt, dd; + + for_each_opt(opt, key_options, NULL) { + switch (opt) { + default: + printf(key_help); + return; + } + } + argc -= optind; + argv += optind; + + if (argc < 1) { + printf(key_help); + return; + } + + str2ba(argv[0], &bdaddr); + + if (dev_id < 0) { + dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr); + if (dev_id < 0) { + fprintf(stderr, "Not connected.\n"); + exit(1); + } + } + + dd = hci_open_dev(dev_id); + if (dd < 0) { + perror("HCI device open failed"); + exit(1); + } + + cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info)); + if (!cr) + return; + + bacpy(&cr->bdaddr, &bdaddr); + cr->type = ACL_LINK; + if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) { + perror("Get connection info failed"); + exit(1); + } + + if (hci_change_link_key(dd, htobs(cr->conn_info->handle), 25000) < 0) { + perror("Changing link key failed"); + exit(1); + } + + close(dd); + free(cr); +} + static struct { char *cmd; void (*func)(int dev_id, int argc, char **argv); @@ -1529,6 +1597,7 @@ static struct { { "lst", cmd_lst, "Set/display link supervision timeout" }, { "auth", cmd_auth, "Request authentication" }, { "enc", cmd_enc, "Set connection encryption" }, + { "key", cmd_key, "Change connection link key" }, { NULL, NULL, 0 } }; -- cgit