可以看到D-bus的對(duì)象、路徑和方法 詳見(jiàn): https://wiki./wiki/D-Feet client.c #include <stdio.h> #include <stdlib.h> #include <dbus/dbus.h> #include <unistd.h> static DBusConnection *connection; int dbus_init(char const * name) { DBusError error; int ret = 0; dbus_error_init(&error); /* 建立與D-Bus后臺(tái)的連接 */ connection = dbus_bus_get(DBUS_BUS_SESSION, &error); if(dbus_error_is_set(&error)) { fprintf(stderr, "dbus_bus_get: %s\n", error.message); dbus_error_free(&error); return -1; } /* 給連接分配一個(gè)名字 不是必須*/ ret = dbus_bus_request_name(connection, name, DBUS_NAME_FLAG_REPLACE_EXISTING, &error); if(dbus_error_is_set(&error)) { fprintf(stderr, "dbus_bus_request_name: %s\n", error.message); dbus_error_free(&error); return -1; } if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { return -1; } return 0; } /* 發(fā)送一個(gè)信號(hào) */ int dbus_send_signal(DBusConnection * connection, char *value) { dbus_uint32_t serial = 0; DBusMessage* msg; DBusMessageIter args; /* 可以指定對(duì)象、接口、信號(hào)名 */ msg = dbus_message_new_signal("/test/signal/Object", // object name "test.signal.Interface", // interface name "Name"); // name of signal if (NULL == msg) { fprintf(stderr, "dbus_message_new_signal error\n"); return -1; } /* 設(shè)置信號(hào)內(nèi)容 */ dbus_message_iter_init_append(msg, &args); if(!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &value)) { fprintf(stderr, "dbus_message_iter_append_basic error\n"); return -1; } /* 發(fā)送信號(hào) */ if(!dbus_connection_send(connection, msg, &serial)) { fprintf(stderr, "dbus_connection_send error\n"); return -1; } printf("dbus_send_signal: %s\n", value); dbus_connection_flush(connection); /* 釋放內(nèi)存 */ dbus_message_unref(msg); return 0; } int main(int agrc, char** argv) { int ret = 0; char *name = "test.method.client"; char *value = "hello furong"; printf("%s start\n", name); ret = dbus_init(name); if (ret < 0) { return ret; } while(1) { dbus_send_signal(connection, value); sleep(5); } return 0; } server.c #include <stdio.h> #include <stdlib.h> #include <dbus/dbus.h> #include <unistd.h> static DBusConnection *connection; int dbus_init(char const * name) { DBusError error; DBusMessage *message; int ret = 0; dbus_error_init(&error); /* 建立與D-Bus后臺(tái)的連接 */ connection = dbus_bus_get(DBUS_BUS_SESSION, &error); if(dbus_error_is_set(&error)) { fprintf(stderr, "dbus_bus_get: %s\n", error.message); dbus_error_free(&error); return -1; } /* 給連接分配一個(gè)名字 不是必須*/ ret = dbus_bus_request_name(connection, name, DBUS_NAME_FLAG_REPLACE_EXISTING, &error); if(dbus_error_is_set(&error)) { fprintf(stderr, "dbus_bus_request_name: %s\n", error.message); dbus_error_free(&error); return -1; } if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { return -1; } return 0; } int main(int agrc, char** argv) { DBusError error; DBusMessage* message; DBusMessageIter args; int ret = 0; char *name = "test.method.server"; dbus_error_init(&error); printf("%s start\n", name); ret = dbus_init(name); if (ret < 0) { return ret; } /* 監(jiān)聽(tīng)接口消息 */ dbus_bus_add_match(connection, "type='signal', interface='test.signal.Interface'", &error); if(dbus_error_is_set(&error)) { fprintf(stderr, "dbus_bus_add_match: %s\n", error.message); return -1; } while(1) { dbus_connection_read_write(connection, 0); message = dbus_connection_pop_message(connection); if(NULL == message) { sleep(1); continue; } if(dbus_message_is_signal(message, "test.signal.Interface", "Name")) { if(!dbus_message_iter_init(message, &args)) { fprintf(stderr, "dbus_message_is_signal error\n"); } else { switch(dbus_message_iter_get_arg_type(&args)) { case DBUS_TYPE_UINT32: { dbus_uint32_t value = 0; dbus_message_iter_get_basic(&args, &value); printf("dbus_message_iter_get_basic: %d\n", value); } break; case DBUS_TYPE_STRING: { char *value; dbus_message_iter_get_basic(&args, &value); printf("dbus_message_iter_get_basic: %s\n", value); } case DBUS_TYPE_BYTE: break; default: break; } } } dbus_message_unref(message); } return 0; } 編譯 ubuntu@VM-0-8-ubuntu:~/dbus-1.13.18/install/test$ gcc server.c -I ../../ -ldbus-1 -L ../lib/ –lpthread 獲取DBUS_SESSION_BUS_ADDRESS ubuntu@VM-0-8-ubuntu:~$ dbus-launch DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-cEvKyeNF2G,guid=ea9b6fc2f80c05360b70be8e5fdc20a3 DBUS_SESSION_BUS_PID=31602 設(shè)置環(huán)境變量 export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-cEvKyeNF2G,guid=ea9b6fc2f80c05360b70be8e5fdc20a3 發(fā)送端和接收端都需要設(shè)置,而且要設(shè)置一致 使用dbus-monitor接收消息 ubuntu@VM-0-8-ubuntu:~$ dbus-monitor --session "interface='test.signal.Interface'" signal time=1608278895.021694 sender=org.freedesktop.DBus -> destination=:1.16 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired string ":1.16" signal time=1608278895.021868 sender=org.freedesktop.DBus -> destination=:1.16 serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost string ":1.16" signal time=1608278897.288150 sender=:1.14 -> destination=(null destination) serial=18 path=/test/signal/Object; interface=test.signal.Interface; member=Name string "hello furong" signal time=1608278902.288276 sender=:1.14 -> destination=(null destination) serial=19 path=/test/signal/Object; interface=test.signal.Interface; member=Name string "hello furong" 使用server接收消息 ubuntu@VM-0-8-ubuntu:~/dbus-1.13.18/install/test$ ./a.out test.method.server start dbus_message_iter_get_basic: hello furong dbus_message_iter_get_basic: hello furong |
|