Skip to contentSkip to conversation

shared/shell: Add proper support for non-interactive mode

PATCH Applied Luiz Augusto von Dentz 1 participant 15 messages Feb 26, 2018 – Feb 28, 2018 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

cover shared/shell: Add proper support for non-interactive mode

From: Luiz Augusto von Dentz <[email protected]>

This adds functionally to bt_shell to properly handle non-interactive mode making all command line tools handle commands given as argument:

Quoted 1 line

./bluetoothctl power on

[CHG] Controller B8:8A:60:D8:17:D7 Class: 0x002c010c Changing power on succeded

While on non-interactive mode readline will not be used and only the command line arguments will be processed, in addition to that there is now bt_shell_noninteractive_quit which shall be used by the command handler to quit when it is done with the command execution.

Luiz Augusto von Dentz (14):
shared/shell: Add bt_shell_{noninteractive_}quit shared/shell: Fix not reseting optind
shared/shell: Fix timeout parameter
shared/shell: Do not quit immediatelly on noninteractive mode shared/shell: Fix not handling sort options shared/shell: Disable bt_shell_prompt_input in noninteractive mode shared/shell: Disable readline while in noninteractive mode sharead/mainloop-glib: Fix mainloop_exit* shared/mainloop-glib: Fix calling g_main_loop_quit with NULL shared/shell: Allow executing submenu commands
client: Call bt_shell_noninteractive_quit when done with command shared/shell: Add bt_shell_usage
shared/shell: Fix parsing of mandatory arguments tools/btmgmt: Port to use bt_shell

 client/advertising.c       |   84 ++-
 client/gatt.c              |  113 ++--
 client/main.c              |  153 +++--
 src/shared/mainloop-glib.c |    5 +
 src/shared/shell.c         |  130 +++-
 src/shared/shell.h         |    4 +
 tools/btmgmt.c             | 1560 ++++++++++++++++----------------------------
 7 files changed, 905 insertions(+), 1144 deletions(-)
Signature · 1 line
2.14.3
author maintainer
#1 lore raw

PATCH 1/14 shared/shell: Add bt_shell_{noninteractive_}quit

From: Luiz Augusto von Dentz <[email protected]>

The functions can be used to tell the shell to stop running.

---
 src/shared/shell.c | 16 ++++++++++++++++
 src/shared/shell.h |  3 +++
 2 files changed, 19 insertions(+)
src/shared/shell.c +16 -0
index 6cc797cdb..9ecb4c7be 100644
@@ -866,6 +866,22 @@ void bt_shell_run(void)
 	data.init = false;
 }
 
+void bt_shell_quit(int status)
+{
+	if (status == EXIT_SUCCESS)
+		mainloop_exit_success();
+	else
+		mainloop_exit_failure();
+}
+
+void bt_shell_noninteractive_quit(int status)
+{
+	if (!data.mode || data.timeout)
+		return;
+
+	bt_shell_quit(status);
+}
+
 bool bt_shell_set_menu(const struct bt_shell_menu *menu)
 {
 	if (!menu)
src/shared/shell.h +3 -0
index 359629896..10747c955 100644
@@ -68,6 +68,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt);
 
 void bt_shell_run(void);
 
+void bt_shell_quit(int status);
+void bt_shell_noninteractive_quit(int status);
+
 bool bt_shell_set_menu(const struct bt_shell_menu *menu);
 
 bool bt_shell_add_submenu(const struct bt_shell_menu *menu);
Signature · 1 line
2.14.3
author maintainer
#2 lore raw

PATCH 2/14 shared/shell: Fix not reseting optind

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Not reseting optind may crash the tool if getopt and variants are called a second time.

---
 src/shared/shell.c | 1 +
 1 file changed, 1 insertion(+)
src/shared/shell.c +1 -0
index 9ecb4c7be..0653c0ea1 100644
@@ -817,6 +817,7 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 
 	data.argc = argc - optind;
 	data.argv = argv + optind;
+	optind = 0;
 	data.mode = (data.argc > 0);
 
 	if (data.mode)
