Java教程

ubuntu 下dbus的环境搭建和使用

本文主要是介绍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\n");
    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 \n");
    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\n", 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\n");
    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\n");
    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) \n", 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) \n", 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!\n");
      67.             }
      68.             else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args)){
      69.                 printf("not a uint 32 type !\n");
      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\n", 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 ,相见恨晚。
        5. 转自:https://www.cnblogs.com/oracleloyal/p/5329947.html
这篇关于ubuntu 下dbus的环境搭建和使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!