Skip to contentSkip to conversation

shared/shell: Add non-interactive mode

PATCH Applied Luiz Augusto von Dentz 1 participant 9 messages Feb 21, 2018 – Feb 22, 2018

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/8 shared/shell: Add non-interactive mode

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

This detects if any command was given as parameter, execute it and exit disabling all other outputs:

bluetoothctl list
Controller 00:1B:DC:07:31:88 Vudentz's T460s #1 [default] Controller B8:8A:60:D8:17:D7 BlueZ-1

It is also possible to run interactive command with use of timeout option:

bluetoothctl --timeout=10 advertise on
Agent registered
[CHG] Controller 00:1B:DC:07:31:88 SupportedInstances: 0x04 [CHG] Controller 00:1B:DC:07:31:88 ActiveInstances: 0x01 Advertising object registered
Tx Power: off
Name: off
Apperance: off

---
 src/shared/shell.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
src/shared/shell.c +35 -2
index 0ac492886..0f6d613bd 100644
@@ -62,6 +62,10 @@ struct bt_shell_env {
 };
 
 static struct {
+	int argc;
+	char **argv;
+	bool mode;
+	int timeout;
 	struct io *input;
 
 	bool saved_prompt;
@@ -373,6 +377,9 @@ void bt_shell_printf(const char *fmt, ...)
 	char *saved_line;
 	int saved_point;
 
+	if (!data.input)
+		return;
+
 	save_input = !RL_ISSTATE(RL_STATE_DONE);
 
 	if (save_input) {
@@ -744,6 +751,7 @@ static void rl_init(void)
 static const struct option main_options[] = {
 	{ "version",	no_argument, 0, 'v' },
 	{ "help",	no_argument, 0, 'h' },
+	{ "timeout",	required_argument, 0, 't' },
 };
 
 static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
@@ -759,7 +767,8 @@ 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--version \tDisplay version\n"
+	printf("\t--timeout \t\tTimeout in seconds for non-interactive mode\n"
+		"\t--version \tDisplay version\n"
 		"\t--help \t\tDisplay help\n");
 }
 
@@ -791,6 +800,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 			usage(argc, argv, opt);
 			exit(EXIT_SUCCESS);
 			return;
+		case 't':
+			data.timeout = atoi(optarg);
+			break;
 		default:
 			if (c != opt->options[index - offset].val) {
 				usage(argc, argv, opt);
@@ -802,6 +814,10 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 		}
 	}
 
+	data.argc = argc - optind;
+	data.argv = argv + optind;
+	data.mode = (data.argc > 0);
+
 	main_loop = g_main_loop_new(NULL, FALSE);
 
 	rl_init();
@@ -873,7 +889,7 @@ bool bt_shell_add_submenu(const struct bt_shell_menu *menu)
 
 void bt_shell_set_prompt(const char *string)
 {
-	if (!main_loop)
+	if (!main_loop || data.mode)
 		return;
 
 	rl_set_prompt(string);
@@ -888,6 +904,13 @@ static bool input_read(struct io *io, void *user_data)
 	return true;
 }
 
+static gboolean shell_quit(void *data)
+{
+	g_main_loop_quit(main_loop);
+
+	return FALSE;
+}
+
 bool bt_shell_attach(int fd)
 {
 	struct io *io;
@@ -903,6 +926,16 @@ bool bt_shell_attach(int fd)
 
 	data.input = io;
 
+	if (data.mode) {
+		shell_exec(data.argc, data.argv);
+
+		if (!data.timeout) {
+			bt_shell_detach();
+			g_main_loop_quit(main_loop);
+		} else
+			g_timeout_add_seconds(data.timeout, shell_quit, NULL);
+	}
+
 	return true;
 }
 
Signature · 1 line
2.14.3
author maintainer
#1 lore raw

PATCH 2/8 tools/bluetooth-player: Only enable attach input when connected

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

---
 tools/bluetooth-player.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/bluetooth-player.c +2 -1
