如何在使用 gdbus-codegen 框架时验证 D-Bus 属性
How to validate a D-Bus property when using gdbus-codegen skeleton
我正在使用gdbus
和gdbus-codegen
在D-Bus上创建一个服务。
内省是这样的:
<!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
而我的主要.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
代码设置时,是否可以安装自定义验证器?
连接到来自foo
骨架的GDBusInterfaceSkeleton::g-authorize-method
信号。导出的对象处理的每个 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++中检查2D数组中负值的输入验证
- LibGit2 SSH身份验证失败
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- constexpr上下文中std::initializer_list的验证
- 正在验证c++中用户的整数输入
- 加密++验证大文件签名
- C++卡验证问题
- 验证指针链
- 此代码验证公式是什么意思?
- cin 的十进制输入验证?
- 简单的 HTML 验证器
- 密码长度验证 (c++)
- 输入验证以筛选出字符、字符串和一系列整数
- 如何使用 CredUIPromptForWindowsCredentials验证凭据
- 指针验证的最佳实践?
- RSA_PKCS1签名验证
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 使用 C++ 验证 NMEA 句子
- 如何在使用 gdbus-codegen 框架时验证 D-Bus 属性