All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode
@ 2018-02-21 14:14 Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 2/8] tools/bluetooth-player: Only enable attach input when connected Luiz Augusto von Dentz
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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(-)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 0ac492886..0f6d613bd 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -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;
 }
 
-- 
2.14.3


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

* [PATCH BlueZ 2/8] tools/bluetooth-player: Only enable attach input when connected
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
@ 2018-02-21 14:14 ` Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 3/8] tools/obexctl: " Luiz Augusto von Dentz
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 tools/bluetooth-player.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/bluetooth-player.c b/tools/bluetooth-player.c
index 709ef5633..32ae4f6bb 100644
--- a/tools/bluetooth-player.c
+++ b/tools/bluetooth-player.c
@@ -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);
 
-- 
2.14.3


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

* [PATCH BlueZ 3/8] tools/obexctl: Only enable attach input when connected
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 2/8] tools/bluetooth-player: Only enable attach input when connected Luiz Augusto von Dentz
@ 2018-02-21 14:14 ` Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 4/8] shared/shell: Set NON_INTERACTIVE env Luiz Augusto von Dentz
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 tools/obexctl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/obexctl.c b/tools/obexctl.c
index 2ef1c044e..b6333842e 100644
--- a/tools/obexctl.c
+++ b/tools/obexctl.c
@@ -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);
 
-- 
2.14.3


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

* [PATCH BlueZ 4/8] shared/shell: Set NON_INTERACTIVE env
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 2/8] tools/bluetooth-player: Only enable attach input when connected Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 3/8] tools/obexctl: " Luiz Augusto von Dentz
@ 2018-02-21 14:14 ` Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 5/8] client: Don't auto register agent on non-interactive mode Luiz Augusto von Dentz
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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(+)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 0f6d613bd..85657c250 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -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();
-- 
2.14.3


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

* [PATCH BlueZ 5/8] client: Don't auto register agent on non-interactive mode
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
                   ` (2 preceding siblings ...)
  2018-02-21 14:14 ` [PATCH BlueZ 4/8] shared/shell: Set NON_INTERACTIVE env Luiz Augusto von Dentz
@ 2018-02-21 14:14 ` Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 6/8] shared/mainloop: Add GLIB wrapper Luiz Augusto von Dentz
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

There is no use to register an agent when on non-interactive mode.
---
 client/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/client/main.c b/client/main.c
index 38df75828..962f3383b 100644
--- a/client/main.c
+++ b/client/main.c
@@ -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);
 		}
-- 
2.14.3


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

* [PATCH BlueZ 6/8] shared/mainloop: Add GLIB wrapper
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
                   ` (3 preceding siblings ...)
  2018-02-21 14:14 ` [PATCH BlueZ 5/8] client: Don't auto register agent on non-interactive mode Luiz Augusto von Dentz
