1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_opp_proxy"
17 #endif
18
19 #include "bluetooth_opp_proxy.h"
20 #include "bluetooth_log.h"
21 #include "bluetooth_errorcode.h"
22
23 namespace OHOS {
24 namespace Bluetooth {
SendFile(std::string & device,std::vector<std::string> & filePaths,std::vector<std::string> & mimeTypes,bool & result)25 int32_t BluetoothOppProxy::SendFile(std::string &device,
26 std::vector<std::string> &filePaths, std::vector<std::string> &mimeTypes, bool& result)
27 {
28 MessageParcel data;
29 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
30 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
31 CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_INTERNAL_ERROR, "Write device error");
32
33 if (!WriteParcelableStringVector(filePaths, data)) {
34 HILOGE("[SendFile] fail: write result failed");
35 return BT_ERR_INTERNAL_ERROR;
36 }
37 if (!WriteParcelableStringVector(mimeTypes, data)) {
38 HILOGE("[SendFile] fail: write result failed");
39 return BT_ERR_INTERNAL_ERROR;
40 }
41
42 MessageParcel reply;
43 MessageOption option {
44 MessageOption::TF_SYNC
45 };
46
47 int error = Remote()->SendRequest(
48 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_SEND_FILE), data, reply, option);
49 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
50 int32_t ret = reply.ReadInt32();
51 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
52 result = ret;
53 return BT_NO_ERROR;
54 }
55
SetIncomingFileConfirmation(bool accept)56 int32_t BluetoothOppProxy::SetIncomingFileConfirmation(bool accept)
57 {
58 MessageParcel data;
59 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
60 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
61 CHECK_AND_RETURN_LOG_RET(data.WriteBool(accept), BT_ERR_INTERNAL_ERROR,
62 "setIncomingFileConfirmation write bool error");
63
64 MessageParcel reply;
65 MessageOption option {
66 MessageOption::TF_SYNC
67 };
68
69 int error = Remote()->SendRequest(
70 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_SET_INCOMING_FILE_CONFIRMATION), data, reply, option);
71 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
72 return reply.ReadInt32();
73 }
74
GetCurrentTransferInformation(BluetoothIOppTransferInformation & oppInformation)75 int32_t BluetoothOppProxy::GetCurrentTransferInformation(BluetoothIOppTransferInformation &oppInformation)
76 {
77 MessageParcel data;
78 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
79 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
80
81 MessageParcel reply;
82 MessageOption option = {MessageOption::TF_SYNC};
83 int error = Remote()->SendRequest(static_cast<uint32_t>(
84 BluetoothOppInterfaceCode::COMMAND_GET_CURRENT_TRANSFER_INFORMATION), data, reply, option);
85 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
86 std::unique_ptr<BluetoothIOppTransferInformation>
87 oppInformation_(reply.ReadParcelable<BluetoothIOppTransferInformation>());
88 CHECK_AND_RETURN_LOG_RET((oppInformation_ != nullptr), BT_ERR_DEVICE_DISCONNECTED, "oppInformation is nullptr");
89 oppInformation = *oppInformation_;
90 return BT_NO_ERROR;
91 }
92
CancelTransfer(bool & result)93 int32_t BluetoothOppProxy::CancelTransfer(bool &result)
94 {
95 MessageParcel data;
96 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
97 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
98 MessageParcel reply;
99 MessageOption option {
100 MessageOption::TF_SYNC
101 };
102
103 int error = Remote()->SendRequest(
104 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_CANCEL_TRANSFER), data, reply, option);
105 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
106
107 result = reply.ReadInt32() == BT_NO_ERROR ? true : false;
108 return BT_NO_ERROR;
109 }
110
RegisterObserver(const sptr<IBluetoothOppObserver> & observer)111 void BluetoothOppProxy::RegisterObserver(const sptr<IBluetoothOppObserver> &observer)
112 {
113 MessageParcel data;
114 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()), "WriteInterfaceToken error");
115 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
116
117 MessageParcel reply;
118 MessageOption option {
119 MessageOption::TF_ASYNC
120 };
121
122 int error = Remote()->SendRequest(
123 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_REGISTER_OBSERVER), data, reply, option);
124 CHECK_AND_RETURN_LOG((error == BT_NO_ERROR), "error: %{public}d", error);
125 }
126
DeregisterObserver(const sptr<IBluetoothOppObserver> & observer)127 void BluetoothOppProxy::DeregisterObserver(const sptr<IBluetoothOppObserver> &observer)
128 {
129 MessageParcel data;
130 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()), "WriteInterfaceToken error");
131 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
132
133 MessageParcel reply;
134 MessageOption option {
135 MessageOption::TF_ASYNC
136 };
137
138 int error = Remote()->SendRequest(
139 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_DEREGISTER_OBSERVER), data, reply, option);
140 CHECK_AND_RETURN_LOG((error == BT_NO_ERROR), "error: %{public}d", error);
141 }
142
GetDeviceState(const BluetoothRawAddress & device,int & result)143 int32_t BluetoothOppProxy::GetDeviceState(const BluetoothRawAddress &device, int& result)
144 {
145 MessageParcel data;
146 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
147 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
148 if (!data.WriteParcelable(&device)) {
149 HILOGE("BluetoothOppProxy::GetDeviceState write device error");
150 return BT_ERR_INTERNAL_ERROR;
151 }
152
153 MessageParcel reply;
154 MessageOption option {
155 MessageOption::TF_SYNC
156 };
157
158 int error = Remote()->SendRequest(
159 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_GET_DEVICE_STATE), data, reply, option);
160 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
161
162 int32_t ec = reply.ReadInt32();
163 if (FAILED(ec)) {
164 return ec;
165 }
166
167 result = reply.ReadInt32();
168 return BT_NO_ERROR;
169 }
170
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)171 int32_t BluetoothOppProxy::GetDevicesByStates(
172 const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
173 {
174 MessageParcel data;
175 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
176 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
177 if (!WriteParcelableInt32Vector(states, data)) {
178 HILOGE("[GetDevicesByStates] fail: write result failed");
179 return INVALID_DATA;
180 }
181
182 MessageParcel reply;
183 MessageOption option = {MessageOption::TF_SYNC};
184 int error = Remote()->SendRequest(
185 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_GET_DEVICES_BY_STATES), data, reply, option);
186 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
187 int32_t rawAddsSize = reply.ReadInt32();
188 const int32_t maxSize = 100;
189 if (rawAddsSize > maxSize) {
190 return BT_ERR_INVALID_PARAM;
191 }
192 for (int i = 0; i < rawAddsSize; i++) {
193 std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
194 if (!address) {
195 return TRANSACTION_ERR;
196 }
197 result.push_back(*address);
198 }
199 return BT_NO_ERROR;
200 }
201
WriteParcelableStringVector(const std::vector<std::string> & parcelableVector,Parcel & reply)202 bool BluetoothOppProxy::WriteParcelableStringVector(const std::vector<std::string> &parcelableVector, Parcel &reply)
203 {
204 if (!reply.WriteInt32(parcelableVector.size())) {
205 HILOGE("write ParcelableVector failed");
206 return false;
207 }
208
209 for (auto parcelable : parcelableVector) {
210 if (!reply.WriteString(parcelable)) {
211 HILOGE("write ParcelableVector failed");
212 return false;
213 }
214 }
215 return true;
216 }
217
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)218 bool BluetoothOppProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
219 {
220 if (!reply.WriteInt32(parcelableVector.size())) {
221 HILOGE("write ParcelableVector failed");
222 return false;
223 }
224
225 for (auto parcelable : parcelableVector) {
226 if (!reply.WriteInt32(parcelable)) {
227 HILOGE("write ParcelableVector failed");
228 return false;
229 }
230 }
231 return true;
232 }
233 } // namespace Bluetooth
234 } // namespace OHOS