summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_bt_profile.c
blob: 9b4171fa6b50b23ab4e36f45c5a04f24651f2848 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <dbus/dbus.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include "cras_bt_constants.h"
#include "cras_bt_device.h"
#include "cras_bt_profile.h"
#include "cras_dbus_util.h"
#include "utlist.h"

#define PROFILE_INTROSPECT_XML                                                 \
	DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                              \
	"<node>\n"                                                             \
	"  <interface name=\"org.bluez.Profile1\">\n"                          \
	"    <method name=\"Release\">\n"                                      \
	"    </method>\n"                                                      \
	"    <method name=\"NewConnection\">\n"                                \
	"      <arg name=\"device\" type=\"o\" direction=\"in\">\n"            \
	"      <arg name=\"fd\" type=\"h\" direction=\"in\">\n"                \
	"      <arg name=\"fd_properties\" type=\"a{sv}\" direction=\"in\">\n" \
	"    </method>\n"                                                      \
	"    <method name=\"RequestDisconnection\">\n"                         \
	"      <arg name=\"device\" type=\"o\" direction=\"in\">\n"            \
	"    </method>\n"                                                      \
	"    <method name=\"Cancel\">\n"                                       \
	"    </method>\n"                                                      \
	"  <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE "\">\n"           \
	"    <method name=\"Introspect\">\n"                                   \
	"      <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"            \
	"    </method>\n"                                                      \
	"  </interface>\n"                                                     \
	"</node>\n"

/* Profiles */
static struct cras_bt_profile *profiles;

static DBusHandlerResult cras_bt_profile_handle_release(DBusConnection *conn,
							DBusMessage *message,
							void *arg)
{
	DBusMessage *reply;
	const char *profile_path;
	struct cras_bt_profile *profile;

	profile_path = dbus_message_get_path(message);

	profile = cras_bt_profile_get(profile_path);
	if (!profile)
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

	syslog(LOG_ERR, "Profile %s released by bluetoothd", profile->name);
	profile->release(profile);

	reply = dbus_message_new_method_return(message);
	if (!reply)
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	if (!dbus_connection_send(conn, reply, NULL)) {
		dbus_message_unref(reply);
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	}

	dbus_message_unref(reply);

	return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusHandlerResult
cras_bt_profile_handle_new_connection(DBusConnection *conn,
				      DBusMessage *message, void *arg)
{
	DBusMessageIter message_iter;
	DBusMessage *reply;
	const char *profile_path, *object_path;
	int fd = -1;
	int err;
	struct cras_bt_profile *profile;
	struct cras_bt_device *device;

	profile_path = dbus_message_get_path(message);

	dbus_message_iter_init(message, &message_iter);
	dbus_message_iter_get_basic(&message_iter, &object_path);
	dbus_message_iter_next(&message_iter);

	if (dbus_message_iter_get_arg_type(&message_iter) !=
	    DBUS_TYPE_UNIX_FD) {
		syslog(LOG_ERR, "Argument not a valid unix file descriptor");
		goto invalid;
	}

	dbus_message_iter_get_basic(&message_iter, &fd);
	dbus_message_iter_next(&message_iter);
	if (fd < 0)
		goto invalid;

	profile = cras_bt_profile_get(profile_path);
	if (!profile)
		goto invalid;

	device = cras_bt_device_get(object_path);
	if (!device) {
		syslog(LOG_ERR, "Device %s not found at %s new connection",
		       object_path, profile_path);
		device = cras_bt_device_create(conn, object_path);
	}

	err = profile->new_connection(conn, profile, device, fd);
	if (err) {
		syslog(LOG_INFO, "%s new connection rejected", profile->name);
		close(fd);
		reply = dbus_message_new_error(
			message, "org.chromium.Cras.Error.RejectNewConnection",
			"Possibly another headset already in use");
		if (!dbus_connection_send(conn, reply, NULL))
			return DBUS_HANDLER_RESULT_NEED_MEMORY;

		dbus_message_unref(reply);
		return DBUS_HANDLER_RESULT_HANDLED;
	}

	reply = dbus_message_new_method_return(message);
	if (!reply)
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	if (!dbus_connection_send(conn, reply, NULL)) {
		dbus_message_unref(reply);
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	}

	dbus_message_unref(reply);
	return DBUS_HANDLER_RESULT_HANDLED;

invalid:
	if (fd >= 0)
		close(fd);
	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusHandlerResult
cras_bt_profile_handle_request_disconnection(DBusConnection *conn,
					     DBusMessage *message, void *arg)
{
	DBusMessageIter message_iter;
	DBusMessage *reply;
	const char *prpofile_path, *object_path;
	struct cras_bt_profile *profile;
	struct cras_bt_device *device;

	prpofile_path = dbus_message_get_path(message);

	dbus_message_iter_init(message, &message_iter);
	dbus_message_iter_get_basic(&message_iter, &object_path);
	dbus_message_iter_next(&message_iter);

	profile = cras_bt_profile_get(prpofile_path);
	if (!profile)
		goto invalid;

	device = cras_bt_device_get(object_path);
	if (!device)
		goto invalid;

	profile->request_disconnection(profile, device);

	reply = dbus_message_new_method_return(message);
	if (!reply)
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	if (!dbus_connection_send(conn, reply, NULL)) {
		dbus_message_unref(reply);
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	}

	dbus_message_unref(reply);

	return DBUS_HANDLER_RESULT_HANDLED;

invalid:
	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusHandlerResult cras_bt_profile_handle_cancel(DBusConnection *conn,
						       DBusMessage *message,
						       void *arg)
{
	DBusMessage *reply;
	const char *profile_path;
	struct cras_bt_profile *profile;

	profile_path = dbus_message_get_path(message);

	profile = cras_bt_profile_get(profile_path);
	if (!profile)
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

	profile->cancel(profile);

	reply = dbus_message_new_method_return(message);
	if (!reply)
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	if (!dbus_connection_send(conn, reply, NULL)) {
		dbus_message_unref(reply);
		return DBUS_HANDLER_RESULT_NEED_MEMORY;
	}

	dbus_message_unref(reply);

	return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusHandlerResult cras_bt_handle_profile_messages(DBusConnection *conn,
							 DBusMessage *message,
							 void *arg)
{
	if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE,
					"Introspect")) {
		DBusMessage *reply;
		const char *xml = PROFILE_INTROSPECT_XML;

		reply = dbus_message_new_method_return(message);
		if (!reply)
			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &xml,
					      DBUS_TYPE_INVALID)) {
			dbus_message_unref(reply);
			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		}
		if (!dbus_connection_send(conn, reply, NULL)) {
			dbus_message_unref(reply);
			return DBUS_HANDLER_RESULT_NEED_MEMORY;
		}

		dbus_message_unref(reply);
		return DBUS_HANDLER_RESULT_HANDLED;
	} else if (dbus_message_is_method_call(message, BLUEZ_INTERFACE_PROFILE,
					       "Release")) {
		return cras_bt_profile_handle_release(conn, message, arg);
	} else if (dbus_message_is_method_call(message, BLUEZ_INTERFACE_PROFILE,
					       "NewConnection")) {
		return cras_bt_profile_handle_new_connection(conn, message,
							     arg);
	} else if (dbus_message_is_method_call(message, BLUEZ_INTERFACE_PROFILE,
					       "RequestDisconnection")) {
		return cras_bt_profile_handle_request_disconnection(
			conn, message, arg);
	} else if (dbus_message_is_method_call(message, BLUEZ_INTERFACE_PROFILE,
					       "Cancel")) {
		return cras_bt_profile_handle_cancel(conn, message, arg);
	} else {
		syslog(LOG_ERR, "Unknown Profile message");
	}

	return DBUS_HANDLER_RESULT_HANDLED;
}

