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) 2008-2009 Leonid Movshovich <event.riga@gmail.org>
8 * Copyright (C) 2010 ProFUSION embedded systems
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include <dbus/dbus.h>
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/sdp.h>
43 #include <bluetooth/sdp_lib.h>
45 #include "sdp-client.h"
51 #include "dbus-common.h"
54 char *name; /* Bus id */
55 char *path; /* D-Bus path */
56 guint watch; /* Disconnect watch */
61 gateway_stream_cb_t cb;
66 gateway_state_t state;
71 struct hf_agent *agent;
77 struct gateway_state_callback {
83 static GSList *gateway_callbacks = NULL;
85 int gateway_close(struct audio_device *device);
87 GQuark gateway_error_quark(void)
89 return g_quark_from_static_string("gateway-error-quark");
92 static const char *state2str(gateway_state_t state)
95 case GATEWAY_STATE_DISCONNECTED:
96 return "disconnected";
97 case GATEWAY_STATE_CONNECTING:
99 case GATEWAY_STATE_CONNECTED:
101 case GATEWAY_STATE_PLAYING:
108 static void agent_free(struct hf_agent *agent)
118 static void change_state(struct audio_device *dev, gateway_state_t new_state)
120 struct gateway *gw = dev->gateway;
123 gateway_state_t old_state;
125 if (gw->state == new_state)
128 val = state2str(new_state);
129 old_state = gw->state;
130 gw->state = new_state;
132 emit_property_changed(dev->conn, dev->path,
133 AUDIO_GATEWAY_INTERFACE, "State",
134 DBUS_TYPE_STRING, &val);
136 for (l = gateway_callbacks; l != NULL; l = l->next) {
137 struct gateway_state_callback *cb = l->data;
138 cb->cb(dev, old_state, new_state, cb->user_data);
142 void gateway_set_state(struct audio_device *dev, gateway_state_t new_state)
145 case GATEWAY_STATE_DISCONNECTED:
148 case GATEWAY_STATE_CONNECTING:
149 case GATEWAY_STATE_CONNECTED:
150 case GATEWAY_STATE_PLAYING:
155 static void agent_disconnect(struct audio_device *dev, struct hf_agent *agent)
159 msg = dbus_message_new_method_call(agent->name, agent->path,
160 "org.bluez.HandsfreeAgent", "Release");
162 g_dbus_send_message(dev->conn, msg);
165 static gboolean agent_sendfd(struct hf_agent *agent, int fd,
166 DBusPendingCallNotifyFunction notify, void *data)
168 struct audio_device *dev = data;
169 struct gateway *gw = dev->gateway;
171 DBusPendingCall *call;
173 msg = dbus_message_new_method_call(agent->name, agent->path,
174 "org.bluez.HandsfreeAgent", "NewConnection");
176 dbus_message_append_args(msg, DBUS_TYPE_UNIX_FD, &fd,
177 DBUS_TYPE_UINT16, &gw->version,
180 if (dbus_connection_send_with_reply(dev->conn, msg,
181 &call, -1) == FALSE) {
182 dbus_message_unref(msg);
186 dbus_pending_call_set_notify(call, notify, dev, NULL);
187 dbus_pending_call_unref(call);
188 dbus_message_unref(msg);
193 static unsigned int connect_cb_new(struct gateway *gw,
194 gateway_stream_cb_t func,
197 struct connect_cb *cb;
198 static unsigned int free_cb_id = 1;
203 cb = g_new(struct connect_cb, 1);
206 cb->cb_data = user_data;
207 cb->id = free_cb_id++;
209 gw->callbacks = g_slist_append(gw->callbacks, cb);
214 static void run_connect_cb(struct audio_device *dev, GError *err)
216 struct gateway *gw = dev->gateway;
219 for (l = gw->callbacks; l != NULL; l = l->next) {
220 struct connect_cb *cb = l->data;
221 cb->cb(dev, err, cb->cb_data);
224 g_slist_free_full(gw->callbacks, g_free);
225 gw->callbacks = NULL;
228 static gboolean sco_io_cb(GIOChannel *chan, GIOCondition cond,
229 struct audio_device *dev)
231 struct gateway *gw = dev->gateway;
233 if (cond & G_IO_NVAL)
236 DBG("sco connection is released");
237 g_io_channel_shutdown(gw->sco, TRUE, NULL);
238 g_io_channel_unref(gw->sco);
240 change_state(dev, GATEWAY_STATE_CONNECTED);
245 static void sco_connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
247 struct audio_device *dev = (struct audio_device *) user_data;
248 struct gateway *gw = dev->gateway;
250 DBG("at the begin of sco_connect_cb() in gateway.c");
252 gw->sco = g_io_channel_ref(chan);
255 error("sco_connect_cb(): %s", err->message);
256 gateway_suspend_stream(dev);
260 g_io_add_watch(gw->sco, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
261 (GIOFunc) sco_io_cb, dev);
263 change_state(dev, GATEWAY_STATE_PLAYING);
264 run_connect_cb(dev, NULL);
267 static gboolean rfcomm_disconnect_cb(GIOChannel *chan, GIOCondition cond,
268 struct audio_device *dev)
270 if (cond & G_IO_NVAL)
278 static void newconnection_reply(DBusPendingCall *call, void *data)
280 struct audio_device *dev = data;
281 struct gateway *gw = dev->gateway;
282 DBusMessage *reply = dbus_pending_call_steal_reply(call);
285 if (!dev->gateway->rfcomm) {
286 DBG("RFCOMM disconnected from server before agent reply");
290 dbus_error_init(&derr);
291 if (!dbus_set_error_from_message(&derr, reply)) {
292 DBG("Agent reply: file descriptor passed successfully");
293 g_io_add_watch(gw->rfcomm, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
294 (GIOFunc) rfcomm_disconnect_cb, dev);
295 change_state(dev, GATEWAY_STATE_CONNECTED);
299 DBG("Agent reply: %s", derr.message);
301 dbus_error_free(&derr);
305 dbus_message_unref(reply);
308 static void rfcomm_connect_cb(GIOChannel *chan, GError *err,
311 struct audio_device *dev = user_data;
312 struct gateway *gw = dev->gateway;
317 error("connect(): %s", err->message);
322 error("Handsfree Agent not registered");
326 sk = g_io_channel_unix_get_fd(chan);
328 if (gw->rfcomm == NULL)
329 gw->rfcomm = g_io_channel_ref(chan);
331 ret = agent_sendfd(gw->agent, sk, newconnection_reply, dev);
337 reply = dbus_message_new_method_return(gw->msg);
339 reply = btd_error_failed(gw->msg, "Can't pass file descriptor");
341 g_dbus_send_message(dev->conn, reply);
348 reply = btd_error_failed(gw->msg, "Connect failed");
349 g_dbus_send_message(dev->conn, reply);
355 static int get_remote_profile_version(sdp_record_t *rec)
358 sdp_list_t *profiles;
359 sdp_profile_desc_t *desc;
362 sdp_uuid16_create(&uuid, HANDSFREE_PROFILE_ID);
364 sdp_get_profile_descs(rec, &profiles);
365 if (profiles == NULL)
368 desc = profiles->data;
370 if (sdp_uuid16_cmp(&desc->uuid, &uuid) == 0)
373 sdp_list_free(profiles, free);
379 static void get_incoming_record_cb(sdp_list_t *recs, int err,
382 struct audio_device *dev = user_data;
383 struct gateway *gw = dev->gateway;
387 error("Unable to get service record: %s (%d)", strerror(-err),
392 if (!recs || !recs->data) {
393 error("No records found");
397 gw->version = get_remote_profile_version(recs->data);
398 if (gw->version == 0)
401 rfcomm_connect_cb(gw->incoming, gerr, dev);
408 static void unregister_incoming(gpointer user_data)
410 struct audio_device *dev = user_data;
411 struct gateway *gw = dev->gateway;
414 g_io_channel_unref(gw->incoming);
419 static void rfcomm_incoming_cb(GIOChannel *chan, GError *err,
422 struct audio_device *dev = user_data;
423 struct gateway *gw = dev->gateway;
426 gw->incoming = g_io_channel_ref(chan);
428 sdp_uuid16_create(&uuid, HANDSFREE_AGW_SVCLASS_ID);
429 if (bt_search_service(&dev->src, &dev->dst, &uuid,
430 get_incoming_record_cb, dev,
431 unregister_incoming) == 0)
434 unregister_incoming(dev);
438 static void get_record_cb(sdp_list_t *recs, int err, gpointer user_data)
440 struct audio_device *dev = user_data;
441 struct gateway *gw = dev->gateway;
443 sdp_list_t *protos, *classes;
449 error("Unable to get service record: %s (%d)", strerror(-err),
454 if (!recs || !recs->data) {
455 error("No records found");
460 if (sdp_get_service_classes(recs->data, &classes) < 0) {
461 error("Unable to get service classes from record");
466 if (sdp_get_access_protos(recs->data, &protos) < 0) {
467 error("Unable to get access protocols from record");
472 gw->version = get_remote_profile_version(recs->data);
473 if (gw->version == 0) {
474 error("Unable to get profile version from record");
479 memcpy(&uuid, classes->data, sizeof(uuid));
480 sdp_list_free(classes, free);
482 if (!sdp_uuid128_to_uuid(&uuid) || uuid.type != SDP_UUID16 ||
483 uuid.value.uuid16 != HANDSFREE_AGW_SVCLASS_ID) {
484 sdp_list_free(protos, NULL);
485 error("Invalid service record or not HFP");
490 ch = sdp_get_proto_port(protos, RFCOMM_UUID);
491 sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
492 sdp_list_free(protos, NULL);
494 error("Unable to extract RFCOMM channel from service record");
499 io = bt_io_connect(BT_IO_RFCOMM, rfcomm_connect_cb, dev, NULL, &gerr,
500 BT_IO_OPT_SOURCE_BDADDR, &dev->src,
501 BT_IO_OPT_DEST_BDADDR, &dev->dst,
502 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
503 BT_IO_OPT_CHANNEL, ch,
506 error("Unable to connect: %s", gerr->message);
510 g_io_channel_unref(io);
515 DBusMessage *reply = btd_error_failed(gw->msg,
516 gerr ? gerr->message : strerror(-err));
517 g_dbus_send_message(dev->conn, reply);
526 static int get_records(struct audio_device *device)
530 change_state(device, GATEWAY_STATE_CONNECTING);
531 sdp_uuid16_create(&uuid, HANDSFREE_AGW_SVCLASS_ID);
532 return bt_search_service(&device->src, &device->dst, &uuid,
533 get_record_cb, device, NULL);
536 static DBusMessage *ag_connect(DBusConnection *conn, DBusMessage *msg,
539 struct audio_device *au_dev = (struct audio_device *) data;
540 struct gateway *gw = au_dev->gateway;
544 return btd_error_agent_not_available(msg);
546 err = get_records(au_dev);
548 return btd_error_failed(msg, strerror(-err));
550 gw->msg = dbus_message_ref(msg);
555 int gateway_close(struct audio_device *device)
558 struct gateway *gw = device->gateway;
562 sock = g_io_channel_unix_get_fd(gw->rfcomm);
563 shutdown(sock, SHUT_RDWR);
565 g_io_channel_shutdown(gw->rfcomm, TRUE, NULL);
566 g_io_channel_unref(gw->rfcomm);
571 g_io_channel_shutdown(gw->sco, TRUE, NULL);
572 g_io_channel_unref(gw->sco);
576 change_state(device, GATEWAY_STATE_DISCONNECTED);
577 g_set_error(&gerr, GATEWAY_ERROR,
578 GATEWAY_ERROR_DISCONNECTED, "Disconnected");
579 run_connect_cb(device, gerr);
585 static DBusMessage *ag_disconnect(DBusConnection *conn, DBusMessage *msg,
588 struct audio_device *device = data;
589 struct gateway *gw = device->gateway;
590 DBusMessage *reply = NULL;
597 return btd_error_not_connected(msg);
599 reply = dbus_message_new_method_return(msg);
603 gateway_close(device);
604 ba2str(&device->dst, gw_addr);
605 DBG("Disconnected from %s, %s", gw_addr, device->path);
610 static void agent_exited(DBusConnection *conn, void *data)
612 struct gateway *gateway = data;
613 struct hf_agent *agent = gateway->agent;
615 DBG("Agent %s exited", agent->name);
618 gateway->agent = NULL;
621 static DBusMessage *ag_get_properties(DBusConnection *conn, DBusMessage *msg,
624 struct audio_device *device = data;
625 struct gateway *gw = device->gateway;
627 DBusMessageIter iter;
628 DBusMessageIter dict;
632 reply = dbus_message_new_method_return(msg);
636 dbus_message_iter_init_append(reply, &iter);
638 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
639 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
640 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
641 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
643 value = state2str(gw->state);
644 dict_append_entry(&dict, "State",
645 DBUS_TYPE_STRING, &value);
647 dbus_message_iter_close_container(&iter, &dict);
652 static DBusMessage *register_agent(DBusConnection *conn,
653 DBusMessage *msg, void *data)
655 struct audio_device *device = data;
656 struct gateway *gw = device->gateway;
657 struct hf_agent *agent;
658 const char *path, *name;
661 return btd_error_already_exists(msg);
663 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
665 return btd_error_invalid_args(msg);
667 name = dbus_message_get_sender(msg);
668 agent = g_new0(struct hf_agent, 1);
670 agent->name = g_strdup(name);
671 agent->path = g_strdup(path);
673 agent->watch = g_dbus_add_disconnect_watch(conn, name,
674 agent_exited, gw, NULL);
678 return dbus_message_new_method_return(msg);
681 static DBusMessage *unregister_agent(DBusConnection *conn,
682 DBusMessage *msg, void *data)
684 struct audio_device *device = data;
685 struct gateway *gw = device->gateway;
691 if (strcmp(gw->agent->name, dbus_message_get_sender(msg)) != 0)
692 return btd_error_not_authorized(msg);
694 if (!dbus_message_get_args(msg, NULL,
695 DBUS_TYPE_OBJECT_PATH, &path,
697 return btd_error_invalid_args(msg);
699 if (strcmp(gw->agent->path, path) != 0)
700 return btd_error_does_not_exist(msg);
702 g_dbus_remove_watch(device->conn, gw->agent->watch);
704 agent_free(gw->agent);
708 return dbus_message_new_method_return(msg);
711 static const GDBusMethodTable gateway_methods[] = {
712 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL, ag_connect) },
713 { GDBUS_ASYNC_METHOD("Disconnect", NULL, NULL, ag_disconnect) },
714 { GDBUS_METHOD("GetProperties",
715 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
716 ag_get_properties) },
717 { GDBUS_METHOD("RegisterAgent",
718 GDBUS_ARGS({ "agent", "o" }), NULL, register_agent) },
719 { GDBUS_METHOD("UnregisterAgent",
720 GDBUS_ARGS({ "agent", "o" }), NULL, unregister_agent) },
724 static const GDBusSignalTable gateway_signals[] = {
725 { GDBUS_SIGNAL("PropertyChanged",
726 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
730 static void path_unregister(void *data)
732 struct audio_device *dev = data;
734 DBG("Unregistered interface %s on path %s",
735 AUDIO_GATEWAY_INTERFACE, dev->path);
739 g_free(dev->gateway);
743 void gateway_unregister(struct audio_device *dev)
745 if (dev->gateway->agent)
746 agent_disconnect(dev, dev->gateway->agent);
748 g_dbus_unregister_interface(dev->conn, dev->path,
749 AUDIO_GATEWAY_INTERFACE);
752 struct gateway *gateway_init(struct audio_device *dev)
754 if (!g_dbus_register_interface(dev->conn, dev->path,
755 AUDIO_GATEWAY_INTERFACE,
756 gateway_methods, gateway_signals,
757 NULL, dev, path_unregister))
760 return g_new0(struct gateway, 1);
763 gboolean gateway_is_connected(struct audio_device *dev)
765 struct gateway *gw = dev->gateway;
767 if (gw->state == GATEWAY_STATE_CONNECTED)
773 gboolean gateway_is_active(struct audio_device *dev)
775 struct gateway *gw = dev->gateway;
777 if (gw->state != GATEWAY_STATE_DISCONNECTED)
783 int gateway_connect_rfcomm(struct audio_device *dev, GIOChannel *io)
788 dev->gateway->rfcomm = g_io_channel_ref(io);
790 change_state(dev, GATEWAY_STATE_CONNECTING);
795 int gateway_connect_sco(struct audio_device *dev, GIOChannel *io)
797 struct gateway *gw = dev->gateway;
802 gw->sco = g_io_channel_ref(io);
804 g_io_add_watch(gw->sco, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
805 (GIOFunc) sco_io_cb, dev);
807 change_state(dev, GATEWAY_STATE_PLAYING);
812 void gateway_start_service(struct audio_device *dev)
814 struct gateway *gw = dev->gateway;
817 if (gw->rfcomm == NULL)
820 if (!bt_io_accept(gw->rfcomm, rfcomm_incoming_cb, dev, NULL, &err)) {
821 error("bt_io_accept: %s", err->message);
827 static gboolean request_stream_cb(gpointer data)
829 run_connect_cb(data, NULL);
833 /* These are functions to be called from unix.c for audio system
834 * ifaces (alsa, gstreamer, etc.) */
835 unsigned int gateway_request_stream(struct audio_device *dev,
836 gateway_stream_cb_t cb, void *user_data)
838 struct gateway *gw = dev->gateway;
845 io = bt_io_connect(BT_IO_SCO, sco_connect_cb, dev, NULL, &err,
846 BT_IO_OPT_SOURCE_BDADDR, &dev->src,
847 BT_IO_OPT_DEST_BDADDR, &dev->dst,
848 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
851 error("%s", err->message);
856 g_idle_add(request_stream_cb, dev);
858 return connect_cb_new(gw, cb, user_data);
861 int gateway_config_stream(struct audio_device *dev, gateway_stream_cb_t cb,
864 struct gateway *gw = dev->gateway;
867 id = connect_cb_new(gw, cb, user_data);
872 g_idle_add(request_stream_cb, dev);
877 gboolean gateway_cancel_stream(struct audio_device *dev, unsigned int id)
879 struct gateway *gw = dev->gateway;
881 struct connect_cb *cb = NULL;
883 for (l = gw->callbacks; l != NULL; l = l->next) {
884 struct connect_cb *tmp = l->data;
895 gw->callbacks = g_slist_remove(gw->callbacks, cb);
898 gateway_suspend_stream(dev);
903 int gateway_get_sco_fd(struct audio_device *dev)
905 struct gateway *gw = dev->gateway;
910 return g_io_channel_unix_get_fd(gw->sco);
913 void gateway_suspend_stream(struct audio_device *dev)
916 struct gateway *gw = dev->gateway;
921 g_io_channel_shutdown(gw->sco, TRUE, NULL);
922 g_io_channel_unref(gw->sco);
924 g_set_error(&gerr, GATEWAY_ERROR, GATEWAY_ERROR_SUSPENDED, "Suspended");
925 run_connect_cb(dev, gerr);
927 change_state(dev, GATEWAY_STATE_CONNECTED);
930 unsigned int gateway_add_state_cb(gateway_state_cb cb, void *user_data)
932 struct gateway_state_callback *state_cb;
933 static unsigned int id = 0;
935 state_cb = g_new(struct gateway_state_callback, 1);
937 state_cb->user_data = user_data;
940 gateway_callbacks = g_slist_append(gateway_callbacks, state_cb);
945 gboolean gateway_remove_state_cb(unsigned int id)
949 for (l = gateway_callbacks; l != NULL; l = l->next) {
950 struct gateway_state_callback *cb = l->data;
951 if (cb && cb->id == id) {
952 gateway_callbacks = g_slist_remove(gateway_callbacks,
962 gateway_lock_t gateway_get_lock(struct audio_device *dev)
964 struct gateway *gw = dev->gateway;
969 gboolean gateway_lock(struct audio_device *dev, gateway_lock_t lock)
971 struct gateway *gw = dev->gateway;
981 gboolean gateway_unlock(struct audio_device *dev, gateway_lock_t lock)
983 struct gateway *gw = dev->gateway;
985 if (!(gw->lock & lock))
993 if (gw->state == GATEWAY_STATE_PLAYING)
994 gateway_suspend_stream(dev);