summaryrefslogtreecommitdiff
path: root/src/dbus.c
blob: d5f493c96662fd880c3428220cd8626bef7fa261 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * feedback
 *
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>

#include "common.h"
#include "log.h"
#include "dbus.h"

/* -1 is a default timeout value, it's converted to 25*1000 internally. */
#define DBUS_REPLY_TIMEOUT	(-1)

/** extract from dbus/dbus-protocol.h
 * (GDbus use the same maximum value.)
 * Max length in bytes of a bus name, interface, or member (not object
 * path, paths are unlimited). This is limited because lots of stuff
 * is O(n) in this number, plus it would be obnoxious to type in a
 * paragraph-long method name so most likely something like that would
 * be an exploit.
 */
#define DBUS_MAXIMUM_NAME_LENGTH 255

#define SIGNAL_VIBRATOR_INITIATED "InitiateVibrator"

struct feedback_restart_callback {
	feedback_restart_cb func;
	guint feedback_id;
};

struct proxy_node {
	GDBusProxy *proxy;
	char *dest;
	char *path;
	char *interface;
};

static GList *proxy_pool;
static pthread_mutex_t dmutex = PTHREAD_MUTEX_INITIALIZER;
static int bus_init;

static dd_list *callback_list;

static int g_dbus_error_to_errno(int code)
{
	/**
	 * if device is not supported,
	 * deviced does not register the method call of the device.
	 * in this case, dbus will return UNKNOWN_METHOD error.
	 */
	/* refer to gio/gioenums.h */
	if (code == G_DBUS_ERROR_ACCESS_DENIED)
		return -EACCES;
	else if (code == G_DBUS_ERROR_UNKNOWN_METHOD)
		return -ENOTSUP;
	return -ECOMM;
}

static GVariant *append_g_variant(const char *sig, char *param[])
{
	GVariantBuilder builder;
	char *ch;
	int i;
	struct dbus_byte *bytes;

	if (!sig || !param)
		return NULL;

	g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);

	for (ch = (char *)sig, i = 0; *ch != '\0'; ++i, ++ch) {
		switch (*ch) {
		case 'i':
			g_variant_builder_add(&builder, "i", atoi(param[i]));
			break;
		case 'u':
			g_variant_builder_add(&builder, "u", strtoul(param[i], NULL, 10));
			break;
		case 't':
			g_variant_builder_add(&builder, "t", atoll(param[i]));
			break;
		case 's':
			g_variant_builder_add(&builder, "s", param[i]);
			break;
		case 'a':
			++ch;
			switch (*ch) {
			case 'y':
				++i;
				bytes = (struct dbus_byte *)param[i];
				g_variant_builder_add(&builder, "@ay",
					g_variant_new_from_data(G_VARIANT_TYPE("ay"),
					bytes->data, bytes->size, TRUE, NULL, NULL));
				break;
			default:
				break;
			}
			break;
		default:
			return NULL;
		}
	}

	return g_variant_builder_end(&builder);
}

static struct proxy_node *find_matched_proxy_node(const char *dest,
		const char *path,
		const char *interface)
{
	GList *elem;
	struct proxy_node *node;
	int plen;

	if (!dest || !path || !interface)
		return NULL;

	plen = strlen(path) + 1;

	/* find matched proxy object */
	for (elem = proxy_pool; elem; elem = elem->next) {
		node = elem->data;
		if (!node)
			continue;
		if (!strncmp(node->dest, dest, DBUS_MAXIMUM_NAME_LENGTH) &&
		    !strncmp(node->path, path, plen) &&
		    !strncmp(node->interface, interface,
			    DBUS_MAXIMUM_NAME_LENGTH))
			return node;
	}

	return NULL;
}

//LCOV_EXCL_START Not called Callback
static void on_name_vanished(GDBusConnection *connection,
		const gchar *name,
		gpointer user_data)
{
	GList *elem;
	GList *next;
	struct proxy_node *node;

	pthread_mutex_lock(&dmutex);
	for (elem = proxy_pool, next = g_list_next(elem); elem;
		elem = next, next = g_list_next(elem)) {
		node = elem->data;
		if (!node)
			continue;
		proxy_pool = g_list_delete_link(proxy_pool, elem);
		g_object_unref(node->proxy);
		free(node->dest);
		free(node->path);
		free(node->interface);
		free(node);
	}
	pthread_mutex_unlock(&dmutex);
}
//LCOV_EXCL_STOP

static GDBusConnection *get_dbus_connection(void)
{
	GError *err = NULL;
	static GDBusConnection *conn;

	conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
	if (!conn) {
		//LCOV_EXCL_START System Error
		if (err)
			_D("Fail to get dbus connection: %s", err->message);
		else
			_D("Fail to get dbus connection");
		return NULL;
		//LCOV_EXCL_STOP
	}

	return conn;
}

static GDBusProxy *get_proxy_from_proxy_pool(const char *dest,
		const char *path,
		const char *interface,
		GError **err)
{
	GDBusConnection *conn;
	GDBusProxy *proxy;
	struct proxy_node *node;

	if (!dest || !path || !interface) {
		if (err)
			g_set_error(err, G_IO_ERROR,
					G_IO_ERROR_INVALID_ARGUMENT,
					"Cannot determine destination address");
		return NULL;
	}

	/* find matched proxy node in proxy pool */
	node = find_matched_proxy_node(dest, path, interface);
	if (node)
		return node->proxy;

	conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, err);
	if (!conn)
		return NULL;

	if (!bus_init) {
		bus_init++;
		g_bus_watch_name_on_connection(conn,
			DEVICED_BUS_NAME,
			G_BUS_NAME_WATCHER_FLAGS_NONE,
			NULL,
			on_name_vanished,
			NULL,
			NULL);
	}

	proxy = g_dbus_proxy_new_sync(conn,
			G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
			NULL,      /* GDBusinterfaceinfo */
			dest,      /* bus name */
			path,      /* object path */
			interface, /* interface name */
			NULL,      /* GCancellable */
			err);
	if (!proxy)
		return NULL;

	node = malloc(sizeof(struct proxy_node));
	if (!node) {
		//LCOV_EXCL_START System Error
		g_object_unref(proxy);
		if (err)
			g_set_error(err, G_IO_ERROR,
					G_IO_ERROR_FAILED,
					"Cannot allocate proxy_node memory");
		return NULL;
		//LCOV_EXCL_STOP
	}

	node->proxy = proxy;
	node->dest = strdup(dest);
	node->path = strdup(path);
	node->interface = strdup(interface);

	proxy_pool = g_list_append(proxy_pool, node);

	return proxy;
}

