D-Bus、UDisk 和 Glibmm 绑定

发布于 2024-12-29 01:38:26 字数 2423 浏览 3 评论 0原文

我对简单的 D-Bus 概念有疑问。我使用 Glibmm D-Bus 绑定(Gio::DBus 命名空间)来访问 UDisks 接口。我想读取系统上找到的每个硬盘的一些属性,因此首先我需要枚举 UDisks 报告的所有磁盘,如下所示:

Glib::RefPtr<Gio::DBus::Connection> bus;

int main() {
    using namespace Glib;
    using namespace Gio;

    Glib::init();
    Gio::init();

    bus = DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SYSTEM);
    RefPtr<DBus::Proxy> udisks_proxy = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");

    VariantContainerBase devices_variant = udisks_proxy->call_sync("EnumerateDevices");
    VariantIter iterator(devices_variant.get_child(0));

    Variant<ustring> var;
    while(iterator.next_value(var)) {
        ustring name = var.get();

        LOG("device: '%s", name.c_str());
        process_device(name);
    }

    return 0;
}

这似乎工作正常,因为 call_sync()返回一个 VariantContainerBase,它保存 (ao) 对象,该对象基本上是:“对象路径数组的一个结构”。从文档中我读到“对象路径”类型的处理方式与“字符串”类型相同,这就是为什么在 get_child(0) 期间创建非类型化 VariantBase 的原因code> 允许将自身转换为 Variant 对象。使用此参数化变体,使用 var.get() 提取字符串非常简单。

但随后我尝试使用此方法从每个驱动器的属性中读取一些内容(在本例中为 NativePath 属性):

void process_device(const Glib::ustring& objpath) {
    using namespace Glib;
    using namespace Gio;

    RefPtr<DBus::Proxy> attrs = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", objpath, "org.freedesktop.DBus.Properties");

    std::vector<VariantBase> args;
    args.push_back(Variant<ustring>::create(objpath));
    args.push_back(Variant<ustring>::create("NativePath"));
    VariantContainerBase data = attrs->call_sync("Get", VariantContainerBase::create_tuple(args));

    LOG("return type: %s", data.get_type_string().c_str());
}

问题是 VariantContainerBase 对象包含 (v) 签名。这意味着该对象是“变体”,因此我无法将其转换为任何对象。

对属性的内省表明 NativePath 包含一个 string 值。那么为什么 call_sync() 方法返回一个变体类型的对象呢?我错过了什么吗?谁能告诉我,如何正确读取 NativePath 属性,而不使用 get_data() 方法,也不将数据 memcpy'ing 到我的自己的缓冲区?我想尽可能做到类型安全。

还有一件事。当我使用 data.print(true) 方法时,我以编码形式获取 NativePath 属性的正确内容。这意味着引擎知道这是一个字符串。那么为什么它将其报告为变体呢?这是一个错误还是一个功能? :P

抱歉我的英语,我的困惑,感谢您的帮助。

I'm having a problem with a simple D-Bus concept. I'm using Glibmm D-Bus bindings (Gio::DBus namespace) to access the UDisks interface. I'd like to read some attributes of every hard disk found on the system, so first I need to enumerate all disks that UDisks is reporting, like this:

Glib::RefPtr<Gio::DBus::Connection> bus;

int main() {
    using namespace Glib;
    using namespace Gio;

    Glib::init();
    Gio::init();

    bus = DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SYSTEM);
    RefPtr<DBus::Proxy> udisks_proxy = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");

    VariantContainerBase devices_variant = udisks_proxy->call_sync("EnumerateDevices");
    VariantIter iterator(devices_variant.get_child(0));

    Variant<ustring> var;
    while(iterator.next_value(var)) {
        ustring name = var.get();

        LOG("device: '%s", name.c_str());
        process_device(name);
    }

    return 0;
}

This seems to work OK, because call_sync() returns a VariantContainerBase, which holds the (ao) object, which basically is: "one struct of an array of object paths". From the docs I read that the "object path" type is handled in the same way as a "string" type, that's why the untyped VariantBase that is created during get_child(0) allows itself to be casted into Variant<ustring> object. Using this parametrized Variant, it's trivial to extract the string by using var.get().

But then I'm trying to read some stuff (in this case, the NativePath attribute) from the attributes of each drive using this method:

void process_device(const Glib::ustring& objpath) {
    using namespace Glib;
    using namespace Gio;

    RefPtr<DBus::Proxy> attrs = DBus::Proxy::create_sync(bus, "org.freedesktop.UDisks", objpath, "org.freedesktop.DBus.Properties");

    std::vector<VariantBase> args;
    args.push_back(Variant<ustring>::create(objpath));
    args.push_back(Variant<ustring>::create("NativePath"));
    VariantContainerBase data = attrs->call_sync("Get", VariantContainerBase::create_tuple(args));

    LOG("return type: %s", data.get_type_string().c_str());
}

The problem is that the VariantContainerBase object contains the (v) signature. This means that the object is "variant", so I can't cast it to anything.

Introspection of the attributes show that NativePath holds a string value. So why the call_sync() method is returning an object of a variant type? Am I missing something? Could anyone tell me, how can I properly read the NativePath attribute, without using get_data() method and without memcpy'ing the data into my own buffer? I'd like to do it as type-safe as possible.

One more thing. When I use the data.print(true) method, I get the proper contents of NativePath attribute in an encoded form. This means that the engine knows that this is a string. So why it reports it as a variant? Is it a bug or a feature? :P

Sorry for my english, my confusion, and thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2025-01-05 01:38:26

好吧,我有解决方案:这似乎是一个错误,或者至少这是一个我不理解的逻辑。

正确的方法是调用直接 Glib 函数 g_variant_get_child(),而不是调用 VariantContainerBaseget_child(0),如下所示:

GVariant *output;
g_variant_get_child(data.gobj(), 0, "v", & output);

它将正确地将 output 的变体类型设置为 v 内部的类型(在我的例子中,它将把 output 的类型设置为 >s)。之后,当您获取 s 类型的远程数据时,您可以将其转换为 Variant ,如下所示:

Variant<Glib::ustring> item(output);
Glib::ustring text = item.get();

它似乎有效。

OK, I have the solution: this seems to be a bug, or at least it's a logic that I don't understand.

Instead of calling VariantContainerBase's get_child(0), a proper way is to call a direct Glib function g_variant_get_child(), like f.e. this:

GVariant *output;
g_variant_get_child(data.gobj(), 0, "v", & output);

It will properly set the output's variant type to one that's inside v (in my case it will set the type of output to s). After this, when you get the remote data in the s type, you can cast it to a Variant<ustring> like this:

Variant<Glib::ustring> item(output);
Glib::ustring text = item.get();

It seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文