• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "bluetooth_socket_manager.h"
18 #include "permission_helpers.h"
19 
20 #include <base/logging.h>
21 #include <binder/IPCThreadState.h>
22 
23 using ::android::String8;
24 using ::android::binder::Status;
25 using ::android::os::ParcelFileDescriptor;
26 using ::android::os::ParcelUuid;
27 
28 namespace android {
29 namespace bluetooth {
30 
connectSocket(const BluetoothDevice & device,int32_t type,const std::unique_ptr<ParcelUuid> & uuid,int32_t port,int32_t flag,std::unique_ptr<ParcelFileDescriptor> * _aidl_return)31 Status BluetoothSocketManagerBinderServer::connectSocket(
32     const BluetoothDevice& device, int32_t type,
33     const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
34     std::unique_ptr<ParcelFileDescriptor>* _aidl_return) {
35   if (!isCallerActiveUserOrManagedProfile()) {
36     LOG(WARNING) << "connectSocket() - Not allowed for non-active users";
37     return Status::fromExceptionCode(
38         Status::EX_SECURITY, String8("Not allowed for non-active users"));
39   }
40 
41   ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
42 
43   IPCThreadState* ipc = IPCThreadState::self();
44 
45   int socket_fd = -1;
46   bt_status_t status = socketInterface->connect(
47       &device.address, (btsock_type_t)type, uuid ? &uuid->uuid : nullptr, port,
48       &socket_fd, flag, ipc->getCallingUid());
49   if (status != BT_STATUS_SUCCESS) {
50     LOG(ERROR) << "Socket connection failed: " << +status;
51     socket_fd = -1;
52   }
53 
54   if (socket_fd < 0) {
55     LOG(ERROR) << "Fail to create file descriptor on socket fd";
56     return Status::ok();
57   }
58 
59   _aidl_return->reset(
60       new ParcelFileDescriptor(android::base::unique_fd(socket_fd)));
61   return Status::ok();
62 }
63 
createSocketChannel(int32_t type,const std::unique_ptr<::android::String16> & serviceName,const std::unique_ptr<ParcelUuid> & uuid,int32_t port,int32_t flag,std::unique_ptr<ParcelFileDescriptor> * _aidl_return)64 Status BluetoothSocketManagerBinderServer::createSocketChannel(
65     int32_t type, const std::unique_ptr<::android::String16>& serviceName,
66     const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
67     std::unique_ptr<ParcelFileDescriptor>* _aidl_return) {
68   if (!isCallerActiveUserOrManagedProfile()) {
69     LOG(WARNING) << "createSocketChannel() - Not allowed for non-active users";
70     return Status::fromExceptionCode(
71         Status::EX_SECURITY, String8("Not allowed for non-active users"));
72   }
73 
74   ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
75 
76   VLOG(1) << __func__ << ": SOCK FLAG=" << flag;
77 
78   IPCThreadState* ipc = IPCThreadState::self();
79   int socket_fd = -1;
80 
81   const std::string payload_url{};
82 
83   bt_status_t status = socketInterface->listen(
84       (btsock_type_t)type,
85       serviceName ? String8{*serviceName}.c_str() : nullptr,
86       uuid ? &uuid->uuid : nullptr, port, &socket_fd, flag,
87       ipc->getCallingUid());
88 
89   if (status != BT_STATUS_SUCCESS) {
90     LOG(ERROR) << "Socket listen failed: " << +status;
91     socket_fd = -1;
92   }
93 
94   if (socket_fd < 0) {
95     LOG(ERROR) << "Failed to create file descriptor on socket fd";
96     return Status::ok();
97   }
98 
99   _aidl_return->reset(
100       new ParcelFileDescriptor(android::base::unique_fd(socket_fd)));
101   return Status::ok();
102 }
103 
requestMaximumTxDataLength(const BluetoothDevice & device)104 Status BluetoothSocketManagerBinderServer::requestMaximumTxDataLength(
105     const BluetoothDevice& device) {
106   if (!isCallerActiveUserOrManagedProfile()) {
107     LOG(WARNING) << __func__ << ": Not allowed for non-active users";
108     return Status::fromExceptionCode(
109         Status::EX_SECURITY, String8("Not allowed for non-active users"));
110   }
111 
112   ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
113 
114   VLOG(1) << __func__;
115 
116   socketInterface->request_max_tx_data_length(device.address);
117   return Status::ok();
118 }
119 
120 }  // namespace bluetooth
121 }  // namespace android
122