• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2007  Nokia Corporation
6  *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <stdint.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <ctype.h>
39 #include <signal.h>
40 
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/hci.h>
43 #include <bluetooth/hci_lib.h>
44 #include <bluetooth/rfcomm.h>
45 #include <bluetooth/sdp.h>
46 #include <bluetooth/sdp_lib.h>
47 
48 #include <glib.h>
49 #include <dbus/dbus.h>
50 #include <gdbus.h>
51 
52 #include "glib-helper.h"
53 #include "btio.h"
54 #include "../src/manager.h"
55 #include "../src/adapter.h"
56 #include "../src/device.h"
57 
58 #include "logging.h"
59 #include "textfile.h"
60 #include "ipc.h"
61 #include "device.h"
62 #include "error.h"
63 #include "avdtp.h"
64 #include "a2dp.h"
65 #include "headset.h"
66 #include "gateway.h"
67 #include "sink.h"
68 #include "source.h"
69 #include "control.h"
70 #include "manager.h"
71 #include "sdpd.h"
72 #include "telephony.h"
73 
74 typedef enum {
75 	HEADSET	= 1 << 0,
76 	GATEWAY	= 1 << 1,
77 	SINK	= 1 << 2,
78 	SOURCE	= 1 << 3,
79 	CONTROL	= 1 << 4,
80 	TARGET	= 1 << 5,
81 	INVALID	= 1 << 6
82 } audio_service_type;
83 
84 typedef enum {
85 		GENERIC_AUDIO = 0,
86 		ADVANCED_AUDIO,
87 		AV_REMOTE,
88 		GET_RECORDS
89 } audio_sdp_state_t;
90 
91 struct audio_adapter {
92 	struct btd_adapter *btd_adapter;
93 	uint32_t hsp_ag_record_id;
94 	uint32_t hfp_ag_record_id;
95 	uint32_t hfp_hs_record_id;
96 	GIOChannel *hsp_ag_server;
97 	GIOChannel *hfp_ag_server;
98 	GIOChannel *hfp_hs_server;
99 	gint ref;
100 };
101 
102 static gboolean auto_connect = TRUE;
103 static int max_connected_headsets = 1;
104 static DBusConnection *connection = NULL;
105 static GKeyFile *config = NULL;
106 static GSList *adapters = NULL;
107 static GSList *devices = NULL;
108 
109 static struct enabled_interfaces enabled = {
110 	.hfp		= TRUE,
111 	.headset	= TRUE,
112 	.gateway	= FALSE,
113 	.sink		= TRUE,
114 	.source		= FALSE,
115 	.control	= TRUE,
116 };
117 
find_adapter(GSList * list,struct btd_adapter * btd_adapter)118 static struct audio_adapter *find_adapter(GSList *list,
119 					struct btd_adapter *btd_adapter)
120 {
121 	GSList *l;
122 
123 	for (l = list; l; l = l->next) {
124 		struct audio_adapter *adapter = l->data;
125 
126 		if (adapter->btd_adapter == btd_adapter)
127 			return adapter;
128 	}
129 
130 	return NULL;
131 }
132 
server_is_enabled(bdaddr_t * src,uint16_t svc)133 gboolean server_is_enabled(bdaddr_t *src, uint16_t svc)
134 {
135 	switch (svc) {
136 	case HEADSET_SVCLASS_ID:
137 		return enabled.headset;
138 	case HEADSET_AGW_SVCLASS_ID:
139 		return FALSE;
140 	case HANDSFREE_SVCLASS_ID:
141 		return enabled.headset && enabled.hfp;
142 	case HANDSFREE_AGW_SVCLASS_ID:
143 		return enabled.gateway;
144 	case AUDIO_SINK_SVCLASS_ID:
145 		return enabled.sink;
146 	case AUDIO_SOURCE_SVCLASS_ID:
147 		return enabled.source;
148 	case AV_REMOTE_TARGET_SVCLASS_ID:
149 	case AV_REMOTE_SVCLASS_ID:
150 		return enabled.control;
151 	default:
152 		return FALSE;
153 	}
154 }
155 
handle_uuid(const char * uuidstr,struct audio_device * device)156 static void handle_uuid(const char *uuidstr, struct audio_device *device)
157 {
158 	uuid_t uuid;
159 	uint16_t uuid16;
160 
161 	if (bt_string2uuid(&uuid, uuidstr) < 0) {
162 		error("%s not detected as an UUID-128", uuidstr);
163 		return;
164 	}
165 
166 	if (!sdp_uuid128_to_uuid(&uuid) && uuid.type != SDP_UUID16) {
167 		error("Could not convert %s to a UUID-16", uuidstr);
168 		return;
169 	}
170 
171 	uuid16 = uuid.value.uuid16;
172 
173 	if (!server_is_enabled(&device->src, uuid16)) {
174 		debug("audio handle_uuid: server not enabled for %s (0x%04x)",
175 				uuidstr, uuid16);
176 		return;
177 	}
178 
179 	switch (uuid16) {
180 	case HEADSET_SVCLASS_ID:
181 		debug("Found Headset record");
182 		if (device->headset)
183 			headset_update(device, uuid16, uuidstr);
184 		else
185 			device->headset = headset_init(device, uuid16,
186 							uuidstr);
187 		break;
188 	case HEADSET_AGW_SVCLASS_ID:
189 		debug("Found Headset AG record");
190 		break;
191 	case HANDSFREE_SVCLASS_ID:
192 		debug("Found Handsfree record");
193 		if (device->headset)
194 			headset_update(device, uuid16, uuidstr);
195 		else
196 			device->headset = headset_init(device, uuid16,
197 							uuidstr);
198 		break;
199 	case HANDSFREE_AGW_SVCLASS_ID:
200 		debug("Found Handsfree AG record");
201 		if (device->gateway == NULL)
202 			device->gateway = gateway_init(device);
203 		break;
204 	case AUDIO_SINK_SVCLASS_ID:
205 		debug("Found Audio Sink");
206 		if (device->sink == NULL)
207 			device->sink = sink_init(device);
208 		break;
209 	case AUDIO_SOURCE_SVCLASS_ID:
210 		debug("Found Audio Source");
211 		if (device->source == NULL)
212 			device->source = source_init(device);
213 		break;
214 	case AV_REMOTE_SVCLASS_ID:
215 	case AV_REMOTE_TARGET_SVCLASS_ID:
216 		debug("Found AV %s", uuid16 == AV_REMOTE_SVCLASS_ID ?
217 							"Remote" : "Target");
218 		if (device->control)
219 			control_update(device, uuid16);
220 		else
221 			device->control = control_init(device, uuid16);
222 		if (device->sink && sink_is_active(device))
223 			avrcp_connect(device);
224 		break;
225 	default:
226 		debug("Unrecognized UUID: 0x%04X", uuid16);
227 		break;
228 	}
229 }
230 
hsp_ag_record(uint8_t ch)231 static sdp_record_t *hsp_ag_record(uint8_t ch)
232 {
233 	sdp_list_t *svclass_id, *pfseq, *apseq, *root;
234 	uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
235 	uuid_t l2cap_uuid, rfcomm_uuid;
236 	sdp_profile_desc_t profile;
237 	sdp_record_t *record;
238 	sdp_list_t *aproto, *proto[2];
239 	sdp_data_t *channel;
240 
241 	record = sdp_record_alloc();
242 	if (!record)
243 		return NULL;
244 
245 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
246 	root = sdp_list_append(0, &root_uuid);
247 	sdp_set_browse_groups(record, root);
248 
249 	sdp_uuid16_create(&svclass_uuid, HEADSET_AGW_SVCLASS_ID);
250 	svclass_id = sdp_list_append(0, &svclass_uuid);
251 	sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
252 	svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
253 	sdp_set_service_classes(record, svclass_id);
254 
255 	sdp_uuid16_create(&profile.uuid, HEADSET_PROFILE_ID);
256 	profile.version = 0x0102;
257 	pfseq = sdp_list_append(0, &profile);
258 	sdp_set_profile_descs(record, pfseq);
259 
260 	sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
261 	proto[0] = sdp_list_append(0, &l2cap_uuid);
262 	apseq = sdp_list_append(0, proto[0]);
263 
264 	sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
265 	proto[1] = sdp_list_append(0, &rfcomm_uuid);
266 	channel = sdp_data_alloc(SDP_UINT8, &ch);
267 	proto[1] = sdp_list_append(proto[1], channel);
268 	apseq = sdp_list_append(apseq, proto[1]);
269 
270 	aproto = sdp_list_append(0, apseq);
271 	sdp_set_access_protos(record, aproto);
272 
273 	sdp_set_info_attr(record, "Headset Audio Gateway", 0, 0);
274 
275 	sdp_data_free(channel);
276 	sdp_list_free(proto[0], 0);
277 	sdp_list_free(proto[1], 0);
278 	sdp_list_free(apseq, 0);
279 	sdp_list_free(pfseq, 0);
280 	sdp_list_free(aproto, 0);
281 	sdp_list_free(root, 0);
282 	sdp_list_free(svclass_id, 0);
283 
284 	return record;
285 }
286 
hfp_hs_record(uint8_t ch)287 static sdp_record_t *hfp_hs_record(uint8_t ch)
288 {
289 	sdp_list_t *svclass_id, *pfseq, *apseq, *root;
290 	uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
291 	uuid_t l2cap_uuid, rfcomm_uuid;
292 	sdp_profile_desc_t profile;
293 	sdp_record_t *record;
294 	sdp_list_t *aproto, *proto[2];
295 	sdp_data_t *channel;
296 
297 	record = sdp_record_alloc();
298 	if (!record)
299 		return NULL;
300 
301 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
302 	root = sdp_list_append(0, &root_uuid);
303 	sdp_set_browse_groups(record, root);
304 
305 	sdp_uuid16_create(&svclass_uuid, HANDSFREE_SVCLASS_ID);
306 	svclass_id = sdp_list_append(0, &svclass_uuid);
307 	sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
308 	svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
309 	sdp_set_service_classes(record, svclass_id);
310 
311 	sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
312 	profile.version = 0x0100;
313 	pfseq = sdp_list_append(0, &profile);
314 	sdp_set_profile_descs(record, pfseq);
315 
316 	sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
317 	proto[0] = sdp_list_append(0, &l2cap_uuid);
318 	apseq = sdp_list_append(0, proto[0]);
319 
320 	sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
321 	proto[1] = sdp_list_append(0, &rfcomm_uuid);
322 	channel = sdp_data_alloc(SDP_UINT8, &ch);
323 	proto[1] = sdp_list_append(proto[1], channel);
324 	apseq = sdp_list_append(apseq, proto[1]);
325 
326 	aproto = sdp_list_append(0, apseq);
327 	sdp_set_access_protos(record, aproto);
328 
329 	sdp_set_info_attr(record, "Hands-Free", 0, 0);
330 
331 	sdp_data_free(channel);
332 	sdp_list_free(proto[0], 0);
333 	sdp_list_free(proto[1], 0);
334 	sdp_list_free(apseq, 0);
335 	sdp_list_free(pfseq, 0);
336 	sdp_list_free(aproto, 0);
337 	sdp_list_free(root, 0);
338 	sdp_list_free(svclass_id, 0);
339 
340 	return record;
341 }
342 
hfp_ag_record(uint8_t ch,uint32_t feat)343 static sdp_record_t *hfp_ag_record(uint8_t ch, uint32_t feat)
344 {
345 	sdp_list_t *svclass_id, *pfseq, *apseq, *root;
346 	uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
347 	uuid_t l2cap_uuid, rfcomm_uuid;
348 	sdp_profile_desc_t profile;
349 	sdp_list_t *aproto, *proto[2];
350 	sdp_record_t *record;
351 	sdp_data_t *channel, *features;
352 	uint8_t netid = 0x01;
353 	uint16_t sdpfeat;
354 	sdp_data_t *network = sdp_data_alloc(SDP_UINT8, &netid);
355 
356 	record = sdp_record_alloc();
357 	if (!record)
358 		return NULL;
359 
360 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
361 	root = sdp_list_append(0, &root_uuid);
362 	sdp_set_browse_groups(record, root);
363 
364 	sdp_uuid16_create(&svclass_uuid, HANDSFREE_AGW_SVCLASS_ID);
365 	svclass_id = sdp_list_append(0, &svclass_uuid);
366 	sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
367 	svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
368 	sdp_set_service_classes(record, svclass_id);
369 
370 	sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
371 	profile.version = 0x0105;
372 	pfseq = sdp_list_append(0, &profile);
373 	sdp_set_profile_descs(record, pfseq);
374 
375 	sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
376 	proto[0] = sdp_list_append(0, &l2cap_uuid);
377 	apseq = sdp_list_append(0, proto[0]);
378 
379 	sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
380 	proto[1] = sdp_list_append(0, &rfcomm_uuid);
381 	channel = sdp_data_alloc(SDP_UINT8, &ch);
382 	proto[1] = sdp_list_append(proto[1], channel);
383 	apseq = sdp_list_append(apseq, proto[1]);
384 
385 	sdpfeat = (uint16_t) feat & 0xF;
386 	features = sdp_data_alloc(SDP_UINT16, &sdpfeat);
387 	sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
388 
389 	aproto = sdp_list_append(0, apseq);
390 	sdp_set_access_protos(record, aproto);
391 
392 	sdp_set_info_attr(record, "Hands-Free Audio Gateway", 0, 0);
393 
394 	sdp_attr_add(record, SDP_ATTR_EXTERNAL_NETWORK, network);
395 
396 	sdp_data_free(channel);
397 	sdp_list_free(proto[0], 0);
398 	sdp_list_free(proto[1], 0);
399 	sdp_list_free(apseq, 0);
400 	sdp_list_free(pfseq, 0);
401 	sdp_list_free(aproto, 0);
402 	sdp_list_free(root, 0);
403 	sdp_list_free(svclass_id, 0);
404 
405 	return record;
406 }
407 
headset_auth_cb(DBusError * derr,void * user_data)408 static void headset_auth_cb(DBusError *derr, void *user_data)
409 {
410 	struct audio_device *device = user_data;
411 	GError *err = NULL;
412 	GIOChannel *io;
413 
414 	if (derr && dbus_error_is_set(derr)) {
415 		error("Access denied: %s", derr->message);
416 		headset_set_state(device, HEADSET_STATE_DISCONNECTED);
417 		return;
418 	}
419 
420 	io = headset_get_rfcomm(device);
421 
422 	if (!bt_io_accept(io, headset_connect_cb, device, NULL, &err)) {
423 		error("bt_io_accept: %s", err->message);
424 		g_error_free(err);
425 		headset_set_state(device, HEADSET_STATE_DISCONNECTED);
426 		return;
427 	}
428 }
429 
ag_confirm(GIOChannel * chan,gpointer data)430 static void ag_confirm(GIOChannel *chan, gpointer data)
431 {
432 	const char *server_uuid, *remote_uuid;
433 	struct audio_device *device;
434 	gboolean hfp_active;
435 	bdaddr_t src, dst;
436 	int perr;
437 	GError *err = NULL;
438 	uint8_t ch;
439 
440 	bt_io_get(chan, BT_IO_RFCOMM, &err,
441 			BT_IO_OPT_SOURCE_BDADDR, &src,
442 			BT_IO_OPT_DEST_BDADDR, &dst,
443 			BT_IO_OPT_CHANNEL, &ch,
444 			BT_IO_OPT_INVALID);
445 	if (err) {
446 		error("%s", err->message);
447 		g_error_free(err);
448 		goto drop;
449 	}
450 
451 	if (ch == DEFAULT_HS_AG_CHANNEL) {
452 		hfp_active = FALSE;
453 		server_uuid = HSP_AG_UUID;
454 		remote_uuid = HSP_HS_UUID;
455 	} else {
456 		hfp_active = TRUE;
457 		server_uuid = HFP_AG_UUID;
458 		remote_uuid = HFP_HS_UUID;
459 	}
460 
461 	device = manager_get_device(&src, &dst, TRUE);
462 	if (!device)
463 		goto drop;
464 
465 	if (!manager_allow_headset_connection(device)) {
466 		debug("Refusing headset: too many existing connections");
467 		goto drop;
468 	}
469 
470 	if (!device->headset) {
471 		btd_device_add_uuid(device->btd_dev, remote_uuid);
472 		if (!device->headset)
473 			goto drop;
474 	}
475 
476 	if (headset_get_state(device) > HEADSET_STATE_DISCONNECTED) {
477 		debug("Refusing new connection since one already exists");
478 		goto drop;
479 	}
480 
481 	set_hfp_active(device, hfp_active);
482 
483 	if (headset_connect_rfcomm(device, chan) < 0) {
484 		error("headset_connect_rfcomm failed");
485 		goto drop;
486 	}
487 
488 	headset_set_state(device, HEADSET_STATE_CONNECT_IN_PROGRESS);
489 
490 	perr = audio_device_request_authorization(device, server_uuid,
491 						headset_auth_cb, device);
492 	if (perr < 0) {
493 		debug("Authorization denied: %s", strerror(-perr));
494 		headset_set_state(device, HEADSET_STATE_DISCONNECTED);
495 		return;
496 	}
497 
498 	device->auto_connect = auto_connect;
499 
500 	return;
501 
502 drop:
503 	g_io_channel_shutdown(chan, TRUE, NULL);
504 }
505 
gateway_auth_cb(DBusError * derr,void * user_data)506 static void gateway_auth_cb(DBusError *derr, void *user_data)
507 {
508 	struct audio_device *device = user_data;
509 
510 	if (derr && dbus_error_is_set(derr))
511 		error("Access denied: %s", derr->message);
512 	else {
513 		char ag_address[18];
514 
515 		ba2str(&device->dst, ag_address);
516 		debug("Accepted AG connection from %s for %s",
517 			ag_address, device->path);
518 
519 		gateway_start_service(device);
520 	}
521 }
522 
hf_io_cb(GIOChannel * chan,gpointer data)523 static void hf_io_cb(GIOChannel *chan, gpointer data)
524 {
525 	bdaddr_t src, dst;
526 	GError *err = NULL;
527 	uint8_t ch;
528 	const char *server_uuid, *remote_uuid;
529 	uint16_t svclass;
530 	struct audio_device *device;
531 	int perr;
532 
533 	bt_io_get(chan, BT_IO_RFCOMM, &err,
534 			BT_IO_OPT_SOURCE_BDADDR, &src,
535 			BT_IO_OPT_DEST_BDADDR, &dst,
536 			BT_IO_OPT_CHANNEL, &ch,
537 			BT_IO_OPT_INVALID);
538 
539 	if (err) {
540 		error("%s", err->message);
541 		g_error_free(err);
542 		return;
543 	}
544 
545 	server_uuid = HFP_HS_UUID;
546 	remote_uuid = HFP_AG_UUID;
547 	svclass = HANDSFREE_AGW_SVCLASS_ID;
548 
549 	device = manager_get_device(&src, &dst, TRUE);
550 	if (!device)
551 		goto drop;
552 
553 	if (!device->gateway) {
554 		btd_device_add_uuid(device->btd_dev, remote_uuid);
555 		if (!device->gateway)
556 			goto drop;
557 	}
558 
559 	if (gateway_is_connected(device)) {
560 		debug("Refusing new connection since one already exists");
561 		goto drop;
562 	}
563 
564 	if (gateway_connect_rfcomm(device, chan) < 0) {
565 		error("Allocating new GIOChannel failed!");
566 		goto drop;
567 	}
568 
569 	perr = audio_device_request_authorization(device, server_uuid,
570 						gateway_auth_cb, device);
571 	if (perr < 0) {
572 		debug("Authorization denied!");
573 		goto drop;
574 	}
575 
576 	return;
577 
578 drop:
579 	g_io_channel_shutdown(chan, TRUE, NULL);
580 	g_io_channel_unref(chan);
581 	return;
582 }
583 
headset_server_init(struct audio_adapter * adapter)584 static int headset_server_init(struct audio_adapter *adapter)
585 {
586 	uint8_t chan = DEFAULT_HS_AG_CHANNEL;
587 	sdp_record_t *record;
588 	gboolean master = TRUE;
589 	GError *err = NULL;
590 	uint32_t features;
591 	GIOChannel *io;
592 	bdaddr_t src;
593 
594 	if (config) {
595 		gboolean tmp;
596 
597 		tmp = g_key_file_get_boolean(config, "General", "Master",
598 						&err);
599 		if (err) {
600 			debug("audio.conf: %s", err->message);
601 			g_clear_error(&err);
602 		} else
603 			master = tmp;
604 	}
605 
606 	adapter_get_address(adapter->btd_adapter, &src);
607 
608 	io =  bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
609 				BT_IO_OPT_SOURCE_BDADDR, &src,
610 				BT_IO_OPT_CHANNEL, chan,
611 				BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
612 				BT_IO_OPT_MASTER, master,
613 				BT_IO_OPT_INVALID);
614 	if (!io)
615 		goto failed;
616 
617 	adapter->hsp_ag_server = io;
618 
619 	record = hsp_ag_record(chan);
620 	if (!record) {
621 		error("Unable to allocate new service record");
622 		goto failed;
623 	}
624 
625 	if (add_record_to_server(&src, record) < 0) {
626 		error("Unable to register HS AG service record");
627 		sdp_record_free(record);
628 		goto failed;
629 	}
630 	adapter->hsp_ag_record_id = record->handle;
631 
632 	features = headset_config_init(config);
633 
634 	if (!enabled.hfp)
635 		return 0;
636 
637 	chan = DEFAULT_HF_AG_CHANNEL;
638 
639 	io = bt_io_listen(BT_IO_RFCOMM, NULL, ag_confirm, adapter, NULL, &err,
640 				BT_IO_OPT_SOURCE_BDADDR, &src,
641 				BT_IO_OPT_CHANNEL, chan,
642 				BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
643 				BT_IO_OPT_MASTER, master,
644 				BT_IO_OPT_INVALID);
645 	if (!io)
646 		goto failed;
647 
648 	adapter->hfp_ag_server = io;
649 
650 	record = hfp_ag_record(chan, features);
651 	if (!record) {
652 		error("Unable to allocate new service record");
653 		goto failed;
654 	}
655 
656 	if (add_record_to_server(&src, record) < 0) {
657 		error("Unable to register HF AG service record");
658 		sdp_record_free(record);
659 		goto failed;
660 	}
661 	adapter->hfp_ag_record_id = record->handle;
662 
663 	return 0;
664 
665 failed:
666 	error("%s", err->message);
667 	g_error_free(err);
668 	if (adapter->hsp_ag_server) {
669 		g_io_channel_shutdown(adapter->hsp_ag_server, TRUE, NULL);
670 		g_io_channel_unref(adapter->hsp_ag_server);
671 		adapter->hsp_ag_server = NULL;
672 	}
673 
674 	if (adapter->hfp_ag_server) {
675 		g_io_channel_shutdown(adapter->hfp_ag_server, TRUE, NULL);
676 		g_io_channel_unref(adapter->hfp_ag_server);
677 		adapter->hfp_ag_server = NULL;
678 	}
679 
680 	return -1;
681 }
682 
gateway_server_init(struct audio_adapter * adapter)683 static int gateway_server_init(struct audio_adapter *adapter)
684 {
685 	uint8_t chan = DEFAULT_HFP_HS_CHANNEL;
686 	sdp_record_t *record;
687 	gboolean master = TRUE;
688 	GError *err = NULL;
689 	GIOChannel *io;
690 	bdaddr_t src;
691 
692 	if (config) {
693 		gboolean tmp;
694 
695 		tmp = g_key_file_get_boolean(config, "General", "Master",
696 						&err);
697 		if (err) {
698 			debug("audio.conf: %s", err->message);
699 			g_clear_error(&err);
700 		} else
701 			master = tmp;
702 	}
703 
704 	adapter_get_address(adapter->btd_adapter, &src);
705 
706 	io = bt_io_listen(BT_IO_RFCOMM, NULL, hf_io_cb, adapter, NULL, &err,
707 				BT_IO_OPT_SOURCE_BDADDR, &src,
708 				BT_IO_OPT_CHANNEL, chan,
709 				BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
710 				BT_IO_OPT_MASTER, master,
711 				BT_IO_OPT_INVALID);
712 	if (!io) {
713 		error("%s", err->message);
714 		g_error_free(err);
715 		return -1;
716 	}
717 
718 	adapter->hfp_hs_server = io;
719 	record = hfp_hs_record(chan);
720 	if (!record) {
721 		error("Unable to allocate new service record");
722 		return -1;
723 	}
724 
725 	if (add_record_to_server(&src, record) < 0) {
726 		error("Unable to register HFP HS service record");
727 		sdp_record_free(record);
728 		g_io_channel_unref(adapter->hfp_hs_server);
729 		adapter->hfp_hs_server = NULL;
730 		return -1;
731 	}
732 
733 	adapter->hfp_hs_record_id = record->handle;
734 
735 	return 0;
736 }
737 
audio_probe(struct btd_device * device,GSList * uuids)738 static int audio_probe(struct btd_device *device, GSList *uuids)
739 {
740 	struct btd_adapter *adapter = device_get_adapter(device);
741 	bdaddr_t src, dst;
742 	struct audio_device *audio_dev;
743 
744 	adapter_get_address(adapter, &src);
745 	device_get_address(device, &dst);
746 
747 	audio_dev = manager_get_device(&src, &dst, TRUE);
748 	if (!audio_dev) {
749 		debug("audio_probe: unable to get a device object");
750 		return -1;
751 	}
752 
753 	g_slist_foreach(uuids, (GFunc) handle_uuid, audio_dev);
754 
755 	return 0;
756 }
757 
audio_remove(struct btd_device * device)758 static void audio_remove(struct btd_device *device)
759 {
760 	struct audio_device *dev;
761 	const char *path;
762 
763 	path = device_get_path(device);
764 
765 	dev = manager_find_device(path, NULL, NULL, NULL, FALSE);
766 	if (!dev)
767 		return;
768 
769 	devices = g_slist_remove(devices, dev);
770 
771 	audio_device_unregister(dev);
772 }
773 
audio_adapter_ref(struct audio_adapter * adp)774 static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp)
775 {
776 	adp->ref++;
777 
778 	debug("audio_adapter_ref(%p): ref=%d", adp, adp->ref);
779 
780 	return adp;
781 }
782 
audio_adapter_unref(struct audio_adapter * adp)783 static void audio_adapter_unref(struct audio_adapter *adp)
784 {
785 	adp->ref--;
786 
787 	debug("audio_adapter_unref(%p): ref=%d", adp, adp->ref);
788 
789 	if (adp->ref > 0)
790 		return;
791 
792 	adapters = g_slist_remove(adapters, adp);
793 	btd_adapter_unref(adp->btd_adapter);
794 	g_free(adp);
795 }
796 
audio_adapter_create(struct btd_adapter * adapter)797 static struct audio_adapter *audio_adapter_create(struct btd_adapter *adapter)
798 {
799 	struct audio_adapter *adp;
800 
801 	adp = g_new0(struct audio_adapter, 1);
802 	adp->btd_adapter = btd_adapter_ref(adapter);
803 
804 	return audio_adapter_ref(adp);
805 }
806 
audio_adapter_get(struct btd_adapter * adapter)807 static struct audio_adapter *audio_adapter_get(struct btd_adapter *adapter)
808 {
809 	struct audio_adapter *adp;
810 
811 	adp = find_adapter(adapters, adapter);
812 	if (!adp) {
813 		adp = audio_adapter_create(adapter);
814 		if (!adp)
815 			return NULL;
816 		adapters = g_slist_append(adapters, adp);
817 	} else
818 		audio_adapter_ref(adp);
819 
820 	return adp;
821 }
822 
headset_server_probe(struct btd_adapter * adapter)823 static int headset_server_probe(struct btd_adapter *adapter)
824 {
825 	struct audio_adapter *adp;
826 	const gchar *path = adapter_get_path(adapter);
827 	int ret;
828 
829 	DBG("path %s", path);
830 
831 	adp = audio_adapter_get(adapter);
832 	if (!adp)
833 		return -EINVAL;
834 
835 	ret = headset_server_init(adp);
836 	if (ret < 0) {
837 		audio_adapter_unref(adp);
838 		return ret;
839 	}
840 
841 	return 0;
842 }
843 
headset_server_remove(struct btd_adapter * adapter)844 static void headset_server_remove(struct btd_adapter *adapter)
845 {
846 	struct audio_adapter *adp;
847 	const gchar *path = adapter_get_path(adapter);
848 
849 	DBG("path %s", path);
850 
851 	adp = find_adapter(adapters, adapter);
852 	if (!adp)
853 		return;
854 
855 	if (adp->hsp_ag_record_id) {
856 		remove_record_from_server(adp->hsp_ag_record_id);
857 		adp->hsp_ag_record_id = 0;
858 	}
859 
860 	if (adp->hsp_ag_server) {
861 		g_io_channel_shutdown(adp->hsp_ag_server, TRUE, NULL);
862 		g_io_channel_unref(adp->hsp_ag_server);
863 		adp->hsp_ag_server = NULL;
864 	}
865 
866 	if (adp->hfp_ag_record_id) {
867 		remove_record_from_server(adp->hfp_ag_record_id);
868 		adp->hfp_ag_record_id = 0;
869 	}
870 
871 	if (adp->hfp_ag_server) {
872 		g_io_channel_shutdown(adp->hfp_ag_server, TRUE, NULL);
873 		g_io_channel_unref(adp->hfp_ag_server);
874 		adp->hfp_ag_server = NULL;
875 	}
876 
877 	audio_adapter_unref(adp);
878 }
879 
gateway_server_probe(struct btd_adapter * adapter)880 static int gateway_server_probe(struct btd_adapter *adapter)
881 {
882 	struct audio_adapter *adp;
883 	const gchar *path = adapter_get_path(adapter);
884 	int ret;
885 
886 	DBG("path %s", path);
887 
888 	adp = audio_adapter_get(adapter);
889 	if (!adp)
890 		return -EINVAL;
891 
892 	ret = gateway_server_init(adp);
893 	if (ret < 0) {
894 		audio_adapter_ref(adp);
895 		return ret;
896 	}
897 
898 	return 0;
899 }
900 
gateway_server_remove(struct btd_adapter * adapter)901 static void gateway_server_remove(struct btd_adapter *adapter)
902 {
903 	struct audio_adapter *adp;
904 	const gchar *path = adapter_get_path(adapter);
905 
906 	DBG("path %s", path);
907 
908 	adp = find_adapter(adapters, adapter);
909 	if (!adp)
910 		return;
911 
912 	if (adp->hfp_hs_record_id) {
913 		remove_record_from_server(adp->hfp_hs_record_id);
914 		adp->hfp_hs_record_id = 0;
915 	}
916 
917 	if (adp->hfp_hs_server) {
918 		g_io_channel_unref(adp->hfp_hs_server);
919 		adp->hfp_hs_server = NULL;
920 	}
921 
922 	audio_adapter_ref(adp);
923 }
924 
a2dp_server_probe(struct btd_adapter * adapter)925 static int a2dp_server_probe(struct btd_adapter *adapter)
926 {
927 	struct audio_adapter *adp;
928 	const gchar *path = adapter_get_path(adapter);
929 	bdaddr_t src;
930 	int ret;
931 
932 	DBG("path %s", path);
933 
934 	adp = audio_adapter_get(adapter);
935 	if (!adp)
936 		return -EINVAL;
937 
938 	adapter_get_address(adapter, &src);
939 
940 	ret = a2dp_register(connection, &src, config);
941 	if (ret < 0) {
942 		audio_adapter_unref(adp);
943 		return ret;
944 	}
945 
946 	return 0;
947 }
948 
a2dp_server_remove(struct btd_adapter * adapter)949 static void a2dp_server_remove(struct btd_adapter *adapter)
950 {
951 	struct audio_adapter *adp;
952 	const gchar *path = adapter_get_path(adapter);
953 	bdaddr_t src;
954 
955 	DBG("path %s", path);
956 
957 	adp = find_adapter(adapters, adapter);
958 	if (!adp)
959 		return;
960 
961 	adapter_get_address(adapter, &src);
962 	a2dp_unregister(&src);
963 	audio_adapter_unref(adp);
964 }
965 
avrcp_server_probe(struct btd_adapter * adapter)966 static int avrcp_server_probe(struct btd_adapter *adapter)
967 {
968 	struct audio_adapter *adp;
969 	const gchar *path = adapter_get_path(adapter);
970 	bdaddr_t src;
971 
972 	DBG("path %s", path);
973 
974 	adp = audio_adapter_get(adapter);
975 	if (!adp)
976 		return -EINVAL;
977 
978 	adapter_get_address(adapter, &src);
979 
980 	return avrcp_register(connection, &src, config);
981 }
982 
avrcp_server_remove(struct btd_adapter * adapter)983 static void avrcp_server_remove(struct btd_adapter *adapter)
984 {
985 	struct audio_adapter *adp;
986 	const gchar *path = adapter_get_path(adapter);
987 	bdaddr_t src;
988 
989 	DBG("path %s", path);
990 
991 	adp = find_adapter(adapters, adapter);
992 	if (!adp)
993 		return;
994 
995 	adapter_get_address(adapter, &src);
996 	avrcp_unregister(&src);
997 	audio_adapter_unref(adp);
998 }
999 
1000 static struct btd_device_driver audio_driver = {
1001 	.name	= "audio",
1002 	.uuids	= BTD_UUIDS(HSP_HS_UUID, HFP_HS_UUID, HSP_AG_UUID, HFP_AG_UUID,
1003 			ADVANCED_AUDIO_UUID, A2DP_SOURCE_UUID, A2DP_SINK_UUID,
1004 			AVRCP_TARGET_UUID, AVRCP_REMOTE_UUID),
1005 	.probe	= audio_probe,
1006 	.remove	= audio_remove,
1007 };
1008 
1009 static struct btd_adapter_driver headset_server_driver = {
1010 	.name	= "audio-headset",
1011 	.probe	= headset_server_probe,
1012 	.remove	= headset_server_remove,
1013 };
1014 
1015 static struct btd_adapter_driver gateway_server_driver = {
1016 	.name	= "audio-gateway",
1017 	.probe	= gateway_server_probe,
1018 	.remove	= gateway_server_remove,
1019 };
1020 
1021 static struct btd_adapter_driver a2dp_server_driver = {
1022 	.name	= "audio-a2dp",
1023 	.probe	= a2dp_server_probe,
1024 	.remove	= a2dp_server_remove,
1025 };
1026 
1027 static struct btd_adapter_driver avrcp_server_driver = {
1028 	.name	= "audio-control",
1029 	.probe	= avrcp_server_probe,
1030 	.remove	= avrcp_server_remove,
1031 };
1032 
audio_manager_init(DBusConnection * conn,GKeyFile * conf,gboolean * enable_sco)1033 int audio_manager_init(DBusConnection *conn, GKeyFile *conf,
1034 							gboolean *enable_sco)
1035 {
1036 	char **list;
1037 	int i;
1038 	gboolean b;
1039 	GError *err = NULL;
1040 
1041 	connection = dbus_connection_ref(conn);
1042 
1043 	if (!conf)
1044 		goto proceed;
1045 
1046 	config = conf;
1047 
1048 	list = g_key_file_get_string_list(config, "General", "Enable",
1049 						NULL, NULL);
1050 	for (i = 0; list && list[i] != NULL; i++) {
1051 		if (g_str_equal(list[i], "Headset"))
1052 			enabled.headset = TRUE;
1053 		else if (g_str_equal(list[i], "Gateway"))
1054 			enabled.gateway = TRUE;
1055 		else if (g_str_equal(list[i], "Sink"))
1056 			enabled.sink = TRUE;
1057 		else if (g_str_equal(list[i], "Source"))
1058 			enabled.source = TRUE;
1059 		else if (g_str_equal(list[i], "Control"))
1060 			enabled.control = TRUE;
1061 	}
1062 	g_strfreev(list);
1063 
1064 	list = g_key_file_get_string_list(config, "General", "Disable",
1065 						NULL, NULL);
1066 	for (i = 0; list && list[i] != NULL; i++) {
1067 		if (g_str_equal(list[i], "Headset"))
1068 			enabled.headset = FALSE;
1069 		else if (g_str_equal(list[i], "Gateway"))
1070 			enabled.gateway = FALSE;
1071 		else if (g_str_equal(list[i], "Sink"))
1072 			enabled.sink = FALSE;
1073 		else if (g_str_equal(list[i], "Source"))
1074 			enabled.source = FALSE;
1075 		else if (g_str_equal(list[i], "Control"))
1076 			enabled.control = FALSE;
1077 	}
1078 	g_strfreev(list);
1079 
1080 	b = g_key_file_get_boolean(config, "General", "AutoConnect", &err);
1081 	if (err) {
1082 		debug("audio.conf: %s", err->message);
1083 		g_clear_error(&err);
1084 	} else
1085 		auto_connect = b;
1086 
1087 	b = g_key_file_get_boolean(config, "Headset", "HFP",
1088 					&err);
1089 	if (err)
1090 		g_clear_error(&err);
1091 	else
1092 		enabled.hfp = b;
1093 
1094 	err = NULL;
1095 	i = g_key_file_get_integer(config, "Headset", "MaxConnected",
1096 					&err);
1097 	if (err) {
1098 		debug("audio.conf: %s", err->message);
1099 		g_clear_error(&err);
1100 	} else
1101 		max_connected_headsets = i;
1102 
1103 proceed:
1104 	if (enabled.headset) {
1105 		telephony_init();
1106 		btd_register_adapter_driver(&headset_server_driver);
1107 	}
1108 
1109 	if (enabled.gateway)
1110 		btd_register_adapter_driver(&gateway_server_driver);
1111 
1112 	if (enabled.source || enabled.sink)
1113 		btd_register_adapter_driver(&a2dp_server_driver);
1114 
1115 	if (enabled.control)
1116 		btd_register_adapter_driver(&avrcp_server_driver);
1117 
1118 	btd_register_device_driver(&audio_driver);
1119 
1120 	*enable_sco = (enabled.gateway || enabled.headset);
1121 
1122 	return 0;
1123 }
1124 
audio_manager_exit(void)1125 void audio_manager_exit(void)
1126 {
1127 	/* Bail out early if we haven't been initialized */
1128 	if (connection == NULL)
1129 		return;
1130 
1131 	dbus_connection_unref(connection);
1132 	connection = NULL;
1133 
1134 	if (config) {
1135 		g_key_file_free(config);
1136 		config = NULL;
1137 	}
1138 
1139 	if (enabled.headset) {
1140 		btd_unregister_adapter_driver(&headset_server_driver);
1141 		telephony_exit();
1142 	}
1143 
1144 	if (enabled.gateway)
1145 		btd_unregister_adapter_driver(&gateway_server_driver);
1146 
1147 	if (enabled.source || enabled.sink)
1148 		btd_unregister_adapter_driver(&a2dp_server_driver);
1149 
1150 	if (enabled.control)
1151 		btd_unregister_adapter_driver(&avrcp_server_driver);
1152 
1153 	btd_unregister_device_driver(&audio_driver);
1154 }
1155 
manager_find_device(const char * path,const bdaddr_t * src,const bdaddr_t * dst,const char * interface,gboolean connected)1156 struct audio_device *manager_find_device(const char *path,
1157 					const bdaddr_t *src,
1158 					const bdaddr_t *dst,
1159 					const char *interface,
1160 					gboolean connected)
1161 {
1162 	GSList *l;
1163 
1164 	for (l = devices; l != NULL; l = l->next) {
1165 		struct audio_device *dev = l->data;
1166 
1167 		if ((path && (strcmp(path, "")) && strcmp(dev->path, path)))
1168 			continue;
1169 
1170 		if ((src && bacmp(src, BDADDR_ANY)) && bacmp(&dev->src, src))
1171 			continue;
1172 
1173 		if ((dst && bacmp(dst, BDADDR_ANY)) && bacmp(&dev->dst, dst))
1174 			continue;
1175 
1176 		if (interface && !strcmp(AUDIO_HEADSET_INTERFACE, interface)
1177 				&& !dev->headset)
1178 			continue;
1179 
1180 		if (interface && !strcmp(AUDIO_GATEWAY_INTERFACE, interface)
1181 				&& !dev->gateway)
1182 			continue;
1183 
1184 		if (interface && !strcmp(AUDIO_SINK_INTERFACE, interface)
1185 				&& !dev->sink)
1186 			continue;
1187 
1188 		if (interface && !strcmp(AUDIO_SOURCE_INTERFACE, interface)
1189 				&& !dev->source)
1190 			continue;
1191 
1192 		if (interface && !strcmp(AUDIO_CONTROL_INTERFACE, interface)
1193 				&& !dev->control)
1194 			continue;
1195 
1196 		if (connected && !audio_device_is_active(dev, interface))
1197 			continue;
1198 
1199 		return dev;
1200 	}
1201 
1202 	return NULL;
1203 }
1204 
manager_get_device(const bdaddr_t * src,const bdaddr_t * dst,gboolean create)1205 struct audio_device *manager_get_device(const bdaddr_t *src,
1206 					const bdaddr_t *dst,
1207 					gboolean create)
1208 {
1209 	struct audio_device *dev;
1210 	struct btd_adapter *adapter;
1211 	struct btd_device *device;
1212 	char addr[18];
1213 	const char *path;
1214 
1215 	dev = manager_find_device(NULL, src, dst, NULL, FALSE);
1216 	if (dev)
1217 		return dev;
1218 
1219 	if (!create)
1220 		return NULL;
1221 
1222 	ba2str(src, addr);
1223 
1224 	adapter = manager_find_adapter(src);
1225 	if (!adapter) {
1226 		error("Unable to get a btd_adapter object for %s",
1227 				addr);
1228 		return NULL;
1229 	}
1230 
1231 	ba2str(dst, addr);
1232 
1233 	device = adapter_get_device(connection, adapter, addr);
1234 	if (!device) {
1235 		error("Unable to get btd_device object for %s", addr);
1236 		return NULL;
1237 	}
1238 
1239 	path = device_get_path(device);
1240 
1241 	dev = audio_device_register(connection, device, path, src, dst);
1242 	if (!dev)
1243 		return NULL;
1244 
1245 	devices = g_slist_append(devices, dev);
1246 
1247 	return dev;
1248 }
1249 
manager_allow_headset_connection(struct audio_device * device)1250 gboolean manager_allow_headset_connection(struct audio_device *device)
1251 {
1252 	GSList *l;
1253 	int connected = 0;
1254 
1255 	for (l = devices; l != NULL; l = l->next) {
1256 		struct audio_device *dev = l->data;
1257 		struct headset *hs = dev->headset;
1258 
1259 		if (dev == device)
1260 			continue;
1261 
1262 		if (bacmp(&dev->src, &device->src))
1263 			continue;
1264 
1265 		if (!hs)
1266 			continue;
1267 
1268 		if (headset_get_state(dev) > HEADSET_STATE_DISCONNECTED)
1269 			connected++;
1270 
1271 		if (connected >= max_connected_headsets)
1272 			return FALSE;
1273 	}
1274 
1275 	return TRUE;
1276 }
1277