Dbus 学习笔记

如何获得dbus的service的module name呢?

dbus-send --system            \
>   --dest=org.freedesktop.DBus \
>   --type=method_call          \
>   --print-reply               \
>   /org/freedesktop/DBus       \
>   org.freedesktop.DBus.ListNames

dbus 中的session 和service 区别是什么

bus和service的区别

A “bus” is a “communication world/space”. You can have several buses running simultaneously on the same computer: system & session are the traditionnal ones. There can also be private buses. They are usually identified by their daemon socket: “unix:path=/var/run/dbus/system_bus_socket”
A “service” is normally a program that is started on-demand and provide an expected interface (and a a “well-known” “bus name”, such as “org.gnome.keyring”)

service: 总线模式:采用总线模式时,系统需要维护一个DBus Daemon,每个DBus的请求通过DBus Daemon转发。这种模式Server只需要维护一个与DBus Daemon的链接,结构清晰,可以通过广播的方式发送消息到各个Client。

session:会话模式:这种模式一般称之为点对点的星型结构,Client与Server之间是直接的Peer2Peer的连接,少了DBus Daemon的中转,因此性能较好。

conf文件中的字段是怎么定义的?是否可以提前生成?

conf中定义的是bus的obj path 和 interface的权限

需要参考network manager中的nmcli命令封装 dbus的请求ipc接口?

dbus-send 的参数,如何调用到network manager的接口的?

dbus-send/dbus-monit的使用
https://gitlab.freedesktop.org/NetworkManager

  • 获取bus总线上,某个object的所有interface定义
sudo dbus-send --system --print-reply --type=method_call --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.Introspectable.Introspect
  • 根据上面的输出,获取一个method的输出
sudo dbus-send --system --print-reply --type=method_call --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.Debug.Stats.GetStats
  • 带参数的调用method
sudo dbus-send --system --print-reply --type=method_call --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.Properties.GetAll string:"org.freedesktop.DBus"
method return time=1632207028.948570 sender=org.freedesktop.DBus -> destination=:1.24 serial=3 reply_serial=2
   array [
      dict entry(
         string "Features"
         variant             array [
            ]
      )
      dict entry(
         string "Interfaces"
         variant             array [
               string "org.freedesktop.DBus.Monitoring"
               string "org.freedesktop.DBus.Debug.Stats"
            ]
      )
   ]
  • 在我们的系统里查看networkmanager服务的接口类型
  1. 获取所有NetworkManager模块中的xml接口和方法
dbus-send --system --print-reply --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.DBus.Introspectable.Introspect
  1. 根据设备名称获取Device路径
root@ubuntu:/data/service# dbus-send --system --print-reply --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.GetDeviceByIpIface string:"eth1"
method return time=1632210331.198178 sender=:1.889 -> destination=:1.9769 serial=361266 reply_serial=2
   object path "/org/freedesktop/NetworkManager/Devices/5"
  1. 根据新的object path查看对应这个object 的xml自定义方法
root@ubuntu:/data/service# dbus-send --system --print-reply --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager/Devices/5 org.freedesktop.DBus.Introspectable.Introspect
method return time=1632210400.598859 sender=:1.889 -> destination=:1.9780 serial=361616 reply_serial=2
   string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
                      "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<!-- GDBus 2.58.0 -->
