diff mbox series

[BlueZ,v3] tools/obexctl: Add support for system/session bus

Message ID 20241115064304.503655-1-quic_dgangire@quicinc.com
State New
Headers show
Series [BlueZ,v3] tools/obexctl: Add support for system/session bus | expand

Commit Message

Damodar Reddy GangiReddy Nov. 15, 2024, 6:43 a.m. UTC
From: Damodar Reddy GangiReddy <quic_dgangire@quicinc.com>

Currently obexctl only uses session bus.
As obexd has been enabled support for both session and system bus.
Configuring obexctl to use session/system bus during the runtime
if the name is available over session or system bus.

---
 tools/obexctl.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/tools/obexctl.c b/tools/obexctl.c
index a398b095b..95350358c 100644
--- a/tools/obexctl.c
+++ b/tools/obexctl.c
@@ -43,8 +43,10 @@ 
 #define OBEX_PBAP_INTERFACE "org.bluez.obex.PhonebookAccess1"
 #define OBEX_MAP_INTERFACE "org.bluez.obex.MessageAccess1"
 #define OBEX_MSG_INTERFACE "org.bluez.obex.Message1"
+#define OBEXD_SERVICE  "org.bluez.obex"
 
-static DBusConnection *dbus_conn;
+static DBusConnection *dbus_session_conn;
+static DBusConnection *dbus_system_conn;
 static GDBusProxy *default_session;
 static GList *sessions = NULL;
 static GList *opps = NULL;
@@ -2149,6 +2151,22 @@  static void property_changed(GDBusProxy *proxy, const char *name,
 		session_property_changed(proxy, name, iter);
 }
 
+static bool check_obexd_service(DBusConnection *conn)
+{
+	DBusError err;
+
+	dbus_error_init(&err);
+
+	bool has_owner = dbus_bus_name_has_owner(conn, OBEXD_SERVICE, &err);
+
+	if (dbus_error_is_set(&err)) {
+		fprintf(stderr, "D-Bus Error: %s\n", err.message);
+		dbus_error_free(&err);
+	}
+
+	return has_owner;
+}
+
 int main(int argc, char *argv[])
 {
 	GDBusClient *client;
@@ -2158,10 +2176,23 @@  int main(int argc, char *argv[])
 	bt_shell_set_menu(&main_menu);
 	bt_shell_set_prompt(PROMPT, NULL);
 
-	dbus_conn = g_dbus_setup_bus(DBUS_BUS_SESSION, NULL, NULL);
+	bool session_bus_active = false;
+	bool system_bus_active = false;
+
+	dbus_session_conn = g_dbus_setup_bus(DBUS_BUS_SESSION, NULL, NULL);
+	if (dbus_session_conn)
+		session_bus_active = check_obexd_service(dbus_session_conn);
 
-	client = g_dbus_client_new(dbus_conn, "org.bluez.obex",
-							"/org/bluez/obex");
+	dbus_system_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
+	if (dbus_system_conn)
+		system_bus_active = check_obexd_service(dbus_system_conn);
+
+	if (session_bus_active)
+		client = g_dbus_client_new(dbus_session_conn, OBEXD_SERVICE,
+								"/org/bluez/obex");
+	else if (system_bus_active)
+		client = g_dbus_client_new(dbus_system_conn, OBEXD_SERVICE,
+								"/org/bluez/obex");
 
 	g_dbus_client_set_connect_watch(client, connect_handler, NULL);
 	g_dbus_client_set_disconnect_watch(client, disconnect_handler, NULL);
@@ -2173,7 +2204,11 @@  int main(int argc, char *argv[])
 
 	g_dbus_client_unref(client);
 
-	dbus_connection_unref(dbus_conn);
+	if (dbus_session_conn)
+		dbus_connection_unref(dbus_session_conn);
+
+	if (dbus_system_conn)
+		dbus_connection_unref(dbus_system_conn);
 
 	return status;
 }