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(const std::string & device,const std::vector<BluetoothIOppTransferFileHolder> & fileHolders,bool & result)25 int32_t BluetoothOppProxy::SendFile(const std::string &device,
26 const std::vector<BluetoothIOppTransferFileHolder> &fileHolders, 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 (!WriteParcelableFileHolderVector(fileHolders, data)) {
34 HILOGE("[SendFile] fail: write fileHolders failed");
35 return BT_ERR_INTERNAL_ERROR;
36 }
37
38 MessageParcel reply;
39 MessageOption option {
40 MessageOption::TF_SYNC
41 };
42
43 int error = Remote()->SendRequest(
44 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_SEND_FILE), data, reply, option);
45 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
46 int32_t ret = reply.ReadInt32();
47 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
48 result = ret;
49 return BT_NO_ERROR;
50 }
51
SetIncomingFileConfirmation(bool accept,int fd)52 int32_t BluetoothOppProxy::SetIncomingFileConfirmation(bool accept, int fd)
53 {
54 MessageParcel data;
55 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
56 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
57 CHECK_AND_RETURN_LOG_RET(data.WriteBool(accept), BT_ERR_INTERNAL_ERROR,
58 "setIncomingFileConfirmation write bool error");
59 data.WriteFileDescriptor(fd);
60 HILOGE("write fd %{public}d success", fd);
61 MessageParcel reply;
62 MessageOption option {
63 MessageOption::TF_SYNC
64 };
65
66 int error = Remote()->SendRequest(
67 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_SET_INCOMING_FILE_CONFIRMATION), data, reply, option);
68 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
69 return reply.ReadInt32();
70 }
71
GetCurrentTransferInformation(BluetoothIOppTransferInformation & oppInformation)72 int32_t BluetoothOppProxy::GetCurrentTransferInformation(BluetoothIOppTransferInformation &oppInformation)
73 {
74 MessageParcel data;
75 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
76 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
77
78 MessageParcel reply;
79 MessageOption option = {MessageOption::TF_SYNC};
80 int error = Remote()->SendRequest(static_cast<uint32_t>(
81 BluetoothOppInterfaceCode::COMMAND_GET_CURRENT_TRANSFER_INFORMATION), data, reply, option);
82 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
83 int32_t ret = reply.ReadInt32();
84 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
85 std::unique_ptr<BluetoothIOppTransferInformation>
86 oppInformation_(reply.ReadParcelable<BluetoothIOppTransferInformation>());
87 CHECK_AND_RETURN_LOG_RET((oppInformation_ != nullptr), BT_ERR_DEVICE_DISCONNECTED, "oppInformation is nullptr");
88 oppInformation = *oppInformation_;
89 return BT_NO_ERROR;
90 }
91
CancelTransfer()92 int32_t BluetoothOppProxy::CancelTransfer()
93 {
94 MessageParcel data;
95 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
96 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
97 MessageParcel reply;
98 MessageOption option {
99 MessageOption::TF_SYNC
100 };
101
102 int error = Remote()->SendRequest(
103 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_CANCEL_TRANSFER), data, reply, option);
104 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
105
106 return reply.ReadInt32();
107 }
108
SetLastReceivedFileUri(const std::string & uri)109 int32_t BluetoothOppProxy::SetLastReceivedFileUri(const std::string &uri)
110 {
111 MessageParcel data;
112 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
113 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
114 CHECK_AND_RETURN_LOG_RET(data.WriteString(uri), BT_ERR_INTERNAL_ERROR, "Write uri error");
115
116 MessageParcel reply;
117 MessageOption option {
118 MessageOption::TF_SYNC
119 };
120
121 int error = Remote()->SendRequest(
122 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_SET_LAST_RECEIVED_URI), data, reply, option);
123 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
124 int32_t ret = reply.ReadInt32();
125 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
126 return BT_NO_ERROR;
127 }
128
RegisterObserver(const sptr<IBluetoothOppObserver> & observer)129 void BluetoothOppProxy::RegisterObserver(const sptr<IBluetoothOppObserver> &observer)
130 {
131 MessageParcel data;
132 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()), "WriteInterfaceToken error");
133 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
134
135 MessageParcel reply;
136 MessageOption option {
137 MessageOption::TF_ASYNC
138 };
139
140 int error = Remote()->SendRequest(
141 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_REGISTER_OBSERVER), data, reply, option);
142 CHECK_AND_RETURN_LOG((error == BT_NO_ERROR), "error: %{public}d", error);
143 }
144
DeregisterObserver(const sptr<IBluetoothOppObserver> & observer)145 void BluetoothOppProxy::DeregisterObserver(const sptr<IBluetoothOppObserver> &observer)
146 {
147 MessageParcel data;
148 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()), "WriteInterfaceToken error");
149 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
150
151 MessageParcel reply;
152 MessageOption option {
153 MessageOption::TF_ASYNC
154 };
155
156 int error = Remote()->SendRequest(
157 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_DEREGISTER_OBSERVER), data, reply, option);
158 CHECK_AND_RETURN_LOG((error == BT_NO_ERROR), "error: %{public}d", error);
159 }
160
GetDeviceState(const BluetoothRawAddress & device,int & result)161 int32_t BluetoothOppProxy::GetDeviceState(const BluetoothRawAddress &device, int& result)
162 {
163 MessageParcel data;
164 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
165 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
166 if (!data.WriteParcelable(&device)) {
167 HILOGE("BluetoothOppProxy::GetDeviceState write device error");
168 return BT_ERR_INTERNAL_ERROR;
169 }
170
171 MessageParcel reply;
172 MessageOption option {
173 MessageOption::TF_SYNC
174 };
175
176 int error = Remote()->SendRequest(
177 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_GET_DEVICE_STATE), data, reply, option);
178 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
179
180 int32_t ret = reply.ReadInt32();
181 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
182
183 result = reply.ReadInt32();
184 return BT_NO_ERROR;
185 }
186
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)187 int32_t BluetoothOppProxy::GetDevicesByStates(
188 const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
189 {
190 MessageParcel data;
191 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
192 BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
193 if (!WriteParcelableInt32Vector(states, data)) {
194 HILOGE("[GetDevicesByStates] fail: write result failed");
195 return INVALID_DATA;
196 }
197
198 MessageParcel reply;
199 MessageOption option = {MessageOption::TF_SYNC};
200 int error = Remote()->SendRequest(
201 static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_GET_DEVICES_BY_STATES), data, reply, option);
202 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
203 int32_t ret = reply.ReadInt32();
204 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
205 int32_t rawAddsSize = reply.ReadInt32();
206 const int32_t maxSize = 100;
207 if (rawAddsSize > maxSize) {
208 return BT_ERR_INVALID_PARAM;
209 }
210 for (int i = 0; i < rawAddsSize; i++) {
211 std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
212 if (!address) {
213 return TRANSACTION_ERR;
214 }
215 result.push_back(*address);
216 }
217 return BT_NO_ERROR;
218 }
219
WriteParcelableStringVector(const std::vector<std::string> & parcelableVector,Parcel & reply)220 bool BluetoothOppProxy::WriteParcelableStringVector(const std::vector<std::string> &parcelableVector, Parcel &reply)
221 {
222 if (!reply.WriteInt32(parcelableVector.size())) {
223 HILOGE("write ParcelableVector failed");
224 return false;
225 }
226
227 for (auto parcelable : parcelableVector) {
228 if (!reply.WriteString(parcelable)) {
229 HILOGE("write ParcelableVector failed");
230 return false;
231 }
232 }
233 return true;
234 }
235
WriteParcelableFileHolderVector(const std::vector<BluetoothIOppTransferFileHolder> & fileHolders,MessageParcel & reply)236 bool BluetoothOppProxy::WriteParcelableFileHolderVector(const std::vector<BluetoothIOppTransferFileHolder> &fileHolders,
237 MessageParcel &reply)
238 {
239 if (!reply.WriteInt32(fileHolders.size())) {
240 HILOGE("write ParcelableVector failed");
241 return false;
242 }
243
244 for (auto &fileHolder : fileHolders) {
245 if (!reply.WriteString(fileHolder.GetFilePath())) {
246 HILOGE("write file path failed");
247 return false;
248 }
249 if (!reply.WriteInt64(fileHolder.GetFileSize())) {
250 HILOGE("write file size failed");
251 return false;
252 }
253 if (!reply.WriteFileDescriptor(fileHolder.GetFileFd())) {
254 HILOGE("write file fd failed");
255 return false;
256 }
257 }
258 return true;
259 }
260
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)261 bool BluetoothOppProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
262 {
263 if (!reply.WriteInt32(parcelableVector.size())) {
264 HILOGE("write ParcelableVector failed");
265 return false;
266 }
267
268 for (auto parcelable : parcelableVector) {
269 if (!reply.WriteInt32(parcelable)) {
270 HILOGE("write ParcelableVector failed");
271 return false;
272 }
273 }
274 return true;
275 }
276 } // namespace Bluetooth
277 } // namespace OHOS