]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/ipc.c
Merge commit 'coling/master'
[pulseaudio] / src / modules / bluetooth / ipc.c
1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22
23 #include "ipc.h"
24
25 /* This table contains the string representation for messages */
26 static const char *strmsg[] = {
27 "BT_GETCAPABILITIES_REQ",
28 "BT_GETCAPABILITIES_RSP",
29 "BT_SETCONFIGURATION_REQ",
30 "BT_SETCONFIGURATION_RSP",
31 "BT_STREAMSTART_REQ",
32 "BT_STREAMSTART_RSP",
33 "BT_STREAMSTOP_REQ",
34 "BT_STREAMSTOP_RSP",
35 "BT_STREAMSUSPEND_IND",
36 "BT_STREAMRESUME_IND",
37 "BT_CONTROL_REQ",
38 "BT_CONTROL_RSP",
39 "BT_CONTROL_IND",
40 "BT_STREAMFD_IND",
41 };
42
43 int bt_audio_service_open(void)
44 {
45 int sk;
46 int err;
47 struct sockaddr_un addr = {
48 AF_UNIX, BT_IPC_SOCKET_NAME
49 };
50
51 sk = socket(PF_LOCAL, SOCK_STREAM, 0);
52 if (sk < 0) {
53 err = errno;
54 fprintf(stderr, "%s: Cannot open socket: %s (%d)\n",
55 __FUNCTION__, strerror(err), err);
56 errno = err;
57 return -1;
58 }
59
60 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
61 err = errno;
62 fprintf(stderr, "%s: connect() failed: %s (%d)\n",
63 __FUNCTION__, strerror(err), err);
64 close(sk);
65 errno = err;
66 return -1;
67 }
68
69 return sk;
70 }
71
72 int bt_audio_service_close(int sk)
73 {
74 return close(sk);
75 }
76
77 int bt_audio_service_get_data_fd(int sk)
78 {
79 char cmsg_b[CMSG_SPACE(sizeof(int))], m;
80 int err, ret;
81 struct iovec iov = { &m, sizeof(m) };
82 struct msghdr msgh;
83 struct cmsghdr *cmsg;
84
85 memset(&msgh, 0, sizeof(msgh));
86 msgh.msg_iov = &iov;
87 msgh.msg_iovlen = 1;
88 msgh.msg_control = &cmsg_b;
89 msgh.msg_controllen = CMSG_LEN(sizeof(int));
90
91 ret = (int) recvmsg(sk, &msgh, 0);
92 if (ret < 0) {
93 err = errno;
94 fprintf(stderr, "%s: Unable to receive fd: %s (%d)\n",
95 __FUNCTION__, strerror(err), err);
96 errno = err;
97 return -1;
98 }
99
100 /* Receive auxiliary data in msgh */
101 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
102 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
103 if (cmsg->cmsg_level == SOL_SOCKET
104 && cmsg->cmsg_type == SCM_RIGHTS)
105 return (*(int *) CMSG_DATA(cmsg));
106 }
107
108 errno = EINVAL;
109 return -1;
110 }
111
112 const char *bt_audio_strmsg(int type)
113 {
114 if (type < 0 || (size_t) type > (sizeof(strmsg) / sizeof(strmsg[0])))
115 return NULL;
116
117 return strmsg[type];
118 }