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 <bluetooth/log.h>
25 #include <com_android_bluetooth_flags.h>
26 #include <hardware/bluetooth.h>
27 #include <hardware/bt_sock.h>
28
29 #include <atomic>
30
31 #include "bta/include/bta_api.h"
32 #include "btif_sock_hal.h"
33 #include "btif_sock_l2cap.h"
34 #include "btif_sock_logging.h"
35 #include "btif_sock_rfc.h"
36 #include "btif_sock_sco.h"
37 #include "btif_sock_thread.h"
38 #include "btif_uid.h"
39 #include "osi/include/osi.h" // INVALID_FD
40 #include "osi/include/thread.h"
41 #include "types/bluetooth/uuid.h"
42 #include "types/raw_address.h"
43
44 using bluetooth::Uuid;
45 using namespace bluetooth;
46
47 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, const Uuid* uuid,
48 int channel, int* sock_fd, int flags, int app_uid,
49 btsock_data_path_t data_path, const char* socket_name,
50 uint64_t hub_id, uint64_t endpoint_id, int max_rx_packet_size);
51 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type, const Uuid* uuid,
52 int channel, int* sock_fd, int flags, int app_uid,
53 btsock_data_path_t data_path, const char* socket_name,
54 uint64_t hub_id, uint64_t endpoint_id, int max_rx_packet_size);
55 static void btsock_request_max_tx_data_length(const RawAddress& bd_addr);
56 static bt_status_t btsock_control_req(uint8_t dlci, const RawAddress& bd_addr, uint8_t modem_signal,
57 uint8_t break_signal, uint8_t discard_buffers,
58 uint8_t break_signal_seq, bool fc);
59
60 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id);
61 static bt_status_t btsock_disconnect_all(const RawAddress* bd_addr);
62 static bt_status_t btsock_get_l2cap_local_cid(Uuid& conn_uuid, uint16_t* cid);
63 static bt_status_t btsock_get_l2cap_remote_cid(Uuid& conn_uuid, uint16_t* cid);
64
65 static std::atomic_int thread_handle{-1};
66 static thread_t* thread;
67
btif_sock_get_interface(void)68 const btsock_interface_t* btif_sock_get_interface(void) {
69 static btsock_interface_t interface = {
70 sizeof(interface),
71 btsock_listen,
72 btsock_connect,
73 btsock_request_max_tx_data_length,
74 btsock_control_req,
75 btsock_disconnect_all,
76 btsock_get_l2cap_local_cid,
77 btsock_get_l2cap_remote_cid,
78 };
79
80 return &interface;
81 }
82
btif_sock_init(uid_set_t * uid_set)83 bt_status_t btif_sock_init(uid_set_t* uid_set) {
84 log::assert_that(thread_handle == -1, "assert failed: thread_handle == -1");
85 log::assert_that(thread == NULL, "assert failed: thread == NULL");
86
87 bt_status_t status;
88 btsock_thread_init();
89 thread_handle = btsock_thread_create(btsock_signaled, NULL);
90 if (thread_handle == -1) {
91 log::error("unable to create btsock_thread.");
92 goto error;
93 }
94
95 status = btsock_rfc_init(thread_handle, uid_set);
96 if (status != BT_STATUS_SUCCESS) {
97 log::error("error initializing RFCOMM sockets: {}", status);
98 goto error;
99 }
100
101 status = btsock_l2cap_init(thread_handle, uid_set);
102 if (status != BT_STATUS_SUCCESS) {
103 log::error("error initializing L2CAP sockets: {}", status);
104 goto error;
105 }
106
107 thread = thread_new("btif_sock");
108 if (!thread) {
109 log::error("error creating new thread.");
110 btsock_rfc_cleanup();
111 goto error;
112 }
113
114 status = btsock_sco_init(thread);
115 if (status != BT_STATUS_SUCCESS) {
116 log::error("error initializing SCO sockets: {}", status);
117 btsock_rfc_cleanup();
118 goto error;
119 }
120
121 if (com::android::bluetooth::flags::socket_settings_api()) {
122 status = btsock_hal_init();
123 if (status != BT_STATUS_SUCCESS) {
124 log::warn("error initializing socket hal: {}", status);
125 }
126 }
127
128 return BT_STATUS_SUCCESS;
129
130 error:
131 thread_free(thread);
132 thread = NULL;
133 if (thread_handle != -1) {
134 btsock_thread_exit(thread_handle);
135 }
136 thread_handle = -1;
137 uid_set = NULL;
138 return BT_STATUS_SOCKET_ERROR;
139 }
140
btif_sock_cleanup(void)141 void btif_sock_cleanup(void) {
142 int saved_handle = thread_handle;
143 if (std::atomic_exchange(&thread_handle, -1) == -1) {
144 return;
145 }
146
147 btsock_thread_exit(saved_handle);
148 btsock_rfc_cleanup();
149 btsock_sco_cleanup();
150 btsock_l2cap_cleanup();
151 thread_free(thread);
152 thread = NULL;
153 }
154
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)155 static bt_status_t btsock_control_req(uint8_t dlci, const RawAddress& bd_addr, uint8_t modem_signal,
156 uint8_t break_signal, uint8_t discard_buffers,
157 uint8_t break_signal_seq, bool fc) {
158 return btsock_rfc_control_req(dlci, bd_addr, modem_signal, break_signal, discard_buffers,
159 break_signal_seq, fc);
160 }
161
btsock_listen(btsock_type_t type,const char * service_name,const Uuid * service_uuid,int channel,int * sock_fd,int flags,int app_uid,btsock_data_path_t data_path,const char * socket_name,uint64_t hub_id,uint64_t endpoint_id,int max_rx_packet_size)162 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
163 const Uuid* service_uuid, int channel, int* sock_fd, int flags,
164 int app_uid, btsock_data_path_t data_path, const char* socket_name,
165 uint64_t hub_id, uint64_t endpoint_id, int max_rx_packet_size) {
166 if ((flags & BTSOCK_FLAG_NO_SDP) == 0) {
167 log::assert_that(sock_fd != NULL, "assert failed: sock_fd != NULL");
168 }
169
170 *sock_fd = INVALID_FD;
171 bt_status_t status = BT_STATUS_SOCKET_ERROR;
172
173 log::info(
174 "Attempting listen for socket connections for device: {}, type: {}, "
175 "channel: {}, app_uid: {}, data_path: {}, hub_id: {}, endpoint_id: {}, "
176 "max_rx_packet_size: {}",
177 RawAddress::kEmpty, type, channel, app_uid, data_path, hub_id, endpoint_id,
178 max_rx_packet_size);
179 btif_sock_connection_logger(RawAddress::kEmpty, 0, type, SOCKET_CONNECTION_STATE_LISTENING,
180 SOCKET_ROLE_LISTEN, app_uid, channel, 0, 0, service_name, 0,
181 BTSOCK_ERROR_NONE, data_path);
182 switch (type) {
183 case BTSOCK_RFCOMM:
184 status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd, flags, app_uid,
185 data_path, socket_name, hub_id, endpoint_id, max_rx_packet_size);
186 break;
187 case BTSOCK_L2CAP:
188 status = btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid, data_path,
189 socket_name, hub_id, endpoint_id, max_rx_packet_size);
190 break;
191 case BTSOCK_L2CAP_LE:
192 status = btsock_l2cap_listen(service_name, channel, sock_fd, flags | BTSOCK_FLAG_LE_COC,
193 app_uid, data_path, socket_name, hub_id, endpoint_id,
194 max_rx_packet_size);
195 break;
196 case BTSOCK_SCO:
197 status = btsock_sco_listen(sock_fd, flags);
198 break;
199
200 default:
201 log::error("unknown/unsupported socket type: {}", type);
202 status = BT_STATUS_UNSUPPORTED;
203 break;
204 }
205 if (status != BT_STATUS_SUCCESS) {
206 log::error(
207 "failed to listen for socket connections for device: {}, type: {}, "
208 "channel: {}, app_uid: {}",
209 RawAddress::kEmpty, type, channel, app_uid);
210 btif_sock_connection_logger(RawAddress::kEmpty, 0, type, SOCKET_CONNECTION_STATE_DISCONNECTED,
211 SOCKET_ROLE_LISTEN, app_uid, channel, 0, 0, service_name, 0,
212 BTSOCK_ERROR_LISTEN_FAILURE, data_path);
213 }
214 return status;
215 }
216
btsock_connect(const RawAddress * bd_addr,btsock_type_t type,const Uuid * uuid,int channel,int * sock_fd,int flags,int app_uid,btsock_data_path_t data_path,const char * socket_name,uint64_t hub_id,uint64_t endpoint_id,int max_rx_packet_size)217 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type, const Uuid* uuid,
218 int channel, int* sock_fd, int flags, int app_uid,
219 btsock_data_path_t data_path, const char* socket_name,
220 uint64_t hub_id, uint64_t endpoint_id, int max_rx_packet_size) {
221 log::assert_that(bd_addr != NULL, "assert failed: bd_addr != NULL");
222 log::assert_that(sock_fd != NULL, "assert failed: sock_fd != NULL");
223
224 log::info(
225 "Attempting socket connection for device: {}, type: {}, channel: {}, "
226 "app_uid: {}, data_path: {}, hub_id: {}, endpoint_id: {}, max_rx_packet_size: {}",
227 *bd_addr, type, channel, app_uid, data_path, hub_id, endpoint_id, max_rx_packet_size);
228
229 *sock_fd = INVALID_FD;
230 bt_status_t status = BT_STATUS_SOCKET_ERROR;
231
232 btif_sock_connection_logger(
233 *bd_addr, 0, type, SOCKET_CONNECTION_STATE_CONNECTING, SOCKET_ROLE_CONNECTION, app_uid,
234 channel, 0, 0, uuid ? uuid->ToString().c_str() : "", 0, BTSOCK_ERROR_NONE, data_path);
235 switch (type) {
236 case BTSOCK_RFCOMM:
237 status = btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid, data_path,
238 socket_name, hub_id, endpoint_id, max_rx_packet_size);
239 break;
240
241 case BTSOCK_L2CAP:
242 status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid, data_path,
243 socket_name, hub_id, endpoint_id, max_rx_packet_size);
244 break;
245 case BTSOCK_L2CAP_LE:
246 status =
247 btsock_l2cap_connect(bd_addr, channel, sock_fd, (flags | BTSOCK_FLAG_LE_COC), app_uid,
248 data_path, socket_name, hub_id, endpoint_id, max_rx_packet_size);
249 break;
250 case BTSOCK_SCO:
251 status = btsock_sco_connect(bd_addr, sock_fd, flags);
252 break;
253
254 default:
255 log::error("unknown/unsupported socket type: {}", type);
256 status = BT_STATUS_UNSUPPORTED;
257 break;
258 }
259 if (status != BT_STATUS_SUCCESS) {
260 log::error(
261 "Socket connection failed for device: {}, type: {}, channel: {}, "
262 "app_uid: {}",
263 *bd_addr, type, channel, app_uid);
264 btif_sock_connection_logger(*bd_addr, 0, type, SOCKET_CONNECTION_STATE_DISCONNECTED,
265 SOCKET_ROLE_CONNECTION, app_uid, channel, 0, 0,
266 uuid ? uuid->ToString().c_str() : "", 0,
267 BTSOCK_ERROR_CONNECTION_FAILURE, data_path);
268 }
269 return status;
270 }
271
btsock_request_max_tx_data_length(const RawAddress & remote_device)272 static void btsock_request_max_tx_data_length(const RawAddress& remote_device) {
273 BTA_DmBleRequestMaxTxDataLength(remote_device);
274 }
275
btsock_signaled(int fd,int type,int flags,uint32_t user_id)276 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id) {
277 switch (type) {
278 case BTSOCK_RFCOMM:
279 btsock_rfc_signaled(fd, flags, user_id);
280 break;
281 case BTSOCK_L2CAP:
282 case BTSOCK_L2CAP_LE:
283 /* Note: The caller may not distinguish between BTSOCK_L2CAP and
284 * BTSOCK_L2CAP_LE correctly */
285 btsock_l2cap_signaled(fd, flags, user_id);
286 break;
287 default:
288 log::fatal("Invalid socket type! type={} fd={} flags={} user_id={}", type, fd, flags,
289 user_id);
290 break;
291 }
292 }
293
btsock_disconnect_all(const RawAddress * bd_addr)294 static bt_status_t btsock_disconnect_all(const RawAddress* bd_addr) {
295 log::assert_that(bd_addr != NULL, "assert failed: bd_addr != NULL");
296
297 bt_status_t rfc_status = btsock_rfc_disconnect(bd_addr);
298 bt_status_t l2cap_status = btsock_l2cap_disconnect(bd_addr);
299 /* SCO is disconnected via btif_hf, so is not handled here. */
300
301 log::info("rfc status: {}, l2cap status: {}", rfc_status, l2cap_status);
302
303 /* Return error status, if any. */
304 if (rfc_status == BT_STATUS_SUCCESS) {
305 return l2cap_status;
306 }
307 return rfc_status;
308 }
309
btsock_get_l2cap_local_cid(Uuid & conn_uuid,uint16_t * cid)310 static bt_status_t btsock_get_l2cap_local_cid(Uuid& conn_uuid, uint16_t* cid) {
311 return btsock_l2cap_get_l2cap_local_cid(conn_uuid, cid);
312 }
313
btsock_get_l2cap_remote_cid(Uuid & conn_uuid,uint16_t * cid)314 static bt_status_t btsock_get_l2cap_remote_cid(Uuid& conn_uuid, uint16_t* cid) {
315 return btsock_l2cap_get_l2cap_remote_cid(conn_uuid, cid);
316 }
317