D-Bus 簡介

D-Bus 就像是一個「訊息總線」,允許應用程式彼此發送訊息、呼叫函數或訂閱事件。

 

以下簡單說明以 busctl 指令來跟 D-Bus 互動的範例:

busctl [list]
  - 顯示 bus 上所有的 service names,包含 system 及 user session。
      - UNIT: System daemon,
      - PROCESS: System daemon 所呼叫執行的執行檔。
      - NAME: PROCESS 在 D-Bus 所用到的名稱,稱為 bus name,也稱為 service name 或 bus name owner。
          - Well-Known Names (Service Names): 由其它 client 使用。
          - Unique Names (Connection Names):  由 D-Bus 分配,並於內部使用。
  - 在 D-Bus 上的 service 的用途:
      - 提供 interfaces 及 objects 供其它 client 端程式使用。

  - 也可以只列出 system 或 user session 的 service:
      - # busctl --system list
      - # busctl --user list

 

busctl tree [SERVICE...]
  - 顯示此 Service 所提供的 object tree,service name 可以從上個指令取得。

 

busctl introspect SERVICE OBJECT [INTERFACE]
  - 顯示此 Service 中,針對此特定 object 所支援的所有 interfaces, methods, properties and signals。
    如果有指定 [INTERFACE] 的話,則顯示其支援的 methods, properties and signals。

 

busctl call SERVICE OBJECT INTERFACE METHOD [SIGNATURE [ARGUMENT...]]
  - 呼叫此 Service 中,針對此特定 object 的 interface 的 method。

 

busctl get-property SERVICE OBJECT INTERFACE PROPERTY...
  - 呼叫此 Service 中,針對此特定 object 的 interface 的 property。

 

busctl monitor [SERVICE...]
  - 顯示在 D-Bus 所發送或接收的所有 messages,如果有指令 service 的話,則只顯示該 service 相關的 messages。

 

 

如上所示,可以從 busctl tree [SERVICE...] 來查查該 service 的所有 objects。
而如果要從 object 來反查是屬於哪個 service 的話,就會需要使用 mapper 指令。

mapper get-service OBJECT
  - 從指定的 object 反推出其所屬的 service



 

指令範例:

查看所有 d-bus service 是否有 xyz.openbmc_project.Network:
# busctl | grep xyz.openbmc_project.Network
:1.61                                                              598 phosphor-networ root             :1.61         xyz.openbmc_project.Network.service                              -       -
xyz.openbmc_project.Network                                        598 phosphor-networ root             :1.61         xyz.openbmc_project.Network.service                              -       -

查看該 service 的 object tree:
# busctl tree xyz.openbmc_project.Network
`- /xyz
  `- /xyz/openbmc_project
    `- /xyz/openbmc_project/network
      |- /xyz/openbmc_project/network/config
      `- /xyz/openbmc_project/network/eth1
        |- /xyz/openbmc_project/network/eth1/_310_2e6_2e10_2e217_2f24
        |- /xyz/openbmc_project/network/eth1/_66e80_3a_3a2fb_3af9ff_3afe5d_3a1a19_2f64
        |- /xyz/openbmc_project/network/eth1/dhcp4
        `- /xyz/openbmc_project/network/eth1/dhcp6

查看該 object 所支援的所有 interfaces, methods, properties and signals:
# busctl introspect xyz.openbmc_project.Network /xyz/openbmc_project/network/eth1/dhcp4
NAME                                          TYPE      SIGNATURE RESULT/VALUE FLAGS
org.freedesktop.DBus.Introspectable           interface -         -            -
.Introspect                                   method    -         s            -
org.freedesktop.DBus.Peer                     interface -         -            -
.GetMachineId                                 method    -         s            -
.Ping                                         method    -         -            -
org.freedesktop.DBus.Properties               interface -         -            -
.Get                                          method    ss        v            -
.GetAll                                       method    s         a{sv}        -
.Set                                          method    ssv       -            -
.PropertiesChanged                            signal    sa{sv}as  -            -
xyz.openbmc_project.Network.DHCPConfiguration interface -         -            -
.DNSEnabled                                   property  b         true         emits-change writable
.DomainEnabled                                property  b         true         emits-change writable
.HostNameEnabled                              property  b         true         emits-change writable
.NTPEnabled                                   property  b         true         emits-change writable
.SendHostNameEnabled                          property  b         true         emits-change writable

查看 property - DNSEnabled 的值:
# busctl get-property xyz.openbmc_project.Network /xyz/openbmc_project/network/eth1/dhcp4 xyz.openbmc_project.Network.DHCPConfiguration DNSEnabled
b true

呼叫 method - GetAll:
# busctl call xyz.openbmc_project.Network /xyz/openbmc_project/network/eth1/dhcp4 org.freedesktop.DBus.Properties GetAll s xyz.openbmc_project.Network.DHCPConfiguration
a{sv} 5 "DNSEnabled" b true "NTPEnabled" b true "HostNameEnabled" b true "DomainEnabled" b true "SendHostNameEnabled" b true

 

 

 

Reference
WIKI: D-Bus

 

 

文字內容 或 影像內容 部份參考、引用自網路,如有侵權,請告知。

 

全站熱搜
創作者介紹
創作者 silverwind1982 的頭像
silverwind1982

拾人牙慧

silverwind1982 發表在 痞客邦 留言(0) 人氣()