1 /****************************************************************************** 2 * 3 * Copyright 2009-2012 Broadcom Corporation 4 * 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: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 ******************************************************************************/ 18 19 #define LOG_TAG "bt_btif_sock" 20 21 #include "btif_sock_util.h" 22 23 #include <arpa/inet.h> 24 #include <errno.h> 25 #include <netinet/in.h> 26 #include <netinet/tcp.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <sys/ioctl.h> 31 #include <sys/socket.h> 32 #include <sys/types.h> 33 #include <sys/un.h> 34 #include <unistd.h> 35 36 #include <hardware/bluetooth.h> 37 #include <hardware/bt_sock.h> 38 39 #include "bt_common.h" 40 #include "bt_target.h" 41 #include "bta_api.h" 42 #include "bta_jv_api.h" 43 #include "bta_jv_co.h" 44 #include "btif_common.h" 45 #include "btif_sock_sdp.h" 46 #include "btif_sock_thread.h" 47 #include "btif_util.h" 48 #include "btm_api.h" 49 #include "btm_int.h" 50 #include "btu.h" 51 #include "hcimsgs.h" 52 #include "osi/include/log.h" 53 #include "port_api.h" 54 #include "sdp_api.h" 55 56 #define asrt(s) \ 57 do { \ 58 if (!(s)) \ 59 BTIF_TRACE_ERROR("## %s assert %s failed at line:%d ##", __func__, #s, \ 60 __LINE__) \ 61 } while (0) 62 63 int sock_send_all(int sock_fd, const uint8_t* buf, int len) { 64 int s = len; 65 66 while (s) { 67 ssize_t ret; 68 OSI_NO_INTR(ret = send(sock_fd, buf, s, 0)); 69 if (ret <= 0) { 70 BTIF_TRACE_ERROR("sock fd:%d send errno:%d, ret:%d", sock_fd, errno, ret); 71 return -1; 72 } 73 buf += ret; 74 s -= ret; 75 } 76 return len; 77 } 78 int sock_recv_all(int sock_fd, uint8_t* buf, int len) { 79 int r = len; 80 81 while (r) { 82 ssize_t ret; 83 OSI_NO_INTR(ret = recv(sock_fd, buf, r, MSG_WAITALL)); 84 if (ret <= 0) { 85 BTIF_TRACE_ERROR("sock fd:%d recv errno:%d, ret:%d", sock_fd, errno, ret); 86 return -1; 87 } 88 buf += ret; 89 r -= ret; 90 } 91 return len; 92 } 93 94 int sock_send_fd(int sock_fd, const uint8_t* buf, int len, int send_fd) { 95 struct msghdr msg; 96 unsigned char* buffer = (unsigned char*)buf; 97 memset(&msg, 0, sizeof(msg)); 98 99 struct cmsghdr* cmsg; 100 char msgbuf[CMSG_SPACE(1)]; 101 asrt(send_fd != -1); 102 if (sock_fd == -1 || send_fd == -1) return -1; 103 // Add any pending outbound file descriptors to the message 104 // See "man cmsg" really 105 msg.msg_control = msgbuf; 106 msg.msg_controllen = sizeof msgbuf; 107 cmsg = CMSG_FIRSTHDR(&msg); 108 cmsg->cmsg_level = SOL_SOCKET; 109 cmsg->cmsg_type = SCM_RIGHTS; 110 cmsg->cmsg_len = CMSG_LEN(sizeof send_fd); 111 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd); 112 113 // We only write our msg_control during the first write 114 int ret_len = len; 115 while (len > 0) { 116 struct iovec iv; 117 memset(&iv, 0, sizeof(iv)); 118 119 iv.iov_base = buffer; 120 iv.iov_len = len; 121 122 msg.msg_iov = &iv; 123 msg.msg_iovlen = 1; 124 125 ssize_t ret; 126 OSI_NO_INTR(ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL)); 127 if (ret < 0) { 128 BTIF_TRACE_ERROR("fd:%d, send_fd:%d, sendmsg ret:%d, errno:%d, %s", 129 sock_fd, send_fd, (int)ret, errno, strerror(errno)); 130 ret_len = -1; 131 break; 132 } 133 134 buffer += ret; 135 len -= ret; 136 137 // Wipes out any msg_control too 138 memset(&msg, 0, sizeof(msg)); 139 } 140 BTIF_TRACE_DEBUG("close fd:%d after sent", send_fd); 141 // TODO: This seems wrong - if the FD is not opened in JAVA before this is 142 // called 143 // we get a "socket closed" exception in java, when reading from the 144 // socket... 145 close(send_fd); 146 return ret_len; 147 } 148