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::OK;
24 using ::android::String8;
25 using ::android::binder::Status;
26 using ::android::os::ParcelFileDescriptor;
27 using ::android::os::ParcelUuid;
28
29 namespace android {
30 namespace bluetooth {
31
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)32 Status BluetoothSocketManagerBinderServer::connectSocket(
33 const BluetoothDevice& device, int32_t type,
34 const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
35 std::unique_ptr<ParcelFileDescriptor>* _aidl_return) {
36 if (!isCallerActiveUserOrManagedProfile()) {
37 LOG(WARNING) << "connectSocket() - Not allowed for non-active users";
38 return Status::fromExceptionCode(
39 Status::EX_SECURITY, String8("Not allowed for non-active users"));
40 }
41
42 ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
43
44 IPCThreadState* ipc = IPCThreadState::self();
45
46 int socket_fd = -1;
47 bt_status_t status = socketInterface->connect(
48 &device.address, (btsock_type_t)type, uuid ? &uuid->uuid : nullptr, port,
49 &socket_fd, flag, ipc->getCallingUid());
50 if (status != BT_STATUS_SUCCESS) {
51 LOG(ERROR) << "Socket connection failed: " << +status;
52 socket_fd = -1;
53 }
54
55 if (socket_fd < 0) {
56 LOG(ERROR) << "Fail to create file descriptor on socket fd";
57 return Status::ok();
58 }
59
60 _aidl_return->reset(new ParcelFileDescriptor());
61 (*_aidl_return)->setFileDescriptor(socket_fd, true);
62 return Status::ok();
63 }
64
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)65 Status BluetoothSocketManagerBinderServer::createSocketChannel(
66 int32_t type, const std::unique_ptr<::android::String16>& serviceName,
67 const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
68 std::unique_ptr<ParcelFileDescriptor>* _aidl_return) {
69 if (!isCallerActiveUserOrManagedProfile()) {
70 LOG(WARNING) << "createSocketChannel() - Not allowed for non-active users";
71 return Status::fromExceptionCode(
72 Status::EX_SECURITY, String8("Not allowed for non-active users"));
73 }
74
75 ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
76
77 VLOG(1) << __func__ << ": SOCK FLAG=" << flag;
78
79 IPCThreadState* ipc = IPCThreadState::self();
80 int socket_fd = -1;
81
82 const std::string payload_url{};
83
84 bt_status_t status = socketInterface->listen(
85 (btsock_type_t)type,
86 serviceName ? String8{*serviceName}.c_str() : nullptr,
87 uuid ? &uuid->uuid : nullptr, port, &socket_fd, flag,
88 ipc->getCallingUid());
89
90 if (status != BT_STATUS_SUCCESS) {
91 LOG(ERROR) << "Socket listen failed: " << +status;
92 socket_fd = -1;
93 }
94
95 if (socket_fd < 0) {
96 LOG(ERROR) << "Failed to create file descriptor on socket fd";
97 return Status::ok();
98 }
99
100 _aidl_return->reset(new ParcelFileDescriptor());
101 (*_aidl_return)->setFileDescriptor(socket_fd, true);
102 return Status::ok();
103 }
104
requestMaximumTxDataLength(const BluetoothDevice & device)105 Status BluetoothSocketManagerBinderServer::requestMaximumTxDataLength(
106 const BluetoothDevice& device) {
107 if (!isCallerActiveUserOrManagedProfile()) {
108 LOG(WARNING) << __func__ << ": Not allowed for non-active users";
109 return Status::fromExceptionCode(
110 Status::EX_SECURITY, String8("Not allowed for non-active users"));
111 }
112
113 ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
114
115 VLOG(1) << __func__;
116
117 socketInterface->request_max_tx_data_length(device.address);
118 return Status::ok();
119 }
120
121 } // namespace bluetooth
122 } // namespace android
123