cglibdbusgobjectgio

How can I convert a GDBusInterface into a GDBusProxy?


I am trying to write some C that uses the DBus support in GLib's GIO.

I have a GDBusObjectManager and I have used g_signal_connect() to add a handler to the interface-added signal. The signature of the handler according to the docs is:

void
user_function (GDBusObjectManager *manager,
               GDBusObject        *object,
               GDBusInterface     *interface,
               gpointer            user_data)

I want to check if the interface that was added matches the interface that I care about (org.bluez.Adapter1) and then call some methods from that interface on the object that implements it. I believe that the first step is to check if the interface name matches. I believe I could do this by comparing against the return from const gchar *g_dbus_proxy_get_name(GDBusProxy *proxy);, but I don't have a GDBusProxy, I have a GDBusInterface. So my question is: How can I get a GDBusProxy from a GDBusInterface?


Solution

  • GDBusInterface is an interface which is implemented by the GDBusProxy class, so you already have the GDBusProxy — you just need to cast it to the right type, which you can do using G_DBUS_PROXY (interface).

    Depending on whether you’re using proxy instances generated by gdbus-codegen, you can then either check whether it’s an instance of the proxy for org.bluez.Adapter1 using a macro from the generated code like MY_NAMESPACE_IS_ADAPTER1 (interface); or you can check the interface name manually using g_str_equal (g_dbus_proxy_get_interface_name (G_DBUS_PROXY (interface)), "org.bluez.Adapter1").

    Note that g_dbus_proxy_get_name() will return the well-known or unique bus name which the proxy is for, which is not the same as the interface name. A well known or unique name identifies a peer (typically another process) on the bus. That peer exposes objects at object paths, and each object implements one or more interfaces.