• ubuntu 下dbus的环境搭建和使用


    从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && make install

    client.c

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <dbus/dbus.h>
    4. #include <unistd.h>
    5.  
    6. const int RES_SUCCESS = -1;
    7. const int RES_FAILED  = 0;
    8.  
    9. int my_dbus_initialization(char const * _bus_name, DBusConnection ** _conn) {
    10.     DBusError err;
    11.  
    12.     int ret;
    13.  
    14.     dbus_error_init(&err);
    15.  
    16.     *_conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
    17.     if(dbus_error_is_set(&err)) {
    18.         printf("Connection Error ");
    19.         dbus_error_free(&err);
    20.         return RES_FAILED;
    21.     }
    22.  
    23.     ret = dbus_bus_request_name(*_conn, _bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
    24.     if(dbus_error_is_set(&err)){
    25.         printf("Requece name error ");
    26.         dbus_error_free(&err);
    27.         return RES_FAILED;
    28.     }
    29.  
    30.     if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
    31.         return RES_FAILED;
    32.     }
    33.  
    34.     return RES_SUCCESS;
    35. }
    36.  
    37. int my_dbus_send_sigal(DBusConnection * conn) {
    38.     dbus_uint32_t serial = 0;
    39.     DBusMessage* msg;
    40.     DBusMessageIter args;
    41.     char sigvalue[20] = "liyiwen";
    42.  
    43.     msg = dbus_message_new_signal("/test/signal/Object",  // object name
    44.         "test.signal.Type",     // interface name
    45.         "Test");                // name of signal
    46.  
    47.     if (NULL == msg) {
    48.         printf("Message Null");
    49.         return RES_FAILED;
    50.     }
    51.  
    52.     dbus_message_iter_init_append(msg, &args);
    53.  
    54.     printf("%s ", sigvalue);
    55.     dbus_uint32_t my_age = 10;
    56.     if(!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &my_age)) {
    57.         printf("Out of memory ");
    58.         return RES_FAILED;
    59.     }
    60.  
    61.     if(!dbus_connection_send(conn, msg, &serial)) {
    62.         printf("Out of memory");
    63.         return RES_FAILED;
    64.     }
    65.     dbus_connection_flush(conn);
    66.  
    67.     dbus_message_unref(msg);
    68.  
    69.     return RES_SUCCESS;
    70.  
    71. }
    72.  
    73. int main(int agrc, char** argv)
    74. {
    75.     DBusConnection * conn;
    76.  
    77.     printf("Start ");
    78.     if (RES_FAILED == my_dbus_initialization("test.method.client", &conn)) {
    79.         exit(1);
    80.     }
    81.     my_dbus_send_sigal(conn);
    82.  
    83.     while(1){sleep(10);}
    84.  
    85.     return 0;
    86. }
    87. sercer.c
      1. #include <stdio.h>
      2. #include <stdlib.h>
      3. #include <dbus/dbus.h>
      4. #include <unistd.h>
      5.  
      6. const int RES_SUCCESS = -1;
      7. const int RES_FAILED  = 0;
      8.  
      9. int my_dbus_initialization(char const * _bus_name, DBusConnection **_conn) {
      10.     DBusError err;
      11.     int ret;
      12.  
      13.     dbus_error_init(&err);
      14.  
      15.     *_conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
      16.     if(dbus_error_is_set(&err)) {
      17.         printf("Connection Error(%s) ", err.message);
      18.         dbus_error_free(&err);
      19.         return RES_FAILED;
      20.     }
      21.  
      22.     ret = dbus_bus_request_name(*_conn, _bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
      23.     if(dbus_error_is_set(&err)){
      24.         printf("Requece name error(%s) ", err.message);
      25.         dbus_error_free(&err);
      26.         return RES_FAILED;
      27.     }
      28.     if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
      29.         return RES_FAILED;
      30.     }
      31.     return RES_SUCCESS;
      32. }
      33.  
      34. int main(int agrc, char** argv)
      35. {
      36.  
      37.     DBusError err;
      38.     DBusMessage* msg;
      39.     DBusMessageIter args;
      40.  
      41.     dbus_error_init(&err);
      42.     DBusConnection *conn;
      43.     if (RES_FAILED == my_dbus_initialization("test.method.server", &conn)) {
      44.         exit(1);
      45.     }
      46.  
      47.     dbus_bus_add_match(conn, "type='signal', interface='test.signal.Type'", &err);
      48.  
      49.     dbus_connection_flush(conn);
      50.     if(dbus_error_is_set(&err)) {
      51.         printf("dbus_bus_add_match err (%s)", err.message);
      52.         return RES_FAILED;
      53.     }
      54.  
      55.     while(1) {
      56.         dbus_connection_read_write(conn, 0);
      57.         msg = dbus_connection_pop_message(conn);
      58.  
      59.         if(NULL == msg) {
      60.             sleep(1);
      61.             continue;
      62.         }
      63.  
      64.         if(dbus_message_is_signal(msg, "test.signal.Type", "Test")) {
      65.             if(!dbus_message_iter_init(msg, &args)) {
      66.                 printf("dbus_message_iter_init error, msg has no arguments! ");
      67.             }
      68.             else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args)){
      69.                 printf("not a uint 32 type ! ");
      70.             }
      71.             else {
      72.                 dbus_uint32_t my_age = 0;
      73.                 dbus_message_iter_get_basic(&args, &my_age);
      74.                 printf("Got signal with value %d ", my_age);
      75.             }
      76.         }
      77.  
      78.         dbus_message_unref(msg);
      79.  
      80.     }
      81.  
      82.     return 0;
      83. }
        1. http://dbus.freedesktop.org/doc/dbus-specification.html  这当然是最权威最重要的资料,但我觉得不是一个很好的入门资料。
        2. http://dbus.freedesktop.org/doc/dbus-tutorial.html 这里面有一些不错的例子,对Names 的解释也很好,但用的是 glib 的 binding,不能探究更底层的动作一度还是让我云里雾里。
        3. http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html  DBus 的 C 编程接口的在线文档,非常棒也非常有用
        4. http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html 如何用 C API 层面的 DBus ,相见恨晚。
  • 相关阅读:
    两个序列最大子集公共和问题
    两个随机变量相关系数
    多项分布的定义
    算法分析结课总结回溯算法
    潜语义分析LSA相比向量空间模型VSM改变了什么
    类对象的声明和类对象指针的申请的种种
    C#中 常用的方法
    转:GridView鼠标移动行变色 (http://www.cnblogs.com/lovenets/articles/808071.html)[同ceng]
    WinForm 获取指定行列的Value
    华丽丽的分隔符
  • 原文地址:https://www.cnblogs.com/oracleloyal/p/5329947.html
Copyright © 2020-2023  润新知