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 #define DBUS_CALL_DEFAULT_TIMEOUT 1000 // in milliseconds
42 bool dbus_maybe_add_dict_entry_string(DBusMessageIter
*dict_iter_ptr
, const char * key
, const char * value
)
44 DBusMessageIter dict_entry_iter
;
51 if (!dbus_message_iter_open_container(dict_iter_ptr
, DBUS_TYPE_DICT_ENTRY
, NULL
, &dict_entry_iter
))
56 if (!dbus_message_iter_append_basic(&dict_entry_iter
, DBUS_TYPE_STRING
, (const void *) &key
))
58 dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
);
62 method_iter_append_variant(&dict_entry_iter
, DBUS_TYPE_STRING
, &value
);
64 if (!dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
))
72 bool dbus_add_dict_entry_uint32(DBusMessageIter
* dict_iter_ptr
, const char * key
, dbus_uint32_t value
)
74 DBusMessageIter dict_entry_iter
;
76 if (!dbus_message_iter_open_container(dict_iter_ptr
, DBUS_TYPE_DICT_ENTRY
, NULL
, &dict_entry_iter
))
81 if (!dbus_message_iter_append_basic(&dict_entry_iter
, DBUS_TYPE_STRING
, (const void *) &key
))
83 dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
);
87 method_iter_append_variant(&dict_entry_iter
, DBUS_TYPE_UINT32
, &value
);
89 if (!dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
))
97 bool dbus_add_dict_entry_bool(DBusMessageIter
* dict_iter_ptr
, const char * key
, dbus_bool_t value
)
99 DBusMessageIter dict_entry_iter
;
101 if (!dbus_message_iter_open_container(dict_iter_ptr
, DBUS_TYPE_DICT_ENTRY
, NULL
, &dict_entry_iter
))
106 if (!dbus_message_iter_append_basic(&dict_entry_iter
, DBUS_TYPE_STRING
, (const void *) &key
))
108 dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
);
112 method_iter_append_variant(&dict_entry_iter
, DBUS_TYPE_BOOLEAN
, &value
);
114 if (!dbus_message_iter_close_container(dict_iter_ptr
, &dict_entry_iter
))
124 const char * service
,
128 char * input_signature
,
131 DBusMessageIter iter
;
132 DBusMessage
* request_ptr
;
133 DBusMessage
* reply_ptr
;
134 const char * output_signature
;
135 const char * reply_signature
;
138 void * parameter_ptr
;
140 DBusSignatureIter sig_iter
;
142 lash_info("dbus_call_simple('%s', '%s', '%s', '%s')", service
, object
, iface
, method
);
145 va_start(ap
, input_signature
);
147 if (!dbus_signature_validate(input_signature
, NULL
))
149 lash_error("input signature '%s' is invalid", input_signature
);
153 dbus_signature_iter_init(&sig_iter
, input_signature
);
155 request_ptr
= dbus_message_new_method_call(service
, object
, iface
, method
);
156 if (request_ptr
== NULL
)
158 lash_error("dbus_message_new_method_call() failed.");
162 dbus_message_iter_init_append(request_ptr
, &iter
);
164 while (*input_signature
!= '\0')
166 type
= dbus_signature_iter_get_current_type(&sig_iter
);
167 if (!dbus_type_is_basic(type
))
169 lash_error("non-basic input parameter '%c' (%d)", *input_signature
, type
);
173 parameter_ptr
= va_arg(ap
, void *);
175 if (!dbus_message_iter_append_basic(&iter
, type
, parameter_ptr
))
177 lash_error("dbus_message_iter_append_basic() failed.");
181 dbus_signature_iter_next(&sig_iter
);
185 output_signature
= va_arg(ap
, const char *);
187 reply_ptr
= dbus_connection_send_with_reply_and_block(
190 DBUS_CALL_DEFAULT_TIMEOUT
,
193 dbus_message_unref(request_ptr
);
195 if (reply_ptr
== NULL
)
197 lash_error("calling method '%s' failed, error is '%s'", method
, g_dbus_error
.message
);
198 dbus_error_free(&g_dbus_error
);
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 */