1

我有一个 D-Bus 方法,它接收将字符串映射到结构的字典作为输入:

<method name="myMethod">
  <arg name="input_dict" type="a{s(ss)}" direction="in"/>
  ...
</method>

我试图使用 busctl 调用这种方法但没有成功。到目前为止,我已经尝试使用以下参数:

busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 "str1" \( "str2" "str3" \)
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 str1 str2 str3
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" str1 str2 str3
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 str1 '(ss)' str2 str3
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 str1 \(ss\) str2 str3
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 str1 "ss" str2 str3
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 str1 \"ss\" str2 str3
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 str1 \(str2 str3\)
busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a{s(ss)}" 1 str1 \(ss\) \(str2 str3\)

根据通话,我收到不同的错误:

No such method 'WakeUp' in interface 'com.verisure.cuxscored.DECT' at object path '/com/verisure/cuxscored/dect' (signature 'a{s(ss)}')

Too many parameters for signature.

因此,我的问题是:使用 busctl 时如何将参数传递给 myMethod?

编辑

感谢ukBaz的回答,我得到了洞察力。我也有一个 getter 方法,我之前调用过,我得到的结果与 ukBaz 的有点不同:

<method name="myGetMethod">
  <arg name="output_dict" type="a{s(ss)}" direction="out"/>
  ...
</method>

运行 busctl 命令它给了我这个:

a(s(ss)) 1 "str1" "str2" "str3"

注意与预期解决方案的区别(a(s(ss))而不是a{s(ss)},括号而不是大括号)。如果我做对了,那么输出应该被解释为一个包含一个字符串的结构数组,以及另一个包含两个字符串的结构。

我不太确定为什么结果不同,但我已经克服了使用 getter 的输出直接作为另一种方法的输入的问题:

busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod `busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myGetMethod`

同样,如果我“按原样”使用 myGetMethod 的输出,该方法也可以工作:

busctl call ${SERVICE} ${OBJECT} ${INTERFACE} myMethod "a(s(ss))" 1 "str1" "str2" "str3"

所以剩下的唯一问题是为什么 D-bus 方法返回一个结构数组,即使它被声明为返回一个字典。

编辑 2

如果我运行 dbusctl introspect,这就是我得到的:

NAME            TYPE      SIGNATURE RESULT/VALUE FLAGS
${INTERFACE}    interface -         -            -
.myGetMethod    method    -         a{s(ss)}     -
.myMethod       method    a{s(ss)}  -            -
...

作为一个问题,我也尝试过使用 D-Feet,但没有更好的结果。我调用了一个返回 a{s(ss)} 类型值的方法,D-Feet 返回以下结构:

[('str1', ('str2', 'str3'))]

如果我然后使用相同的结构在 D-Feet 上调用 myMethod,我得到一个 DBus.Error.UnknownMethod:

No '
 "such method 'myMethod' in interface '${INTERFACE}' at object "
 "path '${OBJECT}' (signature 'a{s(ss)}') (19)")

但是,如果我使用参数、大括号等,我会收到不同的错误消息,但我无法让该方法正常工作。那么,在使用 D-Feet 时,将这种参数传递给 myMethod 的适当方式是什么?

4

1 回答 1

1

我为自己创建了一个带有该签名的 DBus 服务来测试它:

from gi.repository import GLib
from pydbus import SessionBus

loop = GLib.MainLoop()

class MyDBUSService(object):
    """
        <node>
            <interface name='net.lew21.pydbus.ClientServerExample'>
                <method name='Hello'>
                    <arg type='a{s(ss)}' name='response' direction='in'/>
                </method>
                <method name='Echo'>
                    <arg type='a{s(ss)}' name='response' direction='out'/>
                </method>
                <method name='Quit'/>
            </interface>
        </node>
    """

    def Hello(self, data):
        """Prints the data sent to it"""
        for my_key in data:
            print(f'key: {my_key} - Value: {data[my_key]}')

    def Echo(self):
        """Sends a known string back"""
        return {"str1" : ("str2", "str3")}

    def Quit(self):
        """removes this object from the DBUS connection and exits"""
        loop.quit()

bus = SessionBus()
bus.publish("net.lew21.pydbus.ClientServerExample", MyDBUSService())
loop.run()

内省busctl给出了:

$ busctl --user introspect net.lew21.pydbus.ClientServerExample /net/lew21/pydbus/ClientServerExample net.lew21.pydbus.ClientServerExample 
NAME                                 TYPE      SIGNATURE RESULT/VALUE FLAGS
.Echo                                method    -         a{s(ss)}     -
.Hello                               method    a{s(ss)}  -            -
.Quit                                method    -         -            -

结果输出Echo为:

$ busctl --user call net.lew21.pydbus.ClientServerExample /net/lew21/pydbus/ClientServerExample net.lew21.pydbus.ClientServerExample Echo
a{s(ss)} 1 "str1" "str2" "str3"

我使用该输出来锻炼如何将值放入:

$ busctl --user call net.lew21.pydbus.ClientServerExample /net/lew21/pydbus/ClientServerExample net.lew21.pydbus.ClientServerExample Hello "a{s(ss)}" 1 str str2 str3

这从我的 Python 脚本中给出了正确的输出:

 $ python3 dbus_service_struct.py
key: str - Value: ('str2', 'str3')

所以我不知道为什么你的第二次尝试没有成功

为了完整起见,下面是一个在字典中发送两个值的示例:

$ busctl --user call net.lew21.pydbus.ClientServerExample /net/lew21/pydbus/ClientServerExample net.lew21.pydbus.ClientServerExample Hello 'a{s(ss)}' 2 "key1" "value1:1" "value1:2" "key2" "value2:1" "value2:1"
$ python3 dbus_service_struct.py
key: key1 - Value: ('value1:1', 'value1:2')
key: key2 - Value: ('value2:1', 'value2:1')
于 2021-07-15T15:55:50.523 回答