• 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 <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_load_system_ability.h"
27 #include "bluetooth_log.h"
28 #include "bluetooth_utils.h"
29 #include "iservice_registry.h"
30 #include "raw_address.h"
31 #include "system_ability_definition.h"
32 namespace OHOS {
33 namespace Bluetooth {
GetName() const34 std::u16string PbapPullPhoneBookParam::GetName() const
35 {
36     return name_;
37 }
38 
SetName(const std::u16string & name)39 void PbapPullPhoneBookParam::SetName(const std::u16string &name)
40 {
41     name_ = name;
42     specifiedBitset_ |= NAME;
43 }
44 
GetPropertySelector() const45 uint64_t PbapPullPhoneBookParam::GetPropertySelector() const
46 {
47     return propertySelector_;
48 }
49 
SetPropertySelector(uint64_t propertySelector)50 void PbapPullPhoneBookParam::SetPropertySelector(uint64_t propertySelector)
51 {
52     propertySelector_ = propertySelector;
53     specifiedBitset_ |= PROPERTY_SELECTOR;
54 }
55 
GetFormat() const56 uint8_t PbapPullPhoneBookParam::GetFormat() const
57 {
58     return format_;
59 }
60 
SetFormat(uint8_t format)61 void PbapPullPhoneBookParam::SetFormat(uint8_t format)
62 {
63     format_ = format;
64     specifiedBitset_ |= FORMAT;
65 }
66 
GetMaxListCount() const67 uint16_t PbapPullPhoneBookParam::GetMaxListCount() const
68 {
69     return maxListCount_;
70 }
71 
SetMaxListCount(uint16_t maxListCount)72 void PbapPullPhoneBookParam::SetMaxListCount(uint16_t maxListCount)
73 {
74     maxListCount_ = maxListCount;
75     specifiedBitset_ |= MAX_LIST_COUNT;
76 }
77 
GetListStartOffset() const78 uint16_t PbapPullPhoneBookParam::GetListStartOffset() const
79 {
80     return listStartOffset_;
81 }
82 
SetListStartOffset(uint16_t listStartOffset)83 void PbapPullPhoneBookParam::SetListStartOffset(uint16_t listStartOffset)
84 {
85     listStartOffset_ = listStartOffset;
86     specifiedBitset_ |= LIST_START_OFFSET;
87 }
88 
GetResetNewMissedCalls() const89 uint8_t PbapPullPhoneBookParam::GetResetNewMissedCalls() const
90 {
91     return resetNewMissedCalls_;
92 }
93 
SetResetNewMissedCalls(uint8_t resetNewMissedCalls)94 void PbapPullPhoneBookParam::SetResetNewMissedCalls(uint8_t resetNewMissedCalls)
95 {
96     resetNewMissedCalls_ = resetNewMissedCalls;
97     specifiedBitset_ |= RESET_NEW_MISSED_CALLS;
98 }
99 
GetvCardSelector() const100 uint64_t PbapPullPhoneBookParam::GetvCardSelector() const
101 {
102     return vCardSelector_;
103 }
104 
SetvCardSelector(uint64_t vCardSelector)105 void PbapPullPhoneBookParam::SetvCardSelector(uint64_t vCardSelector)
106 {
107     vCardSelector_ = vCardSelector;
108     specifiedBitset_ |= VCARD_SELECTOR;
109 }
110 
GetvCardSelectorOp() const111 uint8_t PbapPullPhoneBookParam::GetvCardSelectorOp() const
112 {
113     return vCardSelectorOp_;
114 }
115 
SetvCardSelectorOp(uint8_t vCardSelectorOp)116 void PbapPullPhoneBookParam::SetvCardSelectorOp(uint8_t vCardSelectorOp)
117 {
118     vCardSelectorOp_ = vCardSelectorOp;
119     specifiedBitset_ |= VCARD_SELECTOROP;
120 }
121 
GetSpecifiedBitset() const122 uint64_t PbapPullPhoneBookParam::GetSpecifiedBitset() const
123 {
124     return specifiedBitset_;
125 }
126 
SetSpecifiedBitset(uint64_t specifiedBitset)127 void PbapPullPhoneBookParam::SetSpecifiedBitset(uint64_t specifiedBitset)
128 {
129     specifiedBitset_ = specifiedBitset;
130 }
131 
IsSpecified(int bitSpecified) const132 bool PbapPullPhoneBookParam::IsSpecified(int bitSpecified) const
133 {
134     if (bitSpecified & specifiedBitset_) {
135         return true;
136     } else {
137         return false;
138     }
139 }
140 
141 // implement PbapPullvCardListingParam
GetName() const142 std::u16string PbapPullvCardListingParam::GetName() const
143 {
144     return name_;
145 }
146 
SetName(const std::u16string & name)147 void PbapPullvCardListingParam::SetName(const std::u16string &name)
148 {
149     name_ = name;
150     specifiedBitset_ |= NAME;
151 }
152 
GetOrder() const153 uint8_t PbapPullvCardListingParam::GetOrder() const
154 {
155     return order_;
156 }
157 
SetOrder(uint8_t order)158 void PbapPullvCardListingParam::SetOrder(uint8_t order)
159 {
160     order_ = order;
161     specifiedBitset_ |= ORDER;
162 }
163 
GetSearchValue() const164 std::string PbapPullvCardListingParam::GetSearchValue() const
165 {
166     return searchValue_;
167 }
168 
SetSearchValue(const std::string & searchValue)169 void PbapPullvCardListingParam::SetSearchValue(const std::string &searchValue)
170 {
171     searchValue_ = searchValue;
172     specifiedBitset_ |= SEARCH_VALUE;
173 }
174 
GetSearchProperty() const175 uint8_t PbapPullvCardListingParam::GetSearchProperty() const
176 {
177     return searchProperty_;
178 }
179 
SetSearchProperty(uint8_t searchProperty)180 void PbapPullvCardListingParam::SetSearchProperty(uint8_t searchProperty)
181 {
182     searchProperty_ = searchProperty;
183     specifiedBitset_ |= SEARCH_PROPERTY;
184 }
185 
GetMaxListCount() const186 uint16_t PbapPullvCardListingParam::GetMaxListCount() const
187 {
188     return maxListCount_;
189 }
190 
SetMaxListCount(uint16_t maxListCount)191 void PbapPullvCardListingParam::SetMaxListCount(uint16_t maxListCount)
192 {
193     maxListCount_ = maxListCount;
194     specifiedBitset_ |= MAX_LIST_COUNT;
195 }
196 
GetListStartOffset() const197 uint16_t PbapPullvCardListingParam::GetListStartOffset() const
198 {
199     return listStartOffset_;
200 }
201 
SetListStartOffset(uint16_t listStartOffset)202 void PbapPullvCardListingParam::SetListStartOffset(uint16_t listStartOffset)
203 {
204     listStartOffset_ = listStartOffset;
205     specifiedBitset_ |= LIST_START_OFFSET;
206 }
207 
GetResetNewMissedCalls() const208 uint8_t PbapPullvCardListingParam::GetResetNewMissedCalls() const
209 {
210     return resetNewMissedCalls_;
211 }
212 
SetResetNewMissedCalls(uint8_t resetNewMissedCalls)213 void PbapPullvCardListingParam::SetResetNewMissedCalls(uint8_t resetNewMissedCalls)
214 {
215     resetNewMissedCalls_ = resetNewMissedCalls;
216     specifiedBitset_ |= RESET_NEW_MISSED_CALLS;
217 }
218 
GetvCardSelector() const219 uint64_t PbapPullvCardListingParam::GetvCardSelector() const
220 {
221     return vCardSelector_;
222 }
223 
SetvCardSelector(uint64_t vCardSelector)224 void PbapPullvCardListingParam::SetvCardSelector(uint64_t vCardSelector)
225 {
226     vCardSelector_ = vCardSelector;
227     specifiedBitset_ |= VCARD_SELECTOR;
228 }
229 
GetvCardSelectorOp() const230 uint8_t PbapPullvCardListingParam::GetvCardSelectorOp() const
231 {
232     return vCardSelectorOp_;
233 }
234 
SetvCardSelectorOp(uint8_t vCardSelectorOp)235 void PbapPullvCardListingParam::SetvCardSelectorOp(uint8_t vCardSelectorOp)
236 {
237     vCardSelectorOp_ = vCardSelectorOp;
238     specifiedBitset_ |= VCARD_SELECTOROP;
239 }
240 
GetSpecifiedBitset() const241 uint64_t PbapPullvCardListingParam::GetSpecifiedBitset() const
242 {
243     return specifiedBitset_;
244 }
245 
SetSpecifiedBitset(uint64_t specifiedBitset)246 void PbapPullvCardListingParam::SetSpecifiedBitset(uint64_t specifiedBitset)
247 {
248     specifiedBitset_ = specifiedBitset;
249 }
250 
IsSpecified(int bitSpecified) const251 bool PbapPullvCardListingParam::IsSpecified(int bitSpecified) const
252 {
253     if (bitSpecified & specifiedBitset_) {
254         return true;
255     } else {
256         return false;
257     }
258 }
259 
260 // implement PbapPullvCardEntryParam
GetName() const261 std::u16string PbapPullvCardEntryParam::GetName() const
262 {
263     return name_;
264 }
265 
SetName(const std::u16string & name)266 void PbapPullvCardEntryParam::SetName(const std::u16string &name)
267 {
268     name_ = name;
269     specifiedBitset_ |= NAME;
270 }
271 
GetPropertySelector() const272 uint64_t PbapPullvCardEntryParam::GetPropertySelector() const
273 {
274     return propertySelector_;
275 }
276 
SetPropertySelector(uint64_t propertySelector)277 void PbapPullvCardEntryParam::SetPropertySelector(uint64_t propertySelector)
278 {
279     propertySelector_ = propertySelector;
280     specifiedBitset_ |= PROPERTY_SELECTOR;
281 }
282 
GetFormat() const283 uint8_t PbapPullvCardEntryParam::GetFormat() const
284 {
285     return format_;
286 }
287 
SetFormat(uint8_t format)288 void PbapPullvCardEntryParam::SetFormat(uint8_t format)
289 {
290     format_ = format;
291     specifiedBitset_ |= FORMAT;
292 }
293 
GetSpecifiedBitset() const294 uint64_t PbapPullvCardEntryParam::GetSpecifiedBitset() const
295 {
296     return specifiedBitset_;
297 }
298 
SetSpecifiedBitset(uint64_t specifiedBitset)299 void PbapPullvCardEntryParam::SetSpecifiedBitset(uint64_t specifiedBitset)
300 {
301     specifiedBitset_ = specifiedBitset;
302 }
303 
IsSpecified(int bitSpecified) const304 bool PbapPullvCardEntryParam::IsSpecified(int bitSpecified) const
305 {
306     if (bitSpecified & specifiedBitset_) {
307         return true;
308     } else {
309         return false;
310     }
311 }
312 
313 class BluetoothIPbapPullPhoneBookParamAdapter {
314 public:
315     explicit BluetoothIPbapPullPhoneBookParamAdapter() = default;
BluetoothIPbapPullPhoneBookParamAdapter(const PbapPullPhoneBookParam & other)316     explicit BluetoothIPbapPullPhoneBookParamAdapter(const PbapPullPhoneBookParam &other)
317     {
318         data_.SetName(other.GetName());
319         data_.SetPropertySelector(other.GetPropertySelector());
320         data_.SetFormat(other.GetFormat());
321         data_.SetMaxListCount(other.GetMaxListCount());
322         data_.SetListStartOffset(other.GetListStartOffset());
323         data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
324         data_.SetvCardSelector(other.GetvCardSelector());
325         data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
326         data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
327     }
328     virtual ~BluetoothIPbapPullPhoneBookParamAdapter() = default;
operator BluetoothIPbapPullPhoneBookParam() const329     operator BluetoothIPbapPullPhoneBookParam() const
330     {
331         return data_;
332     }
333 
334 private:
335     BluetoothIPbapPullPhoneBookParam data_;
336 };
337 
338 class BluetoothIPbapPullvCardListingParamAdapter {
339 public:
340     explicit BluetoothIPbapPullvCardListingParamAdapter() = default;
BluetoothIPbapPullvCardListingParamAdapter(const PbapPullvCardListingParam & other)341     explicit BluetoothIPbapPullvCardListingParamAdapter(const PbapPullvCardListingParam &other)
342     {
343         data_.SetName(other.GetName());
344         data_.SetOrder(other.GetOrder());
345         data_.SetSearchValue(other.GetSearchValue());
346         data_.SetSearchProperty(other.GetSearchProperty());
347         data_.SetMaxListCount(other.GetMaxListCount());
348         data_.SetListStartOffset(other.GetListStartOffset());
349         data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
350         data_.SetvCardSelector(other.GetvCardSelector());
351         data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
352         data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
353     }
354     virtual ~BluetoothIPbapPullvCardListingParamAdapter() = default;
operator BluetoothIPbapPullvCardListingParam() const355     operator BluetoothIPbapPullvCardListingParam() const
356     {
357         return data_;
358     }
359 
360 private:
361     BluetoothIPbapPullvCardListingParam data_;
362 };
363 
364 class BluetoothIPbapPullvCardEntryParamAdapter {
365 public:
366     explicit BluetoothIPbapPullvCardEntryParamAdapter() = default;
BluetoothIPbapPullvCardEntryParamAdapter(const PbapPullvCardEntryParam & other)367     explicit BluetoothIPbapPullvCardEntryParamAdapter(const PbapPullvCardEntryParam &other)
368     {
369         data_.SetName(other.GetName());
370         data_.SetPropertySelector(other.GetPropertySelector());
371         data_.SetFormat(other.GetFormat());
372         data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
373     }
374     virtual ~BluetoothIPbapPullvCardEntryParamAdapter() = default;
operator BluetoothIPbapPullvCardEntryParam() const375     operator BluetoothIPbapPullvCardEntryParam() const
376     {
377         return data_;
378     }
379 
380 private:
381     BluetoothIPbapPullvCardEntryParam data_;
382 };
383 
384 class PbapPhoneBookDataAdapter {
385 public:
PbapPhoneBookDataAdapter(const BluetoothIPbapPhoneBookData & other)386     explicit PbapPhoneBookDataAdapter(const BluetoothIPbapPhoneBookData &other)
387     {
388         data_.phoneBookSize_ = other.phoneBookSize_;
389         data_.primaryFolderVersion_ = other.primaryFolderVersion_;
390         data_.secondaryFolderVersion_ = other.secondaryFolderVersion_;
391         data_.databaseIdentifier_ = other.databaseIdentifier_;
392         data_.result_ = other.result_;
393         const_cast<BluetoothIPbapPhoneBookData &>(other).Load(data_.result_);
394     }
operator PbapPhoneBookData() const395     operator PbapPhoneBookData() const
396     {
397         return data_;
398     }
399 
400 private:
401     PbapPhoneBookData data_ {};
402 };
403 
404 class BluetoothPbapPceObserverImpl : public BluetoothPbapPceObserverStub {
405 public:
406     BluetoothPbapPceObserverImpl() = default;
407     ~BluetoothPbapPceObserverImpl() override = default;
SetObserver(BluetoothObserverList<PbapClientObserver> * frameworkObserverListPtr)408     void SetObserver(BluetoothObserverList<PbapClientObserver> *frameworkObserverListPtr)
409     {
410         frameworkObserverListPtr_ = frameworkObserverListPtr;
411         if (frameworkObserverListPtr_ == nullptr) {
412             HILOGI("frameworkObserverListPtr_ is nullptr error.");
413         }
414     }
415 
OnServiceConnectionStateChanged(const BluetoothRawAddress & device,int state)416     void OnServiceConnectionStateChanged(const BluetoothRawAddress &device, int state) override
417     {
418         HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
419         if (frameworkObserverListPtr_ != nullptr) {
420             frameworkObserverListPtr_->ForEach([device, state](std::shared_ptr<PbapClientObserver> observer) {
421                 BluetoothRemoteDevice dev(device.GetAddress(), 0);
422                 observer->OnServiceConnectionStateChanged(dev, state);
423             });
424         }
425     }
426 
OnServicePasswordRequired(const BluetoothRawAddress & device,const::std::vector<uint8_t> & description,int8_t charset,bool fullAccess)427     void OnServicePasswordRequired(const BluetoothRawAddress &device,
428         const ::std::vector<uint8_t> &description, int8_t charset, bool fullAccess) override
429     {
430         HILOGI("enter, device: %{public}s, charset: %{public}d, fullAccess: %{public}d",
431             GetEncryptAddr((device).GetAddress()).c_str(), charset, fullAccess);
432         if (frameworkObserverListPtr_ != nullptr) {
433             frameworkObserverListPtr_->ForEach(
434                 [device, description, charset, fullAccess](std::shared_ptr<PbapClientObserver> observer) {
435                     BluetoothRemoteDevice dev(device.GetAddress(), 0);
436                     observer->OnServicePasswordRequired(dev, description, charset, fullAccess);
437                 });
438         }
439     }
440 
OnActionCompleted(const BluetoothRawAddress & device,int respCode,int actionType,const BluetoothIPbapPhoneBookData & result)441     void  OnActionCompleted(const BluetoothRawAddress &device,
442         int respCode, int actionType, const BluetoothIPbapPhoneBookData &result) override
443     {
444         HILOGI("enter, device: %{public}s, respCode: %{public}d, actionType: %{public}d",
445             GetEncryptAddr((device).GetAddress()).c_str(), respCode, actionType);
446         if (frameworkObserverListPtr_ != nullptr) {
447             frameworkObserverListPtr_->ForEach([device,
448                 respCode, actionType, result](std::shared_ptr<PbapClientObserver> observer) {
449                 BluetoothRemoteDevice dev(device.GetAddress(), 0);
450                 observer->OnActionCompleted(dev, respCode, actionType, PbapPhoneBookDataAdapter(result));
451             });
452         }
453     }
454 
455 private:
456     BluetoothObserverList<PbapClientObserver> *frameworkObserverListPtr_ {nullptr};
457 };
458 
459 struct PbapClient::impl {
460     sptr<IBluetoothPbapPce> proxy_;
461     class BluetoothPbapPceDeathRecipient;
462     sptr<BluetoothPbapPceDeathRecipient> deathRecipient_ = nullptr;
463     impl();
~implOHOS::Bluetooth::PbapClient::impl464     ~impl()
465     {
466         if (proxy_ != nullptr) {
467             proxy_->DeregisterObserver(serviceObserverImpl_);
468             proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
469         }
470     }
RegisterObserverOHOS::Bluetooth::PbapClient::impl471     void RegisterObserver(std::shared_ptr<PbapClientObserver> &observer)
472     {
473         HILOGI("enter");
474         frameworkObserverList_.Register(observer);
475     }
476 
DeregisterObserverOHOS::Bluetooth::PbapClient::impl477     void DeregisterObserver(std::shared_ptr<PbapClientObserver> &observer)
478     {
479         HILOGI("enter");
480         frameworkObserverList_.Deregister(observer);
481     }
482 
GetDeviceStateOHOS::Bluetooth::PbapClient::impl483     int GetDeviceState(const BluetoothRemoteDevice &device)
484     {
485         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
486         if (proxy_ != nullptr) {
487             return proxy_->GetDeviceState(bluetooth::RawAddress(device.GetDeviceAddr()));
488         }
489         return static_cast<int>(BTConnectState::DISCONNECTED);
490     }
491 
GetDevicesByStatesOHOS::Bluetooth::PbapClient::impl492     std::vector<BluetoothRemoteDevice> GetDevicesByStates(const std::vector<int> &states)
493     {
494         HILOGI("enter");
495         std::vector<BluetoothRemoteDevice> remoteDevices;
496         std::vector<BluetoothRawAddress> rawDevices;
497         std::vector<int32_t> tmpStates;
498         for (int state : states) {
499             tmpStates.push_back((int32_t)state);
500         }
501         if (proxy_ != nullptr) {
502             proxy_->GetDevicesByStates(tmpStates, rawDevices);
503         }
504         for (BluetoothRawAddress rawDevice : rawDevices) {
505             BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
506             remoteDevices.push_back(remoteDevice);
507         }
508         return remoteDevices;
509     }
510 
GetConnectedDevicesOHOS::Bluetooth::PbapClient::impl511     std::vector<BluetoothRemoteDevice> GetConnectedDevices()
512     {
513         HILOGI("enter");
514         std::vector<BluetoothRemoteDevice> remoteDevices;
515         std::vector<BluetoothRawAddress> rawDevices;
516         std::vector<int> states {static_cast<int>(BTConnectState::CONNECTED)};
517         if (proxy_ != nullptr) {
518             proxy_->GetDevicesByStates(states, rawDevices);
519         }
520         for (BluetoothRawAddress rawDevice : rawDevices) {
521             BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
522             remoteDevices.push_back(remoteDevice);
523         }
524         return remoteDevices;
525     }
526 
ConnectOHOS::Bluetooth::PbapClient::impl527     bool Connect(const BluetoothRemoteDevice &device)
528     {
529         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
530         if (proxy_ != nullptr) {
531             int ret = proxy_->Connect(bluetooth::RawAddress(device.GetDeviceAddr()));
532             return ret == RET_NO_ERROR;
533         } else {
534             return false;
535         }
536     }
537 
PullPhoneBookOHOS::Bluetooth::PbapClient::impl538     int PullPhoneBook(const BluetoothRemoteDevice &device, const BluetoothIPbapPullPhoneBookParam &param)
539     {
540         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
541         int ret = -1;
542         if (proxy_ != nullptr) {
543             ret = proxy_->PullPhoneBook(bluetooth::RawAddress(device.GetDeviceAddr()), param);
544         }
545         return ret;
546     }
547 
SetPhoneBookOHOS::Bluetooth::PbapClient::impl548     int SetPhoneBook(const BluetoothRemoteDevice &device, const std::u16string &name, int flag)
549     {
550         HILOGI("enter, device: %{public}s, flag: %{public}d", GET_ENCRYPT_ADDR(device), flag);
551         int ret = -1;
552         if (proxy_ != nullptr) {
553             ret = proxy_->SetPhoneBook(
554                 bluetooth::RawAddress(device.GetDeviceAddr()), name.c_str(), flag);
555         }
556         return ret;
557     }
558 
PullvCardListingOHOS::Bluetooth::PbapClient::impl559     int PullvCardListing(const BluetoothRemoteDevice &device, const BluetoothIPbapPullvCardListingParam &param)
560     {
561         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
562         int ret = -1;
563         if (proxy_ != nullptr) {
564             proxy_->PullvCardListing(bluetooth::RawAddress(device.GetDeviceAddr()), param);
565         }
566         return ret;
567     }
568 
PullvCardEntryOHOS::Bluetooth::PbapClient::impl569     int PullvCardEntry(const BluetoothRemoteDevice &device, const BluetoothIPbapPullvCardEntryParam &param)
570     {
571         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
572         int ret = -1;
573         if (proxy_ != nullptr) {
574             ret = proxy_->PullvCardEntry(bluetooth::RawAddress(device.GetDeviceAddr()), param);
575         }
576         return ret;
577     }
578 
IsDownloadingOHOS::Bluetooth::PbapClient::impl579     bool IsDownloading(const BluetoothRemoteDevice &device)
580     {
581         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
582         bool ret = false;
583         if (proxy_ != nullptr) {
584             ret = proxy_->IsDownloading(bluetooth::RawAddress(device.GetDeviceAddr()));
585         }
586         return ret;
587     }
588 
AbortDownloadingOHOS::Bluetooth::PbapClient::impl589     int AbortDownloading(const BluetoothRemoteDevice &device)
590     {
591         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
592         int ret = -1;
593         if (proxy_ != nullptr) {
594             ret = proxy_->AbortDownloading(bluetooth::RawAddress(device.GetDeviceAddr()));
595         }
596         return ret;
597     }
598 
DisconnectOHOS::Bluetooth::PbapClient::impl599     bool Disconnect(const BluetoothRemoteDevice &device)
600     {
601         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
602         if (proxy_ != nullptr) {
603             int ret = proxy_->Disconnect(bluetooth::RawAddress(device.GetDeviceAddr()));
604             return ret == RET_NO_ERROR;
605         } else {
606             return false;
607         }
608     }
609 
SetConnectionStrategyOHOS::Bluetooth::PbapClient::impl610     bool SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
611     {
612         HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
613         if (proxy_ != nullptr) {
614             int ret = proxy_->SetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()), strategy);
615             return ret == RET_NO_ERROR;
616         } else {
617             return false;
618         }
619     }
620 
GetConnectionStrategyOHOS::Bluetooth::PbapClient::impl621     int GetConnectionStrategy(const BluetoothRemoteDevice &device)
622     {
623         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
624         int ret = static_cast<int>(BTStrategyType::CONNECTION_UNKNOWN);
625         if (proxy_ != nullptr) {
626             ret = proxy_->GetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()));
627         }
628         return ret;
629     }
630 
SetDevicePasswordOHOS::Bluetooth::PbapClient::impl631     int SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, const std::string &userId)
632     {
633         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
634         int ret = -1;
635         if (proxy_ != nullptr) {
636             ret = proxy_->SetDevicePassword(
637                 bluetooth::RawAddress(device.GetDeviceAddr()), password.c_str(), userId.c_str());
638         }
639         return ret;
640     }
641 
642     bool InitPbapClientProxy(void);
643 
644 private:
645 
646     std::mutex mutex_;
647     BluetoothObserverList<PbapClientObserver> frameworkObserverList_ {};
648     sptr<BluetoothPbapPceObserverImpl> serviceObserverImpl_;
649 };
650 
651 class PbapClient::impl::BluetoothPbapPceDeathRecipient final : public IRemoteObject::DeathRecipient {
652 public:
BluetoothPbapPceDeathRecipient(PbapClient::impl & pbapPce)653     explicit BluetoothPbapPceDeathRecipient(PbapClient::impl &pbapPce) : pbapPce_(pbapPce) {};
654     ~BluetoothPbapPceDeathRecipient() final = default;
655     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothPbapPceDeathRecipient);
656 
OnRemoteDied(const wptr<IRemoteObject> & remote)657     void OnRemoteDied(const wptr<IRemoteObject> &remote) final
658     {
659         HILOGI("starts");
660         if (!pbapPce_.proxy_) {
661             return;
662         }
663     }
664 
665 private:
666     PbapClient::impl &pbapPce_;
667 };
impl()668 PbapClient::impl::impl()
669 {
670     if (proxy_) {
671         return;
672     }
673     BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_PBAP_PCE);
674     if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) {
675         BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility();
676         return;
677     }
678     InitPbapClientProxy();
679 }
680 
InitPbapClientProxy(void)681 bool PbapClient::impl::InitPbapClientProxy(void)
682 {
683     if (proxy_) {
684         return true;
685     }
686 
687     proxy_ = GetRemoteProxy<IBluetoothPbapPce>(PROFILE_PBAP_PCE);
688     if (!proxy_) {
689         HILOGE("get PbapClient proxy_ failed");
690         return false;
691     }
692 
693     serviceObserverImpl_ = new BluetoothPbapPceObserverImpl();
694     serviceObserverImpl_->SetObserver(&frameworkObserverList_);  // bind frameworkObserverList_
695     if (serviceObserverImpl_ != nullptr) {
696         proxy_->RegisterObserver(serviceObserverImpl_);
697     }
698 
699     deathRecipient_ = new BluetoothPbapPceDeathRecipient(*this);
700     if (deathRecipient_ != nullptr) {
701         proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
702     }
703     return true;
704 }
705 
GetProfile()706 PbapClient *PbapClient::GetProfile()
707 {
708     static PbapClient instance;
709     return &instance;
710 }
711 
PbapClient()712 PbapClient::PbapClient()
713 {
714     pimpl = std::make_unique<impl>();
715 }
716 
~PbapClient()717 PbapClient::~PbapClient()
718 {}
719 
Init()720 void PbapClient::Init()
721 {
722     if (!pimpl) {
723         HILOGE("fails: no pimpl");
724         return;
725     }
726     if (!pimpl->InitPbapClientProxy()) {
727         HILOGE("PbapClient proxy_ is nullptr");
728         return;
729     }
730 }
731 
RegisterObserver(PbapClientObserver * observer)732 void PbapClient::RegisterObserver(PbapClientObserver *observer)
733 {
734     HILOGI("enter");
735     std::shared_ptr<PbapClientObserver> pointer(observer, [](PbapClientObserver *) {});
736     return pimpl->RegisterObserver(pointer);
737 }
738 
DeregisterObserver(PbapClientObserver * observer)739 void PbapClient::DeregisterObserver(PbapClientObserver *observer)
740 {
741     HILOGI("enter");
742     std::shared_ptr<PbapClientObserver> pointer(observer, [](PbapClientObserver *) {});
743     return pimpl->DeregisterObserver(pointer);
744 }
745 
GetDeviceState(const BluetoothRemoteDevice & device)746 int PbapClient::GetDeviceState(const BluetoothRemoteDevice &device)
747 {
748     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
749     if (!IS_BT_ENABLED()) {
750         HILOGE("bluetooth is off.");
751         return static_cast<int>(BTConnectState::DISCONNECTED);
752     }
753 
754     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
755         HILOGE("pimpl or pbapClient proxy_ is nullptr");
756         return static_cast<int>(BTConnectState::DISCONNECTED);
757     }
758 
759     if (!device.IsValidBluetoothRemoteDevice()) {
760         return static_cast<int>(BTConnectState::DISCONNECTED);
761     }
762 
763     return pimpl->GetDeviceState(device);
764 }
765 
GetDevicesByStates(const std::vector<int> & states)766 std::vector<BluetoothRemoteDevice> PbapClient::GetDevicesByStates(const std::vector<int> &states)
767 {
768     HILOGI("enter");
769     if (!IS_BT_ENABLED()) {
770         HILOGE("bluetooth is off.");
771         return std::vector<BluetoothRemoteDevice>();
772     }
773 
774     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
775         HILOGE("pimpl or pbapClient proxy_ is nullptr");
776         return std::vector<BluetoothRemoteDevice>();
777     }
778 
779     return pimpl->GetDevicesByStates(states);
780 }
781 
GetConnectedDevices()782 std::vector<BluetoothRemoteDevice> PbapClient::GetConnectedDevices()
783 {
784     HILOGI("enter");
785     if (!IS_BT_ENABLED()) {
786         HILOGE("bluetooth is off.");
787         return std::vector<BluetoothRemoteDevice>();
788     }
789 
790     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
791         HILOGE("pimpl or pbapClient proxy_ is nullptr");
792         return std::vector<BluetoothRemoteDevice>();
793     }
794 
795     return pimpl->GetConnectedDevices();
796 }
797 
Connect(const BluetoothRemoteDevice & device)798 bool PbapClient::Connect(const BluetoothRemoteDevice &device)
799 {
800     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
801     if (!IS_BT_ENABLED()) {
802         HILOGE("bluetooth is off.");
803         return false;
804     }
805 
806     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
807         HILOGE("pimpl or pbapClient proxy_ is nullptr");
808         return false;
809     }
810 
811     if (!device.IsValidBluetoothRemoteDevice()) {
812         return false;
813     }
814 
815     return pimpl->Connect(device);
816 }
817 
Disconnect(const BluetoothRemoteDevice & device)818 bool PbapClient::Disconnect(const BluetoothRemoteDevice &device)
819 {
820     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
821     if (!IS_BT_ENABLED()) {
822         HILOGE("bluetooth is off.");
823         return false;
824     }
825 
826     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
827         HILOGE("pimpl or pbapClient proxy_ is nullptr");
828         return false;
829     }
830 
831     if (!device.IsValidBluetoothRemoteDevice()) {
832         return false;
833     }
834     return pimpl->Disconnect(device);
835 }
836 
SetConnectionStrategy(const BluetoothRemoteDevice & device,int strategy)837 bool PbapClient::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
838 {
839     HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
840     if (!IS_BT_ENABLED()) {
841         HILOGE("bluetooth is off.");
842         return false;
843     }
844 
845     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
846         HILOGE("pimpl or pbapClient proxy_ is nullptr");
847         return false;
848     }
849 
850     if (!device.IsValidBluetoothRemoteDevice()) {
851         return false;
852     }
853     return pimpl->SetConnectionStrategy(device, strategy);
854 }
855 
GetConnectionStrategy(const BluetoothRemoteDevice & device)856 int PbapClient::GetConnectionStrategy(const BluetoothRemoteDevice &device)
857 {
858     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
859     if (!IS_BT_ENABLED()) {
860         HILOGE("bluetooth is off.");
861         return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
862     }
863 
864     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
865         HILOGE("pimpl or pbapClient proxy_ is nullptr");
866         return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
867     }
868 
869     if (!device.IsValidBluetoothRemoteDevice()) {
870         return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
871     }
872 
873     return pimpl->GetConnectionStrategy(device);
874 }
875 
SetDevicePassword(const BluetoothRemoteDevice & device,const std::string & password,const std::string & userId)876 int PbapClient::SetDevicePassword(
877     const BluetoothRemoteDevice &device, const std::string &password, const std::string &userId)
878 {
879     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
880     if (!IS_BT_ENABLED()) {
881         HILOGE("bluetooth is off.");
882         return RET_BAD_STATUS;
883     }
884 
885     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
886         HILOGE("pimpl or pbapClient proxy_ is nullptr");
887         return RET_BAD_STATUS;
888     }
889 
890     if (!device.IsValidBluetoothRemoteDevice()) {
891         return RET_BAD_PARAM;
892     }
893 
894     return pimpl->SetDevicePassword(device, password, userId);
895 }
896 
PullPhoneBook(const BluetoothRemoteDevice & device,const PbapPullPhoneBookParam & param)897 int PbapClient::PullPhoneBook(const BluetoothRemoteDevice &device, const PbapPullPhoneBookParam &param)
898 {
899     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
900     if (!IS_BT_ENABLED()) {
901         HILOGE("bluetooth is off.");
902         return RET_BAD_STATUS;
903     }
904 
905     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
906         HILOGE("pimpl or pbapClient proxy_ is nullptr");
907         return RET_BAD_STATUS;
908     }
909 
910     if (!device.IsValidBluetoothRemoteDevice()) {
911         return RET_BAD_STATUS;
912     }
913 
914     return pimpl->PullPhoneBook(device, BluetoothIPbapPullPhoneBookParamAdapter(param));
915 }
916 
SetPhoneBook(const BluetoothRemoteDevice & device,const std::u16string & name,int flag)917 int PbapClient::SetPhoneBook(const BluetoothRemoteDevice &device, const std::u16string &name, int flag)
918 {
919     HILOGI("enter, device: %{public}s, flag: %{public}d", GET_ENCRYPT_ADDR(device), flag);
920     if (!IS_BT_ENABLED()) {
921         HILOGE("bluetooth is off.");
922         return RET_BAD_STATUS;
923     }
924 
925     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
926         HILOGE("pimpl or pbapClient proxy_ is nullptr");
927         return RET_BAD_STATUS;
928     }
929 
930     if (!device.IsValidBluetoothRemoteDevice()) {
931         return RET_BAD_PARAM;
932     }
933     return pimpl->SetPhoneBook(device, name, flag);
934 }
935 
PullvCardListing(const BluetoothRemoteDevice & device,const PbapPullvCardListingParam & param)936 int PbapClient::PullvCardListing(const BluetoothRemoteDevice &device, const PbapPullvCardListingParam &param)
937 {
938     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
939     if (!IS_BT_ENABLED()) {
940         HILOGE("bluetooth is off.");
941         return RET_BAD_STATUS;
942     }
943 
944     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
945         HILOGE("pimpl or pbapClient proxy_ is nullptr");
946         return RET_BAD_STATUS;
947     }
948 
949     if (!device.IsValidBluetoothRemoteDevice()) {
950         return RET_BAD_PARAM;
951     }
952     return pimpl->PullvCardListing(device, BluetoothIPbapPullvCardListingParamAdapter(param));
953 }
954 
PullvCardEntry(const BluetoothRemoteDevice & device,const PbapPullvCardEntryParam & param)955 int PbapClient::PullvCardEntry(const BluetoothRemoteDevice &device, const PbapPullvCardEntryParam &param)
956 {
957     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
958     if (!IS_BT_ENABLED()) {
959         HILOGE("bluetooth is off.");
960         return RET_BAD_STATUS;
961     }
962 
963     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
964         HILOGE("pimpl or pbapClient proxy_ is nullptr");
965         return RET_BAD_STATUS;
966     }
967 
968     if (!device.IsValidBluetoothRemoteDevice()) {
969         return RET_BAD_PARAM;
970     }
971     return pimpl->PullvCardEntry(device, BluetoothIPbapPullvCardEntryParamAdapter(param));
972 }
973 
IsDownloading(const BluetoothRemoteDevice & device)974 bool PbapClient::IsDownloading(const BluetoothRemoteDevice &device)
975 {
976     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
977     if (!IS_BT_ENABLED()) {
978         HILOGE("bluetooth is off.");
979         return false;
980     }
981 
982     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
983         HILOGE("pimpl or pbapClient proxy_ is nullptr");
984         return false;
985     }
986 
987     if (!device.IsValidBluetoothRemoteDevice()) {
988         return false;
989     }
990     return pimpl->IsDownloading(device);
991 }
992 
AbortDownloading(const BluetoothRemoteDevice & device)993 int PbapClient::AbortDownloading(const BluetoothRemoteDevice &device)
994 {
995     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
996     if (!IS_BT_ENABLED()) {
997         HILOGE("bluetooth is off.");
998         return RET_BAD_STATUS;
999     }
1000 
1001     if (pimpl == nullptr || !pimpl->InitPbapClientProxy()) {
1002         HILOGE("pimpl or pbapClient proxy_ is nullptr");
1003         return RET_BAD_STATUS;
1004     }
1005 
1006     if (!device.IsValidBluetoothRemoteDevice()) {
1007         return RET_BAD_PARAM;
1008     }
1009     return pimpl->AbortDownloading(device);
1010 }
1011 }  // namespace Bluetooth
1012 }  // namespace OHOS