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