@ 2018-02-21 14:14 ` Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 7/8] shared/util: Add strdelimit and strsuffix Luiz Augusto von Dentz
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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

diff --git a/Makefile.am b/Makefile.am
index 02f02a100..daf34b6ca 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c
new file mode 100644
index 000000000..14d43207a
--- /dev/null
+++ b/src/shared/mainloop-glib.c
@@ -0,0 +1,112 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2018  Intel Corporation
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <signal.h>
+#include <sys/signalfd.h>
+
+#include <glib.h>
+
+#include "mainloop.h"
+
+static GMainLoop *main_loop;
+static int exit_status;
+
+void mainloop_init(void)
+{
+	main_loop = g_main_loop_new(NULL, FALSE);
+}
+
+void mainloop_quit(void)
+{
+	g_main_loop_quit(main_loop);
+}
+
+void mainloop_exit_success(void)
+{
+	exit_status = EXIT_SUCCESS;
+}
+
+void mainloop_exit_failure(void)
+{
+	exit_status = EXIT_FAILURE;
+}
+
+int mainloop_run(void)
+{
+	if (!main_loop)
+		return -EINVAL;
+
+	g_main_loop_run(main_loop);
+
+	g_main_loop_unref(main_loop);
+	main_loop = NULL;
+
+	return exit_status;
+}
+
+int mainloop_add_fd(int fd, uint32_t events, mainloop_event_func callback,
+				void *user_data, mainloop_destroy_func destroy)
+{
+	return -ENOSYS;
+}
+
+int mainloop_modify_fd(int fd, uint32_t events)
+{
+	return -ENOSYS;
+}
+
+int mainloop_remove_fd(int fd)
+{
+	return -ENOSYS;
+}
+
+int mainloop_add_timeout(unsigned int msec, mainloop_timeout_func callback,
+				void *user_data, mainloop_destroy_func destroy)
+{
+	return -ENOSYS;
+}
+
+int mainloop_modify_timeout(int fd, unsigned int msec)
+{
+	return -ENOSYS;
+}
+
+int mainloop_remove_timeout(int id)
+{
+	return -ENOSYS;
+}
+
+int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback,
+				void *user_data, mainloop_destroy_func destroy)
+{
+	return -ENOSYS;
+}
-- 
2.14.3


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

* [PATCH BlueZ 7/8] shared/util: Add strdelimit and strsuffix
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
                   ` (4 preceding siblings ...)
  2018-02-21 14:14 ` [PATCH BlueZ 6/8] shared/mainloop: Add GLIB wrapper Luiz Augusto von Dentz
@ 2018-02-21 14:14 ` Luiz Augusto von Dentz
  2018-02-21 14:14 ` [PATCH BlueZ 8/8] shared/shell: Use mainloop wrappers instead of GLIB directly Luiz Augusto von Dentz
  2018-02-22  9:56 ` [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 src/shared/util.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 src/shared/util.h |  3 +++
 2 files changed, 48 insertions(+)

diff --git a/src/shared/util.c b/src/shared/util.c
index f6f265e56..986e2b22d 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -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);
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 3f5f6dfb5..604dc3be4 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -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);
-- 
2.14.3


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

* [PATCH BlueZ 8/8] shared/shell: Use mainloop wrappers instead of GLIB directly
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
                   ` (5 preceding siblings ...)
  2018-02-21 14:14 ` [PATCH BlueZ 7/8] shared/util: Add strdelimit and strsuffix Luiz Augusto von Dentz
@ 2018-02-21 14:14 ` Luiz Augusto von Dentz
  2018-02-22  9:56 ` [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-21 14:14 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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(-)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 85657c250..6cc797cdb 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -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;
-- 
2.14.3


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

* Re: [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode
  2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
                   ` (6 preceding siblings ...)
  2018-02-21 14:14 ` [PATCH BlueZ 8/8] shared/shell: Use mainloop wrappers instead of GLIB directly Luiz Augusto von Dentz
@ 2018-02-22  9:56 ` Luiz Augusto von Dentz
  7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2018-02-22  9:56 UTC (permalink / raw)
  To: linux-bluetooth

Hi,

On Wed, Feb 21, 2018 at 4:14 PM, Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
> From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
>
> 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(-)
>
> diff --git a/src/shared/shell.c b/src/shared/shell.c
> index 0ac492886..0f6d613bd 100644
> --- a/src/shared/shell.c
> +++ b/src/shared/shell.c
> @@ -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;
>  }

Applied.

-- 
Luiz Augusto von Dentz

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

end of thread, other threads:[~2018-02-22  9:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-21 14:14 [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz
2018-02-21 14:14 ` [PATCH BlueZ 2/8] tools/bluetooth-player: Only enable attach input when connected Luiz Augusto von Dentz
2018-02-21 14:14 ` [PATCH BlueZ 3/8] tools/obexctl: " Luiz Augusto von Dentz
2018-02-21 14:14 ` [PATCH BlueZ 4/8] shared/shell: Set NON_INTERACTIVE env Luiz Augusto von Dentz
2018-02-21 14:14 ` [PATCH BlueZ 5/8] client: Don't auto register agent on non-interactive mode Luiz Augusto von Dentz
2018-02-21 14:14 ` [PATCH BlueZ 6/8] shared/mainloop: Add GLIB wrapper Luiz Augusto von Dentz
2018-02-21 14:14 ` [PATCH BlueZ 7/8] shared/util: Add strdelimit and strsuffix Luiz Augusto von Dentz
2018-02-21 14:14 ` [PATCH BlueZ 8/8] shared/shell: Use mainloop wrappers instead of GLIB directly Luiz Augusto von Dentz
2018-02-22  9:56 ` [PATCH BlueZ 1/8] shared/shell: Add non-interactive mode Luiz Augusto von Dentz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.