1 /*
2 * Copyright (C) 2021 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
16 #include "bluetooth_pbap_pce_proxy.h"
17 #include "bluetooth_log.h"
18 #include "i_bluetooth_pbap_pce.h"
19 #include "bluetooth_errorcode.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
GetDeviceState(const BluetoothRawAddress & device)23 int BluetoothPbapPceProxy::GetDeviceState(const BluetoothRawAddress &device)
24 {
25 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
26 MessageParcel data;
27 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
28 HILOGE("BluetoothPbapPceProxy::GetDeviceState WriteInterfaceToken error");
29 return ERROR;
30 }
31
32 if (!data.WriteParcelable(&device)) {
33 HILOGE("BluetoothPbapPceProxy::GetDeviceState error");
34 return ERROR;
35 }
36
37 MessageParcel reply;
38 MessageOption option {
39 MessageOption::TF_SYNC
40 };
41
42 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_GET_DEVICE_STATE, data, reply, option);
43 if (error != NO_ERROR) {
44 HILOGE("BluetoothPbapPceProxy::GetDeviceState done fail, error: %d", error);
45 return ERROR;
46 }
47 return reply.ReadInt32();
48 }
49
Connect(const BluetoothRawAddress & device)50 int BluetoothPbapPceProxy::Connect(const BluetoothRawAddress &device)
51 {
52 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
53 MessageParcel data;
54 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
55 HILOGE("BluetoothPbapPceProxy::Connect WriteInterfaceToken error");
56 return ERROR;
57 }
58
59 if (!data.WriteParcelable(&device)) {
60 HILOGE("BluetoothPbapPceProxy::Connect error");
61 return ERROR;
62 }
63
64 MessageParcel reply;
65 MessageOption option {
66 MessageOption::TF_SYNC
67 };
68
69 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_CONNECT, data, reply, option);
70 if (error != NO_ERROR) {
71 HILOGE("BluetoothPbapPceProxy::Connect done fail, error: %d", error);
72 return ERROR;
73 }
74 return reply.ReadInt32();
75 }
76
PullPhoneBook(const BluetoothRawAddress & device,const BluetoothIPbapPullPhoneBookParam & param)77 int BluetoothPbapPceProxy::PullPhoneBook(const BluetoothRawAddress &device, const BluetoothIPbapPullPhoneBookParam ¶m)
78 {
79 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
80 MessageParcel data;
81 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
82 HILOGE("BluetoothPbapPceProxy::PullPhoneBook WriteInterfaceToken error");
83 return ERROR;
84 }
85
86 if (!data.WriteParcelable(&device)) {
87 HILOGE("BluetoothPbapPceProxy::PullPhoneBook error");
88 return ERROR;
89 }
90
91 if (!data.WriteParcelable(¶m)) {
92 HILOGE("BluetoothPbapPceProxy::PullPhoneBook error");
93 return ERROR;
94 }
95
96 MessageParcel reply;
97 MessageOption option {
98 MessageOption::TF_SYNC
99 };
100
101 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_PULL_PHONEBOOK, data, reply, option);
102 if (error != NO_ERROR) {
103 HILOGE("BluetoothPbapPceProxy::PullPhoneBook done fail, error: %d", error);
104 return ERROR;
105 }
106 return reply.ReadInt32();
107 }
108
SetPhoneBook(const BluetoothRawAddress & device,const std::u16string & name,int32_t flag)109 int BluetoothPbapPceProxy::SetPhoneBook(const BluetoothRawAddress &device, const std::u16string &name, int32_t flag)
110 {
111 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
112 MessageParcel data;
113 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
114 HILOGE("BluetoothPbapPceProxy::SetPhoneBook WriteInterfaceToken error");
115 return ERROR;
116 }
117
118 if (!data.WriteParcelable(&device)) {
119 HILOGE("BluetoothPbapPceProxy::SetPhoneBook error");
120 return ERROR;
121 }
122
123 if (!data.WriteString16(name)) {
124 HILOGE("BluetoothPbapPceProxy::SetPhoneBook error");
125 return ERROR;
126 }
127
128 if (!data.WriteInt32(flag)) {
129 HILOGE("BluetoothPbapPceProxy::SetPhoneBook error");
130 return ERROR;
131 }
132
133 MessageParcel reply;
134 MessageOption option {
135 MessageOption::TF_SYNC
136 };
137
138 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_SET_PHONEBOOK, data, reply, option);
139 if (error != NO_ERROR) {
140 HILOGE("BluetoothPbapPceProxy::SetPhoneBook done fail, error: %d", error);
141 return ERROR;
142 }
143 return reply.ReadInt32();
144 }
145
PullvCardListing(const BluetoothRawAddress & device,const BluetoothIPbapPullvCardListingParam & param)146 int BluetoothPbapPceProxy::PullvCardListing(const BluetoothRawAddress &device, const BluetoothIPbapPullvCardListingParam ¶m)
147 {
148 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
149 MessageParcel data;
150 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
151 HILOGE("BluetoothPbapPceProxy::PullvCardListing WriteInterfaceToken error");
152 return ERROR;
153 }
154
155 if (!data.WriteParcelable(&device)) {
156 HILOGE("BluetoothPbapPceProxy::PullvCardListing error");
157 return ERROR;
158 }
159
160 if (!data.WriteParcelable(¶m)) {
161 HILOGE("BluetoothPbapPceProxy::PullvCardListing error");
162 return ERROR;
163 }
164
165 MessageParcel reply;
166 MessageOption option {
167 MessageOption::TF_SYNC
168 };
169
170 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_PULL_VCARD_LISTING, data, reply, option);
171 if (error != NO_ERROR) {
172 HILOGE("BluetoothPbapPceProxy::PullvCardListing done fail, error: %d", error);
173 return ERROR;
174 }
175 return reply.ReadInt32();
176 }
177
PullvCardEntry(const BluetoothRawAddress & device,const BluetoothIPbapPullvCardEntryParam & param)178 int BluetoothPbapPceProxy::PullvCardEntry(const BluetoothRawAddress &device, const BluetoothIPbapPullvCardEntryParam ¶m)
179 {
180 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
181 MessageParcel data;
182 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
183 HILOGE("BluetoothPbapPceProxy::PullvCardEntry WriteInterfaceToken error");
184 return ERROR;
185 }
186
187 if (!data.WriteParcelable(&device)) {
188 HILOGE("BluetoothPbapPceProxy::PullvCardEntry error");
189 return ERROR;
190 }
191
192 if (!data.WriteParcelable(¶m)) {
193 HILOGE("BluetoothPbapPceProxy::PullvCardEntry error");
194 return ERROR;
195 }
196
197 MessageParcel reply;
198 MessageOption option {
199 MessageOption::TF_SYNC
200 };
201
202 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_PULL_VCARD_ENTRY, data, reply, option);
203 if (error != NO_ERROR) {
204 HILOGE("BluetoothPbapPceProxy::PullvCardEntry done fail, error: %d", error);
205 return ERROR;
206 }
207 return reply.ReadInt32();
208 }
209
IsDownloading(const BluetoothRawAddress & device)210 bool BluetoothPbapPceProxy::IsDownloading(const BluetoothRawAddress &device)
211 {
212 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
213 MessageParcel data;
214 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
215 HILOGE("BluetoothPbapPceProxy::IsDownloading WriteInterfaceToken error");
216 return ERROR;
217 }
218
219 if (!data.WriteParcelable(&device)) {
220 HILOGE("BluetoothPbapPceProxy::IsDownloading error");
221 return ERROR;
222 }
223
224 MessageParcel reply;
225 MessageOption option {
226 MessageOption::TF_SYNC
227 };
228
229 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_PULL_ISDOWNLOAGING, data, reply, option);
230 if (error != NO_ERROR) {
231 HILOGE("BluetoothPbapPceProxy::IsDownloading done fail, error: %d", error);
232 return ERROR;
233 }
234 return reply.ReadBool();
235 }
236
AbortDownloading(const BluetoothRawAddress & device)237 int BluetoothPbapPceProxy::AbortDownloading(const BluetoothRawAddress &device)
238 {
239 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
240 MessageParcel data;
241 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
242 HILOGE("BluetoothPbapPceProxy::AbortDownloading WriteInterfaceToken error");
243 return ERROR;
244 }
245
246 if (!data.WriteParcelable(&device)) {
247 HILOGE("BluetoothPbapPceProxy::AbortDownloading error");
248 return ERROR;
249 }
250
251 MessageParcel reply;
252 MessageOption option {
253 MessageOption::TF_SYNC
254 };
255
256 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_ABORT_DOWNLOADING, data, reply, option);
257 if (error != NO_ERROR) {
258 HILOGE("BluetoothPbapPceProxy::AbortDownloading done fail, error: %d", error);
259 return ERROR;
260 }
261 return reply.ReadInt32();
262 }
263
SetDevicePassword(const BluetoothRawAddress & device,const std::string & password,const std::string & userId)264 int BluetoothPbapPceProxy::SetDevicePassword(const BluetoothRawAddress &device, const std::string &password, const std::string &userId)
265 {
266 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
267 MessageParcel data;
268 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
269 HILOGE("BluetoothPbapPceProxy::SetDevicePassword WriteInterfaceToken error");
270 return ERROR;
271 }
272
273 if (!data.WriteParcelable(&device)) {
274 HILOGE("BluetoothPbapPceProxy::SetDevicePassword error");
275 return ERROR;
276 }
277
278 if (!data.WriteString(password)) {
279 HILOGE("BluetoothPbapPceProxy::SetDevicePassword error");
280 return ERROR;
281 }
282
283 if (!data.WriteString(userId)) {
284 HILOGE("BluetoothPbapPceProxy::SetDevicePassword error");
285 return ERROR;
286 }
287
288 MessageParcel reply;
289 MessageOption option {
290 MessageOption::TF_SYNC
291 };
292
293 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_SET_DEVICE_PASSWORD, data, reply, option);
294 if (error != NO_ERROR) {
295 HILOGE("BluetoothPbapPceProxy::SetDevicePassword done fail, error: %d", error);
296 return ERROR;
297 }
298 return reply.ReadInt32();
299 }
300
Disconnect(const BluetoothRawAddress & device)301 int BluetoothPbapPceProxy::Disconnect(const BluetoothRawAddress &device)
302 {
303 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
304 MessageParcel data;
305 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
306 HILOGE("BluetoothPbapPceProxy::Disconnect WriteInterfaceToken error");
307 return ERROR;
308 }
309
310 if (!data.WriteParcelable(&device)) {
311 HILOGE("BluetoothPbapPceProxy::Disconnect error");
312 return ERROR;
313 }
314
315 MessageParcel reply;
316 MessageOption option {
317 MessageOption::TF_SYNC
318 };
319
320 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_DISCONNECT, data, reply, option);
321 if (error != NO_ERROR) {
322 HILOGE("BluetoothPbapPceProxy::Disconnect done fail, error: %d", error);
323 return ERROR;
324 }
325 return reply.ReadInt32();
326 }
327
SetConnectionStrategy(const BluetoothRawAddress & device,int32_t strategy)328 int BluetoothPbapPceProxy::SetConnectionStrategy(const BluetoothRawAddress &device, int32_t strategy)
329 {
330 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
331 MessageParcel data;
332 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
333 HILOGE("BluetoothPbapPceProxy::SetConnectionStrategy WriteInterfaceToken error");
334 return ERROR;
335 }
336
337 if (!data.WriteParcelable(&device)) {
338 HILOGE("BluetoothPbapPceProxy::SetConnectionStrategy error");
339 return ERROR;
340 }
341
342 if (!data.WriteInt32(strategy)) {
343 HILOGE("BluetoothPbapPceProxy::SetConnectionStrategy error");
344 return ERROR;
345 }
346
347 MessageParcel reply;
348 MessageOption option {
349 MessageOption::TF_SYNC
350 };
351
352 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_SET_CONNECT_STRATEGY, data, reply, option);
353 if (error != NO_ERROR) {
354 HILOGE("BluetoothPbapPceProxy::SetConnectionStrategy done fail, error: %d", error);
355 return ERROR;
356 }
357 return reply.ReadInt32();
358 }
359
GetConnectionStrategy(const BluetoothRawAddress & device)360 int BluetoothPbapPceProxy::GetConnectionStrategy(const BluetoothRawAddress &device)
361 {
362 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
363 MessageParcel data;
364 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
365 HILOGE("BluetoothPbapPceProxy::GetConnectionStrategy WriteInterfaceToken error");
366 return ERROR;
367 }
368
369 if (!data.WriteParcelable(&device)) {
370 HILOGE("BluetoothPbapPceProxy::GetConnectionStrategy error");
371 return ERROR;
372 }
373
374 MessageParcel reply;
375 MessageOption option {
376 MessageOption::TF_SYNC
377 };
378
379 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_GET_CONNECT_STRATEGY, data, reply, option);
380 if (error != NO_ERROR) {
381 HILOGE("BluetoothPbapPceProxy::GetConnectionStrategy done fail, error: %d", error);
382 return ERROR;
383 }
384 return reply.ReadInt32();
385 }
386
GetDevicesByStates(const std::vector<int32_t> tmpStates,std::vector<BluetoothRawAddress> & rawDevices)387 void BluetoothPbapPceProxy::GetDevicesByStates(const std::vector<int32_t> tmpStates, std::vector<BluetoothRawAddress> &rawDevices)
388 {
389 HILOGI("BluetoothPbapPceProxy::GetServices start");
390 MessageParcel data;
391 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
392 HILOGE("BluetoothPbapPceProxy::GetServices WriteInterfaceToken error");
393 return;
394 }
395 if (!data.WriteInt32Vector(tmpStates)) {
396 HILOGE("BluetoothPbapPceProxy::GetServices transport error");
397 return;
398 }
399 MessageParcel reply;
400 MessageOption option {
401 MessageOption::TF_SYNC
402 };
403 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_GET_DEVICES_BY_STATE, data, reply, option);
404 if (error != NO_ERROR) {
405 HILOGE("BluetoothPbapPceProxy::GetServices done fail, error: %d", error);
406 }
407 int dev_num = reply.ReadInt32();
408 for (int i = dev_num; i > 0; i--) {
409 std::unique_ptr<BluetoothRawAddress> dev(reply.ReadParcelable<BluetoothRawAddress>());
410 rawDevices.push_back(*dev);
411 }
412 }
413
RegisterObserver(const sptr<IBluetoothPbapPceObserver> & observer)414 void BluetoothPbapPceProxy::RegisterObserver(const sptr<IBluetoothPbapPceObserver> &observer)
415 {
416 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
417 MessageParcel data;
418 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
419 HILOGE("BluetoothPbapPceProxy::RegisterObserver WriteInterfaceToken error");
420 return;
421 }
422 if (!data.WriteRemoteObject(observer->AsObject())) {
423 HILOGE("BluetoothPbapPceProxy::RegisterObserver error");
424 return;
425 }
426
427 MessageParcel reply;
428 MessageOption option {
429 MessageOption::TF_SYNC
430 };
431 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_REGISTER_OBSERVER, data, reply, option);
432 if (error != NO_ERROR) {
433 HILOGE("BluetoothPbapPceProxy::RegisterObserver done fail, error: %d", error);
434 return;
435 }
436 return;
437 }
438
DeregisterObserver(const sptr<IBluetoothPbapPceObserver> & observer)439 void BluetoothPbapPceProxy::DeregisterObserver(const sptr<IBluetoothPbapPceObserver> &observer)
440 {
441 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
442 MessageParcel data;
443 if (!data.WriteInterfaceToken(BluetoothPbapPceProxy::GetDescriptor())) {
444 HILOGE("BluetoothPbapPceProxy::RegisterObserver WriteInterfaceToken error");
445 return;
446 }
447 if (!data.WriteRemoteObject(observer->AsObject())) {
448 HILOGE("BluetoothPbapPceProxy::RegisterObserver error");
449 return;
450 }
451
452 MessageParcel reply;
453 MessageOption option {
454 MessageOption::TF_SYNC
455 };
456 int error = Remote()->SendRequest(IBluetoothPbapPce::Code::PBAP_PCE_DEREGISTER_OBSERVER, data, reply, option);
457 if (error != NO_ERROR) {
458 HILOGE("BluetoothPbapPceProxy::RegisterObserver done fail, error: %d", error);
459 return;
460 }
461 return;
462 }
463 } // namespace Bluetooth
464 } // namespace OHOS