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
msg = dbus_message_new_method_call("com.nokia.phone.net", "/com/nokia/phone/net", "Phone.Net", "get_registration_status");
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); }
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