summaryrefslogtreecommitdiff
path: root/liblazy/liblazy_dbus.c
blob: 2a1d91937d4dd99ebd06864c8bdb7a0a7558ee1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************************************************
 *                                                                         *
 *                              liblazy                                    *
 *                                                                         *
 *           Copyright (C) 2006 Holger Macht <holger@homac.de>             *
 *                                                                         *
 *              Author(s): Holger Macht <holger@homac.de>                  *
 *                                                                         *
 * This library is free software; you can redistribute it and/or modify it *
 * under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation; either version 2.1 of the License, or  *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This library is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
 * Lesser General Public License for more details.                         *
 *                                                                         *
 * You should have received a copy of the GNU Lesser General Public        *
 * License along with this library; if not, write to the Free Software     *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA           *
 * 02110-1301  USA                                                         *
 *                                                                         *
 ***************************************************************************/

#include "liblazy.h"
#include "liblazy_local.h"

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

int liblazy_dbus_send_method_call(const char *destination, const char *path,
				  const char *interface, const char *method,
				  int bus_type,
				  DBusMessage **reply,
				  int first_arg_type, va_list var_args)
{
	DBusError	dbus_error;
	DBusConnection	*dbus_connection	= NULL;
	DBusMessage	*message		= NULL;
	int		ret			= 0;

	dbus_error_init(&dbus_error);

	dbus_connection = dbus_bus_get(bus_type, &dbus_error);
	if (dbus_connection == NULL || dbus_error_is_set(&dbus_error)) {
		ERROR("Connection to dbus not ready, skipping method call %s: %s",
		      method, dbus_error.message);
		ret = LIBLAZY_ERROR_DBUS_NOT_READY;
		goto Free_Error;
	}

	message = dbus_message_new_method_call(destination, path, interface, method);
	dbus_message_append_args_valist(message, first_arg_type, var_args);

	if (reply == NULL) {
		if (!dbus_connection_send(dbus_connection, message, NULL)) {
			ERROR("Could not send method call: OOM");
			ret = LIBLAZY_ERROR_GENERAL;
		}
	} else {
		*reply = dbus_connection_send_with_reply_and_block(dbus_connection,
								   message, -1,
								   &dbus_error);
		if (dbus_error_is_set(&dbus_error)) {
			ERROR("Received error reply: %s", dbus_error.message);
			ret = LIBLAZY_ERROR_DBUS_ERROR_IS_SET;
		}
	}

	dbus_message_unref(message);
Free_Error:
	dbus_error_free(&dbus_error);
	return ret;
}

int liblazy_dbus_system_send_method_call(const char *destination, const char *path,
					 const char *interface, const char *method,
					 DBusMessage **reply,
					 int first_arg_type, ...)
{
	int	ret;
	va_list	var_args;

	va_start(var_args, first_arg_type);
	ret = liblazy_dbus_send_method_call(destination,
					    path,
					    interface,
					    method,
					    DBUS_BUS_SYSTEM,
					    reply,
					    first_arg_type,
					    var_args);
	va_end(var_args);
	return ret;
}

int liblazy_dbus_session_send_method_call(const char *destination, const char *path,
					  const char *interface, const char *method,
					  DBusMessage **reply,
					  int first_arg_type, ...)
{
	int	ret;
	va_list	var_args;

	va_start(var_args, first_arg_type);
	ret = liblazy_dbus_send_method_call(destination,
					    path,
					    interface,
					    method,
					    DBUS_BUS_SESSION,
					    reply,
					    first_arg_type,
					    var_args);
	va_end(var_args);
	return ret;
}

static int liblazy_dbus_send_signal(const char *path, const char *interface,
				    const char *name, int bus_type,
				    int first_arg_type, va_list var_args)
{
	DBusError	dbus_error;
	DBusMessage	*message;
	DBusConnection	*dbus_connection;
	int		ret = 0;

	dbus_error_init(&dbus_error);

	dbus_connection = dbus_bus_get(bus_type, &dbus_error);
	if (dbus_connection == NULL || dbus_error_is_set(&dbus_error)) {
		ERROR("Connection to dbus not ready, skipping signal %s: %s",
		      name, dbus_error.message);
		ret = LIBLAZY_ERROR_DBUS_NOT_READY;
		goto Free_Error;
	}

	message = dbus_message_new_signal(path, interface, name);
	dbus_message_append_args_valist(message, first_arg_type, var_args);

	if (!dbus_connection_send(dbus_connection, message, NULL)) {
		ERROR("Could not send signal to D-BUS system bus.");
		ret = LIBLAZY_ERROR_GENERAL;
	}

	dbus_message_unref(message);
Free_Error:
	dbus_error_free(&dbus_error);
	return ret;
}

