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