summaryrefslogtreecommitdiff
path: root/plugins/service.c
blob: 1980bbfe245e80518c35b8583ed05bf61647db9a (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
449
450
451
452
453
454
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2014  Intel Corporation.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <unistd.h>

#include <glib.h>
#include <gdbus/gdbus.h>

#include "lib/bluetooth.h"
#include "lib/sdp.h"
#include "lib/uuid.h"
#include "src/log.h"
#include "src/plugin.h"
#include "src/dbus-common.h"
#include "src/error.h"
#include "src/adapter.h"
#include "src/device.h"
#include "src/service.h"
#include "src/profile.h"

#define SERVICE_INTERFACE "org.bluez.Service1"

static unsigned int service_id = 0;
static GSList *services = NULL;

struct service_data {
	struct btd_service *service;
	btd_service_state_t state;
	char *path;
	DBusMessage *connect;
	DBusMessage *disconnect;
};

static struct service_data *find_data(struct btd_service *service)
{
	GSList *l;

	for (l = services; l; l = l->next) {
		struct service_data *data = l->data;

		if (data->service == service)
			return data;
	}

	return NULL;
}

static void data_free(void *user_data)
{
	struct service_data *data = user_data;

	if (data->connect)
		dbus_message_unref(data->connect);

	if (data->disconnect)
		dbus_message_unref(data->disconnect);

	g_free(data->path);
	g_free(data);
}

static void data_remove(struct service_data *data)
{
	services = g_slist_remove(services, data);
	g_dbus_unregister_interface(btd_get_dbus_connection(), data->path,
							SERVICE_INTERFACE);
}

static DBusMessage *service_disconnect(DBusConnection *conn, DBusMessage *msg,
								void *user_data)
{
	struct service_data *data = user_data;
	int err;

	if (data->disconnect)
		return btd_error_in_progress(msg);

	data->disconnect = dbus_message_ref(msg);

	err = btd_service_disconnect(data->service);
	if (err == 0)
		return NULL;

	dbus_message_unref(data->disconnect);
	data->disconnect = NULL;

	return btd_error_failed(msg, strerror(-err));
}

static DBusMessage *service_connect(DBusConnection *conn, DBusMessage *msg,
								void *user_data)
{
	struct service_data *data = user_data;
	int err;

	if (data->connect)
		return btd_error_in_progress(msg);

	err = btd_service_connect(data->service);
	if (err < 0)
		return btd_error_failed(msg, strerror(-err));

	data->connect = dbus_message_ref(msg);

	return NULL;
}

static const char *data_get_state(struct service_data *data)
{
	struct btd_service *service = data->service;
	int err;

	data->state = btd_service_get_state(service);

	switch (data->state) {
	case BTD_SERVICE_STATE_UNAVAILABLE:
		return "unavailable";
	case BTD_SERVICE_STATE_DISCONNECTED:
		if (btd_service_is_reconnecting(service))
			return "reconnecting";
		err = btd_service_get_error(data->service);
		return err < 0 ? "error" : "disconnected";
	case BTD_SERVICE_STATE_CONNECTING:
		return btd_service_is_reconnecting(service) ? "reconnecting" :
								"connecting";
	case BTD_SERVICE_STATE_CONNECTED:
		return "connected";
	case BTD_SERVICE_STATE_DISCONNECTING:
		return "disconnecting";
	}

	return "unknown";
}

static gboolean get_device(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *user_data)
{
	struct service_data *data = user_data;
	struct btd_device *dev = btd_service_get_device(data->service);
	const char *path = btd_device_get_path(dev);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);

	return TRUE;
}

static gboolean get_state(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *user_data)
{
	struct service_data *data = user_data;
	const char *state;

	state = data_get_state(data);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &state);

	return TRUE;
}

static gboolean remote_uuid_exists(const GDBusPropertyTable *property,
								void *user_data)
{
	struct service_data *data = user_data;
	struct btd_profile *p = btd_service_get_profile(data->service);

	return p->remote_uuid != NULL;
}

static gboolean get_remote_uuid(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *user_data)
{
	struct service_data *data = user_data;
	struct btd_profile *p = btd_service_get_profile(data->service);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &p->remote_uuid);

	return TRUE;
}


static gboolean local_uuid_exists(const GDBusPropertyTable *property,
								void *user_data)
{
	struct service_data *data = user_data;
	struct btd_profile *p = btd_service_get_profile(data->service);

	return p->local_uuid != NULL;
}

static gboolean get_local_uuid(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *user_data)
{
	struct service_data *data = user_data;
	struct btd_profile *p = btd_service_get_profile(data->service);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &p->local_uuid);

	return TRUE;
}

static gboolean version_exists(const GDBusPropertyTable *property,
								void *user_data)
{
	struct service_data *data = user_data;
	uint16_t version = btd_service_get_version(data->service);

	return version != 0x0000;
}

static gboolean get_version(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *user_data)
{
	struct service_data *data = user_data;
	uint16_t version = btd_service_get_version(data->service);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, &version);

	return TRUE;
}

static gboolean get_auto_connect(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *user_data)
{
	struct service_data *data = user_data;
	dbus_bool_t value = btd_service_get_auto_connect(data->service);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &value);

	return TRUE;
}

