linux使用dbus

image

可以看到D-bus的对象、路径和方法

详见: https://wiki.deepin.org/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后台的连接 */
    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;
    }

    /* 给连接分配一个名字 不是必须*/
    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 dbus_send_signal(DBusConnection * connection, char *value)
{
    dbus_uint32_t serial = 0;
    DBusMessage* msg;
    DBusMessageIter args;

    /* 可以指定对象、接口、信号名 */
    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;
    }

    /* 设置信号内容 */
    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;
    }

    /* 发送信号 */
    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);

    /* 释放内存 */
    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后台的连接 */
    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;
    }

    /* 给连接分配一个名字 不是必须*/
    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;
    }

    /* 监听接口消息 */
    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

设置环境变量

export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-cEvKyeNF2G,guid=ea9b6fc2f80c05360b70be8e5fdc20a3

发送端和接收端都需要设置,而且要设置一致

使用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
posted @ 2020-12-18 11:35  thomas_blog  阅读(2676)  评论(0编辑  收藏  举报