static void cras_bt_on_register_profile(DBusPendingCall *pending_call,
					void *data)
{
	DBusMessage *reply;

	reply = dbus_pending_call_steal_reply(pending_call);
	dbus_pending_call_unref(pending_call);

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
		syslog(LOG_ERR, "RegisterProfile returned error: %s",
		       dbus_message_get_error_name(reply));
	dbus_message_unref(reply);
}

int cras_bt_register_profile(DBusConnection *conn,
			     struct cras_bt_profile *profile)
{
	DBusMessage *method_call;
	DBusMessageIter message_iter;
	DBusMessageIter properties_array_iter;
	DBusPendingCall *pending_call;

	method_call = dbus_message_new_method_call(BLUEZ_SERVICE,
						   PROFILE_MANAGER_OBJ_PATH,
						   BLUEZ_PROFILE_MGMT_INTERFACE,
						   "RegisterProfile");

	if (!method_call)
		return -ENOMEM;

	dbus_message_iter_init_append(method_call, &message_iter);
	dbus_message_iter_append_basic(&message_iter, DBUS_TYPE_OBJECT_PATH,
				       &profile->object_path);
	dbus_message_iter_append_basic(&message_iter, DBUS_TYPE_STRING,
				       &profile->uuid);

	dbus_message_iter_open_container(
		&message_iter, DBUS_TYPE_ARRAY,
		DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING
			DBUS_TYPE_VARIANT_AS_STRING
				DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
		&properties_array_iter);

	if (!append_key_value(&properties_array_iter, "Name", DBUS_TYPE_STRING,
			      DBUS_TYPE_STRING_AS_STRING, &profile->name)) {
		dbus_message_unref(method_call);
		return -ENOMEM;
	}

	if (profile->record &&
	    !append_key_value(&properties_array_iter, "ServiceRecord",
			      DBUS_TYPE_STRING, DBUS_TYPE_STRING_AS_STRING,
			      &profile->record)) {
		dbus_message_unref(method_call);
		return -ENOMEM;
	}

