3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/socket.h>
38 #include <bluetooth/bluetooth.h>
39 #include <bluetooth/sdp.h>
40 #include <dbus/dbus.h>
56 #define check_nul(str) (str[sizeof(str) - 1] == '\0')
66 typedef void (*notify_cb_t) (struct audio_device *dev, void *data);
69 struct avdtp *session;
70 struct avdtp_stream *stream;
79 struct audio_device *dev;
85 struct a2dp_data a2dp;
86 struct headset_data hs;
90 int data_fd; /* To be deleted once two phase configuration is fully implemented */
93 gboolean (*cancel) (struct audio_device *dev, unsigned int id);
96 static GSList *clients = NULL;
98 static int unix_sock = -1;
100 static void client_free(void *data)
102 struct unix_client *client = data;
104 DBG("client_free(%p)", client);
106 if (client->cancel && client->dev && client->req_id > 0)
107 client->cancel(client->dev, client->req_id);
109 if (client->sock >= 0)
112 g_slist_free_full(client->caps, g_free);
114 g_free(client->interface);
118 static int set_nonblocking(int fd)
122 arg = fcntl(fd, F_GETFL);
126 /* Return if already nonblocking */
127 if (arg & O_NONBLOCK)
131 if (fcntl(fd, F_SETFL, arg) < 0)
137 /* Pass file descriptor through local domain sockets (AF_LOCAL, formerly
138 * AF_UNIX) and the sendmsg() system call with the cmsg_type field of a "struct
139 * cmsghdr" set to SCM_RIGHTS and the data being an integer value equal to the
140 * handle of the file descriptor to be passed. */
141 static int unix_sendmsg_fd(int sock, int fd)
143 char cmsg_b[CMSG_SPACE(sizeof(int))], m = 'm';
144 struct cmsghdr *cmsg;
145 struct iovec iov = { &m, sizeof(m) };
148 memset(&msgh, 0, sizeof(msgh));
151 msgh.msg_control = &cmsg_b;
152 msgh.msg_controllen = CMSG_LEN(sizeof(int));
154 cmsg = CMSG_FIRSTHDR(&msgh);
155 cmsg->cmsg_level = SOL_SOCKET;
156 cmsg->cmsg_type = SCM_RIGHTS;
157 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
158 /* Initialize the payload */
159 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
161 return sendmsg(sock, &msgh, MSG_NOSIGNAL);
164 static void unix_ipc_sendmsg(struct unix_client *client,
165 const bt_audio_msg_header_t *msg)
167 const char *type = bt_audio_strtype(msg->type);
168 const char *name = bt_audio_strname(msg->name);
170 DBG("Audio API: %s -> %s", type, name);
172 if (send(client->sock, msg, msg->length, 0) < 0)
173 error("Error %s(%d)", strerror(errno), errno);
176 static void unix_ipc_error(struct unix_client *client, uint8_t name, int err)
178 char buf[BT_SUGGESTED_BUFFER_SIZE];
179 bt_audio_error_t *rsp = (void *) buf;
181 if (!g_slist_find(clients, client))
184 memset(buf, 0, sizeof(buf));
185 rsp->h.type = BT_ERROR;
187 rsp->h.length = sizeof(*rsp);
189 rsp->posix_errno = err;
191 DBG("sending error %s(%d)", strerror(err), err);
192 unix_ipc_sendmsg(client, &rsp->h);
195 static service_type_t select_service(struct audio_device *dev, const char *interface)
198 if (dev->sink && avdtp_is_connected(&dev->src, &dev->dst))
200 else if (dev->source && avdtp_is_connected(&dev->src,
203 else if (dev->headset && headset_is_active(dev))
207 else if (dev->source)
209 else if (dev->headset)
211 } else if (!strcmp(interface, AUDIO_SOURCE_INTERFACE) && dev->source)
213 else if (!strcmp(interface, AUDIO_SINK_INTERFACE) && dev->sink)
215 else if (!strcmp(interface, AUDIO_HEADSET_INTERFACE) && dev->headset)
217 else if (!strcmp(interface, AUDIO_GATEWAY_INTERFACE) && dev->gateway)
223 static void stream_state_changed(struct avdtp_stream *stream,
224 avdtp_state_t old_state,
225 avdtp_state_t new_state,
226 struct avdtp_error *err,
229 struct unix_client *client = user_data;
230 struct a2dp_data *a2dp = &client->d.a2dp;
233 case AVDTP_STATE_IDLE:
235 a2dp_sep_unlock(a2dp->sep, a2dp->session);
239 avdtp_unref(a2dp->session);
240 a2dp->session = NULL;
250 static uint8_t headset_generate_capability(struct audio_device *dev,
251 codec_capabilities_t *codec)
253 pcm_capabilities_t *pcm;
255 codec->seid = BT_A2DP_SEID_RANGE + 1;
256 codec->transport = BT_CAPABILITIES_TRANSPORT_SCO;
257 codec->type = BT_HFP_CODEC_PCM;
258 codec->length = sizeof(*pcm);
260 pcm = (void *) codec;
261 pcm->sampling_rate = 8000;
263 if (headset_get_nrec(dev))
264 pcm->flags |= BT_PCM_FLAG_NREC;
265 if (!headset_get_sco_hci(dev))
266 pcm->flags |= BT_PCM_FLAG_PCM_ROUTING;
267 codec->configured = headset_is_active(dev);
268 codec->lock = headset_get_lock(dev);
270 pcm->flags |= BT_PCM_FLAG_NREC;
271 codec->configured = TRUE;
275 return codec->length;
278 static void headset_discovery_complete(struct audio_device *dev, void *user_data)
280 struct unix_client *client = user_data;
281 char buf[BT_SUGGESTED_BUFFER_SIZE];
282 struct bt_get_capabilities_rsp *rsp = (void *) buf;
290 memset(buf, 0, sizeof(buf));
292 length = headset_generate_capability(dev, (void *) rsp->data);
294 rsp->h.type = BT_RESPONSE;
295 rsp->h.name = BT_GET_CAPABILITIES;
296 rsp->h.length = sizeof(*rsp) + length;
298 ba2str(&dev->src, rsp->source);
299 ba2str(&dev->dst, rsp->destination);
300 strncpy(rsp->object, dev->path, sizeof(rsp->object));
302 unix_ipc_sendmsg(client, &rsp->h);
307 error("discovery failed");
308 unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
311 static void headset_setup_complete(struct audio_device *dev, void *user_data)
313 struct unix_client *client = user_data;
314 char buf[BT_SUGGESTED_BUFFER_SIZE];
315 struct bt_set_configuration_rsp *rsp = (void *) buf;
322 memset(buf, 0, sizeof(buf));
324 rsp->h.type = BT_RESPONSE;
325 rsp->h.name = BT_SET_CONFIGURATION;
326 rsp->h.length = sizeof(*rsp);
330 client->data_fd = headset_get_sco_fd(dev);
332 unix_ipc_sendmsg(client, &rsp->h);
337 error("config failed");
338 unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
341 static void gateway_setup_complete(struct audio_device *dev, GError *err, void *user_data)
343 struct unix_client *client = user_data;
344 char buf[BT_SUGGESTED_BUFFER_SIZE];
345 struct bt_set_configuration_rsp *rsp = (void *) buf;
348 unix_ipc_error(client, BT_SET_CONFIGURATION, err->code);
354 memset(buf, 0, sizeof(buf));
356 rsp->h.type = BT_RESPONSE;
357 rsp->h.name = BT_SET_CONFIGURATION;
358 rsp->h.length = sizeof(*rsp);
362 client->data_fd = gateway_get_sco_fd(dev);
364 unix_ipc_sendmsg(client, &rsp->h);
367 static void headset_resume_complete(struct audio_device *dev, void *user_data)
369 struct unix_client *client = user_data;
370 char buf[BT_SUGGESTED_BUFFER_SIZE];
371 struct bt_start_stream_rsp *rsp = (void *) buf;
372 struct bt_new_stream_ind *ind = (void *) buf;
379 client->data_fd = headset_get_sco_fd(dev);
380 if (client->data_fd < 0) {
381 error("Unable to get a SCO fd");
385 memset(buf, 0, sizeof(buf));
386 rsp->h.type = BT_RESPONSE;
387 rsp->h.name = BT_START_STREAM;
388 rsp->h.length = sizeof(*rsp);
390 unix_ipc_sendmsg(client, &rsp->h);
392 memset(buf, 0, sizeof(buf));
393 ind->h.type = BT_INDICATION;
394 ind->h.name = BT_NEW_STREAM;
395 ind->h.length = sizeof(*ind);
397 unix_ipc_sendmsg(client, &ind->h);
399 if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
400 error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
407 error("headset_resume_complete: resume failed");
408 unix_ipc_error(client, BT_START_STREAM, EIO);
411 static void gateway_resume_complete(struct audio_device *dev, GError *err, void *user_data)
413 struct unix_client *client = user_data;
414 char buf[BT_SUGGESTED_BUFFER_SIZE];
415 struct bt_start_stream_rsp *rsp = (void *) buf;
416 struct bt_new_stream_ind *ind = (void *) buf;
419 unix_ipc_error(client, BT_START_STREAM, err->code);
423 memset(buf, 0, sizeof(buf));
424 rsp->h.type = BT_RESPONSE;
425 rsp->h.name = BT_START_STREAM;
426 rsp->h.length = sizeof(*rsp);
428 unix_ipc_sendmsg(client, &rsp->h);
430 memset(buf, 0, sizeof(buf));
431 ind->h.type = BT_INDICATION;
432 ind->h.name = BT_NEW_STREAM;
433 ind->h.length = sizeof(*ind);
435 unix_ipc_sendmsg(client, &ind->h);
437 client->data_fd = gateway_get_sco_fd(dev);
438 if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
439 error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
440 unix_ipc_error(client, BT_START_STREAM, EIO);
446 static void headset_suspend_complete(struct audio_device *dev, void *user_data)
448 struct unix_client *client = user_data;
449 char buf[BT_SUGGESTED_BUFFER_SIZE];
450 struct bt_stop_stream_rsp *rsp = (void *) buf;
455 memset(buf, 0, sizeof(buf));
456 rsp->h.type = BT_RESPONSE;
457 rsp->h.name = BT_STOP_STREAM;
458 rsp->h.length = sizeof(*rsp);
460 unix_ipc_sendmsg(client, &rsp->h);
465 error("suspend failed");
466 unix_ipc_error(client, BT_STOP_STREAM, EIO);
469 static void print_mpeg12(struct mpeg_codec_cap *mpeg)
471 DBG("Media Codec: MPEG12"
472 " Channel Modes: %s%s%s%s"
473 " Frequencies: %s%s%s%s%s%s"
476 mpeg->channel_mode & MPEG_CHANNEL_MODE_MONO ? "Mono " : "",
477 mpeg->channel_mode & MPEG_CHANNEL_MODE_DUAL_CHANNEL ?
479 mpeg->channel_mode & MPEG_CHANNEL_MODE_STEREO ? "Stereo " : "",
480 mpeg->channel_mode & MPEG_CHANNEL_MODE_JOINT_STEREO ?
482 mpeg->frequency & MPEG_SAMPLING_FREQ_16000 ? "16Khz " : "",
483 mpeg->frequency & MPEG_SAMPLING_FREQ_22050 ? "22.05Khz " : "",
484 mpeg->frequency & MPEG_SAMPLING_FREQ_24000 ? "24Khz " : "",
485 mpeg->frequency & MPEG_SAMPLING_FREQ_32000 ? "32Khz " : "",
486 mpeg->frequency & MPEG_SAMPLING_FREQ_44100 ? "44.1Khz " : "",
487 mpeg->frequency & MPEG_SAMPLING_FREQ_48000 ? "48Khz " : "",
488 mpeg->layer & MPEG_LAYER_MP1 ? "1 " : "",
489 mpeg->layer & MPEG_LAYER_MP2 ? "2 " : "",
490 mpeg->layer & MPEG_LAYER_MP3 ? "3 " : "",
491 mpeg->crc ? "Yes" : "No");
494 static void print_sbc(struct sbc_codec_cap *sbc)
496 DBG("Media Codec: SBC"
497 " Channel Modes: %s%s%s%s"
498 " Frequencies: %s%s%s%s"
502 sbc->channel_mode & SBC_CHANNEL_MODE_MONO ? "Mono " : "",
503 sbc->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL ?
505 sbc->channel_mode & SBC_CHANNEL_MODE_STEREO ? "Stereo " : "",
506 sbc->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO ? "JointStereo" : "",
507 sbc->frequency & SBC_SAMPLING_FREQ_16000 ? "16Khz " : "",
508 sbc->frequency & SBC_SAMPLING_FREQ_32000 ? "32Khz " : "",
509 sbc->frequency & SBC_SAMPLING_FREQ_44100 ? "44.1Khz " : "",
510 sbc->frequency & SBC_SAMPLING_FREQ_48000 ? "48Khz " : "",
511 sbc->subbands & SBC_SUBBANDS_4 ? "4 " : "",
512 sbc->subbands & SBC_SUBBANDS_8 ? "8 " : "",
513 sbc->block_length & SBC_BLOCK_LENGTH_4 ? "4 " : "",
514 sbc->block_length & SBC_BLOCK_LENGTH_8 ? "8 " : "",
515 sbc->block_length & SBC_BLOCK_LENGTH_12 ? "12 " : "",
516 sbc->block_length & SBC_BLOCK_LENGTH_16 ? "16 " : "",
517 sbc->min_bitpool, sbc->max_bitpool);
520 static int a2dp_append_codec(struct bt_get_capabilities_rsp *rsp,
521 struct avdtp_service_capability *cap,
527 struct avdtp_media_codec_capability *codec_cap = (void *) cap->data;
528 codec_capabilities_t *codec = (void *) rsp + rsp->h.length;
531 if (rsp->h.length > BT_SUGGESTED_BUFFER_SIZE)
534 space_left = BT_SUGGESTED_BUFFER_SIZE - rsp->h.length;
536 /* endianness prevents direct cast */
537 if (codec_cap->media_codec_type == A2DP_CODEC_SBC) {
538 struct sbc_codec_cap *sbc_cap = (void *) codec_cap;
539 sbc_capabilities_t *sbc = (void *) codec;
541 if (space_left < sizeof(sbc_capabilities_t))
544 if (type == AVDTP_SEP_TYPE_SINK)
545 codec->type = BT_A2DP_SBC_SINK;
546 else if (type == AVDTP_SEP_TYPE_SOURCE)
547 codec->type = BT_A2DP_SBC_SOURCE;
551 codec->length = sizeof(sbc_capabilities_t);
553 sbc->channel_mode = sbc_cap->channel_mode;
554 sbc->frequency = sbc_cap->frequency;
555 sbc->allocation_method = sbc_cap->allocation_method;
556 sbc->subbands = sbc_cap->subbands;
557 sbc->block_length = sbc_cap->block_length;
558 sbc->min_bitpool = sbc_cap->min_bitpool;
559 sbc->max_bitpool = sbc_cap->max_bitpool;
562 } else if (codec_cap->media_codec_type == A2DP_CODEC_MPEG12) {
563 struct mpeg_codec_cap *mpeg_cap = (void *) codec_cap;
564 mpeg_capabilities_t *mpeg = (void *) codec;
566 if (space_left < sizeof(mpeg_capabilities_t))
569 if (type == AVDTP_SEP_TYPE_SINK)
570 codec->type = BT_A2DP_MPEG12_SINK;
571 else if (type == AVDTP_SEP_TYPE_SOURCE)
572 codec->type = BT_A2DP_MPEG12_SOURCE;
576 codec->length = sizeof(mpeg_capabilities_t);
578 mpeg->channel_mode = mpeg_cap->channel_mode;
579 mpeg->crc = mpeg_cap->crc;
580 mpeg->layer = mpeg_cap->layer;
581 mpeg->frequency = mpeg_cap->frequency;
582 mpeg->mpf = mpeg_cap->mpf;
583 mpeg->bitrate = mpeg_cap->bitrate;
585 print_mpeg12(mpeg_cap);
587 size_t codec_length, type_length, total_length;
589 codec_length = cap->length - (sizeof(struct avdtp_service_capability)
590 + sizeof(struct avdtp_media_codec_capability));
591 type_length = sizeof(codec_cap->media_codec_type);
592 total_length = type_length + codec_length +
593 sizeof(codec_capabilities_t);
595 if (space_left < total_length)
598 if (type == AVDTP_SEP_TYPE_SINK)
599 codec->type = BT_A2DP_UNKNOWN_SINK;
600 else if (type == AVDTP_SEP_TYPE_SOURCE)
601 codec->type = BT_A2DP_UNKNOWN_SOURCE;
605 codec->length = total_length;
606 memcpy(codec->data, &codec_cap->media_codec_type, type_length);
607 memcpy(codec->data + type_length, codec_cap->data,
612 codec->configured = configured;
614 rsp->h.length += codec->length;
616 DBG("Append %s seid %d - length %d - total %d",
617 configured ? "configured" : "", seid, codec->length,
623 static void a2dp_discovery_complete(struct avdtp *session, GSList *seps,
624 struct avdtp_error *err,
627 struct unix_client *client = user_data;
628 char buf[BT_SUGGESTED_BUFFER_SIZE];
629 struct bt_get_capabilities_rsp *rsp = (void *) buf;
630 struct a2dp_data *a2dp = &client->d.a2dp;
632 if (!g_slist_find(clients, client)) {
633 DBG("Client disconnected during discovery");
640 memset(buf, 0, sizeof(buf));
643 rsp->h.type = BT_RESPONSE;
644 rsp->h.name = BT_GET_CAPABILITIES;
645 rsp->h.length = sizeof(*rsp);
646 ba2str(&client->dev->src, rsp->source);
647 ba2str(&client->dev->dst, rsp->destination);
648 strncpy(rsp->object, client->dev->path, sizeof(rsp->object));
650 for (; seps; seps = g_slist_next(seps)) {
651 struct avdtp_remote_sep *rsep = seps->data;
652 struct a2dp_sep *sep;
653 struct avdtp_service_capability *cap;
654 struct avdtp_stream *stream;
655 uint8_t type, seid, configured = 0, lock = 0;
658 type = avdtp_get_type(rsep);
660 if (type != AVDTP_SEP_TYPE_SINK &&
661 type != AVDTP_SEP_TYPE_SOURCE)
664 cap = avdtp_get_codec(rsep);
666 if (cap->category != AVDTP_MEDIA_CODEC)
669 seid = avdtp_get_seid(rsep);
671 if (client->seid != 0 && client->seid != seid)
674 stream = avdtp_get_stream(rsep);
677 if (client->seid == seid)
678 cap = avdtp_stream_get_codec(stream);
681 for (cl = clients; cl; cl = cl->next) {
682 struct unix_client *c = cl->data;
683 struct a2dp_data *ca2dp = &c->d.a2dp;
685 if (ca2dp->session == session && c->seid == seid) {
691 sep = a2dp_get_sep(session, stream);
692 if (sep && a2dp_sep_get_lock(sep))
693 lock = BT_WRITE_LOCK;
695 a2dp_append_codec(rsp, cap, seid, type, configured, lock);
698 unix_ipc_sendmsg(client, &rsp->h);
703 error("discovery failed");
704 unix_ipc_error(client, BT_GET_CAPABILITIES, EIO);
707 a2dp_sep_unlock(a2dp->sep, a2dp->session);
711 avdtp_unref(a2dp->session);
712 a2dp->session = NULL;
716 static void a2dp_config_complete(struct avdtp *session, struct a2dp_sep *sep,
717 struct avdtp_stream *stream,
718 struct avdtp_error *err,
721 struct unix_client *client = user_data;
722 char buf[BT_SUGGESTED_BUFFER_SIZE];
723 struct bt_set_configuration_rsp *rsp = (void *) buf;
724 struct a2dp_data *a2dp = &client->d.a2dp;
733 memset(buf, 0, sizeof(buf));
738 if (client->cb_id > 0)
739 avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
743 a2dp->stream = stream;
745 if (!avdtp_stream_get_transport(stream, &client->data_fd, &imtu, &omtu,
747 error("Unable to get stream transport");
751 rsp->h.type = BT_RESPONSE;
752 rsp->h.name = BT_SET_CONFIGURATION;
753 rsp->h.length = sizeof(*rsp);
755 /* FIXME: Use imtu when fd_opt is CFG_FD_OPT_READ */
756 rsp->link_mtu = omtu;
758 unix_ipc_sendmsg(client, &rsp->h);
760 client->cb_id = avdtp_stream_add_cb(session, stream,
761 stream_state_changed, client);
766 error("config failed");
768 unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
770 avdtp_unref(a2dp->session);
772 a2dp->session = NULL;
777 static void a2dp_resume_complete(struct avdtp *session,
778 struct avdtp_error *err, void *user_data)
780 struct unix_client *client = user_data;
781 char buf[BT_SUGGESTED_BUFFER_SIZE];
782 struct bt_start_stream_rsp *rsp = (void *) buf;
783 struct bt_new_stream_ind *ind = (void *) buf;
784 struct a2dp_data *a2dp = &client->d.a2dp;
789 memset(buf, 0, sizeof(buf));
790 rsp->h.type = BT_RESPONSE;
791 rsp->h.name = BT_START_STREAM;
792 rsp->h.length = sizeof(*rsp);
794 unix_ipc_sendmsg(client, &rsp->h);
796 memset(buf, 0, sizeof(buf));
797 ind->h.type = BT_RESPONSE;
798 ind->h.name = BT_NEW_STREAM;
799 rsp->h.length = sizeof(*ind);
801 unix_ipc_sendmsg(client, &ind->h);
803 if (unix_sendmsg_fd(client->sock, client->data_fd) < 0) {
804 error("unix_sendmsg_fd: %s(%d)", strerror(errno), errno);
811 error("resume failed");
813 unix_ipc_error(client, BT_START_STREAM, EIO);
815 if (client->cb_id > 0) {
816 avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
822 a2dp_sep_unlock(a2dp->sep, a2dp->session);
826 avdtp_unref(a2dp->session);
827 a2dp->session = NULL;
831 static void a2dp_suspend_complete(struct avdtp *session,
832 struct avdtp_error *err, void *user_data)
834 struct unix_client *client = user_data;
835 char buf[BT_SUGGESTED_BUFFER_SIZE];
836 struct bt_stop_stream_rsp *rsp = (void *) buf;
841 memset(buf, 0, sizeof(buf));
842 rsp->h.type = BT_RESPONSE;
843 rsp->h.name = BT_STOP_STREAM;
844 rsp->h.length = sizeof(*rsp);
846 unix_ipc_sendmsg(client, &rsp->h);
851 error("suspend failed");
853 unix_ipc_error(client, BT_STOP_STREAM, EIO);
856 static void start_discovery(struct audio_device *dev, struct unix_client *client)
858 struct a2dp_data *a2dp;
861 switch (client->type) {
864 a2dp = &client->d.a2dp;
867 a2dp->session = avdtp_get(&dev->src, &dev->dst);
869 if (!a2dp->session) {
870 error("Unable to get a session");
874 err = avdtp_discover(a2dp->session, a2dp_discovery_complete,
878 avdtp_unref(a2dp->session);
879 a2dp->session = NULL;
887 headset_discovery_complete(dev, client);
891 error("No known services for device");
900 unix_ipc_error(client, BT_GET_CAPABILITIES, err ? : EIO);
903 static void open_complete(struct audio_device *dev, void *user_data)
905 struct unix_client *client = user_data;
906 char buf[BT_SUGGESTED_BUFFER_SIZE];
907 struct bt_open_rsp *rsp = (void *) buf;
909 memset(buf, 0, sizeof(buf));
911 rsp->h.type = BT_RESPONSE;
912 rsp->h.name = BT_OPEN;
913 rsp->h.length = sizeof(*rsp);
915 ba2str(&dev->src, rsp->source);
916 ba2str(&dev->dst, rsp->destination);
917 strncpy(rsp->object, dev->path, sizeof(rsp->object));
919 unix_ipc_sendmsg(client, &rsp->h);
922 static void start_open(struct audio_device *dev, struct unix_client *client)
924 struct a2dp_data *a2dp;
925 struct headset_data *hs;
926 struct avdtp_remote_sep *rsep;
927 gboolean unref_avdtp_on_fail = FALSE;
929 switch (client->type) {
932 a2dp = &client->d.a2dp;
934 if (!a2dp->session) {
935 a2dp->session = avdtp_get(&dev->src, &dev->dst);
936 unref_avdtp_on_fail = TRUE;
939 if (!a2dp->session) {
940 error("Unable to get a session");
945 error("Client already has an opened session");
949 rsep = avdtp_get_remote_sep(a2dp->session, client->seid);
951 error("Invalid seid %d", client->seid);
955 a2dp->sep = a2dp_get(a2dp->session, rsep);
957 error("seid %d not available or locked", client->seid);
961 if (!a2dp_sep_lock(a2dp->sep, a2dp->session)) {
962 error("Unable to open seid %d", client->seid);
973 error("Client already has an opened session");
977 hs->locked = headset_lock(dev, client->lock);
979 error("Unable to open seid %d", client->seid);
987 error("No known services for device");
993 open_complete(dev, client);
998 if (unref_avdtp_on_fail && a2dp->session) {
999 avdtp_unref(a2dp->session);
1000 a2dp->session = NULL;
1002 unix_ipc_error(client, BT_OPEN, EINVAL);
1005 static void start_config(struct audio_device *dev, struct unix_client *client)
1007 struct a2dp_data *a2dp;
1008 struct headset_data *hs;
1011 switch (client->type) {
1014 a2dp = &client->d.a2dp;
1017 a2dp->session = avdtp_get(&dev->src, &dev->dst);
1019 if (!a2dp->session) {
1020 error("Unable to get a session");
1025 error("seid %d not opened", client->seid);
1029 id = a2dp_config(a2dp->session, a2dp->sep, a2dp_config_complete,
1030 client->caps, client);
1031 client->cancel = a2dp_cancel;
1038 error("seid %d not opened", client->seid);
1042 id = headset_config_stream(dev, TRUE, headset_setup_complete,
1044 client->cancel = headset_cancel_stream;
1047 id = gateway_config_stream(dev, gateway_setup_complete, client);
1048 client->cancel = gateway_cancel_stream;
1052 error("No known services for device");
1057 error("config failed");
1061 client->req_id = id;
1062 g_slist_free(client->caps);
1063 client->caps = NULL;
1068 unix_ipc_error(client, BT_SET_CONFIGURATION, EIO);
1071 static void start_resume(struct audio_device *dev, struct unix_client *client)
1073 struct a2dp_data *a2dp = NULL;
1074 struct headset_data *hs;
1076 struct avdtp *session = NULL;
1078 switch (client->type) {
1081 a2dp = &client->d.a2dp;
1084 error("seid not opened");
1088 if (!a2dp->session) {
1089 session = avdtp_get(&dev->src, &dev->dst);
1091 error("Unable to get a session");
1094 a2dp->session = session;
1097 id = a2dp_resume(a2dp->session, a2dp->sep, a2dp_resume_complete,
1099 client->cancel = a2dp_cancel;
1107 error("seid not opened");
1111 id = headset_request_stream(dev, headset_resume_complete,
1113 client->cancel = headset_cancel_stream;
1117 id = gateway_request_stream(dev, gateway_resume_complete,
1119 client->cancel = gateway_cancel_stream;
1123 error("No known services for device");
1128 error("start_resume: resume failed");
1132 client->req_id = id;
1138 avdtp_unref(session);
1139 a2dp->session = NULL;
1142 unix_ipc_error(client, BT_START_STREAM, EIO);
1145 static void start_suspend(struct audio_device *dev, struct unix_client *client)
1147 struct a2dp_data *a2dp = NULL;
1148 struct headset_data *hs;
1150 struct avdtp *session = NULL;
1152 switch (client->type) {
1155 a2dp = &client->d.a2dp;
1158 error("seid not opened");
1162 if (!a2dp->session) {
1163 session = avdtp_get(&dev->src, &dev->dst);
1165 error("Unable to get a session");
1168 a2dp->session = session;
1172 error("Unable to get a sep");
1176 id = a2dp_suspend(a2dp->session, a2dp->sep,
1177 a2dp_suspend_complete, client);
1178 client->cancel = a2dp_cancel;
1185 error("seid not opened");
1189 id = headset_suspend_stream(dev, headset_suspend_complete,
1191 client->cancel = headset_cancel_stream;
1195 gateway_suspend_stream(dev);
1196 client->cancel = gateway_cancel_stream;
1197 headset_suspend_complete(dev, client);
1202 error("No known services for device");
1207 error("suspend failed");
1215 avdtp_unref(session);
1216 a2dp->session = NULL;
1219 unix_ipc_error(client, BT_STOP_STREAM, EIO);
1222 static void close_complete(struct audio_device *dev, void *user_data)
1224 struct unix_client *client = user_data;
1225 char buf[BT_SUGGESTED_BUFFER_SIZE];
1226 struct bt_close_rsp *rsp = (void *) buf;
1228 memset(buf, 0, sizeof(buf));
1230 rsp->h.type = BT_RESPONSE;
1231 rsp->h.name = BT_CLOSE;
1232 rsp->h.length = sizeof(*rsp);
1234 unix_ipc_sendmsg(client, &rsp->h);
1239 static void start_close(struct audio_device *dev, struct unix_client *client,
1242 struct a2dp_data *a2dp;
1243 struct headset_data *hs;
1248 switch (client->type) {
1252 if (client->dev && hs->locked) {
1253 headset_unlock(client->dev, client->lock);
1261 a2dp = &client->d.a2dp;
1263 if (client->cb_id > 0) {
1264 avdtp_stream_remove_cb(a2dp->session, a2dp->stream,
1269 a2dp_sep_unlock(a2dp->sep, a2dp->session);
1272 if (a2dp->session) {
1273 avdtp_unref(a2dp->session);
1274 a2dp->session = NULL;
1276 a2dp->stream = NULL;
1279 error("No known services for device");
1286 close_complete(dev, client);
1293 unix_ipc_error(client, BT_STOP_STREAM, EINVAL);
1296 static void handle_getcapabilities_req(struct unix_client *client,
1297 struct bt_get_capabilities_req *req)
1299 struct audio_device *dev;
1302 const char *interface;
1304 if (!check_nul(req->source) || !check_nul(req->destination) ||
1305 !check_nul(req->object)) {
1310 str2ba(req->source, &src);
1311 str2ba(req->destination, &dst);
1313 if (!manager_find_device(req->object, &src, &dst, NULL, FALSE))
1316 if (req->transport == BT_CAPABILITIES_TRANSPORT_SCO)
1317 interface = AUDIO_HEADSET_INTERFACE;
1318 else if (req->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
1319 interface = AUDIO_SINK_INTERFACE;
1321 interface = client->interface;
1323 dev = manager_find_device(req->object, &src, &dst, interface, TRUE);
1324 if (!dev && (req->flags & BT_FLAG_AUTOCONNECT))
1325 dev = manager_find_device(req->object, &src, &dst,
1329 if (req->transport == BT_CAPABILITIES_TRANSPORT_SCO)
1330 interface = AUDIO_GATEWAY_INTERFACE;
1331 else if (req->transport == BT_CAPABILITIES_TRANSPORT_A2DP)
1332 interface = AUDIO_SOURCE_INTERFACE;
1335 dev = manager_find_device(req->object, &src, &dst,
1337 if (!dev && (req->flags & BT_FLAG_AUTOCONNECT))
1338 dev = manager_find_device(req->object, &src, &dst,
1343 error("Unable to find a matching device");
1347 client->type = select_service(dev, interface);
1348 if (client->type == TYPE_NONE) {
1349 error("No matching service found");
1353 if (g_strcmp0(interface, client->interface) != 0) {
1354 g_free(client->interface);
1355 client->interface = g_strdup(interface);
1358 client->seid = req->seid;
1360 start_discovery(dev, client);
1365 unix_ipc_error(client, BT_GET_CAPABILITIES, err);
1368 static int handle_sco_open(struct unix_client *client, struct bt_open_req *req)
1370 if (!client->interface)
1371 client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
1372 else if (!g_str_equal(client->interface, AUDIO_HEADSET_INTERFACE) &&
1373 !g_str_equal(client->interface, AUDIO_GATEWAY_INTERFACE))
1376 DBG("open sco - object=%s source=%s destination=%s lock=%s%s",
1377 strcmp(req->object, "") ? req->object : "ANY",
1378 strcmp(req->source, "") ? req->source : "ANY",
1379 strcmp(req->destination, "") ? req->destination : "ANY",
1380 req->lock & BT_READ_LOCK ? "read" : "",
1381 req->lock & BT_WRITE_LOCK ? "write" : "");
1386 static int handle_a2dp_open(struct unix_client *client, struct bt_open_req *req)
1388 if (!client->interface)
1389 /* FIXME: are we treating a sink or a source? */
1390 client->interface = g_strdup(AUDIO_SINK_INTERFACE);
1391 else if (!g_str_equal(client->interface, AUDIO_SINK_INTERFACE) &&
1392 !g_str_equal(client->interface, AUDIO_SOURCE_INTERFACE))
1395 DBG("open a2dp - object=%s source=%s destination=%s lock=%s%s",
1396 strcmp(req->object, "") ? req->object : "ANY",
1397 strcmp(req->source, "") ? req->source : "ANY",
1398 strcmp(req->destination, "") ? req->destination : "ANY",
1399 req->lock & BT_READ_LOCK ? "read" : "",
1400 req->lock & BT_WRITE_LOCK ? "write" : "");
1405 static void handle_open_req(struct unix_client *client, struct bt_open_req *req)
1407 struct audio_device *dev;
1411 if (!check_nul(req->source) || !check_nul(req->destination) ||
1412 !check_nul(req->object)) {
1417 str2ba(req->source, &src);
1418 str2ba(req->destination, &dst);
1420 if (req->seid > BT_A2DP_SEID_RANGE) {
1421 err = handle_sco_open(client, req);
1427 err = handle_a2dp_open(client, req);
1434 if (!manager_find_device(req->object, &src, &dst, NULL, FALSE))
1437 dev = manager_find_device(req->object, &src, &dst, client->interface,
1440 dev = manager_find_device(req->object, &src, &dst,
1441 client->interface, FALSE);
1446 client->seid = req->seid;
1447 client->lock = req->lock;
1449 start_open(dev, client);
1454 unix_ipc_error(client, BT_OPEN, err ? : EIO);
1457 static int handle_sco_transport(struct unix_client *client,
1458 struct bt_set_configuration_req *req)
1460 struct audio_device *dev = client->dev;
1462 if (!client->interface) {
1464 client->interface = g_strdup(AUDIO_HEADSET_INTERFACE);
1465 else if (dev->gateway)
1466 client->interface = g_strdup(AUDIO_GATEWAY_INTERFACE);
1469 } else if (!g_str_equal(client->interface, AUDIO_HEADSET_INTERFACE) &&
1470 !g_str_equal(client->interface, AUDIO_GATEWAY_INTERFACE))
1476 static int handle_a2dp_transport(struct unix_client *client,
1477 struct bt_set_configuration_req *req)
1479 struct avdtp_service_capability *media_transport, *media_codec;
1480 struct sbc_codec_cap sbc_cap;
1481 struct mpeg_codec_cap mpeg_cap;
1483 if (!client->interface)
1484 /* FIXME: are we treating a sink or a source? */
1485 client->interface = g_strdup(AUDIO_SINK_INTERFACE);
1486 else if (!g_str_equal(client->interface, AUDIO_SINK_INTERFACE) &&
1487 !g_str_equal(client->interface, AUDIO_SOURCE_INTERFACE))
1490 g_slist_free_full(client->caps, g_free);
1491 client->caps = NULL;
1493 media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
1496 client->caps = g_slist_append(client->caps, media_transport);
1498 if (req->codec.type == BT_A2DP_MPEG12_SINK ||
1499 req->codec.type == BT_A2DP_MPEG12_SOURCE) {
1500 mpeg_capabilities_t *mpeg = (void *) &req->codec;
1502 memset(&mpeg_cap, 0, sizeof(mpeg_cap));
1504 mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
1505 mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12;
1506 mpeg_cap.channel_mode = mpeg->channel_mode;
1507 mpeg_cap.crc = mpeg->crc;
1508 mpeg_cap.layer = mpeg->layer;
1509 mpeg_cap.frequency = mpeg->frequency;
1510 mpeg_cap.mpf = mpeg->mpf;
1511 mpeg_cap.bitrate = mpeg->bitrate;
1513 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap,
1516 print_mpeg12(&mpeg_cap);
1517 } else if (req->codec.type == BT_A2DP_SBC_SINK ||
1518 req->codec.type == BT_A2DP_SBC_SOURCE) {
1519 sbc_capabilities_t *sbc = (void *) &req->codec;
1521 memset(&sbc_cap, 0, sizeof(sbc_cap));
1523 sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
1524 sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC;
1525 sbc_cap.channel_mode = sbc->channel_mode;
1526 sbc_cap.frequency = sbc->frequency;
1527 sbc_cap.allocation_method = sbc->allocation_method;
1528 sbc_cap.subbands = sbc->subbands;
1529 sbc_cap.block_length = sbc->block_length;
1530 sbc_cap.min_bitpool = sbc->min_bitpool;
1531 sbc_cap.max_bitpool = sbc->max_bitpool;
1533 media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
1536 print_sbc(&sbc_cap);
1540 client->caps = g_slist_append(client->caps, media_codec);
1545 static void handle_setconfiguration_req(struct unix_client *client,
1546 struct bt_set_configuration_req *req)
1550 if (req->codec.seid != client->seid) {
1551 error("Unable to set configuration: seid %d not opened",
1559 if (req->codec.transport == BT_CAPABILITIES_TRANSPORT_SCO) {
1560 err = handle_sco_transport(client, req);
1565 } else if (req->codec.transport == BT_CAPABILITIES_TRANSPORT_A2DP) {
1566 err = handle_a2dp_transport(client, req);
1573 start_config(client->dev, client);
1578 unix_ipc_error(client, BT_SET_CONFIGURATION, err ? : EIO);
1581 static void handle_streamstart_req(struct unix_client *client,
1582 struct bt_start_stream_req *req)
1587 start_resume(client->dev, client);
1592 unix_ipc_error(client, BT_START_STREAM, EIO);
1595 static void handle_streamstop_req(struct unix_client *client,
1596 struct bt_stop_stream_req *req)
1601 start_suspend(client->dev, client);
1606 unix_ipc_error(client, BT_STOP_STREAM, EIO);
1609 static void handle_close_req(struct unix_client *client,
1610 struct bt_close_req *req)
1615 start_close(client->dev, client, TRUE);
1620 unix_ipc_error(client, BT_CLOSE, EIO);
1623 static void handle_control_req(struct unix_client *client,
1624 struct bt_control_req *req)
1626 /* FIXME: really implement that */
1627 char buf[BT_SUGGESTED_BUFFER_SIZE];
1628 struct bt_set_configuration_rsp *rsp = (void *) buf;
1630 memset(buf, 0, sizeof(buf));
1631 rsp->h.type = BT_RESPONSE;
1632 rsp->h.name = BT_CONTROL;
1633 rsp->h.length = sizeof(*rsp);
1635 unix_ipc_sendmsg(client, &rsp->h);
1638 static void handle_delay_report_req(struct unix_client *client,
1639 struct bt_delay_report_req *req)
1641 char buf[BT_SUGGESTED_BUFFER_SIZE];
1642 struct bt_set_configuration_rsp *rsp = (void *) buf;
1643 struct a2dp_data *a2dp;
1651 switch (client->type) {
1658 a2dp = &client->d.a2dp;
1659 if (a2dp->session && a2dp->stream) {
1660 err = avdtp_delay_report(a2dp->session, a2dp->stream,
1670 error("No known services for device");
1675 memset(buf, 0, sizeof(buf));
1676 rsp->h.type = BT_RESPONSE;
1677 rsp->h.name = BT_DELAY_REPORT;
1678 rsp->h.length = sizeof(*rsp);
1680 unix_ipc_sendmsg(client, &rsp->h);
1685 unix_ipc_error(client, BT_DELAY_REPORT, -err);
1688 static gboolean client_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
1690 char buf[BT_SUGGESTED_BUFFER_SIZE];
1691 bt_audio_msg_header_t *msghdr = (void *) buf;
1692 struct unix_client *client = data;
1694 const char *type, *name;
1696 if (cond & G_IO_NVAL)
1699 if (cond & (G_IO_HUP | G_IO_ERR)) {
1700 DBG("Unix client disconnected (fd=%d)", client->sock);
1705 memset(buf, 0, sizeof(buf));
1707 len = recv(client->sock, buf, sizeof(buf), 0);
1709 error("recv: %s (%d)", strerror(errno), errno);
1713 type = bt_audio_strtype(msghdr->type);
1714 name = bt_audio_strname(msghdr->name);
1716 DBG("Audio API: %s <- %s", type, name);
1718 if (msghdr->length != len) {
1719 error("Invalid message: length mismatch");
1723 switch (msghdr->name) {
1724 case BT_GET_CAPABILITIES:
1725 handle_getcapabilities_req(client,
1726 (struct bt_get_capabilities_req *) msghdr);
1729 handle_open_req(client,
1730 (struct bt_open_req *) msghdr);
1732 case BT_SET_CONFIGURATION:
1733 handle_setconfiguration_req(client,
1734 (struct bt_set_configuration_req *) msghdr);
1736 case BT_START_STREAM:
1737 handle_streamstart_req(client,
1738 (struct bt_start_stream_req *) msghdr);
1740 case BT_STOP_STREAM:
1741 handle_streamstop_req(client,
1742 (struct bt_stop_stream_req *) msghdr);
1745 handle_close_req(client,
1746 (struct bt_close_req *) msghdr);
1749 handle_control_req(client,
1750 (struct bt_control_req *) msghdr);
1752 case BT_DELAY_REPORT:
1753 handle_delay_report_req(client,
1754 (struct bt_delay_report_req *) msghdr);
1757 error("Audio API: received unexpected message name %d",
1764 clients = g_slist_remove(clients, client);
1765 start_close(client->dev, client, FALSE);
1766 client_free(client);
1770 static gboolean server_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
1772 struct sockaddr_un addr;
1775 struct unix_client *client;
1778 if (cond & G_IO_NVAL)
1781 if (cond & (G_IO_HUP | G_IO_ERR)) {
1782 g_io_channel_shutdown(chan, TRUE, NULL);
1786 sk = g_io_channel_unix_get_fd(chan);
1788 memset(&addr, 0, sizeof(addr));
1789 addrlen = sizeof(addr);
1791 cli_sk = accept(sk, (struct sockaddr *) &addr, &addrlen);
1793 error("accept: %s (%d)", strerror(errno), errno);
1797 DBG("Accepted new client connection on unix socket (fd=%d)", cli_sk);
1798 set_nonblocking(cli_sk);
1800 client = g_new0(struct unix_client, 1);
1801 client->sock = cli_sk;
1802 clients = g_slist_append(clients, client);
1804 io = g_io_channel_unix_new(cli_sk);
1805 g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1807 g_io_channel_unref(io);
1812 void unix_device_removed(struct audio_device *dev)
1816 DBG("unix_device_removed(%p)", dev);
1820 struct unix_client *client = l->data;
1824 if (client->dev == dev) {
1825 clients = g_slist_remove(clients, client);
1826 start_close(client->dev, client, FALSE);
1827 client_free(client);
1832 void unix_delay_report(struct audio_device *dev, uint8_t seid, uint16_t delay)
1835 struct bt_delay_report_ind ind;
1837 DBG("unix_delay_report(%p): %u.%ums", dev, delay / 10, delay % 10);
1839 memset(&ind, 0, sizeof(ind));
1840 ind.h.type = BT_INDICATION;
1841 ind.h.name = BT_DELAY_REPORT;
1842 ind.h.length = sizeof(ind);
1845 for (l = clients; l != NULL; l = g_slist_next(l)) {
1846 struct unix_client *client = l->data;
1848 if (client->dev != dev || client->seid != seid)
1851 unix_ipc_sendmsg(client, (void *) &ind);
1858 struct sockaddr_un addr = {
1859 AF_UNIX, BT_IPC_SOCKET_NAME
1864 sk = socket(PF_LOCAL, SOCK_STREAM, 0);
1867 error("Can't create unix socket: %s (%d)", strerror(-err),
1872 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1874 error("Can't bind unix socket: %s (%d)", strerror(-err),
1880 set_nonblocking(sk);
1882 if (listen(sk, 1) < 0) {
1884 error("Can't listen on unix socket: %s (%d)", strerror(-err),
1892 io = g_io_channel_unix_new(sk);
1893 g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1895 g_io_channel_unref(io);
1897 DBG("Unix socket created: %d", sk);
1902 void unix_exit(void)
1904 g_slist_free_full(clients, client_free);
1905 if (unix_sock >= 0) {