dbus c程序 - 发送(带回复)并使用method_call接收

时间:2015-06-22 09:23:44

标签: dbus

我是D-Bus的新手。我想要一个c程序使用dbus_message_new_method_call函数发送和接收数据。我从链接How to reply a D-Bus message尝试了以下程序,但我在server.c端遇到错误,如#34;任何.service文件都没有提供名称client.signal.Object&#34 ;

"server.c"
/* server.c */

#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void     *usr_data)
{
     DBusMessage *reply;
     dbus_bool_t handled = false;
     char *word = NULL;
     DBusError dberr;
     dbus_error_init(&dberr);
     dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
     printf("receive message: %s\n", word);
     handled = true;

     reply = dbus_message_new_method_return(message);
     char * reply_content;
     printf("\nEnter your Reply Msg : ");
     scanf("%s",reply_content);

     dbus_message_append_args(reply, DBUS_TYPE_STRING, &reply_content, DBUS_TYPE_INVALID);

     dbus_connection_send(connection, reply, NULL);
     dbus_connection_flush(connection);
     dbus_message_unref(reply);

 return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}

int main(int argc, char *argv[])
{
     DBusError dberr;
     DBusConnection *dbconn;

     dbus_error_init(&dberr);
     dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);

         if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL)) 
         {
            return -1;
         }

    dbus_bus_add_match(dbconn, "type='method_call',interface='client.signal.Type'", &dberr);

         while(dbus_connection_read_write_dispatch(dbconn, -1)) 
         {
             /* loop */
         }
 return 0;
}

这里是client.c

#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
      dbus_bool_t handled = false;
      char *word = NULL;
      DBusError dberr;

      dbus_error_init(&dberr);
      dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);

       printf("receive message %s\n", word);
       handled = true;

 return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}


int db_send(DBusConnection *dbconn)
{
     DBusMessage *dbmsg;
     char *word = (char *)malloc(sizeof(char));
     int i;

     dbmsg = dbus_message_new_method_call("client.signal.Object","/client/signal/Object", "client.signal.Type", "Test");

     scanf("%s", word);

         if (!dbus_message_append_args(dbmsg, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID)) 
         {
              return -1;
         }

         if (!dbus_connection_send(dbconn, dbmsg, NULL)) 
         {
              return -1;
         }
     dbus_connection_flush(dbconn);
     printf("send message %s\n", word);

     dbus_message_unref(dbmsg);
 return 0;
}

int main(int argc, char *argv[])
{
     DBusError dberr;
     DBusConnection *dbconn;

     dbus_error_init(&dberr);

     dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
         if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL)) 
         {
             return -1;
         }

     db_send(dbconn);

          while(dbus_connection_read_write_dispatch(dbconn, -1)) 
          {
               db_send(dbconn);
          }
     dbus_connection_unref(dbconn);

 return 0;
}

请帮我解决。

1 个答案:

答案 0 :(得分:0)

看看这里:http://www.freedesktop.org/wiki/IntroductionToDBus/

从它看起来你需要写一个文件&#34; * .service&#34;描述服务。

(引自网站)

# (Lines starting with hash marks are comments)

# Fixed section header (do not change):
[D-BUS Service]
Names=com.bigmoneybank.Deposits;com.bigmoneybank.Withdrawals
Exec=/usr/local/bin/bankcounter

这是我在Googleing的5分钟内找到的。