• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(LOG_TAG, "%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(LOG_TAG, "%s error initializing RFCOMM sockets: %d", __func__,
84               status);
85     goto error;
86   }
87 
88   status = btsock_l2cap_init(thread_handle, uid_set);
89   if (status != BT_STATUS_SUCCESS) {
90     LOG_ERROR(LOG_TAG, "%s error initializing L2CAP sockets: %d", __func__,
91               status);
92     goto error;
93   }
94 
95   thread = thread_new("btif_sock");
96   if (!thread) {
97     LOG_ERROR(LOG_TAG, "%s error creating new thread.", __func__);
98     btsock_rfc_cleanup();
99     goto error;
100   }
101 
102   status = btsock_sco_init(thread);
103   if (status != BT_STATUS_SUCCESS) {
104     LOG_ERROR(LOG_TAG, "%s error initializing SCO sockets: %d", __func__,
105               status);
106     btsock_rfc_cleanup();
107     goto error;
108   }
109 
110   return BT_STATUS_SUCCESS;
111 
112 error:;
113   thread_free(thread);
114   thread = NULL;
115   if (thread_handle != -1) btsock_thread_exit(thread_handle);
116   thread_handle = -1;
117   uid_set = NULL;
118   return BT_STATUS_FAIL;
119 }
120 
btif_sock_cleanup(void)121 void btif_sock_cleanup(void) {
122   int saved_handle = thread_handle;
123   if (std::atomic_exchange(&thread_handle, -1) == -1) return;
124 
125   btsock_thread_exit(saved_handle);
126   btsock_rfc_cleanup();
127   btsock_sco_cleanup();
128   btsock_l2cap_cleanup();
129   thread_free(thread);
130   thread = NULL;
131 }
132 
btsock_listen(btsock_type_t type,const char * service_name,const Uuid * service_uuid,int channel,int * sock_fd,int flags,int app_uid)133 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
134                                  const Uuid* service_uuid, int channel,
135                                  int* sock_fd, int flags, int app_uid) {
136   if ((flags & BTSOCK_FLAG_NO_SDP) == 0) {
137     CHECK(sock_fd != NULL);
138   }
139 
140   *sock_fd = INVALID_FD;
141   bt_status_t status = BT_STATUS_FAIL;
142   int original_channel = channel;
143 
144   bluetooth::common::LogSocketConnectionState(
145       RawAddress::kEmpty, 0, type,
146       android::bluetooth::SocketConnectionstateEnum::
147           SOCKET_CONNECTION_STATE_LISTENING,
148       0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_LISTEN);
149   switch (type) {
150     case BTSOCK_RFCOMM:
151       status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd,
152                                  flags, app_uid);
153       break;
154     case BTSOCK_L2CAP:
155       status =
156           btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
157       break;
158     case BTSOCK_L2CAP_LE:
159       if (flags & BTSOCK_FLAG_NO_SDP) {
160         /* Set channel to zero so that it will be assigned */
161         channel = 0;
162       } else if (channel <= 0) {
163         LOG_ERROR(LOG_TAG, "%s: type BTSOCK_L2CAP_LE: invalid channel=%d",
164                   __func__, channel);
165         break;
166       }
167       flags |= BTSOCK_FLAG_LE_COC;
168       LOG_DEBUG(
169           LOG_TAG,
170           "%s: type=BTSOCK_L2CAP_LE, channel=0x%x, original=0x%x, flags=0x%x",
171           __func__, channel, original_channel, flags);
172       status =
173           btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
174       break;
175     case BTSOCK_SCO:
176       status = btsock_sco_listen(sock_fd, flags);
177       break;
178 
179     default:
180       LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__,
181                 type);
182       status = BT_STATUS_UNSUPPORTED;
183       break;
184   }
185   if (status != BT_STATUS_SUCCESS) {
186     bluetooth::common::LogSocketConnectionState(
187         RawAddress::kEmpty, 0, type,
188         android::bluetooth::SocketConnectionstateEnum::
189             SOCKET_CONNECTION_STATE_DISCONNECTED,
190         0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_LISTEN);
191   }
192   return status;
193 }
194 
btsock_connect(const RawAddress * bd_addr,btsock_type_t type,const Uuid * uuid,int channel,int * sock_fd,int flags,int app_uid)195 static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type,
196                                   const Uuid* uuid, int channel, int* sock_fd,
197                                   int flags, int app_uid) {
198   CHECK(bd_addr != NULL);
199   CHECK(sock_fd != NULL);
200 
201   *sock_fd = INVALID_FD;
202   bt_status_t status = BT_STATUS_FAIL;
203 
204   bluetooth::common::LogSocketConnectionState(
205       *bd_addr, 0, type,
206       android::bluetooth::SocketConnectionstateEnum::
207           SOCKET_CONNECTION_STATE_CONNECTING,
208       0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_CONNECTION);
209   switch (type) {
210     case BTSOCK_RFCOMM:
211       status =
212           btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid);
213       break;
214 
215     case BTSOCK_L2CAP:
216       status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
217       break;
218 
219     case BTSOCK_L2CAP_LE: {
220       flags |= BTSOCK_FLAG_LE_COC;
221 
222       // Ensure device is in inquiry database
223       int addr_type = 0;
224       int device_type = 0;
225 
226       if (btif_get_address_type(*bd_addr, &addr_type) &&
227           btif_get_device_type(*bd_addr, &device_type) &&
228           device_type != BT_DEVICE_TYPE_BREDR) {
229         BTA_DmAddBleDevice(*bd_addr, addr_type, device_type);
230       }
231 
232       LOG_DEBUG(LOG_TAG, "%s: type=BTSOCK_L2CAP_LE, channel=0x%x, flags=0x%x",
233                 __func__, channel, flags);
234       status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
235       break;
236     }
237 
238     case BTSOCK_SCO:
239       status = btsock_sco_connect(bd_addr, sock_fd, flags);
240       break;
241 
242     default:
243       LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__,
244                 type);
245       status = BT_STATUS_UNSUPPORTED;
246       break;
247   }
248   if (status != BT_STATUS_SUCCESS) {
249     bluetooth::common::LogSocketConnectionState(
250         *bd_addr, 0, type,
251         android::bluetooth::SocketConnectionstateEnum::
252             SOCKET_CONNECTION_STATE_DISCONNECTED,
253         0, 0, app_uid, channel, android::bluetooth::SOCKET_ROLE_CONNECTION);
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   const controller_t* controller = controller_get_interface();
260   uint16_t max_len = controller->get_ble_maximum_tx_data_length();
261 
262   DVLOG(2) << __func__ << ": max_len=" << max_len;
263 
264   BTA_DmBleSetDataLength(remote_device, max_len);
265 }
266 
btsock_signaled(int fd,int type,int flags,uint32_t user_id)267 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id) {
268   switch (type) {
269     case BTSOCK_RFCOMM:
270       btsock_rfc_signaled(fd, flags, user_id);
271       break;
272     case BTSOCK_L2CAP:
273     case BTSOCK_L2CAP_LE:
274       /* Note: The caller may not distinguish between BTSOCK_L2CAP and
275        * BTSOCK_L2CAP_LE correctly */
276       btsock_l2cap_signaled(fd, flags, user_id);
277       break;
278     default:
279       LOG(FATAL) << "Invalid socket type! type=" << type << " fd=" << fd
280                  << " flags=" << flags << " user_id=" << user_id;
281       break;
282   }
283 }
284