5 * Copyright (C) 2004-2008 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 len, struct_level, dict_level;
66 struct_level = dict_level = 0;
67 memset(type, 0, sizeof(type));
69 /* Gather enough data to have a single complete type */
70 for (len = 0; len < (sizeof(type) - 1) && sig[i]; len++, i++) {
77 if (struct_level <= 0 && dict_level <= 0)
85 if (struct_level <= 0 && dict_level <= 0)
91 if (struct_level <= 0 && dict_level <= 0)
104 g_string_append_printf(gstr,
105 "\t\t\t<arg type=\"%s\" direction=\"%s\"/>\n",
108 g_string_append_printf(gstr,
109 "\t\t\t<arg type=\"%s\"/>\n",
114 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
116 GDBusMethodTable *method;
117 GDBusSignalTable *signal;
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",
124 g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
126 print_arguments(gstr, method->signature, "in");
127 print_arguments(gstr, method->reply, "out");
128 g_string_append_printf(gstr, "\t\t</method>\n");
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",
137 g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
139 print_arguments(gstr, signal->signature, NULL);
140 g_string_append_printf(gstr, "\t\t</signal>\n");
145 static void generate_introspection_xml(DBusConnection *conn,
146 struct generic_data *data, const char *path)
153 g_free(data->introspect);
155 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
157 g_string_append_printf(gstr, "<node name=\"%s\">\n", path);
159 for (list = data->interfaces; list; list = list->next) {
160 struct interface_data *iface = list->data;
162 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
165 generate_interface_xml(gstr, iface);
167 g_string_append_printf(gstr, "\t</interface>\n");
170 if (!dbus_connection_list_registered(conn, path, &children))
173 for (i = 0; children[i]; i++)
174 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
177 dbus_free_string_array(children);
180 g_string_append_printf(gstr, "</node>\n");
182 data->introspect = g_string_free(gstr, FALSE);
185 static DBusHandlerResult introspect(DBusConnection *connection,
186 DBusMessage *message, struct generic_data *data)
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;
195 if (!data->introspect)
196 generate_introspection_xml(connection, data,
197 dbus_message_get_path(message));
199 reply = dbus_message_new_method_return(message);
201 return DBUS_HANDLER_RESULT_NEED_MEMORY;
203 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
206 dbus_connection_send(connection, reply, NULL);
208 dbus_message_unref(reply);
210 return DBUS_HANDLER_RESULT_HANDLED;
213 static void generic_unregister(DBusConnection *connection, void *user_data)
215 struct generic_data *data = user_data;
217 g_free(data->introspect);
221 static struct interface_data *find_interface(GSList *interfaces,
226 for (list = interfaces; list; list = list->next) {
227 struct interface_data *iface = list->data;
228 if (!strcmp(name, iface->name))
235 static DBusHandlerResult generic_message(DBusConnection *connection,
236 DBusMessage *message, void *user_data)
238 struct generic_data *data = user_data;
239 struct interface_data *iface;
240 GDBusMethodTable *method;
241 const char *interface;
243 if (dbus_message_is_method_call(message,
244 DBUS_INTERFACE_INTROSPECTABLE,
246 return introspect(connection, message, data);
248 interface = dbus_message_get_interface(message);
250 iface = find_interface(data->interfaces, interface);
252 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
254 for (method = iface->methods; method &&
255 method->name && method->function; method++) {
258 if (dbus_message_is_method_call(message, iface->name,
259 method->name) == FALSE)
262 if (dbus_message_has_signature(message,
263 method->signature) == FALSE)
266 reply = method->function(connection, message, iface->user_data);
268 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
270 dbus_message_unref(reply);
271 return DBUS_HANDLER_RESULT_HANDLED;
274 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
276 return DBUS_HANDLER_RESULT_HANDLED;
280 return DBUS_HANDLER_RESULT_NEED_MEMORY;
282 dbus_connection_send(connection, reply, NULL);
283 dbus_message_unref(reply);
285 return DBUS_HANDLER_RESULT_HANDLED;
288 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
291 static DBusObjectPathVTable generic_table = {
292 .unregister_function = generic_unregister,
293 .message_function = generic_message,
296 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
298 struct generic_data *data = NULL;
299 char *parent_path, *slash;
301 parent_path = g_strdup(child_path);
302 slash = strrchr(parent_path, '/');
307 if (!strlen(parent_path))
310 if (!dbus_connection_get_object_path_data(conn, parent_path,
317 g_free(data->introspect);
318 data->introspect = NULL;
324 static struct generic_data *object_path_ref(DBusConnection *connection,
327 struct generic_data *data;
329 if (dbus_connection_get_object_path_data(connection, path,
330 (void *) &data) == TRUE) {
337 data = g_new0(struct generic_data, 1);
339 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
343 if (!dbus_connection_register_object_path(connection, path,
344 &generic_table, data)) {
345 g_free(data->introspect);
350 invalidate_parent_data(connection, path);
355 static void object_path_unref(DBusConnection *connection, const char *path)
357 struct generic_data *data = NULL;
359 if (dbus_connection_get_object_path_data(connection, path,
360 (void *) &data) == FALSE)
368 if (data->refcount > 0)
371 invalidate_parent_data(connection, path);
373 dbus_connection_unregister_object_path(connection, path);
376 static gboolean check_signal(DBusConnection *conn, const char *path,
377 const char *interface, const char *name,
380 struct generic_data *data = NULL;
381 struct interface_data *iface;
382 GDBusSignalTable *signal;
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",
392 iface = find_interface(data->interfaces, interface);
395 error("dbus_connection_emit_signal: %s does not implement %s",
400 for (signal = iface->signals; signal && signal->name; signal++) {
401 if (!strcmp(signal->name, name)) {
402 *args = signal->signature;
408 error("No signal named %s on interface %s", name, interface);
415 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
417 const char *interface,
424 const char *signature, *args;
426 if (!check_signal(conn, path, interface, name, &args))
429 signal = dbus_message_new_signal(path, interface, name);
431 error("Unable to allocate new %s.%s signal", interface, name);
435 ret = dbus_message_append_args_valist(signal, first, var_args);
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);
447 ret = dbus_connection_send(conn, signal, NULL);
450 dbus_message_unref(signal);
455 gboolean g_dbus_register_interface(DBusConnection *connection,
456 const char *path, const char *name,
457 GDBusMethodTable *methods,
458 GDBusSignalTable *signals,
459 GDBusPropertyTable *properties,
461 GDBusDestroyFunction destroy)
463 struct generic_data *data;
464 struct interface_data *iface;
466 data = object_path_ref(connection, path);
470 if (find_interface(data->interfaces, name))
473 iface = g_new0(struct interface_data, 1);
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;
482 data->interfaces = g_slist_append(data->interfaces, iface);
484 g_free(data->introspect);
485 data->introspect = NULL;
490 gboolean g_dbus_unregister_interface(DBusConnection *connection,
491 const char *path, const char *name)
493 struct generic_data *data = NULL;
494 struct interface_data *iface;
496 if (dbus_connection_get_object_path_data(connection, path,
497 (void *) &data) == FALSE)
503 iface = find_interface(data->interfaces, name);
507 data->interfaces = g_slist_remove(data->interfaces, iface);
510 iface->destroy(iface->user_data);
515 g_free(data->introspect);
516 data->introspect = NULL;
518 object_path_unref(connection, path);
523 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
524 const char *format, va_list args)
528 vsnprintf(str, sizeof(str), format, args);
530 return dbus_message_new_error(message, name, str);
533 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
534 const char *format, ...)
539 va_start(args, format);
541 reply = g_dbus_create_error_valist(message, name, format, args);
548 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
549 int type, va_list args)
553 reply = dbus_message_new_method_return(message);
557 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
558 dbus_message_unref(reply);
565 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
570 va_start(args, type);
572 reply = g_dbus_create_reply_valist(message, type, args);
579 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
583 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
584 dbus_message_set_no_reply(message, TRUE);
586 result = dbus_connection_send(connection, message, NULL);
588 dbus_message_unref(message);
593 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
594 DBusMessage *message, int type, va_list args)
598 reply = dbus_message_new_method_return(message);
602 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
603 dbus_message_unref(reply);
607 return g_dbus_send_message(connection, reply);
610 gboolean g_dbus_send_reply(DBusConnection *connection,
611 DBusMessage *message, int type, ...)
616 va_start(args, type);
618 result = g_dbus_send_reply_valist(connection, message, type, args);
625 gboolean g_dbus_emit_signal(DBusConnection *connection,
626 const char *path, const char *interface,
627 const char *name, int type, ...)
632 va_start(args, type);
634 result = emit_signal_valist(connection, path, interface,
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)
646 return emit_signal_valist(connection, path, interface,