diff mbox series

[BlueZ,2/3] client: Respect --timeout when bluetoothd isn't running

Message ID 20240912092253.119754-3-hadess@hadess.net
State New
Headers show
Series Fix bluetoothctl hanging if daemon isn't running | expand

Commit Message

Bastien Nocera Sept. 12, 2024, 9:20 a.m. UTC
Exit after <timeout> seconds if bluetoothd isn't available. This
functionality is useful for non-interactive uses of bluetoothctl.
---
 client/main.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/client/main.c b/client/main.c
index a96a4263849d..8791466e49ff 100644
--- a/client/main.c
+++ b/client/main.c
@@ -22,7 +22,9 @@ 
 
 #include <glib.h>
 
+#include "src/shared/mainloop.h"
 #include "src/shared/shell.h"
+#include "src/shared/timeout.h"
 #include "src/shared/util.h"
 #include "src/shared/ad.h"
 #include "gdbus/gdbus.h"
@@ -3173,13 +3175,25 @@  static const struct bt_shell_opt opt = {
 
 static void client_ready(GDBusClient *client, void *user_data)
 {
+	unsigned int *timeout_id = user_data;
+
+	if (*timeout_id > 0)
+		timeout_remove(*timeout_id);
 	setup_standard_input();
 }
 
+static bool timeout_quit(void *user_data)
+{
+	mainloop_exit_failure();
+	return true;
+}
+
 int main(int argc, char *argv[])
 {
 	GDBusClient *client;
 	int status;
+	int timeout;
+	unsigned int timeout_id;
 
 	bt_shell_init(argc, argv, &opt);
 	bt_shell_set_menu(&main_menu);
@@ -3217,8 +3231,11 @@  int main(int argc, char *argv[])
 	g_dbus_client_set_proxy_handlers(client, proxy_added, proxy_removed,
 							property_changed, NULL);
 
-	g_dbus_client_set_ready_watch(client, client_ready, NULL);
-
+	timeout = bt_shell_get_timeout();
+	timeout_id = 0;
+	if (timeout > 0)
+		timeout_id = timeout_add(timeout * 1000, timeout_quit, NULL, NULL);
+	g_dbus_client_set_ready_watch(client, client_ready, &timeout_id);
 	status = bt_shell_run();
 
 	admin_remove_submenu();