Use tabs for indentation
[platform/upstream/connman.git] / gdbus / object.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2008  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <glib.h>
32 #include <dbus/dbus.h>
33
34 #include "gdbus.h"
35
36 #define info(fmt...)
37 #define error(fmt...)
38 #define debug(fmt...)
39
40 struct generic_data {
41         unsigned int refcount;
42         GSList *interfaces;
43         char *introspect;
44 };
45
46 struct interface_data {
47         char *name;
48         GDBusMethodTable *methods;
49         GDBusSignalTable *signals;
50         GDBusPropertyTable *properties;
51         void *user_data;
52         GDBusDestroyFunction destroy;
53 };
54
55 static void print_arguments(GString *gstr, const char *sig,
56                                                 const char *direction)
57 {
58         int i;
59
60         for (i = 0; sig[i]; i++) {
61                 char type[32];
62                 int len, struct_level, dict_level;
63                 gboolean complete;
64
65                 complete = FALSE;
66                 struct_level = dict_level = 0;
67                 memset(type, 0, sizeof(type));
68
69                 /* Gather enough data to have a single complete type */
70                 for (len = 0; len < (sizeof(type) - 1) && sig[i]; len++, i++) {
71                         switch (sig[i]){
72                         case '(':
73                                 struct_level++;
74                                 break;
75                         case ')':
76                                 struct_level--;
77                                 if (struct_level <= 0 && dict_level <= 0)
78                                         complete = TRUE;
79                                 break;
80                         case '{':
81                                 dict_level++;
82                                 break;
83                         case '}':
84                                 dict_level--;
85                                 if (struct_level <= 0 && dict_level <= 0)
86                                         complete = TRUE;
87                                 break;
88                         case 'a':
89                                 break;
90                         default:
91                                 if (struct_level <= 0 && dict_level <= 0)
92                                         complete = TRUE;
93                                 break;
94                         }
95
96                         type[len] = sig[i];
97
98                         if (complete)
99                                 break;
100                 }
101
102
103                 if (direction)
104                         g_string_append_printf(gstr,
105                                         "\t\t\t<arg type=\"%s\" direction=\"%s\"/>\n",
106                                         type, direction);
107                 else
108                         g_string_append_printf(gstr,
109                                         "\t\t\t<arg type=\"%s\"/>\n",
110                                         type);
111         }
112 }
113
114 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
115 {
116         GDBusMethodTable *method;
117         GDBusSignalTable *signal;
118
119         for (method = iface->methods; method && method->name; method++) {
120                 if (!strlen(method->signature) && !strlen(method->reply))
121                         g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
122                                                                 method->name);
123                 else {
124                         g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
125                                                                 method->name);
126                         print_arguments(gstr, method->signature, "in");
127                         print_arguments(gstr, method->reply, "out");
128                         g_string_append_printf(gstr, "\t\t</method>\n");
129                 }
130         }
131
132         for (signal = iface->signals; signal && signal->name; signal++) {
133                 if (!strlen(signal->signature))
134                         g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
135                                                                 signal->name);
136                 else {
137                         g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
138                                                                 signal->name);
139                         print_arguments(gstr, signal->signature, NULL);
140                         g_string_append_printf(gstr, "\t\t</signal>\n");
141                 }
142         }
143 }
144
145 static void generate_introspection_xml(DBusConnection *conn,
146                                 struct generic_data *data, const char *path)
147 {
148         GSList *list;
149         GString *gstr;
150         char **children;
151         int i;
152
153         g_free(data->introspect);
154
155         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
156
157         g_string_append_printf(gstr, "<node name=\"%s\">\n", path);
158
159         for (list = data->interfaces; list; list = list->next) {
160                 struct interface_data *iface = list->data;
161
162                 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
163                                                                 iface->name);
164
165                 generate_interface_xml(gstr, iface);
166
167                 g_string_append_printf(gstr, "\t</interface>\n");
168         }
169
170         if (!dbus_connection_list_registered(conn, path, &children))
171                 goto done;
172
173         for (i = 0; children[i]; i++)
174                 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
175                                                                 children[i]);
176
177         dbus_free_string_array(children);
178
179 done:
180         g_string_append_printf(gstr, "</node>\n");
181
182         data->introspect = g_string_free(gstr, FALSE);
183 }
184
185 static DBusHandlerResult introspect(DBusConnection *connection,
186                                 DBusMessage *message, struct generic_data *data)
187 {
188         DBusMessage *reply;
189
190         if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
191                 error("Unexpected signature to introspect call");
192                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
193         }
194
195         if (!data->introspect)
196                 generate_introspection_xml(connection, data,
197                                                 dbus_message_get_path(message));
198
199         reply = dbus_message_new_method_return(message);
200         if (!reply)
201                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
202
203         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
204                                         DBUS_TYPE_INVALID);
205
206         dbus_connection_send(connection, reply, NULL);
207
208         dbus_message_unref(reply);
209
210         return DBUS_HANDLER_RESULT_HANDLED;
211 }
212
213 static void generic_unregister(DBusConnection *connection, void *user_data)
214 {
215         struct generic_data *data = user_data;
216
217         g_free(data->introspect);
218         g_free(data);
219 }
220
221 static struct interface_data *find_interface(GSList *interfaces,
222                                                 const char *name)
223 {
224         GSList *list;
225
226         for (list = interfaces; list; list = list->next) {
227                 struct interface_data *iface = list->data;
228                 if (!strcmp(name, iface->name))
229                         return iface;
230         }
231
232         return NULL;
233 }
234
235 static DBusHandlerResult generic_message(DBusConnection *connection,
236                                         DBusMessage *message, void *user_data)
237 {
238         struct generic_data *data = user_data;
239         struct interface_data *iface;
240         GDBusMethodTable *method;
241         const char *interface;
242
243         if (dbus_message_is_method_call(message,
244                                         DBUS_INTERFACE_INTROSPECTABLE,
245                                                                 "Introspect"))
246                 return introspect(connection, message, data);
247
248         interface = dbus_message_get_interface(message);
249
250         iface = find_interface(data->interfaces, interface);
251         if (!iface)
252                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
253
254         for (method = iface->methods; method &&
255                         method->name && method->function; method++) {
256                 DBusMessage *reply;
257
258                 if (dbus_message_is_method_call(message, iface->name,
259                                                         method->name) == FALSE)
260                         continue;
261
262                 if (dbus_message_has_signature(message,
263                                                 method->signature) == FALSE)
264                         continue;
265
266                 reply = method->function(connection, message, iface->user_data);
267
268                 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
269                         if (reply != NULL)
270                                 dbus_message_unref(reply);
271                         return DBUS_HANDLER_RESULT_HANDLED;
272                 }
273
274                 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
275                         if (reply == NULL)
276                                 return DBUS_HANDLER_RESULT_HANDLED;
277                 }
278
279                 if (reply == NULL)
280                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
281
282                 dbus_connection_send(connection, reply, NULL);
283                 dbus_message_unref(reply);
284
285                 return DBUS_HANDLER_RESULT_HANDLED;
286         }
287
288         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
289 }
290
291 static DBusObjectPathVTable generic_table = {
292         .unregister_function    = generic_unregister,
293         .message_function       = generic_message,
294 };
295
296 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
297 {
298         struct generic_data *data = NULL;
299         char *parent_path, *slash;
300
301         parent_path = g_strdup(child_path);
302         slash = strrchr(parent_path, '/');
303         if (!slash)
304                 goto done;
305
306         *slash = '\0';
307         if (!strlen(parent_path))
308                 goto done;
309
310         if (!dbus_connection_get_object_path_data(conn, parent_path,
311                                                         (void *) &data))
312                 goto done;
313
314         if (!data)
315                 goto done;
316
317         g_free(data->introspect);
318         data->introspect = NULL;
319
320 done:
321         g_free(parent_path);
322 }
323
324 static struct generic_data *object_path_ref(DBusConnection *connection,
325                                                         const char *path)
326 {
327         struct generic_data *data;
328
329         if (dbus_connection_get_object_path_data(connection, path,
330                                                 (void *) &data) == TRUE) {
331                 if (data != NULL) {
332                         data->refcount++;
333                         return data;
334                 }
335         }
336
337         data = g_new0(struct generic_data, 1);
338
339         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
340
341         data->refcount = 1;
342
343         if (!dbus_connection_register_object_path(connection, path,
344                                                 &generic_table, data)) {
345                 g_free(data->introspect);
346                 g_free(data);
347                 return NULL;
348         }
349
350         invalidate_parent_data(connection, path);
351
352         return data;
353 }
354
355 static void object_path_unref(DBusConnection *connection, const char *path)
356 {
357         struct generic_data *data = NULL;
358
359         if (dbus_connection_get_object_path_data(connection, path,
360                                                 (void *) &data) == FALSE)
361                 return;
362
363         if (data == NULL)
364                 return;
365
366         data->refcount--;
367
368         if (data->refcount > 0)
369                 return;
370
371         invalidate_parent_data(connection, path);
372
373         dbus_connection_unregister_object_path(connection, path);
374 }
375
376 static gboolean check_signal(DBusConnection *conn, const char *path,
377                                 const char *interface, const char *name,
378                                 const char **args)
379 {
380         struct generic_data *data = NULL;
381         struct interface_data *iface;
382         GDBusSignalTable *signal;
383
384         *args = NULL;
385         if (!dbus_connection_get_object_path_data(conn, path,
386                                         (void *) &data) || !data) {
387                 error("dbus_connection_emit_signal: path %s isn't registered",
388                                 path);
389                 return FALSE;
390         }
391
392         iface = find_interface(data->interfaces, interface);
393
394         if (!iface) {
395                 error("dbus_connection_emit_signal: %s does not implement %s",
396                                 path, interface);
397                 return FALSE;
398         }
399
400         for (signal = iface->signals; signal && signal->name; signal++) {
401                 if (!strcmp(signal->name, name)) {
402                         *args = signal->signature;
403                         break;
404                 }
405         }
406
407         if (!*args) {
408                 error("No signal named %s on interface %s", name, interface);
409                 return FALSE;
410         }
411
412         return TRUE;
413 }
414
415 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
416                                                 const char *path,
417                                                 const char *interface,
418                                                 const char *name,
419                                                 int first,
420                                                 va_list var_args)
421 {
422         DBusMessage *signal;
423         dbus_bool_t ret;
424         const char *signature, *args;
425
426         if (!check_signal(conn, path, interface, name, &args))
427                 return FALSE;
428
429         signal = dbus_message_new_signal(path, interface, name);
430         if (!signal) {
431                 error("Unable to allocate new %s.%s signal", interface,  name);
432                 return FALSE;
433         }
434
435         ret = dbus_message_append_args_valist(signal, first, var_args);
436         if (!ret)
437                 goto fail;
438
439         signature = dbus_message_get_signature(signal);
440         if (strcmp(args, signature) != 0) {
441                 error("%s.%s: expected signature'%s' but got '%s'",
442                                 interface, name, args, signature);
443                 ret = FALSE;
444                 goto fail;
445         }
446
447         ret = dbus_connection_send(conn, signal, NULL);
448
449 fail:
450         dbus_message_unref(signal);
451
452         return ret;
453 }
454
455 gboolean g_dbus_register_interface(DBusConnection *connection,
456                                         const char *path, const char *name,
457                                         GDBusMethodTable *methods,
458                                         GDBusSignalTable *signals,
459                                         GDBusPropertyTable *properties,
460                                         void *user_data,
461                                         GDBusDestroyFunction destroy)
462 {
463         struct generic_data *data;
464         struct interface_data *iface;
465
466         data = object_path_ref(connection, path);
467         if (data == NULL)
468                 return FALSE;
469
470         if (find_interface(data->interfaces, name))
471                 return FALSE;
472
473         iface = g_new0(struct interface_data, 1);
474
475         iface->name = g_strdup(name);
476         iface->methods = methods;
477         iface->signals = signals;
478         iface->properties = properties;
479         iface->user_data = user_data;
480         iface->destroy = destroy;
481
482         data->interfaces = g_slist_append(data->interfaces, iface);
483
484         g_free(data->introspect);
485         data->introspect = NULL;
486
487         return TRUE;
488 }
489
490 gboolean g_dbus_unregister_interface(DBusConnection *connection,
491                                         const char *path, const char *name)
492 {
493         struct generic_data *data = NULL;
494         struct interface_data *iface;
495
496         if (dbus_connection_get_object_path_data(connection, path,
497                                                 (void *) &data) == FALSE)
498                 return FALSE;
499
500         if (data == NULL)
501                 return FALSE;
502
503         iface = find_interface(data->interfaces, name);
504         if (!iface)
505                 return FALSE;
506
507         data->interfaces = g_slist_remove(data->interfaces, iface);
508
509         if (iface->destroy)
510                 iface->destroy(iface->user_data);
511
512         g_free(iface->name);
513         g_free(iface);
514
515         g_free(data->introspect);
516         data->introspect = NULL;
517
518         object_path_unref(connection, path);
519
520         return TRUE;
521 }
522
523 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
524                                         const char *format, va_list args)
525 {
526         char str[1024];
527
528         vsnprintf(str, sizeof(str), format, args);
529
530         return dbus_message_new_error(message, name, str);
531 }
532
533 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
534                                                 const char *format, ...)
535 {
536         va_list args;
537         DBusMessage *reply;
538
539         va_start(args, format);
540
541         reply = g_dbus_create_error_valist(message, name, format, args);
542
543         va_end(args);
544
545         return reply;
546 }
547
548 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
549                                                 int type, va_list args)
550 {
551         DBusMessage *reply;
552
553         reply = dbus_message_new_method_return(message);
554         if (reply == NULL)
555                 return NULL;
556
557         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
558                 dbus_message_unref(reply);
559                 return NULL;
560         }
561
562         return reply;
563 }
564
565 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
566 {
567         va_list args;
568         DBusMessage *reply;
569
570         va_start(args, type);
571
572         reply = g_dbus_create_reply_valist(message, type, args);
573
574         va_end(args);
575
576         return reply;
577 }
578
579 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
580 {
581         dbus_bool_t result;
582
583         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
584                 dbus_message_set_no_reply(message, TRUE);
585
586         result = dbus_connection_send(connection, message, NULL);
587
588         dbus_message_unref(message);
589
590         return result;
591 }
592
593 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
594                                 DBusMessage *message, int type, va_list args)
595 {
596         DBusMessage *reply;
597
598         reply = dbus_message_new_method_return(message);
599         if (reply == NULL)
600                 return FALSE;
601
602         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
603                 dbus_message_unref(reply);
604                 return FALSE;
605         }
606
607         return g_dbus_send_message(connection, reply);
608 }
609
610 gboolean g_dbus_send_reply(DBusConnection *connection,
611                                 DBusMessage *message, int type, ...)
612 {
613         va_list args;
614         gboolean result;
615
616         va_start(args, type);
617
618         result = g_dbus_send_reply_valist(connection, message, type, args);
619
620         va_end(args);
621
622         return result;
623 }
624
625 gboolean g_dbus_emit_signal(DBusConnection *connection,
626                                 const char *path, const char *interface,
627                                 const char *name, int type, ...)
628 {
629         va_list args;
630         gboolean result;
631
632         va_start(args, type);
633
634         result = emit_signal_valist(connection, path, interface,
635                                                         name, type, args);
636
637         va_end(args);
638
639         return result;
640 }
641
642 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
643                                 const char *path, const char *interface,
644                                 const char *name, int type, va_list args)
645 {
646         return emit_signal_valist(connection, path, interface,
647                                                         name, type, args);
648 }