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 <atomic>
22 
23 #include <base/logging.h>
24 
25 #include <frameworks/base/core/proto/android/bluetooth/enums.pb.h>
26 #include <hardware/bluetooth.h>
27 #include <hardware/bt_sock.h>
28 
29 #include "bta_api.h"
30 #include "btif_common.h"
31 #include "btif_config.h"
32 #include "btif_sock_l2cap.h"
33 #include "btif_sock_rfc.h"
34 #include "btif_sock_sco.h"
35 #include "btif_sock_sdp.h"
36 #include "btif_sock_thread.h"
37 #include "btif_uid.h"
38 #include "btif_util.h"
39 #include "common/metrics.h"
40 #include "device/include/controller.h"
41 #include "osi/include/thread.h"
42 
43 using bluetooth::Uuid;
44 
45 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
46                                  const Uuid* uuid, int channel, int* sock_fd,
47                                  int flags, int app_uid);
48 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type,
49                                   const Uuid* uuid, int channel, int* sock_fd,
50                                   int flags, int app_uid);
51 
52 static void btsock_request_max_tx_data_length(const RawAddress& bd_addr);
53 
54 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id);
55 
56 static std::atomic_int thread_handle{-1};
57 static thread_t* thread;
58 
btif_sock_get_interface(void)59 const btsock_interface_t* btif_sock_get_interface(void) {
60   static btsock_interface_t interface = {
61       sizeof(interface), btsock_listen, /* listen */
62       btsock_connect,                   /* connect */
63       btsock_request_max_tx_data_length /* request_max_tx_data_length */
64   };
65 
66   return &interface;
67 }
68 
btif_sock_init(uid_set_t * uid_set)69 bt_status_t btif_sock_init(uid_set_t* uid_set) {
70   CHECK(thread_handle == -1);
71   CHECK(thread == NULL);
72 
73   bt_status_t status;
74   btsock_thread_init();
75   thread_handle = btsock_thread_create(btsock_signaled, NULL);
76   if (thread_handle == -1) {
77     LOG_ERROR("%s unable to create btsock_thread.", __func__);
78     goto error;
79   }
80 
81   status = btsock_rfc_init(thread_handle, uid_set);
82   if (status != BT_STATUS_SUCCESS) {
83     LOG_ERROR("%s error initializing RFCOMM sockets: %d", __func__, status);
84     goto error;
85   }
86 
87   status = btsock_l2cap_init(thread_handle, uid_set);
88   if (status != BT_STATUS_SUCCESS) {
89     LOG_ERROR("%s error initializing L2CAP sockets: %d", __func__, status);
90     goto error;
91   }
92 
93   thread = thread_new("btif_sock");
94   if (!thread) {
95     LOG_ERROR("%s error creating new thread.", __func__);
96     btsock_rfc_cleanup();
97     goto error;
98   }
99 
100   status = btsock_sco_init(thread);
101   if (status != BT_STATUS_SUCCESS) {
102     LOG_ERROR("%s error initializing SCO sockets: %d", __func__, status);
103     btsock_rfc_cleanup();
104     goto error;
105   }
106 
107   return BT_STATUS_SUCCESS;
108 
109 error:;
110   thread_free(thread);
111   thread = NULL;
112   if (thread_handle != -1) btsock_thread_exit(thread_handle);
113   thread_handle = -1;
114   uid_set = NULL;
115   return BT_STATUS_FAIL;
116 }
117 
btif_sock_cleanup(void)118 void btif_sock_cleanup(void) {
119   int saved_handle = thread_handle;
120   if (std::atomic_exchange(&thread_handle, -1) == -1) return;
121 
122   btsock_thread_exit(saved_handle);
123   btsock_rfc_cleanup();
124   btsock_sco_cleanup();
125   btsock_l2cap_cleanup();
126   thread_free(thread);
127   thread = NULL;
128 }
129 
btsock_listen(btsock_type_t type,const char * service_name,const Uuid * service_uuid,int channel,int * sock_fd,int flags,int app_uid)130 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
131                                  const Uuid* service_uuid, int channel,
132                                  int* sock_fd, int flags, int app_uid) {
133   if ((flags & BTSOCK_FLAG_NO_SDP) == 0) {
134     CHECK(sock_fd != NULL);
135   }
136 
137   *sock_fd = INVALID_FD;
138   bt_status_t status = BT_STATUS_FAIL;
139   int original_channel = channel;
140 
141   bluetooth::common::LogSocketConnectionState(
142       RawAddress::kEmpty, 0, type,
143       android::bluetooth::SocketConnectionstateEnum::
144           SOCKET_CONNECTION_STATE_LISTENING,
145       0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_LISTEN);
146   switch (type) {
147     case BTSOCK_RFCOMM:
148       status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd,
149                                  flags, app_uid);
150       break;
151     case BTSOCK_L2CAP:
152       status =
153           btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
154       break;
155     case BTSOCK_L2CAP_LE:
156       if (flags & BTSOCK_FLAG_NO_SDP) {
157         /* Set channel to zero so that it will be assigned */
158         channel = 0;
159       } else if (channel <= 0) {
160         LOG_ERROR("%s: type BTSOCK_L2CAP_LE: invalid channel=%d", __func__,
161                   channel);
162         break;
163       }
164       flags |= BTSOCK_FLAG_LE_COC;
165       LOG_DEBUG(
166 
167           "%s: type=BTSOCK_L2CAP_LE, channel=0x%x, original=0x%x, flags=0x%x",
168           __func__, channel, original_channel, flags);
169       status =
170           btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
171       break;
172     case BTSOCK_SCO:
173       status = btsock_sco_listen(sock_fd, flags);
174       break;
175 
176     default:
177       LOG_ERROR("%s unknown/unsupported socket type: %d", __func__, type);
178       status = BT_STATUS_UNSUPPORTED;
179       break;
180   }
181   if (status != BT_STATUS_SUCCESS) {
182     bluetooth::common::LogSocketConnectionState(
183         RawAddress::kEmpty, 0, type,
184         android::bluetooth::SocketConnectionstateEnum::
185             SOCKET_CONNECTION_STATE_DISCONNECTED,
186         0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_LISTEN);
187   }
188   return status;
189 }
190 
btsock_connect(const RawAddress * bd_addr,btsock_type_t type,const Uuid * uuid,int channel,int * sock_fd,int flags,int app_uid)191 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type,
192                                   const Uuid* uuid, int channel, int* sock_fd,
193                                   int flags, int app_uid) {
194   CHECK(bd_addr != NULL);
195   CHECK(sock_fd != NULL);
196 
197   *sock_fd = INVALID_FD;
198   bt_status_t status = BT_STATUS_FAIL;
199 
200   bluetooth::common::LogSocketConnectionState(
201       *bd_addr, 0, type,
202       android::bluetooth::SocketConnectionstateEnum::
203           SOCKET_CONNECTION_STATE_CONNECTING,
204       0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_CONNECTION);
205   switch (type) {
206     case BTSOCK_RFCOMM:
207       status =
208           btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid);
209       break;
210 
211     case BTSOCK_L2CAP:
212       status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
213       break;
214 
215     case BTSOCK_L2CAP_LE: {
216       flags |= BTSOCK_FLAG_LE_COC;
217 
218       // Ensure device is in inquiry database
219       int addr_type = 0;
220       int device_type = 0;
221 
222       if (btif_get_address_type(*bd_addr, &addr_type) &&
223           btif_get_device_type(*bd_addr, &device_type) &&
224           device_type != BT_DEVICE_TYPE_BREDR) {
225         BTA_DmAddBleDevice(*bd_addr, addr_type, device_type);
226       }
227 
228       LOG_DEBUG("%s: type=BTSOCK_L2CAP_LE, channel=0x%x, flags=0x%x", __func__,
229                 channel, flags);
230       status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
231       break;
232     }
233 
234     case BTSOCK_SCO:
235       status = btsock_sco_connect(bd_addr, sock_fd, flags);
236       break;
237 
238     default:
239       LOG_ERROR("%s unknown/unsupported socket type: %d", __func__, type);
240       status = BT_STATUS_UNSUPPORTED;
241       break;
242   }
243   if (status != BT_STATUS_SUCCESS) {
244     bluetooth::common::LogSocketConnectionState(
245         *bd_addr, 0, type,
246         android::bluetooth::SocketConnectionstateEnum::
247             SOCKET_CONNECTION_STATE_DISCONNECTED,
248         0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_CONNECTION);
249   }
250   return status;
251 }
252 
btsock_request_max_tx_data_length(const RawAddress & remote_device)253 static void btsock_request_max_tx_data_length(const RawAddress& remote_device) {
254   const controller_t* controller = controller_get_interface();
255   uint16_t max_len = controller->get_ble_maximum_tx_data_length();
256 
257   DVLOG(2) << __func__ << ": max_len=" << max_len;
258 
259   BTA_DmBleSetDataLength(remote_device, max_len);
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=" << type << " fd=" << fd
275                  << " flags=" << flags << " user_id=" << user_id;
276       break;
277   }
278 }
279