index 709ef5633..32ae4f6bb 100644
@@ -57,11 +57,13 @@ static GList *items = NULL;
 
 static void connect_handler(DBusConnection *connection, void *user_data)
 {
+	bt_shell_attach(fileno(stdin));
 	bt_shell_set_prompt(PROMPT_ON);
 }
 
 static void disconnect_handler(DBusConnection *connection, void *user_data)
 {
+	bt_shell_detach();
 	bt_shell_set_prompt(PROMPT_OFF);
 }
 
@@ -1124,7 +1126,6 @@ int main(int argc, char *argv[])
 	bt_shell_init(argc, argv, NULL);
 	bt_shell_set_menu(&main_menu);
 	bt_shell_set_prompt(PROMPT_OFF);
-	bt_shell_attach(fileno(stdin));
 
 	dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
 
Signature · 1 line
2.14.3
author maintainer
#2 lore raw

PATCH 3/8 tools/obexctl: Only enable attach input when connected

In reply to Luiz Augusto von Dentz

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

---
 tools/obexctl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/obexctl.c +2 -1
index 2ef1c044e..b6333842e 100644
@@ -75,11 +75,13 @@ struct transfer_data {
 
 static void connect_handler(DBusConnection *connection, void *user_data)
 {
+	bt_shell_attach(fileno(stdin));
 	bt_shell_set_prompt(PROMPT_ON);
 }
 
 static void disconnect_handler(DBusConnection *connection, void *user_data)
 {
+	bt_shell_detach();
 	bt_shell_set_prompt(PROMPT_OFF);
 }
 
@@ -2111,7 +2113,6 @@ int main(int argc, char *argv[])
 	bt_shell_init(argc, argv, NULL);
 	bt_shell_set_menu(&main_menu);
 	bt_shell_set_prompt(PROMPT_OFF);
-	bt_shell_attach(fileno(stdin));
 
 	dbus_conn = g_dbus_setup_bus(DBUS_BUS_SESSION, NULL, NULL);
 
Signature · 1 line
2.14.3
author maintainer
#3 lore raw

PATCH 4/8 shared/shell: Set NON_INTERACTIVE env

In reply to Luiz Augusto von Dentz

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

This sets NON_INTERACTIVE environment variable which applications can then use to query under what mode they are running.

---
 src/shared/shell.c | 3 +++
 1 file changed, 3 insertions(+)
src/shared/shell.c +3 -0
index 0f6d613bd..85657c250 100644
@@ -818,6 +818,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 	data.argv = argv + optind;
 	data.mode = (data.argc > 0);
 
+	if (data.mode)
+		bt_shell_set_env("NON_INTERACTIVE", &data.mode);
+
 	main_loop = g_main_loop_new(NULL, FALSE);
 
 	rl_init();
Signature · 1 line
2.14.3
author maintainer
#4 lore raw

PATCH 5/8 client: Don't auto register agent on non-interactive mode

In reply to Luiz Augusto von Dentz

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

There is no use to register an agent when on non-interactive mode.

---
 client/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
client/main.c +2 -1
index 38df75828..962f3383b 100644
@@ -520,7 +520,8 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 		if (!agent_manager) {
 			agent_manager = proxy;
 
-			if (auto_register_agent)
+			if (auto_register_agent &&
+					!bt_shell_get_env("NON_INTERACTIVE"))
 				agent_register(dbus_conn, agent_manager,
 							auto_register_agent);
 		}
Signature · 1 line
2.14.3
author maintainer
#5 lore raw

PATCH 6/8 shared/mainloop: Add GLIB wrapper

In reply to Luiz Augusto von Dentz

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

This adds wrappers function to interface with GLIB mainloop so applications can use mainloop functions no matter what is the underline implementation.

Note: Most functions are not actually implemented on purpose since both io and timeout functions already exists for GLIB covering the same functionality.

---
 Makefile.am                |   3 +-
 src/shared/mainloop-glib.c | 112 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+), 1 deletion(-)
 create mode 100644 src/shared/mainloop-glib.c
