ubuntu12.04 安装dbus
1.下载dubs
下载https://dbus.freedesktop.org/releases/dbus/
dbus-1.8.0.tar.gz
2.配置编译
./configure –prefix=/home/xx/dbus/install/ (安装目录)
make
make install
3.使用
dbus-daemon是一个后台进程,负责消息的转发
./dbus-daemon –config-file=../etc/dbus-1/session.conf –print-address
会打印一个类似unix:abstract=/tmp/dbus-eW4m3qv5Pv,guid=48fba668e981b73068475c035acda368的值
每次启动的值都不一样
dbus-launch 是启动配置dbus-daemon的程序
4.编译应用程序dbus.c
gcc dbus.c -o dbus -I /home/xx/dbus/install/include/dbus-1.0/ -I /home/xx/dbus/install/lib/dbus-1.0/include/ -L /home/xx/dbus/install/lib/ -l dbus-1
/home/xx/dbus/install/include/dbus-1.0/ 为了找到dbus/dbus.h
/home/xx/dbus/install/lib/dbus-1.0/include/ 为了找到dbus-arch-deps.h
-L 指定动态库的目录
-l 要链接的动态库
5.测试
先运行dbus-launch打印
./dbus-launch
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-Pwy1U2h5hv,guid=9aa4b7bd692f6ffa512384615acdafb8
DBUS_SESSION_BUS_PID=9735
导出环境变量
export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-Pwy1U2h5hv,guid=9aa4b7bd692f6ffa512384615acdafb8
运行应用程序
./dbus send “hello test”
Sending signal with value hello test
Signal Sent
./dbus receive
Listening for signals
Match rule sent
Got Signal with value hello test
6.Dbus 调试工具 D-feet
安装 apt-get install d-feet
使用:填写Bus Address
7.dbus-send的使用
dbus-send :发送信息到message bus
dbus-send [--system | --session] [--dest=NAME] [--print-reply] [--type=TYPE] <destination object
path> <message name> [contents ...]
--system :指系统总线(System Bus),用于系统linux和用户程序间进行通信
--session:指会话总线(Session Bus),用于桌面用户程序之间的通信
如果不指定选项,默认使用--session
--dest :是必须的选项,是准备发往bus上的名字描述
--dest= 连接名 对象路径 接口名.方法名 参数类型:参数值 参数类型:参数值
支持的参数类型包括:string,int32,uint32,double,byte,boolean。
--print-reply:打印接收到的返回信息
--type=TYPE:指定"method_call"或者"signal"(默认是signal)
eg:
dbus-send --system --print-reply --dest=org.freedesktop.DBus / org.freedesktop.DBus.ListActivatableNames
连接名:org.freedesktop.DBus
对象路径:/
接口名.方法名org.freedesktop.DBus.ListActivatableNames
返回:
method return sender=org.freedesktop.DBus -> dest=:1.217 reply_serial=2
array [
string "org.freedesktop.DBus"
string "com.ubuntu.LanguageSelector"
string "org.freedesktop.ColorManager"
string "com.hp.hplip"
string "com.ubuntu.USBCreator"
string "com.ubuntu.SoftwareProperties"
string "org.freedesktop.Avahi"
string "org.freedesktop.PolicyKit1"
string "org.debian.apt"
string "org.freedesktop.hostname1"
string "org.gnome.cpuFreqSelector"
string "org.freedesktop.ConsoleKit"
string "com.ubuntu.DeviceDriver"
string "org.freedesktop.UPower"
string "org.freedesktop.PackageKit"
string "org.freedesktop.UDisks"
string "org.freedesktop.ModemManager"
string "com.ubuntu.SystemService"
string "fi.epitest.hostap.WPASupplicant"
string "org.debian.AptXapianIndex"
string "org.freedesktop.RealtimeKit1"
string "fi.w1.wpa_supplicant1"
string "org.freedesktop.Accounts"
string "com.ubuntu.WhoopsiePreferences"
string "org.freedesktop.locale1"
string "org.gnome.SettingsDaemon.DateTimeMechanism"
string "org.freedesktop.nm_dispatcher"
string "org.opensuse.CupsPkHelper.Mechanism"
]
8.dbus-monitor使用
填写interface接口名监听
$ dbus-monitor --session "interface='com.example.dbus.Interface'"
signal sender=org.freedesktop.DBus -> dest=:1.1716 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
string ":1.1716"
method call sender=:1.1717 -> dest=com.example.dbus serial=2 path=/; interface=com.example.dbus.Interface; member=add
int32 5
int32 8