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