static void set_auto_connect(const GDBusPropertyTable *property,
						DBusMessageIter *value,
						GDBusPendingPropertySet id,
						void *user_data)
{
	struct service_data *data = user_data;
	dbus_bool_t b;

	if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_BOOLEAN) {
		g_dbus_pending_property_error(id,
					ERROR_INTERFACE ".InvalidArguments",
					"Invalid arguments in method call");
		return;
	}

	dbus_message_iter_get_basic(value, &b);

	btd_service_set_auto_connect(data->service, b);

	g_dbus_pending_property_success(id);
}

static gboolean get_blocked(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *user_data)
{
	struct service_data *data = user_data;
	dbus_bool_t value = btd_service_is_blocked(data->service);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &value);

	return TRUE;
}

static void set_blocked(const GDBusPropertyTable *property,
						DBusMessageIter *value,
						GDBusPendingPropertySet id,
						void *user_data)
{
	struct service_data *data = user_data;
	dbus_bool_t b;

	if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_BOOLEAN) {
		g_dbus_pending_property_error(id,
					ERROR_INTERFACE ".InvalidArguments",
					"Invalid arguments in method call");
		return;
	}

	dbus_message_iter_get_basic(value, &b);

	btd_service_set_blocked(data->service, b);

	g_dbus_pending_property_success(id);
}

static const GDBusPropertyTable service_properties[] = {
	{ "Device", "o", get_device, NULL, NULL },
	{ "State", "s", get_state, NULL, NULL },
	{ "RemoteUUID", "s", get_remote_uuid, NULL, remote_uuid_exists },
	{ "LocalUUID", "s", get_local_uuid, NULL, local_uuid_exists },
	{ "Version", "q", get_version, NULL, version_exists },
	{ "AutoConnect", "b", get_auto_connect, set_auto_connect, NULL },
	{ "Blocked", "b", get_blocked, set_blocked, NULL },
	{ }
};

static const GDBusMethodTable service_methods[] = {
	{ GDBUS_ASYNC_METHOD("Disconnect", NULL, NULL, service_disconnect) },
	{ GDBUS_ASYNC_METHOD("Connect", NULL, NULL, service_connect) },
	{}
};

static struct service_data *service_get_data(struct btd_service *service)
{
	struct btd_device *dev = btd_service_get_device(service);
	struct btd_profile *p = btd_service_get_profile(service);
	struct service_data *data;

	data = find_data(service);
	if (data != NULL)
		return data;

	data = g_new0(struct service_data, 1);
	data->path = g_strdup_printf("%s/%s", btd_device_get_path(dev),
								p->remote_uuid);
	data->path = g_strdelimit(data->path, "-", '_');
	data->service = service;
	if (g_dbus_register_interface(btd_get_dbus_connection(),
					data->path, SERVICE_INTERFACE,
					service_methods, NULL,
					service_properties, data,
					data_free) == FALSE) {
		error("Unable to register service interface for %s",
								data->path);
		data_free(data);
		return NULL;
	}

	services = g_slist_prepend(services, data);

	DBG("%s", data->path);

	return data;
}

static void service_connected(struct service_data *data)
{
	DBusMessage *reply;

	if (!data->connect)
		return;

	reply = dbus_message_new_method_return(data->connect);
	g_dbus_send_message(btd_get_dbus_connection(), reply);
	dbus_message_unref(data->connect);
	data->connect = NULL;
}

static void service_disconnected(struct service_data *data)
{
	DBusMessage *reply;
	int err;

	if (data->disconnect) {
		reply = dbus_message_new_method_return(data->disconnect);
		g_dbus_send_message(btd_get_dbus_connection(), reply);
		dbus_message_unref(data->disconnect);
		data->connect = NULL;
	}

	if (!data->connect)
		return;

	err = btd_service_get_error(data->service);

	reply = btd_error_failed(data->connect, strerror(-err));
	g_dbus_send_message(btd_get_dbus_connection(), reply);
	dbus_message_unref(data->connect);
	data->connect = NULL;
}

static void service_cb(struct btd_service *service,
						btd_service_state_t old_state,
						btd_service_state_t new_state,
						void *user_data)
{
	struct service_data *data;

	data = service_get_data(service);
	if (!data)
		return;

	switch (new_state) {
	case BTD_SERVICE_STATE_UNAVAILABLE:
		data_remove(data);
		return;
	case BTD_SERVICE_STATE_CONNECTED:
		service_connected(data);
		break;
	case BTD_SERVICE_STATE_DISCONNECTED:
		service_disconnected(data);
		break;
	case BTD_SERVICE_STATE_CONNECTING:
		break;
	case BTD_SERVICE_STATE_DISCONNECTING:
		break;
	default:
		break;
	}

	if (data->state != btd_service_get_state(service))
		g_dbus_emit_property_changed(btd_get_dbus_connection(),
						data->path, SERVICE_INTERFACE,
						"State");
}

static int service_init(void)
{
	DBG("");

	service_id = btd_service_add_state_cb(service_cb, NULL);

	return 0;
}

static void service_exit(void)
{
	DBG("");

	btd_service_remove_state_cb(service_id);

	while (services)
		data_remove(services->data);
}

BLUETOOTH_PLUGIN_DEFINE(service, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
					service_init, service_exit)