2007-05-27 00:31:17 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2007-10-24 01:17:47 +08:00
|
|
|
* Copyright (C) 2006-2007 Nokia Corporation
|
2007-05-27 00:31:17 +08:00
|
|
|
* Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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-05-27 01:17:34 +08:00
|
|
|
#include <stdio.h>
|
2007-06-12 07:26:24 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2007-05-27 01:17:34 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2007-08-13 16:14:22 +08:00
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/sdp.h>
|
|
|
|
#include <dbus/dbus.h>
|
2007-05-27 01:17:34 +08:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "logging.h"
|
|
|
|
#include "dbus.h"
|
2007-08-13 16:14:22 +08:00
|
|
|
#include "ipc.h"
|
2007-08-13 07:58:15 +08:00
|
|
|
#include "device.h"
|
2007-06-07 01:58:19 +08:00
|
|
|
#include "manager.h"
|
2007-08-16 23:42:10 +08:00
|
|
|
#include "avdtp.h"
|
|
|
|
#include "a2dp.h"
|
|
|
|
#include "headset.h"
|
|
|
|
#include "sink.h"
|
2007-08-11 19:05:24 +08:00
|
|
|
#include "unix.h"
|
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
typedef enum {
|
|
|
|
TYPE_NONE,
|
|
|
|
TYPE_HEADSET,
|
|
|
|
TYPE_SINK,
|
|
|
|
TYPE_SOURCE
|
|
|
|
} service_type_t;
|
|
|
|
|
|
|
|
typedef void (*notify_cb_t) (struct device *dev, void *data);
|
|
|
|
|
2007-08-17 05:48:33 +08:00
|
|
|
struct a2dp_data {
|
|
|
|
struct avdtp *session;
|
|
|
|
struct avdtp_stream *stream;
|
2007-08-24 20:15:20 +08:00
|
|
|
struct a2dp_sep *sep;
|
2007-08-17 05:48:33 +08:00
|
|
|
};
|
|
|
|
|
2007-08-31 19:51:43 +08:00
|
|
|
struct headset_data {
|
|
|
|
headset_lock_t lock;
|
|
|
|
};
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
struct unix_client {
|
|
|
|
struct device *dev;
|
2007-12-04 06:41:29 +08:00
|
|
|
GSList *caps;
|
2007-08-16 23:42:10 +08:00
|
|
|
service_type_t type;
|
2007-08-27 20:05:10 +08:00
|
|
|
char *interface;
|
2007-08-16 23:42:10 +08:00
|
|
|
union {
|
2007-08-17 05:48:33 +08:00
|
|
|
struct a2dp_data a2dp;
|
2007-08-31 19:51:43 +08:00
|
|
|
struct headset_data hs;
|
2007-08-17 05:48:33 +08:00
|
|
|
} d;
|
2007-08-11 19:05:24 +08:00
|
|
|
int sock;
|
2007-11-22 04:24:11 +08:00
|
|
|
int access_mode;
|
|
|
|
int data_fd; /* To be deleted once two phase configuration is fully implemented */
|
2007-08-17 05:48:33 +08:00
|
|
|
unsigned int req_id;
|
|
|
|
unsigned int cb_id;
|
2007-12-04 06:41:29 +08:00
|
|
|
gboolean (*cancel) (struct device *dev, unsigned int id);
|
2007-08-11 19:05:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static GSList *clients = NULL;
|
2007-05-27 01:17:34 +08:00
|
|
|
|
|
|
|
static int unix_sock = -1;
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
static void client_free(struct unix_client *client)
|
|
|
|
{
|
2007-08-17 05:48:33 +08:00
|
|
|
struct a2dp_data *a2dp;
|
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
switch (client->type) {
|
|
|
|
case TYPE_SINK:
|
|
|
|
case TYPE_SOURCE:
|
2007-08-17 05:48:33 +08:00
|
|
|
a2dp = &client->d.a2dp;
|
|
|
|
if (client->cb_id > 0)
|
|
|
|
avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
|
2007-08-30 07:54:05 +08:00
|
|
|
client->cb_id);
|
2007-08-31 18:25:18 +08:00
|
|
|
if (a2dp->sep)
|
|
|
|
a2dp_sep_unlock(a2dp->sep, a2dp->session);
|
2007-08-30 07:54:05 +08:00
|
|
|
if (a2dp->session)
|
2007-08-17 05:48:33 +08:00
|
|
|
avdtp_unref(a2dp->session);
|
2007-08-16 23:42:10 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
if (client->sock >= 0)
|
|
|
|
close(client->sock);
|
2007-08-30 07:54:05 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
if (client->caps) {
|
|
|
|
g_slist_foreach(client->caps, (GFunc) g_free, NULL);
|
|
|
|
g_slist_free(client->caps);
|
|
|
|
}
|
2007-08-30 07:54:05 +08:00
|
|
|
|
2007-08-27 20:05:10 +08:00
|
|
|
g_free(client->interface);
|
2007-08-11 19:05:24 +08:00
|
|
|
g_free(client);
|
|
|
|
}
|
|
|
|
|
2007-06-12 07:26:24 +08:00
|
|
|
/* Pass file descriptor through local domain sockets (AF_LOCAL, formerly AF_UNIX)
|
|
|
|
and the sendmsg() system call with the cmsg_type field of a "struct cmsghdr" set
|
2007-11-22 04:24:11 +08:00
|
|
|
to SCM_RIGHTS and the data being an integer value equal to the handle of the
|
2007-06-12 07:26:24 +08:00
|
|
|
file descriptor to be passed.*/
|
2007-09-05 22:27:18 +08:00
|
|
|
static int unix_sendmsg_fd(int sock, int fd)
|
2007-06-12 07:26:24 +08:00
|
|
|
{
|
2007-09-05 22:27:18 +08:00
|
|
|
char cmsg_b[CMSG_SPACE(sizeof(int))], m = 'm';
|
2007-06-12 07:26:24 +08:00
|
|
|
struct cmsghdr *cmsg;
|
2007-09-05 22:27:18 +08:00
|
|
|
struct iovec iov = { &m, sizeof(m) };
|
|
|
|
struct msghdr msgh;
|
|
|
|
|
|
|
|
memset(&msgh, 0, sizeof(msgh));
|
|
|
|
msgh.msg_iov = &iov;
|
|
|
|
msgh.msg_iovlen = 1;
|
|
|
|
msgh.msg_control = &cmsg_b;
|
|
|
|
msgh.msg_controllen = CMSG_LEN(sizeof(int));
|
2007-06-12 07:26:24 +08:00
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msgh);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
|
|
|
/* Initialize the payload */
|
2007-06-12 14:43:16 +08:00
|
|
|
(*(int *) CMSG_DATA(cmsg)) = fd;
|
2007-06-12 07:26:24 +08:00
|
|
|
|
|
|
|
return sendmsg(sock, &msgh, MSG_NOSIGNAL);
|
|
|
|
}
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
static void unix_ipc_sendmsg(struct unix_client *client,
|
|
|
|
const bt_audio_msg_header_t *msg)
|
|
|
|
{
|
|
|
|
info("Audio API: sending %s", bt_audio_strmsg(msg->msg_type));
|
|
|
|
|
|
|
|
if (send(client->sock, msg, BT_AUDIO_IPC_PACKET_SIZE, 0) < 0)
|
|
|
|
error("Error %s(%d)", strerror(errno), errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unix_ipc_error(struct unix_client *client, int type, int err)
|
|
|
|
{
|
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_getcapabilities_rsp *rsp = (void *) buf;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
rsp->h.msg_type = type;
|
|
|
|
rsp->posix_errno = err;
|
|
|
|
|
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
|
|
|
}
|
|
|
|
|
2007-08-24 07:37:15 +08:00
|
|
|
static service_type_t select_service(struct device *dev, const char *interface)
|
2007-08-16 23:42:10 +08:00
|
|
|
{
|
2007-08-24 07:37:15 +08:00
|
|
|
if (!interface) {
|
|
|
|
if (dev->sink && avdtp_is_connected(&dev->src, &dev->dst))
|
|
|
|
return TYPE_SINK;
|
|
|
|
else if (dev->headset && headset_is_active(dev))
|
|
|
|
return TYPE_HEADSET;
|
|
|
|
else if (dev->sink)
|
|
|
|
return TYPE_SINK;
|
|
|
|
else if (dev->headset)
|
|
|
|
return TYPE_HEADSET;
|
|
|
|
} else if (!strcmp(interface, AUDIO_SINK_INTERFACE) && dev->sink)
|
2007-08-16 23:42:10 +08:00
|
|
|
return TYPE_SINK;
|
2007-08-24 07:37:15 +08:00
|
|
|
else if (!strcmp(interface, AUDIO_HEADSET_INTERFACE) && dev->headset)
|
2007-08-16 23:42:10 +08:00
|
|
|
return TYPE_HEADSET;
|
2007-08-24 07:37:15 +08:00
|
|
|
|
|
|
|
return TYPE_NONE;
|
2007-08-16 23:42:10 +08:00
|
|
|
}
|
|
|
|
|
2007-08-17 05:48:33 +08:00
|
|
|
static void stream_state_changed(struct avdtp_stream *stream,
|
|
|
|
avdtp_state_t old_state,
|
|
|
|
avdtp_state_t new_state,
|
|
|
|
struct avdtp_error *err,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct unix_client *client = user_data;
|
|
|
|
struct a2dp_data *a2dp = &client->d.a2dp;
|
|
|
|
|
|
|
|
switch (new_state) {
|
|
|
|
case AVDTP_STATE_IDLE:
|
2007-08-31 18:25:18 +08:00
|
|
|
if (a2dp->sep) {
|
|
|
|
a2dp_sep_unlock(a2dp->sep, a2dp->session);
|
|
|
|
a2dp->sep = NULL;
|
|
|
|
}
|
2007-08-19 23:27:56 +08:00
|
|
|
client->dev = NULL;
|
2007-08-17 05:48:33 +08:00
|
|
|
if (a2dp->session) {
|
|
|
|
avdtp_unref(a2dp->session);
|
|
|
|
a2dp->session = NULL;
|
|
|
|
}
|
|
|
|
a2dp->stream = NULL;
|
|
|
|
client->cb_id = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-12-04 06:41:29 +08:00
|
|
|
/*
|
|
|
|
static void headset_discovery_complete(struct device *dev, void *user_data)
|
2007-08-27 19:31:01 +08:00
|
|
|
{
|
|
|
|
struct unix_client *client = user_data;
|
2007-11-22 04:24:11 +08:00
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_getcapabilities_rsp *rsp = (void *) buf;
|
2007-08-31 19:51:43 +08:00
|
|
|
struct headset_data *hs = &client->d.hs;
|
2007-08-27 19:31:01 +08:00
|
|
|
|
2007-08-28 20:23:34 +08:00
|
|
|
client->req_id = 0;
|
|
|
|
|
2007-08-27 19:31:01 +08:00
|
|
|
if (!dev) {
|
2007-12-04 06:41:29 +08:00
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
|
2007-08-27 19:31:01 +08:00
|
|
|
client->dev = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
switch (client->access_mode) {
|
|
|
|
case BT_CAPABILITIES_ACCESS_MODE_READ:
|
2007-08-31 19:51:43 +08:00
|
|
|
hs->lock = HEADSET_LOCK_READ;
|
|
|
|
break;
|
2007-11-22 04:24:11 +08:00
|
|
|
case BT_CAPABILITIES_ACCESS_MODE_WRITE:
|
2007-08-31 19:51:43 +08:00
|
|
|
hs->lock = HEADSET_LOCK_WRITE;
|
|
|
|
break;
|
2007-11-22 04:24:11 +08:00
|
|
|
case BT_CAPABILITIES_ACCESS_MODE_READWRITE:
|
2007-08-31 19:51:43 +08:00
|
|
|
hs->lock = HEADSET_LOCK_READ | HEADSET_LOCK_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hs->lock = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!headset_lock(dev, hs->lock)) {
|
|
|
|
error("Unable to lock headset");
|
2007-12-04 06:41:29 +08:00
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
|
2007-08-31 19:51:43 +08:00
|
|
|
client->dev = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2007-08-28 20:22:29 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2007-08-27 19:31:01 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
rsp->h.msg_type = BT_GETCAPABILITIES_RSP;
|
|
|
|
rsp->transport = BT_CAPABILITIES_TRANSPORT_SCO;
|
|
|
|
rsp->sampling_rate = 8000;
|
2007-08-27 19:31:01 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
client->data_fd = headset_get_sco_fd(dev);
|
2007-08-27 19:31:01 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
2007-08-27 19:31:01 +08:00
|
|
|
}
|
2007-12-04 06:41:29 +08:00
|
|
|
*/
|
|
|
|
static void headset_setup_complete(struct device *dev, void *user_data)
|
|
|
|
{
|
|
|
|
struct unix_client *client = user_data;
|
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_setconfiguration_rsp *rsp = (void *) buf;
|
|
|
|
struct headset_data *hs = &client->d.hs;
|
2007-08-27 19:31:01 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
client->req_id = 0;
|
|
|
|
|
|
|
|
if (!dev) {
|
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
|
|
|
|
client->dev = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (client->access_mode) {
|
|
|
|
case BT_CAPABILITIES_ACCESS_MODE_READ:
|
|
|
|
hs->lock = HEADSET_LOCK_READ;
|
|
|
|
break;
|
|
|
|
case BT_CAPABILITIES_ACCESS_MODE_WRITE:
|
|
|
|
hs->lock = HEADSET_LOCK_WRITE;
|
|
|
|
break;
|
|
|
|
case BT_CAPABILITIES_ACCESS_MODE_READWRITE:
|
|
|
|
hs->lock = HEADSET_LOCK_READ | HEADSET_LOCK_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hs->lock = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!headset_lock(dev, hs->lock)) {
|
|
|
|
error("Unable to lock headset");
|
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
|
|
|
|
client->dev = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
rsp->h.msg_type = BT_SETCONFIGURATION_RSP;
|
|
|
|
rsp->transport = BT_CAPABILITIES_TRANSPORT_SCO;
|
|
|
|
rsp->access_mode = client->access_mode;
|
|
|
|
|
|
|
|
client->data_fd = headset_get_sco_fd(dev);
|
|
|
|
|
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void a2dp_discovery_complete(struct avdtp *session, GSList *seps,
|
|
|
|
struct avdtp_error *err,
|
|
|
|
void *user_data)
|
2007-08-16 23:42:10 +08:00
|
|
|
{
|
|
|
|
struct unix_client *client = user_data;
|
2007-11-22 04:24:11 +08:00
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_getcapabilities_rsp *rsp = (void *) buf;
|
2007-12-04 06:41:29 +08:00
|
|
|
struct a2dp_data *a2dp = &client->d.a2dp;
|
|
|
|
struct sbc_codec_cap *sbc_cap = NULL;
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
client->req_id = 0;
|
|
|
|
|
|
|
|
rsp->h.msg_type = BT_GETCAPABILITIES_RSP;
|
|
|
|
rsp->transport = BT_CAPABILITIES_TRANSPORT_A2DP;
|
|
|
|
|
|
|
|
for (l = seps; l; l = g_slist_next(l)) {
|
|
|
|
struct avdtp_remote_sep *rsep = l->data;
|
|
|
|
struct avdtp_service_capability *cap;
|
|
|
|
struct avdtp_media_codec_capability *codec_cap;
|
|
|
|
|
|
|
|
cap = avdtp_get_codec(rsep);
|
|
|
|
|
|
|
|
if (cap->category != AVDTP_MEDIA_CODEC)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
codec_cap = (void *) cap->data;
|
|
|
|
|
|
|
|
if (codec_cap->media_codec_type == A2DP_CODEC_SBC && !sbc_cap)
|
|
|
|
sbc_cap = (void *) codec_cap;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* endianess prevent direct cast */
|
|
|
|
if (sbc_cap) {
|
|
|
|
rsp->sbc_capabilities.channel_mode = sbc_cap->channel_mode;
|
|
|
|
rsp->sbc_capabilities.frequency = sbc_cap->frequency;
|
|
|
|
rsp->sbc_capabilities.allocation_method = sbc_cap->allocation_method;
|
|
|
|
rsp->sbc_capabilities.subbands = sbc_cap->subbands;
|
|
|
|
rsp->sbc_capabilities.block_length = sbc_cap->block_length;
|
|
|
|
rsp->sbc_capabilities.min_bitpool = sbc_cap->min_bitpool;
|
|
|
|
rsp->sbc_capabilities.max_bitpool = sbc_cap->max_bitpool;
|
|
|
|
}
|
|
|
|
|
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
error("discovery failed");
|
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
|
|
|
|
|
|
|
|
avdtp_unref(a2dp->session);
|
|
|
|
|
|
|
|
a2dp->session = NULL;
|
|
|
|
a2dp->stream = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void a2dp_config_complete(struct avdtp *session, struct a2dp_sep *sep,
|
|
|
|
struct avdtp_stream *stream,
|
|
|
|
struct avdtp_error *err,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct unix_client *client = user_data;
|
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_setconfiguration_rsp *rsp = (void *) buf;
|
2007-08-17 05:48:33 +08:00
|
|
|
struct a2dp_data *a2dp = &client->d.a2dp;
|
2007-11-06 01:02:16 +08:00
|
|
|
uint16_t imtu, omtu;
|
2007-08-16 23:42:10 +08:00
|
|
|
GSList *caps;
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2007-08-17 05:48:33 +08:00
|
|
|
client->req_id = 0;
|
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
if (!stream)
|
|
|
|
goto failed;
|
|
|
|
|
2007-08-31 18:25:18 +08:00
|
|
|
if (!a2dp_sep_lock(sep, session)) {
|
2007-08-20 16:50:22 +08:00
|
|
|
error("Unable to lock A2DP source SEP");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2007-08-31 18:25:18 +08:00
|
|
|
a2dp->sep = sep;
|
2007-08-17 05:48:33 +08:00
|
|
|
a2dp->stream = stream;
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
if (!avdtp_stream_get_transport(stream, &client->data_fd, &imtu, &omtu, &caps)) {
|
2007-08-16 23:42:10 +08:00
|
|
|
error("Unable to get stream transport");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
rsp->h.msg_type = BT_SETCONFIGURATION_RSP;
|
2007-11-22 04:24:11 +08:00
|
|
|
rsp->transport = BT_CAPABILITIES_TRANSPORT_A2DP;
|
|
|
|
client->access_mode = BT_CAPABILITIES_ACCESS_MODE_WRITE;
|
|
|
|
rsp->access_mode = client->access_mode;
|
2007-11-06 01:02:16 +08:00
|
|
|
/* FIXME: Use imtu when fd_opt is CFG_FD_OPT_READ */
|
2007-11-22 04:24:11 +08:00
|
|
|
rsp->link_mtu = omtu;
|
2007-08-16 23:42:10 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
2007-08-16 23:42:10 +08:00
|
|
|
|
2007-08-17 05:48:33 +08:00
|
|
|
client->cb_id = avdtp_stream_add_cb(session, stream,
|
|
|
|
stream_state_changed, client);
|
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
2007-12-04 06:41:29 +08:00
|
|
|
error("setup failed");
|
|
|
|
|
|
|
|
if (a2dp->sep) {
|
|
|
|
a2dp_sep_unlock(a2dp->sep, a2dp->session);
|
|
|
|
a2dp->sep = NULL;
|
|
|
|
}
|
|
|
|
unix_ipc_error(client, BT_SETCONFIGURATION_RSP, EIO);
|
|
|
|
|
|
|
|
avdtp_unref(a2dp->session);
|
|
|
|
|
|
|
|
a2dp->session = NULL;
|
|
|
|
a2dp->stream = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void a2dp_resume_complete(struct avdtp *session,
|
|
|
|
struct avdtp_error *err, void *user_data)
|
|
|
|
{
|
|
|
|
struct unix_client *client = user_data;
|
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_streamstart_rsp *rsp = (void *) buf;
|
|
|
|
struct bt_datafd_ind *ind = (void *) buf;
|
|
|
|
struct a2dp_data *a2dp = &client->d.a2dp;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
rsp->h.msg_type = BT_STREAMSTART_RSP;
|
|
|
|
rsp->posix_errno = 0;
|
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
ind->h.msg_type = BT_STREAMFD_IND;
|
|
|
|
unix_ipc_sendmsg(client, &ind->h);
|
|
|
|
|
|
|
|
if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
|
|
|
|
error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
error("resume failed");
|
|
|
|
|
|
|
|
if (a2dp->sep) {
|
|
|
|
a2dp_sep_unlock(a2dp->sep, a2dp->session);
|
|
|
|
a2dp->sep = NULL;
|
|
|
|
}
|
|
|
|
unix_ipc_error(client, BT_STREAMSTART_REQ, EIO);
|
|
|
|
|
|
|
|
avdtp_unref(a2dp->session);
|
|
|
|
|
|
|
|
a2dp->session = NULL;
|
|
|
|
a2dp->stream = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void a2dp_suspend_complete(struct avdtp *session,
|
|
|
|
struct avdtp_error *err, void *user_data)
|
|
|
|
{
|
|
|
|
struct unix_client *client = user_data;
|
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_streamstart_rsp *rsp = (void *) buf;
|
|
|
|
struct a2dp_data *a2dp = &client->d.a2dp;
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
rsp->h.msg_type = BT_STREAMSTOP_RSP;
|
|
|
|
rsp->posix_errno = 0;
|
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
error("suspend failed");
|
|
|
|
|
2007-08-31 18:25:18 +08:00
|
|
|
if (a2dp->sep) {
|
|
|
|
a2dp_sep_unlock(a2dp->sep, a2dp->session);
|
|
|
|
a2dp->sep = NULL;
|
|
|
|
}
|
2007-12-04 06:41:29 +08:00
|
|
|
unix_ipc_error(client, BT_STREAMSTOP_REQ, EIO);
|
2007-08-30 07:54:05 +08:00
|
|
|
|
2007-08-17 05:48:33 +08:00
|
|
|
avdtp_unref(a2dp->session);
|
2007-08-30 07:54:05 +08:00
|
|
|
|
2007-08-17 05:48:33 +08:00
|
|
|
a2dp->session = NULL;
|
|
|
|
a2dp->stream = NULL;
|
2007-08-16 23:42:10 +08:00
|
|
|
}
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
static void start_discovery(struct device *dev, struct unix_client *client)
|
2007-06-30 07:19:36 +08:00
|
|
|
{
|
2007-08-17 05:48:33 +08:00
|
|
|
struct a2dp_data *a2dp;
|
2007-08-27 20:05:10 +08:00
|
|
|
unsigned int id;
|
2007-12-04 06:41:29 +08:00
|
|
|
int err = 0;
|
2007-06-30 07:19:36 +08:00
|
|
|
|
2007-08-27 20:05:10 +08:00
|
|
|
client->type = select_service(dev, client->interface);
|
2007-08-16 23:42:10 +08:00
|
|
|
|
|
|
|
switch (client->type) {
|
|
|
|
case TYPE_SINK:
|
2007-08-17 05:48:33 +08:00
|
|
|
a2dp = &client->d.a2dp;
|
|
|
|
|
|
|
|
if (!a2dp->session)
|
|
|
|
a2dp->session = avdtp_get(&dev->src, &dev->dst);
|
2007-08-16 23:42:10 +08:00
|
|
|
|
2007-08-21 14:48:14 +08:00
|
|
|
if (!a2dp->session) {
|
|
|
|
error("Unable to get a session");
|
|
|
|
goto failed;
|
2007-08-24 07:37:15 +08:00
|
|
|
}
|
2007-08-21 14:48:14 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
err = avdtp_discover(a2dp->session, a2dp_discovery_complete,
|
|
|
|
client);
|
|
|
|
if (err)
|
|
|
|
goto failed;
|
2007-08-16 23:42:10 +08:00
|
|
|
break;
|
2007-08-30 07:54:05 +08:00
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
case TYPE_HEADSET:
|
2007-08-30 07:54:05 +08:00
|
|
|
id = headset_request_stream(dev, headset_setup_complete, client);
|
2007-12-04 06:41:29 +08:00
|
|
|
client->cancel = headset_cancel_stream;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("No known services for device");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, err ? : EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start_config(struct device *dev, struct unix_client *client)
|
|
|
|
{
|
|
|
|
struct a2dp_data *a2dp;
|
|
|
|
unsigned int id;
|
|
|
|
|
|
|
|
client->type = select_service(dev, client->interface);
|
|
|
|
|
|
|
|
switch (client->type) {
|
|
|
|
case TYPE_SINK:
|
|
|
|
a2dp = &client->d.a2dp;
|
|
|
|
|
|
|
|
if (!a2dp->session)
|
|
|
|
a2dp->session = avdtp_get(&dev->src, &dev->dst);
|
|
|
|
|
|
|
|
if (!a2dp->session) {
|
|
|
|
error("Unable to get a session");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = a2dp_source_config(a2dp->session, a2dp_config_complete,
|
|
|
|
client->caps, client);
|
|
|
|
client->cancel = a2dp_source_cancel;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_HEADSET:
|
|
|
|
id = headset_request_stream(dev, headset_setup_complete, client);
|
|
|
|
client->cancel = headset_cancel_stream;
|
2007-08-16 23:42:10 +08:00
|
|
|
break;
|
2007-08-30 07:54:05 +08:00
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
default:
|
|
|
|
error("No known services for device");
|
2007-08-11 19:05:24 +08:00
|
|
|
goto failed;
|
2007-08-16 23:42:10 +08:00
|
|
|
}
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2007-08-27 19:31:01 +08:00
|
|
|
if (id == 0) {
|
2007-12-04 06:41:29 +08:00
|
|
|
error("config failed");
|
2007-08-27 19:31:01 +08:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
client->req_id = id;
|
2007-08-11 19:05:24 +08:00
|
|
|
client->dev = dev;
|
2007-08-27 19:31:01 +08:00
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
2007-12-04 06:41:29 +08:00
|
|
|
unix_ipc_error(client, BT_SETCONFIGURATION_RSP, EIO);
|
2007-06-30 07:19:36 +08:00
|
|
|
}
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
static void start_resume(struct device *dev, struct unix_client *client)
|
2007-08-27 20:05:10 +08:00
|
|
|
{
|
2007-12-04 06:41:29 +08:00
|
|
|
struct a2dp_data *a2dp;
|
|
|
|
unsigned int id;
|
2007-08-27 20:05:10 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
client->type = select_service(dev, client->interface);
|
|
|
|
|
|
|
|
switch (client->type) {
|
|
|
|
case TYPE_SINK:
|
|
|
|
a2dp = &client->d.a2dp;
|
|
|
|
|
|
|
|
if (!a2dp->session)
|
|
|
|
a2dp->session = avdtp_get(&dev->src, &dev->dst);
|
|
|
|
|
|
|
|
if (!a2dp->session) {
|
|
|
|
error("Unable to get a session");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!a2dp->sep) {
|
|
|
|
error("Unable to get a sep");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = a2dp_source_resume(a2dp->session, a2dp->sep,
|
|
|
|
a2dp_resume_complete, client);
|
|
|
|
client->cancel = a2dp_source_cancel;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_HEADSET:
|
|
|
|
id = headset_request_stream(dev, headset_setup_complete, client);
|
|
|
|
client->cancel = headset_cancel_stream;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("No known services for device");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == 0) {
|
|
|
|
error("resume failed");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
unix_ipc_error(client, BT_STREAMSTART_RSP, EIO);
|
2007-08-27 20:05:10 +08:00
|
|
|
}
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
static void start_suspend(struct device *dev, struct unix_client *client)
|
2007-08-30 03:38:37 +08:00
|
|
|
{
|
2007-12-04 06:41:29 +08:00
|
|
|
struct a2dp_data *a2dp;
|
|
|
|
unsigned int id;
|
|
|
|
|
|
|
|
client->type = select_service(dev, client->interface);
|
|
|
|
|
|
|
|
switch (client->type) {
|
|
|
|
case TYPE_SINK:
|
|
|
|
a2dp = &client->d.a2dp;
|
|
|
|
|
|
|
|
if (!a2dp->session)
|
|
|
|
a2dp->session = avdtp_get(&dev->src, &dev->dst);
|
|
|
|
|
|
|
|
if (!a2dp->session) {
|
|
|
|
error("Unable to get a session");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!a2dp->sep) {
|
|
|
|
error("Unable to get a sep");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = a2dp_source_suspend(a2dp->session, a2dp->sep,
|
|
|
|
a2dp_suspend_complete, client);
|
|
|
|
client->cancel = a2dp_source_cancel;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_HEADSET:
|
|
|
|
id = headset_request_stream(dev, headset_setup_complete, client);
|
|
|
|
client->cancel = headset_cancel_stream;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("No known services for device");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == 0) {
|
|
|
|
error("suspend failed");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
unix_ipc_error(client, BT_STREAMSTOP_RSP, EIO);
|
2007-11-22 04:24:11 +08:00
|
|
|
}
|
2007-08-30 03:38:37 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
static void create_cb(struct device *dev, void *user_data)
|
2007-11-22 04:24:11 +08:00
|
|
|
{
|
2007-12-04 06:41:29 +08:00
|
|
|
struct unix_client *client = user_data;
|
2007-08-30 03:38:37 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
if (!dev)
|
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
|
|
|
|
else
|
|
|
|
start_discovery(dev, client);
|
2007-08-30 03:38:37 +08:00
|
|
|
}
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
static void handle_getcapabilities_req(struct unix_client *client,
|
|
|
|
struct bt_getcapabilities_req *req)
|
2007-08-27 20:05:10 +08:00
|
|
|
{
|
|
|
|
struct device *dev;
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
str2ba(req->device, &bdaddr);
|
|
|
|
|
2007-08-27 20:05:10 +08:00
|
|
|
if (client->interface) {
|
|
|
|
g_free(client->interface);
|
|
|
|
client->interface = NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
if (req->transport == BT_CAPABILITIES_TRANSPORT_SCO)
|
2007-08-27 20:05:10 +08:00
|
|
|
client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
|
2007-11-22 04:24:11 +08:00
|
|
|
else if (req->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
|
2007-08-27 20:05:10 +08:00
|
|
|
client->interface = g_strdup(AUDIO_SINK_INTERFACE);
|
|
|
|
|
|
|
|
if (!manager_find_device(&bdaddr, NULL, FALSE)) {
|
|
|
|
if (!bacmp(&bdaddr, BDADDR_ANY))
|
|
|
|
goto failed;
|
2007-08-30 16:12:20 +08:00
|
|
|
if (!manager_create_device(&bdaddr, create_cb, client))
|
|
|
|
goto failed;
|
2007-08-27 20:05:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = manager_find_device(&bdaddr, client->interface, TRUE);
|
|
|
|
if (!dev)
|
|
|
|
dev = manager_find_device(&bdaddr, client->interface, FALSE);
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
goto failed;
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
start_discovery(dev, client);
|
2007-08-30 03:38:37 +08:00
|
|
|
|
2007-08-27 20:05:10 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
2007-12-04 06:41:29 +08:00
|
|
|
unix_ipc_error(client, BT_GETCAPABILITIES_RSP, EIO);
|
2007-08-27 20:05:10 +08:00
|
|
|
}
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
static void handle_setconfiguration_req(struct unix_client *client,
|
|
|
|
struct bt_setconfiguration_req *req)
|
2007-06-30 07:19:36 +08:00
|
|
|
{
|
2007-12-04 06:41:29 +08:00
|
|
|
struct avdtp_service_capability *media_transport, *media_codec;
|
|
|
|
struct sbc_codec_cap sbc_cap;
|
|
|
|
struct device *dev;
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
int err = 0;
|
2007-11-22 04:24:11 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
if (!req->access_mode) {
|
|
|
|
err = EINVAL;
|
|
|
|
goto failed;
|
|
|
|
}
|
2007-11-22 04:24:11 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
str2ba(req->device, &bdaddr);
|
|
|
|
|
|
|
|
if (client->interface) {
|
|
|
|
g_free(client->interface);
|
|
|
|
client->interface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->transport == BT_CAPABILITIES_TRANSPORT_SCO)
|
|
|
|
client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
|
|
|
|
else if (req->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
|
|
|
|
client->interface = g_strdup(AUDIO_SINK_INTERFACE);
|
|
|
|
|
|
|
|
if (!manager_find_device(&bdaddr, NULL, FALSE)) {
|
|
|
|
if (!bacmp(&bdaddr, BDADDR_ANY))
|
|
|
|
goto failed;
|
|
|
|
if (!manager_create_device(&bdaddr, create_cb, client))
|
|
|
|
goto failed;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = manager_find_device(&bdaddr, client->interface, TRUE);
|
|
|
|
if (!dev)
|
|
|
|
dev = manager_find_device(&bdaddr, client->interface, FALSE);
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
client->access_mode = req->access_mode;
|
|
|
|
|
|
|
|
if (client->caps) {
|
|
|
|
g_slist_foreach(client->caps, (GFunc) g_free, NULL);
|
|
|
|
g_slist_free(client->caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
|
|
|
|
NULL, 0);
|
|
|
|
|
|
|
|
client->caps = g_slist_append(client->caps, media_transport);
|
|
|
|
|
|
|
|
memset(&sbc_cap, 0, sizeof(sbc_cap));
|
|
|
|
|
|
|
|
sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
|
|
|
|
sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC;
|
|
|
|
sbc_cap.channel_mode = req->sbc_capabilities.channel_mode;
|
|
|
|
sbc_cap.frequency = req->sbc_capabilities.frequency;
|
|
|
|
sbc_cap.allocation_method = req->sbc_capabilities.allocation_method;
|
|
|
|
sbc_cap.subbands = req->sbc_capabilities.subbands;
|
|
|
|
sbc_cap.block_length = req->sbc_capabilities.block_length ;
|
|
|
|
sbc_cap.min_bitpool = req->sbc_capabilities.min_bitpool;
|
|
|
|
sbc_cap.max_bitpool = req->sbc_capabilities.max_bitpool;
|
|
|
|
|
|
|
|
media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
|
|
|
|
sizeof(sbc_cap));
|
|
|
|
|
|
|
|
client->caps = g_slist_append(client->caps, media_codec);
|
|
|
|
|
|
|
|
start_config(dev, client);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
unix_ipc_error(client, BT_SETCONFIGURATION_RSP, err ? : EIO);
|
2007-06-30 07:19:36 +08:00
|
|
|
}
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
static void handle_streamstart_req(struct unix_client *client,
|
|
|
|
struct bt_streamstart_req *req)
|
2007-09-04 03:12:40 +08:00
|
|
|
{
|
2007-12-04 06:41:29 +08:00
|
|
|
if (!client->dev)
|
|
|
|
goto failed;
|
2007-09-04 03:12:40 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
start_resume(client->dev, client);
|
2007-09-04 03:12:40 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
return;
|
2007-11-22 04:24:11 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
failed:
|
|
|
|
unix_ipc_error(client, BT_STREAMSTART_REQ, EIO);
|
2007-11-22 04:24:11 +08:00
|
|
|
}
|
2007-09-04 03:12:40 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
static void handle_streamstop_req(struct unix_client *client,
|
|
|
|
struct bt_streamstop_req *req)
|
|
|
|
{
|
2007-12-04 06:41:29 +08:00
|
|
|
if (!client->dev)
|
|
|
|
goto failed;
|
2007-09-04 03:12:40 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
start_suspend(client->dev, client);
|
2007-11-22 04:24:11 +08:00
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
unix_ipc_error(client, BT_STREAMSTOP_REQ, EIO);
|
2007-09-04 03:12:40 +08:00
|
|
|
}
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
static void handle_control_req(struct unix_client *client,
|
|
|
|
struct bt_control_req *req)
|
2007-05-27 01:17:34 +08:00
|
|
|
{
|
2007-11-22 04:24:11 +08:00
|
|
|
/* FIXME: really implement that */
|
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
struct bt_setconfiguration_rsp *rsp = (void *) buf;
|
2007-06-30 07:19:36 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
rsp->h.msg_type = BT_CONTROL_RSP;
|
|
|
|
rsp->posix_errno = 0;
|
2007-09-04 03:12:40 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
unix_ipc_sendmsg(client, &rsp->h);
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean client_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
|
|
|
|
{
|
2007-11-22 04:24:11 +08:00
|
|
|
char buf[BT_AUDIO_IPC_PACKET_SIZE];
|
|
|
|
bt_audio_msg_header_t *msghdr = (void *) buf;
|
2007-08-11 19:05:24 +08:00
|
|
|
struct unix_client *client = data;
|
2007-11-22 04:24:11 +08:00
|
|
|
int len;
|
2007-08-31 18:25:18 +08:00
|
|
|
struct a2dp_data *a2dp = &client->d.a2dp;
|
2007-08-31 19:51:43 +08:00
|
|
|
struct headset_data *hs = &client->d.hs;
|
2007-11-22 04:24:11 +08:00
|
|
|
const char *type;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
|
|
|
if (cond & G_IO_NVAL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (cond & (G_IO_HUP | G_IO_ERR)) {
|
2007-08-24 20:15:20 +08:00
|
|
|
debug("Unix client disconnected (fd=%d)", client->sock);
|
2007-08-31 18:25:18 +08:00
|
|
|
switch (client->type) {
|
|
|
|
case TYPE_HEADSET:
|
|
|
|
if (client->dev)
|
2007-08-31 19:51:43 +08:00
|
|
|
headset_unlock(client->dev, hs->lock);
|
2007-08-31 18:25:18 +08:00
|
|
|
break;
|
|
|
|
case TYPE_SOURCE:
|
|
|
|
case TYPE_SINK:
|
|
|
|
if (a2dp->sep) {
|
|
|
|
a2dp_sep_unlock(a2dp->sep, a2dp->session);
|
|
|
|
a2dp->sep = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
if (client->cancel && client->req_id > 0)
|
|
|
|
client->cancel(client->dev, client->req_id);
|
2007-08-11 19:05:24 +08:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
len = recv(client->sock, buf, sizeof(buf), MSG_WAITALL);
|
2007-08-11 19:05:24 +08:00
|
|
|
if (len < 0) {
|
|
|
|
error("recv: %s (%d)", strerror(errno), errno);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
if ((type = bt_audio_strmsg(msghdr->msg_type)))
|
|
|
|
info("Audio API: received %s", type);
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2007-11-22 04:24:11 +08:00
|
|
|
switch (msghdr->msg_type) {
|
|
|
|
case BT_GETCAPABILITIES_REQ:
|
|
|
|
handle_getcapabilities_req(client,
|
|
|
|
(struct bt_getcapabilities_req *) msghdr);
|
2007-08-11 19:05:24 +08:00
|
|
|
break;
|
2007-11-22 04:24:11 +08:00
|
|
|
case BT_SETCONFIGURATION_REQ:
|
|
|
|
handle_setconfiguration_req(client,
|
|
|
|
(struct bt_setconfiguration_req *) msghdr);
|
2007-08-11 19:05:24 +08:00
|
|
|
break;
|
2007-11-22 04:24:11 +08:00
|
|
|
case BT_STREAMSTART_REQ:
|
|
|
|
handle_streamstart_req(client,
|
|
|
|
(struct bt_streamstart_req *) msghdr);
|
2007-08-11 19:05:24 +08:00
|
|
|
break;
|
2007-11-22 04:24:11 +08:00
|
|
|
case BT_STREAMSTOP_REQ:
|
|
|
|
handle_streamstop_req(client,
|
|
|
|
(struct bt_streamstop_req *) msghdr);
|
|
|
|
break;
|
|
|
|
case BT_CONTROL_REQ:
|
|
|
|
handle_control_req(client,
|
|
|
|
(struct bt_control_req *) msghdr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Audio API: received unexpected packet type %d",
|
|
|
|
msghdr->msg_type);
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
2007-07-10 21:34:57 +08:00
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
return TRUE;
|
2007-06-30 07:19:36 +08:00
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
failed:
|
|
|
|
clients = g_slist_remove(clients, client);
|
|
|
|
client_free(client);
|
|
|
|
return FALSE;
|
2007-06-30 07:19:36 +08:00
|
|
|
}
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
static gboolean server_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
|
2007-06-30 07:19:36 +08:00
|
|
|
{
|
2007-05-27 01:17:34 +08:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addrlen;
|
2007-08-11 19:05:24 +08:00
|
|
|
int sk, cli_sk;
|
|
|
|
struct unix_client *client;
|
|
|
|
GIOChannel *io;
|
2007-05-27 01:17:34 +08:00
|
|
|
|
|
|
|
if (cond & G_IO_NVAL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (cond & (G_IO_HUP | G_IO_ERR)) {
|
|
|
|
g_io_channel_close(chan);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk = g_io_channel_unix_get_fd(chan);
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addrlen = sizeof(addr);
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
cli_sk = accept(sk, (struct sockaddr *) &addr, &addrlen);
|
|
|
|
if (cli_sk < 0) {
|
2007-06-12 14:43:16 +08:00
|
|
|
error("accept: %s (%d)", strerror(errno), errno);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-08-24 20:15:20 +08:00
|
|
|
debug("Accepted new client connection on unix socket (fd=%d)", cli_sk);
|
2007-05-27 01:17:34 +08:00
|
|
|
|
2007-08-14 03:59:52 +08:00
|
|
|
client = g_new0(struct unix_client, 1);
|
2007-08-11 19:05:24 +08:00
|
|
|
client->sock = cli_sk;
|
|
|
|
clients = g_slist_append(clients, client);
|
2007-05-27 01:17:34 +08:00
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
io = g_io_channel_unix_new(cli_sk);
|
|
|
|
g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
|
2007-08-30 07:54:05 +08:00
|
|
|
client_cb, client);
|
2007-08-11 19:05:24 +08:00
|
|
|
g_io_channel_unref(io);
|
2007-06-07 01:58:19 +08:00
|
|
|
|
2007-05-27 01:17:34 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unix_init(void)
|
|
|
|
{
|
|
|
|
GIOChannel *io;
|
2007-06-12 07:26:24 +08:00
|
|
|
struct sockaddr_un addr = {
|
2007-11-22 04:24:11 +08:00
|
|
|
AF_UNIX, BT_IPC_SOCKET_NAME
|
2007-06-12 07:26:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int sk, err;
|
2007-05-27 01:17:34 +08:00
|
|
|
|
2007-06-12 07:26:24 +08:00
|
|
|
sk = socket(PF_LOCAL, SOCK_STREAM, 0);
|
2007-05-27 01:17:34 +08:00
|
|
|
if (sk < 0) {
|
2007-06-12 07:26:24 +08:00
|
|
|
err = errno;
|
|
|
|
error("Can't create unix socket: %s (%d)", strerror(err), err);
|
|
|
|
return -err;
|
2007-05-27 01:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
error("Can't bind unix socket: %s (%d)", strerror(errno), errno);
|
|
|
|
close(sk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_nonblocking(sk);
|
|
|
|
|
|
|
|
unix_sock = sk;
|
|
|
|
|
2007-06-12 07:26:24 +08:00
|
|
|
listen(sk, 1);
|
|
|
|
|
2007-05-27 01:17:34 +08:00
|
|
|
io = g_io_channel_unix_new(sk);
|
|
|
|
g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
|
2007-08-30 07:54:05 +08:00
|
|
|
server_cb, NULL);
|
2007-05-27 01:17:34 +08:00
|
|
|
g_io_channel_unref(io);
|
|
|
|
|
2007-06-06 06:44:36 +08:00
|
|
|
info("Unix socket created: %d", sk);
|
|
|
|
|
2007-05-27 01:17:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unix_exit(void)
|
|
|
|
{
|
2007-08-11 19:05:24 +08:00
|
|
|
g_slist_foreach(clients, (GFunc) client_free, NULL);
|
|
|
|
g_slist_free(clients);
|
2007-05-27 01:17:34 +08:00
|
|
|
close(unix_sock);
|
|
|
|
unix_sock = -1;
|
|
|
|
}
|