2013-06-26 29 views
4

我遇到的问题是特别使用低级API在C中打印出dbus方法调用的响应。我是C的libdbus的新手,但在python-dbus上做了一些工作。打印出Dbus方法调用的响应C

  • 我知道该怎么写DBUS方法和方法调用的Python以及在CLI
  • 我可以在互联网上找到的代码来调用dbus的方法,但它们不返回或打印出来的响应
  • 我一直在看libdbus doxygen api,但无法确定如何提取响应。

我有我的代码设置方式,python dbus守护进程运行我想调用的方法。其中一些返回一个字符串。我想要一个C程序连接到会话总线,调用方法,打印出答复并退出。

这是我目前:

#include <stdio.h> 
#include <dbus/dbus.h> 

static void send_dbus_message (DBusConnection *connection, const char *msg) 
{ 
DBusMessage *message; 
//initialize the message 
message = dbus_message_new_signal ("/org/example/foo/bar", 
            "org.example.foo.bar", 
            msg); 

//send the message 
dbus_connection_send (connection, message, NULL); 
//deallocate the message 
dbus_message_unref (message); 
} 

int main (int argc, char **argv) 
{ 
DBusConnection *connection; 
DBusError error; 

//init error message 
dbus_error_init (&error); 
connection = dbus_bus_get (DBUS_BUS_SESSION, &error); 
if (!connection) 
{ 
    printf ("Connection to D-BUS daemon failed: %s", error.message); 

    //deallocate error message 
    dbus_error_free (&error); 
    return 1; 
} 

send_dbus_message (connection, "HelloWorld"); 
return 0; 
} 

可以同步或异步的。

+0

你看着这些[从实例教程](http://dbus.freedesktop.org/doc/dbus-tutorial.html#glib-more-examples)? – user4815162342

+0

感谢您的回复,但该示例基于Glib,我试图避免,因为我正在写一个没有X功能的系统。 – NuclearPeon

+0

我看了下面的网站:http://www.matthew.ath.cx/misc/dbus ......它确实有一些有用的代码,但它仍然没有打印出答复后运行其“调用方法“部分。代码本身没有param变量,所以如果你复制/粘贴到c文件并编译它,它就会失败。即使有变量,它也不会打印出任何东西。 – NuclearPeon

回答

6

您可以使用http://www.matthew.ath.cx/misc/dbus中提到的方法获取方法回复消息。

一旦你有一个dbus消息,你可以使用下面的方法来提取数据。 要解析dbus消息,您需要参数迭代器。启动它以读取传入消息的内容。

DBusMessageIter MsgIter; 
dbus_message_iter_init(msg, &MsgIter);//msg is pointer to dbus message received 

在读取消息之前,您必须验证传入消息的签名。或者你也可以通过参数验证去争论。例如,如果参数类型为字符串

if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&MsgIter)){ 
    char* str = NULL; 
    dbus_message_iter_get_basic(&MsgIter, &str);//this function is used to read basic dbus types like int, string etc. 
} 

对于复杂的类型,如结构,数组,变体和字典条目,则必须创建对应的子迭代器来分析每个复元素的内容。说,对于秒的DBUS签名(I {} II我)U,提取是如下

//Parsing a signature s(i{ii}i)u 
DBusMessageIter rootIter; 
dbus_message_iter_init(msg, &rootIter); 

if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&rootIter)) 
{ 
    char* str = NULL; 
    dbus_message_iter_get_basic(&rootIter, &str);//this function is used to read basic dbus types like int, string etc. 
    dbus_message_iter_next(&rootIter);//Go to next argument of root iter 

    //Block to enter and read structure 
    if (DBUS_TYPE_STRUCT == dbus_message_iter_get_arg_type(&rootIter)) 
    { 
    DBusMessageIter structIter; 
    dbus_message_iter_recurse(&rootIter, &structIter);//Initialize iterator for struct 

    //Argument 1 is int32 
    if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&structIter)) 
    { 
     int a; 
      dbus_message_iter_get_basic(&structIter, &a);//Read integer 
     dbus_message_iter_next(&structIter);//Go to next argument of structiter 

     if (DDBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&structIter)) 
     { 
      DBusMessageIter dictIter;   
       dbus_message_iter_recurse(&structIter, &dictIter);//Initialize iterator for dictentry 
      if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&dictIter)) 
      { 
      dbus_message_iter_get_basic(&dictIter, &a);//Read integer 
      dbus_message_iter_next(&dictIter);//Go to next argument of dictentry 
      if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&dictIter)) 
      { 
       dbus_message_iter_get_basic(&dictIter, &a);//Read integer 
      } 
      } 
     } 
     dbus_message_iter_next(&structIter);//Go to next argument of structiter 
      if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&structIter)) 
     { 
      dbus_message_iter_get_basic(&structIter, &a);//Read integer 
     } 
     } 
    } 
    dbus_message_iter_next(&rootIter);//Go to next argument of root iterator 
    if (DBUS_TYPE_UINT32 == dbus_message_iter_get_arg_type(&rootIter)) 
    { 
    uint32_t b; 
    dbus_message_iter_get_basic(&rootIter, &b);//Read integer 
    } 
} 

