[Share Experiences] 【小知识】如何获取DBus的连接名、接口名
Tofloor
poster avatar
enforcee
deepin
2022-11-13 07:58
Author

各位优秀的坛友应该已经学会用三剑客d-feet检查、dbus-monitor截获、dbus-send调用dbus消息了吧

不过一直有一个疑惑,d-feet里面展示的琳琅满目的连接、路径、接口、成员都是怎么获得的,似乎没有相关的命令?

joy

其实答案有点离谱也有点合理,DBus获取名称的接口也是用DBus实现的!话不多说,大家自己用d-feet和dbus-send试验一下便知

获取连接名用的是org.freedesktop.DBus连接中位于/org/freedesktop/DBus的org.freedesktop.DBus接口ListNames方法

图片.png

使用dbus-send调用:

dbus-send --session --dest=org.freedesktop.DBus --print-reply --type=method_call /org/freedesktop/DBus org.freedesktop.DBus.ListNames

获取路径和接口比较复杂,大家应该注意到其实每个连接、每个路径里都有org.freedesktop.DBus.Introspectable接口Introspect方法,其实这个就是用来列出接口成员的,不过比较复杂,返回值是采用xml数据格式

举个例子,假如我要查看的连接是org.freedesktop.ScreenSaver,但是我不知道其中有什么路径、什么接口,我就从“/”根目录开始,调用org.freedesktop.DBus.Introspectable接口Introspect方法:

dbus-send --session --dest=org.freedesktop.ScreenSaver --print-reply --type=method_call / org.freedesktop.DBus.Introspectable.Introspect

返回:

method return time=1668267560.214294 sender=:1.46 -> destination=:1.318 serial=114 reply_serial=2
   string "


  
  

"

可以看到这个连接中,根目录里有两个“文件夹”分别是org和ScreenSaver,试试看org里面有什么,只需要把"/"换成"/org":

dbus-send --session --dest=org.freedesktop.ScreenSaver --print-reply --type=method_call /org org.freedesktop.DBus.Introspectable.Introspect

返回:

method return time=1668268042.969891 sender=:1.46 -> destination=:1.323 serial=115 reply_serial=2
   string "


  

"

继续进入freedesktop文件夹:

dbus-send --session --dest=org.freedesktop.ScreenSaver --print-reply --type=method_call /org/freedesktop org.freedesktop.DBus.Introspectable.Introspect

返回:

method return time=1668268133.248823 sender=:1.46 -> destination=:1.324 serial=116 reply_serial=2
   string "


  

"

进入ScreenSaver:

dbus-send --session --dest=org.freedesktop.ScreenSaver --print-reply --type=method_call /org/freedesktop/ScreenSaver org.freedesktop.DBus.Introspectable.Introspect

返回:

method return time=1668268222.458222 sender=:1.46 -> destination=:1.329 serial=121 reply_serial=2
   string "


  
    
      
      
      
    
    
      
      
    
    
      
      
      
    
    
      
      
      
    
  
  
    
      
    
  
  
    
    
      
    
  
  
    
    
    
    
    
      
      
    
    
      
      
    
    
      
      
    
    
      
      
      
      
    
    
      
      
      
      
      
      
    
    
      
      
    
    
      
      
      
      
      
      
    
    
      
      
    
    
      
      
    
  


这样就能看到各个接口、成员、方法的输入和输出参数

其实也可以使用gdbus程序,这个是glib的一部分,功能比dbus-send更强。gdbus自带了introspect命令,比xml更适合终端查看,比如刚才的案例可以用:

gdbus introspect --session --dest org.freedesktop.ScreenSaver --object-path /org/freedesktop/ScreenSaver

Reply Favorite View the author
All Replies
心平气和
deepin
2022-11-13 08:21
#1

我想整理一个 deepin 下 dbus详细的一个文档,一起整理吗

Reply View the author
enforcee
deepin
2022-11-13 08:28
#2
心平气和

我想整理一个 deepin 下 dbus详细的一个文档,一起整理吗

可以啊,不过我会的也不多

blush

Reply View the author
心平气和
deepin
2022-11-13 10:19
#3
enforcee

可以啊,不过我会的也不多

blush

我也是菜鸡,只不过dbus 这块 缺乏文档,想要完善下

Reply View the author
gfdgd_xi
Moderator
2022-11-13 18:09
#4

like

Reply View the author
ThinKinG
deepin testing team
2022-12-27 18:26
#5

https://wiki.deepin.org/zh/07_%E6%8C%89%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%E5%88%92%E5%88%86/Linux%E6%A1%8C%E9%9D%A2/freeDesktop%E7%BF%BB%E8%AF%91/%E7%B3%BB%E7%BB%9F%E7%99%BB%E5%BD%95%E7%9A%84Dbus%E6%8E%A5%E5%8F%A3

可以参考下

Reply View the author