device.c revision 9c3f955375abff0d9ec2c151a5ad4b402c0ac29e
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 <errno.h>
31#include <unistd.h>
32#include <sys/stat.h>
33#include <sys/param.h>
34#include <netinet/in.h>
35
36#include <bluetooth/bluetooth.h>
37#include <bluetooth/hci.h>
38#include <bluetooth/hci_lib.h>
39#include <bluetooth/sdp.h>
40#include <bluetooth/sdp_lib.h>
41
42#include <glib.h>
43#include <dbus/dbus.h>
44#include <gdbus.h>
45
46#include "logging.h"
47#include "textfile.h"
48#include "../src/adapter.h"
49#include "../src/device.h"
50
51#include "error.h"
52#include "ipc.h"
53#include "dbus-common.h"
54#include "device.h"
55#include "unix.h"
56#include "avdtp.h"
57#include "control.h"
58#include "headset.h"
59#include "gateway.h"
60#include "sink.h"
61#include "source.h"
62
63#define AUDIO_INTERFACE "org.bluez.Audio"
64
65#define CONTROL_CONNECT_TIMEOUT 2
66#define AVDTP_CONNECT_TIMEOUT 1
67#define HEADSET_CONNECT_TIMEOUT 1
68
69typedef enum {
70	AUDIO_STATE_DISCONNECTED,
71	AUDIO_STATE_CONNECTING,
72	AUDIO_STATE_CONNECTED,
73} audio_state_t;
74
75struct service_auth {
76	service_auth_cb cb;
77	void *user_data;
78};
79
80struct dev_priv {
81	audio_state_t state;
82
83	headset_state_t hs_state;
84	sink_state_t sink_state;
85	avctp_state_t avctp_state;
86	GSList *auths;
87
88	DBusMessage *conn_req;
89	DBusMessage *dc_req;
90
91	guint control_timer;
92	guint avdtp_timer;
93	guint headset_timer;
94};
95
96static unsigned int sink_callback_id = 0;
97static unsigned int avctp_callback_id = 0;
98static unsigned int avdtp_callback_id = 0;
99static unsigned int headset_callback_id = 0;
100
101static void device_free(struct audio_device *dev)
102{
103	struct dev_priv *priv = dev->priv;
104
105	if (dev->conn)
106		dbus_connection_unref(dev->conn);
107
108	btd_device_unref(dev->btd_dev);
109
110	if (priv) {
111		if (priv->control_timer)
112			g_source_remove(priv->control_timer);
113		if (priv->avdtp_timer)
114			g_source_remove(priv->avdtp_timer);
115		if (priv->headset_timer)
116			g_source_remove(priv->headset_timer);
117		if (priv->dc_req)
118			dbus_message_unref(priv->dc_req);
119		if (priv->conn_req)
120			dbus_message_unref(priv->conn_req);
121		g_free(priv);
122	}
123
124	g_free(dev->path);
125	g_free(dev);
126}
127
128static const char *state2str(audio_state_t state)
129{
130	switch (state) {
131	case AUDIO_STATE_DISCONNECTED:
132		return "disconnected";
133	case AUDIO_STATE_CONNECTING:
134		return "connecting";
135	case AUDIO_STATE_CONNECTED:
136		return "connected";
137	default:
138		error("Invalid audio state %d", state);
139		return NULL;
140	}
141}
142
143static void device_set_state(struct audio_device *dev, audio_state_t new_state)
144{
145	struct dev_priv *priv = dev->priv;
146	const char *state_str;
147	DBusMessage *reply = NULL;
148
149	state_str = state2str(new_state);
150	if (!state_str)
151		return;
152
153	if (dev->priv->state == new_state) {
154		debug("state change attempted from %s to %s",
155							state_str, state_str);
156		return;
157	}
158
159	dev->priv->state = new_state;
160
161	if (priv->dc_req && new_state == AUDIO_STATE_DISCONNECTED) {
162		reply = dbus_message_new_method_return(priv->dc_req);
163		dbus_message_unref(priv->dc_req);
164		priv->dc_req = NULL;
165		g_dbus_send_message(dev->conn, reply);
166	}
167
168	if (priv->conn_req && new_state != AUDIO_STATE_CONNECTING) {
169		if (new_state == AUDIO_STATE_CONNECTED)
170			reply = dbus_message_new_method_return(priv->conn_req);
171		else
172			reply = g_dbus_create_error(priv->conn_req,
173							ERROR_INTERFACE
174							".ConnectFailed",
175							"Connecting failed");
176		dbus_message_unref(priv->conn_req);
177		priv->conn_req = NULL;
178		g_dbus_send_message(dev->conn, reply);
179	}
180
181	emit_property_changed(dev->conn, dev->path,
182				AUDIO_INTERFACE, "State",
183				DBUS_TYPE_STRING, &state_str);
184}
185
186static gboolean control_connect_timeout(gpointer user_data)
187{
188	struct audio_device *dev = user_data;
189
190	dev->priv->control_timer = 0;
191
192	if (dev->control)
193		avrcp_connect(dev);
194
195	return FALSE;
196}
197
198static gboolean device_set_control_timer(struct audio_device *dev)
199{
200	struct dev_priv *priv = dev->priv;
201
202	if (!dev->control)
203		return FALSE;
204
205	if (priv->control_timer)
206		return FALSE;
207
208	priv->control_timer = g_timeout_add_seconds(CONTROL_CONNECT_TIMEOUT,
209							control_connect_timeout,
210							dev);
211
212	return TRUE;
213}
214
215static void device_remove_control_timer(struct audio_device *dev)
216{
217	if (dev->priv->control_timer)
218		g_source_remove(dev->priv->control_timer);
219	dev->priv->control_timer = 0;
220}
221
222static gboolean avdtp_connect_timeout(gpointer user_data)
223{
224	struct audio_device *dev = user_data;
225
226	dev->priv->avdtp_timer = 0;
227
228	if (dev->sink) {
229		struct avdtp *session = avdtp_get(&dev->src, &dev->dst);
230
231		if (!session)
232			return FALSE;
233
234		sink_setup_stream(dev->sink, session);
235		avdtp_unref(session);
236	}
237
238	return FALSE;
239}
240
241static gboolean device_set_avdtp_timer(struct audio_device *dev)
242{
243	struct dev_priv *priv = dev->priv;
244
245	if (!dev->sink)
246		return FALSE;
247
248	if (priv->avdtp_timer)
249		return FALSE;
250
251	priv->avdtp_timer = g_timeout_add_seconds(AVDTP_CONNECT_TIMEOUT,
252							avdtp_connect_timeout,
253							dev);
254
255	return TRUE;
256}
257
258static void device_remove_avdtp_timer(struct audio_device *dev)
259{
260	if (dev->priv->avdtp_timer)
261		g_source_remove(dev->priv->avdtp_timer);
262	dev->priv->avdtp_timer = 0;
263}
264
265static gboolean headset_connect_timeout(gpointer user_data)
266{
267	struct audio_device *dev = user_data;
268	struct dev_priv *priv = dev->priv;
269
270	dev->priv->headset_timer = 0;
271
272	if (dev->headset == NULL)
273		return FALSE;
274
275	if (headset_config_stream(dev, FALSE, NULL, NULL) == 0) {
276		if (priv->state != AUDIO_STATE_CONNECTED &&
277				(priv->sink_state == SINK_STATE_CONNECTED ||
278				priv->sink_state == SINK_STATE_PLAYING))
279			device_set_state(dev, AUDIO_STATE_CONNECTED);
280	}
281
282	return FALSE;
283}
284
285static gboolean device_set_headset_timer(struct audio_device *dev)
286{
287	struct dev_priv *priv = dev->priv;
288
289	if (!dev->headset)
290		return FALSE;
291
292	if (priv->headset_timer)
293		return FALSE;
294
295	priv->headset_timer = g_timeout_add_seconds(HEADSET_CONNECT_TIMEOUT,
296						headset_connect_timeout, dev);
297
298	return TRUE;
299}
300
301static void device_remove_headset_timer(struct audio_device *dev)
302{
303	if (dev->priv->headset_timer)
304		g_source_remove(dev->priv->headset_timer);
305	dev->priv->headset_timer = 0;
306}
307
308static void device_avdtp_cb(struct audio_device *dev, struct avdtp *session,
309				avdtp_session_state_t old_state,
310				avdtp_session_state_t new_state,
311				void *user_data)
312{
313	if (!dev->sink || !dev->control)
314		return;
315
316	if (new_state == AVDTP_SESSION_STATE_CONNECTED) {
317		if (avdtp_stream_setup_active(session))
318			device_set_control_timer(dev);
319		else
320			avrcp_connect(dev);
321	}
322}
323
324static void device_sink_cb(struct audio_device *dev,
325				sink_state_t old_state,
326				sink_state_t new_state,
327				void *user_data)
328{
329	struct dev_priv *priv = dev->priv;
330
331	if (!dev->sink)
332		return;
333
334	priv->sink_state = new_state;
335
336	switch (new_state) {
337	case SINK_STATE_DISCONNECTED:
338		if (dev->control) {
339			device_remove_control_timer(dev);
340			avrcp_disconnect(dev);
341		}
342		if (priv->hs_state == HEADSET_STATE_DISCONNECTED)
343			device_set_state(dev, AUDIO_STATE_DISCONNECTED);
344		else if (old_state == SINK_STATE_CONNECTING) {
345			switch (priv->hs_state) {
346			case HEADSET_STATE_CONNECTED:
347			case HEADSET_STATE_PLAY_IN_PROGRESS:
348			case HEADSET_STATE_PLAYING:
349				device_set_state(dev, AUDIO_STATE_CONNECTED);
350			default:
351				break;
352			}
353		}
354		break;
355	case SINK_STATE_CONNECTING:
356		device_remove_avdtp_timer(dev);
357		if (priv->hs_state == HEADSET_STATE_DISCONNECTED)
358			device_set_state(dev, AUDIO_STATE_CONNECTING);
359		break;
360	case SINK_STATE_CONNECTED:
361		if (old_state == SINK_STATE_PLAYING)
362			break;
363		if (dev->auto_connect) {
364			if (!dev->headset)
365				device_set_state(dev, AUDIO_STATE_CONNECTED);
366			else if (priv->hs_state == HEADSET_STATE_DISCONNECTED)
367				device_set_headset_timer(dev);
368			else if (priv->hs_state == HEADSET_STATE_CONNECTED ||
369					priv->hs_state == HEADSET_STATE_PLAY_IN_PROGRESS ||
370					priv->hs_state == HEADSET_STATE_PLAYING)
371				device_set_state(dev, AUDIO_STATE_CONNECTED);
372		} else if (priv->hs_state == HEADSET_STATE_DISCONNECTED ||
373				priv->hs_state == HEADSET_STATE_CONNECTING)
374			device_set_state(dev, AUDIO_STATE_CONNECTED);
375		break;
376	case SINK_STATE_PLAYING:
377		break;
378	}
379}
380
381static void device_avctp_cb(struct audio_device *dev,
382				avctp_state_t old_state,
383				avctp_state_t new_state,
384				void *user_data)
385{
386	if (!dev->control)
387		return;
388
389	dev->priv->avctp_state = new_state;
390
391	switch (new_state) {
392	case AVCTP_STATE_DISCONNECTED:
393		break;
394	case AVCTP_STATE_CONNECTING:
395		device_remove_control_timer(dev);
396		break;
397	case AVCTP_STATE_CONNECTED:
398		break;
399	}
400}
401
402static void device_headset_cb(struct audio_device *dev,
403				headset_state_t old_state,
404				headset_state_t new_state,
405				void *user_data)
406{
407	struct dev_priv *priv = dev->priv;
408
409	if (!dev->headset)
410		return;
411
412	priv->hs_state = new_state;
413
414	switch (new_state) {
415	case HEADSET_STATE_DISCONNECTED:
416		device_remove_avdtp_timer(dev);
417		if (priv->sink_state != SINK_STATE_DISCONNECTED &&
418						dev->sink && priv->dc_req) {
419			sink_shutdown(dev->sink);
420			break;
421		}
422		if (priv->sink_state == SINK_STATE_DISCONNECTED)
423			device_set_state(dev, AUDIO_STATE_DISCONNECTED);
424		else if (old_state == HEADSET_STATE_CONNECTING &&
425				(priv->sink_state == SINK_STATE_CONNECTED ||
426				priv->sink_state == SINK_STATE_PLAYING))
427			device_set_state(dev, AUDIO_STATE_CONNECTED);
428		break;
429	case HEADSET_STATE_CONNECTING:
430		device_remove_headset_timer(dev);
431		if (priv->sink_state == SINK_STATE_DISCONNECTED)
432			device_set_state(dev, AUDIO_STATE_CONNECTING);
433		break;
434	case HEADSET_STATE_CONNECTED:
435		if (old_state == HEADSET_STATE_CONNECTED ||
436				old_state == HEADSET_STATE_PLAY_IN_PROGRESS ||
437				old_state == HEADSET_STATE_PLAYING)
438			break;
439		if (dev->auto_connect) {
440			if (!dev->sink)
441				device_set_state(dev, AUDIO_STATE_CONNECTED);
442			else if (priv->sink_state == SINK_STATE_DISCONNECTED)
443				device_set_avdtp_timer(dev);
444			else if (priv->sink_state == SINK_STATE_CONNECTED ||
445					priv->sink_state == SINK_STATE_PLAYING)
446				device_set_state(dev, AUDIO_STATE_CONNECTED);
447		} else if (priv->sink_state == SINK_STATE_DISCONNECTED ||
448				priv->sink_state == SINK_STATE_CONNECTING)
449			device_set_state(dev, AUDIO_STATE_CONNECTED);
450		break;
451	case HEADSET_STATE_PLAY_IN_PROGRESS:
452		break;
453	case HEADSET_STATE_PLAYING:
454		break;
455	}
456}
457
458static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
459								void *data)
460{
461	struct audio_device *dev = data;
462	struct dev_priv *priv = dev->priv;
463
464	if (priv->state == AUDIO_STATE_CONNECTING)
465		return g_dbus_create_error(msg, ERROR_INTERFACE ".InProgress",
466						"Connect in Progress");
467	else if (priv->state == AUDIO_STATE_CONNECTED)
468		return g_dbus_create_error(msg, ERROR_INTERFACE
469						".AlreadyConnected",
470						"Already Connected");
471
472	dev->auto_connect = TRUE;
473
474	if (dev->headset)
475		headset_config_stream(dev, FALSE, NULL, NULL);
476
477	if (priv->state != AUDIO_STATE_CONNECTING && dev->sink) {
478		struct avdtp *session = avdtp_get(&dev->src, &dev->dst);
479
480		if (!session)
481			return g_dbus_create_error(msg, ERROR_INTERFACE
482					".Failed",
483					"Failed to get AVDTP session");
484
485		sink_setup_stream(dev->sink, session);
486		avdtp_unref(session);
487	}
488
489	/* The previous calls should cause a call to the state callback to
490	 * indicate AUDIO_STATE_CONNECTING */
491	if (priv->state != AUDIO_STATE_CONNECTING)
492		return g_dbus_create_error(msg, ERROR_INTERFACE
493				".ConnectFailed",
494				"Headset connect failed");
495
496	priv->conn_req = dbus_message_ref(msg);
497
498	return NULL;
499}
500
501static DBusMessage *dev_disconnect(DBusConnection *conn, DBusMessage *msg,
502								void *data)
503{
504	struct audio_device *dev = data;
505	struct dev_priv *priv = dev->priv;
506
507	if (priv->state == AUDIO_STATE_DISCONNECTED)
508		return g_dbus_create_error(msg, ERROR_INTERFACE ".NotConnected",
509						"Not connected");
510
511	if (priv->dc_req)
512		return dbus_message_new_method_return(msg);
513
514	priv->dc_req = dbus_message_ref(msg);
515
516	if (priv->hs_state != HEADSET_STATE_DISCONNECTED)
517		headset_shutdown(dev);
518	else if (dev->sink && priv->sink_state != SINK_STATE_DISCONNECTED)
519		sink_shutdown(dev->sink);
520	else {
521		dbus_message_unref(priv->dc_req);
522		priv->dc_req = NULL;
523		return dbus_message_new_method_return(msg);
524	}
525
526	return NULL;
527}
528
529static DBusMessage *dev_get_properties(DBusConnection *conn, DBusMessage *msg,
530								void *data)
531{
532	struct audio_device *device = data;
533	DBusMessage *reply;
534	DBusMessageIter iter;
535	DBusMessageIter dict;
536	const char *state;
537
538	reply = dbus_message_new_method_return(msg);
539	if (!reply)
540		return NULL;
541
542	dbus_message_iter_init_append(reply, &iter);
543
544	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
545			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
546			DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
547			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
548
549	/* State */
550	state = state2str(device->priv->state);
551	if (state)
552		dict_append_entry(&dict, "State", DBUS_TYPE_STRING, &state);
553
554	dbus_message_iter_close_container(&iter, &dict);
555
556	return reply;
557}
558
559static GDBusMethodTable dev_methods[] = {
560	{ "Connect",		"",	"",	dev_connect,
561						G_DBUS_METHOD_FLAG_ASYNC },
562	{ "Disconnect",		"",	"",	dev_disconnect },
563	{ "GetProperties",	"",	"a{sv}",dev_get_properties },
564	{ NULL, NULL, NULL, NULL }
565};
566
567static GDBusSignalTable dev_signals[] = {
568	{ "PropertyChanged",		"sv"	},
569	{ NULL, NULL }
570};
571
572struct audio_device *audio_device_register(DBusConnection *conn,
573					struct btd_device *device,
574					const char *path, const bdaddr_t *src,
575					const bdaddr_t *dst)
576{
577	struct audio_device *dev;
578
579	if (!conn || !path)
580		return NULL;
581
582	dev = g_new0(struct audio_device, 1);
583
584	dev->btd_dev = btd_device_ref(device);
585	dev->path = g_strdup(path);
586	bacpy(&dev->dst, dst);
587	bacpy(&dev->src, src);
588	dev->conn = dbus_connection_ref(conn);
589	dev->priv = g_new0(struct dev_priv, 1);
590	dev->priv->state = AUDIO_STATE_DISCONNECTED;
591
592	if (!g_dbus_register_interface(dev->conn, dev->path,
593					AUDIO_INTERFACE,
594					dev_methods, dev_signals, NULL,
595					dev, NULL)) {
596		error("Unable to register %s on %s", AUDIO_INTERFACE,
597								dev->path);
598		device_free(dev);
599		return NULL;
600	}
601
602	debug("Registered interface %s on path %s", AUDIO_INTERFACE,
603								dev->path);
604
605	if (sink_callback_id == 0)
606		sink_callback_id = sink_add_state_cb(device_sink_cb, NULL);
607
608	if (avdtp_callback_id == 0)
609		avdtp_callback_id = avdtp_add_state_cb(device_avdtp_cb, NULL);
610	if (avctp_callback_id == 0)
611		avctp_callback_id = avctp_add_state_cb(device_avctp_cb, NULL);
612
613	if (headset_callback_id == 0)
614		headset_callback_id = headset_add_state_cb(device_headset_cb,
615									NULL);
616
617	return dev;
618}
619
620gboolean audio_device_is_active(struct audio_device *dev,
621						const char *interface)
622{
623	if (!interface) {
624		if ((dev->sink || dev->source) &&
625			avdtp_is_connected(&dev->src, &dev->dst))
626			return TRUE;
627
628		if (dev->headset && headset_is_active(dev))
629			return TRUE;
630	}
631	else if (!strcmp(interface, AUDIO_SINK_INTERFACE) && dev->sink &&
632			avdtp_is_connected(&dev->src, &dev->dst))
633		return TRUE;
634	else if (!strcmp(interface, AUDIO_SOURCE_INTERFACE) && dev->source &&
635			avdtp_is_connected(&dev->src, &dev->dst))
636		return TRUE;
637	else if (!strcmp(interface, AUDIO_HEADSET_INTERFACE) && dev->headset &&
638			headset_is_active(dev))
639		return TRUE;
640	else if (!strcmp(interface, AUDIO_CONTROL_INTERFACE) && dev->control &&
641			control_is_active(dev))
642		return TRUE;
643	else if (!strcmp(interface, AUDIO_GATEWAY_INTERFACE) && dev->gateway &&
644			gateway_is_connected(dev))
645		return TRUE;
646
647	return FALSE;
648}
649
650void audio_device_unregister(struct audio_device *device)
651{
652	unix_device_removed(device);
653
654	if (device->headset)
655		headset_unregister(device);
656
657	if (device->sink)
658		sink_unregister(device);
659
660	if (device->source)
661		source_unregister(device);
662
663	if (device->control)
664		control_unregister(device);
665
666	g_dbus_unregister_interface(device->conn, device->path,
667						AUDIO_INTERFACE);
668
669	device_free(device);
670}
671
672static void auth_cb(DBusError *derr, void *user_data)
673{
674	struct audio_device *dev = user_data;
675	struct dev_priv *priv = dev->priv;
676
677	while (priv->auths) {
678		struct service_auth *auth = priv->auths->data;
679
680		auth->cb(derr, auth->user_data);
681		priv->auths = g_slist_remove(priv->auths, auth);
682		g_free(auth);
683	}
684}
685
686int audio_device_request_authorization(struct audio_device *dev,
687					const char *uuid, service_auth_cb cb,
688					void *user_data)
689{
690	struct dev_priv *priv = dev->priv;
691	struct service_auth *auth;
692	int err;
693
694	auth = g_try_new0(struct service_auth, 1);
695	if (!auth)
696		return -ENOMEM;
697
698	auth->cb = cb;
699	auth->user_data = user_data;
700
701	priv->auths = g_slist_append(priv->auths, auth);
702	if (g_slist_length(priv->auths) > 1)
703		return 0;
704
705	err = btd_request_authorization(&dev->src, &dev->dst, uuid, auth_cb,
706					dev);
707	if (err < 0) {
708		priv->auths = g_slist_remove(priv->auths, auth);
709		g_free(auth);
710	}
711
712	return err;
713}
714