new dbus helper function: dbus_call_simple()
[ladish.git] / dbus / helpers.c
blob2b9a0dad70550f40ce9b8a0569b7523d6274b375
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
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.
28 #include <stdbool.h>
29 #include <dbus/dbus.h>
30 #include <string.h>
31 #include <assert.h>
33 #include "helpers.h"
34 #include "method.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;
46 if (value == NULL)
48 return true;
51 if (!dbus_message_iter_open_container(dict_iter_ptr, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter))
53 return false;
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);
59 return false;
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))
66 return false;
69 return true;
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))
78 return false;
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);
84 return false;
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))
91 return false;
94 return true;
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))
103 return false;
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);
109 return false;
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))
116 return false;
119 return true;
122 bool
123 dbus_call_simple(
124 const char * service,
125 const char * object,
126 const char * iface,
127 const char * method,
128 char * input_signature,
129 ...)
131 DBusMessageIter iter;
132 DBusMessage * request_ptr;
133 DBusMessage * reply_ptr;
134 const char * output_signature;
135 const char * reply_signature;
136 va_list ap;
137 bool ret;
138 void * parameter_ptr;
139 int type;
140 DBusSignatureIter sig_iter;
142 lash_info("dbus_call_simple('%s', '%s', '%s', '%s')", service, object, iface, method);
144 ret = false;
145 va_start(ap, input_signature);
147 if (!dbus_signature_validate(input_signature, NULL))
149 lash_error("input signature '%s' is invalid", input_signature);
150 goto fail;
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.");
159 goto fail;
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);
170 goto fail;
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.");
178 goto fail;
181 dbus_signature_iter_next(&sig_iter);
182 input_signature++;
185 output_signature = va_arg(ap, const char *);
187 reply_ptr = dbus_connection_send_with_reply_and_block(
188 g_dbus_connection,
189 request_ptr,
190 DBUS_CALL_DEFAULT_TIMEOUT,
191 &g_dbus_error);
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);
199 goto fail;
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 */
221 ret = true;
223 fail:
224 va_end(ap);
225 return ret;