#define DBUS_API_SUBJECT_TO_CHANGE #include #include #include const char* service = "vandenoever.strigi"; const char* interface = "vandenoever.strigi"; const char* object = "search"; void reply_to_method_call(DBusMessage* msg, DBusConnection* conn) { DBusMessage* reply; DBusMessageIter args; bool stat = true; dbus_uint32_t level = 21614; dbus_uint32_t serial = 0; char* param = ""; // read the arguments if (!dbus_message_iter_init(msg, &args)) fprintf(stderr, "Message has no arguments!\n"); else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) fprintf(stderr, "Argument is not string!\n"); else dbus_message_iter_get_basic(&args, ¶m); printf("Method called with %s\n", param); // create a reply from the message reply = dbus_message_new_method_return(msg); // add the arguments to the reply dbus_message_iter_init_append(reply, &args); if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &stat)) { fprintf(stderr, "Out Of Memory!\n"); exit(1); } if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &level)) { fprintf(stderr, "Out Of Memory!\n"); exit(1); } // send the reply && flush the connection if (!dbus_connection_send(conn, reply, &serial)) { fprintf(stderr, "Out Of Memory!\n"); exit(1); } dbus_connection_flush(conn); // free the reply dbus_message_unref(reply); } /** * Server that exposes a method call and waits for it to be called */ void listen() { DBusMessage* msg; DBusMessage* reply; DBusMessageIter args; DBusConnection* conn; DBusError err; int ret; char* param; printf("Listening for method calls\n"); // initialise the error dbus_error_init(&err); // connect to the 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) { fprintf(stderr, "Connection Null\n"); exit(1); } // request our name on the bus and check for errors ret = dbus_bus_request_name(conn, service, 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) { fprintf(stderr, "Not Primary Owner (%d)\n", ret); exit(1); } // register the introspection interface // TODO // loop, testing for new messages while (true) { // non blocking read of the next available message dbus_connection_read_write(conn, 1); msg = dbus_connection_pop_message(conn); // loop again if we haven't got a message if (NULL == msg) { //sleep(1); continue; } printf("msg interface: %s\n", dbus_message_get_interface(msg)); printf("msg path: %s\n", dbus_message_get_path(msg)); printf("msg member: %s\n", dbus_message_get_member(msg)); // check this is a method call for the right interface & method if (dbus_message_is_method_call(msg, "test.method.Type", "Method")) reply_to_method_call(msg, conn); dbus_connection_flush(conn); // free the message dbus_message_unref(msg); } // close the connection dbus_connection_close(conn); } int main(int argc, char** argv) { listen(); return 0; }