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 <fstream>
17 #include <list>
18 #include <mutex>
19 #include <string>
20 #include "bluetooth_pbap_client.h"
21 #include "bluetooth_host.h"
22 #include "bluetooth_pbap_pce_proxy.h"
23 #include "bluetooth_remote_device.h"
24 #include "bluetooth_pbap_pce_observer_stub.h"
25 #include "bluetooth_observer_list.h"
26 #include "bluetooth_log.h"
27 #include "iservice_registry.h"
28 #include "raw_address.h"
29 #include "system_ability_definition.h"
30 namespace OHOS {
31 namespace Bluetooth {
GetName() const32 std::u16string PbapPullPhoneBookParam::GetName() const
33 {
34 return name_;
35 }
36
SetName(const std::u16string & name)37 void PbapPullPhoneBookParam::SetName(const std::u16string &name)
38 {
39 name_ = name;
40 specifiedBitset_ |= NAME;
41 }
42
GetPropertySelector() const43 uint64_t PbapPullPhoneBookParam::GetPropertySelector() const
44 {
45 return propertySelector_;
46 }
47
SetPropertySelector(uint64_t propertySelector)48 void PbapPullPhoneBookParam::SetPropertySelector(uint64_t propertySelector)
49 {
50 propertySelector_ = propertySelector;
51 specifiedBitset_ |= PROPERTY_SELECTOR;
52 }
53
GetFormat() const54 uint8_t PbapPullPhoneBookParam::GetFormat() const
55 {
56 return format_;
57 }
58
SetFormat(uint8_t format)59 void PbapPullPhoneBookParam::SetFormat(uint8_t format)
60 {
61 format_ = format;
62 specifiedBitset_ |= FORMAT;
63 }
64
GetMaxListCount() const65 uint16_t PbapPullPhoneBookParam::GetMaxListCount() const
66 {
67 return maxListCount_;
68 }
69
SetMaxListCount(uint16_t maxListCount)70 void PbapPullPhoneBookParam::SetMaxListCount(uint16_t maxListCount)
71 {
72 maxListCount_ = maxListCount;
73 specifiedBitset_ |= MAX_LIST_COUNT;
74 }
75
GetListStartOffset() const76 uint16_t PbapPullPhoneBookParam::GetListStartOffset() const
77 {
78 return listStartOffset_;
79 }
80
SetListStartOffset(uint16_t listStartOffset)81 void PbapPullPhoneBookParam::SetListStartOffset(uint16_t listStartOffset)
82 {
83 listStartOffset_ = listStartOffset;
84 specifiedBitset_ |= LIST_START_OFFSET;
85 }
86
GetResetNewMissedCalls() const87 uint8_t PbapPullPhoneBookParam::GetResetNewMissedCalls() const
88 {
89 return resetNewMissedCalls_;
90 }
91
SetResetNewMissedCalls(uint8_t resetNewMissedCalls)92 void PbapPullPhoneBookParam::SetResetNewMissedCalls(uint8_t resetNewMissedCalls)
93 {
94 resetNewMissedCalls_ = resetNewMissedCalls;
95 specifiedBitset_ |= RESET_NEW_MISSED_CALLS;
96 }
97
GetvCardSelector() const98 uint64_t PbapPullPhoneBookParam::GetvCardSelector() const
99 {
100 return vCardSelector_;
101 }
102
SetvCardSelector(uint64_t vCardSelector)103 void PbapPullPhoneBookParam::SetvCardSelector(uint64_t vCardSelector)
104 {
105 vCardSelector_ = vCardSelector;
106 specifiedBitset_ |= VCARD_SELECTOR;
107 }
108
GetvCardSelectorOp() const109 uint8_t PbapPullPhoneBookParam::GetvCardSelectorOp() const
110 {
111 return vCardSelectorOp_;
112 }
113
SetvCardSelectorOp(uint8_t vCardSelectorOp)114 void PbapPullPhoneBookParam::SetvCardSelectorOp(uint8_t vCardSelectorOp)
115 {
116 vCardSelectorOp_ = vCardSelectorOp;
117 specifiedBitset_ |= VCARD_SELECTOROP;
118 }
119
GetSpecifiedBitset() const120 uint64_t PbapPullPhoneBookParam::GetSpecifiedBitset() const
121 {
122 return specifiedBitset_;
123 }
124
SetSpecifiedBitset(uint64_t specifiedBitset)125 void PbapPullPhoneBookParam::SetSpecifiedBitset(uint64_t specifiedBitset)
126 {
127 specifiedBitset_ = specifiedBitset;
128 }
129
IsSpecified(int bitSpecified) const130 bool PbapPullPhoneBookParam::IsSpecified(int bitSpecified) const
131 {
132 if (bitSpecified & specifiedBitset_) {
133 return true;
134 } else {
135 return false;
136 }
137 }
138
139 // implement PbapPullvCardListingParam
GetName() const140 std::u16string PbapPullvCardListingParam::GetName() const
141 {
142 return name_;
143 }
144
SetName(const std::u16string & name)145 void PbapPullvCardListingParam::SetName(const std::u16string &name)
146 {
147 name_ = name;
148 specifiedBitset_ |= NAME;
149 }
150
GetOrder() const151 uint8_t PbapPullvCardListingParam::GetOrder() const
152 {
153 return order_;
154 }
155
SetOrder(uint8_t order)156 void PbapPullvCardListingParam::SetOrder(uint8_t order)
157 {
158 order_ = order;
159 specifiedBitset_ |= ORDER;
160 }
161
GetSearchValue() const162 std::string PbapPullvCardListingParam::GetSearchValue() const
163 {
164 return searchValue_;
165 }
166
SetSearchValue(const std::string & searchValue)167 void PbapPullvCardListingParam::SetSearchValue(const std::string &searchValue)
168 {
169 searchValue_ = searchValue;
170 specifiedBitset_ |= SEARCH_VALUE;
171 }
172
GetSearchProperty() const173 uint8_t PbapPullvCardListingParam::GetSearchProperty() const
174 {
175 return searchProperty_;
176 }
177
SetSearchProperty(uint8_t searchProperty)178 void PbapPullvCardListingParam::SetSearchProperty(uint8_t searchProperty)
179 {
180 searchProperty_ = searchProperty;
181 specifiedBitset_ |= SEARCH_PROPERTY;
182 }
183
GetMaxListCount() const184 uint16_t PbapPullvCardListingParam::GetMaxListCount() const
185 {
186 return maxListCount_;
187 }
188
SetMaxListCount(uint16_t maxListCount)189 void PbapPullvCardListingParam::SetMaxListCount(uint16_t maxListCount)
190 {
191 maxListCount_ = maxListCount;
192 specifiedBitset_ |= MAX_LIST_COUNT;
193 }
194
GetListStartOffset() const195 uint16_t PbapPullvCardListingParam::GetListStartOffset() const
196 {
197 return listStartOffset_;
198 }
199
SetListStartOffset(uint16_t listStartOffset)200 void PbapPullvCardListingParam::SetListStartOffset(uint16_t listStartOffset)
201 {
202 listStartOffset_ = listStartOffset;
203 specifiedBitset_ |= LIST_START_OFFSET;
204 }
205
GetResetNewMissedCalls() const206 uint8_t PbapPullvCardListingParam::GetResetNewMissedCalls() const
207 {
208 return resetNewMissedCalls_;
209 }
210
SetResetNewMissedCalls(uint8_t resetNewMissedCalls)211 void PbapPullvCardListingParam::SetResetNewMissedCalls(uint8_t resetNewMissedCalls)
212 {
213 resetNewMissedCalls_ = resetNewMissedCalls;
214 specifiedBitset_ |= RESET_NEW_MISSED_CALLS;
215 }
216
GetvCardSelector() const217 uint64_t PbapPullvCardListingParam::GetvCardSelector() const
218 {
219 return vCardSelector_;
220 }
221
SetvCardSelector(uint64_t vCardSelector)222 void PbapPullvCardListingParam::SetvCardSelector(uint64_t vCardSelector)
223 {
224 vCardSelector_ = vCardSelector;
225 specifiedBitset_ |= VCARD_SELECTOR;
226 }
227
GetvCardSelectorOp() const228 uint8_t PbapPullvCardListingParam::GetvCardSelectorOp() const
229 {
230 return vCardSelectorOp_;
231 }
232
SetvCardSelectorOp(uint8_t vCardSelectorOp)233 void PbapPullvCardListingParam::SetvCardSelectorOp(uint8_t vCardSelectorOp)
234 {
235 vCardSelectorOp_ = vCardSelectorOp;
236 specifiedBitset_ |= VCARD_SELECTOROP;
237 }
238
GetSpecifiedBitset() const239 uint64_t PbapPullvCardListingParam::GetSpecifiedBitset() const
240 {
241 return specifiedBitset_;
242 }
243
SetSpecifiedBitset(uint64_t specifiedBitset)244 void PbapPullvCardListingParam::SetSpecifiedBitset(uint64_t specifiedBitset)
245 {
246 specifiedBitset_ = specifiedBitset;
247 }
248
IsSpecified(int bitSpecified) const249 bool PbapPullvCardListingParam::IsSpecified(int bitSpecified) const
250 {
251 if (bitSpecified & specifiedBitset_) {
252 return true;
253 } else {
254 return false;
255 }
256 }
257
258 // implement PbapPullvCardEntryParam
GetName() const259 std::u16string PbapPullvCardEntryParam::GetName() const
260 {
261 return name_;
262 }
263
SetName(const std::u16string & name)264 void PbapPullvCardEntryParam::SetName(const std::u16string &name)
265 {
266 name_ = name;
267 specifiedBitset_ |= NAME;
268 }
269
GetPropertySelector() const270 uint64_t PbapPullvCardEntryParam::GetPropertySelector() const
271 {
272 return propertySelector_;
273 }
274
SetPropertySelector(uint64_t propertySelector)275 void PbapPullvCardEntryParam::SetPropertySelector(uint64_t propertySelector)
276 {
277 propertySelector_ = propertySelector;
278 specifiedBitset_ |= PROPERTY_SELECTOR;
279 }
280
GetFormat() const281 uint8_t PbapPullvCardEntryParam::GetFormat() const
282 {
283 return format_;
284 }
285
SetFormat(uint8_t format)286 void PbapPullvCardEntryParam::SetFormat(uint8_t format)
287 {
288 format_ = format;
289 specifiedBitset_ |= FORMAT;
290 }
291
GetSpecifiedBitset() const292 uint64_t PbapPullvCardEntryParam::GetSpecifiedBitset() const
293 {
294 return specifiedBitset_;
295 }
296
SetSpecifiedBitset(uint64_t specifiedBitset)297 void PbapPullvCardEntryParam::SetSpecifiedBitset(uint64_t specifiedBitset)
298 {
299 specifiedBitset_ = specifiedBitset;
300 }
301
IsSpecified(int bitSpecified) const302 bool PbapPullvCardEntryParam::IsSpecified(int bitSpecified) const
303 {
304 if (bitSpecified & specifiedBitset_) {
305 return true;
306 } else {
307 return false;
308 }
309 }
310
311 class BluetoothIPbapPullPhoneBookParamAdapter {
312 public:
313 explicit BluetoothIPbapPullPhoneBookParamAdapter() = default;
BluetoothIPbapPullPhoneBookParamAdapter(const PbapPullPhoneBookParam & other)314 explicit BluetoothIPbapPullPhoneBookParamAdapter(const PbapPullPhoneBookParam &other)
315 {
316 data_.SetName(other.GetName());
317 data_.SetPropertySelector(other.GetPropertySelector());
318 data_.SetFormat(other.GetFormat());
319 data_.SetMaxListCount(other.GetMaxListCount());
320 data_.SetListStartOffset(other.GetListStartOffset());
321 data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
322 data_.SetvCardSelector(other.GetvCardSelector());
323 data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
324 data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
325 }
326 virtual ~BluetoothIPbapPullPhoneBookParamAdapter() = default;
operator BluetoothIPbapPullPhoneBookParam() const327 operator BluetoothIPbapPullPhoneBookParam() const
328 {
329 return data_;
330 }
331
332 private:
333 BluetoothIPbapPullPhoneBookParam data_;
334 };
335
336 class BluetoothIPbapPullvCardListingParamAdapter {
337 public:
338 explicit BluetoothIPbapPullvCardListingParamAdapter() = default;
BluetoothIPbapPullvCardListingParamAdapter(const PbapPullvCardListingParam & other)339 explicit BluetoothIPbapPullvCardListingParamAdapter(const PbapPullvCardListingParam &other)
340 {
341 data_.SetName(other.GetName());
342 data_.SetOrder(other.GetOrder());
343 data_.SetSearchValue(other.GetSearchValue());
344 data_.SetSearchProperty(other.GetSearchProperty());
345 data_.SetMaxListCount(other.GetMaxListCount());
346 data_.SetListStartOffset(other.GetListStartOffset());
347 data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
348 data_.SetvCardSelector(other.GetvCardSelector());
349 data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
350 data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
351 }
352 virtual ~BluetoothIPbapPullvCardListingParamAdapter() = default;
operator BluetoothIPbapPullvCardListingParam() const353 operator BluetoothIPbapPullvCardListingParam() const
354 {
355 return data_;
356 }
357
358 private:
359 BluetoothIPbapPullvCardListingParam data_;
360 };
361
362 class BluetoothIPbapPullvCardEntryParamAdapter {
363 public:
364 explicit BluetoothIPbapPullvCardEntryParamAdapter() = default;
BluetoothIPbapPullvCardEntryParamAdapter(const PbapPullvCardEntryParam & other)365 explicit BluetoothIPbapPullvCardEntryParamAdapter(const PbapPullvCardEntryParam &other)
366 {
367 data_.SetName(other.GetName());
368 data_.SetPropertySelector(other.GetPropertySelector());
369 data_.SetFormat(other.GetFormat());
370 data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
371 }
372 virtual ~BluetoothIPbapPullvCardEntryParamAdapter() = default;
operator BluetoothIPbapPullvCardEntryParam() const373 operator BluetoothIPbapPullvCardEntryParam() const
374 {
375 return data_;
376 }
377
378 private:
379 BluetoothIPbapPullvCardEntryParam data_;
380 };
381
382 class PbapPhoneBookDataAdapter {
383 public:
PbapPhoneBookDataAdapter(const BluetoothIPbapPhoneBookData & other)384 explicit PbapPhoneBookDataAdapter(const BluetoothIPbapPhoneBookData &other)
385 {
386 data_.phoneBookSize_ = other.phoneBookSize_;
387 data_.primaryFolderVersion_ = other.primaryFolderVersion_;
388 data_.secondaryFolderVersion_ = other.secondaryFolderVersion_;
389 data_.databaseIdentifier_ = other.databaseIdentifier_;
390 data_.result_ = other.result_;
391 const_cast<BluetoothIPbapPhoneBookData &>(other).Load(data_.result_);
392 }
operator PbapPhoneBookData() const393 operator PbapPhoneBookData() const
394 {
395 return data_;
396 }
397
398 private:
399 PbapPhoneBookData data_ {};
400 };
401
402 class BluetoothPbapPceObserverImpl : public BluetoothPbapPceObserverStub {
403 public:
404 BluetoothPbapPceObserverImpl() = default;
405 ~BluetoothPbapPceObserverImpl() = default;
SetObserver(BluetoothObserverList<PbapClientObserver> * frameworkObserverListPtr)406 void SetObserver(BluetoothObserverList<PbapClientObserver> *frameworkObserverListPtr)
407 {
408 frameworkObserverListPtr_ = frameworkObserverListPtr;
409 if (frameworkObserverListPtr_ == nullptr) {
410 HILOGI("frameworkObserverListPtr_ is nullptr error.");
411 }
412 }
413
OnServiceConnectionStateChanged(const BluetoothRawAddress & device,int state)414 void OnServiceConnectionStateChanged(const BluetoothRawAddress &device, int state) override
415 {
416 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
417 if (frameworkObserverListPtr_ != nullptr) {
418 frameworkObserverListPtr_->ForEach([device, state](std::shared_ptr<PbapClientObserver> observer) {
419 BluetoothRemoteDevice dev(device.GetAddress(), 0);
420 observer->OnServiceConnectionStateChanged(dev, state);
421 });
422 }
423 }
424
OnServicePasswordRequired(const BluetoothRawAddress & device,const::std::vector<uint8_t> & description,int8_t charset,bool fullAccess)425 void OnServicePasswordRequired(const BluetoothRawAddress &device,
426 const ::std::vector<uint8_t> &description, int8_t charset, bool fullAccess) override
427 {
428 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
429 if (frameworkObserverListPtr_ != nullptr) {
430 frameworkObserverListPtr_->ForEach(
431 [device, description, charset, fullAccess](std::shared_ptr<PbapClientObserver> observer) {
432 BluetoothRemoteDevice dev(device.GetAddress(), 0);
433 observer->OnServicePasswordRequired(dev, description, charset, fullAccess);
434 });
435 }
436 }
437
OnActionCompleted(const BluetoothRawAddress & device,int respCode,int actionType,const BluetoothIPbapPhoneBookData & result)438 void OnActionCompleted(const BluetoothRawAddress &device,
439 int respCode, int actionType, const BluetoothIPbapPhoneBookData &result) override
440 {
441 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
442 if (frameworkObserverListPtr_ != nullptr) {
443 frameworkObserverListPtr_->ForEach([device,
444 respCode, actionType, result](std::shared_ptr<PbapClientObserver> observer) {
445 BluetoothRemoteDevice dev(device.GetAddress(), 0);
446 observer->OnActionCompleted(dev, respCode, actionType, PbapPhoneBookDataAdapter(result));
447 });
448 }
449 }
450
451 private:
452 BluetoothObserverList<PbapClientObserver> *frameworkObserverListPtr_ {nullptr};
453 };
454
455 struct PbapClient::impl {
456 sptr<IBluetoothPbapPce> proxy_;
457 class BluetoothPbapPceDeathRecipient;
458 sptr<BluetoothPbapPceDeathRecipient> deathRecipient_ = nullptr;
459 impl();
~implOHOS::Bluetooth::PbapClient::impl460 ~impl()
461 {
462 if (proxy_ != nullptr) {
463 proxy_->DeregisterObserver(serviceObserverImpl_);
464 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
465 }
466 }
RegisterObserverOHOS::Bluetooth::PbapClient::impl467 void RegisterObserver(std::shared_ptr<PbapClientObserver> &observer)
468 {
469 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
470 frameworkObserverList_.Register(observer);
471 }
472
DeregisterObserverOHOS::Bluetooth::PbapClient::impl473 void DeregisterObserver(std::shared_ptr<PbapClientObserver> &observer)
474 {
475 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
476 frameworkObserverList_.Deregister(observer);
477 }
478
GetDeviceStateOHOS::Bluetooth::PbapClient::impl479 int GetDeviceState(const BluetoothRemoteDevice &device)
480 {
481 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
482 if (proxy_ != nullptr) {
483 return proxy_->GetDeviceState(bluetooth::RawAddress(device.GetDeviceAddr()));
484 }
485 return 0;
486 }
487
GetDevicesByStatesOHOS::Bluetooth::PbapClient::impl488 std::vector<BluetoothRemoteDevice> GetDevicesByStates(const std::vector<int> &states)
489 {
490 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
491 std::vector<BluetoothRemoteDevice> remoteDevices;
492 std::vector<BluetoothRawAddress> rawDevices;
493 std::vector<int32_t> tmpStates;
494 for (int state : states) {
495 tmpStates.push_back((int32_t)state);
496 }
497 if (proxy_ != nullptr) {
498 proxy_->GetDevicesByStates(tmpStates, rawDevices);
499 }
500 for (BluetoothRawAddress rawDevice : rawDevices) {
501 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
502 remoteDevices.push_back(remoteDevice);
503 }
504 return remoteDevices;
505 }
506
GetConnectedDevicesOHOS::Bluetooth::PbapClient::impl507 std::vector<BluetoothRemoteDevice> GetConnectedDevices()
508 {
509 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
510 std::vector<BluetoothRemoteDevice> remoteDevices;
511 std::vector<BluetoothRawAddress> rawDevices;
512 std::vector<int> states {static_cast<int>(BTConnectState::CONNECTED)};
513 if (proxy_ != nullptr) {
514 proxy_->GetDevicesByStates(states, rawDevices);
515 }
516 for (BluetoothRawAddress rawDevice : rawDevices) {
517 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
518 remoteDevices.push_back(remoteDevice);
519 }
520 return remoteDevices;
521 }
522
ConnectOHOS::Bluetooth::PbapClient::impl523 bool Connect(const BluetoothRemoteDevice &device)
524 {
525 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
526 if (proxy_ != nullptr) {
527 int ret = RET_NO_ERROR;
528 ret = proxy_->Connect(bluetooth::RawAddress(device.GetDeviceAddr()));
529 return ret == RET_NO_ERROR;
530 } else {
531 return false;
532 }
533 }
534
PullPhoneBookOHOS::Bluetooth::PbapClient::impl535 int PullPhoneBook(const BluetoothRemoteDevice &device, const BluetoothIPbapPullPhoneBookParam ¶m)
536 {
537 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
538 int ret = -1;
539 if (proxy_ != nullptr) {
540 ret = proxy_->PullPhoneBook(bluetooth::RawAddress(device.GetDeviceAddr()), param);
541 }
542 return ret;
543 }
544
SetPhoneBookOHOS::Bluetooth::PbapClient::impl545 int SetPhoneBook(const BluetoothRemoteDevice &device, const std::u16string &name, int flag)
546 {
547 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
548 int ret = -1;
549 if (proxy_ != nullptr) {
550 ret = proxy_->SetPhoneBook(
551 bluetooth::RawAddress(device.GetDeviceAddr()), name.c_str(), flag);
552 }
553 return ret;
554 }
555
PullvCardListingOHOS::Bluetooth::PbapClient::impl556 int PullvCardListing(const BluetoothRemoteDevice &device, const BluetoothIPbapPullvCardListingParam ¶m)
557 {
558 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
559 int ret = -1;
560 if (proxy_ != nullptr) {
561 proxy_->PullvCardListing(bluetooth::RawAddress(device.GetDeviceAddr()), param);
562 }
563 return ret;
564 }
565
PullvCardEntryOHOS::Bluetooth::PbapClient::impl566 int PullvCardEntry(const BluetoothRemoteDevice &device, const BluetoothIPbapPullvCardEntryParam ¶m)
567 {
568 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
569 int ret = -1;
570 if (proxy_ != nullptr) {
571 ret = proxy_->PullvCardEntry(bluetooth::RawAddress(device.GetDeviceAddr()), param);
572 }
573 return ret;
574 }
575
IsDownloadingOHOS::Bluetooth::PbapClient::impl576 bool IsDownloading(const BluetoothRemoteDevice &device)
577 {
578 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
579 bool ret = false;
580 if (proxy_ != nullptr) {
581 ret = proxy_->IsDownloading(bluetooth::RawAddress(device.GetDeviceAddr()));
582 }
583 return ret;
584 }
585
AbortDownloadingOHOS::Bluetooth::PbapClient::impl586 int AbortDownloading(const BluetoothRemoteDevice &device)
587 {
588 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
589 int ret = -1;
590 if (proxy_ != nullptr) {
591 ret = proxy_->AbortDownloading(bluetooth::RawAddress(device.GetDeviceAddr()));
592 }
593 return ret;
594 }
595
DisconnectOHOS::Bluetooth::PbapClient::impl596 bool Disconnect(const BluetoothRemoteDevice &device)
597 {
598 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
599 if (proxy_ != nullptr) {
600 int ret = RET_NO_ERROR;
601 ret = proxy_->Disconnect(bluetooth::RawAddress(device.GetDeviceAddr()));
602 return ret == RET_NO_ERROR;
603 } else {
604 return false;
605 }
606 }
607
SetConnectionStrategyOHOS::Bluetooth::PbapClient::impl608 bool SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
609 {
610 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
611 if (proxy_ != nullptr) {
612 int ret = RET_NO_ERROR;
613 ret = proxy_->SetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()), strategy);
614 return ret == RET_NO_ERROR;
615 } else {
616 return false;
617 }
618 }
619
GetConnectionStrategyOHOS::Bluetooth::PbapClient::impl620 int GetConnectionStrategy(const BluetoothRemoteDevice &device)
621 {
622 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
623 int ret = (int)BTStrategyType::CONNECTION_UNKNOWN;
624 if (proxy_ != nullptr) {
625 ret = proxy_->GetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()));
626 }
627 return ret;
628 }
629
SetDevicePasswordOHOS::Bluetooth::PbapClient::impl630 int SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, const std::string &userId)
631 {
632 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
633 int ret = -1;
634 if (proxy_ != nullptr) {
635 ret = proxy_->SetDevicePassword(bluetooth::RawAddress(device.GetDeviceAddr()),
636 password.c_str(), userId.c_str());
637 }
638 return ret;
639 }
640
641 private:
642 std::mutex mutex_;
643 BluetoothObserverList<PbapClientObserver> frameworkObserverList_ {};
644 sptr<BluetoothPbapPceObserverImpl> serviceObserverImpl_;
645 };
646
647 class PbapClient::impl::BluetoothPbapPceDeathRecipient final : public IRemoteObject::DeathRecipient {
648 public:
BluetoothPbapPceDeathRecipient(PbapClient::impl & pbapPce)649 BluetoothPbapPceDeathRecipient(PbapClient::impl &pbapPce) : pbapPce_(pbapPce) {};
650 ~BluetoothPbapPceDeathRecipient() final = default;
651 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothPbapPceDeathRecipient);
652
OnRemoteDied(const wptr<IRemoteObject> & remote)653 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
654 {
655 HILOGI("PbapClient::impl::BluetoothPbapPceDeathRecipient::OnRemoteDied starts");
656 pbapPce_.proxy_->AsObject()->RemoveDeathRecipient(pbapPce_.deathRecipient_);
657 pbapPce_.proxy_ = nullptr;
658 }
659
660 private:
661 PbapClient::impl &pbapPce_;
662 };
impl()663 PbapClient::impl::impl()
664 {
665 serviceObserverImpl_ = new BluetoothPbapPceObserverImpl();
666 serviceObserverImpl_->SetObserver(&frameworkObserverList_); // bind frameworkObserverList_
667 HILOGI("PbapClient::impl::impl starts");
668 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
669 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
670
671 if (!hostRemote) {
672 HILOGI("PbapClient::impl:impl() failed: no hostRemote");
673 return;
674 }
675 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
676 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_PBAP_PCE);
677
678 if (!remote) {
679 HILOGE("PbapClient::impl:impl() failed: no remote");
680 return;
681 }
682 HILOGI("PbapClient::impl:impl() remote obtained");
683
684 proxy_ = iface_cast<IBluetoothPbapPce>(remote);
685
686 deathRecipient_ = new BluetoothPbapPceDeathRecipient(*this);
687 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
688 if (proxy_ != nullptr) {
689 proxy_->RegisterObserver(serviceObserverImpl_);
690 }
691 }
GetProfile()692 PbapClient *PbapClient::GetProfile()
693 {
694 static PbapClient instance;
695 return &instance;
696 }
697
PbapClient()698 PbapClient::PbapClient()
699 {
700 pimpl = std::make_unique<impl>();
701 }
702
~PbapClient()703 PbapClient::~PbapClient()
704 {}
705
RegisterObserver(PbapClientObserver * observer)706 void PbapClient::RegisterObserver(PbapClientObserver *observer)
707 {
708 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
709 std::shared_ptr<PbapClientObserver> pointer(observer, [](PbapClientObserver *) {});
710 return pimpl->RegisterObserver(pointer);
711 }
712
DeregisterObserver(PbapClientObserver * observer)713 void PbapClient::DeregisterObserver(PbapClientObserver *observer)
714 {
715 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
716 std::shared_ptr<PbapClientObserver> pointer(observer, [](PbapClientObserver *) {});
717 return pimpl->DeregisterObserver(pointer);
718 }
719
GetDeviceState(const BluetoothRemoteDevice & device)720 int PbapClient::GetDeviceState(const BluetoothRemoteDevice &device)
721 {
722 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
723 if (!IS_BT_ENABLED()) {
724 return (int)BTConnectState::DISCONNECTED;
725 }
726 if (!device.IsValidBluetoothRemoteDevice()) {
727 return (int)BTConnectState::DISCONNECTED;
728 }
729 return pimpl->GetDeviceState(device);
730 }
731
GetDevicesByStates(const std::vector<int> & states)732 std::vector<BluetoothRemoteDevice> PbapClient::GetDevicesByStates(const std::vector<int> &states)
733 {
734 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
735 if (!IS_BT_ENABLED()) {
736 return std::vector<BluetoothRemoteDevice>();
737 }
738 return pimpl->GetDevicesByStates(states);
739 }
740
GetConnectedDevices()741 std::vector<BluetoothRemoteDevice> PbapClient::GetConnectedDevices()
742 {
743 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
744 if (!IS_BT_ENABLED()) {
745 return std::vector<BluetoothRemoteDevice>();
746 }
747 return pimpl->GetConnectedDevices();
748 }
749
Connect(const BluetoothRemoteDevice & device)750 bool PbapClient::Connect(const BluetoothRemoteDevice &device)
751 {
752 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
753 if (!IS_BT_ENABLED()) {
754 return false;
755 }
756 if (!device.IsValidBluetoothRemoteDevice()) {
757 return false;
758 }
759 return pimpl->Connect(device);
760 }
761
Disconnect(const BluetoothRemoteDevice & device)762 bool PbapClient::Disconnect(const BluetoothRemoteDevice &device)
763 {
764 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
765 if (!IS_BT_ENABLED()) {
766 return false;
767 }
768 if (!device.IsValidBluetoothRemoteDevice()) {
769 return false;
770 }
771 return pimpl->Disconnect(device);
772 }
773
SetConnectionStrategy(const BluetoothRemoteDevice & device,int strategy)774 bool PbapClient::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
775 {
776 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
777 if (!IS_BT_ENABLED()) {
778 return false;
779 }
780 if (!device.IsValidBluetoothRemoteDevice()) {
781 return false;
782 }
783 return pimpl->SetConnectionStrategy(device, strategy);
784 }
785
GetConnectionStrategy(const BluetoothRemoteDevice & device)786 int PbapClient::GetConnectionStrategy(const BluetoothRemoteDevice &device)
787 {
788 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
789 if (!IS_BT_ENABLED()) {
790 return (int)BTStrategyType::CONNECTION_FORBIDDEN;
791 }
792 if (!device.IsValidBluetoothRemoteDevice()) {
793 return (int)BTStrategyType::CONNECTION_FORBIDDEN;
794 }
795 return pimpl->GetConnectionStrategy(device);
796 }
797
SetDevicePassword(const BluetoothRemoteDevice & device,const std::string & password,const std::string & userId)798 int PbapClient::SetDevicePassword(
799 const BluetoothRemoteDevice &device, const std::string &password, const std::string &userId)
800 {
801 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
802 if (!IS_BT_ENABLED()) {
803 return RET_BAD_STATUS;
804 }
805 if (!device.IsValidBluetoothRemoteDevice()) {
806 return RET_BAD_PARAM;
807 }
808 return pimpl->SetDevicePassword(device, password, userId);
809 }
810
PullPhoneBook(const BluetoothRemoteDevice & device,const PbapPullPhoneBookParam & param)811 int PbapClient::PullPhoneBook(const BluetoothRemoteDevice &device, const PbapPullPhoneBookParam ¶m)
812 {
813 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
814 if (!IS_BT_ENABLED()) {
815 return RET_BAD_STATUS;
816 }
817 if (!device.IsValidBluetoothRemoteDevice()) {
818 return RET_BAD_PARAM;
819 }
820 return pimpl->PullPhoneBook(device, BluetoothIPbapPullPhoneBookParamAdapter(param));
821 }
822
SetPhoneBook(const BluetoothRemoteDevice & device,const std::u16string & name,int flag)823 int PbapClient::SetPhoneBook(const BluetoothRemoteDevice &device, const std::u16string &name, int flag)
824 {
825 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
826 if (!IS_BT_ENABLED()) {
827 return RET_BAD_STATUS;
828 }
829 if (!device.IsValidBluetoothRemoteDevice()) {
830 return RET_BAD_PARAM;
831 }
832 return pimpl->SetPhoneBook(device, name, flag);
833 }
834
PullvCardListing(const BluetoothRemoteDevice & device,const PbapPullvCardListingParam & param)835 int PbapClient::PullvCardListing(const BluetoothRemoteDevice &device, const PbapPullvCardListingParam ¶m)
836 {
837 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
838 if (!IS_BT_ENABLED()) {
839 return RET_BAD_STATUS;
840 }
841 if (!device.IsValidBluetoothRemoteDevice()) {
842 return RET_BAD_PARAM;
843 }
844 return pimpl->PullvCardListing(device, BluetoothIPbapPullvCardListingParamAdapter(param));
845 }
846
PullvCardEntry(const BluetoothRemoteDevice & device,const PbapPullvCardEntryParam & param)847 int PbapClient::PullvCardEntry(const BluetoothRemoteDevice &device, const PbapPullvCardEntryParam ¶m)
848 {
849 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
850 if (!IS_BT_ENABLED()) {
851 return RET_BAD_STATUS;
852 }
853 if (!device.IsValidBluetoothRemoteDevice()) {
854 return RET_BAD_PARAM;
855 }
856 return pimpl->PullvCardEntry(device, BluetoothIPbapPullvCardEntryParamAdapter(param));
857 }
858
IsDownloading(const BluetoothRemoteDevice & device)859 bool PbapClient::IsDownloading(const BluetoothRemoteDevice &device)
860 {
861 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
862 if (!IS_BT_ENABLED()) {
863 return false;
864 }
865 if (!device.IsValidBluetoothRemoteDevice()) {
866 return false;
867 }
868 return pimpl->IsDownloading(device);
869 }
870
AbortDownloading(const BluetoothRemoteDevice & device)871 int PbapClient::AbortDownloading(const BluetoothRemoteDevice &device)
872 {
873 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
874 if (!IS_BT_ENABLED()) {
875 return RET_BAD_STATUS;
876 }
877 if (!device.IsValidBluetoothRemoteDevice()) {
878 return RET_BAD_PARAM;
879 }
880 return pimpl->AbortDownloading(device);
881 }
882 } // namespace Bluetooth
883 } // namespace OHOS