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 const char* echo_path = "/org/freedesktop/TestSuite"; sl@0: sl@0: typedef struct sl@0: { sl@0: int argc; sl@0: char **argv; sl@0: } EchoData; 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 DBusHandlerResult sl@0: handle_echo (DBusConnection *connection, sl@0: DBusMessage *message) sl@0: { sl@0: DBusError error; sl@0: DBusMessage *reply; sl@0: DBusMessageIter iter; sl@0: int i; sl@0: EchoData *d; sl@0: sl@0: _dbus_verbose ("sending reply to Echo method\n"); sl@0: sl@0: if (!dbus_connection_get_object_path_data (connection, echo_path, (void **)&d)) sl@0: die ("No memory"); sl@0: sl@0: sl@0: dbus_error_init (&error); 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: dbus_message_iter_init_append (reply, &iter); sl@0: for (i = 0; i < d->argc; ++i) sl@0: if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i]))) 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: fprintf (stderr, "Shell echo service echoed the command line\n"); 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 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: 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 (int argc, sl@0: char **argv) sl@0: { sl@0: DBusConnection *connection; sl@0: DBusError error; sl@0: EchoData echo_data; sl@0: int result; sl@0: sl@0: echo_data.argc = argc; sl@0: echo_data.argv = argv; sl@0: sl@0: dbus_error_init (&error); 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: 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*) &echo_data)) 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*) &echo_data) 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.TestSuiteShellEchoServiceSuccess", 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: dbus_connection_unref (connection); sl@0: sl@0: _dbus_loop_unref (loop); sl@0: loop = NULL; sl@0: sl@0: dbus_shutdown (); sl@0: sl@0: _dbus_verbose ("*** Test service exiting\n"); sl@0: sl@0: return 0; sl@0: }