gpt4 book ai didi

c++ - 使用 gdbus-codegen 骨架时如何验证 D-Bus 属性

转载 作者:太空狗 更新时间:2023-10-29 23:13:41 28 4
gpt4 key购买 nike

我正在使用 gdbusgdbus-codegen 在 D-Bus 上创建服务。

自省(introspection)是这样的:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="com.example.foo">

<property name="Bar" type="s" access="readwrite" />

</interface>
</node>

我正在像这样执行 gdbus-codegen:

gdbus-codegen --interface-prefix com.example --generate-c-code=foo foo.xml

我的 main.cpp 看起来像这样:

#include <iostream>

#include "foo.h"

void OnBarChanged(GObject * gobject, GParamSpec * pspec, gpointer user_data)
{
std::cout << "Bar: " << foo_get_bar((Foo *)gobject) << std::endl;
}

void OnBusNameAquired(GDBusConnection * connection,
const gchar * name,
gpointer user_data)
{
Foo * foo = foo_skeleton_new();

g_signal_connect(foo, "notify::bar", G_CALLBACK(&OnBarChanged), NULL);

g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(foo),
connection,
"/com/example/foo",
NULL);
}

int main()
{
std::cout << "Testing DBus properties" << std::endl;

GMainLoop * loop;
loop = g_main_loop_new(NULL, FALSE);
g_bus_own_name(G_BUS_TYPE_SESSION,
"com.example.foo",
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL,
OnBusNameAquired,
NULL,
NULL,
NULL);
g_main_loop_run(loop);
return 0;
}

这按预期工作,我能够使用以下方法设置和获取属性:

gdbus call --session --dest com.example.foo --object-path /com/example/foo --method org.freedesktop.DBus.Properties.Set "com.example.foo" "Bar" "<'baz'>"

gdbus call --session --dest com.example.foo --object-path /com/example/foo --method org.freedesktop.DBus.Properties.Get "com.example.foo" "Bar"
(<'baz'>,)

问题:

我想同步验证属性的设置,并在失败时返回错误。如何使用 gdbus-codegen 生成的代码来实现这一点?

附言:

代码泄露,通常不适合生产。我现在没问题:-)

编辑

经过继续研究,似乎 D-Bus 属性正在使用底层的 GObject 属性功能。当所有这些都由 gdbus-codegen-code 设置时,是否可以安装自定义验证器?

最佳答案

连接到 GDBusInterfaceSkeleton::g-authorize-method来自 foo 骨架的信号。您的回调将针对您导出的对象处理的每个 D-Bus 方法调用被调用——您可以在 org.freedesktop.DBus.Properties.Set 上匹配然后调用并进行验证。

在 flatpak 中有一个这样的例子(对于任意方法调用,不是 D-Bus 属性设置;但原理是一样的):https://github.com/flatpak/flatpak/blob/c915f73b41688a7dc2ec7f0ab2fbcf1a7c738841/system-helper/flatpak-system-helper.c#L1192

关于c++ - 使用 gdbus-codegen 骨架时如何验证 D-Bus 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37541857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com