<node>
  <interface name="org.freedesktop.DBus.Properties">
    <method name="Get">
      <arg type="s" name="interface_name" direction="in"/>
      <arg type="s" name="property_name" direction="in"/>
      <arg type="v" name="value" direction="out"/>
    </method>
    <method name="GetAll">
      <arg type="s" name="interface_name" direction="in"/>
      <arg type="a{sv}" name="properties" direction="out"/>
    </method>
    <method name="Set">
      <arg type="s" name="interface_name" direction="in"/>
      <arg type="s" name="property_name" direction="in"/>
      <arg type="v" name="value" direction="in"/>
    </method>
    <signal name="PropertiesChanged">
      <arg type="s" name="interface_name"/>
      <arg type="a{sv}" name="changed_properties"/>
      <arg type="as" name="invalidated_properties"/>
    </signal>
  </interface>
  <interface name="org.freedesktop.DBus.Introspectable">
    <method name="Introspect">
      <arg type="s" name="xml_data" direction="out"/>
    </method>
  </interface>
  <interface name="org.freedesktop.DBus.Peer">
    <method name="Ping"/>
    <method name="GetMachineId">
      <arg type="s" name="machine_uuid" direction="out"/>
    </method>
  </interface>
  <interface name="org.freedesktop.NetworkManager.Device">
    <method name="Reapply">
      <arg type="a{sa{sv}}" name="connection" direction="in"/>
      <arg type="t" name="version_id" direction="in"/>
      <arg type="u" name="flags" direction="in"/>
    </method>
    <method name="GetAppliedConnection">
      <arg type="u" name="flags" direction="in"/>
      <arg type="a{sa{sv}}" name="connection" direction="out"/>
      <arg type="t" name="version_id" direction="out"/>
    </method>
    <method name="Disconnect"/>
    <method name="Delete"/>
    <signal name="StateChanged">
      <arg type="u" name="new_state"/>
      <arg type="u" name="old_state"/>
      <arg type="u" name="reason"/>
    </signal>
    <property type="s" name="Udi" access="read"/>
    <property type="s" name="Interface" access="read"/>
    <property type="s" name="IpInterface" access="read"/>
    <property type="s" name="Driver" access="read"/>
    <property type="s" name="DriverVersion" access="read"/>
    <property type="s" name="FirmwareVersion" access="read"/>
    <property type="u" name="Capabilities" access="read"/>
    <property type="u" name="Ip4Address" access="read"/>
    <property type="u" name="State" access="read"/>
    <property type="(uu)" name="StateReason" access="read"/>
    <property type="o" name="ActiveConnection" access="read"/>
    <property type="o" name="Ip4Config" access="read"/>
    <property type="o" name="Dhcp4Config" access="read"/>
    <property type="o" name="Ip6Config" access="read"/>
    <property type="o" name="Dhcp6Config" access="read"/>
    <property type="b" name="Managed" access="readwrite"/>
    <property type="b" name="Autoconnect" access="readwrite"/>
    <property type="b" name="FirmwareMissing" access="read"/>
    <property type="b" name="NmPluginMissing" access="read"/>
    <property type="u" name="DeviceType" access="read"/>
    <property type="ao" name="AvailableConnections" access="read"/>
    <property type="s" name="PhysicalPortId" access="read"/>
    <property type="u" name="Mtu" access="read"/>
    <property type="u" name="Metered" access="read"/>
    <property type="aa{sv}" name="LldpNeighbors" access="read"/>
    <property type="b" name="Real" access="read"/>
  </interface>
  <interface name="org.freedesktop.NetworkManager.Device.Statistics">
    <signal name="PropertiesChanged">
      <arg type="a{sv}" name="properties"/>
    </signal>
    <property type="u" name="RefreshRateMs" access="readwrite"/>
    <property type="t" name="TxBytes" access="read"/>
    <property type="t" name="RxBytes" access="read"/>
  </interface>
  <interface name="org.freedesktop.NetworkManager.Device.Wired">
    <signal name="PropertiesChanged">
      <arg type="a{sv}" name="properties"/>
    </signal>
    <property type="s" name="HwAddress" access="read"/>
    <property type="s" name="PermHwAddress" access="read"/>
    <property type="u" name="Speed" access="read"/>
    <property type="as" name="S390Subchannels" access="read"/>
    <property type="b" name="Carrier" access="read"/>
  </interface>
</node>
"

如何创建一个dbusserver 程序

server demo

  • dbus 服务需申请well-know name
  • 通过g_signal_connect 将skeleton和 echo方法的回调匹配
  • 通过g_dbus_interface_skeleton_export把接口export到总线上来
    #dbus 配置文件
    dbus配置文件

自定义服务

  • 如何自定义自己的输入参数、输出数据的格式
  • 如何调用别人的method
  1. 可以利用proxy机制去访问其他服务的method方法
  2. 需要封装一层method的调用方法
  • 如何监听其他模块的事件信息
    同上
  • 如何发送本模块的信号
    xml -> signal

XML如何生成呢?

可以通过dtd文件生成xml文件

dbus’s type system

https://python-dbus-next.readthedocs.io/en/latest/type-system/index.html
https://dbus.freedesktop.org/doc/dbus-specification.html#type-system

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hanhandan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值