sl@0: /* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ sl@0: #include "test-utils.h" sl@0: sl@0: static DBusLoop *loop; sl@0: static dbus_bool_t already_quit = FALSE; sl@0: static dbus_bool_t hello_from_self_reply_received = FALSE; sl@0: sl@0: static void sl@0: quit (void) sl@0: { sl@0: if (!already_quit) sl@0: { sl@0: _dbus_loop_quit (loop); sl@0: already_quit = TRUE; sl@0: } sl@0: } sl@0: sl@0: static void sl@0: die (const char *message) sl@0: { sl@0: fprintf (stderr, "*** test-service: %s", message); sl@0: exit (1); sl@0: } sl@0: sl@0: static void sl@0: check_hello_from_self_reply (DBusPendingCall *pcall, sl@0: void *user_data) sl@0: { sl@0: DBusMessage *reply; sl@0: DBusMessage *echo_message, *echo_reply = NULL; sl@0: DBusError error; sl@0: DBusConnection *connection; sl@0: sl@0: int type; sl@0: sl@0: dbus_error_init (&error); sl@0: sl@0: connection = dbus_bus_get (DBUS_BUS_STARTER, &error); sl@0: if (connection == NULL) sl@0: { sl@0: fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", sl@0: error.message); sl@0: dbus_error_free (&error); sl@0: die("no memory"); sl@0: } sl@0: sl@0: sl@0: echo_message = (DBusMessage *)user_data; sl@0: sl@0: reply = dbus_pending_call_steal_reply (pcall); sl@0: sl@0: type = dbus_message_get_type (reply); sl@0: sl@0: if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN) sl@0: { sl@0: const char *s; sl@0: printf ("Reply from HelloFromSelf received\n"); sl@0: sl@0: if (!dbus_message_get_args (echo_message, sl@0: &error, sl@0: DBUS_TYPE_STRING, &s, sl@0: DBUS_TYPE_INVALID)) sl@0: { sl@0: echo_reply = dbus_message_new_error (echo_message, sl@0: error.name, sl@0: error.message); sl@0: sl@0: if (echo_reply == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: } sl@0: else sl@0: { sl@0: echo_reply = dbus_message_new_method_return (echo_message); sl@0: if (echo_reply == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: if (!dbus_message_append_args (echo_reply, sl@0: DBUS_TYPE_STRING, &s, sl@0: DBUS_TYPE_INVALID)) sl@0: die ("No memory"); sl@0: } sl@0: sl@0: if (!dbus_connection_send (connection, echo_reply, NULL)) sl@0: die ("No memory\n"); sl@0: sl@0: dbus_message_unref (echo_reply); sl@0: } sl@0: else if (type == DBUS_MESSAGE_TYPE_ERROR) sl@0: { sl@0: dbus_set_error_from_message (&error, reply); sl@0: printf ("Error type in reply: %s\n", error.message); sl@0: sl@0: if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0) sl@0: { sl@0: echo_reply = dbus_message_new_error (echo_reply, sl@0: error.name, sl@0: error.message); sl@0: sl@0: if (echo_reply == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: if (!dbus_connection_send (connection, echo_reply, NULL)) sl@0: die ("No memory\n"); sl@0: sl@0: dbus_message_unref (echo_reply); sl@0: } sl@0: dbus_error_free (&error); sl@0: } sl@0: else sl@0: _dbus_assert_not_reached ("Unexpected message received\n"); sl@0: sl@0: hello_from_self_reply_received = TRUE; sl@0: sl@0: dbus_message_unref (reply); sl@0: dbus_message_unref (echo_message); sl@0: dbus_pending_call_unref (pcall); sl@0: dbus_connection_unref (connection); sl@0: } sl@0: sl@0: static DBusHandlerResult sl@0: handle_run_hello_from_self (DBusConnection *connection, sl@0: DBusMessage *message) sl@0: { sl@0: DBusError error; sl@0: DBusMessage *reply, *self_message; sl@0: DBusPendingCall *pcall; sl@0: char *s; sl@0: sl@0: _dbus_verbose ("sending reply to Echo method\n"); sl@0: sl@0: dbus_error_init (&error); sl@0: sl@0: if (!dbus_message_get_args (message, sl@0: &error, sl@0: DBUS_TYPE_STRING, &s, sl@0: DBUS_TYPE_INVALID)) sl@0: { sl@0: reply = dbus_message_new_error (message, sl@0: error.name, sl@0: error.message); sl@0: sl@0: if (reply == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: if (!dbus_connection_send (connection, reply, NULL)) sl@0: die ("No memory\n"); sl@0: sl@0: dbus_message_unref (reply); sl@0: sl@0: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; sl@0: } sl@0: printf ("Sending HelloFromSelf\n"); sl@0: sl@0: _dbus_verbose ("*** Sending message to self\n"); sl@0: self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService", sl@0: "/org/freedesktop/TestSuite", sl@0: "org.freedesktop.TestSuite", sl@0: "HelloFromSelf"); sl@0: sl@0: if (self_message == NULL) sl@0: die ("No memory"); sl@0: sl@0: if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1)) sl@0: die("No memory"); sl@0: sl@0: dbus_message_ref (message); sl@0: if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL)) sl@0: die("No memory"); sl@0: sl@0: printf ("Sent HelloFromSelf\n"); sl@0: return DBUS_HANDLER_RESULT_HANDLED; sl@0: } sl@0: sl@0: static DBusHandlerResult sl@0: handle_echo (DBusConnection *connection, sl@0: DBusMessage *message) sl@0: { sl@0: DBusError error; sl@0: DBusMessage *reply; sl@0: char *s; sl@0: sl@0: _dbus_verbose ("sending reply to Echo method\n"); sl@0: sl@0: dbus_error_init (&error); sl@0: sl@0: if (!dbus_message_get_args (message, sl@0: &error, sl@0: DBUS_TYPE_STRING, &s, sl@0: DBUS_TYPE_INVALID)) sl@0: { sl@0: reply = dbus_message_new_error (message, sl@0: error.name, sl@0: error.message); sl@0: sl@0: if (reply == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: if (!dbus_connection_send (connection, reply, NULL)) sl@0: die ("No memory\n"); sl@0: sl@0: dbus_message_unref (reply); sl@0: sl@0: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; sl@0: } sl@0: sl@0: reply = dbus_message_new_method_return (message); sl@0: if (reply == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: if (!dbus_message_append_args (reply, sl@0: DBUS_TYPE_STRING, &s, sl@0: DBUS_TYPE_INVALID)) sl@0: die ("No memory"); sl@0: sl@0: if (!dbus_connection_send (connection, reply, NULL)) sl@0: die ("No memory\n"); sl@0: sl@0: fprintf (stderr, "Echo service echoed string: \"%s\"\n", s); sl@0: sl@0: dbus_message_unref (reply); sl@0: sl@0: return DBUS_HANDLER_RESULT_HANDLED; sl@0: } sl@0: sl@0: static void sl@0: path_unregistered_func (DBusConnection *connection, sl@0: void *user_data) sl@0: { sl@0: /* connection was finalized */ sl@0: } sl@0: sl@0: static DBusHandlerResult sl@0: path_message_func (DBusConnection *connection, sl@0: DBusMessage *message, sl@0: void *user_data) sl@0: { sl@0: if (dbus_message_is_method_call (message, sl@0: "org.freedesktop.TestSuite", sl@0: "Echo")) sl@0: return handle_echo (connection, message); sl@0: else if (dbus_message_is_method_call (message, sl@0: "org.freedesktop.TestSuite", sl@0: "Exit")) sl@0: { sl@0: quit (); sl@0: return DBUS_HANDLER_RESULT_HANDLED; sl@0: } sl@0: else if (dbus_message_is_method_call (message, sl@0: "org.freedesktop.TestSuite", sl@0: "EmitFoo")) sl@0: { sl@0: /* Emit the Foo signal */ sl@0: DBusMessage *signal; sl@0: double v_DOUBLE; sl@0: sl@0: _dbus_verbose ("emitting signal Foo\n"); sl@0: sl@0: signal = dbus_message_new_signal ("/org/freedesktop/TestSuite", sl@0: "org.freedesktop.TestSuite", sl@0: "Foo"); sl@0: if (signal == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: v_DOUBLE = 42.6; sl@0: if (!dbus_message_append_args (signal, sl@0: DBUS_TYPE_DOUBLE, &v_DOUBLE, sl@0: DBUS_TYPE_INVALID)) sl@0: die ("No memory"); sl@0: sl@0: if (!dbus_connection_send (connection, signal, NULL)) sl@0: die ("No memory\n"); sl@0: sl@0: return DBUS_HANDLER_RESULT_HANDLED; sl@0: } sl@0: sl@0: else if (dbus_message_is_method_call (message, sl@0: "org.freedesktop.TestSuite", sl@0: "RunHelloFromSelf")) sl@0: { sl@0: return handle_run_hello_from_self (connection, message); sl@0: } sl@0: else if (dbus_message_is_method_call (message, sl@0: "org.freedesktop.TestSuite", sl@0: "HelloFromSelf")) sl@0: { sl@0: DBusMessage *reply; sl@0: printf ("Received the HelloFromSelf message\n"); sl@0: sl@0: reply = dbus_message_new_method_return (message); sl@0: if (reply == NULL) sl@0: die ("No memory"); sl@0: sl@0: if (!dbus_connection_send (connection, reply, NULL)) sl@0: die ("No memory"); sl@0: sl@0: return DBUS_HANDLER_RESULT_HANDLED; sl@0: } sl@0: else sl@0: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; sl@0: } sl@0: sl@0: static DBusObjectPathVTable sl@0: echo_vtable = { sl@0: path_unregistered_func, sl@0: path_message_func, sl@0: NULL, sl@0: }; sl@0: sl@0: sl@0: static const char* echo_path = "/org/freedesktop/TestSuite" ; sl@0: sl@0: static DBusHandlerResult sl@0: filter_func (DBusConnection *connection, sl@0: DBusMessage *message, sl@0: void *user_data) sl@0: { sl@0: if (dbus_message_is_signal (message, sl@0: DBUS_INTERFACE_LOCAL, sl@0: "Disconnected")) sl@0: { sl@0: quit (); sl@0: return DBUS_HANDLER_RESULT_HANDLED; sl@0: } sl@0: else sl@0: { sl@0: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; sl@0: } sl@0: } sl@0: sl@0: int sl@0: main () sl@0: { sl@0: DBusError error; sl@0: int result,bus_get = 1; sl@0: DBusConnection *connection; sl@0: sl@0: dbus_error_init (&error); sl@0: connection = dbus_bus_get(DBUS_BUS_SESSION, &error); sl@0: if(connection == NULL) sl@0: { sl@0: sl@0: dbus_error_free(&error); sl@0: /*test-service.exe spawned by dbus_daemon_tets_main was returning NULL for dbus_bus_get(), work around for this is to use below API */ sl@0: connection = dbus_connection_open_private ("tcp:host=localhost,port=12436", &error); sl@0: bus_get = 0; sl@0: } sl@0: if (connection == NULL) sl@0: { sl@0: fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", sl@0: error.message); sl@0: dbus_error_free (&error); sl@0: return 1; sl@0: } sl@0: sl@0: if( !bus_get) sl@0: { sl@0: sl@0: if (!dbus_bus_register (connection, &error)) sl@0: { sl@0: sl@0: //_dbus_connection_close_possibly_shared (connection); sl@0: dbus_connection_unref (connection); sl@0: return -1; sl@0: } sl@0: } sl@0: loop = _dbus_loop_new (); sl@0: if (loop == NULL) sl@0: die ("No memory\n"); sl@0: sl@0: if (!test_connection_setup (loop, connection)) sl@0: die ("No memory\n"); sl@0: sl@0: if (!dbus_connection_add_filter (connection, sl@0: filter_func, NULL, NULL)) sl@0: die ("No memory"); sl@0: sl@0: if (!dbus_connection_register_object_path (connection, sl@0: echo_path, sl@0: &echo_vtable, sl@0: (void*) 0xdeadbeef)) sl@0: die ("No memory"); sl@0: sl@0: { sl@0: void *d; sl@0: if (!dbus_connection_get_object_path_data (connection, echo_path, &d)) sl@0: die ("No memory"); sl@0: if (d != (void*) 0xdeadbeef) sl@0: die ("dbus_connection_get_object_path_data() doesn't seem to work right\n"); sl@0: } sl@0: sl@0: result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService", sl@0: 0, &error); sl@0: if (dbus_error_is_set (&error)) sl@0: { sl@0: fprintf (stderr, "Error %s\n", error.message); sl@0: _dbus_verbose ("*** Failed to acquire service: %s\n", sl@0: error.message); sl@0: dbus_error_free (&error); sl@0: exit (1); sl@0: } sl@0: sl@0: _dbus_verbose ("*** Test service entering main loop\n"); sl@0: _dbus_loop_run (loop); sl@0: sl@0: test_connection_shutdown (loop, connection); sl@0: sl@0: dbus_connection_remove_filter (connection, filter_func, NULL); sl@0: sl@0: if( !bus_get) sl@0: dbus_connection_close(connection); sl@0: sl@0: dbus_connection_unref (connection); sl@0: sl@0: _dbus_loop_unref (loop); sl@0: loop = NULL; sl@0: sl@0: dbus_free(loop); sl@0: dbus_shutdown (); sl@0: sl@0: _dbus_verbose ("*** Test service exiting\n"); sl@0: sl@0: return 0; sl@0: }