Tizen 2.0 Release
[framework/connectivity/bluez.git] / audio / ipc.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include "ipc.h"
25
26 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
27
28 /* This table contains the string representation for messages types */
29 static const char *strtypes[] = {
30         "BT_REQUEST",
31         "BT_RESPONSE",
32         "BT_INDICATION",
33         "BT_ERROR",
34 };
35
36 /* This table contains the string representation for messages names */
37 static const char *strnames[] = {
38         "BT_GET_CAPABILITIES",
39         "BT_OPEN",
40         "BT_SET_CONFIGURATION",
41         "BT_NEW_STREAM",
42         "BT_START_STREAM",
43         "BT_STOP_STREAM",
44         "BT_SUSPEND_STREAM",
45         "BT_RESUME_STREAM",
46         "BT_CONTROL",
47 };
48
49 int bt_audio_service_open(void)
50 {
51         int sk;
52         int err;
53         struct sockaddr_un addr = {
54                 AF_UNIX, BT_IPC_SOCKET_NAME
55         };
56
57         sk = socket(PF_LOCAL, SOCK_STREAM, 0);
58         if (sk < 0) {
59                 err = -errno;
60                 fprintf(stderr, "%s: Cannot open socket: %s (%d)\n",
61                         __FUNCTION__, strerror(-err), -err);
62                 errno = -err;
63                 return -1;
64         }
65
66         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
67                 err = -errno;
68                 fprintf(stderr, "%s: connect() failed: %s (%d)\n",
69                         __FUNCTION__, strerror(-err), -err);
70                 close(sk);
71                 errno = -err;
72                 return -1;
73         }
74
75         return sk;
76 }
77
78 int bt_audio_service_close(int sk)
79 {
80         return close(sk);
81 }
82
83 int bt_audio_service_get_data_fd(int sk)
84 {
85         char cmsg_b[CMSG_SPACE(sizeof(int))], m;
86         int err, ret;
87         struct iovec iov = { &m, sizeof(m) };
88         struct msghdr msgh;
89         struct cmsghdr *cmsg;
90
91         memset(&msgh, 0, sizeof(msgh));
92         msgh.msg_iov = &iov;
93         msgh.msg_iovlen = 1;
94         msgh.msg_control = &cmsg_b;
95         msgh.msg_controllen = CMSG_LEN(sizeof(int));
96
97         ret = recvmsg(sk, &msgh, 0);
98         if (ret < 0) {
99                 err = -errno;
100                 fprintf(stderr, "%s: Unable to receive fd: %s (%d)\n",
101                         __FUNCTION__, strerror(-err), -err);
102                 errno = -err;
103                 return -1;
104         }
105
106         /* Receive auxiliary data in msgh */
107         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
108                         cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
109                 if (cmsg->cmsg_level == SOL_SOCKET
110                                 && cmsg->cmsg_type == SCM_RIGHTS) {
111                         memcpy(&ret, CMSG_DATA(cmsg), sizeof(int));
112                         return ret;
113                 }
114         }
115
116         errno = EINVAL;
117         return -1;
118 }
119
120 const char *bt_audio_strtype(uint8_t type)
121 {
122         if (type >= ARRAY_SIZE(strtypes))
123                 return NULL;
124
125         return strtypes[type];
126 }
127
128 const char *bt_audio_strname(uint8_t name)
129 {
130         if (name >= ARRAY_SIZE(strnames))
131                 return NULL;
132
133         return strnames[name];
134 }