2014-12-07 52 views
0

我正在尋找一個示例c代碼來測量其在兩個簡單應用程序之間發送數據而不使用任何glib綁定的時間量,我在許多帖子中看到http://www.matthew.ath.cx/misc/dbus有一個例子,但該鏈接現在不存在。DBus:使用直接dbus API測量數據交換的時間

我的代碼發送時沒有提供任何錯誤,但接收應用程序沒有收到任何內容,也附加了service.conf文件。得到了一些其他網站的驗證碼輸入while循環中得到main函數之前

////////////////////////////Sender Code 

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <time.h> 
#include <unistd.h> 
#include <signal.h> 
#include <dbus/dbus.h> 
#define false 0 
#define true 1 
#define bool char 

#define DERR_SHOW_CLEAN(error)        \ 
    if (dbus_error_is_set(&(error)))       \ 
    {              \ 
     printf("error [%s:%d] %s\n %s\n",     \ 
       __FILE__, __LINE__, error.name, error.message); \ 
     dbus_error_free(&(error));       \ 
    } 

int main() { 
    dbus_bool_t rc; 
    DBusError error; 
    DBusConnection* conn; 
    int ret; 
    char* sigvalue = "Test"; 
    // initialise the errors 
    dbus_error_init(&error); //TODO 
    conn = dbus_connection_open("unix:path=/dev/shmem/dbus_service_socket", &error); 
    DERR_SHOW_CLEAN(error); 

    dbus_uint32_t serial = 0; // unique number to associate replies with requests 
    DBusMessage* msg; 
    DBusMessageIter args; 

    rc = dbus_bus_register(conn, &error); 
    DERR_SHOW_CLEAN(error); 

    // create a signal and check for errors 
    msg = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal 
    "org.freedesktop.DBus", // interface name of the signal 
    "Test"); // name of the signal 
    if (NULL == msg) 
    { 
    fprintf(stderr, "Message Null\n"); 
    } 

    // append arguments onto signal 
    dbus_message_iter_init_append(msg, &args); 
    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) { 
    fprintf(stderr, "Out Of Memory!\n"); 
    } 
    while(1) 
    { 
     // send the message and flush the connection 
     if (!dbus_connection_send(conn, msg, &serial)) { 
     fprintf(stderr, "Out Of Memory!\n"); 
     break; 
     } 
     else 
     { 
      sleep(5); 
     } 
    } 
    dbus_connection_flush(conn); 

    // free the message 
    dbus_message_unref(msg); 
    return 0; 
} 

////////////////////////////Receiver Code 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <time.h> 
#include <unistd.h> 
#include <dbus/dbus.h> 
#define false 0 
#define true 1 
#define bool char 

#define DERR_SHOW_CLEAN(error)        \ 
    if (dbus_error_is_set(&(error)))       \ 
    {              \ 
     printf("error [%s:%d] %s\n %s\n",     \ 
       __FILE__, __LINE__, error.name, error.message); \ 
     dbus_error_free(&(error));       \ 
    } 

int main() 
{ 
    DBusError err; 
    DBusConnection* conn; 
    DBusMessage* msg; 
    DBusMessageIter args; 
    int ret; 
    char* sigvalue; 
    dbus_bool_t rc; 
    // initialise the errors 
    dbus_error_init(&err); //TODO 
    conn = dbus_connection_open("unix:path=/dev/shmem/dbus_service_socket", &err); 
    DERR_SHOW_CLEAN(err); 
    rc = dbus_bus_register(conn, &err); 
    DERR_SHOW_CLEAN(err); 

    while (true) { 
     // non blocking read of the next available message 
     dbus_connection_read_write(conn, 0); 
     msg = dbus_connection_pop_message(conn); 
     printf("Got Si3\n"); 
     // loop again if we haven't read a message 
     if (NULL == msg) { 
      sleep(5); 
      continue; 
     } 

     // check if the message is a signal from the correct interface 
     // and with the correct name 
     if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "Test")) { 
      // read the parameters 
      printf("Got Si4\n"); 
      if (!dbus_message_iter_init(msg, &args)) 
       fprintf(stderr, "Message has no arguments!\n"); 
      else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) 
       fprintf(stderr, "Argument is not string!\n"); 
      else { 
       dbus_message_iter_get_basic(&args, &sigvalue); 
       printf("Got Signal with value %s\n", sigvalue); 
      } 
     } 
     else 
     { 
      printf("Got Signal with type %s\n", dbus_message_get_sender(msg)); 
     } 
     // free the message 
     dbus_message_unref(msg); 
    } 

} 


/////service.conf file contents which is used while dbus-daemon starts 
<!-- This configuration file controls the systemwide message bus. 
    Add a system-local.conf and edit that rather than changing this 
    file directly. --> 

<!-- Note that there are any number of ways you can hose yourself 
    security-wise by screwing up this file; in particular, you 
    probably don't want to listen on any more addresses, add any more 
    auth mechanisms, run as a different user, etc. --> 

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN" 
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> 
<busconfig> 

    <!-- Our well-known bus type, do not change this --> 
    <type>system</type> 

    <!-- Run as special user --> 
    <user>root</user> 

    <!-- Fork into daemon mode --> 
    <fork/> 

    <!-- Write a pid file --> 
    <pidfile>/dev/shmem/dbus_service.pid</pidfile> 

    <!-- Enable logging to syslog --> 
    <syslog/> 

    <!-- allow everyone --> 
    <allow_anonymous /> 

    <!-- Only allow socket-credentials-based authentication --> 
    <!-- <auth>EXTERNAL</auth>--> 

    <!-- Only listen on a local socket. (abstract=/path/to/socket 
     means use abstract namespace, don't really create filesystem 
     file; only Linux supports this. Use path=/whatever on other 
     systems.) --> 
    <listen>unix:path=/dev/shmem/dbus_service_socket</listen> 

    <policy context="default"> 
    <!-- All users can connect to system bus --> 
    <allow user="*"/> 
    <allow group="*"/> 
    <allow own="*"/> 

    <!-- Signals and reply messages (method returns, errors) are allowed 
     by default --> 
    <allow send_type="signal"/> 
    <allow send_type="method_call" log="true"/> 
    <allow send_requested_reply="true" send_type="method_return"/> 
    <allow send_requested_reply="true" send_type="error"/> 

    <!-- All messages may be received by default --> 
    <allow receive_type="method_call"/> 
    <allow receive_type="method_return"/> 
    <allow receive_type="error"/> 
    <allow receive_type="signal"/> 

    <allow receive_interface="org.freedesktop.DBus.Introspectable"/> 
    <allow send_destination="*"/> 
    <allow receive_sender="*" /> 
    <allow receive_path="/org/freedesktop/DBus"/> 

    <!-- Allow anyone to talk to the message bus --> 
    <allow send_destination="org.freedesktop.DBus"/> 
    </policy> 

    <!-- Config files are placed here that among other things, punch 
     holes in the above policy for specific services. --> 
    <includedir>service.d</includedir>  
</busconfig> 

回答