1 /******************************************************************************
2 *
3 * Copyright 2014 Google, Inc.
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/include/btif_sock.h"
22
23 #include <base/functional/callback.h>
24 #include <base/logging.h>
25 #include <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
26 #include <hardware/bluetooth.h>
27 #include <hardware/bt_sock.h>
28 #include <time.h>
29
30 #include <atomic>
31
32 #include "bta_api.h"
33 #include "btif_common.h"
34 #include "btif_config.h"
35 #include "btif_metrics_logging.h"
36 #include "btif_sock_l2cap.h"
37 #include "btif_sock_rfc.h"
38 #include "btif_sock_sco.h"
39 #include "btif_sock_sdp.h"
40 #include "btif_sock_thread.h"
41 #include "btif_uid.h"
42 #include "btif_util.h"
43 #include "osi/include/thread.h"
44 #include "types/bluetooth/uuid.h"
45 #include "types/raw_address.h"
46
47 bool btif_get_address_type(const RawAddress& bda, tBLE_ADDR_TYPE* p_addr_type);
48 bool btif_get_device_type(const RawAddress& bda, int* p_device_type);
49
50 using bluetooth::Uuid;
51
52 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
53 const Uuid* uuid, int channel, int* sock_fd,
54 int flags, int app_uid);
55 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type,
56 const Uuid* uuid, int channel, int* sock_fd,
57 int flags, int app_uid);
58
59 static void btsock_request_max_tx_data_length(const RawAddress& bd_addr);
60 static bt_status_t btsock_control_req(uint8_t dlci, const RawAddress& bd_addr,
61 uint8_t modem_signal,
62 uint8_t break_signal,
63 uint8_t discard_buffers,
64 uint8_t break_signal_seq, bool fc);
65
66 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id);
67
68 static std::atomic_int thread_handle{-1};
69 static thread_t* thread;
70
71 #define SOCK_LOGGER_SIZE_MAX 16
72
73 struct SockConnectionEvent {
74 bool used;
75 RawAddress addr;
76 int state;
77 int role;
78 struct timespec timestamp;
79
80 void dump(const int fd);
81 };
82
83 static std::atomic<uint8_t> logger_index;
84
85 static SockConnectionEvent connection_logger[SOCK_LOGGER_SIZE_MAX];
86
btif_sock_get_interface(void)87 const btsock_interface_t* btif_sock_get_interface(void) {
88 static btsock_interface_t interface = {
89 sizeof(interface), btsock_listen, /* listen */
90 btsock_connect, /* connect */
91 btsock_request_max_tx_data_length, /* request_max_tx_data_length */
92 btsock_control_req /* send_control_req */
93 };
94
95 return &interface;
96 }
97
btif_sock_init(uid_set_t * uid_set)98 bt_status_t btif_sock_init(uid_set_t* uid_set) {
99 CHECK(thread_handle == -1);
100 CHECK(thread == NULL);
101
102 bt_status_t status;
103 btsock_thread_init();
104 thread_handle = btsock_thread_create(btsock_signaled, NULL);
105 if (thread_handle == -1) {
106 LOG_ERROR("%s unable to create btsock_thread.", __func__);
107 goto error;
108 }
109
110 status = btsock_rfc_init(thread_handle, uid_set);
111 if (status != BT_STATUS_SUCCESS) {
112 LOG_ERROR("%s error initializing RFCOMM sockets: %d", __func__, status);
113 goto error;
114 }
115
116 status = btsock_l2cap_init(thread_handle, uid_set);
117 if (status != BT_STATUS_SUCCESS) {
118 LOG_ERROR("%s error initializing L2CAP sockets: %d", __func__, status);
119 goto error;
120 }
121
122 thread = thread_new("btif_sock");
123 if (!thread) {
124 LOG_ERROR("%s error creating new thread.", __func__);
125 btsock_rfc_cleanup();
126 goto error;
127 }
128
129 status = btsock_sco_init(thread);
130 if (status != BT_STATUS_SUCCESS) {
131 LOG_ERROR("%s error initializing SCO sockets: %d", __func__, status);
132 btsock_rfc_cleanup();
133 goto error;
134 }
135
136 return BT_STATUS_SUCCESS;
137
138 error:;
139 thread_free(thread);
140 thread = NULL;
141 if (thread_handle != -1) btsock_thread_exit(thread_handle);
142 thread_handle = -1;
143 uid_set = NULL;
144 return BT_STATUS_FAIL;
145 }
146
btif_sock_cleanup(void)147 void btif_sock_cleanup(void) {
148 int saved_handle = thread_handle;
149 if (std::atomic_exchange(&thread_handle, -1) == -1) return;
150
151 btsock_thread_exit(saved_handle);
152 btsock_rfc_cleanup();
153 btsock_sco_cleanup();
154 btsock_l2cap_cleanup();
155 thread_free(thread);
156 thread = NULL;
157 }
158
btif_sock_connection_logger(int state,int role,const RawAddress & addr)159 void btif_sock_connection_logger(int state, int role, const RawAddress& addr) {
160 LOG_INFO("address=%s, role=%d, state=%d", ADDRESS_TO_LOGGABLE_CSTR(addr),
161 state, role);
162
163 uint8_t index = logger_index++ % SOCK_LOGGER_SIZE_MAX;
164
165 connection_logger[index] = {
166 .used = true,
167 .addr = addr,
168 .state = state,
169 .role = role,
170 };
171 clock_gettime(CLOCK_REALTIME, &connection_logger[index].timestamp);
172 }
173
btif_sock_dump(int fd)174 void btif_sock_dump(int fd) {
175 dprintf(fd, "\nSocket Events: \n");
176 dprintf(fd, " Time \tAddress \tState \tRole\n");
177
178 const uint8_t head = logger_index.load() % SOCK_LOGGER_SIZE_MAX;
179
180 uint8_t index = head;
181 do {
182 connection_logger[index].dump(fd);
183
184 index++;
185 index %= SOCK_LOGGER_SIZE_MAX;
186 } while (index != head);
187 dprintf(fd, "\n");
188 }
189
dump(const int fd)190 void SockConnectionEvent::dump(const int fd) {
191 if (!used) {
192 return;
193 }
194
195 char eventtime[20];
196 char temptime[20];
197 struct tm* tstamp = localtime(×tamp.tv_sec);
198 strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
199 snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
200 timestamp.tv_nsec / 1000000);
201
202 const char* str_state;
203 switch (state) {
204 case SOCKET_CONNECTION_STATE_LISTENING:
205 str_state = "STATE_LISTENING";
206 break;
207 case SOCKET_CONNECTION_STATE_CONNECTING:
208 str_state = "STATE_CONNECTING";
209 break;
210 case SOCKET_CONNECTION_STATE_CONNECTED:
211 str_state = "STATE_CONNECTED";
212 break;
213 case SOCKET_CONNECTION_STATE_DISCONNECTING:
214 str_state = "STATE_DISCONNECTING";
215 break;
216 case SOCKET_CONNECTION_STATE_DISCONNECTED:
217 str_state = "STATE_DISCONNECTED";
218 break;
219 default:
220 str_state = "STATE_UNKNOWN";
221 break;
222 }
223
224 const char* str_role;
225 switch (role) {
226 case SOCKET_ROLE_LISTEN:
227 str_role = "ROLE_LISTEN";
228 break;
229 case SOCKET_ROLE_CONNECTION:
230 str_role = "ROLE_CONNECTION";
231 break;
232 default:
233 str_role = "ROLE_UNKNOWN";
234 break;
235 }
236
237 dprintf(fd, " %s\t%s\t%s \t%s\n", eventtime,
238 ADDRESS_TO_LOGGABLE_CSTR(addr), str_state, str_role);
239 }
240
btsock_control_req(uint8_t dlci,const RawAddress & bd_addr,uint8_t modem_signal,uint8_t break_signal,uint8_t discard_buffers,uint8_t break_signal_seq,bool fc)241 static bt_status_t btsock_control_req(uint8_t dlci, const RawAddress& bd_addr,
242 uint8_t modem_signal,
243 uint8_t break_signal,
244 uint8_t discard_buffers,
245 uint8_t break_signal_seq, bool fc) {
246 return btsock_rfc_control_req(dlci, bd_addr, modem_signal, break_signal,
247 discard_buffers, break_signal_seq, fc);
248 }
249
btsock_listen(btsock_type_t type,const char * service_name,const Uuid * service_uuid,int channel,int * sock_fd,int flags,int app_uid)250 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
251 const Uuid* service_uuid, int channel,
252 int* sock_fd, int flags, int app_uid) {
253 if ((flags & BTSOCK_FLAG_NO_SDP) == 0) {
254 CHECK(sock_fd != NULL);
255 }
256
257 *sock_fd = INVALID_FD;
258 bt_status_t status = BT_STATUS_FAIL;
259 int original_channel = channel;
260
261 btif_sock_connection_logger(SOCKET_CONNECTION_STATE_LISTENING,
262 SOCKET_ROLE_LISTEN, RawAddress::kEmpty);
263 log_socket_connection_state(RawAddress::kEmpty, 0, type,
264 android::bluetooth::SocketConnectionstateEnum::
265 SOCKET_CONNECTION_STATE_LISTENING,
266 0, 0, app_uid, channel,
267 android::bluetooth::SOCKET_ROLE_LISTEN);
268 switch (type) {
269 case BTSOCK_RFCOMM:
270 status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd,
271 flags, app_uid);
272 break;
273 case BTSOCK_L2CAP:
274 status =
275 btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
276 break;
277 case BTSOCK_L2CAP_LE:
278 if (flags & BTSOCK_FLAG_NO_SDP) {
279 /* Set channel to zero so that it will be assigned */
280 channel = 0;
281 } else if (channel <= 0) {
282 LOG_ERROR("%s: type BTSOCK_L2CAP_LE: invalid channel=%d", __func__,
283 channel);
284 break;
285 }
286 flags |= BTSOCK_FLAG_LE_COC;
287 LOG_INFO(
288
289 "%s: type=BTSOCK_L2CAP_LE, channel=0x%x, original=0x%x, flags=0x%x",
290 __func__, channel, original_channel, flags);
291 status =
292 btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
293 break;
294 case BTSOCK_SCO:
295 status = btsock_sco_listen(sock_fd, flags);
296 break;
297
298 default:
299 LOG_ERROR("%s unknown/unsupported socket type: %d", __func__, type);
300 status = BT_STATUS_UNSUPPORTED;
301 break;
302 }
303 if (status != BT_STATUS_SUCCESS) {
304 btif_sock_connection_logger(SOCKET_CONNECTION_STATE_DISCONNECTED,
305 SOCKET_ROLE_LISTEN, RawAddress::kEmpty);
306 log_socket_connection_state(RawAddress::kEmpty, 0, type,
307 android::bluetooth::SocketConnectionstateEnum::
308 SOCKET_CONNECTION_STATE_DISCONNECTED,
309 0, 0, app_uid, channel,
310 android::bluetooth::SOCKET_ROLE_LISTEN);
311 }
312 return status;
313 }
314
btsock_connect(const RawAddress * bd_addr,btsock_type_t type,const Uuid * uuid,int channel,int * sock_fd,int flags,int app_uid)315 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type,
316 const Uuid* uuid, int channel, int* sock_fd,
317 int flags, int app_uid) {
318 CHECK(bd_addr != NULL);
319 CHECK(sock_fd != NULL);
320
321 LOG_INFO("%s", __func__);
322
323 *sock_fd = INVALID_FD;
324 bt_status_t status = BT_STATUS_FAIL;
325
326 btif_sock_connection_logger(SOCKET_CONNECTION_STATE_CONNECTING,
327 SOCKET_ROLE_CONNECTION, *bd_addr);
328 log_socket_connection_state(*bd_addr, 0, type,
329 android::bluetooth::SocketConnectionstateEnum::
330 SOCKET_CONNECTION_STATE_CONNECTING,
331 0, 0, app_uid, channel,
332 android::bluetooth::SOCKET_ROLE_CONNECTION);
333 switch (type) {
334 case BTSOCK_RFCOMM:
335 status =
336 btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid);
337 break;
338
339 case BTSOCK_L2CAP:
340 status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
341 break;
342
343 case BTSOCK_L2CAP_LE: {
344 flags |= BTSOCK_FLAG_LE_COC;
345
346 // Ensure device is in inquiry database
347 tBLE_ADDR_TYPE addr_type = BLE_ADDR_PUBLIC;
348 int device_type = 0;
349
350 if (btif_get_address_type(*bd_addr, &addr_type) &&
351 btif_get_device_type(*bd_addr, &device_type) &&
352 device_type != BT_DEVICE_TYPE_BREDR) {
353 BTA_DmAddBleDevice(*bd_addr, addr_type, device_type);
354 }
355
356 LOG_INFO("%s: type=BTSOCK_L2CAP_LE, channel=0x%x, flags=0x%x", __func__,
357 channel, flags);
358 status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
359 break;
360 }
361
362 case BTSOCK_SCO:
363 status = btsock_sco_connect(bd_addr, sock_fd, flags);
364 break;
365
366 default:
367 LOG_ERROR("%s unknown/unsupported socket type: %d", __func__, type);
368 status = BT_STATUS_UNSUPPORTED;
369 break;
370 }
371 if (status != BT_STATUS_SUCCESS) {
372 btif_sock_connection_logger(SOCKET_CONNECTION_STATE_DISCONNECTED,
373 SOCKET_ROLE_CONNECTION, *bd_addr);
374 log_socket_connection_state(*bd_addr, 0, type,
375 android::bluetooth::SocketConnectionstateEnum::
376 SOCKET_CONNECTION_STATE_DISCONNECTED,
377 0, 0, app_uid, channel,
378 android::bluetooth::SOCKET_ROLE_CONNECTION);
379 }
380 return status;
381 }
382
btsock_request_max_tx_data_length(const RawAddress & remote_device)383 static void btsock_request_max_tx_data_length(const RawAddress& remote_device) {
384 BTA_DmBleRequestMaxTxDataLength(remote_device);
385 }
386
btsock_signaled(int fd,int type,int flags,uint32_t user_id)387 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id) {
388 switch (type) {
389 case BTSOCK_RFCOMM:
390 btsock_rfc_signaled(fd, flags, user_id);
391 break;
392 case BTSOCK_L2CAP:
393 case BTSOCK_L2CAP_LE:
394 /* Note: The caller may not distinguish between BTSOCK_L2CAP and
395 * BTSOCK_L2CAP_LE correctly */
396 btsock_l2cap_signaled(fd, flags, user_id);
397 break;
398 default:
399 LOG(FATAL) << "Invalid socket type! type=" << type << " fd=" << fd
400 << " flags=" << flags << " user_id=" << user_id;
401 break;
402 }
403 }
404