1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
20 /************************************************************************************
24 * Description: Handsfree Profile Bluetooth Interface
27 ***********************************************************************************/
28 #include <hardware/bluetooth.h>
29 #include <hardware/bt_sock.h>
31 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
42 #include <sys/ioctl.h>
44 #include <cutils/sockets.h>
45 #include <netinet/tcp.h>
48 #define LOG_TAG "bt_btif_sock"
49 #include "btif_common.h"
50 #include "btif_util.h"
53 #include "btif_sock_thread.h"
54 #include "btif_sock_sdp.h"
56 #include "bt_target.h"
63 #include "bta_jv_api.h"
64 #include "bta_jv_co.h"
66 #include "osi/include/log.h"
68 #define asrt(s) if(!(s)) BTIF_TRACE_ERROR("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
71 int sock_send_all(int sock_fd, const uint8_t* buf, int len)
77 do ret = send(sock_fd, buf, s, 0);
78 while(ret < 0 && errno == EINTR);
81 BTIF_TRACE_ERROR("sock fd:%d send errno:%d, ret:%d", sock_fd, errno, ret);
89 int sock_recv_all(int sock_fd, uint8_t* buf, int len)
95 do ret = recv(sock_fd, buf, r, MSG_WAITALL);
96 while(ret < 0 && errno == EINTR);
99 BTIF_TRACE_ERROR("sock fd:%d recv errno:%d, ret:%d", sock_fd, errno, ret);
108 int sock_send_fd(int sock_fd, const uint8_t* buf, int len, int send_fd)
112 unsigned char *buffer = (unsigned char *)buf;
113 memset(&msg, 0, sizeof(msg));
115 struct cmsghdr *cmsg;
116 char msgbuf[CMSG_SPACE(1)];
118 if(sock_fd == -1 || send_fd == -1)
120 // Add any pending outbound file descriptors to the message
121 // See "man cmsg" really
122 msg.msg_control = msgbuf;
123 msg.msg_controllen = sizeof msgbuf;
124 cmsg = CMSG_FIRSTHDR(&msg);
125 cmsg->cmsg_level = SOL_SOCKET;
126 cmsg->cmsg_type = SCM_RIGHTS;
127 cmsg->cmsg_len = CMSG_LEN(sizeof send_fd);
128 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd);
130 // We only write our msg_control during the first write
134 memset(&iv, 0, sizeof(iv));
136 iv.iov_base = buffer;
143 ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
144 } while (ret < 0 && errno == EINTR);
147 BTIF_TRACE_ERROR("fd:%d, send_fd:%d, sendmsg ret:%d, errno:%d, %s",
148 sock_fd, send_fd, (int)ret, errno, strerror(errno));
156 // Wipes out any msg_control too
157 memset(&msg, 0, sizeof(msg));
159 BTIF_TRACE_DEBUG("close fd:%d after sent", send_fd);
160 // TODO: This seems wrong - if the FD is not opened in JAVA before this is called
161 // we get a "socket closed" exception in java, when reading from the socket...
167 #define PRINT(s) __android_log_write(ANDROID_LOG_DEBUG, NULL, s)
168 static const char* hex_table = "0123456789abcdef";
169 static inline void byte2hex(const char* data, char** str)
171 **str = hex_table[(*data >> 4) & 0xf];
173 **str = hex_table[*data & 0xf];
176 static inline void byte2char(const char* data, char** str)
178 **str = *data < ' ' ? '.' : *data > '~' ? '.' : *data;
181 static inline void word2hex(const char* data, char** hex)
183 byte2hex(&data[1], hex);
184 byte2hex(&data[0], hex);
186 void dump_bin(const char* title, const char* data, int size)
191 const int width = 16;
192 LOG_DEBUG("%s, size:%d, dump started {", title, size);
203 for(j = 0; j < width; j++)
205 byte2hex((const char*)&j, &line);
211 for(i = 0; i < size / width; i++)
216 word2hex((const char*)&addr, &line);
217 *line++ = ':'; *line++ = ' ';
219 for(j = 0; j < width; j++)
221 byte2hex(&data[j], &line);
225 for(j = 0; j < width; j++)
226 byte2char(data++, &line);
227 //wirte the end of line
232 //last line of left over if any
233 int leftover = size % width;
239 word2hex((const char*)&addr, &line);
240 *line++ = ':'; *line++ = ' ';
242 for(j = 0; j < leftover; j++) {
243 byte2hex(&data[j], &line);
247 for(; j < width; j++) {
253 for(j = 0; j < leftover; j++)
254 byte2char(data++, &line);
255 //write the end of line
260 LOG_DEBUG("%s, size:%d, dump ended }", title, size);