从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && make install
client.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <dbus/dbus.h>
- #include <unistd.h>
- const int RES_SUCCESS = -1;
- const int RES_FAILED = 0;
- int my_dbus_initialization(char const * _bus_name, DBusConnection ** _conn) {
- DBusError err;
- int ret;
- dbus_error_init(&err);
- *_conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
- if(dbus_error_is_set(&err)) {
- printf("Connection Error ");
- dbus_error_free(&err);
- return RES_FAILED;
- }
- ret = dbus_bus_request_name(*_conn, _bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
- if(dbus_error_is_set(&err)){
- printf("Requece name error ");
- dbus_error_free(&err);
- return RES_FAILED;
- }
- if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
- return RES_FAILED;
- }
- return RES_SUCCESS;
- }
- int my_dbus_send_sigal(DBusConnection * conn) {
- dbus_uint32_t serial = 0;
- DBusMessage* msg;
- DBusMessageIter args;
- char sigvalue[20] = "liyiwen";
- msg = dbus_message_new_signal("/test/signal/Object", // object name
- "test.signal.Type", // interface name
- "Test"); // name of signal
- if (NULL == msg) {
- printf("Message Null");
- return RES_FAILED;
- }
- dbus_message_iter_init_append(msg, &args);
- printf("%s ", sigvalue);
- dbus_uint32_t my_age = 10;
- if(!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &my_age)) {
- printf("Out of memory ");
- return RES_FAILED;
- }
- if(!dbus_connection_send(conn, msg, &serial)) {
- printf("Out of memory");
- return RES_FAILED;
- }
- dbus_connection_flush(conn);
- dbus_message_unref(msg);
- return RES_SUCCESS;
- }
- int main(int agrc, char** argv)
- {
- DBusConnection * conn;
- printf("Start ");
- if (RES_FAILED == my_dbus_initialization("test.method.client", &conn)) {
- exit(1);
- }
- my_dbus_send_sigal(conn);
- while(1){sleep(10);}
- return 0;
- }
- sercer.c
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <dbus/dbus.h>
- #include <unistd.h>
- const int RES_SUCCESS = -1;
- const int RES_FAILED = 0;
- int my_dbus_initialization(char const * _bus_name, DBusConnection **_conn) {
- DBusError err;
- int ret;
- dbus_error_init(&err);
- *_conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
- if(dbus_error_is_set(&err)) {
- printf("Connection Error(%s) ", err.message);
- dbus_error_free(&err);
- return RES_FAILED;
- }
- ret = dbus_bus_request_name(*_conn, _bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
- if(dbus_error_is_set(&err)){
- printf("Requece name error(%s) ", err.message);
- dbus_error_free(&err);
- return RES_FAILED;
- }
- if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
- return RES_FAILED;
- }
- return RES_SUCCESS;
- }
- int main(int agrc, char** argv)
- {
- DBusError err;
- DBusMessage* msg;
- DBusMessageIter args;
- dbus_error_init(&err);
- DBusConnection *conn;
- if (RES_FAILED == my_dbus_initialization("test.method.server", &conn)) {
- exit(1);
- }
- dbus_bus_add_match(conn, "type='signal', interface='test.signal.Type'", &err);
- dbus_connection_flush(conn);
- if(dbus_error_is_set(&err)) {
- printf("dbus_bus_add_match err (%s)", err.message);
- return RES_FAILED;
- }
- while(1) {
- dbus_connection_read_write(conn, 0);
- msg = dbus_connection_pop_message(conn);
- if(NULL == msg) {
- sleep(1);
- continue;
- }
- if(dbus_message_is_signal(msg, "test.signal.Type", "Test")) {
- if(!dbus_message_iter_init(msg, &args)) {
- printf("dbus_message_iter_init error, msg has no arguments! ");
- }
- else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args)){
- printf("not a uint 32 type ! ");
- }
- else {
- dbus_uint32_t my_age = 0;
- dbus_message_iter_get_basic(&args, &my_age);
- printf("Got signal with value %d ", my_age);
- }
- }
- dbus_message_unref(msg);
- }
- return 0;
- }
- http://dbus.freedesktop.org/doc/dbus-specification.html 这当然是最权威最重要的资料,但我觉得不是一个很好的入门资料。
- http://dbus.freedesktop.org/doc/dbus-tutorial.html 这里面有一些不错的例子,对Names 的解释也很好,但用的是 glib 的 binding,不能探究更底层的动作一度还是让我云里雾里。
- http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html DBus 的 C 编程接口的在线文档,非常棒也非常有用
- http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html 如何用 C API 层面的 DBus ,相见恨晚。