dbus 初探 版本 dbus-1.4.0
转载时请注明出处和作者联系方式
文章出处:http://www.lt-net.cn
作者联系方式:刘敏 <qtgirl666@gmail.com>
编译系统 | Ubuntu10.04(确保/opt 可写) |
交叉编译器 | arm-linux-gcc 4.3.3 |
硬件设备 | LT2440开发板 |
测试软件 | dbus-1.4.0 |
依赖库 | libxml2-2.7.6 |
一直以来都想学习dbus,想把dbus移植到LT2440开发板上,今天弱弱的看了一下相关的资料,编译了dbus库,并且做了一个测试。
首先,编译 libxml2-2.7.6
$ ./configure --host=arm-linux --prefix=/opt/gtkfb
$ make
$ make install
其次,编译dbus-1.4.0
需要先让dbus找到 libxml 库,使用PKG_CONFIG_PATH,使用下面的export 命令即可
$ export PKG_CONFIG_PATH=/opt/gtkfb/lib/pkgconfig
$ ./configure --host=arm-linux --prefix=/opt/gtkfb --without-x --with-xml=libxml
$ make
$ make install
这样dbus库就编译好了,并且会得到一些命令,在/opt/gtkfb/bin目录下,以dbus开头
将相关库拷贝到文件系统,拷贝以上命令到bin目录,已经移植好了,找一个程序做测试
sample.c
- /*
- * Example low-level D-Bus code.
- * Written by Matthew Johnson <dbus@matthew.ath.cx>
- *
- * This code has been released into the Public Domain.
- * You may do whatever you like with it.
- */
- #include <dbus/dbus.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- /**
- * Connect to the DBUS bus and send a broadcast signal
- */
- void sendsignal(char* sigvalue)
- {
- DBusMessage* msg;
- DBusMessageIter args;
- DBusConnection* conn;
- DBusError err;
- int ret;
- dbus_uint32_t serial = 0;
- printf("Sending signal with value %s/n", sigvalue);
- // initialise the error value
- dbus_error_init(&err);
- // connect to the DBUS system bus, and check for errors
- conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
- if (dbus_error_is_set(&err)) {
- fprintf(stderr, "Connection Error (%s)/n", err.message);
- dbus_error_free(&err);
- }
- if (NULL == conn) {
- exit(1);
- }
- // register our name on the bus, and check for errors
- ret = dbus_bus_request_name(conn, "test.signal.source", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
- if (dbus_error_is_set(&err)) {
- fprintf(stderr, "Name Error (%s)/n", err.message);
- dbus_error_free(&err);
- }
- if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
- exit(1);
- }
- // create a signal & check for errors
- msg = dbus_message_new_signal("/test/signal/Object", // object name of the signal
- "test.signal.Type", // interface name of the signal
- "Test"); // name of the signal
- if (NULL == msg)
- {
- fprintf(stderr, "Message Null/n");
- exit(1);
- }
- // append arguments onto signal
- dbus_message_iter_init_append(msg, &args);
- if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) {
- fprintf(stderr, "Out Of Memory!/n");