ladish_control: Expose all current methods
[ladish.git] / dbus / helpers.c
blob2971bb38867d8aaf5a7bf3bbde30c53c83c903fe
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 bool dbus_maybe_add_dict_entry_string(DBusMessageIter *dict_iter_ptr, const char * key, const char * value)
42 DBusMessageIter dict_entry_iter;
44 if (value == NULL)
46 return true;
49 if (!dbus_message_iter_open_container(dict_iter_ptr, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter))
51 return false;
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);
57 return false;
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))
64 return false;
67 return true;
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))
76 return false;
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);
82 return false;
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))
89 return false;
92 return true;
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))
101 return false;
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);
107 return false;
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))
114 return false;
117 return true;
120 bool
121 dbus_call_simple(
122 const char * service,
123 const char * object,
124 const char * iface,
125 const char * method,
126 char * input_signature,
127 ...)
129 DBusMessageIter iter;
130 DBusMessage * request_ptr;
131 DBusMessage * reply_ptr;
132 const char * output_signature;
133 const char * reply_signature;
134 va_list ap;
135 bool ret;
136 void * parameter_ptr;
137 int type;
138 DBusSignatureIter sig_iter;
140 //lash_info("dbus_call_simple('%s', '%s', '%s', '%s')", service, object, iface, method);
142 ret = false;
143 va_start(ap, input_signature);
145 if (!dbus_signature_validate(input_signature, NULL))
147 lash_error("input signature '%s' is invalid", input_signature);
148 goto fail;
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.");
157 goto fail;
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);
168 goto fail;
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.");
176 goto fail;
179 dbus_signature_iter_next(&sig_iter);
180 input_signature++;
183 output_signature = va_arg(ap, const char *);
185 reply_ptr = dbus_connection_send_with_reply_and_block(
186 g_dbus_connection,
187 request_ptr,
188 DBUS_CALL_DEFAULT_TIMEOUT,
189 &g_dbus_error);
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);
197 goto fail;
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 */
221 else
223 parameter_ptr = va_arg(ap, DBusMessage **);
224 *(DBusMessage **)parameter_ptr = reply_ptr;
227 ret = true;
229 fail:
230 va_end(ap);
231 return ret;
234 static
235 const char *
236 compose_signal_match(
237 const char * service,
238 const char * object,
239 const char * iface,
240 const char * signal)
242 static char rule[1024];
243 snprintf(rule, sizeof(rule), "type='signal',sender='%s',path='%s',interface='%s',member='%s'", service, object, iface, signal);
244 return rule;
247 bool
248 dbus_register_object_signal_handler(
249 DBusConnection * connection,
250 const char * service,
251 const char * object,
252 const char * iface,
253 const char * const * signals,
254 DBusHandleMessageFunction handler,
255 void * handler_data)
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);
266 return false;
270 dbus_connection_add_filter(g_dbus_connection, handler, handler_data, NULL);
272 return true;
275 bool
276 dbus_unregister_object_signal_handler(
277 DBusConnection * connection,
278 const char * service,
279 const char * object,
280 const char * iface,
281 const char * const * signals,
282 DBusHandleMessageFunction handler,
283 void * handler_data)
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);
294 return false;
298 dbus_connection_remove_filter(g_dbus_connection, handler, handler_data);
300 return true;