Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 1 | /* |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | * Copyright 2014 Samsung System LSI |
| 3 | * Copyright 2013 The Android Open Source Project |
Jakub Pawlowski | a09c348 | 2017-06-16 08:42:05 -0700 | [diff] [blame] | 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 | */ |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 17 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 18 | #define LOG_TAG "bt_btif_sock" |
| 19 | |
| 20 | #include "btif_sock_l2cap.h" |
| 21 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 22 | #include <errno.h> |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
Jakub Pawlowski | f47bfde | 2018-01-29 13:54:24 -0800 | [diff] [blame] | 28 | #include <vector> |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 29 | |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 30 | #include <mutex> |
| 31 | |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 32 | #include <hardware/bt_sock.h> |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 33 | |
Pavlin Radoslavov | 56a3be0 | 2015-06-02 13:54:58 -0700 | [diff] [blame] | 34 | #include "osi/include/allocator.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 35 | #include "osi/include/log.h" |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 36 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 37 | #include "bt_common.h" |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 38 | #include "bt_target.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 39 | #include "bta_api.h" |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 40 | #include "bta_jv_api.h" |
| 41 | #include "bta_jv_co.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 42 | #include "btif_common.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 43 | #include "btif_sock_sdp.h" |
| 44 | #include "btif_sock_thread.h" |
| 45 | #include "btif_sock_util.h" |
Adam Lesinski | 0620f97 | 2015-12-02 22:15:08 -0800 | [diff] [blame] | 46 | #include "btif_uid.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 47 | #include "btif_util.h" |
| 48 | #include "btm_api.h" |
| 49 | #include "btm_int.h" |
| 50 | #include "btu.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 51 | #include "hcimsgs.h" |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 52 | #include "l2c_api.h" |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 53 | #include "l2c_int.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 54 | #include "l2cdefs.h" |
| 55 | #include "port_api.h" |
| 56 | #include "sdp_api.h" |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 57 | |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 58 | struct packet { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 59 | struct packet *next, *prev; |
| 60 | uint32_t len; |
| 61 | uint8_t* data; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | typedef struct l2cap_socket { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 65 | struct l2cap_socket* prev; // link to prev list item |
| 66 | struct l2cap_socket* next; // link to next list item |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 67 | RawAddress addr; // other side's address |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 68 | char name[256]; // user-friendly name of the service |
| 69 | uint32_t id; // just a tag to find this struct |
| 70 | int app_uid; // The UID of the app who requested this socket |
| 71 | int handle; // handle from lower layers |
| 72 | unsigned security; // security flags |
| 73 | int channel; // channel (fixed_chan) or PSM (!fixed_chan) |
| 74 | int our_fd; // fd from our side |
| 75 | int app_fd; // fd from app's side |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 76 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 77 | unsigned bytes_buffered; |
| 78 | struct packet* first_packet; // fist packet to be delivered to app |
| 79 | struct packet* last_packet; // last packet to be delivered to app |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 80 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 81 | unsigned fixed_chan : 1; // fixed channel (or psm?) |
| 82 | unsigned server : 1; // is a server? (or connecting?) |
| 83 | unsigned connected : 1; // is connected? |
| 84 | unsigned outgoing_congest : 1; // should we hold? |
| 85 | unsigned server_psm_sent : 1; // The server shall only send PSM once. |
| 86 | bool is_le_coc; // is le connection oriented channel? |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 87 | uint16_t mtu; |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 88 | } l2cap_socket; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 89 | |
Jakub Pawlowski | e5e2dd5 | 2018-01-30 21:46:08 -0800 | [diff] [blame^] | 90 | static void btsock_l2cap_server_listen(l2cap_socket* sock); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 91 | |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 92 | static std::mutex state_lock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 93 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 94 | l2cap_socket* socks = NULL; |
Adam Lesinski | 0620f97 | 2015-12-02 22:15:08 -0800 | [diff] [blame] | 95 | static uid_set_t* uid_set = NULL; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 96 | static int pth = -1; |
| 97 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 98 | static void btsock_l2cap_cbk(tBTA_JV_EVT event, tBTA_JV* p_data, |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 99 | uint32_t l2cap_socket_id); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 100 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 101 | /* TODO: Consider to remove this buffer, as we have a buffer in l2cap as well, |
| 102 | * and we risk |
| 103 | * a buffer overflow with this implementation if the socket data is not |
| 104 | * read from |
| 105 | * JAVA for a while. In such a case we should use flow control to tell the |
| 106 | * sender to |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 107 | * back off. |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 108 | * BUT remember we need to avoid blocking the BTA task execution - hence |
| 109 | * we cannot |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 110 | * directly write to the socket. |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 111 | * we should be able to change to store the data pointer here, and just |
| 112 | * wait |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 113 | * confirming the l2cap_ind until we have more space in the buffer. */ |
| 114 | |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 115 | /* returns false if none - caller must free "data" memory when done with it */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 116 | static char packet_get_head_l(l2cap_socket* sock, uint8_t** data, |
| 117 | uint32_t* len) { |
| 118 | struct packet* p = sock->first_packet; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 119 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 120 | if (!p) return false; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 121 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 122 | if (data) *data = sock->first_packet->data; |
| 123 | if (len) *len = sock->first_packet->len; |
| 124 | sock->first_packet = p->next; |
| 125 | if (sock->first_packet) |
| 126 | sock->first_packet->prev = NULL; |
| 127 | else |
| 128 | sock->last_packet = NULL; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 129 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 130 | if (len) sock->bytes_buffered -= *len; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 131 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 132 | osi_free(p); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 133 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 134 | return true; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 137 | static struct packet* packet_alloc(const uint8_t* data, uint32_t len) { |
| 138 | struct packet* p = (struct packet*)osi_calloc(sizeof(*p)); |
| 139 | uint8_t* buf = (uint8_t*)osi_malloc(len); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 140 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 141 | p->data = buf; |
| 142 | p->len = len; |
| 143 | memcpy(p->data, data, len); |
| 144 | return p; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 147 | /* makes a copy of the data, returns true on success */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 148 | static char packet_put_head_l(l2cap_socket* sock, const void* data, |
| 149 | uint32_t len) { |
| 150 | struct packet* p = packet_alloc((const uint8_t*)data, len); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 151 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 152 | /* |
| 153 | * We do not check size limits here since this is used to undo "getting" a |
| 154 | * packet that the user read incompletely. That is to say the packet was |
| 155 | * already in the queue. We do check thos elimits in packet_put_tail_l() since |
| 156 | * that function is used to put new data into the queue. |
| 157 | */ |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 158 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 159 | if (!p) return false; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 160 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 161 | p->prev = NULL; |
| 162 | p->next = sock->first_packet; |
| 163 | sock->first_packet = p; |
| 164 | if (p->next) |
| 165 | p->next->prev = p; |
| 166 | else |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 167 | sock->last_packet = p; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 168 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 169 | sock->bytes_buffered += len; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 170 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 171 | return true; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 174 | /* makes a copy of the data, returns true on success */ |
| 175 | static char packet_put_tail_l(l2cap_socket* sock, const void* data, |
| 176 | uint32_t len) { |
| 177 | struct packet* p = packet_alloc((const uint8_t*)data, len); |
| 178 | |
| 179 | if (sock->bytes_buffered >= L2CAP_MAX_RX_BUFFER) { |
| 180 | LOG_ERROR(LOG_TAG, "packet_put_tail_l: buffer overflow"); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if (!p) { |
| 185 | LOG_ERROR(LOG_TAG, "packet_put_tail_l: unable to allocate packet..."); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | p->next = NULL; |
| 190 | p->prev = sock->last_packet; |
| 191 | sock->last_packet = p; |
| 192 | if (p->prev) |
| 193 | p->prev->next = p; |
| 194 | else |
| 195 | sock->first_packet = p; |
| 196 | |
| 197 | sock->bytes_buffered += len; |
| 198 | |
| 199 | return true; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 202 | static char is_inited(void) { |
| 203 | std::unique_lock<std::mutex> lock(state_lock); |
| 204 | return pth != -1; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 207 | /* only call with std::mutex taken */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 208 | static l2cap_socket* btsock_l2cap_find_by_id_l(uint32_t id) { |
| 209 | l2cap_socket* sock = socks; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 210 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 211 | while (sock && sock->id != id) sock = sock->next; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 212 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 213 | return sock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 216 | static void btsock_l2cap_free_l(l2cap_socket* sock) { |
| 217 | uint8_t* buf; |
| 218 | l2cap_socket* t = socks; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 219 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 220 | while (t && t != sock) t = t->next; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 221 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 222 | if (!t) /* prever double-frees */ |
| 223 | return; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 224 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 225 | if (sock->next) sock->next->prev = sock->prev; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 226 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 227 | if (sock->prev) |
| 228 | sock->prev->next = sock->next; |
| 229 | else |
| 230 | socks = sock->next; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 231 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 232 | shutdown(sock->our_fd, SHUT_RDWR); |
| 233 | close(sock->our_fd); |
| 234 | if (sock->app_fd != -1) { |
| 235 | close(sock->app_fd); |
| 236 | } else { |
| 237 | APPL_TRACE_ERROR("SOCK_LIST: free(id = %d) - NO app_fd!", sock->id); |
| 238 | } |
| 239 | |
| 240 | while (packet_get_head_l(sock, &buf, NULL)) osi_free(buf); |
| 241 | |
| 242 | // lower-level close() should be idempotent... so let's call it and see... |
| 243 | if (sock->is_le_coc) { |
| 244 | // Only call if we are non server connections |
Myles Watson | b749ebd | 2017-10-06 16:39:05 -0700 | [diff] [blame] | 245 | if (sock->handle >= 0 && (!sock->server)) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 246 | BTA_JvL2capClose(sock->handle); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 247 | } |
Myles Watson | b749ebd | 2017-10-06 16:39:05 -0700 | [diff] [blame] | 248 | if ((sock->channel >= 0) && (sock->server)) { |
Stanley Tng | 49dd53c | 2017-12-20 09:38:30 -0800 | [diff] [blame] | 249 | BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP_LE); |
Navin Kochar | 6721232 | 2016-03-09 23:11:53 +0530 | [diff] [blame] | 250 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 251 | } else { |
| 252 | // Only call if we are non server connections |
Myles Watson | b749ebd | 2017-10-06 16:39:05 -0700 | [diff] [blame] | 253 | if ((sock->handle >= 0) && (!sock->server)) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 254 | if (sock->fixed_chan) |
| 255 | BTA_JvL2capCloseLE(sock->handle); |
| 256 | else |
| 257 | BTA_JvL2capClose(sock->handle); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 258 | } |
Myles Watson | b749ebd | 2017-10-06 16:39:05 -0700 | [diff] [blame] | 259 | if ((sock->channel >= 0) && (sock->server)) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 260 | if (sock->fixed_chan) |
| 261 | BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP_LE); |
| 262 | else |
| 263 | BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 264 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 265 | if (!sock->fixed_chan) { |
| 266 | APPL_TRACE_DEBUG("%s stopping L2CAP server channel %d", __func__, |
| 267 | sock->channel); |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 268 | BTA_JvL2capStopServer(sock->channel, sock->id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | APPL_TRACE_DEBUG("%s: free(id = %d)", __func__, sock->id); |
| 274 | osi_free(sock); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 277 | static l2cap_socket* btsock_l2cap_alloc_l(const char* name, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 278 | const RawAddress* addr, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 279 | char is_server, int flags) { |
| 280 | unsigned security = 0; |
| 281 | int fds[2]; |
| 282 | l2cap_socket* sock = (l2cap_socket*)osi_calloc(sizeof(*sock)); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 283 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 284 | if (flags & BTSOCK_FLAG_ENCRYPT) |
| 285 | security |= is_server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT; |
| 286 | if (flags & BTSOCK_FLAG_AUTH) |
| 287 | security |= is_server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE; |
| 288 | if (flags & BTSOCK_FLAG_AUTH_MITM) |
| 289 | security |= is_server ? BTM_SEC_IN_MITM : BTM_SEC_OUT_MITM; |
| 290 | if (flags & BTSOCK_FLAG_AUTH_16_DIGIT) |
| 291 | security |= BTM_SEC_IN_MIN_16_DIGIT_PIN; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 292 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 293 | if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, fds)) { |
| 294 | APPL_TRACE_ERROR("socketpair failed, errno:%d", errno); |
| 295 | goto fail_sockpair; |
| 296 | } |
| 297 | |
| 298 | sock->our_fd = fds[0]; |
| 299 | sock->app_fd = fds[1]; |
| 300 | sock->security = security; |
| 301 | sock->server = is_server; |
| 302 | sock->connected = false; |
| 303 | sock->handle = 0; |
| 304 | sock->server_psm_sent = false; |
| 305 | sock->app_uid = -1; |
| 306 | |
| 307 | if (name) strncpy(sock->name, name, sizeof(sock->name) - 1); |
| 308 | if (addr) sock->addr = *addr; |
| 309 | |
| 310 | sock->first_packet = NULL; |
| 311 | sock->last_packet = NULL; |
| 312 | |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 313 | sock->mtu = L2CAP_LE_MIN_MTU; |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 314 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 315 | sock->next = socks; |
| 316 | sock->prev = NULL; |
| 317 | if (socks) socks->prev = sock; |
| 318 | sock->id = (socks ? socks->id : 0) + 1; |
| 319 | socks = sock; |
| 320 | /* paranoia cap on: verify no ID duplicates due to overflow and fix as needed |
| 321 | */ |
| 322 | while (1) { |
| 323 | l2cap_socket* t; |
| 324 | t = socks->next; |
| 325 | while (t && t->id != sock->id) { |
| 326 | t = t->next; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 327 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 328 | if (!t && sock->id) /* non-zeor handle is unique -> we're done */ |
| 329 | break; |
| 330 | /* if we're here, we found a duplicate */ |
| 331 | if (!++sock->id) /* no zero IDs allowed */ |
| 332 | sock->id++; |
| 333 | } |
| 334 | APPL_TRACE_DEBUG("SOCK_LIST: alloc(id = %d)", sock->id); |
| 335 | return sock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 336 | |
| 337 | fail_sockpair: |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 338 | osi_free(sock); |
| 339 | return NULL; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 342 | bt_status_t btsock_l2cap_init(int handle, uid_set_t* set) { |
Stanley Tng | 49dd53c | 2017-12-20 09:38:30 -0800 | [diff] [blame] | 343 | APPL_TRACE_DEBUG("%s: handle = %d", __func__, handle); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 344 | std::unique_lock<std::mutex> lock(state_lock); |
| 345 | pth = handle; |
| 346 | socks = NULL; |
| 347 | uid_set = set; |
| 348 | return BT_STATUS_SUCCESS; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 351 | bt_status_t btsock_l2cap_cleanup() { |
| 352 | std::unique_lock<std::mutex> lock(state_lock); |
| 353 | pth = -1; |
| 354 | while (socks) btsock_l2cap_free_l(socks); |
| 355 | return BT_STATUS_SUCCESS; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 358 | static inline bool send_app_psm_or_chan_l(l2cap_socket* sock) { |
Stanley Tng | 49dd53c | 2017-12-20 09:38:30 -0800 | [diff] [blame] | 359 | APPL_TRACE_DEBUG("%s: channel=%d", __func__, sock->channel); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 360 | return sock_send_all(sock->our_fd, (const uint8_t*)&sock->channel, |
| 361 | sizeof(sock->channel)) == sizeof(sock->channel); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 364 | static bool send_app_connect_signal(int fd, const RawAddress* addr, int channel, |
| 365 | int status, int send_fd, int tx_mtu) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 366 | sock_connect_signal_t cs; |
| 367 | cs.size = sizeof(cs); |
| 368 | cs.bd_addr = *addr; |
| 369 | cs.channel = channel; |
| 370 | cs.status = status; |
| 371 | cs.max_rx_packet_size = L2CAP_MAX_SDU_LENGTH; |
| 372 | cs.max_tx_packet_size = tx_mtu; |
| 373 | if (send_fd != -1) { |
| 374 | if (sock_send_fd(fd, (const uint8_t*)&cs, sizeof(cs), send_fd) == |
| 375 | sizeof(cs)) |
| 376 | return true; |
| 377 | else |
| 378 | APPL_TRACE_ERROR("sock_send_fd failed, fd:%d, send_fd:%d", fd, send_fd); |
| 379 | } else if (sock_send_all(fd, (const uint8_t*)&cs, sizeof(cs)) == sizeof(cs)) { |
| 380 | return true; |
| 381 | } |
| 382 | return false; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 385 | static void on_srv_l2cap_listen_started(tBTA_JV_L2CAP_START* p_start, |
| 386 | uint32_t id) { |
| 387 | l2cap_socket* sock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 388 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 389 | std::unique_lock<std::mutex> lock(state_lock); |
| 390 | sock = btsock_l2cap_find_by_id_l(id); |
| 391 | if (!sock) return; |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 392 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 393 | if (p_start->status != BTA_JV_SUCCESS) { |
| 394 | APPL_TRACE_ERROR("Error starting l2cap_listen - status: 0x%04x", |
| 395 | p_start->status); |
| 396 | btsock_l2cap_free_l(sock); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | sock->handle = p_start->handle; |
| 401 | APPL_TRACE_DEBUG("on_srv_l2cap_listen_started() sock->handle =%d id:%d", |
| 402 | sock->handle, sock->id); |
| 403 | |
Myles Watson | b749ebd | 2017-10-06 16:39:05 -0700 | [diff] [blame] | 404 | if (!sock->server_psm_sent) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 405 | if (!send_app_psm_or_chan_l(sock)) { |
| 406 | // closed |
| 407 | APPL_TRACE_DEBUG("send_app_psm() failed, close rs->id:%d", sock->id); |
| 408 | btsock_l2cap_free_l(sock); |
| 409 | } else { |
| 410 | sock->server_psm_sent = true; |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 411 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 412 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 415 | static void on_cl_l2cap_init(tBTA_JV_L2CAP_CL_INIT* p_init, uint32_t id) { |
| 416 | l2cap_socket* sock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 417 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 418 | std::unique_lock<std::mutex> lock(state_lock); |
| 419 | sock = btsock_l2cap_find_by_id_l(id); |
| 420 | if (!sock) return; |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 421 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 422 | if (p_init->status != BTA_JV_SUCCESS) { |
| 423 | btsock_l2cap_free_l(sock); |
| 424 | return; |
| 425 | } |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 426 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 427 | sock->handle = p_init->handle; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 431 | * Here we allocate a new sock instance to mimic the BluetoothSocket. The socket |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 432 | * will be a clone of the sock representing the BluetoothServerSocket. |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 433 | * */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 434 | static void on_srv_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, |
| 435 | l2cap_socket* sock) { |
| 436 | l2cap_socket* accept_rs; |
| 437 | uint32_t new_listen_id; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 438 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 439 | // std::mutex locked by caller |
Jakub Pawlowski | a09c348 | 2017-06-16 08:42:05 -0700 | [diff] [blame] | 440 | accept_rs = btsock_l2cap_alloc_l(sock->name, &p_open->rem_bda, false, 0); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 441 | accept_rs->connected = true; |
| 442 | accept_rs->security = sock->security; |
| 443 | accept_rs->fixed_chan = sock->fixed_chan; |
| 444 | accept_rs->channel = sock->channel; |
| 445 | accept_rs->handle = sock->handle; |
| 446 | accept_rs->app_uid = sock->app_uid; |
| 447 | sock->handle = |
| 448 | -1; /* We should no longer associate this handle with the server socket */ |
| 449 | accept_rs->is_le_coc = sock->is_le_coc; |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 450 | accept_rs->mtu = sock->mtu; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 451 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 452 | /* Swap IDs to hand over the GAP connection to the accepted socket, and start |
| 453 | a new server on |
| 454 | the newly create socket ID. */ |
| 455 | new_listen_id = accept_rs->id; |
| 456 | accept_rs->id = sock->id; |
| 457 | sock->id = new_listen_id; |
| 458 | |
| 459 | if (accept_rs) { |
| 460 | // start monitor the socket |
| 461 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, |
| 462 | SOCK_THREAD_FD_EXCEPTION, sock->id); |
| 463 | btsock_thread_add_fd(pth, accept_rs->our_fd, BTSOCK_L2CAP, |
| 464 | SOCK_THREAD_FD_RD, accept_rs->id); |
| 465 | APPL_TRACE_DEBUG( |
| 466 | "sending connect signal & app fd: %d to app server to accept() the" |
| 467 | " connection", |
| 468 | accept_rs->app_fd); |
| 469 | APPL_TRACE_DEBUG("server fd:%d, scn:%d", sock->our_fd, sock->channel); |
| 470 | send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0, |
| 471 | accept_rs->app_fd, p_open->tx_mtu); |
| 472 | accept_rs->app_fd = |
| 473 | -1; // The fd is closed after sent to app in send_app_connect_signal() |
| 474 | // But for some reason we still leak a FD - either the server socket |
| 475 | // one or the accept socket one. |
Jakub Pawlowski | e5e2dd5 | 2018-01-30 21:46:08 -0800 | [diff] [blame^] | 476 | btsock_l2cap_server_listen(sock); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | static void on_srv_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN* p_open, |
| 481 | l2cap_socket* sock) { |
| 482 | l2cap_socket* accept_rs; |
| 483 | uint32_t new_listen_id; |
| 484 | |
| 485 | // std::mutex locked by caller |
Jakub Pawlowski | a09c348 | 2017-06-16 08:42:05 -0700 | [diff] [blame] | 486 | accept_rs = btsock_l2cap_alloc_l(sock->name, &p_open->rem_bda, false, 0); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 487 | if (accept_rs) { |
| 488 | // swap IDs |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 489 | new_listen_id = accept_rs->id; |
| 490 | accept_rs->id = sock->id; |
| 491 | sock->id = new_listen_id; |
| 492 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 493 | accept_rs->handle = p_open->handle; |
| 494 | accept_rs->connected = true; |
| 495 | accept_rs->security = sock->security; |
| 496 | accept_rs->fixed_chan = sock->fixed_chan; |
| 497 | accept_rs->channel = sock->channel; |
| 498 | accept_rs->app_uid = sock->app_uid; |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 499 | accept_rs->mtu = sock->mtu; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 500 | |
| 501 | // if we do not set a callback, this socket will be dropped */ |
| 502 | *(p_open->p_p_cback) = (void*)btsock_l2cap_cbk; |
| 503 | *(p_open->p_user_data) = UINT_TO_PTR(accept_rs->id); |
| 504 | |
| 505 | // start monitor the socket |
| 506 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, |
| 507 | SOCK_THREAD_FD_EXCEPTION, sock->id); |
| 508 | btsock_thread_add_fd(pth, accept_rs->our_fd, BTSOCK_L2CAP, |
| 509 | SOCK_THREAD_FD_RD, accept_rs->id); |
| 510 | APPL_TRACE_DEBUG( |
| 511 | "sending connect signal & app fd:%dto app server to accept() the" |
| 512 | " connection", |
| 513 | accept_rs->app_fd); |
| 514 | APPL_TRACE_DEBUG("server fd:%d, scn:%d", sock->our_fd, sock->channel); |
| 515 | send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0, |
| 516 | accept_rs->app_fd, p_open->tx_mtu); |
| 517 | accept_rs->app_fd = -1; // the fd is closed after sent to app |
| 518 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 521 | static void on_cl_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, |
| 522 | l2cap_socket* sock) { |
Jakub Pawlowski | a09c348 | 2017-06-16 08:42:05 -0700 | [diff] [blame] | 523 | sock->addr = p_open->rem_bda; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 524 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 525 | if (!send_app_psm_or_chan_l(sock)) { |
| 526 | APPL_TRACE_ERROR("send_app_psm_or_chan_l failed"); |
| 527 | return; |
| 528 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 529 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 530 | if (send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1, |
| 531 | p_open->tx_mtu)) { |
| 532 | // start monitoring the socketpair to get call back when app writing data |
| 533 | APPL_TRACE_DEBUG( |
| 534 | "on_l2cap_connect_ind, connect signal sent, slot id:%d, psm:%d," |
| 535 | " server:%d", |
| 536 | sock->id, sock->channel, sock->server); |
| 537 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, |
| 538 | sock->id); |
| 539 | sock->connected = true; |
| 540 | } else |
| 541 | APPL_TRACE_ERROR("send_app_connect_signal failed"); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 544 | static void on_cl_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN* p_open, |
| 545 | l2cap_socket* sock) { |
Jakub Pawlowski | a09c348 | 2017-06-16 08:42:05 -0700 | [diff] [blame] | 546 | sock->addr = p_open->rem_bda; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 547 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 548 | if (!send_app_psm_or_chan_l(sock)) { |
| 549 | APPL_TRACE_ERROR("send_app_psm_or_chan_l failed"); |
| 550 | return; |
| 551 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 552 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 553 | if (send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1, |
| 554 | p_open->tx_mtu)) { |
| 555 | // start monitoring the socketpair to get call back when app writing data |
| 556 | APPL_TRACE_DEBUG( |
| 557 | "on_l2cap_connect_ind, connect signal sent, slot id:%d, Chan:%d," |
| 558 | " server:%d", |
| 559 | sock->id, sock->channel, sock->server); |
| 560 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, |
| 561 | sock->id); |
| 562 | sock->connected = true; |
| 563 | } else |
| 564 | APPL_TRACE_ERROR("send_app_connect_signal failed"); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 567 | static void on_l2cap_connect(tBTA_JV* p_data, uint32_t id) { |
| 568 | l2cap_socket* sock; |
| 569 | tBTA_JV_L2CAP_OPEN* psm_open = &p_data->l2c_open; |
| 570 | tBTA_JV_L2CAP_LE_OPEN* le_open = &p_data->l2c_le_open; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 571 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 572 | std::unique_lock<std::mutex> lock(state_lock); |
| 573 | sock = btsock_l2cap_find_by_id_l(id); |
| 574 | if (!sock) { |
| 575 | APPL_TRACE_ERROR("on_l2cap_connect on unknown socket"); |
| 576 | return; |
| 577 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 578 | |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 579 | sock->mtu = le_open->tx_mtu; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 580 | if (sock->fixed_chan && le_open->status == BTA_JV_SUCCESS) { |
| 581 | if (!sock->server) |
| 582 | on_cl_l2cap_le_connect_l(le_open, sock); |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 583 | else |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 584 | on_srv_l2cap_le_connect_l(le_open, sock); |
| 585 | } else if (!sock->fixed_chan && psm_open->status == BTA_JV_SUCCESS) { |
| 586 | if (!sock->server) |
| 587 | on_cl_l2cap_psm_connect_l(psm_open, sock); |
| 588 | else |
| 589 | on_srv_l2cap_psm_connect_l(psm_open, sock); |
| 590 | } else |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 591 | btsock_l2cap_free_l(sock); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 594 | static void on_l2cap_close(tBTA_JV_L2CAP_CLOSE* p_close, uint32_t id) { |
| 595 | l2cap_socket* sock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 596 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 597 | std::unique_lock<std::mutex> lock(state_lock); |
| 598 | sock = btsock_l2cap_find_by_id_l(id); |
| 599 | if (!sock) return; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 600 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 601 | APPL_TRACE_DEBUG("on_l2cap_close, slot id:%d, fd:%d, %s:%d, server:%d", |
| 602 | sock->id, sock->our_fd, |
| 603 | sock->fixed_chan ? "fixed_chan" : "PSM", sock->channel, |
| 604 | sock->server); |
| 605 | // TODO: This does not seem to be called... |
| 606 | // I'm not sure if this will be called for non-server sockets? |
Myles Watson | b749ebd | 2017-10-06 16:39:05 -0700 | [diff] [blame] | 607 | if (!sock->fixed_chan && (sock->server)) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 608 | BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); |
| 609 | } |
| 610 | btsock_l2cap_free_l(sock); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 613 | static void on_l2cap_outgoing_congest(tBTA_JV_L2CAP_CONG* p, uint32_t id) { |
| 614 | l2cap_socket* sock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 615 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 616 | std::unique_lock<std::mutex> lock(state_lock); |
| 617 | sock = btsock_l2cap_find_by_id_l(id); |
| 618 | if (!sock) return; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 619 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 620 | sock->outgoing_congest = p->cong ? 1 : 0; |
| 621 | // mointer the fd for any outgoing data |
| 622 | if (!sock->outgoing_congest) { |
| 623 | APPL_TRACE_DEBUG( |
| 624 | "on_l2cap_outgoing_congest: adding fd to btsock_thread..."); |
| 625 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, |
| 626 | sock->id); |
| 627 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Jakub Pawlowski | c474342 | 2018-01-11 07:34:39 -0800 | [diff] [blame] | 630 | static void on_l2cap_write_done(uint16_t len, uint32_t id) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 631 | std::unique_lock<std::mutex> lock(state_lock); |
Jakub Pawlowski | 19d3061 | 2018-01-08 01:23:52 -0800 | [diff] [blame] | 632 | l2cap_socket* sock = btsock_l2cap_find_by_id_l(id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 633 | if (!sock) return; |
| 634 | |
Jakub Pawlowski | 19d3061 | 2018-01-08 01:23:52 -0800 | [diff] [blame] | 635 | int app_uid = sock->app_uid; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 636 | if (!sock->outgoing_congest) { |
| 637 | // monitor the fd for any outgoing data |
| 638 | APPL_TRACE_DEBUG("on_l2cap_write_done: adding fd to btsock_thread..."); |
| 639 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, |
| 640 | sock->id); |
| 641 | } |
| 642 | |
| 643 | uid_set_add_tx(uid_set, app_uid, len); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 644 | } |
| 645 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 646 | static void on_l2cap_data_ind(tBTA_JV* evt, uint32_t id) { |
| 647 | l2cap_socket* sock; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 648 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 649 | int app_uid = -1; |
| 650 | uint32_t bytes_read = 0; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 651 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 652 | std::unique_lock<std::mutex> lock(state_lock); |
| 653 | sock = btsock_l2cap_find_by_id_l(id); |
| 654 | if (!sock) return; |
| 655 | |
| 656 | app_uid = sock->app_uid; |
| 657 | |
| 658 | if (sock->fixed_chan) { /* we do these differently */ |
| 659 | |
| 660 | tBTA_JV_LE_DATA_IND* p_le_data_ind = &evt->le_data_ind; |
| 661 | BT_HDR* p_buf = p_le_data_ind->p_buf; |
| 662 | uint8_t* data = (uint8_t*)(p_buf + 1) + p_buf->offset; |
| 663 | |
| 664 | if (packet_put_tail_l(sock, data, p_buf->len)) { |
| 665 | bytes_read = p_buf->len; |
| 666 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, |
| 667 | sock->id); |
| 668 | } else { // connection must be dropped |
| 669 | APPL_TRACE_DEBUG( |
| 670 | "on_l2cap_data_ind() unable to push data to socket - closing" |
| 671 | " fixed channel"); |
| 672 | BTA_JvL2capCloseLE(sock->handle); |
| 673 | btsock_l2cap_free_l(sock); |
| 674 | } |
| 675 | |
| 676 | } else { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 677 | uint32_t count; |
| 678 | |
| 679 | if (BTA_JvL2capReady(sock->handle, &count) == BTA_JV_SUCCESS) { |
Jakub Pawlowski | f47bfde | 2018-01-29 13:54:24 -0800 | [diff] [blame] | 680 | std::vector<uint8_t> buffer(count); |
| 681 | if (BTA_JvL2capRead(sock->handle, sock->id, buffer.data(), count) == |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 682 | BTA_JV_SUCCESS) { |
Jakub Pawlowski | f47bfde | 2018-01-29 13:54:24 -0800 | [diff] [blame] | 683 | if (packet_put_tail_l(sock, buffer.data(), count)) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 684 | bytes_read = count; |
| 685 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, |
| 686 | SOCK_THREAD_FD_WR, sock->id); |
| 687 | } else { // connection must be dropped |
| 688 | APPL_TRACE_DEBUG( |
| 689 | "on_l2cap_data_ind() unable to push data to socket" |
| 690 | " - closing channel"); |
| 691 | BTA_JvL2capClose(sock->handle); |
| 692 | btsock_l2cap_free_l(sock); |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 693 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 694 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 695 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 696 | } |
Adam Lesinski | 0620f97 | 2015-12-02 22:15:08 -0800 | [diff] [blame] | 697 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 698 | uid_set_add_rx(uid_set, app_uid, bytes_read); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 701 | static void btsock_l2cap_cbk(tBTA_JV_EVT event, tBTA_JV* p_data, |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 702 | uint32_t l2cap_socket_id) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 703 | switch (event) { |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 704 | case BTA_JV_L2CAP_START_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 705 | on_srv_l2cap_listen_started(&p_data->l2c_start, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 706 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 707 | |
| 708 | case BTA_JV_L2CAP_CL_INIT_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 709 | on_cl_l2cap_init(&p_data->l2c_cl_init, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 710 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 711 | |
| 712 | case BTA_JV_L2CAP_OPEN_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 713 | on_l2cap_connect(p_data, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 714 | BTA_JvSetPmProfile(p_data->l2c_open.handle, BTA_JV_PM_ID_1, |
| 715 | BTA_JV_CONN_OPEN); |
| 716 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 717 | |
| 718 | case BTA_JV_L2CAP_CLOSE_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 719 | APPL_TRACE_DEBUG("BTA_JV_L2CAP_CLOSE_EVT: id: %u", l2cap_socket_id); |
| 720 | on_l2cap_close(&p_data->l2c_close, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 721 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 722 | |
| 723 | case BTA_JV_L2CAP_DATA_IND_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 724 | on_l2cap_data_ind(p_data, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 725 | APPL_TRACE_DEBUG("BTA_JV_L2CAP_DATA_IND_EVT"); |
| 726 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 727 | |
| 728 | case BTA_JV_L2CAP_READ_EVT: |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 729 | APPL_TRACE_DEBUG("BTA_JV_L2CAP_READ_EVT not used"); |
| 730 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 731 | |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 732 | case BTA_JV_L2CAP_WRITE_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 733 | APPL_TRACE_DEBUG("BTA_JV_L2CAP_WRITE_EVT: id: %u", l2cap_socket_id); |
Jakub Pawlowski | c474342 | 2018-01-11 07:34:39 -0800 | [diff] [blame] | 734 | on_l2cap_write_done(p_data->l2c_write.len, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 735 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 736 | |
| 737 | case BTA_JV_L2CAP_WRITE_FIXED_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 738 | APPL_TRACE_DEBUG("BTA_JV_L2CAP_WRITE_FIXED_EVT: id: %u", l2cap_socket_id); |
Jakub Pawlowski | c474342 | 2018-01-11 07:34:39 -0800 | [diff] [blame] | 739 | on_l2cap_write_done(p_data->l2c_write.len, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 740 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 741 | |
| 742 | case BTA_JV_L2CAP_CONG_EVT: |
Ajay Panicker | 531fe1a | 2016-11-22 13:28:43 -0800 | [diff] [blame] | 743 | on_l2cap_outgoing_congest(&p_data->l2c_cong, l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 744 | break; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 745 | |
| 746 | default: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 747 | APPL_TRACE_ERROR("unhandled event %d, slot id: %u", event, |
| 748 | l2cap_socket_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 749 | break; |
| 750 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* L2CAP default options for OBEX socket connections */ |
Myles Watson | f454816 | 2016-10-19 09:53:56 -0700 | [diff] [blame] | 754 | const tL2CAP_FCR_OPTS obex_l2c_fcr_opts_def = { |
| 755 | L2CAP_FCR_ERTM_MODE, /* Mandatory for OBEX over l2cap */ |
| 756 | OBX_FCR_OPT_TX_WINDOW_SIZE_BR_EDR, /* Tx window size */ |
| 757 | OBX_FCR_OPT_MAX_TX_B4_DISCNT, /* Maximum transmissions before |
| 758 | disconnecting */ |
| 759 | OBX_FCR_OPT_RETX_TOUT, /* Retransmission timeout (2 secs) */ |
| 760 | OBX_FCR_OPT_MONITOR_TOUT, /* Monitor timeout (12 secs) */ |
| 761 | OBX_FCR_OPT_MAX_PDU_SIZE /* MPS segment size */ |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 762 | }; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 763 | const tL2CAP_ERTM_INFO obex_l2c_etm_opt = { |
| 764 | L2CAP_FCR_ERTM_MODE, /* Mandatory for OBEX over l2cap */ |
| 765 | L2CAP_FCR_CHAN_OPT_ERTM, /* Mandatory for OBEX over l2cap */ |
| 766 | OBX_USER_RX_BUF_SIZE, OBX_USER_TX_BUF_SIZE, |
| 767 | OBX_FCR_RX_BUF_SIZE, OBX_FCR_TX_BUF_SIZE}; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 768 | |
| 769 | /** |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 770 | * When using a dynamic PSM, a PSM allocation is requested from |
| 771 | * btsock_l2cap_listen_or_connect(). |
| 772 | * The PSM allocation event is refeived in the JV-callback - currently located |
| 773 | * in RFC-code - |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 774 | * and this function is called with the newly allocated PSM. |
| 775 | */ |
| 776 | void on_l2cap_psm_assigned(int id, int psm) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 777 | /* Setup ETM settings: |
| 778 | * mtu will be set below */ |
| 779 | std::unique_lock<std::mutex> lock(state_lock); |
| 780 | l2cap_socket* sock = btsock_l2cap_find_by_id_l(id); |
| 781 | if (!sock) { |
| 782 | APPL_TRACE_ERROR("%s: Error: sock is null", __func__); |
| 783 | return; |
| 784 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 785 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 786 | sock->channel = psm; |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 787 | |
Jakub Pawlowski | e5e2dd5 | 2018-01-30 21:46:08 -0800 | [diff] [blame^] | 788 | btsock_l2cap_server_listen(sock); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Jakub Pawlowski | e5e2dd5 | 2018-01-30 21:46:08 -0800 | [diff] [blame^] | 791 | static void btsock_l2cap_server_listen(l2cap_socket* sock) { |
Stanley Tng | 49dd53c | 2017-12-20 09:38:30 -0800 | [diff] [blame] | 792 | APPL_TRACE_DEBUG("%s: fixed_chan=%d, channel=%d, is_le_coc=%d", __func__, |
| 793 | sock->fixed_chan, sock->channel, sock->is_le_coc); |
| 794 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 795 | if (sock->fixed_chan) { |
Jakub Pawlowski | e5e2dd5 | 2018-01-30 21:46:08 -0800 | [diff] [blame^] | 796 | BTA_JvL2capStartServerLE(sock->channel, btsock_l2cap_cbk, sock->id); |
| 797 | return; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 798 | } |
Jakub Pawlowski | e5e2dd5 | 2018-01-30 21:46:08 -0800 | [diff] [blame^] | 799 | |
| 800 | int connection_type = |
| 801 | sock->is_le_coc ? BTA_JV_CONN_TYPE_L2CAP_LE : BTA_JV_CONN_TYPE_L2CAP; |
| 802 | |
| 803 | /* If we have a channel specified in the request, just start the server, |
| 804 | * else we request a PSM and start the server after we receive a PSM. */ |
| 805 | if (sock->channel <= 0) { |
| 806 | BTA_JvGetChannelId(connection_type, sock->id, 0); |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | /* Setup ETM settings: mtu will be set below */ |
| 811 | std::unique_ptr<tL2CAP_CFG_INFO> cfg = std::make_unique<tL2CAP_CFG_INFO>( |
| 812 | tL2CAP_CFG_INFO{.fcr_present = true, .fcr = obex_l2c_fcr_opts_def}); |
| 813 | |
| 814 | std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info; |
| 815 | if (!sock->is_le_coc) { |
| 816 | ertm_info.reset(new tL2CAP_ERTM_INFO(obex_l2c_etm_opt)); |
| 817 | } |
| 818 | |
| 819 | BTA_JvL2capStartServer( |
| 820 | connection_type, sock->security, 0, std::move(ertm_info), sock->channel, |
| 821 | L2CAP_MAX_SDU_LENGTH, std::move(cfg), btsock_l2cap_cbk, sock->id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static bt_status_t btsock_l2cap_listen_or_connect(const char* name, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 825 | const RawAddress* addr, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 826 | int channel, int* sock_fd, |
| 827 | int flags, char listen, |
| 828 | int app_uid) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 829 | int fixed_chan = 1; |
| 830 | l2cap_socket* sock; |
| 831 | tL2CAP_CFG_INFO cfg; |
| 832 | bool is_le_coc = false; |
| 833 | |
| 834 | if (!sock_fd) return BT_STATUS_PARM_INVALID; |
| 835 | |
| 836 | if (channel < 0) { |
| 837 | // We need to auto assign a PSM |
| 838 | fixed_chan = 0; |
| 839 | } else { |
Stanley Tng | a34e9b4 | 2018-01-05 09:25:11 -0800 | [diff] [blame] | 840 | is_le_coc = (flags & BTSOCK_FLAG_LE_COC) != 0; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 841 | fixed_chan = (channel & L2CAP_MASK_FIXED_CHANNEL) != 0; |
Stanley Tng | a34e9b4 | 2018-01-05 09:25:11 -0800 | [diff] [blame] | 842 | channel &= ~L2CAP_MASK_FIXED_CHANNEL; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | if (!is_inited()) return BT_STATUS_NOT_READY; |
| 846 | |
| 847 | // TODO: This is kind of bad to lock here, but it is needed for the current |
| 848 | // design. |
| 849 | std::unique_lock<std::mutex> lock(state_lock); |
| 850 | sock = btsock_l2cap_alloc_l(name, addr, listen, flags); |
| 851 | if (!sock) { |
| 852 | return BT_STATUS_NOMEM; |
| 853 | } |
| 854 | |
| 855 | sock->fixed_chan = fixed_chan; |
| 856 | sock->channel = channel; |
| 857 | sock->app_uid = app_uid; |
| 858 | sock->is_le_coc = is_le_coc; |
| 859 | |
Jakub Pawlowski | 8ba5432 | 2017-10-30 12:38:30 -0700 | [diff] [blame] | 860 | /* Setup ETM settings: mtu will be set below */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 861 | memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO)); |
| 862 | |
| 863 | cfg.fcr_present = true; |
| 864 | cfg.fcr = obex_l2c_fcr_opts_def; |
| 865 | |
| 866 | /* "role" is never initialized in rfcomm code */ |
| 867 | if (listen) { |
Jakub Pawlowski | e5e2dd5 | 2018-01-30 21:46:08 -0800 | [diff] [blame^] | 868 | btsock_l2cap_server_listen(sock); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 869 | } else { |
| 870 | if (fixed_chan) { |
Jakub Pawlowski | 8ba5432 | 2017-10-30 12:38:30 -0700 | [diff] [blame] | 871 | BTA_JvL2capConnectLE(sock->security, 0, NULL, channel, L2CAP_DEFAULT_MTU, |
| 872 | NULL, sock->addr, btsock_l2cap_cbk, sock->id); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 873 | } else { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 874 | if (sock->is_le_coc) { |
Jakub Pawlowski | 8ba5432 | 2017-10-30 12:38:30 -0700 | [diff] [blame] | 875 | BTA_JvL2capConnect(BTA_JV_CONN_TYPE_L2CAP_LE, sock->security, 0, NULL, |
| 876 | channel, L2CAP_MAX_SDU_LENGTH, &cfg, sock->addr, |
| 877 | btsock_l2cap_cbk, sock->id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 878 | } else { |
Jakub Pawlowski | 8ba5432 | 2017-10-30 12:38:30 -0700 | [diff] [blame] | 879 | BTA_JvL2capConnect(BTA_JV_CONN_TYPE_L2CAP, sock->security, 0, |
| 880 | &obex_l2c_etm_opt, channel, L2CAP_MAX_SDU_LENGTH, |
| 881 | &cfg, sock->addr, btsock_l2cap_cbk, sock->id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 882 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 883 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Jakub Pawlowski | 8ba5432 | 2017-10-30 12:38:30 -0700 | [diff] [blame] | 886 | *sock_fd = sock->app_fd; |
| 887 | /* We pass the FD to JAVA, but since it runs in another process, we need to |
| 888 | * also close it in native, either straight away, as done when accepting an |
| 889 | * incoming connection, or when doing cleanup after this socket */ |
| 890 | sock->app_fd = -1; |
| 891 | /*This leaks the file descriptor. The FD should be closed in JAVA but it |
| 892 | * apparently do not work */ |
| 893 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, |
| 894 | SOCK_THREAD_FD_EXCEPTION, sock->id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 895 | |
Jakub Pawlowski | 8ba5432 | 2017-10-30 12:38:30 -0700 | [diff] [blame] | 896 | return BT_STATUS_SUCCESS; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 897 | } |
| 898 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 899 | bt_status_t btsock_l2cap_listen(const char* name, int channel, int* sock_fd, |
| 900 | int flags, int app_uid) { |
| 901 | return btsock_l2cap_listen_or_connect(name, NULL, channel, sock_fd, flags, 1, |
| 902 | app_uid); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 905 | bt_status_t btsock_l2cap_connect(const RawAddress* bd_addr, int channel, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 906 | int* sock_fd, int flags, int app_uid) { |
| 907 | return btsock_l2cap_listen_or_connect(NULL, bd_addr, channel, sock_fd, flags, |
| 908 | 0, app_uid); |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 911 | /* return true if we have more to send and should wait for user readiness, false |
| 912 | * else |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 913 | * (for example: unrecoverable error or no data) |
| 914 | */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 915 | static bool flush_incoming_que_on_wr_signal_l(l2cap_socket* sock) { |
| 916 | uint8_t* buf; |
| 917 | uint32_t len; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 918 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 919 | while (packet_get_head_l(sock, &buf, &len)) { |
| 920 | ssize_t sent; |
| 921 | OSI_NO_INTR(sent = send(sock->our_fd, buf, len, MSG_DONTWAIT)); |
| 922 | int saved_errno = errno; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 923 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 924 | if (sent == (signed)len) |
| 925 | osi_free(buf); |
| 926 | else if (sent >= 0) { |
| 927 | packet_put_head_l(sock, buf + sent, len - sent); |
| 928 | osi_free(buf); |
| 929 | if (!sent) /* special case if other end not keeping up */ |
| 930 | return true; |
| 931 | } else { |
| 932 | packet_put_head_l(sock, buf, len); |
| 933 | osi_free(buf); |
| 934 | return saved_errno == EWOULDBLOCK || saved_errno == EAGAIN; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 935 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 936 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 937 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 938 | return false; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Jakub Pawlowski | 8451931 | 2018-01-12 06:22:43 -0800 | [diff] [blame] | 941 | inline BT_HDR* malloc_l2cap_buf(uint16_t len) { |
| 942 | // We need FCS only for L2CAP_FCR_ERTM_MODE, but it's just 2 bytes so it's ok |
| 943 | BT_HDR* msg = (BT_HDR*)osi_malloc(BT_HDR_SIZE + L2CAP_MIN_OFFSET + len + |
| 944 | L2CAP_FCS_LENGTH); |
| 945 | msg->offset = L2CAP_MIN_OFFSET; |
| 946 | msg->len = len; |
| 947 | return msg; |
| 948 | } |
| 949 | |
| 950 | inline uint8_t* get_l2cap_sdu_start_ptr(BT_HDR* msg) { |
| 951 | return (uint8_t*)(msg) + BT_HDR_SIZE + msg->offset; |
| 952 | } |
| 953 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 954 | void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 955 | char drop_it = false; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 956 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 957 | /* We use MSG_DONTWAIT when sending data to JAVA, hence it can be accepted to |
| 958 | * hold the lock. */ |
| 959 | std::unique_lock<std::mutex> lock(state_lock); |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 960 | l2cap_socket* sock = btsock_l2cap_find_by_id_l(user_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 961 | if (!sock) return; |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 962 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 963 | if ((flags & SOCK_THREAD_FD_RD) && !sock->server) { |
| 964 | // app sending data |
| 965 | if (sock->connected) { |
| 966 | int size = 0; |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 967 | bool ioctl_success = ioctl(sock->our_fd, FIONREAD, &size) == 0; |
| 968 | if (!(flags & SOCK_THREAD_FD_EXCEPTION) || (ioctl_success && size)) { |
| 969 | /* FIONREAD return number of bytes that are immediately available for |
| 970 | reading, might be bigger than awaiting packet. |
Arman Uguray | bb95452 | 2015-06-02 21:11:07 -0700 | [diff] [blame] | 971 | |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 972 | BluetoothSocket.write(...) guarantees that any packet send to this |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 973 | socket is broken into pieces no bigger than MTU bytes (as requested |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 974 | by BT spec). */ |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 975 | size = std::min(size, (int)sock->mtu); |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 976 | |
Jakub Pawlowski | 8451931 | 2018-01-12 06:22:43 -0800 | [diff] [blame] | 977 | BT_HDR* buffer = malloc_l2cap_buf(size); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 978 | /* The socket is created with SOCK_SEQPACKET, hence we read one message |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 979 | * at the time. */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 980 | ssize_t count; |
Jakub Pawlowski | 8451931 | 2018-01-12 06:22:43 -0800 | [diff] [blame] | 981 | OSI_NO_INTR(count = recv(fd, get_l2cap_sdu_start_ptr(buffer), size, |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 982 | MSG_NOSIGNAL | MSG_DONTWAIT | MSG_TRUNC)); |
Jakub Pawlowski | 408b5f9 | 2018-01-30 19:45:35 -0800 | [diff] [blame] | 983 | if (count > sock->mtu) { |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 984 | /* This can't happen thanks to check in BluetoothSocket.java but leave |
| 985 | * this in case this socket is ever used anywhere else*/ |
Jakub Pawlowski | 6ee309d | 2018-01-30 19:39:52 -0800 | [diff] [blame] | 986 | LOG(ERROR) << "recv more than MTU. Data will be lost: " << count; |
Jakub Pawlowski | 408b5f9 | 2018-01-30 19:45:35 -0800 | [diff] [blame] | 987 | count = sock->mtu; |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Stanley Tng | 4492288 | 2018-01-25 11:40:35 -0800 | [diff] [blame] | 990 | /* When multiple packets smaller than MTU are flushed to the socket, the |
| 991 | size of the single packet read could be smaller than the ioctl |
| 992 | reported total size of awaiting packets. Hence, we adjust the buffer |
| 993 | length. */ |
| 994 | buffer->len = count; |
Jakub Pawlowski | 8452b05 | 2018-01-05 02:41:36 -0800 | [diff] [blame] | 995 | DVLOG(2) << __func__ << ": bytes received from socket: " << count; |
Marie Janssen | a576468 | 2016-10-10 13:38:30 -0700 | [diff] [blame] | 996 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 997 | if (sock->fixed_chan) { |
Jakub Pawlowski | c474342 | 2018-01-11 07:34:39 -0800 | [diff] [blame] | 998 | // will take care of freeing buffer |
| 999 | BTA_JvL2capWriteFixed(sock->channel, sock->addr, PTR_TO_UINT(buffer), |
Jakub Pawlowski | 8451931 | 2018-01-12 06:22:43 -0800 | [diff] [blame] | 1000 | btsock_l2cap_cbk, buffer, user_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1001 | } else { |
Jakub Pawlowski | c474342 | 2018-01-11 07:34:39 -0800 | [diff] [blame] | 1002 | // will take care of freeing buffer |
Jakub Pawlowski | 8451931 | 2018-01-12 06:22:43 -0800 | [diff] [blame] | 1003 | BTA_JvL2capWrite(sock->handle, PTR_TO_UINT(buffer), buffer, user_id); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } else |
| 1007 | drop_it = true; |
| 1008 | } |
| 1009 | if (flags & SOCK_THREAD_FD_WR) { |
| 1010 | // app is ready to receive more data, tell stack to enable the data flow |
| 1011 | if (flush_incoming_que_on_wr_signal_l(sock) && sock->connected) |
| 1012 | btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, |
| 1013 | sock->id); |
| 1014 | } |
| 1015 | if (drop_it || (flags & SOCK_THREAD_FD_EXCEPTION)) { |
| 1016 | int size = 0; |
| 1017 | if (drop_it || ioctl(sock->our_fd, FIONREAD, &size) != 0 || size == 0) |
| 1018 | btsock_l2cap_free_l(sock); |
| 1019 | } |
Kim Schulz | 8372aa5 | 2015-03-25 10:39:40 +0100 | [diff] [blame] | 1020 | } |