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, "<node name=\"%s\">\n", path);
160 for (list = data->interfaces; list; list = list->next) {
161 struct interface_data *iface = list->data;
163 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
166 generate_interface_xml(gstr, iface);
168 g_string_append_printf(gstr, "\t</interface>\n");
171 if (!dbus_connection_list_registered(conn, path, &children))
174 for (i = 0; children[i]; i++)
175 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
178 dbus_free_string_array(children);
181 g_string_append_printf(gstr, "</node>\n");
183 data->introspect = g_string_free(gstr, FALSE);
186 static DBusHandlerResult introspect(DBusConnection *connection,
187 DBusMessage *message, struct generic_data *data)
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;
196 if (!data->introspect)
197 generate_introspection_xml(connection, data,
198 dbus_message_get_path(message));
200 reply = dbus_message_new_method_return(message);
202 return DBUS_HANDLER_RESULT_NEED_MEMORY;
204 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
207 dbus_connection_send(connection, reply, NULL);
209 dbus_message_unref(reply);
211 return DBUS_HANDLER_RESULT_HANDLED;
214 static void generic_unregister(DBusConnection *connection, void *user_data)
216 struct generic_data *data = user_data;
218 g_free(data->introspect);
222 static struct interface_data *find_interface(GSList *interfaces,
230 for (list = interfaces; list; list = list->next) {
231 struct interface_data *iface = list->data;
232 if (!strcmp(name, iface->name))
239 static DBusHandlerResult generic_message(DBusConnection *connection,
240 DBusMessage *message, void *user_data)
242 struct generic_data *data = user_data;
243 struct interface_data *iface;
244 GDBusMethodTable *method;
245 const char *interface;
247 if (dbus_message_is_method_call(message,
248 DBUS_INTERFACE_INTROSPECTABLE,
250 return introspect(connection, message, data);
252 interface = dbus_message_get_interface(message);
254 iface = find_interface(data->interfaces, interface);
256 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
258 for (method = iface->methods; method &&
259 method->name && method->function; method++) {
262 if (dbus_message_is_method_call(message, iface->name,
263 method->name) == FALSE)
266 if (dbus_message_has_signature(message,
267 method->signature) == FALSE)
270 reply = method->function(connection, message, iface->user_data);
272 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
274 dbus_message_unref(reply);
275 return DBUS_HANDLER_RESULT_HANDLED;
278 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
280 return DBUS_HANDLER_RESULT_HANDLED;
284 return DBUS_HANDLER_RESULT_NEED_MEMORY;
286 dbus_connection_send(connection, reply, NULL);
287 dbus_message_unref(reply);
289 return DBUS_HANDLER_RESULT_HANDLED;
292 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
295 static DBusObjectPathVTable generic_table = {
296 .unregister_function = generic_unregister,
297 .message_function = generic_message,
300 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
302 struct generic_data *data = NULL;
303 char *parent_path, *slash;
305 parent_path = g_strdup(child_path);
306 slash = strrchr(parent_path, '/');
310 if (slash == parent_path && parent_path[1] != '\0')
311 parent_path[1] = '\0';
315 if (!strlen(parent_path))
318 if (!dbus_connection_get_object_path_data(conn, parent_path,
325 g_free(data->introspect);
326 data->introspect = NULL;
332 static struct generic_data *object_path_ref(DBusConnection *connection,
335 struct generic_data *data;
337 if (dbus_connection_get_object_path_data(connection, path,
338 (void *) &data) == TRUE) {
345 data = g_new0(struct generic_data, 1);
347 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
351 if (!dbus_connection_register_object_path(connection, path,
352 &generic_table, data)) {
353 g_free(data->introspect);
358 invalidate_parent_data(connection, path);
363 static void object_path_unref(DBusConnection *connection, const char *path)
365 struct generic_data *data = NULL;
367 if (dbus_connection_get_object_path_data(connection, path,
368 (void *) &data) == FALSE)
376 if (data->refcount > 0)
379 invalidate_parent_data(connection, path);
381 dbus_connection_unregister_object_path(connection, path);
384 static gboolean check_signal(DBusConnection *conn, const char *path,
385 const char *interface, const char *name,
388 struct generic_data *data = NULL;
389 struct interface_data *iface;
390 GDBusSignalTable *signal;
393 if (!dbus_connection_get_object_path_data(conn, path,
394 (void *) &data) || !data) {
395 error("dbus_connection_emit_signal: path %s isn't registered",
400 iface = find_interface(data->interfaces, interface);
402 error("dbus_connection_emit_signal: %s does not implement %s",
407 for (signal = iface->signals; signal && signal->name; signal++) {
408 if (!strcmp(signal->name, name)) {
409 *args = signal->signature;
415 error("No signal named %s on interface %s", name, interface);
422 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
424 const char *interface,
431 const char *signature, *args;
433 if (!check_signal(conn, path, interface, name, &args))
436 signal = dbus_message_new_signal(path, interface, name);
438 error("Unable to allocate new %s.%s signal", interface, name);
442 ret = dbus_message_append_args_valist(signal, first, var_args);
446 signature = dbus_message_get_signature(signal);
447 if (strcmp(args, signature) != 0) {
448 error("%s.%s: expected signature'%s' but got '%s'",
449 interface, name, args, signature);
454 ret = dbus_connection_send(conn, signal, NULL);
457 dbus_message_unref(signal);
462 gboolean g_dbus_register_interface(DBusConnection *connection,
463 const char *path, const char *name,
464 GDBusMethodTable *methods,
465 GDBusSignalTable *signals,
466 GDBusPropertyTable *properties,
468 GDBusDestroyFunction destroy)
470 struct generic_data *data;
471 struct interface_data *iface;
473 data = object_path_ref(connection, path);
477 if (find_interface(data->interfaces, name))
480 iface = g_new0(struct interface_data, 1);
482 iface->name = g_strdup(name);
483 iface->methods = methods;
484 iface->signals = signals;
485 iface->properties = properties;
486 iface->user_data = user_data;
487 iface->destroy = destroy;
489 data->interfaces = g_slist_append(data->interfaces, iface);
491 g_free(data->introspect);
492 data->introspect = NULL;
497 gboolean g_dbus_unregister_interface(DBusConnection *connection,
498 const char *path, const char *name)
500 struct generic_data *data = NULL;
501 struct interface_data *iface;
506 if (dbus_connection_get_object_path_data(connection, path,
507 (void *) &data) == FALSE)
513 iface = find_interface(data->interfaces, name);
517 data->interfaces = g_slist_remove(data->interfaces, iface);
520 iface->destroy(iface->user_data);
525 g_free(data->introspect);
526 data->introspect = NULL;
528 object_path_unref(connection, path);
533 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
534 const char *format, va_list args)
538 vsnprintf(str, sizeof(str), format, args);
540 return dbus_message_new_error(message, name, str);
543 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
544 const char *format, ...)
549 va_start(args, format);
551 reply = g_dbus_create_error_valist(message, name, format, args);
558 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
559 int type, va_list args)
563 reply = dbus_message_new_method_return(message);
567 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
568 dbus_message_unref(reply);
575 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
580 va_start(args, type);
582 reply = g_dbus_create_reply_valist(message, type, args);
589 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
593 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
594 dbus_message_set_no_reply(message, TRUE);
596 result = dbus_connection_send(connection, message, NULL);
598 dbus_message_unref(message);
603 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
604 DBusMessage *message, int type, va_list args)
608 reply = dbus_message_new_method_return(message);
612 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
613 dbus_message_unref(reply);
617 return g_dbus_send_message(connection, reply);
620 gboolean g_dbus_send_reply(DBusConnection *connection,
621 DBusMessage *message, int type, ...)
626 va_start(args, type);
628 result = g_dbus_send_reply_valist(connection, message, type, args);
635 gboolean g_dbus_emit_signal(DBusConnection *connection,
636 const char *path, const char *interface,
637 const char *name, int type, ...)
642 va_start(args, type);
644 result = emit_signal_valist(connection, path, interface,
652 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
653 const char *path, const char *interface,
654 const char *name, int type, va_list args)
656 return emit_signal_valist(connection, path, interface,