From 616edf67b1ec56cabf1b28d4e1d1a6e38e69b775 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 29 Aug 2016 14:43:29 -0700 Subject: [PATCH] tools: Add support for loading identities from files --- tools/btmgmt.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tools/btmgmt.c b/tools/btmgmt.c index 0f44ed740..38e959f69 100644 --- a/tools/btmgmt.c +++ b/tools/btmgmt.c @@ -223,18 +223,14 @@ static void print_eir(const uint8_t *eir, uint16_t eir_len) } } -static bool load_identity(uint16_t index, struct mgmt_irk_info *irk) +static bool load_identity(const char *path, struct mgmt_irk_info *irk) { - char identity_path[PATH_MAX]; char *addr, *key; unsigned int type; int n; FILE *fp; - snprintf(identity_path, sizeof(identity_path), - "/sys/kernel/debug/bluetooth/hci%u/identity", index); - - fp = fopen(identity_path, "r"); + fp = fopen(path, "r"); if (!fp) { error("Failed to open identity file: %s", strerror(errno)); return false; @@ -2812,6 +2808,7 @@ static void irks_usage(void) static struct option irks_options[] = { { "help", 0, 0, 'h' }, { "local", 1, 0, 'l' }, + { "file", 1, 0, 'f' }, { 0, 0, 0, 0 } }; @@ -2822,6 +2819,7 @@ static void cmd_irks(struct mgmt *mgmt, uint16_t index, int argc, char **argv) struct mgmt_cp_load_irks *cp; uint8_t buf[sizeof(*cp) + 23 * MAX_IRKS]; uint16_t count, local_index; + char path[PATH_MAX]; int opt; if (index == MGMT_INDEX_NONE) @@ -2830,7 +2828,7 @@ static void cmd_irks(struct mgmt *mgmt, uint16_t index, int argc, char **argv) cp = (void *) buf; count = 0; - while ((opt = getopt_long(argc, argv, "+l:h", + while ((opt = getopt_long(argc, argv, "+l:f:h", irks_options, NULL)) != -1) { switch (opt) { case 'l': @@ -2844,13 +2842,29 @@ static void cmd_irks(struct mgmt *mgmt, uint16_t index, int argc, char **argv) local_index = atoi(optarg + 3); else local_index = atoi(optarg); - if (!load_identity(local_index, &cp->irks[count])) { + snprintf(path, sizeof(path), + "/sys/kernel/debug/bluetooth/hci%u/identity", + local_index); + if (!load_identity(path, &cp->irks[count])) { error("Unable to load identity"); optind = 0; return noninteractive_quit(EXIT_FAILURE); } count++; break; + case 'f': + if (count >= MAX_IRKS) { + error("Number of IRKs exceeded"); + optind = 0; + return noninteractive_quit(EXIT_FAILURE); + } + if (!load_identity(optarg, &cp->irks[count])) { + error("Unable to load identities"); + optind = 0; + return noninteractive_quit(EXIT_FAILURE); + } + count++; + break; case 'h': irks_usage(); optind = 0; -- 2.47.3