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 #include "bluetooth_opp_proxy.h"
16 #include "bluetooth_log.h"
17 #include "bluetooth_errorcode.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
SendFile(std::string &device, std::vector<std::string> &filePaths, std::vector<std::string> &mimeTypes, bool& result)21 int32_t BluetoothOppProxy::SendFile(std::string &device,
22     std::vector<std::string> &filePaths, std::vector<std::string> &mimeTypes, bool& result)
23 {
24     MessageParcel data;
25     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
26         BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
27     CHECK_AND_RETURN_LOG_RET(data.WriteString(device), BT_ERR_INTERNAL_ERROR, "Write device error");
28 
29     if (!WriteParcelableStringVector(filePaths, data)) {
30         HILOGE("[SendFile] fail: write result failed");
31         return BT_ERR_INTERNAL_ERROR;
32     }
33     if (!WriteParcelableStringVector(mimeTypes, data)) {
34         HILOGE("[SendFile] fail: write result 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)52 int32_t BluetoothOppProxy::SetIncomingFileConfirmation(bool accept)
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 
60     MessageParcel reply;
61     MessageOption option {
62         MessageOption::TF_SYNC
63     };
64 
65     int error = Remote()->SendRequest(
66         static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_SET_INCOMING_FILE_CONFIRMATION), data, reply, option);
67     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
68     return reply.ReadInt32();
69 }
70 
GetCurrentTransferInformation(BluetoothIOppTransferInformation &oppInformation)71 int32_t BluetoothOppProxy::GetCurrentTransferInformation(BluetoothIOppTransferInformation &oppInformation)
72 {
73     MessageParcel data;
74     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
75         BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
76 
77     MessageParcel reply;
78     MessageOption option = {MessageOption::TF_SYNC};
79     int error = Remote()->SendRequest(static_cast<uint32_t>(
80         BluetoothOppInterfaceCode::COMMAND_GET_CURRENT_TRANSFER_INFORMATION), data, reply, option);
81     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
82     std::unique_ptr<BluetoothIOppTransferInformation>
83         oppInformation_(reply.ReadParcelable<BluetoothIOppTransferInformation>());
84     CHECK_AND_RETURN_LOG_RET((oppInformation_ != nullptr), BT_ERR_DEVICE_DISCONNECTED, "oppInformation is nullptr");
85     oppInformation = *oppInformation_;
86     return BT_NO_ERROR;
87 }
88 
CancelTransfer(bool &result)89 int32_t BluetoothOppProxy::CancelTransfer(bool &result)
90 {
91     MessageParcel data;
92     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
93         BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
94     MessageParcel reply;
95     MessageOption option {
96         MessageOption::TF_SYNC
97     };
98 
99     int error = Remote()->SendRequest(
100         static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_CANCEL_TRANSFER), data, reply, option);
101     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
102 
103     result = reply.ReadInt32() == BT_NO_ERROR ? true : false;
104     return BT_NO_ERROR;
105 }
106 
RegisterObserver(const sptr<IBluetoothOppObserver> &observer)107 void BluetoothOppProxy::RegisterObserver(const sptr<IBluetoothOppObserver> &observer)
108 {
109     MessageParcel data;
110     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()), "WriteInterfaceToken error");
111     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
112 
113     MessageParcel reply;
114     MessageOption option {
115         MessageOption::TF_ASYNC
116     };
117 
118     int error = Remote()->SendRequest(
119         static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_REGISTER_OBSERVER), data, reply, option);
120     CHECK_AND_RETURN_LOG((error == BT_NO_ERROR), "error: %{public}d", error);
121 }
122 
DeregisterObserver(const sptr<IBluetoothOppObserver> &observer)123 void BluetoothOppProxy::DeregisterObserver(const sptr<IBluetoothOppObserver> &observer)
124 {
125     MessageParcel data;
126     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()), "WriteInterfaceToken error");
127     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
128 
129     MessageParcel reply;
130     MessageOption option {
131         MessageOption::TF_ASYNC
132     };
133 
134     int error = Remote()->SendRequest(
135         static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_DEREGISTER_OBSERVER), data, reply, option);
136     CHECK_AND_RETURN_LOG((error == BT_NO_ERROR), "error: %{public}d", error);
137 }
138 
GetDeviceState(const BluetoothRawAddress &device, int& result)139 int32_t BluetoothOppProxy::GetDeviceState(const BluetoothRawAddress &device, int& result)
140 {
141     MessageParcel data;
142     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
143         BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
144     if (!data.WriteParcelable(&device)) {
145         HILOGE("BluetoothOppProxy::GetDeviceState write device error");
146         return BT_ERR_INTERNAL_ERROR;
147     }
148 
149     MessageParcel reply;
150     MessageOption option {
151         MessageOption::TF_SYNC
152     };
153 
154     int error = Remote()->SendRequest(
155         static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_GET_DEVICE_STATE), data, reply, option);
156     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
157 
158     int32_t ec = reply.ReadInt32();
159     if (FAILED(ec)) {
160         return ec;
161     }
162 
163     result = reply.ReadInt32();
164     return BT_NO_ERROR;
165 }
166 
GetDevicesByStates( const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)167 int32_t BluetoothOppProxy::GetDevicesByStates(
168     const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
169 {
170     MessageParcel data;
171     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothOppProxy::GetDescriptor()),
172         BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
173     if (!WriteParcelableInt32Vector(states, data)) {
174         HILOGE("[GetDevicesByStates] fail: write result failed");
175         return INVALID_DATA;
176     }
177 
178     MessageParcel reply;
179     MessageOption option = {MessageOption::TF_SYNC};
180     int error = Remote()->SendRequest(
181         static_cast<uint32_t>(BluetoothOppInterfaceCode::COMMAND_GET_DEVICES_BY_STATES), data, reply, option);
182     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error);
183     int32_t rawAddsSize = reply.ReadInt32();
184     for (int i = 0; i < rawAddsSize; i++) {
185         std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
186         if (!address) {
187             return TRANSACTION_ERR;
188         }
189         result.push_back(*address);
190     }
191     return BT_NO_ERROR;
192 }
193 
WriteParcelableStringVector(const std::vector<std::string> &parcelableVector, Parcel &reply)194 bool BluetoothOppProxy::WriteParcelableStringVector(const std::vector<std::string> &parcelableVector, Parcel &reply)
195 {
196     if (!reply.WriteInt32(parcelableVector.size())) {
197         HILOGE("write ParcelableVector failed");
198         return false;
199     }
200 
201     for (auto parcelable : parcelableVector) {
202         if (!reply.WriteString(parcelable)) {
203             HILOGE("write ParcelableVector failed");
204             return false;
205         }
206     }
207     return true;
208 }
209 
WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)210 bool BluetoothOppProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
211 {
212     if (!reply.WriteInt32(parcelableVector.size())) {
213         HILOGE("write ParcelableVector failed");
214         return false;
215     }
216 
217     for (auto parcelable : parcelableVector) {
218         if (!reply.WriteInt32(parcelable)) {
219             HILOGE("write ParcelableVector failed");
220             return false;
221         }
222     }
223     return true;
224 }
225 }  // namespace Bluetooth
226 }  // namespace OHOS