Makefile.am +2 -1
index 02f02a100..daf34b6ca 100644
@@ -123,7 +123,8 @@ shared_sources = src/shared/io.h src/shared/timeout.h \
 
 src_libshared_glib_la_SOURCES = $(shared_sources) \
 				src/shared/io-glib.c \
-				src/shared/timeout-glib.c
+				src/shared/timeout-glib.c \
+				src/shared/mainloop-glib.c
 
 src_libshared_mainloop_la_SOURCES = $(shared_sources) \
 				src/shared/io-mainloop.c \
src/shared/mainloop-glib.c new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.14.3
author maintainer
#6 lore raw

PATCH 7/8 shared/util: Add strdelimit and strsuffix

In reply to Luiz Augusto von Dentz

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

---
 src/shared/util.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 src/shared/util.h |  3 +++
 2 files changed, 48 insertions(+)
src/shared/util.c +45 -0
index f6f265e56..986e2b22d 100644
@@ -980,3 +980,48 @@ const char *bt_appear_to_str(uint16_t appearance)
 
 	return str;
 }
+
+char *strdelimit(char *str, char *del, char c)
+{
+	char *dup;
+
+	if (!str)
+		return NULL;
+
+	dup = strdup(str);
+	if (dup[0] == '\0')
+		return dup;
+
+	while (del[0] != '\0') {
+		char *rep = dup;
+
+		while ((rep = strchr(rep, del[0])))
+			rep[0] = c;
+
+		del++;
+	}
+
+	return dup;
+}
+
+int strsuffix(const char *str, const char *suffix)
+{
+	int len;
+	int suffix_len;
+
+	if (!str || !suffix)
+		return false;
+
+	if (str[0] == '\0' && suffix[0] != '\0')
+		return false;
+
+	if (suffix[0] == '\0' && str[0] != '\0')
+		return false;
+
+	len = strlen(str);
+	suffix_len = strlen(suffix);
+	if (len < suffix_len)
+		return false;
+
+	return strncmp(str + len - suffix_len, suffix, suffix_len);
+}
src/shared/util.h +3 -0
index 3f5f6dfb5..604dc3be4 100644
@@ -92,6 +92,9 @@ do {						\
 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
 #define malloc0(n) (calloc((n), 1))
 
+char *strdelimit(char *str, char *del, char c);
+int strsuffix(const char *str, const char *suffix);
+
 void *btd_malloc(size_t size);
 
 typedef void (*util_debug_func_t)(const char *str, void *user_data);
Signature · 1 line
2.14.3
author maintainer
#7 lore raw

PATCH 8/8 shared/shell: Use mainloop wrappers instead of GLIB directly

In reply to Luiz Augusto von Dentz

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

This will allow the shell to be used by the likes of btmgmt.

---
 src/shared/shell.c | 49 ++++++++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 23 deletions(-)
src/shared/shell.c +26 -23
index 85657c250..6cc797cdb 100644
@@ -29,6 +29,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <signal.h>
 #include <sys/signalfd.h>
@@ -37,8 +38,9 @@
 
 #include <readline/readline.h>
 #include <readline/history.h>
-#include <glib.h>
 
+#include "src/shared/mainloop.h"
+#include "src/shared/timeout.h"
 #include "src/shared/io.h"
 #include "src/shared/util.h"
 #include "src/shared/queue.h"
@@ -54,14 +56,13 @@
 		printf(COLOR_BLUE "%s %-*s " COLOR_OFF "%s\n", \
 			cmd, (int)(CMD_LENGTH - strlen(cmd)), "", desc)
 
-static GMainLoop *main_loop;
-
 struct bt_shell_env {
 	char *name;
 	void *value;
 };
 
 static struct {
+	bool init;
 	int argc;
 	char **argv;
 	bool mode;
@@ -88,7 +89,7 @@ static void cmd_version(int argc, char *argv[])
 
 static void cmd_quit(int argc, char *argv[])
 {
-	g_main_loop_quit(main_loop);
+	mainloop_quit();
 }
 
 static void cmd_help(int argc, char *argv[])
@@ -249,18 +250,18 @@ static int parse_args(char *arg, wordexp_t *w, char *del, int flags)
 {
 	char *str;
 
-	str = g_strdelimit(arg, del, '"');
+	str = strdelimit(arg, del, '"');
 
 	if (wordexp(str, w, flags)) {
-		g_free(str);
+		free(str);
 		return -EINVAL;
 	}
 
 	/* If argument ends with ,,, set we_offs bypass strict checks */
-	if (w->we_wordc && g_str_has_suffix(w->we_wordv[w->we_wordc -1], "..."))
+	if (w->we_wordc && strsuffix(w->we_wordv[w->we_wordc -1], "..."))
 		w->we_offs = 1;
 
-	g_free(str);
+	free(str);
 
 	return 0;
 }
@@ -460,7 +461,7 @@ static void rl_handler(char *input)
 		rl_insert_text("quit");
 		rl_redisplay();
 		rl_crlf();
-		g_main_loop_quit(main_loop);
+		mainloop_quit();
 		return;
 	}
 
@@ -586,7 +587,7 @@ static char **args_completion(const struct bt_shell_menu_entry *entry, int argc,
 		goto done;
 
 	/* Split values separated by / */
-	str = g_strdelimit(args.we_wordv[index], "/", ' ');
+	str = strdelimit(args.we_wordv[index], "/", ' ');
 
 	if (wordexp(str, &args, WRDE_NOCMD))
 		goto done;
@@ -659,7 +660,7 @@ static char **shell_completion(const char *text, int start, int end)
 
 static bool io_hup(struct io *io, void *user_data)
 {
-	g_main_loop_quit(main_loop);
+	mainloop_quit();
 
 	return false;
 }
@@ -699,7 +700,7 @@ static bool signal_read(struct io *io, void *user_data)
 		if (!terminated) {
 			rl_replace_line("", 0);
 			rl_crlf();
-			g_main_loop_quit(main_loop);
+			mainloop_quit();
 		}
 
 		terminated = true;
@@ -821,9 +822,11 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 	if (data.mode)
 		bt_shell_set_env("NON_INTERACTIVE", &data.mode);
 
-	main_loop = g_main_loop_new(NULL, FALSE);
+	mainloop_init();
 
 	rl_init();
+
+	data.init = true;
 }
 
 static void rl_cleanup(void)
@@ -846,22 +849,21 @@ void bt_shell_run(void)
 
 	signal = setup_signalfd();
 
-	g_main_loop_run(main_loop);
+	mainloop_run();
 
 	bt_shell_release_prompt("");
 	bt_shell_detach();
 
 	io_destroy(signal);
 
-	g_main_loop_unref(main_loop);
-	main_loop = NULL;
-
 	if (data.envs) {
 		queue_destroy(data.envs, env_destroy);
 		data.envs = NULL;
 	}
 
 	rl_cleanup();
+
+	data.init = false;
 }
 
 bool bt_shell_set_menu(const struct bt_shell_menu *menu)
