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);
181   switch (type) {
182     case BTSOCK_RFCOMM:
183       status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd, flags, app_uid);
184       break;
185     case BTSOCK_L2CAP:
186       status = btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid, data_path,
187                                    socket_name, hub_id, endpoint_id, max_rx_packet_size);
188       break;
189     case BTSOCK_L2CAP_LE:
190       status = btsock_l2cap_listen(service_name, channel, sock_fd, flags | BTSOCK_FLAG_LE_COC,
191                                    app_uid, data_path, socket_name, hub_id, endpoint_id,
192                                    max_rx_packet_size);
193       break;
194     case BTSOCK_SCO:
195       status = btsock_sco_listen(sock_fd, flags);
196       break;
197 
198     default:
199       log::error("unknown/unsupported socket type: {}", type);
200       status = BT_STATUS_UNSUPPORTED;
201       break;
202   }
203   if (status != BT_STATUS_SUCCESS) {
204     log::error(
205             "failed to listen for socket connections for device: {}, type: {}, "
206             "channel: {}, app_uid: {}",
207             RawAddress::kEmpty, type, channel, app_uid);
208     btif_sock_connection_logger(RawAddress::kEmpty, 0, type, SOCKET_CONNECTION_STATE_DISCONNECTED,
209                                 SOCKET_ROLE_LISTEN, app_uid, channel, 0, 0, service_name);
210   }
211   return status;
212 }
213 
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)214 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type, const Uuid* uuid,
215                                   int channel, int* sock_fd, int flags, int app_uid,
216                                   btsock_data_path_t data_path, const char* socket_name,
217                                   uint64_t hub_id, uint64_t endpoint_id, int max_rx_packet_size) {
218   log::assert_that(bd_addr != NULL, "assert failed: bd_addr != NULL");
219   log::assert_that(sock_fd != NULL, "assert failed: sock_fd != NULL");
220 
221   log::info(
222           "Attempting socket connection for device: {}, type: {}, channel: {}, "
223           "app_uid: {}, data_path: {}, hub_id: {}, endpoint_id: {}, max_rx_packet_size: {}",
224           *bd_addr, type, channel, app_uid, data_path, hub_id, endpoint_id, max_rx_packet_size);
225 
226   *sock_fd = INVALID_FD;
227   bt_status_t status = BT_STATUS_SOCKET_ERROR;
228 
229   btif_sock_connection_logger(*bd_addr, 0, type, SOCKET_CONNECTION_STATE_CONNECTING,
230                               SOCKET_ROLE_CONNECTION, app_uid, channel, 0, 0,
231                               uuid ? uuid->ToString().c_str() : "");
232   switch (type) {
233     case BTSOCK_RFCOMM:
234       status = btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid);
235       break;
236 
237     case BTSOCK_L2CAP:
238       status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid, data_path,
239                                     socket_name, hub_id, endpoint_id, max_rx_packet_size);
240       break;
241     case BTSOCK_L2CAP_LE:
242       status =
243               btsock_l2cap_connect(bd_addr, channel, sock_fd, (flags | BTSOCK_FLAG_LE_COC), app_uid,
244                                    data_path, socket_name, hub_id, endpoint_id, max_rx_packet_size);
245       break;
246     case BTSOCK_SCO:
247       status = btsock_sco_connect(bd_addr, sock_fd, flags);
248       break;
249 
250     default:
251       log::error("unknown/unsupported socket type: {}", type);
252       status = BT_STATUS_UNSUPPORTED;
253       break;
254   }
255   if (status != BT_STATUS_SUCCESS) {
256     log::error(
257             "Socket connection failed for device: {}, type: {}, channel: {}, "
258             "app_uid: {}",
259             *bd_addr, type, channel, app_uid);
260     btif_sock_connection_logger(*bd_addr, 0, type, SOCKET_CONNECTION_STATE_DISCONNECTED,
261                                 SOCKET_ROLE_CONNECTION, app_uid, channel, 0, 0,
262                                 uuid ? uuid->ToString().c_str() : "");
263   }
264   return status;
265 }
266 
btsock_request_max_tx_data_length(const RawAddress & remote_device)267 static void btsock_request_max_tx_data_length(const RawAddress& remote_device) {
268   BTA_DmBleRequestMaxTxDataLength(remote_device);
269 }
270 
btsock_signaled(int fd,int type,int flags,uint32_t user_id)271 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id) {
272   switch (type) {
273     case BTSOCK_RFCOMM:
274       btsock_rfc_signaled(fd, flags, user_id);
275       break;
276     case BTSOCK_L2CAP:
277     case BTSOCK_L2CAP_LE:
278       /* Note: The caller may not distinguish between BTSOCK_L2CAP and
279        * BTSOCK_L2CAP_LE correctly */
280       btsock_l2cap_signaled(fd, flags, user_id);
281       break;
282     default:
283       log::fatal("Invalid socket type! type={} fd={} flags={} user_id={}", type, fd, flags,
284                  user_id);
285       break;
286   }
287 }
288 
btsock_disconnect_all(const RawAddress * bd_addr)289 static bt_status_t btsock_disconnect_all(const RawAddress* bd_addr) {
290   log::assert_that(bd_addr != NULL, "assert failed: bd_addr != NULL");
291 
292   bt_status_t rfc_status = btsock_rfc_disconnect(bd_addr);
293   bt_status_t l2cap_status = btsock_l2cap_disconnect(bd_addr);
294   /* SCO is disconnected via btif_hf, so is not handled here. */
295 
296   log::info("rfc status: {}, l2cap status: {}", rfc_status, l2cap_status);
297 
298   /* Return error status, if any. */
299   if (rfc_status == BT_STATUS_SUCCESS) {
300     return l2cap_status;
301   }
302   return rfc_status;
303 }
304 
btsock_get_l2cap_local_cid(Uuid & conn_uuid,uint16_t * cid)305 static bt_status_t btsock_get_l2cap_local_cid(Uuid& conn_uuid, uint16_t* cid) {
306   return btsock_l2cap_get_l2cap_local_cid(conn_uuid, cid);
307 }
308 
btsock_get_l2cap_remote_cid(Uuid & conn_uuid,uint16_t * cid)309 static bt_status_t btsock_get_l2cap_remote_cid(Uuid& conn_uuid, uint16_t* cid) {
310   return btsock_l2cap_get_l2cap_remote_cid(conn_uuid, cid);
311 }
312