View Single Post
hopbeat's Avatar
Posts: 516 | Thanked: 643 times | Joined on Oct 2009 @ Denmark/Poland
#1
Hello,

I'm a noob in D-bus handling, so please, be patient.

I'm trying to do as simple thing, namely to write a C program that would invoke a remote method and would handle the response.
I would like to implement this call (provided by @qwerty12):

Code:
Nokia-N900-42-11:~# dbus-send --system --print-reply --type=method_call --dest='com.nokia.phone.net' /com/nokia/phone/net Phone.Net.get_registration_status
method return sender=:1.17 -> dest=:1.539 reply_serial=2
   byte 0
   uint16 1002
   uint32 799685
   uint32 20
   uint32 238
   byte 0
   byte 3
   int32 0
When trying to use

Code:
msg = dbus_message_new_method_call("com.nokia.phone.net", 
                                      "/com/nokia/phone/net",
                                      "Phone.Net", 
                                      "get_registration_status");
I don't get any result (or I cannot handle it). Can someone point me in the right direction? It should be straightforward, but I cannot figure this out.

Thanks!

[edit] This is the function I'm using:

Code:
void query(char* param)
{
   DBusMessage* msg;
   DBusMessageIter args;
   DBusConnection* conn;
   DBusError err;
   DBusPendingCall* pending;
   int ret;
   char stat;
   dbus_uint16_t level;

   printf("Calling remote method with %s\n", param);

   // initialiset the errors
   dbus_error_init(&err);

   // connect to the system bus and check for errors
   conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
   if (dbus_error_is_set(&err)) {
      fprintf(stderr, "Connection Error (%s)\n", err.message);
      dbus_error_free(&err);
   }
   if (NULL == conn) {
      exit(1);
   }

   // request our name on the bus
   ret = dbus_bus_request_name(conn, "test.method.caller", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
   if (dbus_error_is_set(&err)) {
      fprintf(stderr, "Name Error (%s)\n", err.message);
      dbus_error_free(&err);
   }
   if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
      exit(1);
   }



   msg = dbus_message_new_method_call("com.nokia.phone.net", // target for the method call
                                      "/com/nokia/phone/net", // object to call on
                                      "Phone.Net", // interface to call on
                                      "get_registration_status"); // method name
   if (NULL == msg) {
      fprintf(stderr, "Message Null\n");
      exit(1);
// send message and get a handle for a reply
   if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
      fprintf(stderr, "Out Of Memory!\n");
      exit(1);
   }
   if (NULL == pending) {
      fprintf(stderr, "Pending Call Null\n");
      exit(1);
   }
   dbus_connection_flush(conn);

   printf("Request Sent\n");

   // free message
   dbus_message_unref(msg);

   // block until we recieve a reply
   dbus_pending_call_block(pending);

   // get the reply message
   msg = dbus_pending_call_steal_reply(pending);
   if (NULL == msg) {
      fprintf(stderr, "Reply Null\n");
      exit(1);
   }
   // free the pending message handle
   dbus_pending_call_unref(pending);

   // read the parameters
   if (!dbus_message_iter_init(msg, &args))
      fprintf(stderr, "Message has no arguments!\n");
 //  else if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&args))
   //   fprintf(stderr, "Argument is not boolean!\n");
   else
      dbus_message_iter_get_basic(&args, &stat);

   if (!dbus_message_iter_next(&args))
      fprintf(stderr, "Message has too few arguments!\n");
   else if (DBUS_TYPE_UINT16 != dbus_message_iter_get_arg_type(&args))
      fprintf(stderr, "Argument is not int!\n");
   else
      dbus_message_iter_get_basic(&args, &level);





   printf("Got Reply: %d, %d\n", stat, level);

   // free reply
   dbus_message_unref(msg);
}
The reply I get:
Code:
Nokia-N900-42-11:/home/user# ./dbus.out query
Calling remote method with no param
Request Sent
Message has too few arguments!
Got Reply: 12, 0
[edit2] I have changed the bus to the system one, but it doesn't solve anything...
__________________
Hi! I'm a Maemo Greeter!
Witaj na talk.maemo.org!

Useful links for newcomers:
Użyteczne linki:
Nowi użyktownicy mówią cześć | New members say hello , Tu zaczynają nowi użytkownicy | New users start here, Podforum społeczności | Community subforum, Wiki dla początkujących | Beginners' wiki page, Maemo5 101, Często zadawane pytania | Frequently Asked Questions (FAQ), Google

Jeżeli mogę w czymś pomóc, pytaj!
If I can help with anything else, just ask!

Bored? Follow me

Last edited by hopbeat; 2009-11-21 at 21:23.