1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-service.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,424 @@
1.4 +/* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
1.5 +#include "test-utils.h"
1.6 +
1.7 +static DBusLoop *loop;
1.8 +static dbus_bool_t already_quit = FALSE;
1.9 +static dbus_bool_t hello_from_self_reply_received = FALSE;
1.10 +
1.11 +static void
1.12 +quit (void)
1.13 +{
1.14 + if (!already_quit)
1.15 + {
1.16 + _dbus_loop_quit (loop);
1.17 + already_quit = TRUE;
1.18 + }
1.19 +}
1.20 +
1.21 +static void
1.22 +die (const char *message)
1.23 +{
1.24 + fprintf (stderr, "*** test-service: %s", message);
1.25 + exit (1);
1.26 +}
1.27 +
1.28 +static void
1.29 +check_hello_from_self_reply (DBusPendingCall *pcall,
1.30 + void *user_data)
1.31 +{
1.32 + DBusMessage *reply;
1.33 + DBusMessage *echo_message, *echo_reply = NULL;
1.34 + DBusError error;
1.35 + DBusConnection *connection;
1.36 +
1.37 + int type;
1.38 +
1.39 + dbus_error_init (&error);
1.40 +
1.41 + connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
1.42 + if (connection == NULL)
1.43 + {
1.44 + fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
1.45 + error.message);
1.46 + dbus_error_free (&error);
1.47 + die("no memory");
1.48 + }
1.49 +
1.50 +
1.51 + echo_message = (DBusMessage *)user_data;
1.52 +
1.53 + reply = dbus_pending_call_steal_reply (pcall);
1.54 +
1.55 + type = dbus_message_get_type (reply);
1.56 +
1.57 + if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1.58 + {
1.59 + const char *s;
1.60 + printf ("Reply from HelloFromSelf received\n");
1.61 +
1.62 + if (!dbus_message_get_args (echo_message,
1.63 + &error,
1.64 + DBUS_TYPE_STRING, &s,
1.65 + DBUS_TYPE_INVALID))
1.66 + {
1.67 + echo_reply = dbus_message_new_error (echo_message,
1.68 + error.name,
1.69 + error.message);
1.70 +
1.71 + if (echo_reply == NULL)
1.72 + die ("No memory\n");
1.73 +
1.74 + }
1.75 + else
1.76 + {
1.77 + echo_reply = dbus_message_new_method_return (echo_message);
1.78 + if (echo_reply == NULL)
1.79 + die ("No memory\n");
1.80 +
1.81 + if (!dbus_message_append_args (echo_reply,
1.82 + DBUS_TYPE_STRING, &s,
1.83 + DBUS_TYPE_INVALID))
1.84 + die ("No memory");
1.85 + }
1.86 +
1.87 + if (!dbus_connection_send (connection, echo_reply, NULL))
1.88 + die ("No memory\n");
1.89 +
1.90 + dbus_message_unref (echo_reply);
1.91 + }
1.92 + else if (type == DBUS_MESSAGE_TYPE_ERROR)
1.93 + {
1.94 + dbus_set_error_from_message (&error, reply);
1.95 + printf ("Error type in reply: %s\n", error.message);
1.96 +
1.97 + if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
1.98 + {
1.99 + echo_reply = dbus_message_new_error (echo_reply,
1.100 + error.name,
1.101 + error.message);
1.102 +
1.103 + if (echo_reply == NULL)
1.104 + die ("No memory\n");
1.105 +
1.106 + if (!dbus_connection_send (connection, echo_reply, NULL))
1.107 + die ("No memory\n");
1.108 +
1.109 + dbus_message_unref (echo_reply);
1.110 + }
1.111 + dbus_error_free (&error);
1.112 + }
1.113 + else
1.114 + _dbus_assert_not_reached ("Unexpected message received\n");
1.115 +
1.116 + hello_from_self_reply_received = TRUE;
1.117 +
1.118 + dbus_message_unref (reply);
1.119 + dbus_message_unref (echo_message);
1.120 + dbus_pending_call_unref (pcall);
1.121 + dbus_connection_unref (connection);
1.122 +}
1.123 +
1.124 +static DBusHandlerResult
1.125 +handle_run_hello_from_self (DBusConnection *connection,
1.126 + DBusMessage *message)
1.127 +{
1.128 + DBusError error;
1.129 + DBusMessage *reply, *self_message;
1.130 + DBusPendingCall *pcall;
1.131 + char *s;
1.132 +
1.133 + _dbus_verbose ("sending reply to Echo method\n");
1.134 +
1.135 + dbus_error_init (&error);
1.136 +
1.137 + if (!dbus_message_get_args (message,
1.138 + &error,
1.139 + DBUS_TYPE_STRING, &s,
1.140 + DBUS_TYPE_INVALID))
1.141 + {
1.142 + reply = dbus_message_new_error (message,
1.143 + error.name,
1.144 + error.message);
1.145 +
1.146 + if (reply == NULL)
1.147 + die ("No memory\n");
1.148 +
1.149 + if (!dbus_connection_send (connection, reply, NULL))
1.150 + die ("No memory\n");
1.151 +
1.152 + dbus_message_unref (reply);
1.153 +
1.154 + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1.155 + }
1.156 + printf ("Sending HelloFromSelf\n");
1.157 +
1.158 + _dbus_verbose ("*** Sending message to self\n");
1.159 + self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
1.160 + "/org/freedesktop/TestSuite",
1.161 + "org.freedesktop.TestSuite",
1.162 + "HelloFromSelf");
1.163 +
1.164 + if (self_message == NULL)
1.165 + die ("No memory");
1.166 +
1.167 + if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
1.168 + die("No memory");
1.169 +
1.170 + dbus_message_ref (message);
1.171 + if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
1.172 + die("No memory");
1.173 +
1.174 + printf ("Sent HelloFromSelf\n");
1.175 + return DBUS_HANDLER_RESULT_HANDLED;
1.176 +}
1.177 +
1.178 +static DBusHandlerResult
1.179 +handle_echo (DBusConnection *connection,
1.180 + DBusMessage *message)
1.181 +{
1.182 + DBusError error;
1.183 + DBusMessage *reply;
1.184 + char *s;
1.185 +
1.186 + _dbus_verbose ("sending reply to Echo method\n");
1.187 +
1.188 + dbus_error_init (&error);
1.189 +
1.190 + if (!dbus_message_get_args (message,
1.191 + &error,
1.192 + DBUS_TYPE_STRING, &s,
1.193 + DBUS_TYPE_INVALID))
1.194 + {
1.195 + reply = dbus_message_new_error (message,
1.196 + error.name,
1.197 + error.message);
1.198 +
1.199 + if (reply == NULL)
1.200 + die ("No memory\n");
1.201 +
1.202 + if (!dbus_connection_send (connection, reply, NULL))
1.203 + die ("No memory\n");
1.204 +
1.205 + dbus_message_unref (reply);
1.206 +
1.207 + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1.208 + }
1.209 +
1.210 + reply = dbus_message_new_method_return (message);
1.211 + if (reply == NULL)
1.212 + die ("No memory\n");
1.213 +
1.214 + if (!dbus_message_append_args (reply,
1.215 + DBUS_TYPE_STRING, &s,
1.216 + DBUS_TYPE_INVALID))
1.217 + die ("No memory");
1.218 +
1.219 + if (!dbus_connection_send (connection, reply, NULL))
1.220 + die ("No memory\n");
1.221 +
1.222 + fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
1.223 +
1.224 + dbus_message_unref (reply);
1.225 +
1.226 + return DBUS_HANDLER_RESULT_HANDLED;
1.227 +}
1.228 +
1.229 +static void
1.230 +path_unregistered_func (DBusConnection *connection,
1.231 + void *user_data)
1.232 +{
1.233 + /* connection was finalized */
1.234 +}
1.235 +
1.236 +static DBusHandlerResult
1.237 +path_message_func (DBusConnection *connection,
1.238 + DBusMessage *message,
1.239 + void *user_data)
1.240 +{
1.241 + if (dbus_message_is_method_call (message,
1.242 + "org.freedesktop.TestSuite",
1.243 + "Echo"))
1.244 + return handle_echo (connection, message);
1.245 + else if (dbus_message_is_method_call (message,
1.246 + "org.freedesktop.TestSuite",
1.247 + "Exit"))
1.248 + {
1.249 + quit ();
1.250 + return DBUS_HANDLER_RESULT_HANDLED;
1.251 + }
1.252 + else if (dbus_message_is_method_call (message,
1.253 + "org.freedesktop.TestSuite",
1.254 + "EmitFoo"))
1.255 + {
1.256 + /* Emit the Foo signal */
1.257 + DBusMessage *signal;
1.258 + double v_DOUBLE;
1.259 +
1.260 + _dbus_verbose ("emitting signal Foo\n");
1.261 +
1.262 + signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
1.263 + "org.freedesktop.TestSuite",
1.264 + "Foo");
1.265 + if (signal == NULL)
1.266 + die ("No memory\n");
1.267 +
1.268 + v_DOUBLE = 42.6;
1.269 + if (!dbus_message_append_args (signal,
1.270 + DBUS_TYPE_DOUBLE, &v_DOUBLE,
1.271 + DBUS_TYPE_INVALID))
1.272 + die ("No memory");
1.273 +
1.274 + if (!dbus_connection_send (connection, signal, NULL))
1.275 + die ("No memory\n");
1.276 +
1.277 + return DBUS_HANDLER_RESULT_HANDLED;
1.278 + }
1.279 +
1.280 + else if (dbus_message_is_method_call (message,
1.281 + "org.freedesktop.TestSuite",
1.282 + "RunHelloFromSelf"))
1.283 + {
1.284 + return handle_run_hello_from_self (connection, message);
1.285 + }
1.286 + else if (dbus_message_is_method_call (message,
1.287 + "org.freedesktop.TestSuite",
1.288 + "HelloFromSelf"))
1.289 + {
1.290 + DBusMessage *reply;
1.291 + printf ("Received the HelloFromSelf message\n");
1.292 +
1.293 + reply = dbus_message_new_method_return (message);
1.294 + if (reply == NULL)
1.295 + die ("No memory");
1.296 +
1.297 + if (!dbus_connection_send (connection, reply, NULL))
1.298 + die ("No memory");
1.299 +
1.300 + return DBUS_HANDLER_RESULT_HANDLED;
1.301 + }
1.302 + else
1.303 + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1.304 +}
1.305 +
1.306 +static DBusObjectPathVTable
1.307 +echo_vtable = {
1.308 + path_unregistered_func,
1.309 + path_message_func,
1.310 + NULL,
1.311 +};
1.312 +
1.313 +
1.314 +static const char* echo_path = "/org/freedesktop/TestSuite" ;
1.315 +
1.316 +static DBusHandlerResult
1.317 +filter_func (DBusConnection *connection,
1.318 + DBusMessage *message,
1.319 + void *user_data)
1.320 +{
1.321 + if (dbus_message_is_signal (message,
1.322 + DBUS_INTERFACE_LOCAL,
1.323 + "Disconnected"))
1.324 + {
1.325 + quit ();
1.326 + return DBUS_HANDLER_RESULT_HANDLED;
1.327 + }
1.328 + else
1.329 + {
1.330 + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1.331 + }
1.332 +}
1.333 +
1.334 +int
1.335 +main ()
1.336 +{
1.337 + DBusError error;
1.338 + int result,bus_get = 1;
1.339 + DBusConnection *connection;
1.340 +
1.341 + dbus_error_init (&error);
1.342 + connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
1.343 + if(connection == NULL)
1.344 + {
1.345 +
1.346 + dbus_error_free(&error);
1.347 + /*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 */
1.348 + connection = dbus_connection_open_private ("tcp:host=localhost,port=12436", &error);
1.349 + bus_get = 0;
1.350 + }
1.351 + if (connection == NULL)
1.352 + {
1.353 + fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
1.354 + error.message);
1.355 + dbus_error_free (&error);
1.356 + return 1;
1.357 + }
1.358 +
1.359 +if( !bus_get)
1.360 +{
1.361 +
1.362 + if (!dbus_bus_register (connection, &error))
1.363 + {
1.364 +
1.365 + //_dbus_connection_close_possibly_shared (connection);
1.366 + dbus_connection_unref (connection);
1.367 + return -1;
1.368 + }
1.369 +}
1.370 + loop = _dbus_loop_new ();
1.371 + if (loop == NULL)
1.372 + die ("No memory\n");
1.373 +
1.374 + if (!test_connection_setup (loop, connection))
1.375 + die ("No memory\n");
1.376 +
1.377 + if (!dbus_connection_add_filter (connection,
1.378 + filter_func, NULL, NULL))
1.379 + die ("No memory");
1.380 +
1.381 + if (!dbus_connection_register_object_path (connection,
1.382 + echo_path,
1.383 + &echo_vtable,
1.384 + (void*) 0xdeadbeef))
1.385 + die ("No memory");
1.386 +
1.387 + {
1.388 + void *d;
1.389 + if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
1.390 + die ("No memory");
1.391 + if (d != (void*) 0xdeadbeef)
1.392 + die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
1.393 + }
1.394 +
1.395 + result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
1.396 + 0, &error);
1.397 + if (dbus_error_is_set (&error))
1.398 + {
1.399 + fprintf (stderr, "Error %s\n", error.message);
1.400 + _dbus_verbose ("*** Failed to acquire service: %s\n",
1.401 + error.message);
1.402 + dbus_error_free (&error);
1.403 + exit (1);
1.404 + }
1.405 +
1.406 + _dbus_verbose ("*** Test service entering main loop\n");
1.407 + _dbus_loop_run (loop);
1.408 +
1.409 + test_connection_shutdown (loop, connection);
1.410 +
1.411 + dbus_connection_remove_filter (connection, filter_func, NULL);
1.412 +
1.413 + if( !bus_get)
1.414 + dbus_connection_close(connection);
1.415 +
1.416 + dbus_connection_unref (connection);
1.417 +
1.418 + _dbus_loop_unref (loop);
1.419 + loop = NULL;
1.420 +
1.421 + dbus_free(loop);
1.422 + dbus_shutdown ();
1.423 +
1.424 + _dbus_verbose ("*** Test service exiting\n");
1.425 +
1.426 + return 0;
1.427 +}