DBUS学习-DBUS实例

  • 由 b178903294创建, 最后修改于8月 20, 2019

一、原生DBUS实例

原生dbus客户端代码和服务端代码:

client.c 折叠源码

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <dbus/dbus.h>

  

void printf_Buddha()

{

    printf("                                                     \n ");

    printf("   **                    _ooOoo_                     \n ");

    printf("   **                   o8888888o                    \n ");

    printf("   **                   88\" . \"88                    \n ");

    printf("   **                   (| -_- |)                    \n ");

    printf("   **                    O\\ = /O                     \n ");

    printf("   **                ____/`---'\\____                 \n ");

    printf("   **              .   ' \\\\| |// `.                  \n ");

    printf("   **               / \\\\||| : |||//\\                \n ");

    printf("   **             / _||||| -:- |||||- \\              \n ");

    printf("   **               | | \\\\\\ - /// | |                \n ");

    printf("   **             | \\_| ''\\---/'' | |                \n ");

    printf("   **              \\ .-\\__ `-` ___/-. /              \n ");

    printf("   **           ___`. .' /--.--\\ `. . __             \n ");

    printf("   **        ."" '< `.___\\_<|>_/___.' >'"".          \n ");

    printf("   **       | | : `- \\`.;`\\ _ /`;.`/ - ` : | |       \n ");

    printf("   **         \\ \\ `-. \\_ __\\ /__ _/ .-` / /          \n ");

    printf("   ** ======`-.____`-.___\\_____/___.-`____.-'======  \n ");

    printf("   **                    `=---='                     \n ");

    printf("   **                                                \n ");

    printf("   ** .............................................  \n ");

    printf("   **          佛祖保佑             永无BUG          \n ");

    printf("   **/                                               \n ");

}

  

DBusConnection* init_bus()

{

    DBusConnection *connection;

    DBusError err;

    int ret;

  

    dbus_error_init(&err);

    connection = dbus_bus_get(DBUS_BUS_SESSION, &err);

    if(dbus_error_is_set(&err))

    {

        printf("connection error: :%s -- %s\n", err.name, err.message);

        dbus_error_free(&err);

        return NULL;

    }

    ret = dbus_bus_request_name(connection, "hello.world.client", DBUS_NAME_FLAG_REPLACE_EXISTING, &err);

    if(dbus_error_is_set(&err))

    {

        printf("Name error: %s -- %s\n", err.name, err.message);

        dbus_error_free(&err);

        return NULL;

    }

    if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)

        return NULL;

  

    return connection;

}

  

  

void send_signal(DBusConnection *connection)

{

    DBusMessage *msg;

    DBusMessageIter arg;

    char *str = "hello world!";

  

  

    //创建一个signal对象

    //param1: path (这个逻辑来说,可以是任何字符串,只要符合规则即可)

    //param2: interface (一样)

    //param3: 信号方法名(必须与服务端名匹配)

    if((msg = dbus_message_new_signal("/hello""aa.bb.cc""alarm_test")) == NULL)

    {

        printf("message is NULL\n");

        return;

    }

#if 0

     //这个看需求添加,一般来说,信号是一种单向广播,加上这一句变单向单播

     //param2: bus_name

    if(!dbus_message_set_destination(msg, "hello.world.service"))

        {

                printf("memory error\n");

        }

#endif

  

    //添加参数的一些接口

    dbus_message_iter_init_append(msg, &arg);

    dbus_message_iter_append_basic(&arg, DBUS_TYPE_STRING, &str);

    //入队

    dbus_connection_send(connection, msg, NULL);

    //发送

    dbus_connection_flush(connection);

    //释放内存

    dbus_message_unref(msg);

  

    return;

}

  

void send_method_call(DBusConnection *connection)

{

    DBusMessage *msg;

    DBusMessageIter arg;

    DBusPendingCall *pending;

    int a = 100;

    int b = 99;

    int sum;

    //输入a和b两个整型数据

    printf("please input value of a and b \n");

    scanf("%d %d", &a, &b);

    printf("a= %d b=%d \n", a,b);

    msg = dbus_message_new_method_call("hello.world.service""/hello/world","hello.world""add");

    if(msg == NULL)

    {

        printf("no memory\n");

        return;

    }

  

    dbus_message_iter_init_append(msg, &arg);

        if(!dbus_message_iter_append_basic (&arg, DBUS_TYPE_INT32,&a)){

            printf("no memory!");

            dbus_message_unref(msg);

            return;

        }

    if(!dbus_message_iter_append_basic (&arg, DBUS_TYPE_INT32,&b)){

            printf("no memory!");

            dbus_message_unref(msg);

            return;

        }

  

    //入队message,等待回复

    //param1: 连接描述符

    //param2: message

    //param3: 相当于一个回调的一个描述符,为了

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值