3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2006-2010 Nokia Corporation
6
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
7
* Copyright (C) 2009 Joao Paulo Rechi Vita
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33
#include <bluetooth/bluetooth.h>
34
#include <bluetooth/sdp.h>
37
#include <dbus/dbus.h>
48
#include "dbus-common.h"
49
#include "../src/adapter.h"
50
#include "../src/device.h"
52
#define STREAM_SETUP_RETRY_TIMER 2
54
struct pending_request {
61
struct audio_device *dev;
62
struct avdtp *session;
63
struct avdtp_stream *stream;
66
avdtp_session_state_t session_state;
67
avdtp_state_t stream_state;
69
struct pending_request *connect;
70
struct pending_request *disconnect;
74
struct source_state_callback {
80
static GSList *source_callbacks = NULL;
82
static unsigned int avdtp_callback_id = 0;
84
static const char *state2str(source_state_t state)
87
case SOURCE_STATE_DISCONNECTED:
88
return "disconnected";
89
case SOURCE_STATE_CONNECTING:
91
case SOURCE_STATE_CONNECTED:
93
case SOURCE_STATE_PLAYING:
96
error("Invalid source state %d", state);
101
static void source_set_state(struct audio_device *dev, source_state_t new_state)
103
struct source *source = dev->source;
104
const char *state_str;
105
source_state_t old_state = source->state;
108
source->state = new_state;
110
state_str = state2str(new_state);
112
emit_property_changed(dev->conn, dev->path,
113
AUDIO_SOURCE_INTERFACE, "State",
114
DBUS_TYPE_STRING, &state_str);
116
for (l = source_callbacks; l != NULL; l = l->next) {
117
struct source_state_callback *cb = l->data;
118
cb->cb(dev, old_state, new_state, cb->user_data);
122
static void avdtp_state_callback(struct audio_device *dev,
123
struct avdtp *session,
124
avdtp_session_state_t old_state,
125
avdtp_session_state_t new_state,
128
struct source *source = dev->source;
134
case AVDTP_SESSION_STATE_DISCONNECTED:
135
source_set_state(dev, SOURCE_STATE_DISCONNECTED);
137
case AVDTP_SESSION_STATE_CONNECTING:
138
source_set_state(dev, SOURCE_STATE_CONNECTING);
140
case AVDTP_SESSION_STATE_CONNECTED:
144
source->session_state = new_state;
147
static void pending_request_free(struct audio_device *dev,
148
struct pending_request *pending)
151
dbus_connection_unref(pending->conn);
153
dbus_message_unref(pending->msg);
155
a2dp_cancel(dev, pending->id);
160
static void stream_state_changed(struct avdtp_stream *stream,
161
avdtp_state_t old_state,
162
avdtp_state_t new_state,
163
struct avdtp_error *err,
166
struct audio_device *dev = user_data;
167
struct source *source = dev->source;
173
case AVDTP_STATE_IDLE:
174
if (source->disconnect) {
176
struct pending_request *p;
178
p = source->disconnect;
179
source->disconnect = NULL;
181
reply = dbus_message_new_method_return(p->msg);
182
g_dbus_send_message(p->conn, reply);
183
pending_request_free(dev, p);
186
if (source->session) {
187
avdtp_unref(source->session);
188
source->session = NULL;
190
source->stream = NULL;
193
case AVDTP_STATE_OPEN:
194
source_set_state(dev, SOURCE_STATE_CONNECTED);
196
case AVDTP_STATE_STREAMING:
197
source_set_state(dev, SOURCE_STATE_PLAYING);
199
case AVDTP_STATE_CONFIGURED:
200
case AVDTP_STATE_CLOSING:
201
case AVDTP_STATE_ABORTING:
206
source->stream_state = new_state;
209
static DBusHandlerResult error_failed(DBusConnection *conn,
210
DBusMessage *msg, const char * desc)
212
return error_common_reply(conn, msg, ERROR_INTERFACE ".Failed", desc);
215
static gboolean stream_setup_retry(gpointer user_data)
217
struct source *source = user_data;
218
struct pending_request *pending = source->connect;
220
source->retry_id = 0;
222
if (source->stream_state >= AVDTP_STATE_OPEN) {
223
DBG("Stream successfully created, after XCASE connect:connect");
226
reply = dbus_message_new_method_return(pending->msg);
227
g_dbus_send_message(pending->conn, reply);
230
DBG("Stream setup failed, after XCASE connect:connect");
232
error_failed(pending->conn, pending->msg, "Stream setup failed");
235
source->connect = NULL;
236
pending_request_free(source->dev, pending);
241
static void stream_setup_complete(struct avdtp *session, struct a2dp_sep *sep,
242
struct avdtp_stream *stream,
243
struct avdtp_error *err, void *user_data)
245
struct source *source = user_data;
246
struct pending_request *pending;
248
pending = source->connect;
253
DBG("Stream successfully created");
257
reply = dbus_message_new_method_return(pending->msg);
258
g_dbus_send_message(pending->conn, reply);
261
source->connect = NULL;
262
pending_request_free(source->dev, pending);
267
avdtp_unref(source->session);
268
source->session = NULL;
269
if (avdtp_error_category(err) == AVDTP_ERRNO
270
&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
271
DBG("connect:connect XCASE detected");
272
source->retry_id = g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
277
error_failed(pending->conn, pending->msg, "Stream setup failed");
278
source->connect = NULL;
279
pending_request_free(source->dev, pending);
280
DBG("Stream setup failed : %s", avdtp_strerror(err));
284
static void select_complete(struct avdtp *session, struct a2dp_sep *sep,
285
GSList *caps, void *user_data)
287
struct source *source = user_data;
288
struct pending_request *pending;
291
pending = source->connect;
298
id = a2dp_config(session, sep, stream_setup_complete, caps, source);
307
error_failed(pending->conn, pending->msg, "Stream setup failed");
308
pending_request_free(source->dev, pending);
309
source->connect = NULL;
310
avdtp_unref(source->session);
311
source->session = NULL;
314
static void discovery_complete(struct avdtp *session, GSList *seps, struct avdtp_error *err,
317
struct source *source = user_data;
318
struct pending_request *pending;
321
pending = source->connect;
324
avdtp_unref(source->session);
325
source->session = NULL;
326
if (avdtp_error_category(err) == AVDTP_ERRNO
327
&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
328
DBG("connect:connect XCASE detected");
330
g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
338
DBG("Discovery complete");
340
id = a2dp_select_capabilities(source->session, AVDTP_SEP_TYPE_SOURCE, NULL,
341
select_complete, source);
350
error_failed(pending->conn, pending->msg, "Stream setup failed");
351
pending_request_free(source->dev, pending);
352
source->connect = NULL;
353
avdtp_unref(source->session);
354
source->session = NULL;
357
gboolean source_setup_stream(struct source *source, struct avdtp *session)
359
if (source->connect || source->disconnect)
362
if (session && !source->session)
363
source->session = avdtp_ref(session);
365
if (!source->session)
368
avdtp_set_auto_disconnect(source->session, FALSE);
370
if (avdtp_discover(source->session, discovery_complete, source) < 0)
373
source->connect = g_new0(struct pending_request, 1);
378
static DBusMessage *source_connect(DBusConnection *conn,
379
DBusMessage *msg, void *data)
381
struct audio_device *dev = data;
382
struct source *source = dev->source;
383
struct pending_request *pending;
385
if (!source->session)
386
source->session = avdtp_get(&dev->src, &dev->dst);
388
if (!source->session)
389
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
390
"Unable to get a session");
392
if (source->connect || source->disconnect)
393
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
394
"%s", strerror(EBUSY));
396
if (source->stream_state >= AVDTP_STATE_OPEN)
397
return g_dbus_create_error(msg, ERROR_INTERFACE
399
"Device Already Connected");
401
if (!source_setup_stream(source, NULL))
402
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
403
"Failed to create a stream");
405
dev->auto_connect = FALSE;
407
pending = source->connect;
409
pending->conn = dbus_connection_ref(conn);
410
pending->msg = dbus_message_ref(msg);
412
DBG("stream creation in progress");
417
static DBusMessage *source_disconnect(DBusConnection *conn,
418
DBusMessage *msg, void *data)
420
struct audio_device *device = data;
421
struct source *source = device->source;
422
struct pending_request *pending;
425
if (!source->session)
426
return g_dbus_create_error(msg, ERROR_INTERFACE
428
"Device not Connected");
430
if (source->connect || source->disconnect)
431
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
432
"%s", strerror(EBUSY));
434
if (source->stream_state < AVDTP_STATE_OPEN) {
435
DBusMessage *reply = dbus_message_new_method_return(msg);
438
avdtp_unref(source->session);
439
source->session = NULL;
443
err = avdtp_close(source->session, source->stream, FALSE);
445
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
446
"%s", strerror(-err));
448
pending = g_new0(struct pending_request, 1);
449
pending->conn = dbus_connection_ref(conn);
450
pending->msg = dbus_message_ref(msg);
451
source->disconnect = pending;
456
static DBusMessage *source_get_properties(DBusConnection *conn,
457
DBusMessage *msg, void *data)
459
struct audio_device *device = data;
460
struct source *source = device->source;
462
DBusMessageIter iter;
463
DBusMessageIter dict;
466
reply = dbus_message_new_method_return(msg);
470
dbus_message_iter_init_append(reply, &iter);
472
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
473
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
474
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
475
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
478
state = state2str(source->state);
480
dict_append_entry(&dict, "State", DBUS_TYPE_STRING, &state);
482
dbus_message_iter_close_container(&iter, &dict);
487
static GDBusMethodTable source_methods[] = {
488
{ "Connect", "", "", source_connect,
489
G_DBUS_METHOD_FLAG_ASYNC },
490
{ "Disconnect", "", "", source_disconnect,
491
G_DBUS_METHOD_FLAG_ASYNC },
492
{ "GetProperties", "", "a{sv}",source_get_properties },
493
{ NULL, NULL, NULL, NULL }
496
static GDBusSignalTable source_signals[] = {
497
{ "PropertyChanged", "sv" },
501
static void source_free(struct audio_device *dev)
503
struct source *source = dev->source;
506
avdtp_stream_remove_cb(source->session, source->stream,
510
avdtp_unref(source->session);
513
pending_request_free(dev, source->connect);
515
if (source->disconnect)
516
pending_request_free(dev, source->disconnect);
518
if (source->retry_id)
519
g_source_remove(source->retry_id);
525
static void path_unregister(void *data)
527
struct audio_device *dev = data;
529
DBG("Unregistered interface %s on path %s",
530
AUDIO_SOURCE_INTERFACE, dev->path);
535
void source_unregister(struct audio_device *dev)
537
g_dbus_unregister_interface(dev->conn, dev->path,
538
AUDIO_SOURCE_INTERFACE);
541
struct source *source_init(struct audio_device *dev)
543
struct source *source;
545
if (!g_dbus_register_interface(dev->conn, dev->path,
546
AUDIO_SOURCE_INTERFACE,
547
source_methods, source_signals, NULL,
548
dev, path_unregister))
551
DBG("Registered interface %s on path %s",
552
AUDIO_SOURCE_INTERFACE, dev->path);
554
if (avdtp_callback_id == 0)
555
avdtp_callback_id = avdtp_add_state_cb(avdtp_state_callback,
558
source = g_new0(struct source, 1);
565
gboolean source_is_active(struct audio_device *dev)
567
struct source *source = dev->source;
575
avdtp_state_t source_get_state(struct audio_device *dev)
577
struct source *source = dev->source;
579
return source->stream_state;
582
gboolean source_new_stream(struct audio_device *dev, struct avdtp *session,
583
struct avdtp_stream *stream)
585
struct source *source = dev->source;
590
if (!source->session)
591
source->session = avdtp_ref(session);
593
source->stream = stream;
595
source->cb_id = avdtp_stream_add_cb(session, stream,
596
stream_state_changed, dev);
601
gboolean source_shutdown(struct source *source)
606
if (avdtp_close(source->session, source->stream, FALSE) < 0)
612
unsigned int source_add_state_cb(source_state_cb cb, void *user_data)
614
struct source_state_callback *state_cb;
615
static unsigned int id = 0;
617
state_cb = g_new(struct source_state_callback, 1);
619
state_cb->user_data = user_data;
622
source_callbacks = g_slist_append(source_callbacks, state_cb);
627
gboolean source_remove_state_cb(unsigned int id)
631
for (l = source_callbacks; l != NULL; l = l->next) {
632
struct source_state_callback *cb = l->data;
633
if (cb && cb->id == id) {
634
source_callbacks = g_slist_remove(source_callbacks, cb);