tools: Add support for loading identities from files
diff --git a/tools/btmgmt.c b/tools/btmgmt.c
index 0f44ed7..38e959f 100644
--- a/tools/btmgmt.c
+++ b/tools/btmgmt.c
@@ -223,18 +223,14 @@
}
}
-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 struct option irks_options[] = {
{ "help", 0, 0, 'h' },
{ "local", 1, 0, 'l' },
+ { "file", 1, 0, 'f' },
{ 0, 0, 0, 0 }
};
@@ -2822,6 +2819,7 @@
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 @@
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 @@
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;