Add support for sending flight mode indication
[connman] / gdbus / object.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2009  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         if (!name)
228                 return NULL;
229
230         for (list = interfaces; list; list = list->next) {
231                 struct interface_data *iface = list->data;
232                 if (!strcmp(name, iface->name))
233                         return iface;
234         }
235
236         return NULL;
237 }
238
239 static DBusHandlerResult generic_message(DBusConnection *connection,
240                                         DBusMessage *message, void *user_data)
241 {
242         struct generic_data *data = user_data;
243         struct interface_data *iface;
244         GDBusMethodTable *method;
245         const char *interface;
246
247         if (dbus_message_is_method_call(message,
248                                         DBUS_INTERFACE_INTROSPECTABLE,
249                                                                 "Introspect"))
250                 return introspect(connection, message, data);
251
252         interface = dbus_message_get_interface(message);
253
254         iface = find_interface(data->interfaces, interface);
255         if (!iface)
256                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
257
258         for (method = iface->methods; method &&
259                         method->name && method->function; method++) {
260                 DBusMessage *reply;
261
262                 if (dbus_message_is_method_call(message, iface->name,
263                                                         method->name) == FALSE)
264                         continue;
265
266                 if (dbus_message_has_signature(message,
267                                                 method->signature) == FALSE)
268                         continue;
269
270                 reply = method->function(connection, message, iface->user_data);
271
272                 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
273                         if (reply != NULL)
274                                 dbus_message_unref(reply);
275                         return DBUS_HANDLER_RESULT_HANDLED;
276                 }
277
278                 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
279                         if (reply == NULL)
280                                 return DBUS_HANDLER_RESULT_HANDLED;
281                 }
282
283                 if (reply == NULL)
284                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
285
286                 dbus_connection_send(connection, reply, NULL);
287                 dbus_message_unref(reply);
288
289                 return DBUS_HANDLER_RESULT_HANDLED;
290         }
291
292         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
293 }
294
295 static DBusObjectPathVTable generic_table = {
296         .unregister_function    = generic_unregister,
297         .message_function       = generic_message,
298 };
299
300 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
301 {
302         struct generic_data *data = NULL;
303         char *parent_path, *slash;
304
305         parent_path = g_strdup(child_path);
306         slash = strrchr(parent_path, '/');
307         if (!slash)
308                 goto done;
309
310         *slash = '\0';
311         if (!strlen(parent_path))
312                 goto done;
313
314         if (!dbus_connection_get_object_path_data(conn, parent_path,
315                                                         (void *) &data))
316                 goto done;
317
318         if (!data)
319                 goto done;
320
321         g_free(data->introspect);
322         data->introspect = NULL;
323
324 done:
325         g_free(parent_path);
326 }
327
328 static struct generic_data *object_path_ref(DBusConnection *connection,
329                                                         const char *path)
330 {
331         struct generic_data *data;
332
333         if (dbus_connection_get_object_path_data(connection, path,
334                                                 (void *) &data) == TRUE) {
335                 if (data != NULL) {
336                         data->refcount++;
337                         return data;
338                 }
339         }
340
341         data = g_new0(struct generic_data, 1);
342
343         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
344
345         data->refcount = 1;
346
347         if (!dbus_connection_register_object_path(connection, path,
348                                                 &generic_table, data)) {
349                 g_free(data->introspect);
350                 g_free(data);
351                 return NULL;
352         }
353
354         invalidate_parent_data(connection, path);
355
356         return data;
357 }
358
359 static void object_path_unref(DBusConnection *connection, const char *path)
360 {
361         struct generic_data *data = NULL;
362
363         if (dbus_connection_get_object_path_data(connection, path,
364                                                 (void *) &data) == FALSE)
365                 return;
366
367         if (data == NULL)
368                 return;
369
370         data->refcount--;
371
372         if (data->refcount > 0)
373                 return;
374
375         invalidate_parent_data(connection, path);
376
377         dbus_connection_unregister_object_path(connection, path);
378 }
379
380 static gboolean check_signal(DBusConnection *conn, const char *path,
381                                 const char *interface, const char *name,
382                                 const char **args)
383 {
384         struct generic_data *data = NULL;
385         struct interface_data *iface;
386         GDBusSignalTable *signal;
387
388         *args = NULL;
389         if (!dbus_connection_get_object_path_data(conn, path,
390                                         (void *) &data) || !data) {
391                 error("dbus_connection_emit_signal: path %s isn't registered",
392                                 path);
393                 return FALSE;
394         }
395
396         iface = find_interface(data->interfaces, interface);
397         if (!iface) {
398                 error("dbus_connection_emit_signal: %s does not implement %s",
399                                 path, interface);
400                 return FALSE;
401         }
402
403         for (signal = iface->signals; signal && signal->name; signal++) {
404                 if (!strcmp(signal->name, name)) {
405                         *args = signal->signature;
406                         break;
407                 }
408         }
409
410         if (!*args) {
411                 error("No signal named %s on interface %s", name, interface);
412                 return FALSE;
413         }
414
415         return TRUE;
416 }
417
418 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
419                                                 const char *path,
420                                                 const char *interface,
421                                                 const char *name,
422                                                 int first,
423                                                 va_list var_args)
424 {
425         DBusMessage *signal;
426         dbus_bool_t ret;
427         const char *signature, *args;
428
429         if (!check_signal(conn, path, interface, name, &args))
430                 return FALSE;
431
432         signal = dbus_message_new_signal(path, interface, name);
433         if (!signal) {
434                 error("Unable to allocate new %s.%s signal", interface,  name);
435                 return FALSE;
436         }
437
438         ret = dbus_message_append_args_valist(signal, first, var_args);
439         if (!ret)
440                 goto fail;
441
442         signature = dbus_message_get_signature(signal);
443         if (strcmp(args, signature) != 0) {
444                 error("%s.%s: expected signature'%s' but got '%s'",
445                                 interface, name, args, signature);
446                 ret = FALSE;
447                 goto fail;
448         }
449
450         ret = dbus_connection_send(conn, signal, NULL);
451
452 fail:
453         dbus_message_unref(signal);
454
455         return ret;
456 }
457
458 gboolean g_dbus_register_interface(DBusConnection *connection,
459                                         const char *path, const char *name,
460                                         GDBusMethodTable *methods,
461                                         GDBusSignalTable *signals,
462                                         GDBusPropertyTable *properties,
463                                         void *user_data,
464                                         GDBusDestroyFunction destroy)
465 {
466         struct generic_data *data;
467         struct interface_data *iface;
468
469         data = object_path_ref(connection, path);
470         if (data == NULL)
471                 return FALSE;
472
473         if (find_interface(data->interfaces, name))
474                 return FALSE;
475
476         iface = g_new0(struct interface_data, 1);
477
478         iface->name = g_strdup(name);
479         iface->methods = methods;
480         iface->signals = signals;
481         iface->properties = properties;
482         iface->user_data = user_data;
483         iface->destroy = destroy;
484
485         data->interfaces = g_slist_append(data->interfaces, iface);
486
487         g_free(data->introspect);
488         data->introspect = NULL;
489
490         return TRUE;
491 }
492
493 gboolean g_dbus_unregister_interface(DBusConnection *connection,
494                                         const char *path, const char *name)
495 {
496         struct generic_data *data = NULL;
497         struct interface_data *iface;
498
499         if (!path)
500                 return FALSE;
501
502         if (dbus_connection_get_object_path_data(connection, path,
503                                                 (void *) &data) == FALSE)
504                 return FALSE;
505
506         if (data == NULL)
507                 return FALSE;
508
509         iface = find_interface(data->interfaces, name);
510         if (!iface)
511                 return FALSE;
512
513         data->interfaces = g_slist_remove(data->interfaces, iface);
514
515         if (iface->destroy)
516                 iface->destroy(iface->user_data);
517
518         g_free(iface->name);
519         g_free(iface);
520
521         g_free(data->introspect);
522         data->introspect = NULL;
523
524         object_path_unref(connection, path);
525
526         return TRUE;
527 }
528
529 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
530                                         const char *format, va_list args)
531 {
532         char str[1024];
533
534         vsnprintf(str, sizeof(str), format, args);
535
536         return dbus_message_new_error(message, name, str);
537 }
538
539 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
540                                                 const char *format, ...)
541 {
542         va_list args;
543         DBusMessage *reply;
544
545         va_start(args, format);
546
547         reply = g_dbus_create_error_valist(message, name, format, args);
548
549         va_end(args);
550
551         return reply;
552 }
553
554 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
555                                                 int type, va_list args)
556 {
557         DBusMessage *reply;
558
559         reply = dbus_message_new_method_return(message);
560         if (reply == NULL)
561                 return NULL;
562
563         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
564                 dbus_message_unref(reply);
565                 return NULL;
566         }
567
568         return reply;
569 }
570
571 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
572 {
573         va_list args;
574         DBusMessage *reply;
575
576         va_start(args, type);
577
578         reply = g_dbus_create_reply_valist(message, type, args);
579
580         va_end(args);
581
582         return reply;
583 }
584
585 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
586 {
587         dbus_bool_t result;
588
589         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
590                 dbus_message_set_no_reply(message, TRUE);
591
592         result = dbus_connection_send(connection, message, NULL);
593
594         dbus_message_unref(message);
595
596         return result;
597 }
598
599 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
600                                 DBusMessage *message, int type, va_list args)
601 {
602         DBusMessage *reply;
603
604         reply = dbus_message_new_method_return(message);
605         if (reply == NULL)
606                 return FALSE;
607
608         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
609                 dbus_message_unref(reply);
610                 return FALSE;
611         }
612
613         return g_dbus_send_message(connection, reply);
614 }
615
616 gboolean g_dbus_send_reply(DBusConnection *connection,
617                                 DBusMessage *message, int type, ...)
618 {
619         va_list args;
620         gboolean result;
621
622         va_start(args, type);
623
624         result = g_dbus_send_reply_valist(connection, message, type, args);
625
626         va_end(args);
627
628         return result;
629 }
630
631 gboolean g_dbus_emit_signal(DBusConnection *connection,
632                                 const char *path, const char *interface,
633                                 const char *name, int type, ...)
634 {
635         va_list args;
636         gboolean result;
637
638         va_start(args, type);
639
640         result = emit_signal_valist(connection, path, interface,
641                                                         name, type, args);
642
643         va_end(args);
644
645         return result;
646 }
647
648 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
649                                 const char *path, const char *interface,
650                                 const char *name, int type, va_list args)
651 {
652         return emit_signal_valist(connection, path, interface,
653                                                         name, type, args);
654 }