int liblazy_dbus_system_send_signal(const char *path, const char *interface,
				    const char *name, int first_arg_type, ...)
{
	int	ret;
	va_list	var_args;

	va_start(var_args, first_arg_type);
	ret = liblazy_dbus_send_signal(path, interface, name, DBUS_BUS_SYSTEM,
				       first_arg_type, var_args);
	va_end(var_args);
	return ret;
}

int liblazy_dbus_session_send_signal(const char *path, const char *interface,
				     const char *name, int first_arg_type, ...)
{
	int	ret;
	va_list	var_args;

	va_start(var_args, first_arg_type);
	ret = liblazy_dbus_send_signal(path, interface, name, DBUS_BUS_SESSION,
				       first_arg_type, var_args);
	va_end(var_args);
	return ret;

}

int liblazy_dbus_message_get_basic_arg(DBusMessage *message, int type,
				       void *arg, int no)
{
	int		current_type;
	DBusMessageIter	iter;
	int		x		= 0;
	int		ret		= LIBLAZY_ERROR_GENERAL;
	int		_no		= 0;

	if (message == NULL) {
		ERROR("Passing in NULL for message argument invalid");
		return ret;
	}

	for (dbus_message_iter_init(message, &iter);
	     (current_type = dbus_message_iter_get_arg_type(&iter)) != DBUS_TYPE_INVALID;
	     x++, dbus_message_iter_next(&iter)) {
		if (current_type == type) {
			if (_no < no) {
				_no++;
				continue;
			} else if (_no > no)
				break;
			else {
				dbus_message_iter_get_basic(&iter, arg);
				ret = 0;
				break;
			}
		}
	}

	return ret;
}

static char **liblazy_dbus_get_strlist_from_array(DBusMessageIter *reply_iter)
{
	const char	*val;
	DBusMessageIter	iter_array;
	char		**strlist	= NULL;
	int		i		= 0;

	if (dbus_message_iter_get_arg_type(reply_iter) != DBUS_TYPE_ARRAY) {
		ERROR("Iterator doesn't contain an array at current position");
		return NULL;
	}
	
	dbus_message_iter_recurse(reply_iter, &iter_array);

	strlist = (char **)malloc(0);
	while (dbus_message_iter_get_arg_type(&iter_array) == DBUS_TYPE_STRING) {
		dbus_message_iter_get_basic(&iter_array, &val);
		strlist = realloc(strlist, sizeof(char**) * (i + 1));
		strlist[i] = strdup(val);
		dbus_message_iter_next(&iter_array);
		i++;
	}
	strlist = realloc(strlist, sizeof(char**) * (i + 1));
	strlist[i] = NULL;
	return strlist;

}

int liblazy_dbus_message_get_strlist_arg(DBusMessage *message,
					 char ***strlist, int no)
{
	int		current_type;
	DBusMessageIter	iter;
	int		x		= 0;
	int		ret		= LIBLAZY_ERROR_GENERAL;
	int		_no		= 0;

	if (message == NULL)
		return LIBLAZY_ERROR_INVALID_ARGUMENT;

	for (dbus_message_iter_init(message, &iter);
	     (current_type = dbus_message_iter_get_arg_type(&iter)) != DBUS_TYPE_INVALID;
	     x++, dbus_message_iter_next(&iter)) {
		if (current_type == DBUS_TYPE_ARRAY) {
			if (_no < no) {
				_no++;
				continue;
			} else if (_no > no)
				break;
			else {
				*strlist = liblazy_dbus_get_strlist_from_array(&iter);
				ret = 0;
				break;
			}
		}
	}
	return ret;
}