object.c revision fb2c49cfc95ee82f77df6b8242199599f5e9f9ee
1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2004-2007  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 <stdlib.h>
29#include <string.h>
30
31#include <glib.h>
32
33#include <dbus/dbus.h>
34
35#include "logging.h"
36
37#include "dbus-helper.h"
38
39struct generic_data {
40	void *user_data;
41	DBusObjectPathUnregisterFunction unregister_function;
42	GSList *interfaces;
43	char *introspect;
44};
45
46struct interface_data {
47	char *name;
48	DBusMethodVTable *methods;
49	DBusSignalVTable *signals;
50	DBusPropertyVTable *properties;
51};
52
53DBusHandlerResult dbus_connection_send_and_unref(DBusConnection *connection,
54							DBusMessage *message)
55{
56	if (message) {
57		dbus_connection_send(connection, message, NULL);
58		dbus_message_unref(message);
59	}
60
61	return DBUS_HANDLER_RESULT_HANDLED;
62}
63
64static void print_arguments(GString *gstr, const char *sig, const char *direction)
65{
66	int i;
67
68	for (i = 0; sig[i]; i++) {
69		char type[32];
70		int len, struct_level, dict_level;
71		gboolean complete;
72
73		complete = FALSE;
74		struct_level = dict_level = 0;
75		memset(type, 0, sizeof(type));
76
77		/* Gather enough data to have a single complete type */
78		for (len = 0; len < (sizeof(type) - 1) && sig[i]; len++, i++) {
79			switch (sig[i]){
80			case '(':
81				struct_level++;
82				break;
83			case ')':
84				struct_level--;
85				if (struct_level <= 0 && dict_level <= 0)
86					complete = TRUE;
87				break;
88			case '{':
89				dict_level++;
90				break;
91			case '}':
92				dict_level--;
93				if (struct_level <= 0 && dict_level <= 0)
94					complete = TRUE;
95				break;
96			case 'a':
97				break;
98			default:
99				if (struct_level <= 0 && dict_level <= 0)
100					complete = TRUE;
101				break;
102			}
103
104			type[len] = sig[i];
105
106			if (complete)
107				break;
108		}
109
110
111		if (direction)
112			g_string_append_printf(gstr,
113					"\t\t\t<arg type=\"%s\" direction=\"%s\"/>\n",
114					type, direction);
115		else
116			g_string_append_printf(gstr,
117					"\t\t\t<arg type=\"%s\"/>\n",
118					type);
119	}
120}
121
122static void generate_introspection_xml(DBusConnection *conn,
123					struct generic_data *data,
124					const char *path)
125{
126	GSList *list;
127	GString *gstr;
128	char **children;
129	int i;
130
131	g_free(data->introspect);
132
133	gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
134
135	g_string_append_printf(gstr, "<node name=\"%s\">\n", path);
136
137	for (list = data->interfaces; list; list = list->next) {
138		struct interface_data *iface = list->data;
139		DBusMethodVTable *method;
140		DBusSignalVTable *signal;
141		DBusPropertyVTable *property;
142
143		g_string_append_printf(gstr, "\t<interface name=\"%s\">\n", iface->name);
144
145		for (method = iface->methods; method && method->name; method++) {
146			debug("%s: adding method %s.%s",
147					path, iface->name, method->name);
148			if (!strlen(method->signature) && !strlen(method->reply))
149				g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
150							method->name);
151			else {
152				g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
153							method->name);
154				print_arguments(gstr, method->signature, "in");
155				print_arguments(gstr, method->reply, "out");
156				g_string_append_printf(gstr, "\t\t</method>\n");
157			}
158		}
159
160		for (signal = iface->signals; signal && signal->name; signal++) {
161			debug("%s: adding signal %s.%s",
162					path, iface->name, signal->name);
163			if (!strlen(signal->signature))
164				g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
165							signal->name);
166			else {
167				g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
168							signal->name);
169				print_arguments(gstr, signal->signature, NULL);
170				g_string_append_printf(gstr, "\t\t</signal>\n");
171			}
172		}
173
174		for (property = iface->properties; property && property->name; property++) {
175			debug("%s: adding property %s.%s",
176					path, iface->name, property->name);
177		}
178
179		g_string_append_printf(gstr, "\t</interface>\n");
180	}
181
182	if (!dbus_connection_list_registered(conn, path, &children))
183		goto done;
184
185	for (i = 0; children[i]; i++)
186		g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n", children[i]);
187
188	dbus_free_string_array(children);
189
190done:
191	g_string_append_printf(gstr, "</node>\n");
192
193	data->introspect = g_string_free(gstr, FALSE);
194}
195
196static DBusHandlerResult introspect(DBusConnection *connection,
197				DBusMessage *message, struct generic_data *data)
198{
199	DBusMessage *reply;
200
201	if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
202		error("Unexpected signature to introspect call");
203		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
204	}
205
206	if (!data->introspect)
207		generate_introspection_xml(connection, data,
208						dbus_message_get_path(message));
209
210	reply = dbus_message_new_method_return(message);
211	if (!reply)
212		return DBUS_HANDLER_RESULT_NEED_MEMORY;
213
214	dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
215					DBUS_TYPE_INVALID);
216
217	return dbus_connection_send_and_unref(connection, reply);
218}
219
220static void generic_unregister(DBusConnection *connection, void *user_data)
221{
222	struct generic_data *data = user_data;
223
224	if (data->unregister_function)
225		data->unregister_function(connection, data->user_data);
226
227	g_free(data->introspect);
228	g_free(data);
229}
230
231static struct interface_data *find_interface(GSList *interfaces,
232						const char *name)
233{
234	GSList *list;
235
236	for (list = interfaces; list; list = list->next) {
237		struct interface_data *iface = list->data;
238		if (!strcmp(name, iface->name))
239			return iface;
240	}
241
242	return NULL;
243}
244
245static DBusHandlerResult generic_message(DBusConnection *connection,
246					DBusMessage *message, void *user_data)
247{
248	struct generic_data *data = user_data;
249	struct interface_data *iface;
250	DBusMethodVTable *current;
251	const char *interface;
252
253	if (dbus_message_is_method_call(message,
254					DBUS_INTERFACE_INTROSPECTABLE,
255					"Introspect"))
256		return introspect(connection, message, data);
257
258	interface = dbus_message_get_interface(message);
259
260	iface = find_interface(data->interfaces, interface);
261	if (!iface)
262		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
263
264	for (current = iface->methods;
265			current->name && current->message_function; current++) {
266		if (!dbus_message_is_method_call(message, iface->name,
267							current->name))
268			continue;
269
270		if (dbus_message_has_signature(message, current->signature)) {
271			debug("%s: %s.%s()", dbus_message_get_path(message),
272					iface->name, current->name);
273			return current->message_function(connection,
274						message, data->user_data);
275		}
276	}
277
278	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
279}
280
281static DBusObjectPathVTable generic_table = {
282	.unregister_function	= generic_unregister,
283	.message_function	= generic_message,
284};
285
286static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
287{
288	struct generic_data *data = NULL;
289	char *parent_path, *slash;
290
291	parent_path = g_strdup(child_path);
292	slash = strrchr(parent_path, '/');
293	if (!slash)
294		goto done;
295
296	*slash = '\0';
297	if (!strlen(parent_path))
298		goto done;
299
300	if (!dbus_connection_get_object_path_data(conn, parent_path,
301							(void *) &data))
302		goto done;
303
304	if (!data)
305		goto done;
306
307	g_free(data->introspect);
308	data->introspect = NULL;
309
310done:
311	g_free(parent_path);
312}
313
314dbus_bool_t dbus_connection_create_object_path(DBusConnection *connection,
315					const char *path, void *user_data,
316					DBusObjectPathUnregisterFunction function)
317{
318	struct generic_data *data;
319
320	data = g_new0(struct generic_data, 1);
321
322	data->user_data = user_data;
323	data->unregister_function = function;
324
325	data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
326
327	if (!dbus_connection_register_object_path(connection, path,
328						&generic_table, data)) {
329		g_free(data);
330		return FALSE;
331	}
332
333	invalidate_parent_data(connection, path);
334
335	return TRUE;
336}
337
338dbus_bool_t dbus_connection_destroy_object_path(DBusConnection *connection,
339							const char *path)
340{
341	invalidate_parent_data(connection, path);
342
343	return dbus_connection_unregister_object_path(connection, path);
344}
345
346dbus_bool_t dbus_connection_get_object_user_data(DBusConnection *connection,
347							const char *path,
348							void **data_p)
349{
350	struct generic_data *data = NULL;
351
352	if (!dbus_connection_get_object_path_data(connection, path,
353						(void *) &data) || !data)
354		return FALSE;
355
356	*data_p = data->user_data;
357
358	return TRUE;
359}
360
361dbus_bool_t dbus_connection_register_interface(DBusConnection *connection,
362					const char *path, const char *name,
363					DBusMethodVTable *methods,
364					DBusSignalVTable *signals,
365					DBusPropertyVTable *properties)
366{
367	struct generic_data *data = NULL;
368	struct interface_data *iface;
369
370	if (!dbus_connection_get_object_path_data(connection, path,
371						(void *) &data) || !data)
372		return FALSE;
373
374	if (find_interface(data->interfaces, name))
375		return FALSE;
376
377	iface = g_new0(struct interface_data, 1);
378
379	iface->name = g_strdup(name);
380	iface->methods = methods;
381	iface->signals = signals;
382	iface->properties = properties;
383
384	data->interfaces = g_slist_append(data->interfaces, iface);
385
386	g_free(data->introspect);
387	data->introspect = NULL;
388
389	return TRUE;
390}
391
392dbus_bool_t dbus_connection_unregister_interface(DBusConnection *connection,
393					const char *path, const char *name)
394{
395	struct generic_data *data = NULL;
396	struct interface_data *iface;
397
398	if (!dbus_connection_get_object_path_data(connection, path,
399						(void *) &data) || !data)
400		return FALSE;
401
402	iface = find_interface(data->interfaces, name);
403	if (!iface)
404		return FALSE;
405
406	data->interfaces = g_slist_remove(data->interfaces, iface);
407
408	g_free(iface->name);
409	g_free(iface);
410
411	g_free(data->introspect);
412	data->introspect = NULL;
413
414	return TRUE;
415}
416
417void dbus_message_iter_append_dict_entry(DBusMessageIter *dict,
418					const char *key, int type, void *val)
419{
420	DBusMessageIter entry;
421	DBusMessageIter value;
422	char *sig;
423
424	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL, &entry);
425
426	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
427
428	switch (type) {
429	case DBUS_TYPE_STRING:
430		sig = DBUS_TYPE_STRING_AS_STRING;
431		break;
432	case DBUS_TYPE_INT16:
433		sig = DBUS_TYPE_INT16_AS_STRING;
434		break;
435	case DBUS_TYPE_UINT32:
436		sig = DBUS_TYPE_UINT32_AS_STRING;
437		break;
438	case DBUS_TYPE_BOOLEAN:
439		sig = DBUS_TYPE_BOOLEAN_AS_STRING;
440		break;
441	default:
442		sig = DBUS_TYPE_VARIANT_AS_STRING;
443		break;
444	}
445
446	dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, sig, &value);
447
448	dbus_message_iter_append_basic(&value, type, val);
449
450	dbus_message_iter_close_container(&entry, &value);
451
452	dbus_message_iter_close_container(dict, &entry);
453}
454
455dbus_bool_t dbus_connection_emit_signal_valist(DBusConnection *conn,
456						const char *path,
457						const char *interface,
458						const char *name,
459						int first,
460						va_list var_args)
461{
462	struct generic_data *data = NULL;
463	struct interface_data *iface;
464	DBusSignalVTable *sig_data;
465	DBusMessage *signal;
466	dbus_bool_t ret;
467	const char *signature, *args = NULL;
468
469	if (!dbus_connection_get_object_path_data(conn, path,
470					(void *) &data) || !data) {
471		error("dbus_connection_emit_signal: path %s isn't registered",
472				path);
473		return FALSE;
474	}
475
476	iface = find_interface(data->interfaces, interface);
477
478	if (!iface) {
479		error("dbus_connection_emit_signal: %s does not implement %s",
480				path, interface);
481		return FALSE;
482	}
483
484	for (sig_data = iface->signals; sig_data && sig_data->name; sig_data++) {
485		if (!strcmp(sig_data->name, name)) {
486			args = sig_data->signature;
487			break;
488		}
489	}
490
491	if (!args) {
492		error("No signal named %s on interface %s", name, interface);
493		return FALSE;
494	}
495
496	signal = dbus_message_new_signal(path, interface, name);
497	if (!signal) {
498		error("Unable to allocate new %s.%s signal", interface,  name);
499		return FALSE;
500	}
501
502	ret = dbus_message_append_args_valist(signal, first, var_args);
503	if (!ret)
504		goto fail;
505
506	signature = dbus_message_get_signature(signal);
507	if (strcmp(args, signature) != 0) {
508		error("%s.%s: expected signature'%s' but got '%s'",
509				interface, name, args, signature);
510		ret = FALSE;
511		goto fail;
512	}
513
514	ret = dbus_connection_send(conn, signal, NULL);
515fail:
516	dbus_message_unref(signal);
517
518	return ret;
519}
520
521dbus_bool_t dbus_connection_emit_signal(DBusConnection *conn, const char *path,
522					const char *interface, const char *name,
523					int first, ...)
524{
525	dbus_bool_t ret;
526	va_list var_args;
527
528	va_start(var_args, first);
529	ret = dbus_connection_emit_signal_valist(conn, path, interface, name,
530							first, var_args);
531	va_end(var_args);
532
533	return ret;
534}
535
536