int dbus_method_sync(const char *dest, const char *path,
		const char *interface, const char *method,
		const char *sig, char *param[])
{
	GDBusProxy *proxy;
	GError *err = NULL;
	GVariant *output;
	int result;

#if !GLIB_CHECK_VERSION(2, 35, 0)
	g_type_init();
#endif

	pthread_mutex_lock(&dmutex);
	proxy = get_proxy_from_proxy_pool(dest, path, interface, &err);
	if (!proxy) {
		pthread_mutex_unlock(&dmutex);
		_E("fail to get proxy from proxy pool : %s-%s (%d-%s)",
				interface, method, err->code, err->message);
		result = g_dbus_error_to_errno(err->code);
		g_clear_error(&err);
		return result;
	}

	output = g_dbus_proxy_call_sync(proxy,
			method,                       /* method name */
			append_g_variant(sig, param), /* parameters */
			G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT,           /* timeout */
			NULL,                         /* GCancellable */
			&err);
	pthread_mutex_unlock(&dmutex);

	if (!output) {
		//LCOV_EXCL_START System Error
		if (err) {
			_E("g_dbus_proxy_call_sync error : %s-%s (%d-%s)",
					interface, method, err->code, err->message);
			result = g_dbus_error_to_errno(err->code);
			g_clear_error(&err);
		} else {
			_E("g_dbus_proxy_call_sync error : %s-%s",
					interface, method);
			result = -ECOMM;
		}
		return result;
		//LCOV_EXCL_STOP
	}

	/* get output value */
	g_variant_get(output, "(i)", &result);

	g_variant_unref(output);

	return result;
}

//LCOV_EXCL_START Not called Callback
static void feedback_signal_callback(GDBusConnection *conn,
		const gchar *sender,
		const gchar *path,
		const gchar *iface,
		const gchar *signal,
		GVariant *params,
		gpointer user_data)
{
	size_t iface_len, signal_len;
	struct feedback_restart_callback *callback;
	dd_list *elem;
	int ret;

	if (!params || !sender || !path || !iface || !signal)
		return;

	iface_len = strlen(iface) + 1;
	signal_len = strlen(signal) + 1;

	if (strncmp(iface, VIBRATOR_INTERFACE_HAPTIC, iface_len))
		return;

	if (strncmp(signal, SIGNAL_VIBRATOR_INITIATED, signal_len))
		return;

	DD_LIST_FOREACH(callback_list, elem, callback) {
		if (!callback->func)
			continue;
		ret = callback->func();
		if (ret < 0)
			_E("Failed to call restart callback");
	}
}
//LCOV_EXCL_STOP

int register_signal_handler(feedback_restart_cb func)
{
	GDBusConnection *conn;
	guint feedback_id = 0;
	struct feedback_restart_callback *callback;
	dd_list *elem;

	DD_LIST_FOREACH(callback_list, elem, callback) {
		if (callback->func != func)
			continue;
		if (callback->feedback_id == 0)
			continue;

		return -EEXIST;
	}

	callback = (struct feedback_restart_callback *)malloc(sizeof(struct feedback_restart_callback));
	if (!callback) {
//LCOV_EXCL_START System Error
		_E("malloc() failed");
		return -ENOMEM;
//LCOV_EXCL_STOP
	}

	conn = get_dbus_connection();
	if (!conn) {
//LCOV_EXCL_START System Error
		free(callback);
		_E("Failed to get dbus connection");
		return -EPERM;
//LCOV_EXCL_STOP
	}

	feedback_id = g_dbus_connection_signal_subscribe(conn,
			NULL,
			VIBRATOR_INTERFACE_HAPTIC,
			NULL,
			NULL,
			NULL,
			G_DBUS_SIGNAL_FLAGS_NONE,
			feedback_signal_callback,
			NULL,
			NULL);
	if (feedback_id == 0) {
//LCOV_EXCL_START System Error
		free(callback);
		_E("Failed to subscrive bus signal");
		return -EPERM;
//LCOV_EXCL_STOP
	}

	callback->func = func;
	callback->feedback_id = feedback_id;

	DD_LIST_APPEND(callback_list, callback);

	return 0;
}

int unregister_signal_handler(feedback_restart_cb func)
{
	GDBusConnection *conn;
	struct feedback_restart_callback *callback;
	dd_list *elem;

	if (!func)
		return -EINVAL;

	conn = get_dbus_connection();
	if (!conn) {
//LCOV_EXCL_START System Error
		_E("Failed to get dbus connection");
		return -EPERM;
//LCOV_EXCL_STOP
	}

	DD_LIST_FOREACH(callback_list, elem, callback) {
		if (callback->func != func)
			continue;
		if (callback->feedback_id > 0)
			g_dbus_connection_signal_unsubscribe(conn, callback->feedback_id);
		DD_LIST_REMOVE(callback_list, callback);
		free(callback);
	}

	return 0;
}