c++ gdbus - 如何使用 gdbus 库获取接口的 属性?
c++ gdbus - how to fetch the property of interface using gdbus library?
我想知道 approach/code 片段以使用 c++ 代码片段从 dbus 接口获取 属性。
我尝试了以下方法,但出现错误。
方法#1 使用 g_dbus_proxy_get_cached_property 但它总是返回 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" 没有导出(或者可能不存在),可以' t 访问 属性 "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);
至少第一种形式应该可以工作(假设您在实际代码中处理错误)但是您的 D-Bus 对象路径存在问题。
首先,有效的对象路径以“/”开头,因此您可能想要 /org/freedesktop/NetworkManager/Device/0
... 但它似乎不是 NetworkManager 使用的路径。
查看他们的 API 参考资料,看来您可能真的想要 /org/freedesktop/NetworkManager/Devices/N
(注意复数形式 "Devices"),但请注意您不能真正确定最后一部分是什么路径(N)将是。在正确的代码中,您应该从 org.freedesktop.NetworkManager
获取设备对象路径,但为了调试,您可能只需要使用像 d-feet 这样的工具来查找可用的对象路径并使用它们。
非常感谢@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;
}
我想知道 approach/code 片段以使用 c++ 代码片段从 dbus 接口获取 属性。
我尝试了以下方法,但出现错误。
方法#1 使用 g_dbus_proxy_get_cached_property 但它总是返回 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" 没有导出(或者可能不存在),可以' t 访问 属性 "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);
至少第一种形式应该可以工作(假设您在实际代码中处理错误)但是您的 D-Bus 对象路径存在问题。
首先,有效的对象路径以“/”开头,因此您可能想要 /org/freedesktop/NetworkManager/Device/0
... 但它似乎不是 NetworkManager 使用的路径。
查看他们的 API 参考资料,看来您可能真的想要 /org/freedesktop/NetworkManager/Devices/N
(注意复数形式 "Devices"),但请注意您不能真正确定最后一部分是什么路径(N)将是。在正确的代码中,您应该从 org.freedesktop.NetworkManager
获取设备对象路径,但为了调试,您可能只需要使用像 d-feet 这样的工具来查找可用的对象路径并使用它们。
非常感谢@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;
}