@@ -892,7 +894,7 @@ bool bt_shell_add_submenu(const struct bt_shell_menu *menu)
 
 void bt_shell_set_prompt(const char *string)
 {
-	if (!main_loop || data.mode)
+	if (!data.init || data.mode)
 		return;
 
 	rl_set_prompt(string);
@@ -907,11 +909,11 @@ static bool input_read(struct io *io, void *user_data)
 	return true;
 }
 
-static gboolean shell_quit(void *data)
+static bool shell_quit(void *data)
 {
-	g_main_loop_quit(main_loop);
+	mainloop_quit();
 
-	return FALSE;
+	return false;
 }
 
 bool bt_shell_attach(int fd)
@@ -934,9 +936,10 @@ bool bt_shell_attach(int fd)
 
 		if (!data.timeout) {
 			bt_shell_detach();
-			g_main_loop_quit(main_loop);
+			mainloop_quit();
 		} else
-			g_timeout_add_seconds(data.timeout, shell_quit, NULL);
+			timeout_add(data.timeout * 1000, shell_quit, NULL,
+								NULL);
 	}
 
 	return true;
Signature · 1 line
2.14.3
author maintainer Applied
#8 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 117 lines · Luiz Augusto von Dentz

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

This detects if any command was given as parameter, execute it and exit disabling all other outputs:

