c ++ gdbus - 如何使用gdbus库获取接口的属性?

时间:2017-11-03 11:50:34

标签: dbus gdbus

我想知道使用c ++代码片段从dbus接口获取属性的方法/代码段。

我尝试了以下错误方法。

使用g_dbus_proxy_get_cached_property

方法#1 ,但它始终返回null

ifproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                       flags,
                                       NULL,
                                       "org.freedesktop.NetworkManager",
                                       "org/freedesktop/NetworkManager/Device/0",
                                       "org.freedesktop.NetworkManager.Device",
                                       NULL, 
                                       &error);
ret = g_dbus_proxy_get_cached_property(ifproxy, "State")

方法#2 g_dbus_proxy_call_sync - 这个说“org.freedesktop.networkmanager”未导出(或可能不存在),无法访问属性“Interface”

ifproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                             G_DBUS_PROXY_FLAGS_NONE,
                                             NULL,
                                             "org.freedesktop.NetworkManager",
                                            "org/freedesktop/NetworkManager/Device/0",
                                             "org.freedesktop.DBus.Properties",
                                             NULL, NULL);
    g_assert (ifproxy);

    /* Get the object path of the Connection details */
    ret = g_dbus_proxy_call_sync (ifproxy,
                                  "Get",
                                  g_variant_new ("(ss)",
                                                "org/freedesktop/NetworkManager/Device/0",
                                                 "Interface"),
                                  G_DBUS_CALL_FLAGS_NONE, -1,
                                  NULL, &error);
    if (!ret) {
        g_dbus_error_strip_remote_error (error);
        g_warning ("Failed to get property: %s\n",
                   error->message);
        g_error_free (error);
        return;
    }

    name = g_variant_get_string(ret, NULL);

    //g_assert(ret != NULL);

    g_variant_get (ret, "s", &name);
    g_variant_unref (ret);

2 个答案:

答案 0 :(得分:1)

至少第一个表单应该可以工作(假设你在实际代码中处理错误),但是你的D-Bus对象路径存在问题。

首先,有效的对象路径以' /'开头。所以你可能想要/org/freedesktop/NetworkManager/Device/0 ...除非它似乎不是NetworkManager使用的路径。

看看他们的API参考,看起来你可能真的想/org/freedesktop/NetworkManager/Devices/N(注意复数"设备")但请注意,你无法确定最后一部分是什么路径(N)将会是。在正确的代码中,您应该从org.freedesktop.NetworkManager获取设备对象路径,但是为了进行调试,您可以使用像d-feet这样的工具来查找哪些对象路径可用并使用它们。

答案 1 :(得分:0)

非常感谢@jku。

我能够解决编码问题。 为其他人分享下面的工作代码。

static void
list_connections (GDBusProxy *proxy)
{
    int i;
    GError *error = NULL;
    GVariant *ret, *ret1;
    char **paths;
    gchar *name;
    GDBusProxy *ifproxy;
    GDBusProxyFlags flags;

    /* Call ListConnections D-Bus method */
    ret = g_dbus_proxy_call_sync (proxy,
                                  "GetDevices",
                                  NULL,
                                  G_DBUS_CALL_FLAGS_NONE, -1,
                                  NULL, &error);
    if (!ret) {
        g_dbus_error_strip_remote_error (error);
        g_print ("ListConnections failed: %s\n", error->message);
        g_error_free (error);
        return;
    }

    g_variant_get (ret, "(^ao)", &paths);
    g_variant_unref (ret);

    flags = static_cast<GDBusProxyFlags> (G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START);

    for (i = 0; paths[i]; i++)
    {
        g_print ("%s\n", paths[i]);
        /*ifproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                           flags,
                                           NULL,
                                           "org.freedesktop.NetworkManager",
                                           paths[i],//"/org/freedesktop/NetworkManager/Devices/0"
                                           "org.freedesktop.NetworkManager.Device",
                                           NULL, 
                                           &error);

        //name = g_dbus_proxy_get_interface_name(ifproxy);
        ret = g_dbus_proxy_get_cached_property(ifproxy, "FirmwareVersion");*/

        ifproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                 G_DBUS_PROXY_FLAGS_NONE,
                                                 NULL,
                                                 "org.freedesktop.NetworkManager",
                                                 "/org/freedesktop/NetworkManager/Devices/0",
                                                 "org.freedesktop.DBus.Properties",
                                                 NULL, NULL);
        g_assert (ifproxy);

        ret = g_dbus_proxy_call_sync (ifproxy,
                                      "Get",
                                      g_variant_new ("(ss)",
                                                    "org.freedesktop.NetworkManager.Device",
                                                     "Interface"),
                                      G_DBUS_CALL_FLAGS_NONE, -1,
                                      NULL, &error);
        if (!ret) {
            g_dbus_error_strip_remote_error (error);
            g_warning ("Failed to get property: %s\n",
                       error->message);
            g_error_free (error);
            return;
        }

         g_print("\nType String of Variant:- %s\n", g_variant_get_type_string (ret));

        g_variant_get (ret, "(v)", &ret1);
        g_variant_unref (ret);

         g_print("\nType String of Variant:- %s\n", g_variant_get_type_string (ret1));
        g_variant_get (ret1, "s", &name);
        g_variant_unref (ret1);


        g_print ("Interface name:- %s\n", name);
    }
    g_strfreev (paths);
}

int
main (int argc, char *argv[])
{
    GDBusProxy *proxy;
    GDBusProxyFlags flags;
    GError *error = NULL;
    flags = static_cast<GDBusProxyFlags> (G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START);

#if !GLIB_CHECK_VERSION (2, 35, 0)
    /* Initialize GType system */
    g_type_init ();
#endif

    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                           flags,
                                           NULL, /* GDBusInterfaceInfo */
                                           "org.freedesktop.NetworkManager",
                                           "/org/freedesktop/NetworkManager",
                                           "org.freedesktop.NetworkManager",
                                           NULL, /* GCancellable */
                                           &error);
    g_assert (proxy != NULL);

    /* List connections of system settings service */
    list_connections (proxy);

    g_object_unref (proxy);

    return 0;
}