From 191f4b663978493004239a03ec8d543c15341cd6 Mon Sep 17 00:00:00 2001 From: Lukasz Rymanowski Date: Thu, 12 Feb 2015 18:00:08 +0100 Subject: [PATCH] tools/btgatt-client: Add support for set/get security on the link --- tools/btgatt-client.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c index 75ea1ae6c..cca3e2c9e 100644 --- a/tools/btgatt-client.c +++ b/tools/btgatt-client.c @@ -938,6 +938,66 @@ static void cmd_unregister_notify(struct client *cli, char *cmd_str) printf("Unregistered notify handler with id: %u\n", id); } +static void set_sec_level_usage(void) +{ + printf("Usage: set_sec_level \n" + "level: 1-3\n" + "e.g.:\n" + "\tset-sec-level 2\n"); +} + +static void cmd_set_sec_level(struct client *cli, char *cmd_str) +{ + char *argvbuf[1]; + char **argv = argvbuf; + int argc = 0; + char *endptr = NULL; + int level; + + if (!bt_gatt_client_is_ready(cli->gatt)) { + printf("GATT client not initialized\n"); + return; + } + + if (!parse_args(cmd_str, 1, argv, &argc)) { + printf("Too many arguments\n"); + set_sec_level_usage(); + return; + } + + if (argc < 1) { + set_sec_level_usage(); + return; + } + + level = strtol(argv[0], &endptr, 0); + if (!endptr || *endptr != '\0' || level < 1 || level > 3) { + printf("Invalid level: %s\n", argv[0]); + return; + } + + if (bt_gatt_client_set_sec_level(cli->gatt, level) < 0) + printf("Could not set sec level\n"); + else + printf("Setting security level %d success\n", level); +} + +static void cmd_get_sec_level(struct client *cli, char *cmd_str) +{ + int level; + + if (!bt_gatt_client_is_ready(cli->gatt)) { + printf("GATT client not initialized\n"); + return; + } + + level = bt_gatt_client_get_sec_level(cli->gatt); + if (level < 0) + printf("Could not set sec level\n"); + else + printf("Security level: %u\n", level); +} + static void cmd_help(struct client *cli, char *cmd_str); typedef void (*command_func_t)(struct client *cli, char *cmd_str); @@ -962,6 +1022,10 @@ static struct { "\tSubscribe to not/ind from a characteristic" }, { "unregister-notify", cmd_unregister_notify, "Unregister a not/ind session"}, + { "set-sec-level", cmd_set_sec_level, + "Set security level on le connection"}, + { "get-sec-level", cmd_get_sec_level, + "Get security level on le connection"}, { } }; -- 2.47.3