2007-06-23 04:40:17 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2010-01-07 17:02:51 +08:00
|
|
|
* Copyright (C) 2006-2010 Nokia Corporation
|
2010-01-02 09:08:17 +08:00
|
|
|
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
|
2009-04-03 11:49:47 +08:00
|
|
|
* Copyright (C) 2008-2009 Leonid Movshovich <event.riga@gmail.org>
|
2010-02-05 02:48:11 +08:00
|
|
|
* Copyright (C) 2010 ProFUSION embedded systems
|
2007-06-23 04:40:17 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2007-08-13 07:58:15 +08:00
|
|
|
|
2007-08-13 16:14:22 +08:00
|
|
|
#include <stdint.h>
|
2009-04-03 11:50:17 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2009-04-03 11:49:47 +08:00
|
|
|
#include <errno.h>
|
2007-08-13 16:14:22 +08:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <dbus/dbus.h>
|
2009-04-03 11:49:47 +08:00
|
|
|
#include <gdbus.h>
|
2007-08-13 16:14:22 +08:00
|
|
|
|
2009-04-03 11:49:47 +08:00
|
|
|
#include <bluetooth/bluetooth.h>
|
2009-04-03 11:50:17 +08:00
|
|
|
#include <bluetooth/sdp.h>
|
|
|
|
#include <bluetooth/sdp_lib.h>
|
2009-04-03 11:49:47 +08:00
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
#include "glib-helper.h"
|
2009-04-03 11:49:47 +08:00
|
|
|
#include "device.h"
|
2007-08-13 07:58:15 +08:00
|
|
|
#include "gateway.h"
|
2010-05-21 20:26:15 +08:00
|
|
|
#include "log.h"
|
2009-04-03 11:50:17 +08:00
|
|
|
#include "error.h"
|
|
|
|
#include "btio.h"
|
|
|
|
#include "dbus-common.h"
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
#ifndef DBUS_TYPE_UNIX_FD
|
|
|
|
#define DBUS_TYPE_UNIX_FD -1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct hf_agent {
|
|
|
|
char *name; /* Bus id */
|
|
|
|
char *path; /* D-Bus path */
|
|
|
|
guint watch; /* Disconnect watch */
|
|
|
|
};
|
2009-04-03 11:49:47 +08:00
|
|
|
|
|
|
|
struct gateway {
|
2009-04-21 20:49:52 +08:00
|
|
|
gateway_state_t state;
|
2009-04-03 11:49:47 +08:00
|
|
|
GIOChannel *rfcomm;
|
|
|
|
GIOChannel *sco;
|
2011-08-03 21:34:17 +08:00
|
|
|
GIOChannel *incoming;
|
2009-04-03 11:49:47 +08:00
|
|
|
gateway_stream_cb_t sco_start_cb;
|
|
|
|
void *sco_start_cb_data;
|
2010-02-05 02:48:11 +08:00
|
|
|
struct hf_agent *agent;
|
|
|
|
DBusMessage *msg;
|
2011-08-03 21:34:17 +08:00
|
|
|
int version;
|
2009-04-03 11:49:47 +08:00
|
|
|
};
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
int gateway_close(struct audio_device *device);
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
static const char *state2str(gateway_state_t state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case GATEWAY_STATE_DISCONNECTED:
|
|
|
|
return "disconnected";
|
|
|
|
case GATEWAY_STATE_CONNECTING:
|
|
|
|
return "connecting";
|
|
|
|
case GATEWAY_STATE_CONNECTED:
|
|
|
|
return "connected";
|
|
|
|
case GATEWAY_STATE_PLAYING:
|
|
|
|
return "playing";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void agent_free(struct hf_agent *agent)
|
|
|
|
{
|
|
|
|
if (!agent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free(agent->name);
|
|
|
|
g_free(agent->path);
|
|
|
|
g_free(agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void change_state(struct audio_device *dev, gateway_state_t new_state)
|
|
|
|
{
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
const char *val;
|
|
|
|
|
|
|
|
if (gw->state == new_state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
val = state2str(new_state);
|
|
|
|
gw->state = new_state;
|
|
|
|
|
|
|
|
emit_property_changed(dev->conn, dev->path,
|
|
|
|
AUDIO_GATEWAY_INTERFACE, "State",
|
|
|
|
DBUS_TYPE_STRING, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void agent_disconnect(struct audio_device *dev, struct hf_agent *agent)
|
|
|
|
{
|
|
|
|
DBusMessage *msg;
|
|
|
|
|
|
|
|
msg = dbus_message_new_method_call(agent->name, agent->path,
|
|
|
|
"org.bluez.HandsfreeAgent", "Release");
|
|
|
|
|
|
|
|
g_dbus_send_message(dev->conn, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean agent_sendfd(struct hf_agent *agent, int fd,
|
|
|
|
DBusPendingCallNotifyFunction notify, void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = data;
|
2011-08-03 21:34:17 +08:00
|
|
|
struct gateway *gw = dev->gateway;
|
2010-02-05 02:48:11 +08:00
|
|
|
DBusMessage *msg;
|
|
|
|
DBusPendingCall *call;
|
|
|
|
|
|
|
|
msg = dbus_message_new_method_call(agent->name, agent->path,
|
|
|
|
"org.bluez.HandsfreeAgent", "NewConnection");
|
|
|
|
|
|
|
|
dbus_message_append_args(msg, DBUS_TYPE_UNIX_FD, &fd,
|
2011-08-03 21:34:17 +08:00
|
|
|
DBUS_TYPE_UINT16, &gw->version,
|
2010-02-05 02:48:11 +08:00
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
if (dbus_connection_send_with_reply(dev->conn, msg, &call, -1) == FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dbus_pending_call_set_notify(call, notify, dev, NULL);
|
|
|
|
dbus_pending_call_unref(call);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
static gboolean sco_io_cb(GIOChannel *chan, GIOCondition cond,
|
|
|
|
struct audio_device *dev)
|
|
|
|
{
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
|
|
|
|
if (cond & G_IO_NVAL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (cond & (G_IO_ERR | G_IO_HUP)) {
|
2010-05-21 23:54:54 +08:00
|
|
|
DBG("sco connection is released");
|
2009-06-30 17:10:52 +08:00
|
|
|
g_io_channel_shutdown(gw->sco, TRUE, NULL);
|
|
|
|
g_io_channel_unref(gw->sco);
|
2009-04-03 11:50:17 +08:00
|
|
|
gw->sco = NULL;
|
2010-02-05 02:48:11 +08:00
|
|
|
change_state(dev, GATEWAY_STATE_CONNECTED);
|
2009-04-03 11:50:17 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sco_connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = (struct audio_device *) user_data;
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
|
2010-05-21 23:54:54 +08:00
|
|
|
DBG("at the begin of sco_connect_cb() in gateway.c");
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
gw->sco = g_io_channel_ref(chan);
|
|
|
|
|
|
|
|
if (gw->sco_start_cb)
|
|
|
|
gw->sco_start_cb(dev, err, gw->sco_start_cb_data);
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
if (err) {
|
2009-05-03 04:33:37 +08:00
|
|
|
error("sco_connect_cb(): %s", err->message);
|
2009-04-03 11:50:17 +08:00
|
|
|
gateway_close(dev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_io_add_watch(gw->sco, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
|
|
|
|
(GIOFunc) sco_io_cb, dev);
|
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
static void newconnection_reply(DBusPendingCall *call, void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = data;
|
|
|
|
DBusMessage *reply = dbus_pending_call_steal_reply(call);
|
|
|
|
DBusError derr;
|
|
|
|
|
|
|
|
if (!dev->gateway->rfcomm) {
|
2010-05-21 23:54:54 +08:00
|
|
|
DBG("RFCOMM disconnected from server before agent reply");
|
2010-02-05 02:48:11 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_error_init(&derr);
|
|
|
|
if (!dbus_set_error_from_message(&derr, reply)) {
|
2010-06-06 10:46:43 +08:00
|
|
|
DBG("Agent reply: file descriptor passed successfully");
|
2010-02-05 02:48:11 +08:00
|
|
|
change_state(dev, GATEWAY_STATE_CONNECTED);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2010-05-21 23:54:54 +08:00
|
|
|
DBG("Agent reply: %s", derr.message);
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
dbus_error_free(&derr);
|
|
|
|
gateway_close(dev);
|
|
|
|
|
|
|
|
done:
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
static void rfcomm_connect_cb(GIOChannel *chan, GError *err,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = user_data;
|
|
|
|
struct gateway *gw = dev->gateway;
|
2010-02-05 02:48:11 +08:00
|
|
|
DBusMessage *reply;
|
2010-02-16 03:34:21 +08:00
|
|
|
int sk, ret;
|
2009-04-03 11:50:17 +08:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
error("connect(): %s", err->message);
|
|
|
|
if (gw->sco_start_cb)
|
2010-02-05 02:48:11 +08:00
|
|
|
gw->sco_start_cb(dev, err, gw->sco_start_cb_data);
|
|
|
|
goto fail;
|
2009-04-03 11:50:17 +08:00
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
if (!gw->agent) {
|
2010-06-06 10:46:43 +08:00
|
|
|
error("Handsfree Agent not registered");
|
2010-02-05 02:48:11 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
sk = g_io_channel_unix_get_fd(chan);
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
gw->rfcomm = g_io_channel_ref(chan);
|
|
|
|
|
2010-02-16 03:34:21 +08:00
|
|
|
ret = agent_sendfd(gw->agent, sk, newconnection_reply, dev);
|
|
|
|
|
|
|
|
if (!gw->msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
reply = dbus_message_new_method_return(gw->msg);
|
|
|
|
else
|
2010-12-14 05:33:05 +08:00
|
|
|
reply = btd_error_failed(gw->msg, "Can't pass file descriptor");
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
g_dbus_send_message(dev->conn, reply);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2010-12-16 06:07:01 +08:00
|
|
|
if (gw->msg) {
|
|
|
|
DBusMessage *reply;
|
|
|
|
reply = btd_error_failed(gw->msg, "Connect failed");
|
|
|
|
g_dbus_send_message(dev->conn, reply);
|
|
|
|
}
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
change_state(dev, GATEWAY_STATE_DISCONNECTED);
|
2009-04-03 11:50:17 +08:00
|
|
|
}
|
|
|
|
|
2011-08-03 21:34:17 +08:00
|
|
|
static int get_remote_profile_version(sdp_record_t *rec)
|
|
|
|
{
|
|
|
|
uuid_t uuid;
|
|
|
|
sdp_list_t *profiles;
|
|
|
|
sdp_profile_desc_t *desc;
|
|
|
|
int ver = 0;
|
|
|
|
|
|
|
|
sdp_uuid16_create(&uuid, HANDSFREE_PROFILE_ID);
|
|
|
|
|
|
|
|
sdp_get_profile_descs(rec, &profiles);
|
|
|
|
if (profiles == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
desc = profiles->data;
|
|
|
|
|
|
|
|
if (sdp_uuid16_cmp(&desc->uuid, &uuid) == 0)
|
|
|
|
ver = desc->version;
|
|
|
|
|
|
|
|
sdp_list_free(profiles, free);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return ver;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_incoming_record_cb(sdp_list_t *recs, int err,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = user_data;
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
GError *gerr = NULL;
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
error("Unable to get service record: %s (%d)", strerror(-err),
|
|
|
|
-err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recs || !recs->data) {
|
|
|
|
error("No records found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gw->version = get_remote_profile_version(recs->data);
|
|
|
|
if (gw->version > 0)
|
|
|
|
rfcomm_connect_cb(gw->incoming, gerr, dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unregister_incoming(gpointer user_data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = user_data;
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
|
|
|
|
if (gw->incoming) {
|
|
|
|
g_io_channel_unref(gw->incoming);
|
|
|
|
gw->incoming = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_incoming_cb(GIOChannel *chan, GError *err,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = user_data;
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
uuid_t uuid;
|
|
|
|
|
|
|
|
gw->incoming = g_io_channel_ref(chan);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&uuid, HANDSFREE_AGW_SVCLASS_ID);
|
|
|
|
if (bt_search_service(&dev->src, &dev->dst, &uuid,
|
|
|
|
get_incoming_record_cb, dev,
|
|
|
|
unregister_incoming) < 0)
|
|
|
|
unregister_incoming(dev);
|
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
static void get_record_cb(sdp_list_t *recs, int err, gpointer user_data)
|
2009-04-03 11:50:17 +08:00
|
|
|
{
|
|
|
|
struct audio_device *dev = user_data;
|
2010-02-05 02:48:11 +08:00
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
int ch;
|
2009-07-16 04:28:43 +08:00
|
|
|
sdp_list_t *protos, *classes;
|
2009-04-03 11:50:17 +08:00
|
|
|
uuid_t uuid;
|
|
|
|
GIOChannel *io;
|
2010-02-05 02:48:11 +08:00
|
|
|
GError *gerr = NULL;
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
if (err < 0) {
|
|
|
|
error("Unable to get service record: %s (%d)", strerror(-err),
|
|
|
|
-err);
|
2009-07-16 04:28:43 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recs || !recs->data) {
|
2009-04-03 11:50:17 +08:00
|
|
|
error("No records found");
|
2010-02-05 02:48:11 +08:00
|
|
|
err = -EIO;
|
2009-07-16 04:28:43 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdp_get_service_classes(recs->data, &classes) < 0) {
|
2009-04-03 11:50:17 +08:00
|
|
|
error("Unable to get service classes from record");
|
2010-02-05 02:48:11 +08:00
|
|
|
err = -EINVAL;
|
2009-07-16 04:28:43 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdp_get_access_protos(recs->data, &protos) < 0) {
|
|
|
|
error("Unable to get access protocols from record");
|
2010-02-05 02:48:11 +08:00
|
|
|
err = -ENODATA;
|
2009-07-16 04:28:43 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-08-03 21:34:17 +08:00
|
|
|
gw->version = get_remote_profile_version(recs->data);
|
|
|
|
if (gw->version == 0) {
|
|
|
|
error("Unable to get profile version from record");
|
|
|
|
err = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:28:43 +08:00
|
|
|
memcpy(&uuid, classes->data, sizeof(uuid));
|
|
|
|
sdp_list_free(classes, free);
|
|
|
|
|
|
|
|
if (!sdp_uuid128_to_uuid(&uuid) || uuid.type != SDP_UUID16 ||
|
|
|
|
uuid.value.uuid16 != HANDSFREE_AGW_SVCLASS_ID) {
|
|
|
|
sdp_list_free(protos, NULL);
|
|
|
|
error("Invalid service record or not HFP");
|
2010-02-05 02:48:11 +08:00
|
|
|
err = -EIO;
|
2009-07-16 04:28:43 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch = sdp_get_proto_port(protos, RFCOMM_UUID);
|
|
|
|
sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
|
|
|
|
sdp_list_free(protos, NULL);
|
|
|
|
if (ch <= 0) {
|
|
|
|
error("Unable to extract RFCOMM channel from service record");
|
2010-02-05 02:48:11 +08:00
|
|
|
err = -EIO;
|
2009-07-16 04:28:43 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
io = bt_io_connect(BT_IO_RFCOMM, rfcomm_connect_cb, dev, NULL, &gerr,
|
2009-07-16 04:28:43 +08:00
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &dev->src,
|
|
|
|
BT_IO_OPT_DEST_BDADDR, &dev->dst,
|
|
|
|
BT_IO_OPT_CHANNEL, ch,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (!io) {
|
2010-02-05 02:48:11 +08:00
|
|
|
error("Unable to connect: %s", gerr->message);
|
2009-07-16 04:28:43 +08:00
|
|
|
gateway_close(dev);
|
2010-02-05 02:48:11 +08:00
|
|
|
goto fail;
|
2009-04-03 11:50:17 +08:00
|
|
|
}
|
|
|
|
|
2009-07-16 04:28:43 +08:00
|
|
|
g_io_channel_unref(io);
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
change_state(dev, GATEWAY_STATE_CONNECTING);
|
2009-07-16 04:28:43 +08:00
|
|
|
return;
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2009-07-16 04:28:43 +08:00
|
|
|
fail:
|
2010-12-16 06:07:01 +08:00
|
|
|
if (gw->msg) {
|
|
|
|
DBusMessage *reply = btd_error_failed(gw->msg,
|
|
|
|
gerr ? gerr->message : strerror(-err));
|
|
|
|
g_dbus_send_message(dev->conn, reply);
|
|
|
|
}
|
2009-07-16 04:28:43 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
change_state(dev, GATEWAY_STATE_DISCONNECTED);
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
if (!gerr)
|
|
|
|
g_set_error(&gerr, BT_IO_ERROR, BT_IO_ERROR_FAILED,
|
|
|
|
"connect: %s (%d)", strerror(-err), -err);
|
|
|
|
|
|
|
|
if (gw->sco_start_cb)
|
|
|
|
gw->sco_start_cb(dev, gerr, gw->sco_start_cb_data);
|
|
|
|
|
|
|
|
g_error_free(gerr);
|
2009-04-03 11:50:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_records(struct audio_device *device)
|
|
|
|
{
|
|
|
|
uuid_t uuid;
|
|
|
|
|
|
|
|
sdp_uuid16_create(&uuid, HANDSFREE_AGW_SVCLASS_ID);
|
|
|
|
return bt_search_service(&device->src, &device->dst, &uuid,
|
|
|
|
get_record_cb, device, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *ag_connect(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *au_dev = (struct audio_device *) data;
|
|
|
|
struct gateway *gw = au_dev->gateway;
|
2010-12-14 05:33:05 +08:00
|
|
|
int err;
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
if (!gw->agent)
|
2010-12-17 01:58:07 +08:00
|
|
|
return btd_error_agent_not_available(msg);
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-12-14 05:33:05 +08:00
|
|
|
err = get_records(au_dev);
|
|
|
|
if (err < 0)
|
|
|
|
return btd_error_failed(msg, strerror(-err));
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
gw->msg = dbus_message_ref(msg);
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
int gateway_close(struct audio_device *device)
|
|
|
|
{
|
|
|
|
struct gateway *gw = device->gateway;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
if (gw->rfcomm) {
|
|
|
|
sock = g_io_channel_unix_get_fd(gw->rfcomm);
|
|
|
|
shutdown(sock, SHUT_RDWR);
|
|
|
|
|
|
|
|
g_io_channel_shutdown(gw->rfcomm, TRUE, NULL);
|
|
|
|
g_io_channel_unref(gw->rfcomm);
|
|
|
|
gw->rfcomm = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gw->sco) {
|
|
|
|
g_io_channel_shutdown(gw->sco, TRUE, NULL);
|
|
|
|
g_io_channel_unref(gw->sco);
|
|
|
|
gw->sco = NULL;
|
|
|
|
gw->sco_start_cb = NULL;
|
|
|
|
gw->sco_start_cb_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
change_state(device, GATEWAY_STATE_DISCONNECTED);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
static DBusMessage *ag_disconnect(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *device = data;
|
|
|
|
struct gateway *gw = device->gateway;
|
|
|
|
DBusMessage *reply = NULL;
|
|
|
|
char gw_addr[18];
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
if (!device->conn)
|
|
|
|
return NULL;
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!gw->rfcomm)
|
2010-12-07 03:10:43 +08:00
|
|
|
return btd_error_not_connected(msg);
|
2009-04-03 11:50:17 +08:00
|
|
|
|
|
|
|
gateway_close(device);
|
|
|
|
ba2str(&device->dst, gw_addr);
|
2010-05-21 23:54:54 +08:00
|
|
|
DBG("Disconnected from %s, %s", gw_addr, device->path);
|
2009-04-03 11:50:17 +08:00
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
static void agent_exited(DBusConnection *conn, void *data)
|
|
|
|
{
|
|
|
|
struct gateway *gateway = data;
|
|
|
|
struct hf_agent *agent = gateway->agent;
|
|
|
|
|
2010-05-21 23:54:54 +08:00
|
|
|
DBG("Agent %s exited", agent->name);
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
agent_free(agent);
|
|
|
|
gateway->agent = NULL;
|
|
|
|
}
|
|
|
|
|
2009-04-03 11:50:17 +08:00
|
|
|
static DBusMessage *ag_get_properties(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *data)
|
|
|
|
{
|
2010-02-05 02:48:11 +08:00
|
|
|
struct audio_device *device = data;
|
|
|
|
struct gateway *gw = device->gateway;
|
|
|
|
DBusMessage *reply;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
DBusMessageIter dict;
|
|
|
|
const char *value;
|
|
|
|
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dbus_message_iter_init_append(reply, &iter);
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
|
|
|
|
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
|
|
|
|
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
|
|
|
|
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
|
|
|
|
|
|
|
|
value = state2str(gw->state);
|
|
|
|
dict_append_entry(&dict, "State",
|
|
|
|
DBUS_TYPE_STRING, &value);
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(&iter, &dict);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *register_agent(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *device = data;
|
|
|
|
struct gateway *gw = device->gateway;
|
|
|
|
struct hf_agent *agent;
|
|
|
|
const char *path, *name;
|
|
|
|
|
|
|
|
if (gw->agent)
|
2010-12-07 03:10:41 +08:00
|
|
|
return btd_error_already_exists(msg);
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
|
|
|
|
DBUS_TYPE_INVALID))
|
2010-12-07 03:10:40 +08:00
|
|
|
return btd_error_invalid_args(msg);
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
name = dbus_message_get_sender(msg);
|
|
|
|
agent = g_new0(struct hf_agent, 1);
|
|
|
|
|
|
|
|
agent->name = g_strdup(name);
|
|
|
|
agent->path = g_strdup(path);
|
|
|
|
|
|
|
|
agent->watch = g_dbus_add_disconnect_watch(conn, name,
|
|
|
|
agent_exited, gw, NULL);
|
|
|
|
|
|
|
|
gw->agent = agent;
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *unregister_agent(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *device = data;
|
|
|
|
struct gateway *gw = device->gateway;
|
2010-02-10 05:36:26 +08:00
|
|
|
const char *path;
|
2010-02-05 02:48:11 +08:00
|
|
|
|
|
|
|
if (!gw->agent)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (strcmp(gw->agent->name, dbus_message_get_sender(msg)) != 0)
|
2010-12-09 04:25:56 +08:00
|
|
|
return btd_error_not_authorized(msg);
|
2010-02-05 02:48:11 +08:00
|
|
|
|
2010-02-10 05:36:26 +08:00
|
|
|
if (!dbus_message_get_args(msg, NULL,
|
|
|
|
DBUS_TYPE_OBJECT_PATH, &path,
|
|
|
|
DBUS_TYPE_INVALID))
|
2010-12-07 03:10:40 +08:00
|
|
|
return btd_error_invalid_args(msg);
|
2010-02-10 05:36:26 +08:00
|
|
|
|
|
|
|
if (strcmp(gw->agent->path, path) != 0)
|
2010-12-09 04:25:57 +08:00
|
|
|
return btd_error_does_not_exist(msg);
|
2010-02-10 05:36:26 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
g_dbus_remove_watch(device->conn, gw->agent->watch);
|
|
|
|
|
|
|
|
agent_free(gw->agent);
|
|
|
|
gw->agent = NULL;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return dbus_message_new_method_return(msg);
|
2009-04-03 11:50:17 +08:00
|
|
|
}
|
|
|
|
|
2009-04-03 11:49:47 +08:00
|
|
|
static GDBusMethodTable gateway_methods[] = {
|
2009-04-03 11:50:17 +08:00
|
|
|
{ "Connect", "", "", ag_connect, G_DBUS_METHOD_FLAG_ASYNC },
|
2010-02-05 02:48:11 +08:00
|
|
|
{ "Disconnect", "", "", ag_disconnect, G_DBUS_METHOD_FLAG_ASYNC },
|
2009-04-03 11:50:17 +08:00
|
|
|
{ "GetProperties", "", "a{sv}", ag_get_properties },
|
2010-02-05 02:48:11 +08:00
|
|
|
{ "RegisterAgent", "o", "", register_agent },
|
|
|
|
{ "UnregisterAgent", "o", "", unregister_agent },
|
2009-04-03 11:49:47 +08:00
|
|
|
{ NULL, NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static GDBusSignalTable gateway_signals[] = {
|
2009-04-03 11:50:17 +08:00
|
|
|
{ "PropertyChanged", "sv" },
|
2009-04-03 11:49:47 +08:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2010-06-08 15:14:39 +08:00
|
|
|
static void path_unregister(void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = data;
|
|
|
|
|
|
|
|
DBG("Unregistered interface %s on path %s",
|
|
|
|
AUDIO_GATEWAY_INTERFACE, dev->path);
|
|
|
|
|
|
|
|
gateway_close(dev);
|
|
|
|
|
|
|
|
g_free(dev->gateway);
|
|
|
|
dev->gateway = NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
void gateway_unregister(struct audio_device *dev)
|
|
|
|
{
|
|
|
|
if (dev->gateway->agent)
|
|
|
|
agent_disconnect(dev, dev->gateway->agent);
|
|
|
|
|
|
|
|
g_dbus_unregister_interface(dev->conn, dev->path,
|
|
|
|
AUDIO_GATEWAY_INTERFACE);
|
|
|
|
}
|
|
|
|
|
2009-04-03 11:49:47 +08:00
|
|
|
struct gateway *gateway_init(struct audio_device *dev)
|
|
|
|
{
|
2010-02-05 02:48:11 +08:00
|
|
|
if (DBUS_TYPE_UNIX_FD < 0)
|
|
|
|
return NULL;
|
2009-04-03 11:49:47 +08:00
|
|
|
|
|
|
|
if (!g_dbus_register_interface(dev->conn, dev->path,
|
|
|
|
AUDIO_GATEWAY_INTERFACE,
|
|
|
|
gateway_methods, gateway_signals,
|
2010-06-08 15:14:39 +08:00
|
|
|
NULL, dev, path_unregister))
|
2009-04-03 11:49:47 +08:00
|
|
|
return NULL;
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
return g_new0(struct gateway, 1);
|
2009-04-03 11:49:47 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gateway_is_connected(struct audio_device *dev)
|
|
|
|
{
|
2009-04-21 20:49:52 +08:00
|
|
|
return (dev && dev->gateway &&
|
|
|
|
dev->gateway->state == GATEWAY_STATE_CONNECTED);
|
2009-04-03 11:49:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int gateway_connect_rfcomm(struct audio_device *dev, GIOChannel *io)
|
|
|
|
{
|
|
|
|
if (!io)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
dev->gateway->rfcomm = g_io_channel_ref(io);
|
2009-04-03 11:49:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-21 20:49:52 +08:00
|
|
|
int gateway_connect_sco(struct audio_device *dev, GIOChannel *io)
|
|
|
|
{
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
|
|
|
|
if (gw->sco)
|
|
|
|
return -EISCONN;
|
|
|
|
|
|
|
|
gw->sco = g_io_channel_ref(io);
|
|
|
|
|
2009-06-30 17:10:52 +08:00
|
|
|
g_io_add_watch(gw->sco, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
|
2010-02-05 02:48:11 +08:00
|
|
|
(GIOFunc) sco_io_cb, dev);
|
2009-04-21 20:49:52 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
change_state(dev, GATEWAY_STATE_PLAYING);
|
|
|
|
|
|
|
|
return 0;
|
2009-04-03 11:50:17 +08:00
|
|
|
}
|
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
void gateway_start_service(struct audio_device *dev)
|
2009-04-03 11:50:17 +08:00
|
|
|
{
|
2010-02-05 02:48:11 +08:00
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
GError *err = NULL;
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2010-02-05 02:48:11 +08:00
|
|
|
if (gw->rfcomm == NULL)
|
|
|
|
return;
|
2009-04-03 11:50:17 +08:00
|
|
|
|
2011-08-03 21:34:17 +08:00
|
|
|
if (!bt_io_accept(gw->rfcomm, rfcomm_incoming_cb, dev, NULL, &err)) {
|
2010-02-05 02:48:11 +08:00
|
|
|
error("bt_io_accept: %s", err->message);
|
|
|
|
g_error_free(err);
|
2009-04-03 11:50:17 +08:00
|
|
|
}
|
2009-04-03 11:49:47 +08:00
|
|
|
}
|
2009-04-08 16:46:55 +08:00
|
|
|
|
|
|
|
/* These are functions to be called from unix.c for audio system
|
|
|
|
* ifaces (alsa, gstreamer, etc.) */
|
|
|
|
gboolean gateway_request_stream(struct audio_device *dev,
|
|
|
|
gateway_stream_cb_t cb, void *user_data)
|
|
|
|
{
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
GError *err = NULL;
|
|
|
|
GIOChannel *io;
|
|
|
|
|
2009-06-30 17:10:52 +08:00
|
|
|
if (!gw->rfcomm) {
|
|
|
|
gw->sco_start_cb = cb;
|
|
|
|
gw->sco_start_cb_data = user_data;
|
|
|
|
get_records(dev);
|
|
|
|
} else if (!gw->sco) {
|
2009-04-08 16:46:55 +08:00
|
|
|
gw->sco_start_cb = cb;
|
|
|
|
gw->sco_start_cb_data = user_data;
|
|
|
|
io = bt_io_connect(BT_IO_SCO, sco_connect_cb, dev, NULL, &err,
|
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &dev->src,
|
|
|
|
BT_IO_OPT_DEST_BDADDR, &dev->dst,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (!io) {
|
|
|
|
error("%s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-02-05 02:48:11 +08:00
|
|
|
} else if (cb)
|
|
|
|
cb(dev, err, user_data);
|
2009-04-08 16:46:55 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gateway_config_stream(struct audio_device *dev, gateway_stream_cb_t sco_cb,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
|
|
|
|
if (!gw->rfcomm) {
|
|
|
|
gw->sco_start_cb = sco_cb;
|
|
|
|
gw->sco_start_cb_data = user_data;
|
|
|
|
return get_records(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sco_cb)
|
2010-02-05 02:48:11 +08:00
|
|
|
sco_cb(dev, NULL, user_data);
|
2009-04-08 16:46:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gateway_cancel_stream(struct audio_device *dev, unsigned int id)
|
|
|
|
{
|
|
|
|
gateway_close(dev);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gateway_get_sco_fd(struct audio_device *dev)
|
|
|
|
{
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
|
|
|
|
if (!gw || !gw->sco)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return g_io_channel_unix_get_fd(gw->sco);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gateway_suspend_stream(struct audio_device *dev)
|
|
|
|
{
|
|
|
|
struct gateway *gw = dev->gateway;
|
|
|
|
|
|
|
|
if (!gw || !gw->sco)
|
|
|
|
return;
|
|
|
|
|
2009-06-30 17:10:52 +08:00
|
|
|
g_io_channel_shutdown(gw->sco, TRUE, NULL);
|
2009-04-08 16:46:55 +08:00
|
|
|
g_io_channel_unref(gw->sco);
|
|
|
|
gw->sco = NULL;
|
|
|
|
gw->sco_start_cb = NULL;
|
|
|
|
gw->sco_start_cb_data = NULL;
|
2011-02-10 18:41:01 +08:00
|
|
|
change_state(dev, GATEWAY_STATE_CONNECTED);
|
2009-04-08 16:46:55 +08:00
|
|
|
}
|