	if (!append_key_value(&properties_array_iter, "Version",
			      DBUS_TYPE_UINT16, DBUS_TYPE_UINT16_AS_STRING,
			      &profile->version)) {
		dbus_message_unref(method_call);
		return -ENOMEM;
	}

	if (profile->role &&
	    !append_key_value(&properties_array_iter, "Role", DBUS_TYPE_STRING,
			      DBUS_TYPE_STRING_AS_STRING, &profile->role)) {
		dbus_message_unref(method_call);
		return -ENOMEM;
	}

	if (profile->features &&
	    !append_key_value(&properties_array_iter, "Features",
			      DBUS_TYPE_UINT16, DBUS_TYPE_UINT16_AS_STRING,
			      &profile->features)) {
		dbus_message_unref(method_call);
		return -ENOMEM;
	}

	dbus_message_iter_close_container(&message_iter,
					  &properties_array_iter);

	if (!dbus_connection_send_with_reply(conn, method_call, &pending_call,
					     DBUS_TIMEOUT_USE_DEFAULT)) {
		dbus_message_unref(method_call);
		return -ENOMEM;
	}

	dbus_message_unref(method_call);
	if (!pending_call)
		return -EIO;

	if (!dbus_pending_call_set_notify(
		    pending_call, cras_bt_on_register_profile, NULL, NULL)) {
		dbus_pending_call_cancel(pending_call);
		dbus_pending_call_unref(pending_call);
		syslog(LOG_ERR, "register profile fail on set notify");
		return -ENOMEM;
	}

	return 0;
}

int cras_bt_unregister_profile(DBusConnection *conn,
			       struct cras_bt_profile *profile)
{
	DBusMessage *method_call;
	DBusMessageIter message_iter;
	DBusError dbus_error;
	DBusMessage *reply;

	method_call = dbus_message_new_method_call(BLUEZ_SERVICE,
						   PROFILE_MANAGER_OBJ_PATH,
						   BLUEZ_PROFILE_MGMT_INTERFACE,
						   "UnregisterProfile");

	if (!method_call)
		return -ENOMEM;
	dbus_error_init(&dbus_error);
	dbus_message_iter_init_append(method_call, &message_iter);
	dbus_message_iter_append_basic(&message_iter, DBUS_TYPE_OBJECT_PATH,
				       &profile->object_path);
	reply = dbus_connection_send_with_reply_and_block(
		conn, method_call, DBUS_TIMEOUT_USE_DEFAULT, &dbus_error);

	if (!reply) {
		dbus_error_free(&dbus_error);
		dbus_message_unref(method_call);
		return -EIO;
	}

	dbus_message_unref(method_call);

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
		syslog(LOG_ERR, "Unregister profile returned error: %s",
		       dbus_message_get_error_name(reply));
		dbus_message_unref(reply);
		return -EIO;
	}
	dbus_message_unref(reply);
	return 0;
}

int cras_bt_register_profiles(DBusConnection *conn)
{
	struct cras_bt_profile *profile;
	int err;

	DL_FOREACH (profiles, profile) {
		err = cras_bt_register_profile(conn, profile);
		if (err)
			return err;
	}

	return 0;
}

int cras_bt_add_profile(DBusConnection *conn, struct cras_bt_profile *profile)
{
	static const DBusObjectPathVTable profile_vtable = {
		NULL, cras_bt_handle_profile_messages, NULL, NULL, NULL, NULL
	};

	DBusError dbus_error;

	dbus_error_init(&dbus_error);

	if (!dbus_connection_register_object_path(
		    conn, profile->object_path, &profile_vtable, &dbus_error)) {
		syslog(LOG_ERR, "Could not register BT profile %s: %s",
		       profile->object_path, dbus_error.message);
		dbus_error_free(&dbus_error);
		return -ENOMEM;
	}

	DL_APPEND(profiles, profile);

	return 0;
}

int cras_bt_rm_profile(DBusConnection *conn, struct cras_bt_profile *profile)
{
	DL_DELETE(profiles, profile);

	if (!dbus_connection_unregister_object_path(conn,
						    profile->object_path)) {
		syslog(LOG_ERR, "Could not unregister BT profile %s",
		       profile->object_path);
		return -ENOMEM;
	}
	return 0;
}

void cras_bt_profile_reset()
{
	struct cras_bt_profile *profile;

	DL_FOREACH (profiles, profile)
		profile->release(profile);
}

struct cras_bt_profile *cras_bt_profile_get(const char *path)
{
	struct cras_bt_profile *profile;
	DL_FOREACH (profiles, profile) {
		if (strcmp(profile->object_path, path) == 0)
			return profile;
	}

	return NULL;
}

void cras_bt_profile_on_device_disconnected(struct cras_bt_device *device)
{
	struct cras_bt_profile *profile;
	DL_FOREACH (profiles, profile)
		profile->request_disconnection(profile, device);
}