Search code examples
cdbusgdbus

dbus call in C from shell dbus-send


I have a dbus-send call: dbus-send --system --print-reply --dest=org.freedesktop.ModemManager1 "/org/freedesktop/ModemManager1/Modem/0" org.freedesktop.DBus.Properties.Get string:org.freedesktop.ModemManager1.Modem string:"SignalQuality"

And I would like to write a simple C code that does the exact same thing, well, C way.

I can't find enough information, or anything that makes sense to me to help me figure out what I need to convert this to C.

I have tried g_dbus_proxy calls with limited success.

I have tried:

#define MM_DBUS_SERVICE "org.freedesktop.ModemManager1"
#define MM_DBUS_PATH "/org/freedesktop/ModemManager1/Modem/0"
#define MM_DBUS_GET_PROPERTIES  "org.freedesktop.DBus.Properties.Get"

proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                           G_DBUS_PROXY_FLAGS_NONE,
                                           NULL,
                                           MM_DBUS_SERVICE,
                                           MM_DBUS_PATH,
                                           MM_DBUS_GET_PROPERTIES,
                                           NULL, NULL);
    ret = g_dbus_proxy_call_sync (proxy,
                                  "SignalQuality",
                                  NULL,
                                  G_DBUS_CALL_FLAGS_NONE, -1,
                                  NULL, &error);
    if (!ret) {
            g_dbus_error_strip_remote_error (error);
            g_print ("failed: %s\n", error->message);
            g_error_free (error);
            return;
    }

But it constantly fails with error failed: No such interface `org.freedesktop.DBus.Properties.Get' on object at path /org/freedesktop/ModemManager1/Modem/0

Any ideas how to get this going?


Solution

  • I think interface_name should be "org.freedesktop.DBus.Properties" when you call g_dbus_proxy_new_for_bus_sync(..), so try

    #define MM_DBUS_GET_PROPERTIES  "org.freedesktop.DBus.Properties"
    

    and method_name should be "org.freedesktop.DBus.Properties.Get" when you call g_dbus_proxy_call_sync(), so try:

    ret = g_dbus_proxy_call_sync (proxy,
                                  "org.freedesktop.DBus.Properties.Get",
                                  g_variant_new ("(ss)",
                                              "org.freedesktop.ModemManager1.Modem",
                                              "SignalQuality"),
                                  G_DBUS_CALL_FLAGS_NONE, -1,
                                  NULL, &error);