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