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 ******************************************************************************/
19 #define LOG_TAG "bt_btif_sock"
21 #include "btif_sock_util.h"
23 #include <arpa/inet.h>
25 #include <netinet/in.h>
26 #include <netinet/tcp.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
36 #include <hardware/bluetooth.h>
37 #include <hardware/bt_sock.h>
39 #include "bt_target.h"
41 #include "bta_jv_api.h"
42 #include "bta_jv_co.h"
43 #include "btif_common.h"
44 #include "btif_sock_sdp.h"
45 #include "btif_sock_thread.h"
46 #include "btif_util.h"
52 #include "osi/include/log.h"
56 #define asrt(s) if(!(s)) BTIF_TRACE_ERROR("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
58 int sock_send_all(int sock_fd, const uint8_t* buf, int len)
64 do ret = send(sock_fd, buf, s, 0);
65 while(ret < 0 && errno == EINTR);
68 BTIF_TRACE_ERROR("sock fd:%d send errno:%d, ret:%d", sock_fd, errno, ret);
76 int sock_recv_all(int sock_fd, uint8_t* buf, int len)
82 do ret = recv(sock_fd, buf, r, MSG_WAITALL);
83 while(ret < 0 && errno == EINTR);
86 BTIF_TRACE_ERROR("sock fd:%d recv errno:%d, ret:%d", sock_fd, errno, ret);
95 int sock_send_fd(int sock_fd, const uint8_t* buf, int len, int send_fd)
99 unsigned char *buffer = (unsigned char *)buf;
100 memset(&msg, 0, sizeof(msg));
102 struct cmsghdr *cmsg;
103 char msgbuf[CMSG_SPACE(1)];
105 if(sock_fd == -1 || send_fd == -1)
107 // Add any pending outbound file descriptors to the message
108 // See "man cmsg" really
109 msg.msg_control = msgbuf;
110 msg.msg_controllen = sizeof msgbuf;
111 cmsg = CMSG_FIRSTHDR(&msg);
112 cmsg->cmsg_level = SOL_SOCKET;
113 cmsg->cmsg_type = SCM_RIGHTS;
114 cmsg->cmsg_len = CMSG_LEN(sizeof send_fd);
115 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd);
117 // We only write our msg_control during the first write
121 memset(&iv, 0, sizeof(iv));
123 iv.iov_base = buffer;
130 ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
131 } while (ret < 0 && errno == EINTR);
134 BTIF_TRACE_ERROR("fd:%d, send_fd:%d, sendmsg ret:%d, errno:%d, %s",
135 sock_fd, send_fd, (int)ret, errno, strerror(errno));
143 // Wipes out any msg_control too
144 memset(&msg, 0, sizeof(msg));
146 BTIF_TRACE_DEBUG("close fd:%d after sent", send_fd);
147 // TODO: This seems wrong - if the FD is not opened in JAVA before this is called
148 // we get a "socket closed" exception in java, when reading from the socket...
153 static const char* hex_table = "0123456789abcdef";
154 static inline void byte2hex(const char* data, char** str)
156 **str = hex_table[(*data >> 4) & 0xf];
158 **str = hex_table[*data & 0xf];
161 static inline void byte2char(const char* data, char** str)
163 **str = *data < ' ' ? '.' : *data > '~' ? '.' : *data;
166 static inline void word2hex(const char* data, char** hex)
168 byte2hex(&data[1], hex);
169 byte2hex(&data[0], hex);
171 void dump_bin(const char* title, const char* data, int size)
176 const int width = 16;
177 LOG_DEBUG(LOG_TAG, "%s, size:%d, dump started {", title, size);
188 for(j = 0; j < width; j++)
190 byte2hex((const char*)&j, &line);
194 LOG_DEBUG(LOG_TAG, "%s", line_buff);
196 for(i = 0; i < size / width; i++)
201 word2hex((const char*)&addr, &line);
202 *line++ = ':'; *line++ = ' ';
204 for(j = 0; j < width; j++)
206 byte2hex(&data[j], &line);
210 for(j = 0; j < width; j++)
211 byte2char(data++, &line);
212 //wirte the end of line
215 LOG_DEBUG(LOG_TAG, "%s", line_buff);
217 //last line of left over if any
218 int leftover = size % width;
224 word2hex((const char*)&addr, &line);
225 *line++ = ':'; *line++ = ' ';
227 for(j = 0; j < leftover; j++) {
228 byte2hex(&data[j], &line);
232 for(; j < width; j++) {
238 for(j = 0; j < leftover; j++)
239 byte2char(data++, &line);
240 //write the end of line
243 LOG_DEBUG(LOG_TAG, "%s", line_buff);
245 LOG_DEBUG(LOG_TAG, "%s, size:%d, dump ended }", title, size);