bluetoothctl list
Controller 00:1B:DC:07:31:88 Vudentz's T460s #1 [default] Controller B8:8A:60:D8:17:D7 BlueZ-1

It is also possible to run interactive command with use of timeout option:

bluetoothctl --timeout=10 advertise on
Agent registered
[CHG] Controller 00:1B:DC:07:31:88 SupportedInstances: 0x04 [CHG] Controller 00:1B:DC:07:31:88 ActiveInstances: 0x01 Advertising object registered
Tx Power: off
Name: off
Apperance: off

---
 src/shared/shell.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
src/shared/shell.c +27 -2
index 0ac492886..0f6d613bd 100644
@@ -62,6 +62,10 @@ struct bt_shell_env {
 };
 
 static struct {
+       int argc;
+       char **argv;
+       bool mode;
+       int timeout;
        struct io *input;
 
        bool saved_prompt;
@@ -373,6 +377,9 @@ void bt_shell_printf(const char *fmt, ...)
        char *saved_line;
        int saved_point;
 
+       if (!data.input)
+               return;
+
        save_input = !RL_ISSTATE(RL_STATE_DONE);
 
        if (save_input) {
@@ -744,6 +751,7 @@ static void rl_init(void)
 static const struct option main_options[] = {
        { "version",    no_argument, 0, 'v' },
        { "help",       no_argument, 0, 'h' },
+       { "timeout",    required_argument, 0, 't' },
 };
 
 static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
@@ -759,7 +767,8 @@ 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--version \tDisplay version\n"
+       printf("\t--timeout \t\tTimeout in seconds for non-interactive mode\n"
+               "\t--version \tDisplay version\n"
                "\t--help \t\tDisplay help\n");
 }
 
@@ -791,6 +800,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
                        usage(argc, argv, opt);
                        exit(EXIT_SUCCESS);
                        return;
+               case 't':
+                       data.timeout = atoi(optarg);
+                       break;
                default:
                        if (c != opt->options[index - offset].val) {
                                usage(argc, argv, opt);
@@ -802,6 +814,10 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
                }
        }
 
+       data.argc = argc - optind;
+       data.argv = argv + optind;
+       data.mode = (data.argc > 0);
+
        main_loop = g_main_loop_new(NULL, FALSE);
 
        rl_init();
@@ -873,7 +889,7 @@ bool bt_shell_add_submenu(const struct bt_shell_menu *menu)
 
 void bt_shell_set_prompt(const char *string)
 {
-       if (!main_loop)
+       if (!main_loop || data.mode)
                return;
 
        rl_set_prompt(string);
@@ -888,6 +904,13 @@ static bool input_read(struct io *io, void *user_data)
        return true;
 }
 
+static gboolean shell_quit(void *data)
+{
+       g_main_loop_quit(main_loop);
+
+       return FALSE;
+}
+
 bool bt_shell_attach(int fd)
 {
        struct io *io;
@@ -903,6 +926,16 @@ bool bt_shell_attach(int fd)
 
        data.input = io;
 
+       if (data.mode) {
+               shell_exec(data.argc, data.argv);

src/shared/shell.c bool bt_shell_attach(int fd) · line 940

Quoted 10 lines
src/shared/shell.c +8 -0
+
+               if (!data.timeout) {
+                       bt_shell_detach();
+                       g_main_loop_quit(main_loop);
+               } else
+                       g_timeout_add_seconds(data.timeout, shell_quit, NULL);
+       }
+
        return true;
 }

Applied.

Signature · 1 line
Luiz Augusto von Dentz