5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
32 #include <dbus/dbus.h>
41 unsigned int refcount;
46 struct interface_data {
48 GDBusMethodTable *methods;
49 GDBusSignalTable *signals;
50 GDBusPropertyTable *properties;
52 GDBusDestroyFunction destroy;
55 static void print_arguments(GString *gstr, const char *sig,
56 const char *direction)
60 for (i = 0; sig[i]; i++) {
62 int struct_level, dict_level;
67 struct_level = dict_level = 0;
68 memset(type, 0, sizeof(type));
70 /* Gather enough data to have a single complete type */
71 for (len = 0; len < (sizeof(type) - 1) && sig[i]; len++, i++) {
78 if (struct_level <= 0 && dict_level <= 0)
86 if (struct_level <= 0 && dict_level <= 0)
92 if (struct_level <= 0 && dict_level <= 0)
105 g_string_append_printf(gstr,
106 "\t\t\t<arg type=\"%s\" direction=\"%s\"/>\n",
109 g_string_append_printf(gstr,
110 "\t\t\t<arg type=\"%s\"/>\n",
115 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
117 GDBusMethodTable *method;
118 GDBusSignalTable *signal;
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",
125 g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
127 print_arguments(gstr, method->signature, "in");
128 print_arguments(gstr, method->reply, "out");
129 g_string_append_printf(gstr, "\t\t</method>\n");
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",
138 g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
140 print_arguments(gstr, signal->signature, NULL);
141 g_string_append_printf(gstr, "\t\t</signal>\n");
146 static void generate_introspection_xml(DBusConnection *conn,
147 struct generic_data *data, const char *path)
154 g_free(data->introspect);
156 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
158 g_string_append_printf(gstr,
159 "<node name=\"%s\">\n"
160 "\t<interface name=\"org.freedesktop.DBus.Introspectable\">\n"
161 "\t\t<method name=\"Introspect\">\n"
162 "\t\t\t<arg name=\"xml_data\" type=\"s\" direction=\"out\"/>\n"
164 "\t</interface>\n", path);
166 for (list = data->interfaces; list; list = list->next) {
167 struct interface_data *iface = list->data;
169 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
172 generate_interface_xml(gstr, iface);
174 g_string_append_printf(gstr, "\t</interface>\n");
177 if (!dbus_connection_list_registered(conn, path, &children))
180 for (i = 0; children[i]; i++)
181 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
184 dbus_free_string_array(children);
187 g_string_append_printf(gstr, "</node>\n");
189 data->introspect = g_string_free(gstr, FALSE);
192 static DBusHandlerResult introspect(DBusConnection *connection,
193 DBusMessage *message, struct generic_data *data)
197 if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
198 error("Unexpected signature to introspect call");
199 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
202 if (!data->introspect)
203 generate_introspection_xml(connection, data,
204 dbus_message_get_path(message));
206 reply = dbus_message_new_method_return(message);
208 return DBUS_HANDLER_RESULT_NEED_MEMORY;
210 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
213 dbus_connection_send(connection, reply, NULL);
215 dbus_message_unref(reply);
217 return DBUS_HANDLER_RESULT_HANDLED;
220 static void generic_unregister(DBusConnection *connection, void *user_data)
222 struct generic_data *data = user_data;
224 g_free(data->introspect);
228 static struct interface_data *find_interface(GSList *interfaces,
236 for (list = interfaces; list; list = list->next) {
237 struct interface_data *iface = list->data;
238 if (!strcmp(name, iface->name))
245 static DBusHandlerResult generic_message(DBusConnection *connection,
246 DBusMessage *message, void *user_data)
248 struct generic_data *data = user_data;
249 struct interface_data *iface;
250 GDBusMethodTable *method;
251 const char *interface;
253 if (dbus_message_is_method_call(message,
254 DBUS_INTERFACE_INTROSPECTABLE,
256 return introspect(connection, message, data);
258 interface = dbus_message_get_interface(message);
260 iface = find_interface(data->interfaces, interface);
262 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
264 for (method = iface->methods; method &&
265 method->name && method->function; method++) {
268 if (dbus_message_is_method_call(message, iface->name,
269 method->name) == FALSE)
272 if (dbus_message_has_signature(message,
273 method->signature) == FALSE)
276 reply = method->function(connection, message, iface->user_data);
278 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
280 dbus_message_unref(reply);
281 return DBUS_HANDLER_RESULT_HANDLED;
284 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
286 return DBUS_HANDLER_RESULT_HANDLED;
290 return DBUS_HANDLER_RESULT_NEED_MEMORY;
292 dbus_connection_send(connection, reply, NULL);
293 dbus_message_unref(reply);
295 return DBUS_HANDLER_RESULT_HANDLED;
298 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
301 static DBusObjectPathVTable generic_table = {
302 .unregister_function = generic_unregister,
303 .message_function = generic_message,
306 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
308 struct generic_data *data = NULL;
309 char *parent_path, *slash;
311 parent_path = g_strdup(child_path);
312 slash = strrchr(parent_path, '/');
316 if (slash == parent_path && parent_path[1] != '\0')
317 parent_path[1] = '\0';
321 if (!strlen(parent_path))
324 if (!dbus_connection_get_object_path_data(conn, parent_path,
331 g_free(data->introspect);
332 data->introspect = NULL;
338 static struct generic_data *object_path_ref(DBusConnection *connection,
341 struct generic_data *data;
343 if (dbus_connection_get_object_path_data(connection, path,
344 (void *) &data) == TRUE) {
351 data = g_new0(struct generic_data, 1);
353 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
357 if (!dbus_connection_register_object_path(connection, path,
358 &generic_table, data)) {
359 g_free(data->introspect);
364 invalidate_parent_data(connection, path);
369 static void object_path_unref(DBusConnection *connection, const char *path)
371 struct generic_data *data = NULL;
373 if (dbus_connection_get_object_path_data(connection, path,
374 (void *) &data) == FALSE)
382 if (data->refcount > 0)
385 invalidate_parent_data(connection, path);
387 dbus_connection_unregister_object_path(connection, path);
390 static gboolean check_signal(DBusConnection *conn, const char *path,
391 const char *interface, const char *name,
394 struct generic_data *data = NULL;
395 struct interface_data *iface;
396 GDBusSignalTable *signal;
399 if (!dbus_connection_get_object_path_data(conn, path,
400 (void *) &data) || !data) {
401 error("dbus_connection_emit_signal: path %s isn't registered",
406 iface = find_interface(data->interfaces, interface);
408 error("dbus_connection_emit_signal: %s does not implement %s",
413 for (signal = iface->signals; signal && signal->name; signal++) {
414 if (!strcmp(signal->name, name)) {
415 *args = signal->signature;
421 error("No signal named %s on interface %s", name, interface);
428 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
430 const char *interface,
437 const char *signature, *args;
439 if (!check_signal(conn, path, interface, name, &args))
442 signal = dbus_message_new_signal(path, interface, name);
444 error("Unable to allocate new %s.%s signal", interface, name);
448 ret = dbus_message_append_args_valist(signal, first, var_args);
452 signature = dbus_message_get_signature(signal);
453 if (strcmp(args, signature) != 0) {
454 error("%s.%s: expected signature'%s' but got '%s'",
455 interface, name, args, signature);
460 ret = dbus_connection_send(conn, signal, NULL);
463 dbus_message_unref(signal);
468 gboolean g_dbus_register_interface(DBusConnection *connection,
469 const char *path, const char *name,
470 GDBusMethodTable *methods,
471 GDBusSignalTable *signals,
472 GDBusPropertyTable *properties,
474 GDBusDestroyFunction destroy)
476 struct generic_data *data;
477 struct interface_data *iface;
479 data = object_path_ref(connection, path);
483 if (find_interface(data->interfaces, name))
486 iface = g_new0(struct interface_data, 1);
488 iface->name = g_strdup(name);
489 iface->methods = methods;
490 iface->signals = signals;
491 iface->properties = properties;
492 iface->user_data = user_data;
493 iface->destroy = destroy;
495 data->interfaces = g_slist_append(data->interfaces, iface);
497 g_free(data->introspect);
498 data->introspect = NULL;
503 gboolean g_dbus_unregister_interface(DBusConnection *connection,
504 const char *path, const char *name)
506 struct generic_data *data = NULL;
507 struct interface_data *iface;
512 if (dbus_connection_get_object_path_data(connection, path,
513 (void *) &data) == FALSE)
519 iface = find_interface(data->interfaces, name);
523 data->interfaces = g_slist_remove(data->interfaces, iface);
526 iface->destroy(iface->user_data);
531 g_free(data->introspect);
532 data->introspect = NULL;
534 object_path_unref(connection, path);
539 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
540 const char *format, va_list args)
544 vsnprintf(str, sizeof(str), format, args);
546 return dbus_message_new_error(message, name, str);
549 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
550 const char *format, ...)
555 va_start(args, format);
557 reply = g_dbus_create_error_valist(message, name, format, args);
564 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
565 int type, va_list args)
569 reply = dbus_message_new_method_return(message);
573 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
574 dbus_message_unref(reply);
581 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
586 va_start(args, type);
588 reply = g_dbus_create_reply_valist(message, type, args);
595 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
599 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
600 dbus_message_set_no_reply(message, TRUE);
602 result = dbus_connection_send(connection, message, NULL);
604 dbus_message_unref(message);
609 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
610 DBusMessage *message, int type, va_list args)
614 reply = dbus_message_new_method_return(message);
618 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
619 dbus_message_unref(reply);
623 return g_dbus_send_message(connection, reply);
626 gboolean g_dbus_send_reply(DBusConnection *connection,
627 DBusMessage *message, int type, ...)
632 va_start(args, type);
634 result = g_dbus_send_reply_valist(connection, message, type, args);
641 gboolean g_dbus_emit_signal(DBusConnection *connection,
642 const char *path, const char *interface,
643 const char *name, int type, ...)
648 va_start(args, type);
650 result = emit_signal_valist(connection, path, interface,
658 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
659 const char *path, const char *interface,
660 const char *name, int type, va_list args)
662 return emit_signal_valist(connection, path, interface,