Can you retrieve a D-Bus property without calling org.freedesktop.DBus.Properties.Get?

StackOverflow https://stackoverflow.com/questions/18243912

  •  24-06-2022
  •  | 
  •  

質問

Say I want to programmatically get the interface name of my ethernet card. This seems to work:

dbus-send --print-reply \
          --type=method_call \
          --system \
          --dest=org.freedesktop.NetworkManager \
          /org/freedesktop/NetworkManager/Devices/0 \
          org.freedesktop.DBus.Properties.Get \
          string:org.freedesktop.NetworkManager.Device \
          string:Interface

Which returns:

method return sender=:1.5 -> dest=:1.135 reply_serial=2
   variant       string "eth0"

Is there some way of cutting out the middleman org.freedesktop.DBus.Properties.Get and retrieve the property more directly? Alas, calling it as a method does not work:

dbus-send --print-reply \
          --type=method_call \
          --system \
          --dest=org.freedesktop.NetworkManager \
          /org/freedesktop/NetworkManager/Devices/0 \
          org.freedesktop.NetworkManager.Device.Interface

Returns:

Error org.freedesktop.DBus.Error.UnknownMethod: 
Method "Interface" with signature "" on interface 
"org.freedesktop.NetworkManager.Device" doesn't exist

I ask because having to call org.freedesktop.DBus.Properties.Get looks like having to call a object.getProp("someproperty") instead of object.getSomeProperty() in Python/Java/etc.

役に立ちましたか?

解決 2

No.

Most likely org.freedesktop.DBus.Properties.GetAll will return you same value, but internally every service implement properties as handlers to messages with org.freedesktop.DBus.Properties.Get/org.freedesktop.DBus.Properties.GetAll method calls.

It looks like object.getProp("someproperty") because it actually is more like this pseudo-code

bus.handleMessage({
  service: "org.freedesktop.NetworkManager",
  object: "/org/freedesktop/NetworkManager/Devices/0",
  iface: "org.freedesktop.NetworkManager.Device.Interface",
  body: [ "org.freedesktop.NetworkManager.Device", "Interface"],
  thisMessageIsReplyTo: null
})

Internally every method call/signal/reply is just a message with big signature (service name/object path/interface) and body

他のヒント

Yep, you can do that if you use qdbus. I don't have NetworkManager with me, but a command like that should work:

qdbus --system \
      org.freedesktop.NetworkManager \
      /org/freedesktop/NetworkManager/Devices/0 \
      org.freedesktop.NetworkManager.Device.Interface

There are various command-line clients for talking to D-Bus, some are more convenient than others. Here's the list of the ones I know.

  • dbus-send (provided with D-Bus itself)
  • gdbus (provided by GLib)
  • qdbus (provided by Qt)
  • busctl (provided by systemd)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top