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