Signature · 1 line
2.14.3
author maintainer
#3 lore raw

PATCH 3/14 shared/shell: Fix timeout parameter

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Fix help message and add proper optstr modifier so both -t/--timeout work properly.

---
 src/shared/shell.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/shared/shell.c +3 -3
index 0653c0ea1..e82307d6f 100644
@@ -768,7 +768,7 @@ static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
 	for (i = 0; opt && opt->options[i].name; i++)
 		printf("\t--%s \t%s\n", opt->options[i].name, opt->help[i]);
 
-	printf("\t--timeout \t\tTimeout in seconds for non-interactive mode\n"
+	printf("\t--timeout \tTimeout in seconds for non-interactive mode\n"
 		"\t--version \tDisplay version\n"
 		"\t--help \t\tDisplay help\n");
 }
@@ -787,9 +787,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 	if (opt) {
 		memcpy(options + offset, opt->options,
 				sizeof(struct option) * opt->optno);
-		snprintf(optstr, sizeof(optstr), "+hv%s", opt->optstr);
+		snprintf(optstr, sizeof(optstr), "+hvt:%s", opt->optstr);
 	} else
-		snprintf(optstr, sizeof(optstr), "+hv");
+		snprintf(optstr, sizeof(optstr), "+hvt:");
 
 	while ((c = getopt_long(argc, argv, optstr, options, &index)) != -1) {
 		switch (c) {
Signature · 1 line
2.14.3
author maintainer
#4 lore raw

PATCH 4/14 shared/shell: Do not quit immediatelly on noninteractive mode

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

The command handler shall be able to tell when it is done by calling bt_shell_noninteractive_quit.

---
 src/shared/shell.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
src/shared/shell.c +1 -4
index e82307d6f..eae4ffbdd 100644
@@ -951,10 +951,7 @@ bool bt_shell_attach(int fd)
 	if (data.mode) {
 		shell_exec(data.argc, data.argv);
 
-		if (!data.timeout) {
-			bt_shell_detach();
-			mainloop_quit();
-		} else
+		if (data.timeout)
 			timeout_add(data.timeout * 1000, shell_quit, NULL,
 								NULL);
 	}
Signature · 1 line
2.14.3
author maintainer
#5 lore raw

PATCH 5/14 shared/shell: Fix not handling sort options

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

getopt_long index is only valid for long options.

---
 src/shared/shell.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
src/shared/shell.c +8 -1
index eae4ffbdd..7f726c2dd 100644
@@ -775,7 +775,7 @@ static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
 
 void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 {
-	int c, index = 0;
+	int c, index = -1;
 	struct option options[256];
 	char optstr[256];
 	size_t offset;
@@ -805,6 +805,13 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 			data.timeout = atoi(optarg);
 			break;
 		default:
+			if (index < 0) {
+				for (index = 0; options[index].val; index++) {
+					if (c == options[index].val)
+						break;
+				}
+			}
+
 			if (c != opt->options[index - offset].val) {
 				usage(argc, argv, opt);
 				exit(EXIT_SUCCESS);
Signature · 1 line
2.14.3
author maintainer
#6 lore raw

PATCH 6/14 shared/shell: Disable bt_shell_prompt_input in noninteractive mode

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Only the input given as argument shall be processed, all the rest shall be ignored.

---
 src/shared/shell.c | 3 +++
 1 file changed, 3 insertions(+)
src/shared/shell.c +3 -0
index 7f726c2dd..bf07d034f 100644
@@ -417,6 +417,9 @@ void bt_shell_hexdump(const unsigned char *buf, size_t len)
 void bt_shell_prompt_input(const char *label, const char *msg,
 			bt_shell_prompt_input_func func, void *user_data)
 {
+	if (!data.init || data.mode)
+		return;
+
 	/* Normal use should not prompt for user input to the value a second
 	 * time before it releases the prompt, but we take a safe action. */
 	if (data.saved_prompt)
Signature · 1 line
2.14.3
author maintainer
#7 lore raw

PATCH 7/14 shared/shell: Disable readline while in noninteractive mode

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

User shall not be able to interact with the shell while in noninteractive mode.

---
 src/shared/shell.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
src/shared/shell.c +21 -4
index bf07d034f..b5556a288 100644
@@ -381,6 +381,13 @@ void bt_shell_printf(const char *fmt, ...)
 	if (!data.input)
 		return;
 
+	if (data.mode) {
+		va_start(args, fmt);
+		vprintf(fmt, args);
+		va_end(args);
+		return;
+	}
+
 	save_input = !RL_ISSTATE(RL_STATE_DONE);
 
 	if (save_input) {
@@ -683,7 +690,7 @@ static bool signal_read(struct io *io, void *user_data)
 
 	switch (si.ssi_signo) {
 	case SIGINT:
-		if (data.input) {
+		if (data.input && !data.mode) {
 			rl_replace_line("", 0);
 			rl_crlf();
 			rl_on_new_line();
@@ -701,8 +708,10 @@ static bool signal_read(struct io *io, void *user_data)
 		/* fall through */
 	case SIGTERM:
 		if (!terminated) {
-			rl_replace_line("", 0);
-			rl_crlf();
+			if (!data.mode) {
+				rl_replace_line("", 0);
+				rl_crlf();
+			}
 			mainloop_quit();
 		}
 
@@ -745,6 +754,9 @@ static struct io *setup_signalfd(void)
 
 static void rl_init(void)
 {
+	if (data.mode)
+		return;
+
 	setlinebuf(stdout);
 	rl_attempted_completion_function = shell_completion;
 
@@ -842,6 +854,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 
 static void rl_cleanup(void)
 {
+	if (data.mode)
+		return;
+
 	rl_message("");
 	rl_callback_handler_remove();
 }
@@ -953,7 +968,9 @@ bool bt_shell_attach(int fd)
 
 	io = io_new(fd);
 
-	io_set_read_handler(io, input_read, NULL, NULL);
+	if (!data.mode)
+		io_set_read_handler(io, input_read, NULL, NULL);
+
 	io_set_disconnect_handler(io, io_hup, NULL, NULL);
 
 	data.input = io;
Signature · 1 line
2.14.3
author maintainer
#8 lore raw

PATCH 8/14 sharead/mainloop-glib: Fix mainloop_exit*

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

These function shall cause the mainloop to quit.

---
 src/shared/mainloop-glib.c | 2 ++
 1 file changed, 2 insertions(+)
src/shared/mainloop-glib.c +2 -0
index 14d43207a..1bbac5d11 100644
@@ -53,11 +53,13 @@ void mainloop_quit(void)
 void mainloop_exit_success(void)
 {
 	exit_status = EXIT_SUCCESS;
+	mainloop_quit();
 }
 
 void mainloop_exit_failure(void)
 {
 	exit_status = EXIT_FAILURE;
+	mainloop_quit();
 }
 
 int mainloop_run(void)
Signature · 1 line
2.14.3
author maintainer
#9 lore raw

PATCH 9/14 shared/mainloop-glib: Fix calling g_main_loop_quit with NULL

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This produces warnings such as:

(process:5122): GLib-CRITICAL **: g_main_loop_quit: assertion 'loop != NULL' failed

---
 src/shared/mainloop-glib.c | 3 +++
 1 file changed, 3 insertions(+)
src/shared/mainloop-glib.c +3 -0
index 1bbac5d11..8436969bb 100644
@@ -47,6 +47,9 @@ void mainloop_init(void)
 
 void mainloop_quit(void)
 {
+	if (!main_loop)
+		return;
+
 	g_main_loop_quit(main_loop);
 }
 
Signature · 1 line
2.14.3
author maintainer
#10 lore raw

PATCH 10/14 shared/shell: Allow executing submenu commands

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This makes it possible to execute submenu commands directly from the main menu by using the format <submenu>.<command>:

[bluetooth]# advertise.uuids 0x1820
[bluetooth]# advertise.name blah
[bluetooth]# advertise on
Advertising object registered
UUID: Internet Protocol Support(0x1820)
Tx Power: off
LocalName: blah
Apperance: off

---
 src/shared/shell.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
src/shared/shell.c +32 -2
index b5556a288..cd46532fe 100644
@@ -356,6 +356,34 @@ static int menu_exec(const struct bt_shell_menu_entry *entry,
 	return -ENOENT;
 }
 
+static int submenu_exec(int argc, char *argv[])
+{
+	char *name;
+	int len, tlen;
+	const struct bt_shell_menu *submenu;
+
+	if (data.menu != data.main)
+		return -ENOENT;
+
+	name = strchr(argv[0], '.');
+	if (!name)
+		return -ENOENT;
+
+	tlen = strlen(argv[0]);
+	len = name - argv[0];
+	name[0] = '\0';
+
+	submenu = find_menu(argv[0]);
+	if (!submenu)
+		return -ENOENT;
+
+	/* Replace submenu.command with command */
+	memmove(argv[0], argv[0] + len + 1, tlen - len - 1);
+	memset(argv[0] + tlen - len - 1, 0, len + 1);
+
+	return menu_exec(submenu->entries, argc, argv);
+}
+
 static void shell_exec(int argc, char *argv[])
 {
 	if (!data.menu || !argv[0])
@@ -363,10 +391,12 @@ static void shell_exec(int argc, char *argv[])
 
 	if (menu_exec(default_menu, argc, argv) == -ENOENT) {
 		if (menu_exec(data.menu->entries, argc, argv) == -ENOENT) {
-			print_text(COLOR_HIGHLIGHT,
+			if (submenu_exec(argc, argv) == -ENOENT) {
+				print_text(COLOR_HIGHLIGHT,
 					"Invalid command in menu %s: %s",
 					data.menu->name , argv[0]);
-			shell_print_help();
+				shell_print_help();
+			}
 		}
 	}
 }
Signature · 1 line
2.14.3
author maintainer
#11 lore raw

PATCH 11/14 client: Call bt_shell_noninteractive_quit when done with command

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This ensures that the commands don't stay hanging since bt_shell no longer quits immediatelly after executing a command.

---
 client/advertising.c |  84 +++++++++++++++++++---------
 client/gatt.c        | 113 ++++++++++++++++++++++++-------------
 client/main.c        | 153 ++++++++++++++++++++++++++++++++++-----------------
 3 files changed, 235 insertions(+), 115 deletions(-)
client/advertising.c +59 -25
index 3cfc318ba..2be96483a 100644
@@ -190,6 +190,7 @@ static void register_reply(DBusMessage *message, void *user_data)
 		ad.registered = true;
 		bt_shell_printf("Advertising object registered\n");
 		print_ad();
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	} else {
 		bt_shell_printf("Failed to register advertisement: %s\n", error.name);
 		dbus_error_free(&error);
@@ -197,6 +198,7 @@ static void register_reply(DBusMessage *message, void *user_data)
 		if (g_dbus_unregister_interface(conn, AD_PATH,
 						AD_IFACE) == FALSE)
 			bt_shell_printf("Failed to unregister advertising object\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 }
 
@@ -441,7 +443,7 @@ void ad_register(DBusConnection *conn, GDBusProxy *manager, const char *type)
 {
 	if (ad.registered) {
 		bt_shell_printf("Advertisement is already registered\n");
-		return;
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 
 	g_free(ad.type);
@@ -450,14 +452,14 @@ void ad_register(DBusConnection *conn, GDBusProxy *manager, const char *type)
 	if (g_dbus_register_interface(conn, AD_PATH, AD_IFACE, ad_methods,
 					NULL, ad_props, NULL, NULL) == FALSE) {
 		bt_shell_printf("Failed to register advertising object\n");
-		return;
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 
 	if (g_dbus_proxy_method_call(manager, "RegisterAdvertisement",
 					register_setup, register_reply,
 					conn, NULL) == FALSE) {
 		bt_shell_printf("Failed to register advertising\n");
-		return;
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 }
 
@@ -481,10 +483,12 @@ static void unregister_reply(DBusMessage *message, void *user_data)
 		if (g_dbus_unregister_interface(conn, AD_PATH,
 							AD_IFACE) == FALSE)
 			bt_shell_printf("Failed to unregister advertising object\n");
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	} else {
 		bt_shell_printf("Failed to unregister advertisement: %s\n",
 								error.name);
 		dbus_error_free(&error);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 }
 
@@ -494,7 +498,7 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager)
 		ad_release(conn);
 
 	if (!ad.registered)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	g_free(ad.type);
 	ad.type = NULL;
@@ -503,7 +507,7 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager)
 					unregister_setup, unregister_reply,
 					conn, NULL) == FALSE) {
 		bt_shell_printf("Failed to unregister advertisement method\n");
-		return;
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 }
 
@@ -518,7 +522,7 @@ void ad_advertise_uuids(DBusConnection *conn, int argc, char *argv[])
 {
 	if (argc < 2 || !strlen(argv[1])) {
 		print_ad_uuids();
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	ad_clear_uuids();
@@ -526,27 +530,33 @@ void ad_advertise_uuids(DBusConnection *conn, int argc, char *argv[])
 	ad.uuids = g_strdupv(&argv[1]);
 	if (!ad.uuids) {
 		bt_shell_printf("Failed to parse input\n");
-		return;
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 
 	ad.uuids_len = g_strv_length(ad.uuids);
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "ServiceUUIDs");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_disable_uuids(DBusConnection *conn)
 {
 	if (!ad.uuids)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad_clear_uuids();
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "ServiceUUIDs");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 static void ad_clear_service(void)
 {
 	g_free(ad.service.uuid);
 	memset(&ad.service, 0, sizeof(ad.service));
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_service(DBusConnection *conn, int argc, char *argv[])
@@ -560,7 +570,7 @@ void ad_advertise_service(DBusConnection *conn, int argc, char *argv[])
 			bt_shell_hexdump(ad.service.data.data,
 						ad.service.data.len);
 		}
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	ad_clear_service();
@@ -590,20 +600,26 @@ void ad_advertise_service(DBusConnection *conn, int argc, char *argv[])
 	}
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "ServiceData");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_disable_service(DBusConnection *conn)
 {
 	if (!ad.service.uuid)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad_clear_service();
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "ServiceData");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 static void ad_clear_manufacturer(void)
 {
 	memset(&ad.manufacturer, 0, sizeof(ad.manufacturer));
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_manufacturer(DBusConnection *conn, int argc, char *argv[])
@@ -621,7 +637,7 @@ void ad_advertise_manufacturer(DBusConnection *conn, int argc, char *argv[])
 						ad.manufacturer.data.len);
 		}
 
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	ad_clear_manufacturer();
@@ -629,7 +645,7 @@ void ad_advertise_manufacturer(DBusConnection *conn, int argc, char *argv[])
 	val = strtol(argv[1], &endptr, 0);
 	if (!endptr || *endptr != '\0' || val > UINT16_MAX) {
 		bt_shell_printf("Invalid manufacture id\n");
-		return;
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 
 	ad.manufacturer.id = val;
@@ -639,14 +655,14 @@ void ad_advertise_manufacturer(DBusConnection *conn, int argc, char *argv[])
 		if (i >= G_N_ELEMENTS(data->data)) {
 			bt_shell_printf("Too much data\n");
 			ad_clear_manufacturer();
-			return;
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
 		}
 
 		val = strtol(argv[i], &endptr, 0);
 		if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
 			bt_shell_printf("Invalid value at index %d\n", i);
 			ad_clear_manufacturer();
-			return;
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
 		}
 
 		data->data[data->len] = val;
@@ -655,23 +671,27 @@ void ad_advertise_manufacturer(DBusConnection *conn, int argc, char *argv[])
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE,
 							"ManufacturerData");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_disable_manufacturer(DBusConnection *conn)
 {
 	if (!ad.manufacturer.id && !ad.manufacturer.data.len)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad_clear_manufacturer();
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE,
 							"ManufacturerData");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_tx_power(DBusConnection *conn, dbus_bool_t *value)
 {
 	if (!value) {
 		bt_shell_printf("Tx Power: %s\n", ad.tx_power ? "on" : "off");
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	if (ad.tx_power == *value)
@@ -680,12 +700,14 @@ void ad_advertise_tx_power(DBusConnection *conn, dbus_bool_t *value)
 	ad.tx_power = *value;
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Includes");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_name(DBusConnection *conn, bool value)
 {
 	if (ad.name == value)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad.name = value;
 
@@ -695,6 +717,8 @@ void ad_advertise_name(DBusConnection *conn, bool value)
 	}
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Includes");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_local_name(DBusConnection *conn, const char *name)
@@ -705,7 +729,7 @@ void ad_advertise_local_name(DBusConnection *conn, const char *name)
 		else
 			bt_shell_printf("Name: %s\n", ad.name ? "on" : "off");
 
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	if (ad.local_name && !strcmp(name, ad.local_name))
@@ -715,12 +739,14 @@ void ad_advertise_local_name(DBusConnection *conn, const char *name)
 	ad.local_name = strdup(name);
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "LocalName");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_appearance(DBusConnection *conn, bool value)
 {
 	if (ad.appearance == value)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad.appearance = value;
 
@@ -728,6 +754,8 @@ void ad_advertise_appearance(DBusConnection *conn, bool value)
 		ad.local_appearance = UINT16_MAX;
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Includes");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_local_appearance(DBusConnection *conn, long int *value)
@@ -741,15 +769,17 @@ void ad_advertise_local_appearance(DBusConnection *conn, long int *value)
 			bt_shell_printf("Apperance: %s\n",
 					ad.appearance ? "on" : "off");
 
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	if (ad.local_appearance == *value)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad.local_appearance = *value;
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Appearance");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_duration(DBusConnection *conn, long int *value)
@@ -757,15 +787,17 @@ void ad_advertise_duration(DBusConnection *conn, long int *value)
 	if (!value) {
 		if (ad.duration)
 			bt_shell_printf("Duration: %u sec\n", ad.duration);
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	if (ad.duration == *value)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad.duration = *value;
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Duration");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 void ad_advertise_timeout(DBusConnection *conn, long int *value)
@@ -773,13 +805,15 @@ void ad_advertise_timeout(DBusConnection *conn, long int *value)
 	if (!value) {
 		if (ad.timeout)
 			bt_shell_printf("Timeout: %u sec\n", ad.timeout);
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 	}
 
 	if (ad.timeout == *value)
-		return;
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 
 	ad.timeout = *value;
 
 	g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Timeout");
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
client/gatt.c +75 -38

425 lines not on this page · raw message

client/main.c +101 -52

561 lines not on this page · raw message

Signature · 1 line
2.14.3
author maintainer
#12 lore raw

PATCH 12/14 shared/shell: Add bt_shell_usage

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

bt_shell_usage can be used to print the usage of the current command in execution.

---
 src/shared/shell.c | 14 ++++++++++++++
 src/shared/shell.h |  1 +
 2 files changed, 15 insertions(+)
src/shared/shell.c +14 -0
index cd46532fe..c90d28a9f 100644
@@ -76,6 +76,7 @@ static struct {
 	const struct bt_shell_menu *menu;
 	const struct bt_shell_menu *main;
 	struct queue *submenus;
+	const struct bt_shell_menu_entry *exec;
 
 	struct queue *envs;
 } data;
@@ -325,9 +326,13 @@ optional:
 	wordfree(&w);
 
 exec:
+	data.exec = entry;
+
 	if (entry->func)
 		entry->func(argc, argv);
 
+	data.exec = NULL;
+
 	return 0;
 
 fail:
@@ -451,6 +456,15 @@ void bt_shell_hexdump(const unsigned char *buf, size_t len)
 	util_hexdump(' ', buf, len, print_string, NULL);
 }
 
+void bt_shell_usage()
+{
+	if (!data.exec)
+		return;
+
+	bt_shell_printf("Usage: %s %s\n", data.exec->cmd,
+					data.exec->arg ? data.exec->arg : "");
+}
+
 void bt_shell_prompt_input(const char *label, const char *msg,
 			bt_shell_prompt_input_func func, void *user_data)
 {
src/shared/shell.h +1 -0
index 10747c955..33c31f35b 100644
@@ -82,6 +82,7 @@ void bt_shell_set_prompt(const char *string);
 void bt_shell_printf(const char *fmt,
 				...) __attribute__((format(printf, 1, 2)));
 void bt_shell_hexdump(const unsigned char *buf, size_t len);
+void bt_shell_usage(void);
 
 void bt_shell_prompt_input(const char *label, const char *msg,
 			bt_shell_prompt_input_func func, void *user_data);
Signature · 1 line
2.14.3
author maintainer
#13 lore raw

PATCH 13/14 shared/shell: Fix parsing of mandatory arguments

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

In certain cases the arguments may no start with the mandatory commands such as when the command handler expects getopt arguments first.

---
 src/shared/shell.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
src/shared/shell.c +14 -3
index c90d28a9f..9ae5af79e 100644
@@ -291,11 +291,22 @@ static int cmd_exec(const struct bt_shell_menu_entry *entry,
 	}
 
 	len = man - entry->arg;
-	man = strndup(entry->arg, len + 1);
+	if (entry->arg[0] == '<')
+		man = strndup(entry->arg, len + 1);
+	else {
+		/* Find where mandatory arguments start */
+		opt = strrchr(entry->arg, '<');
+		/* Skip if mandatory arguments are not in the right format */
+		if (!opt || opt > man) {
+			opt = strdup(entry->arg);
+			goto optional;
+		}
+		man = strndup(opt, man - opt + 1);
+	}
 
 	if (parse_args(man, &w, "<>", flags) < 0) {
 		print_text(COLOR_HIGHLIGHT,
-				"Unable to parse mandatory command arguments");
+			"Unable to parse mandatory command arguments: %s", man );
 		return -EINVAL;
 	}
 
@@ -312,7 +323,7 @@ static int cmd_exec(const struct bt_shell_menu_entry *entry,
 optional:
 	if (parse_args(opt, &w, "[]", flags) < 0) {
 		print_text(COLOR_HIGHLIGHT,
-				"Unable to parse optional command arguments");
+			"Unable to parse optional command arguments: %s", opt);
 		return -EINVAL;
 	}
 
Signature · 1 line
2.14.3
author maintainer Applied
#14 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 30 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds functionally to bt_shell to properly handle non-interactive mode making all command line tools handle commands given as argument:

Quoted 1 line

./bluetoothctl power on

[CHG] Controller B8:8A:60:D8:17:D7 Class: 0x002c010c Changing power on succeded

While on non-interactive mode readline will not be used and only the command line arguments will be processed, in addition to that there is now bt_shell_noninteractive_quit which shall be used by the command handler to quit when it is done with the command execution.

Luiz Augusto von Dentz (14):
shared/shell: Add bt_shell_{noninteractive_}quit shared/shell: Fix not reseting optind
shared/shell: Fix timeout parameter
shared/shell: Do not quit immediatelly on noninteractive mode shared/shell: Fix not handling sort options shared/shell: Disable bt_shell_prompt_input in noninteractive mode shared/shell: Disable readline while in noninteractive mode sharead/mainloop-glib: Fix mainloop_exit* shared/mainloop-glib: Fix calling g_main_loop_quit with NULL shared/shell: Allow executing submenu commands
client: Call bt_shell_noninteractive_quit when done with command shared/shell: Add bt_shell_usage
shared/shell: Fix parsing of mandatory arguments tools/btmgmt: Port to use bt_shell

Quoted 8 lines
 client/advertising.c       |   84 ++-
 client/gatt.c              |  113 ++--
 client/main.c              |  153 +++--
 src/shared/mainloop-glib.c |    5 +
 src/shared/shell.c         |  130 +++-
 src/shared/shell.h         |    4 +
 tools/btmgmt.c             | 1560 ++++++++++++++++----------------------------
 7 files changed, 905 insertions(+), 1144 deletions(-)

Applied.

Signature · 1 line
Luiz Augusto von Dentz