完成。在上面的代码中,我使用的参数签名检查参数。相反,您可以使用 dbus_message_iter_get_signature进行一次性验证。有关更多信息,请参阅libdbus api。

从您的回复我明白,你有问题,建立连接, 这里是一个完整的例子,其中一个方法调用服务器上调用,并将结果打印 如果第一个参数是一个字符串。

#include <stdio.h> 
#include <stdlib.h> 
#include <dbus/dbus.h> 
#include <assert.h> 

DBusConnection* conn = NULL; 

//Helper function to setup connection 
void vsetupconnection(); 

//Send method call, Returns NULL on failure, else pointer to reply 
DBusMessage* sendMethodCall(const char* objectpath, \ 
     const char* busname, \ 
     const char* interfacename, \ 
     const char* methodname); 

#define TEST_BUS_NAME    "org.freedesktop.DBus" 
#define TEST_OBJ_PATH    "/org/freedesktop/DBus" 
#define TEST_INTERFACE_NAME   "org.freedesktop.DBus.Introspectable" 
#define TEST_METHOD_NAME   "Introspect" 

int main (int argc, char **argv) 
{ 
    vsetupconnection(); 

    DBusMessage* reply = sendMethodCall(TEST_OBJ_PATH, TEST_BUS_NAME, TEST_INTERFACE_NAME, TEST_METHOD_NAME); 
    if(reply != NULL) { 

     DBusMessageIter MsgIter; 
     dbus_message_iter_init(reply, &MsgIter);//msg is pointer to dbus message received 

     if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&MsgIter)){ 
      char* str = NULL; 
      dbus_message_iter_get_basic(&MsgIter, &str); 
      printf("Received string: \n %s \n",str); 
     } 

     dbus_message_unref(reply);//unref reply 
    } 
    dbus_connection_close(conn); 
    return 0; 
} 

void vsetupconnection() 
{ 
    DBusError err; 
    // initialise the errors 
    dbus_error_init(&err); 
    // connect to session bus 
    conn = dbus_bus_get(DBUS_BUS_SESSION, &err); 
    if (dbus_error_is_set(&err)) { 
     printf("Connection Error (%s)\n", err.message); 
     dbus_error_free(&err); 
    } 
    if (NULL == conn) { 
     exit(1); 
    } 
    else { 
     printf("Connected to session bus\n"); 
    } 
} 

DBusMessage* sendMethodCall(const char* objectpath, const char* busname, const char* interfacename, const char* methodname) 
{ 
    assert(objectpath != NULL); assert(busname != NULL); assert(interfacename != NULL); 
    assert(methodname != NULL); assert(conn != NULL); 

    DBusMessage* methodcall = dbus_message_new_method_call(busname,objectpath, interfacename, methodname); 

    if (methodcall == NULL) { 
     printf("Cannot allocate DBus message!\n"); 
    } 
    //Now do a sync call 
    DBusPendingCall* pending; 
    DBusMessage* reply; 

    if (!dbus_connection_send_with_reply(conn, methodcall, &pending, -1))//Send and expect reply using pending call object 
    { 
     printf("failed to send message!\n"); 
    } 
    dbus_connection_flush(conn); 
    dbus_message_unref(methodcall); 
    methodcall = NULL; 

    dbus_pending_call_block(pending);//Now block on the pending call 
    reply = dbus_pending_call_steal_reply(pending);//Get the reply message from the queue 
    dbus_pending_call_unref(pending);//Free pending call handle 
    assert(reply != NULL); 

    if(dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) { 
     printf("Error : %s",dbus_message_get_error_name(reply)); 
      dbus_message_unref(reply); 
      reply = NULL; 
    } 

    return reply; 
} 
+0

感谢您的帖子,但我不认为它触及我的问题。首先,你引用了我在我的评论中提到的同一个链接@ user4815162342,我一直没有成功。 打印出答复是我所追求的。 我会经历并测试您在写作时所写的内容,但在回复中没有看到任何打印消息,因此我不认为它正在回答我的问题。 你也正在初始化'char str',但它没有在你的代码中被引用。 – NuclearPeon

+0

我认为你面临着从DBusMessage中提取参数的问题。用测试代码更新答案以供参考。 –