linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index()
@ 2017-11-10  9:51 ERAMOTO Masaya
  2017-11-10  9:51 ` [PATCH Bluez 2/2] tools/btmgmt: Use {get, set}_index() for --index ERAMOTO Masaya
  2017-11-15 12:25 ` [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index() Luiz Augusto von Dentz
  0 siblings, 2 replies; 3+ messages in thread
From: ERAMOTO Masaya @ 2017-11-10  9:51 UTC (permalink / raw)
  To: linux-bluetooth@vger.kernel.org

---
 tools/btmgmt.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/tools/btmgmt.c b/tools/btmgmt.c
index 552f74411..5b472bff0 100644
--- a/tools/btmgmt.c
+++ b/tools/btmgmt.c
@@ -88,6 +88,22 @@ static int pending_index = 0;
 
 #define PROMPT_ON	COLOR_BLUE "[mgmt]" COLOR_OFF "# "
 
+static void set_index(char *arg)
+{
+	if (!arg || !strcmp(arg, "none") || !strcmp(arg, "any") ||
+						!strcmp(arg, "all"))
+		mgmt_index = MGMT_INDEX_NONE;
+	else if (!strncmp(arg, "hci", 3))
+		mgmt_index = atoi(&arg[3]);
+	else
+		mgmt_index = atoi(arg);
+}
+
+static uint16_t get_index(void)
+{
+	return mgmt_index;
+}
+
 static void update_prompt(uint16_t index)
 {
 	char str[32];
@@ -4535,19 +4551,13 @@ static void cmd_select(struct mgmt *mgmt, uint16_t index,
 	mgmt_cancel_all(mgmt);
 	mgmt_unregister_all(mgmt);
 
-	if (!strcmp(argv[1], "none") || !strcmp(argv[1], "any") ||
-						!strcmp(argv[1], "all"))
-		mgmt_index = MGMT_INDEX_NONE;
-	else if (!strncmp(argv[1], "hci", 3))
-		mgmt_index = atoi(&argv[1][3]);
-	else
-		mgmt_index = atoi(argv[1]);
+	set_index(argv[1]);
 
-	register_mgmt_callbacks(mgmt, mgmt_index);
+	register_mgmt_callbacks(mgmt, get_index());
 
-	print("Selected index %u", mgmt_index);
+	print("Selected index %u", get_index());
 
-	update_prompt(mgmt_index);
+	update_prompt(get_index());
 }
 
 static struct cmd_info interactive_cmd[] = {
@@ -4695,7 +4705,7 @@ static void rl_handler(char *input)
 		c = find_cmd(cmd, interactive_cmd, NELEM(interactive_cmd));
 
 	if (c && c->func) {
-		c->func(mgmt, mgmt_index, argc, argv);
+		c->func(mgmt, get_index(), argc, argv);
 		goto free_we;
 	}
 
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH Bluez 2/2] tools/btmgmt: Use {get, set}_index() for --index
  2017-11-10  9:51 [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index() ERAMOTO Masaya
@ 2017-11-10  9:51 ` ERAMOTO Masaya
  2017-11-15 12:25 ` [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index() Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: ERAMOTO Masaya @ 2017-11-10  9:51 UTC (permalink / raw)
  To: linux-bluetooth@vger.kernel.org

The command select can also set the specified index even if prefix hci
includes uppercase characters.
---
 tools/btmgmt.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/tools/btmgmt.c b/tools/btmgmt.c
index 5b472bff0..452ccd40b 100644
--- a/tools/btmgmt.c
+++ b/tools/btmgmt.c
@@ -93,7 +93,7 @@ static void set_index(char *arg)
 	if (!arg || !strcmp(arg, "none") || !strcmp(arg, "any") ||
 						!strcmp(arg, "all"))
 		mgmt_index = MGMT_INDEX_NONE;
-	else if (!strncmp(arg, "hci", 3))
+	else if(strlen(arg) > 3 && !strncasecmp(arg, "hci", 3))
 		mgmt_index = atoi(&arg[3]);
 	else
 		mgmt_index = atoi(arg);
@@ -4815,18 +4815,13 @@ static void mgmt_debug(const char *str, void *user_data)
 int main(int argc, char *argv[])
 {
 	struct io *input;
-	uint16_t index = MGMT_INDEX_NONE;
 	int status, opt;
 
 	while ((opt = getopt_long(argc, argv, "+hi:",
 						main_options, NULL)) != -1) {
 		switch (opt) {
 		case 'i':
-			if (strlen(optarg) > 3 &&
-					strncasecmp(optarg, "hci", 3) == 0)
-				index = atoi(optarg + 3);
-			else
-				index = atoi(optarg);
+			set_index(optarg);
 			break;
 		case 'h':
 		default:
@@ -4860,10 +4855,10 @@ int main(int argc, char *argv[])
 			return EXIT_FAILURE;
 		}
 
-		c->func(mgmt, index, argc, argv);
+		c->func(mgmt, get_index(), argc, argv);
 	}
 
-	register_mgmt_callbacks(mgmt, index);
+	register_mgmt_callbacks(mgmt, get_index());
 
 	/* Interactive mode */
 	if (!argc)
@@ -4879,12 +4874,10 @@ int main(int argc, char *argv[])
 		rl_erase_empty_line = 1;
 		rl_callback_handler_install(NULL, rl_handler);
 
-		update_prompt(index);
+		update_prompt(get_index());
 		rl_redisplay();
 	}
 
-	mgmt_index = index;
-
 	status = mainloop_run();
 
 	if (input) {
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index()
  2017-11-10  9:51 [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index() ERAMOTO Masaya
  2017-11-10  9:51 ` [PATCH Bluez 2/2] tools/btmgmt: Use {get, set}_index() for --index ERAMOTO Masaya
@ 2017-11-15 12:25 ` Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2017-11-15 12:25 UTC (permalink / raw)
  To: ERAMOTO Masaya; +Cc: linux-bluetooth@vger.kernel.org

Hi Eramoto,

On Fri, Nov 10, 2017 at 11:51 AM, ERAMOTO Masaya
<eramoto.masaya@jp.fujitsu.com> wrote:
> ---
>  tools/btmgmt.c | 32 +++++++++++++++++++++-----------
>  1 file changed, 21 insertions(+), 11 deletions(-)
>
> diff --git a/tools/btmgmt.c b/tools/btmgmt.c
> index 552f74411..5b472bff0 100644
> --- a/tools/btmgmt.c
> +++ b/tools/btmgmt.c
> @@ -88,6 +88,22 @@ static int pending_index = 0;
>
>  #define PROMPT_ON      COLOR_BLUE "[mgmt]" COLOR_OFF "# "
>
> +static void set_index(char *arg)
> +{
> +       if (!arg || !strcmp(arg, "none") || !strcmp(arg, "any") ||
> +                                               !strcmp(arg, "all"))
> +               mgmt_index = MGMT_INDEX_NONE;
> +       else if (!strncmp(arg, "hci", 3))
> +               mgmt_index = atoi(&arg[3]);
> +       else
> +               mgmt_index = atoi(arg);
> +}
> +
> +static uint16_t get_index(void)
> +{
> +       return mgmt_index;
> +}

If all it will do is to return the mgmt_index I don't think we really
need get_index do we?

>  static void update_prompt(uint16_t index)
>  {
>         char str[32];
> @@ -4535,19 +4551,13 @@ static void cmd_select(struct mgmt *mgmt, uint16_t index,
>         mgmt_cancel_all(mgmt);
>         mgmt_unregister_all(mgmt);
>
> -       if (!strcmp(argv[1], "none") || !strcmp(argv[1], "any") ||
> -                                               !strcmp(argv[1], "all"))
> -               mgmt_index = MGMT_INDEX_NONE;
> -       else if (!strncmp(argv[1], "hci", 3))
> -               mgmt_index = atoi(&argv[1][3]);
> -       else
> -               mgmt_index = atoi(argv[1]);
> +       set_index(argv[1]);
>
> -       register_mgmt_callbacks(mgmt, mgmt_index);
> +       register_mgmt_callbacks(mgmt, get_index());
>
> -       print("Selected index %u", mgmt_index);
> +       print("Selected index %u", get_index());
>
> -       update_prompt(mgmt_index);
> +       update_prompt(get_index());
>  }
>
>  static struct cmd_info interactive_cmd[] = {
> @@ -4695,7 +4705,7 @@ static void rl_handler(char *input)
>                 c = find_cmd(cmd, interactive_cmd, NELEM(interactive_cmd));
>
>         if (c && c->func) {
> -               c->func(mgmt, mgmt_index, argc, argv);
> +               c->func(mgmt, get_index(), argc, argv);
>                 goto free_we;
>         }
>
> --
> 2.14.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html



-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-11-15 12:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-10  9:51 [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index() ERAMOTO Masaya
2017-11-10  9:51 ` [PATCH Bluez 2/2] tools/btmgmt: Use {get, set}_index() for --index ERAMOTO Masaya
2017-11-15 12:25 ` [PATCH Bluez 1/2] tools/btmgmt: Introduce {get, set}_index() Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).