dbus c program - send(with reply) and receive using method_call

1.8k views Asked by At

I'm new to D-Bus. I want a c program to send and receive data using the dbus_message_new_method_call function. I have tried the following programs from the link How to reply a D-Bus message but I'm getting error in the server.c side like "the name client.signal.Object was not provided by any .service files"

"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;
}

here 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;
}

Please help me to fix.

1

There are 1 answers

0
Dakkaron On BEST ANSWER

Have a look here: http://www.freedesktop.org/wiki/IntroductionToDBus/

From what it looks like you need to write a file "*.service" that describes the service.

(Quoted from the website)

# (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

This is what I found within 5 minutes of Googleing.