blob: 8c4c9c35f786136e1f76be3348e0962a80a62409 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2014 Google, Inc.
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
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
Marie Janssen49120dc2015-07-07 16:47:20 -070019#define LOG_TAG "bt_btif_sock"
20
Myles Watsonb793f4f2017-02-28 11:05:52 -080021#include <atomic>
22
Jack Hef2af1c42016-12-13 01:59:12 -080023#include <base/logging.h>
Marie Janssendb554582015-06-26 14:53:46 -070024
Jack He83f86832019-02-06 20:24:24 -080025#include <frameworks/base/core/proto/android/bluetooth/enums.pb.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080026#include <hardware/bluetooth.h>
27#include <hardware/bt_sock.h>
28
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#include "bta_api.h"
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070030#include "btif_common.h"
Jakub Pawlowski3118cdc2019-08-05 20:28:22 +020031#include "btif_config.h"
Marie Janssendb554582015-06-26 14:53:46 -070032#include "btif_sock_l2cap.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080033#include "btif_sock_rfc.h"
Ian McKellare93ac122013-11-07 16:30:05 -080034#include "btif_sock_sco.h"
Kim Schulz8372aa52015-03-25 10:39:40 +010035#include "btif_sock_sdp.h"
Marie Janssendb554582015-06-26 14:53:46 -070036#include "btif_sock_thread.h"
Adam Lesinski0620f972015-12-02 22:15:08 -080037#include "btif_uid.h"
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070038#include "btif_util.h"
Jack He83f86832019-02-06 20:24:24 -080039#include "common/metrics.h"
Stanley Tng70bebaf2018-01-22 17:08:03 -080040#include "device/include/controller.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070041#include "osi/include/thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080042
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070043using bluetooth::Uuid;
44
Myles Watson6bd442f2016-10-19 09:50:22 -070045static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070046 const Uuid* uuid, int channel, int* sock_fd,
Myles Watson6bd442f2016-10-19 09:50:22 -070047 int flags, int app_uid);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070048static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070049 const Uuid* uuid, int channel, int* sock_fd,
50 int flags, int app_uid);
The Android Open Source Project5738f832012-12-12 16:00:35 -080051
Stanley Tng70bebaf2018-01-22 17:08:03 -080052static void btsock_request_max_tx_data_length(const RawAddress& bd_addr);
53
The Android Open Source Project5738f832012-12-12 16:00:35 -080054static void btsock_signaled(int fd, int type, int flags, uint32_t user_id);
55
Myles Watsonb793f4f2017-02-28 11:05:52 -080056static std::atomic_int thread_handle{-1};
Myles Watson6bd442f2016-10-19 09:50:22 -070057static thread_t* thread;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070058
Jack Heff997912017-12-04 14:30:42 -080059const btsock_interface_t* btif_sock_get_interface(void) {
Stanley Tng70bebaf2018-01-22 17:08:03 -080060 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 };
Kim Schulz8372aa52015-03-25 10:39:40 +010065
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070066 return &interface;
The Android Open Source Project5738f832012-12-12 16:00:35 -080067}
68
Adam Lesinski0620f972015-12-02 22:15:08 -080069bt_status_t btif_sock_init(uid_set_t* uid_set) {
Jack Hef2af1c42016-12-13 01:59:12 -080070 CHECK(thread_handle == -1);
71 CHECK(thread == NULL);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070072
Jakub Pawlowski713993d2016-04-21 13:16:45 -070073 bt_status_t status;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070074 btsock_thread_init();
75 thread_handle = btsock_thread_create(btsock_signaled, NULL);
76 if (thread_handle == -1) {
Marie Janssendb554582015-06-26 14:53:46 -070077 LOG_ERROR(LOG_TAG, "%s unable to create btsock_thread.", __func__);
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -070078 goto error;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070079 }
80
Jakub Pawlowski713993d2016-04-21 13:16:45 -070081 status = btsock_rfc_init(thread_handle, uid_set);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070082 if (status != BT_STATUS_SUCCESS) {
Myles Watson6bd442f2016-10-19 09:50:22 -070083 LOG_ERROR(LOG_TAG, "%s error initializing RFCOMM sockets: %d", __func__,
84 status);
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -070085 goto error;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070086 }
87
Adam Lesinski0620f972015-12-02 22:15:08 -080088 status = btsock_l2cap_init(thread_handle, uid_set);
Kim Schulz8372aa52015-03-25 10:39:40 +010089 if (status != BT_STATUS_SUCCESS) {
Myles Watson6bd442f2016-10-19 09:50:22 -070090 LOG_ERROR(LOG_TAG, "%s error initializing L2CAP sockets: %d", __func__,
91 status);
Kim Schulz8372aa52015-03-25 10:39:40 +010092 goto error;
93 }
94
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -070095 thread = thread_new("btif_sock");
96 if (!thread) {
Marie Janssendb554582015-06-26 14:53:46 -070097 LOG_ERROR(LOG_TAG, "%s error creating new thread.", __func__);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -070098 btsock_rfc_cleanup();
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -070099 goto error;
100 }
101
102 status = btsock_sco_init(thread);
103 if (status != BT_STATUS_SUCCESS) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700104 LOG_ERROR(LOG_TAG, "%s error initializing SCO sockets: %d", __func__,
105 status);
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -0700106 btsock_rfc_cleanup();
107 goto error;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700108 }
109
110 return BT_STATUS_SUCCESS;
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -0700111
112error:;
113 thread_free(thread);
114 thread = NULL;
Myles Watson6bd442f2016-10-19 09:50:22 -0700115 if (thread_handle != -1) btsock_thread_exit(thread_handle);
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -0700116 thread_handle = -1;
Adam Lesinski0620f972015-12-02 22:15:08 -0800117 uid_set = NULL;
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -0700118 return BT_STATUS_FAIL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119}
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700120
121void btif_sock_cleanup(void) {
Myles Watsonb793f4f2017-02-28 11:05:52 -0800122 int saved_handle = thread_handle;
123 if (std::atomic_exchange(&thread_handle, -1) == -1) return;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700124
Myles Watsonb793f4f2017-02-28 11:05:52 -0800125 btsock_thread_exit(saved_handle);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700126 btsock_rfc_cleanup();
127 btsock_sco_cleanup();
Kim Schulz8372aa52015-03-25 10:39:40 +0100128 btsock_l2cap_cleanup();
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -0700129 thread_free(thread);
Sharvil Nanavatic8aaee22014-08-21 18:17:19 -0700130 thread = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800131}
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700132
Myles Watson6bd442f2016-10-19 09:50:22 -0700133static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700134 const Uuid* service_uuid, int channel,
Myles Watson6bd442f2016-10-19 09:50:22 -0700135 int* sock_fd, int flags, int app_uid) {
136 if ((flags & BTSOCK_FLAG_NO_SDP) == 0) {
Jack Hef2af1c42016-12-13 01:59:12 -0800137 CHECK(sock_fd != NULL);
Kim Schulz8372aa52015-03-25 10:39:40 +0100138 }
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700139
140 *sock_fd = INVALID_FD;
141 bt_status_t status = BT_STATUS_FAIL;
Stanley Tng49dd53c2017-12-20 09:38:30 -0800142 int original_channel = channel;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700143
Jack He83f86832019-02-06 20:24:24 -0800144 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);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700149 switch (type) {
150 case BTSOCK_RFCOMM:
Myles Watson6bd442f2016-10-19 09:50:22 -0700151 status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd,
152 flags, app_uid);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700153 break;
Kim Schulz8372aa52015-03-25 10:39:40 +0100154 case BTSOCK_L2CAP:
Myles Watson6bd442f2016-10-19 09:50:22 -0700155 status =
156 btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
Kim Schulz8372aa52015-03-25 10:39:40 +0100157 break;
Stanley Tng49dd53c2017-12-20 09:38:30 -0800158 case BTSOCK_L2CAP_LE:
159 if (flags & BTSOCK_FLAG_NO_SDP) {
Stanley Tnga34e9b42018-01-05 09:25:11 -0800160 /* Set channel to zero so that it will be assigned */
161 channel = 0;
162 } else if (channel <= 0) {
Stanley Tng49dd53c2017-12-20 09:38:30 -0800163 LOG_ERROR(LOG_TAG, "%s: type BTSOCK_L2CAP_LE: invalid channel=%d",
164 __func__, channel);
165 break;
166 }
Stanley Tnga34e9b42018-01-05 09:25:11 -0800167 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);
Stanley Tng49dd53c2017-12-20 09:38:30 -0800172 status =
173 btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
174 break;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700175 case BTSOCK_SCO:
176 status = btsock_sco_listen(sock_fd, flags);
177 break;
178
179 default:
Myles Watson6bd442f2016-10-19 09:50:22 -0700180 LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__,
181 type);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700182 status = BT_STATUS_UNSUPPORTED;
183 break;
184 }
Jack He83f86832019-02-06 20:24:24 -0800185 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 }
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700192 return status;
193}
194
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700195static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700196 const Uuid* uuid, int channel, int* sock_fd,
197 int flags, int app_uid) {
Jack Hef2af1c42016-12-13 01:59:12 -0800198 CHECK(bd_addr != NULL);
199 CHECK(sock_fd != NULL);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700200
201 *sock_fd = INVALID_FD;
202 bt_status_t status = BT_STATUS_FAIL;
203
Jack He83f86832019-02-06 20:24:24 -0800204 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);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700209 switch (type) {
210 case BTSOCK_RFCOMM:
Myles Watson6bd442f2016-10-19 09:50:22 -0700211 status =
212 btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700213 break;
214
Kim Schulz8372aa52015-03-25 10:39:40 +0100215 case BTSOCK_L2CAP:
Adam Lesinski0620f972015-12-02 22:15:08 -0800216 status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
Kim Schulz8372aa52015-03-25 10:39:40 +0100217 break;
218
Jakub Pawlowski3118cdc2019-08-05 20:28:22 +0200219 case BTSOCK_L2CAP_LE: {
Stanley Tnga34e9b42018-01-05 09:25:11 -0800220 flags |= BTSOCK_FLAG_LE_COC;
Jakub Pawlowski3118cdc2019-08-05 20:28:22 +0200221
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
Stanley Tnga34e9b42018-01-05 09:25:11 -0800232 LOG_DEBUG(LOG_TAG, "%s: type=BTSOCK_L2CAP_LE, channel=0x%x, flags=0x%x",
233 __func__, channel, flags);
Stanley Tng49dd53c2017-12-20 09:38:30 -0800234 status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
235 break;
Jakub Pawlowski3118cdc2019-08-05 20:28:22 +0200236 }
Stanley Tng49dd53c2017-12-20 09:38:30 -0800237
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700238 case BTSOCK_SCO:
239 status = btsock_sco_connect(bd_addr, sock_fd, flags);
240 break;
241
242 default:
Myles Watson6bd442f2016-10-19 09:50:22 -0700243 LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__,
244 type);
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700245 status = BT_STATUS_UNSUPPORTED;
246 break;
247 }
Jack He83f86832019-02-06 20:24:24 -0800248 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 }
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700255 return status;
256}
257
Stanley Tng70bebaf2018-01-22 17:08:03 -0800258static 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
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700267static 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;
Kim Schulz8372aa52015-03-25 10:39:40 +0100272 case BTSOCK_L2CAP:
Stanley Tng49dd53c2017-12-20 09:38:30 -0800273 case BTSOCK_L2CAP_LE:
274 /* Note: The caller may not distinguish between BTSOCK_L2CAP and
275 * BTSOCK_L2CAP_LE correctly */
Kim Schulz8372aa52015-03-25 10:39:40 +0100276 btsock_l2cap_signaled(fd, flags, user_id);
277 break;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700278 default:
Hansong Zhanga912f962018-11-15 15:37:12 -0800279 LOG(FATAL) << "Invalid socket type! type=" << type << " fd=" << fd
280 << " flags=" << flags << " user_id=" << user_id;
Sharvil Nanavatife4ec6b2014-08-21 13:17:43 -0700281 break;
282 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283}