device.c revision 12d81a98fe65d109c0b1caa242e96b005bcc8209
1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2006-2007  Nokia Corporation
6 *  Copyright (C) 2004-2008  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
49#include "error.h"
50#include "ipc.h"
51#include "device.h"
52#include "avdtp.h"
53#include "control.h"
54#include "headset.h"
55#include "sink.h"
56
57static void device_free(struct audio_device *dev)
58{
59	if (dev->headset)
60		headset_free(dev);
61
62	if (dev->sink)
63		sink_free(dev);
64
65	if (dev->control)
66		control_free(dev);
67
68	if (dev->conn)
69		dbus_connection_unref(dev->conn);
70
71	g_free(dev->path);
72	g_free(dev);
73}
74
75struct audio_device *device_register(DBusConnection *conn,
76					const char *path, const bdaddr_t *src,
77					const bdaddr_t *dst)
78{
79	struct audio_device *dev;
80
81	if (!conn || !path)
82		return NULL;
83
84	dev = g_new0(struct audio_device, 1);
85
86	dev->path = g_strdup(path);
87	bacpy(&dev->dst, dst);
88	bacpy(&dev->src, src);
89	dev->conn = dbus_connection_ref(conn);
90
91	return dev;
92}
93
94gboolean device_is_connected(struct audio_device *dev, const char *interface)
95{
96	if (!interface) {
97		if ((dev->sink || dev->source) &&
98			avdtp_is_connected(&dev->src, &dev->dst))
99			return TRUE;
100
101		if (dev->headset && headset_is_active(dev))
102			return TRUE;
103	}
104	else if (!strcmp(interface, AUDIO_SINK_INTERFACE) && dev->sink &&
105			avdtp_is_connected(&dev->src, &dev->dst))
106		return TRUE;
107	else if (!strcmp(interface, AUDIO_SOURCE_INTERFACE) && dev->source &&
108			avdtp_is_connected(&dev->src, &dev->dst))
109		return TRUE;
110	else if (!strcmp(interface, AUDIO_HEADSET_INTERFACE) && dev->headset &&
111			headset_is_active(dev))
112		return TRUE;
113	else if (!strcmp(interface, AUDIO_CONTROL_INTERFACE) && dev->headset &&
114			control_is_active(dev))
115		return TRUE;
116
117	return FALSE;
118}
119
120void device_unregister(struct audio_device *device)
121{
122	g_dbus_unregister_all_interfaces(device->conn, device->path);
123
124	device_free(device);
125}
126