1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains code of the D-Bus helpers
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <dbus/dbus.h>
35 #include "../common/debug.h"
37 DBusConnection
* g_dbus_connection
;
38 DBusError g_dbus_error
;
40 bool dbus_maybe_add_dict_entry_string(DBusMessageIter
*dict_iter_ptr
, const char * key
, const char * value
)
42 DBusMessageIter dict_entry_iter
;
49 if (!dbus_message_iter_open_container(dict_iter_ptr
, DBUS_TYPE_DICT_ENTRY
, NULL
, &dict_entry_iter
))
54 if (!dbus_message_iter_append_basic(&dict_entry_iter
, DBUS_TYPE_STRING
, (const void *) &key
))
56 dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
);
60 method_iter_append_variant(&dict_entry_iter
, DBUS_TYPE_STRING
, &value
);
62 if (!dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
))
70 bool dbus_add_dict_entry_uint32(DBusMessageIter
* dict_iter_ptr
, const char * key
, dbus_uint32_t value
)
72 DBusMessageIter dict_entry_iter
;
74 if (!dbus_message_iter_open_container(dict_iter_ptr
, DBUS_TYPE_DICT_ENTRY
, NULL
, &dict_entry_iter
))
79 if (!dbus_message_iter_append_basic(&dict_entry_iter
, DBUS_TYPE_STRING
, (const void *) &key
))
81 dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
);
85 method_iter_append_variant(&dict_entry_iter
, DBUS_TYPE_UINT32
, &value
);
87 if (!dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
))
95 bool dbus_add_dict_entry_bool(DBusMessageIter
* dict_iter_ptr
, const char * key
, dbus_bool_t value
)
97 DBusMessageIter dict_entry_iter
;
99 if (!dbus_message_iter_open_container(dict_iter_ptr
, DBUS_TYPE_DICT_ENTRY
, NULL
, &dict_entry_iter
))
104 if (!dbus_message_iter_append_basic(&dict_entry_iter
, DBUS_TYPE_STRING
, (const void *) &key
))
106 dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
);
110 method_iter_append_variant(&dict_entry_iter
, DBUS_TYPE_BOOLEAN
, &value
);
112 if (!dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
))
122 const char * service
,
126 char * input_signature
,
129 DBusMessageIter iter
;
130 DBusMessage
* request_ptr
;
131 DBusMessage
* reply_ptr
;
132 const char * output_signature
;
133 const char * reply_signature
;
136 void * parameter_ptr
;
138 DBusSignatureIter sig_iter
;
140 //lash_info("dbus_call_simple('%s', '%s', '%s', '%s')", service, object, iface, method);
143 va_start(ap
, input_signature
);
145 if (!dbus_signature_validate(input_signature
, NULL
))
147 lash_error("input signature '%s' is invalid", input_signature
);
151 dbus_signature_iter_init(&sig_iter
, input_signature
);
153 request_ptr
= dbus_message_new_method_call(service
, object
, iface
, method
);
154 if (request_ptr
== NULL
)
156 lash_error("dbus_message_new_method_call() failed.");
160 dbus_message_iter_init_append(request_ptr
, &iter
);
162 while (*input_signature
!= '\0')
164 type
= dbus_signature_iter_get_current_type(&sig_iter
);
165 if (!dbus_type_is_basic(type
))
167 lash_error("non-basic input parameter '%c' (%d)", *input_signature
, type
);
171 parameter_ptr
= va_arg(ap
, void *);
173 if (!dbus_message_iter_append_basic(&iter
, type
, parameter_ptr
))
175 lash_error("dbus_message_iter_append_basic() failed.");
179 dbus_signature_iter_next(&sig_iter
);
183 output_signature
= va_arg(ap
, const char *);
185 reply_ptr
= dbus_connection_send_with_reply_and_block(
188 DBUS_CALL_DEFAULT_TIMEOUT
,
191 dbus_message_unref(request_ptr
);
193 if (reply_ptr
== NULL
)
195 lash_error("calling method '%s' failed, error is '%s'", method
, g_dbus_error
.message
);
196 dbus_error_free(&g_dbus_error
);
200 if (output_signature
!= NULL
)
202 reply_signature
= dbus_message_get_signature(reply_ptr
);
204 if (strcmp(reply_signature
, output_signature
) != 0)
206 lash_error("reply signature is '%s' but expected signature is '%s'", reply_signature
, output_signature
);
209 dbus_message_iter_init(reply_ptr
, &iter
);
211 while (*output_signature
++ != '\0')
213 assert(dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_INVALID
); /* we've checked the signature, this should not happen */
214 parameter_ptr
= va_arg(ap
, void *);
215 dbus_message_iter_get_basic(&iter
, parameter_ptr
);
216 dbus_message_iter_next(&iter
);
219 assert(dbus_message_iter_get_arg_type(&iter
) == DBUS_TYPE_INVALID
); /* we've checked the signature, this should not happen */
223 parameter_ptr
= va_arg(ap
, DBusMessage
**);
224 *(DBusMessage
**)parameter_ptr
= reply_ptr
;
236 compose_signal_match(
237 const char * service
,
242 static char rule
[1024];
243 snprintf(rule
, sizeof(rule
), "type='signal',sender='%s',path='%s',interface='%s',member='%s'", service
, object
, iface
, signal
);
248 dbus_register_object_signal_handler(
249 DBusConnection
* connection
,
250 const char * service
,
253 const char * const * signals
,
254 DBusHandleMessageFunction handler
,
257 const char * const * signal
;
259 for (signal
= signals
; *signal
!= NULL
; signal
++)
261 dbus_bus_add_match(connection
, compose_signal_match(service
, object
, iface
, *signal
), &g_dbus_error
);
262 if (dbus_error_is_set(&g_dbus_error
))
264 lash_error("Failed to add D-Bus match rule: %s", g_dbus_error
.message
);
265 dbus_error_free(&g_dbus_error
);
270 dbus_connection_add_filter(g_dbus_connection
, handler
, handler_data
, NULL
);
276 dbus_unregister_object_signal_handler(
277 DBusConnection
* connection
,
278 const char * service
,
281 const char * const * signals
,
282 DBusHandleMessageFunction handler
,
285 const char * const * signal
;
287 for (signal
= signals
; *signal
!= NULL
; signal
++)
289 dbus_bus_remove_match(connection
, compose_signal_match(service
, object
, iface
, *signal
), &g_dbus_error
);
290 if (dbus_error_is_set(&g_dbus_error
))
292 lash_error("Failed to add D-Bus match rule: %s", g_dbus_error
.message
);
293 dbus_error_free(&g_dbus_error
);
298 dbus_connection_remove_filter(g_dbus_connection
, handler
, handler_data
);