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