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 "bluetooth_utils.h"
28 #include "iservice_registry.h"
29 #include "raw_address.h"
30 #include "system_ability_definition.h"
31 namespace OHOS {
32 namespace Bluetooth {
GetName() const33 std::u16string PbapPullPhoneBookParam::GetName() const
34 {
35 return name_;
36 }
37
SetName(const std::u16string & name)38 void PbapPullPhoneBookParam::SetName(const std::u16string &name)
39 {
40 name_ = name;
41 specifiedBitset_ |= NAME;
42 }
43
GetPropertySelector() const44 uint64_t PbapPullPhoneBookParam::GetPropertySelector() const
45 {
46 return propertySelector_;
47 }
48
SetPropertySelector(uint64_t propertySelector)49 void PbapPullPhoneBookParam::SetPropertySelector(uint64_t propertySelector)
50 {
51 propertySelector_ = propertySelector;
52 specifiedBitset_ |= PROPERTY_SELECTOR;
53 }
54
GetFormat() const55 uint8_t PbapPullPhoneBookParam::GetFormat() const
56 {
57 return format_;
58 }
59
SetFormat(uint8_t format)60 void PbapPullPhoneBookParam::SetFormat(uint8_t format)
61 {
62 format_ = format;
63 specifiedBitset_ |= FORMAT;
64 }
65
GetMaxListCount() const66 uint16_t PbapPullPhoneBookParam::GetMaxListCount() const
67 {
68 return maxListCount_;
69 }
70
SetMaxListCount(uint16_t maxListCount)71 void PbapPullPhoneBookParam::SetMaxListCount(uint16_t maxListCount)
72 {
73 maxListCount_ = maxListCount;
74 specifiedBitset_ |= MAX_LIST_COUNT;
75 }
76
GetListStartOffset() const77 uint16_t PbapPullPhoneBookParam::GetListStartOffset() const
78 {
79 return listStartOffset_;
80 }
81
SetListStartOffset(uint16_t listStartOffset)82 void PbapPullPhoneBookParam::SetListStartOffset(uint16_t listStartOffset)
83 {
84 listStartOffset_ = listStartOffset;
85 specifiedBitset_ |= LIST_START_OFFSET;
86 }
87
GetResetNewMissedCalls() const88 uint8_t PbapPullPhoneBookParam::GetResetNewMissedCalls() const
89 {
90 return resetNewMissedCalls_;
91 }
92
SetResetNewMissedCalls(uint8_t resetNewMissedCalls)93 void PbapPullPhoneBookParam::SetResetNewMissedCalls(uint8_t resetNewMissedCalls)
94 {
95 resetNewMissedCalls_ = resetNewMissedCalls;
96 specifiedBitset_ |= RESET_NEW_MISSED_CALLS;
97 }
98
GetvCardSelector() const99 uint64_t PbapPullPhoneBookParam::GetvCardSelector() const
100 {
101 return vCardSelector_;
102 }
103
SetvCardSelector(uint64_t vCardSelector)104 void PbapPullPhoneBookParam::SetvCardSelector(uint64_t vCardSelector)
105 {
106 vCardSelector_ = vCardSelector;
107 specifiedBitset_ |= VCARD_SELECTOR;
108 }
109
GetvCardSelectorOp() const110 uint8_t PbapPullPhoneBookParam::GetvCardSelectorOp() const
111 {
112 return vCardSelectorOp_;
113 }
114
SetvCardSelectorOp(uint8_t vCardSelectorOp)115 void PbapPullPhoneBookParam::SetvCardSelectorOp(uint8_t vCardSelectorOp)
116 {
117 vCardSelectorOp_ = vCardSelectorOp;
118 specifiedBitset_ |= VCARD_SELECTOROP;
119 }
120
GetSpecifiedBitset() const121 uint64_t PbapPullPhoneBookParam::GetSpecifiedBitset() const
122 {
123 return specifiedBitset_;
124 }
125
SetSpecifiedBitset(uint64_t specifiedBitset)126 void PbapPullPhoneBookParam::SetSpecifiedBitset(uint64_t specifiedBitset)
127 {
128 specifiedBitset_ = specifiedBitset;
129 }
130
IsSpecified(int bitSpecified) const131 bool PbapPullPhoneBookParam::IsSpecified(int bitSpecified) const
132 {
133 if (bitSpecified & specifiedBitset_) {
134 return true;
135 } else {
136 return false;
137 }
138 }
139
140 // implement PbapPullvCardListingParam
GetName() const141 std::u16string PbapPullvCardListingParam::GetName() const
142 {
143 return name_;
144 }
145
SetName(const std::u16string & name)146 void PbapPullvCardListingParam::SetName(const std::u16string &name)
147 {
148 name_ = name;
149 specifiedBitset_ |= NAME;
150 }
151
GetOrder() const152 uint8_t PbapPullvCardListingParam::GetOrder() const
153 {
154 return order_;
155 }
156
SetOrder(uint8_t order)157 void PbapPullvCardListingParam::SetOrder(uint8_t order)
158 {
159 order_ = order;
160 specifiedBitset_ |= ORDER;
161 }
162
GetSearchValue() const163 std::string PbapPullvCardListingParam::GetSearchValue() const
164 {
165 return searchValue_;
166 }
167
SetSearchValue(const std::string & searchValue)168 void PbapPullvCardListingParam::SetSearchValue(const std::string &searchValue)
169 {
170 searchValue_ = searchValue;
171 specifiedBitset_ |= SEARCH_VALUE;
172 }
173
GetSearchProperty() const174 uint8_t PbapPullvCardListingParam::GetSearchProperty() const
175 {
176 return searchProperty_;
177 }
178
SetSearchProperty(uint8_t searchProperty)179 void PbapPullvCardListingParam::SetSearchProperty(uint8_t searchProperty)
180 {
181 searchProperty_ = searchProperty;
182 specifiedBitset_ |= SEARCH_PROPERTY;
183 }
184
GetMaxListCount() const185 uint16_t PbapPullvCardListingParam::GetMaxListCount() const
186 {
187 return maxListCount_;
188 }
189
SetMaxListCount(uint16_t maxListCount)190 void PbapPullvCardListingParam::SetMaxListCount(uint16_t maxListCount)
191 {
192 maxListCount_ = maxListCount;
193 specifiedBitset_ |= MAX_LIST_COUNT;
194 }
195
GetListStartOffset() const196 uint16_t PbapPullvCardListingParam::GetListStartOffset() const
197 {
198 return listStartOffset_;
199 }
200
SetListStartOffset(uint16_t listStartOffset)201 void PbapPullvCardListingParam::SetListStartOffset(uint16_t listStartOffset)
202 {
203 listStartOffset_ = listStartOffset;
204 specifiedBitset_ |= LIST_START_OFFSET;
205 }
206
GetResetNewMissedCalls() const207 uint8_t PbapPullvCardListingParam::GetResetNewMissedCalls() const
208 {
209 return resetNewMissedCalls_;
210 }
211
SetResetNewMissedCalls(uint8_t resetNewMissedCalls)212 void PbapPullvCardListingParam::SetResetNewMissedCalls(uint8_t resetNewMissedCalls)
213 {
214 resetNewMissedCalls_ = resetNewMissedCalls;
215 specifiedBitset_ |= RESET_NEW_MISSED_CALLS;
216 }
217
GetvCardSelector() const218 uint64_t PbapPullvCardListingParam::GetvCardSelector() const
219 {
220 return vCardSelector_;
221 }
222
SetvCardSelector(uint64_t vCardSelector)223 void PbapPullvCardListingParam::SetvCardSelector(uint64_t vCardSelector)
224 {
225 vCardSelector_ = vCardSelector;
226 specifiedBitset_ |= VCARD_SELECTOR;
227 }
228
GetvCardSelectorOp() const229 uint8_t PbapPullvCardListingParam::GetvCardSelectorOp() const
230 {
231 return vCardSelectorOp_;
232 }
233
SetvCardSelectorOp(uint8_t vCardSelectorOp)234 void PbapPullvCardListingParam::SetvCardSelectorOp(uint8_t vCardSelectorOp)
235 {
236 vCardSelectorOp_ = vCardSelectorOp;
237 specifiedBitset_ |= VCARD_SELECTOROP;
238 }
239
GetSpecifiedBitset() const240 uint64_t PbapPullvCardListingParam::GetSpecifiedBitset() const
241 {
242 return specifiedBitset_;
243 }
244
SetSpecifiedBitset(uint64_t specifiedBitset)245 void PbapPullvCardListingParam::SetSpecifiedBitset(uint64_t specifiedBitset)
246 {
247 specifiedBitset_ = specifiedBitset;
248 }
249
IsSpecified(int bitSpecified) const250 bool PbapPullvCardListingParam::IsSpecified(int bitSpecified) const
251 {
252 if (bitSpecified & specifiedBitset_) {
253 return true;
254 } else {
255 return false;
256 }
257 }
258
259 // implement PbapPullvCardEntryParam
GetName() const260 std::u16string PbapPullvCardEntryParam::GetName() const
261 {
262 return name_;
263 }
264
SetName(const std::u16string & name)265 void PbapPullvCardEntryParam::SetName(const std::u16string &name)
266 {
267 name_ = name;
268 specifiedBitset_ |= NAME;
269 }
270
GetPropertySelector() const271 uint64_t PbapPullvCardEntryParam::GetPropertySelector() const
272 {
273 return propertySelector_;
274 }
275
SetPropertySelector(uint64_t propertySelector)276 void PbapPullvCardEntryParam::SetPropertySelector(uint64_t propertySelector)
277 {
278 propertySelector_ = propertySelector;
279 specifiedBitset_ |= PROPERTY_SELECTOR;
280 }
281
GetFormat() const282 uint8_t PbapPullvCardEntryParam::GetFormat() const
283 {
284 return format_;
285 }
286
SetFormat(uint8_t format)287 void PbapPullvCardEntryParam::SetFormat(uint8_t format)
288 {
289 format_ = format;
290 specifiedBitset_ |= FORMAT;
291 }
292
GetSpecifiedBitset() const293 uint64_t PbapPullvCardEntryParam::GetSpecifiedBitset() const
294 {
295 return specifiedBitset_;
296 }
297
SetSpecifiedBitset(uint64_t specifiedBitset)298 void PbapPullvCardEntryParam::SetSpecifiedBitset(uint64_t specifiedBitset)
299 {
300 specifiedBitset_ = specifiedBitset;
301 }
302
IsSpecified(int bitSpecified) const303 bool PbapPullvCardEntryParam::IsSpecified(int bitSpecified) const
304 {
305 if (bitSpecified & specifiedBitset_) {
306 return true;
307 } else {
308 return false;
309 }
310 }
311
312 class BluetoothIPbapPullPhoneBookParamAdapter {
313 public:
314 explicit BluetoothIPbapPullPhoneBookParamAdapter() = default;
BluetoothIPbapPullPhoneBookParamAdapter(const PbapPullPhoneBookParam & other)315 explicit BluetoothIPbapPullPhoneBookParamAdapter(const PbapPullPhoneBookParam &other)
316 {
317 data_.SetName(other.GetName());
318 data_.SetPropertySelector(other.GetPropertySelector());
319 data_.SetFormat(other.GetFormat());
320 data_.SetMaxListCount(other.GetMaxListCount());
321 data_.SetListStartOffset(other.GetListStartOffset());
322 data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
323 data_.SetvCardSelector(other.GetvCardSelector());
324 data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
325 data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
326 }
327 virtual ~BluetoothIPbapPullPhoneBookParamAdapter() = default;
operator BluetoothIPbapPullPhoneBookParam() const328 operator BluetoothIPbapPullPhoneBookParam() const
329 {
330 return data_;
331 }
332
333 private:
334 BluetoothIPbapPullPhoneBookParam data_;
335 };
336
337 class BluetoothIPbapPullvCardListingParamAdapter {
338 public:
339 explicit BluetoothIPbapPullvCardListingParamAdapter() = default;
BluetoothIPbapPullvCardListingParamAdapter(const PbapPullvCardListingParam & other)340 explicit BluetoothIPbapPullvCardListingParamAdapter(const PbapPullvCardListingParam &other)
341 {
342 data_.SetName(other.GetName());
343 data_.SetOrder(other.GetOrder());
344 data_.SetSearchValue(other.GetSearchValue());
345 data_.SetSearchProperty(other.GetSearchProperty());
346 data_.SetMaxListCount(other.GetMaxListCount());
347 data_.SetListStartOffset(other.GetListStartOffset());
348 data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
349 data_.SetvCardSelector(other.GetvCardSelector());
350 data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
351 data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
352 }
353 virtual ~BluetoothIPbapPullvCardListingParamAdapter() = default;
operator BluetoothIPbapPullvCardListingParam() const354 operator BluetoothIPbapPullvCardListingParam() const
355 {
356 return data_;
357 }
358
359 private:
360 BluetoothIPbapPullvCardListingParam data_;
361 };
362
363 class BluetoothIPbapPullvCardEntryParamAdapter {
364 public:
365 explicit BluetoothIPbapPullvCardEntryParamAdapter() = default;
BluetoothIPbapPullvCardEntryParamAdapter(const PbapPullvCardEntryParam & other)366 explicit BluetoothIPbapPullvCardEntryParamAdapter(const PbapPullvCardEntryParam &other)
367 {
368 data_.SetName(other.GetName());
369 data_.SetPropertySelector(other.GetPropertySelector());
370 data_.SetFormat(other.GetFormat());
371 data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
372 }
373 virtual ~BluetoothIPbapPullvCardEntryParamAdapter() = default;
operator BluetoothIPbapPullvCardEntryParam() const374 operator BluetoothIPbapPullvCardEntryParam() const
375 {
376 return data_;
377 }
378
379 private:
380 BluetoothIPbapPullvCardEntryParam data_;
381 };
382
383 class PbapPhoneBookDataAdapter {
384 public:
PbapPhoneBookDataAdapter(const BluetoothIPbapPhoneBookData & other)385 explicit PbapPhoneBookDataAdapter(const BluetoothIPbapPhoneBookData &other)
386 {
387 data_.phoneBookSize_ = other.phoneBookSize_;
388 data_.primaryFolderVersion_ = other.primaryFolderVersion_;
389 data_.secondaryFolderVersion_ = other.secondaryFolderVersion_;
390 data_.databaseIdentifier_ = other.databaseIdentifier_;
391 data_.result_ = other.result_;
392 const_cast<BluetoothIPbapPhoneBookData &>(other).Load(data_.result_);
393 }
operator PbapPhoneBookData() const394 operator PbapPhoneBookData() const
395 {
396 return data_;
397 }
398
399 private:
400 PbapPhoneBookData data_ {};
401 };
402
403 class BluetoothPbapPceObserverImpl : public BluetoothPbapPceObserverStub {
404 public:
405 BluetoothPbapPceObserverImpl() = default;
406 ~BluetoothPbapPceObserverImpl() = default;
SetObserver(BluetoothObserverList<PbapClientObserver> * frameworkObserverListPtr)407 void SetObserver(BluetoothObserverList<PbapClientObserver> *frameworkObserverListPtr)
408 {
409 frameworkObserverListPtr_ = frameworkObserverListPtr;
410 if (frameworkObserverListPtr_ == nullptr) {
411 HILOGI("frameworkObserverListPtr_ is nullptr error.");
412 }
413 }
414
OnServiceConnectionStateChanged(const BluetoothRawAddress & device,int state)415 void OnServiceConnectionStateChanged(const BluetoothRawAddress &device, int state) override
416 {
417 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
418 if (frameworkObserverListPtr_ != nullptr) {
419 frameworkObserverListPtr_->ForEach([device, state](std::shared_ptr<PbapClientObserver> observer) {
420 BluetoothRemoteDevice dev(device.GetAddress(), 0);
421 observer->OnServiceConnectionStateChanged(dev, state);
422 });
423 }
424 }
425
OnServicePasswordRequired(const BluetoothRawAddress & device,const::std::vector<uint8_t> & description,int8_t charset,bool fullAccess)426 void OnServicePasswordRequired(const BluetoothRawAddress &device,
427 const ::std::vector<uint8_t> &description, int8_t charset, bool fullAccess) override
428 {
429 HILOGI("enter, device: %{public}s, charset: %{public}d, fullAccess: %{public}d",
430 GetEncryptAddr((device).GetAddress()).c_str(), charset, fullAccess);
431 if (frameworkObserverListPtr_ != nullptr) {
432 frameworkObserverListPtr_->ForEach(
433 [device, description, charset, fullAccess](std::shared_ptr<PbapClientObserver> observer) {
434 BluetoothRemoteDevice dev(device.GetAddress(), 0);
435 observer->OnServicePasswordRequired(dev, description, charset, fullAccess);
436 });
437 }
438 }
439
OnActionCompleted(const BluetoothRawAddress & device,int respCode,int actionType,const BluetoothIPbapPhoneBookData & result)440 void OnActionCompleted(const BluetoothRawAddress &device,
441 int respCode, int actionType, const BluetoothIPbapPhoneBookData &result) override
442 {
443 HILOGI("enter, device: %{public}s, respCode: %{public}d, actionType: %{public}d",
444 GetEncryptAddr((device).GetAddress()).c_str(), respCode, actionType);
445 if (frameworkObserverListPtr_ != nullptr) {
446 frameworkObserverListPtr_->ForEach([device,
447 respCode, actionType, result](std::shared_ptr<PbapClientObserver> observer) {
448 BluetoothRemoteDevice dev(device.GetAddress(), 0);
449 observer->OnActionCompleted(dev, respCode, actionType, PbapPhoneBookDataAdapter(result));
450 });
451 }
452 }
453
454 private:
455 BluetoothObserverList<PbapClientObserver> *frameworkObserverListPtr_ {nullptr};
456 };
457
458 struct PbapClient::impl {
459 sptr<IBluetoothPbapPce> proxy_;
460 class BluetoothPbapPceDeathRecipient;
461 sptr<BluetoothPbapPceDeathRecipient> deathRecipient_ = nullptr;
462 impl();
~implOHOS::Bluetooth::PbapClient::impl463 ~impl()
464 {
465 if (proxy_ != nullptr) {
466 proxy_->DeregisterObserver(serviceObserverImpl_);
467 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
468 }
469 }
RegisterObserverOHOS::Bluetooth::PbapClient::impl470 void RegisterObserver(std::shared_ptr<PbapClientObserver> &observer)
471 {
472 HILOGI("enter");
473 frameworkObserverList_.Register(observer);
474 }
475
DeregisterObserverOHOS::Bluetooth::PbapClient::impl476 void DeregisterObserver(std::shared_ptr<PbapClientObserver> &observer)
477 {
478 HILOGI("enter");
479 frameworkObserverList_.Deregister(observer);
480 }
481
GetDeviceStateOHOS::Bluetooth::PbapClient::impl482 int GetDeviceState(const BluetoothRemoteDevice &device)
483 {
484 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
485 if (proxy_ != nullptr) {
486 return proxy_->GetDeviceState(bluetooth::RawAddress(device.GetDeviceAddr()));
487 }
488 return 0;
489 }
490
GetDevicesByStatesOHOS::Bluetooth::PbapClient::impl491 std::vector<BluetoothRemoteDevice> GetDevicesByStates(const std::vector<int> &states)
492 {
493 HILOGI("enter");
494 std::vector<BluetoothRemoteDevice> remoteDevices;
495 std::vector<BluetoothRawAddress> rawDevices;
496 std::vector<int32_t> tmpStates;
497 for (int state : states) {
498 tmpStates.push_back((int32_t)state);
499 }
500 if (proxy_ != nullptr) {
501 proxy_->GetDevicesByStates(tmpStates, rawDevices);
502 }
503 for (BluetoothRawAddress rawDevice : rawDevices) {
504 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
505 remoteDevices.push_back(remoteDevice);
506 }
507 return remoteDevices;
508 }
509
GetConnectedDevicesOHOS::Bluetooth::PbapClient::impl510 std::vector<BluetoothRemoteDevice> GetConnectedDevices()
511 {
512 HILOGI("enter");
513 std::vector<BluetoothRemoteDevice> remoteDevices;
514 std::vector<BluetoothRawAddress> rawDevices;
515 std::vector<int> states {static_cast<int>(BTConnectState::CONNECTED)};
516 if (proxy_ != nullptr) {
517 proxy_->GetDevicesByStates(states, rawDevices);
518 }
519 for (BluetoothRawAddress rawDevice : rawDevices) {
520 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
521 remoteDevices.push_back(remoteDevice);
522 }
523 return remoteDevices;
524 }
525
ConnectOHOS::Bluetooth::PbapClient::impl526 bool Connect(const BluetoothRemoteDevice &device)
527 {
528 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
529 if (proxy_ != nullptr) {
530 int ret = proxy_->Connect(bluetooth::RawAddress(device.GetDeviceAddr()));
531 return ret == RET_NO_ERROR;
532 } else {
533 return false;
534 }
535 }
536
PullPhoneBookOHOS::Bluetooth::PbapClient::impl537 int PullPhoneBook(const BluetoothRemoteDevice &device, const BluetoothIPbapPullPhoneBookParam ¶m)
538 {
539 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
540 int ret = -1;
541 if (proxy_ != nullptr) {
542 ret = proxy_->PullPhoneBook(bluetooth::RawAddress(device.GetDeviceAddr()), param);
543 }
544 return ret;
545 }
546
SetPhoneBookOHOS::Bluetooth::PbapClient::impl547 int SetPhoneBook(const BluetoothRemoteDevice &device, const std::u16string &name, int flag)
548 {
549 HILOGI("enter, device: %{public}s, flag: %{public}d", GET_ENCRYPT_ADDR(device), flag);
550 int ret = -1;
551 if (proxy_ != nullptr) {
552 ret = proxy_->SetPhoneBook(
553 bluetooth::RawAddress(device.GetDeviceAddr()), name.c_str(), flag);
554 }
555 return ret;
556 }
557
PullvCardListingOHOS::Bluetooth::PbapClient::impl558 int PullvCardListing(const BluetoothRemoteDevice &device, const BluetoothIPbapPullvCardListingParam ¶m)
559 {
560 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
561 int ret = -1;
562 if (proxy_ != nullptr) {
563 proxy_->PullvCardListing(bluetooth::RawAddress(device.GetDeviceAddr()), param);
564 }
565 return ret;
566 }
567
PullvCardEntryOHOS::Bluetooth::PbapClient::impl568 int PullvCardEntry(const BluetoothRemoteDevice &device, const BluetoothIPbapPullvCardEntryParam ¶m)
569 {
570 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
571 int ret = -1;
572 if (proxy_ != nullptr) {
573 ret = proxy_->PullvCardEntry(bluetooth::RawAddress(device.GetDeviceAddr()), param);
574 }
575 return ret;
576 }
577
IsDownloadingOHOS::Bluetooth::PbapClient::impl578 bool IsDownloading(const BluetoothRemoteDevice &device)
579 {
580 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
581 bool ret = false;
582 if (proxy_ != nullptr) {
583 ret = proxy_->IsDownloading(bluetooth::RawAddress(device.GetDeviceAddr()));
584 }
585 return ret;
586 }
587
AbortDownloadingOHOS::Bluetooth::PbapClient::impl588 int AbortDownloading(const BluetoothRemoteDevice &device)
589 {
590 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
591 int ret = -1;
592 if (proxy_ != nullptr) {
593 ret = proxy_->AbortDownloading(bluetooth::RawAddress(device.GetDeviceAddr()));
594 }
595 return ret;
596 }
597
DisconnectOHOS::Bluetooth::PbapClient::impl598 bool Disconnect(const BluetoothRemoteDevice &device)
599 {
600 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
601 if (proxy_ != nullptr) {
602 int ret = proxy_->Disconnect(bluetooth::RawAddress(device.GetDeviceAddr()));
603 return ret == RET_NO_ERROR;
604 } else {
605 return false;
606 }
607 }
608
SetConnectionStrategyOHOS::Bluetooth::PbapClient::impl609 bool SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
610 {
611 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
612 if (proxy_ != nullptr) {
613 int 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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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("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("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("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("failed: no remote");
680 return;
681 }
682 HILOGI("remote obtained");
683
684 proxy_ = iface_cast<IBluetoothPbapPce>(remote);
685 if (proxy_ == nullptr) {
686 return;
687 }
688 deathRecipient_ = new BluetoothPbapPceDeathRecipient(*this);
689 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
690 proxy_->RegisterObserver(serviceObserverImpl_);
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 HILOGI("enter");
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 HILOGI("enter");
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter");
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 HILOGI("enter");
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s, flag: %{public}d", GET_ENCRYPT_ADDR(device), flag);
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
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