|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "test-utils.h" |
|
19 |
|
20 static DBusLoop *loop; |
|
21 static dbus_bool_t already_quit = FALSE; |
|
22 static dbus_bool_t hello_from_self_reply_received = FALSE; |
|
23 |
|
24 static void |
|
25 quit (void) |
|
26 { |
|
27 if (!already_quit) |
|
28 { |
|
29 _dbus_loop_quit (loop); |
|
30 already_quit = TRUE; |
|
31 } |
|
32 } |
|
33 |
|
34 static void |
|
35 die (const char *message) |
|
36 { |
|
37 fprintf (stderr, "*** test-service: %s", message); |
|
38 exit (1); |
|
39 } |
|
40 |
|
41 static void |
|
42 check_hello_from_self_reply (DBusPendingCall *pcall, |
|
43 void *user_data) |
|
44 { |
|
45 DBusMessage *reply; |
|
46 DBusMessage *echo_message, *echo_reply = NULL; |
|
47 DBusError error; |
|
48 DBusConnection *connection; |
|
49 |
|
50 int type; |
|
51 |
|
52 dbus_error_init (&error); |
|
53 |
|
54 connection = dbus_bus_get (DBUS_BUS_STARTER, &error); |
|
55 if (connection == NULL) |
|
56 { |
|
57 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", |
|
58 error.message); |
|
59 dbus_error_free (&error); |
|
60 die("no memory"); |
|
61 } |
|
62 |
|
63 |
|
64 echo_message = (DBusMessage *)user_data; |
|
65 |
|
66 reply = dbus_pending_call_steal_reply (pcall); |
|
67 |
|
68 type = dbus_message_get_type (reply); |
|
69 |
|
70 if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN) |
|
71 { |
|
72 const char *s; |
|
73 printf ("Reply from HelloFromSelf received\n"); |
|
74 |
|
75 if (!dbus_message_get_args (echo_message, |
|
76 &error, |
|
77 DBUS_TYPE_STRING, &s, |
|
78 DBUS_TYPE_INVALID)) |
|
79 { |
|
80 echo_reply = dbus_message_new_error (echo_message, |
|
81 error.name, |
|
82 error.message); |
|
83 |
|
84 if (echo_reply == NULL) |
|
85 die ("No memory\n"); |
|
86 |
|
87 } |
|
88 else |
|
89 { |
|
90 echo_reply = dbus_message_new_method_return (echo_message); |
|
91 if (echo_reply == NULL) |
|
92 die ("No memory\n"); |
|
93 |
|
94 if (!dbus_message_append_args (echo_reply, |
|
95 DBUS_TYPE_STRING, &s, |
|
96 DBUS_TYPE_INVALID)) |
|
97 die ("No memory"); |
|
98 } |
|
99 |
|
100 if (!dbus_connection_send (connection, echo_reply, NULL)) |
|
101 die ("No memory\n"); |
|
102 |
|
103 dbus_message_unref (echo_reply); |
|
104 } |
|
105 else if (type == DBUS_MESSAGE_TYPE_ERROR) |
|
106 { |
|
107 dbus_set_error_from_message (&error, reply); |
|
108 printf ("Error type in reply: %s\n", error.message); |
|
109 |
|
110 if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0) |
|
111 { |
|
112 echo_reply = dbus_message_new_error (echo_reply, |
|
113 error.name, |
|
114 error.message); |
|
115 |
|
116 if (echo_reply == NULL) |
|
117 die ("No memory\n"); |
|
118 |
|
119 if (!dbus_connection_send (connection, echo_reply, NULL)) |
|
120 die ("No memory\n"); |
|
121 |
|
122 dbus_message_unref (echo_reply); |
|
123 } |
|
124 dbus_error_free (&error); |
|
125 } |
|
126 else |
|
127 _dbus_assert_not_reached ("Unexpected message received\n"); |
|
128 |
|
129 hello_from_self_reply_received = TRUE; |
|
130 |
|
131 dbus_message_unref (reply); |
|
132 dbus_message_unref (echo_message); |
|
133 dbus_pending_call_unref (pcall); |
|
134 dbus_connection_unref (connection); |
|
135 } |
|
136 |
|
137 static DBusHandlerResult |
|
138 handle_run_hello_from_self (DBusConnection *connection, |
|
139 DBusMessage *message) |
|
140 { |
|
141 DBusError error; |
|
142 DBusMessage *reply, *self_message; |
|
143 DBusPendingCall *pcall; |
|
144 char *s; |
|
145 |
|
146 _dbus_verbose ("sending reply to Echo method\n"); |
|
147 |
|
148 dbus_error_init (&error); |
|
149 |
|
150 if (!dbus_message_get_args (message, |
|
151 &error, |
|
152 DBUS_TYPE_STRING, &s, |
|
153 DBUS_TYPE_INVALID)) |
|
154 { |
|
155 reply = dbus_message_new_error (message, |
|
156 error.name, |
|
157 error.message); |
|
158 |
|
159 if (reply == NULL) |
|
160 die ("No memory\n"); |
|
161 |
|
162 if (!dbus_connection_send (connection, reply, NULL)) |
|
163 die ("No memory\n"); |
|
164 |
|
165 dbus_message_unref (reply); |
|
166 |
|
167 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
|
168 } |
|
169 printf ("Sending HelloFromSelf\n"); |
|
170 |
|
171 _dbus_verbose ("*** Sending message to self\n"); |
|
172 self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService", |
|
173 "/org/freedesktop/TestSuite", |
|
174 "org.freedesktop.TestSuite", |
|
175 "HelloFromSelf"); |
|
176 |
|
177 if (self_message == NULL) |
|
178 die ("No memory"); |
|
179 |
|
180 if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1)) |
|
181 die("No memory"); |
|
182 |
|
183 dbus_message_ref (message); |
|
184 if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL)) |
|
185 die("No memory"); |
|
186 |
|
187 printf ("Sent HelloFromSelf\n"); |
|
188 return DBUS_HANDLER_RESULT_HANDLED; |
|
189 } |
|
190 |
|
191 static DBusHandlerResult |
|
192 handle_echo (DBusConnection *connection, |
|
193 DBusMessage *message) |
|
194 { |
|
195 DBusError error; |
|
196 DBusMessage *reply; |
|
197 char *s; |
|
198 |
|
199 _dbus_verbose ("sending reply to Echo method\n"); |
|
200 |
|
201 dbus_error_init (&error); |
|
202 |
|
203 if (!dbus_message_get_args (message, |
|
204 &error, |
|
205 DBUS_TYPE_STRING, &s, |
|
206 DBUS_TYPE_INVALID)) |
|
207 { |
|
208 reply = dbus_message_new_error (message, |
|
209 error.name, |
|
210 error.message); |
|
211 |
|
212 if (reply == NULL) |
|
213 die ("No memory\n"); |
|
214 |
|
215 if (!dbus_connection_send (connection, reply, NULL)) |
|
216 die ("No memory\n"); |
|
217 |
|
218 dbus_message_unref (reply); |
|
219 |
|
220 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
|
221 } |
|
222 |
|
223 reply = dbus_message_new_method_return (message); |
|
224 if (reply == NULL) |
|
225 die ("No memory\n"); |
|
226 |
|
227 if (!dbus_message_append_args (reply, |
|
228 DBUS_TYPE_STRING, &s, |
|
229 DBUS_TYPE_INVALID)) |
|
230 die ("No memory"); |
|
231 |
|
232 if (!dbus_connection_send (connection, reply, NULL)) |
|
233 die ("No memory\n"); |
|
234 |
|
235 fprintf (stderr, "Echo service echoed string: \"%s\"\n", s); |
|
236 |
|
237 dbus_message_unref (reply); |
|
238 |
|
239 return DBUS_HANDLER_RESULT_HANDLED; |
|
240 } |
|
241 |
|
242 static void |
|
243 path_unregistered_func (DBusConnection *connection, |
|
244 void *user_data) |
|
245 { |
|
246 /* connection was finalized */ |
|
247 } |
|
248 |
|
249 static DBusHandlerResult |
|
250 path_message_func (DBusConnection *connection, |
|
251 DBusMessage *message, |
|
252 void *user_data) |
|
253 { |
|
254 if (dbus_message_is_method_call (message, |
|
255 "org.freedesktop.TestSuite", |
|
256 "Echo")) |
|
257 return handle_echo (connection, message); |
|
258 else if (dbus_message_is_method_call (message, |
|
259 "org.freedesktop.TestSuite", |
|
260 "Exit")) |
|
261 { |
|
262 quit (); |
|
263 return DBUS_HANDLER_RESULT_HANDLED; |
|
264 } |
|
265 else if (dbus_message_is_method_call (message, |
|
266 "org.freedesktop.TestSuite", |
|
267 "EmitFoo")) |
|
268 { |
|
269 /* Emit the Foo signal */ |
|
270 DBusMessage *signal; |
|
271 double v_DOUBLE; |
|
272 |
|
273 _dbus_verbose ("emitting signal Foo\n"); |
|
274 |
|
275 signal = dbus_message_new_signal ("/org/freedesktop/TestSuite", |
|
276 "org.freedesktop.TestSuite", |
|
277 "Foo"); |
|
278 if (signal == NULL) |
|
279 die ("No memory\n"); |
|
280 |
|
281 v_DOUBLE = 42.6; |
|
282 if (!dbus_message_append_args (signal, |
|
283 DBUS_TYPE_DOUBLE, &v_DOUBLE, |
|
284 DBUS_TYPE_INVALID)) |
|
285 die ("No memory"); |
|
286 |
|
287 if (!dbus_connection_send (connection, signal, NULL)) |
|
288 die ("No memory\n"); |
|
289 |
|
290 return DBUS_HANDLER_RESULT_HANDLED; |
|
291 } |
|
292 |
|
293 else if (dbus_message_is_method_call (message, |
|
294 "org.freedesktop.TestSuite", |
|
295 "RunHelloFromSelf")) |
|
296 { |
|
297 return handle_run_hello_from_self (connection, message); |
|
298 } |
|
299 else if (dbus_message_is_method_call (message, |
|
300 "org.freedesktop.TestSuite", |
|
301 "HelloFromSelf")) |
|
302 { |
|
303 DBusMessage *reply; |
|
304 printf ("Received the HelloFromSelf message\n"); |
|
305 |
|
306 reply = dbus_message_new_method_return (message); |
|
307 if (reply == NULL) |
|
308 die ("No memory"); |
|
309 |
|
310 if (!dbus_connection_send (connection, reply, NULL)) |
|
311 die ("No memory"); |
|
312 |
|
313 return DBUS_HANDLER_RESULT_HANDLED; |
|
314 } |
|
315 else |
|
316 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
|
317 } |
|
318 |
|
319 static DBusObjectPathVTable |
|
320 echo_vtable = { |
|
321 path_unregistered_func, |
|
322 path_message_func, |
|
323 NULL, |
|
324 }; |
|
325 |
|
326 |
|
327 static const char* echo_path = "/org/freedesktop/TestSuite" ; |
|
328 |
|
329 static DBusHandlerResult |
|
330 filter_func (DBusConnection *connection, |
|
331 DBusMessage *message, |
|
332 void *user_data) |
|
333 { |
|
334 if (dbus_message_is_signal (message, |
|
335 DBUS_INTERFACE_LOCAL, |
|
336 "Disconnected")) |
|
337 { |
|
338 quit (); |
|
339 return DBUS_HANDLER_RESULT_HANDLED; |
|
340 } |
|
341 else |
|
342 { |
|
343 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
|
344 } |
|
345 } |
|
346 |
|
347 int |
|
348 main () |
|
349 { |
|
350 DBusError error; |
|
351 int result,bus_get = 1; |
|
352 DBusConnection *connection; |
|
353 |
|
354 dbus_error_init (&error); |
|
355 connection = dbus_bus_get(DBUS_BUS_SESSION, &error); |
|
356 if(connection == NULL) |
|
357 { |
|
358 |
|
359 dbus_error_free(&error); |
|
360 /*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 */ |
|
361 connection = dbus_connection_open_private ("tcp:host=localhost,port=12436", &error); |
|
362 bus_get = 0; |
|
363 } |
|
364 if (connection == NULL) |
|
365 { |
|
366 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", |
|
367 error.message); |
|
368 dbus_error_free (&error); |
|
369 return 1; |
|
370 } |
|
371 |
|
372 if( !bus_get) |
|
373 { |
|
374 |
|
375 if (!dbus_bus_register (connection, &error)) |
|
376 { |
|
377 |
|
378 //_dbus_connection_close_possibly_shared (connection); |
|
379 dbus_connection_unref (connection); |
|
380 return -1; |
|
381 } |
|
382 } |
|
383 loop = _dbus_loop_new (); |
|
384 if (loop == NULL) |
|
385 die ("No memory\n"); |
|
386 |
|
387 if (!test_connection_setup (loop, connection)) |
|
388 die ("No memory\n"); |
|
389 |
|
390 if (!dbus_connection_add_filter (connection, |
|
391 filter_func, NULL, NULL)) |
|
392 die ("No memory"); |
|
393 |
|
394 if (!dbus_connection_register_object_path (connection, |
|
395 echo_path, |
|
396 &echo_vtable, |
|
397 (void*) 0xdeadbeef)) |
|
398 die ("No memory"); |
|
399 |
|
400 { |
|
401 void *d; |
|
402 if (!dbus_connection_get_object_path_data (connection, echo_path, &d)) |
|
403 die ("No memory"); |
|
404 if (d != (void*) 0xdeadbeef) |
|
405 die ("dbus_connection_get_object_path_data() doesn't seem to work right\n"); |
|
406 } |
|
407 |
|
408 result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService", |
|
409 0, &error); |
|
410 if (dbus_error_is_set (&error)) |
|
411 { |
|
412 fprintf (stderr, "Error %s\n", error.message); |
|
413 _dbus_verbose ("*** Failed to acquire service: %s\n", |
|
414 error.message); |
|
415 dbus_error_free (&error); |
|
416 exit (1); |
|
417 } |
|
418 |
|
419 _dbus_verbose ("*** Test service entering main loop\n"); |
|
420 _dbus_loop_run (loop); |
|
421 |
|
422 test_connection_shutdown (loop, connection); |
|
423 |
|
424 dbus_connection_remove_filter (connection, filter_func, NULL); |
|
425 |
|
426 if( !bus_get) |
|
427 dbus_connection_close(connection); |
|
428 |
|
429 dbus_connection_unref (connection); |
|
430 |
|
431 _dbus_loop_unref (loop); |
|
432 loop = NULL; |
|
433 |
|
434 dbus_free(loop); |
|
435 dbus_shutdown (); |
|
436 |
|
437 _dbus_verbose ("*** Test service exiting\n"); |
|
438 |
|
439 return 0; |
|
440 } |