• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bluetooth_pbap_pce_server.h"
17 #include "i_bluetooth_pbap_pce.h"
18 #include "interface_adapter_manager.h"
19 #include "interface_profile_manager.h"
20 #include "interface_profile_pbap_pce.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_utils_server.h"
23 #include "permission_utils.h"
24 #include "remote_observer_list.h"
25 #include "bluetooth_def.h"
26 
27 namespace OHOS {
28 namespace Bluetooth {
29 using namespace OHOS::bluetooth;
30 
31 class IPbapPullPhoneBookParamAdapter {
32 public:
33     IPbapPullPhoneBookParamAdapter() = default;
IPbapPullPhoneBookParamAdapter(const BluetoothIPbapPullPhoneBookParam & other)34     IPbapPullPhoneBookParamAdapter(const BluetoothIPbapPullPhoneBookParam &other)
35     {
36         data_.SetName(other.GetName());
37         data_.SetPropertySelector(other.GetPropertySelector());
38         data_.SetFormat(other.GetFormat());
39         data_.SetMaxListCount(other.GetMaxListCount());
40         data_.SetListStartOffset(other.GetListStartOffset());
41         data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
42         data_.SetvCardSelector(other.GetvCardSelector());
43         data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
44         data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
45     }
46     virtual ~IPbapPullPhoneBookParamAdapter() = default;
operator bluetooth::IPbapPullPhoneBookParam() const47     operator bluetooth::IPbapPullPhoneBookParam() const
48     {
49         return data_;
50     }
51 
52 private:
53     bluetooth::IPbapPullPhoneBookParam data_;
54 };
55 
56 class IPbapPullvCardListingParamAdapter {
57 public:
58     IPbapPullvCardListingParamAdapter() = default;
IPbapPullvCardListingParamAdapter(const BluetoothIPbapPullvCardListingParam & other)59     IPbapPullvCardListingParamAdapter(const BluetoothIPbapPullvCardListingParam &other)
60     {
61         data_.SetName(other.GetName());
62         data_.SetOrder(other.GetOrder());
63         data_.SetSearchValue(other.GetSearchValue());
64         data_.SetSearchProperty(other.GetSearchProperty());
65         data_.SetMaxListCount(other.GetMaxListCount());
66         data_.SetListStartOffset(other.GetListStartOffset());
67         data_.SetResetNewMissedCalls(other.GetResetNewMissedCalls());
68         data_.SetvCardSelector(other.GetvCardSelector());
69         data_.SetvCardSelectorOp(other.GetvCardSelectorOp());
70         data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
71     }
72     virtual ~IPbapPullvCardListingParamAdapter() = default;
operator bluetooth::IPbapPullvCardListingParam() const73     operator bluetooth::IPbapPullvCardListingParam() const
74     {
75         return data_;
76     }
77 
78 private:
79     bluetooth::IPbapPullvCardListingParam data_;
80 };
81 
82 class IPbapPullvCardEntryParamAdapter {
83 public:
84     IPbapPullvCardEntryParamAdapter() = default;
IPbapPullvCardEntryParamAdapter(const BluetoothIPbapPullvCardEntryParam & other)85     IPbapPullvCardEntryParamAdapter(const BluetoothIPbapPullvCardEntryParam &other)
86     {
87         data_.SetName(other.GetName());
88         data_.SetPropertySelector(other.GetPropertySelector());
89         data_.SetFormat(other.GetFormat());
90         data_.SetSpecifiedBitset(other.GetSpecifiedBitset());
91     }
92     virtual ~IPbapPullvCardEntryParamAdapter() = default;
operator bluetooth::IPbapPullvCardEntryParam() const93     operator bluetooth::IPbapPullvCardEntryParam() const
94     {
95         return data_;
96     }
97 
98 private:
99     bluetooth::IPbapPullvCardEntryParam data_;
100 };
101 
102 class BluetoothIPbapPhoneBookDataAdapter {
103 public:
104     BluetoothIPbapPhoneBookDataAdapter() = default;
BluetoothIPbapPhoneBookDataAdapter(const bluetooth::IPbapPhoneBookData & other)105     BluetoothIPbapPhoneBookDataAdapter(const bluetooth::IPbapPhoneBookData &other)
106     {
107         data_.phoneBookSize_ = other.phoneBookSize_;
108         data_.primaryFolderVersion_ = other.primaryFolderVersion_;
109         data_.secondaryFolderVersion_ = other.secondaryFolderVersion_;
110         data_.databaseIdentifier_ = other.databaseIdentifier_;
111         data_.deviceAddr_ = other.deviceAddr_;
112         data_.vcardPath_ = other.vcardPath_;
113         data_.vcardFileName_ = other.vcardFileName_;
114         data_.resultLoaded_ = other.resultLoaded_;
115         data_.result_ = other.result_;
116     }
117     virtual ~BluetoothIPbapPhoneBookDataAdapter() = default;
operator BluetoothIPbapPhoneBookData() const118     operator BluetoothIPbapPhoneBookData() const
119     {
120         return data_;
121     }
122 
123 private:
124     BluetoothIPbapPhoneBookData data_;
125 };
126 
127 class PbapPceObserver : public bluetooth::IPbapPceObserver {
128 public:
129     PbapPceObserver() = default;
130     ~PbapPceObserver() = default;
OnServiceConnectionStateChanged(const bluetooth::RawAddress & remoteAddr,int state)131     void OnServiceConnectionStateChanged(const bluetooth::RawAddress &remoteAddr, int state) override
132     {
133         HILOGI("state: %{public}d", state);
134         observers_->ForEach([remoteAddr, state](IBluetoothPbapPceObserver *observer) {
135             observer->OnServiceConnectionStateChanged(remoteAddr, state);
136         });
137     }
OnServicePasswordRequired(const bluetooth::RawAddress & remoteAddr,const std::vector<uint8_t> & description,uint8_t charset,bool fullAccess)138     void OnServicePasswordRequired(const bluetooth::RawAddress &remoteAddr,
139         const std::vector<uint8_t> &description, uint8_t charset, bool fullAccess) override
140     {
141         HILOGI("charset: %{public}d, fullAccess: %{public}d", charset, fullAccess);
142         observers_->ForEach([remoteAddr, description, charset, fullAccess](IBluetoothPbapPceObserver *observer) {
143             observer->OnServicePasswordRequired(remoteAddr, description, charset, fullAccess);
144         });
145     }
OnActionCompleted(const bluetooth::RawAddress & remoteAddr,int respCode,int actionType,const bluetooth::IPbapPhoneBookData & result)146     void OnActionCompleted(const bluetooth::RawAddress &remoteAddr, int respCode,
147         int actionType, const bluetooth::IPbapPhoneBookData &result) override
148     {
149         HILOGI("respCode: %{public}d, actionType: %{public}d", respCode, actionType);
150         observers_->ForEach([remoteAddr, respCode, actionType, result](IBluetoothPbapPceObserver *observer) {
151             observer->OnActionCompleted(remoteAddr, respCode, actionType, BluetoothIPbapPhoneBookDataAdapter(result));
152         });
153     }
154 
SetObserver(RemoteObserverList<IBluetoothPbapPceObserver> * observers)155     void SetObserver(RemoteObserverList<IBluetoothPbapPceObserver> *observers)
156     {
157         observers_ = observers;
158     }
159 
160 private:
161     RemoteObserverList<IBluetoothPbapPceObserver> *observers_{nullptr};
162 };
163 
164 struct BluetoothPbapPceServer::impl {
165     impl();
166     RemoteObserverList<IBluetoothPbapPceObserver> observers_;
167     std::unique_ptr<PbapPceObserver> observerImp_ = {std::make_unique<PbapPceObserver>()};
168     bluetooth::IProfilePbapPce *pbapPceService_{nullptr};
169     class SystemStateObserver;
170     std::unique_ptr<SystemStateObserver> systemStateObserver_{nullptr};
171 };
172 
173 class BluetoothPbapPceServer::impl::SystemStateObserver : public bluetooth::ISystemStateObserver {
174 public:
SystemStateObserver(BluetoothPbapPceServer::impl * impl)175     SystemStateObserver(BluetoothPbapPceServer::impl *impl) : impl_(impl) {};
176     ~SystemStateObserver() = default;
177 
OnSystemStateChange(const bluetooth::BTSystemState state)178     void OnSystemStateChange(const bluetooth::BTSystemState state) override
179     {
180         switch (state) {
181             case bluetooth::BTSystemState::ON: {
182                 bluetooth::IProfileManager *serviceManager = bluetooth::IProfileManager::GetInstance();
183                 if (serviceManager != nullptr) {
184                     bluetooth::IProfile *profileService =
185                         serviceManager->GetProfileService(bluetooth::PROFILE_NAME_PBAP_PCE);
186                     if (profileService) {
187                         impl_->pbapPceService_ = (bluetooth::IProfilePbapPce *)profileService;
188                         impl_->pbapPceService_->RegisterObserver(*(impl_->observerImp_));  // re regist observer
189                     } else {
190                         HILOGI("can't find PbapPceService from ProfileServiceManager!");
191                     }
192                 } else {
193                     HILOGI("can't find ProfileServiceManager!");
194                 }
195             } break;
196             case bluetooth::BTSystemState::OFF:
197                 impl_->pbapPceService_ = nullptr;
198                 break;
199             default:
200                 break;
201         }
202     }
203 
204 private:
205     BluetoothPbapPceServer::impl *impl_{nullptr};
206 };
207 
impl()208 BluetoothPbapPceServer::impl::impl()
209 {
210     systemStateObserver_ = std::make_unique<SystemStateObserver>(this);
211     bluetooth::IAdapterManager::GetInstance()->RegisterSystemStateObserver(*systemStateObserver_);
212 }
213 
BluetoothPbapPceServer()214 BluetoothPbapPceServer::BluetoothPbapPceServer()
215 {
216     pimpl = std::make_unique<impl>();
217     pimpl->observerImp_->SetObserver(&(pimpl->observers_));
218     bluetooth::IProfileManager *serviceManager = bluetooth::IProfileManager::GetInstance();
219     if (serviceManager != nullptr) {
220         bluetooth::IProfile *profileService = serviceManager->GetProfileService(bluetooth::PROFILE_NAME_PBAP_PCE);
221         if (profileService) {
222             pimpl->pbapPceService_ = (bluetooth::IProfilePbapPce *)profileService;
223             // register service callback
224             pimpl->pbapPceService_->RegisterObserver(*(pimpl->observerImp_));
225         } else {
226             HILOGE("can't find PbapPceService from ProfileServiceManager!");
227         }
228     } else {
229         HILOGE("can't find ProfileServiceManager!");
230     }
231 }
232 
~BluetoothPbapPceServer()233 BluetoothPbapPceServer::~BluetoothPbapPceServer()
234 {}
235 
GetDeviceState(const BluetoothRawAddress & device)236 int BluetoothPbapPceServer::GetDeviceState(const BluetoothRawAddress &device)
237 {
238     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
239     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
240         HILOGE("false, check permission failed");
241         return BT_FAILURE;
242     }
243     if (pimpl->pbapPceService_) {
244         return pimpl->pbapPceService_->GetDeviceState(device);
245     } else {
246         HILOGE("pimpl->pbapPceService_ null");
247         return RET_BAD_STATUS;
248     }
249 }
250 
Connect(const BluetoothRawAddress & device)251 int BluetoothPbapPceServer::Connect(const BluetoothRawAddress &device)
252 {
253     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
254     if (pimpl->pbapPceService_) {
255         return pimpl->pbapPceService_->Connect(device);
256     } else {
257         HILOGE("pimpl->pbapPceService_ null");
258         return RET_BAD_STATUS;
259     }
260 }
261 
Disconnect(const BluetoothRawAddress & device)262 int BluetoothPbapPceServer::Disconnect(const BluetoothRawAddress &device)
263 {
264     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
265     if (pimpl->pbapPceService_) {
266         return pimpl->pbapPceService_->Disconnect(device);
267     } else {
268         HILOGE("pimpl->pbapPceService_ null");
269         return RET_BAD_STATUS;
270     }
271 }
272 
PullPhoneBook(const BluetoothRawAddress & device,const BluetoothIPbapPullPhoneBookParam & param)273 int BluetoothPbapPceServer::PullPhoneBook(const BluetoothRawAddress &device,
274     const BluetoothIPbapPullPhoneBookParam &param)
275 {
276     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
277     if (pimpl->pbapPceService_) {
278         return pimpl->pbapPceService_->PullPhoneBook(device, IPbapPullPhoneBookParamAdapter(param));
279     } else {
280         HILOGE("pimpl->pbapPceService_ null");
281         return RET_BAD_STATUS;
282     }
283 }
284 
SetPhoneBook(const BluetoothRawAddress & device,const std::u16string & name,int32_t flag)285 int BluetoothPbapPceServer::SetPhoneBook(const BluetoothRawAddress &device, const std::u16string &name, int32_t flag)
286 {
287     HILOGI("device: %{public}s, flag: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), flag);
288     if (pimpl->pbapPceService_) {
289         return pimpl->pbapPceService_->SetPhoneBook(device, name.c_str(), flag);
290     } else {
291         HILOGE("pimpl->pbapPceService_ null");
292         return RET_BAD_STATUS;
293     }
294 }
295 
PullvCardListing(const BluetoothRawAddress & device,const BluetoothIPbapPullvCardListingParam & param)296 int BluetoothPbapPceServer::PullvCardListing(const BluetoothRawAddress &device,
297     const BluetoothIPbapPullvCardListingParam &param)
298 {
299     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
300     if (pimpl->pbapPceService_) {
301         return pimpl->pbapPceService_->PullvCardListing(device, IPbapPullvCardListingParamAdapter(param));
302     } else {
303         HILOGE("pimpl->pbapPceService_ null");
304         return RET_BAD_STATUS;
305     }
306 }
307 
PullvCardEntry(const BluetoothRawAddress & device,const BluetoothIPbapPullvCardEntryParam & param)308 int BluetoothPbapPceServer::PullvCardEntry(const BluetoothRawAddress &device,
309     const BluetoothIPbapPullvCardEntryParam &param)
310 {
311     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
312     if (pimpl->pbapPceService_) {
313         return pimpl->pbapPceService_->PullvCardEntry(device, IPbapPullvCardEntryParamAdapter(param));
314     } else {
315         HILOGE("pimpl->pbapPceService_ null");
316         return RET_BAD_STATUS;
317     }
318 }
319 
IsDownloading(const BluetoothRawAddress & device)320 bool BluetoothPbapPceServer::IsDownloading(const BluetoothRawAddress &device)
321 {
322     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
323     if (pimpl->pbapPceService_) {
324         return pimpl->pbapPceService_->IsDownloading(device);
325     } else {
326         HILOGE("pimpl->pbapPceService_ null");
327         return false;
328     }
329 }
330 
AbortDownloading(const BluetoothRawAddress & device)331 int BluetoothPbapPceServer::AbortDownloading(const BluetoothRawAddress &device)
332 {
333     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
334     if (pimpl->pbapPceService_) {
335         return pimpl->pbapPceService_->AbortDownloading(device);
336     } else {
337         HILOGE("pimpl->pbapPceService_ null");
338         return RET_BAD_STATUS;
339     }
340 }
341 
SetDevicePassword(const BluetoothRawAddress & device,const std::string & password,const std::string & userId)342 int BluetoothPbapPceServer::SetDevicePassword(const BluetoothRawAddress &device,
343     const std::string &password, const std::string &userId)
344 {
345     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
346     if (pimpl->pbapPceService_) {
347         std::string pwdTmp = password;
348         std::string usrIdTmp = userId;
349         return pimpl->pbapPceService_->SetDevicePassword(device, pwdTmp, usrIdTmp);
350     } else {
351         HILOGE("pimpl->pbapPceService_ null");
352         return RET_BAD_STATUS;
353     }
354 }
355 
SetConnectionStrategy(const BluetoothRawAddress & device,int32_t strategy)356 int BluetoothPbapPceServer::SetConnectionStrategy(const BluetoothRawAddress &device, int32_t strategy)
357 {
358     HILOGI("device: %{public}s, strategy: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), strategy);
359     if (pimpl->pbapPceService_) {
360         return pimpl->pbapPceService_->SetConnectionStrategy(device, strategy);
361     } else {
362         HILOGE("pimpl->pbapPceService_ null");
363         return RET_BAD_STATUS;
364     }
365 }
366 
GetConnectionStrategy(const BluetoothRawAddress & device)367 int BluetoothPbapPceServer::GetConnectionStrategy(const BluetoothRawAddress &device)
368 {
369     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
370     if (pimpl->pbapPceService_) {
371         return pimpl->pbapPceService_->GetConnectionStrategy(device);
372     } else {
373         HILOGE("pimpl->pbapPceService_ null");
374         return RET_BAD_STATUS;
375     }
376 }
377 
GetDevicesByStates(const::std::vector<int32_t> states,std::vector<BluetoothRawAddress> & rawDevices)378 void BluetoothPbapPceServer::GetDevicesByStates(const ::std::vector<int32_t> states,
379     std::vector<BluetoothRawAddress> &rawDevices)
380 {
381     HILOGI("Enter!");
382     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
383         HILOGE("false, check permission failed");
384         return;
385     }
386     if (pimpl->pbapPceService_ != nullptr) {
387         std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->pbapPceService_->GetDevicesByStates(states);
388         for (auto &device : serviceDeviceList) {
389             BluetoothRawAddress bluetoothDevice(device.GetAddress());
390             rawDevices.push_back(bluetoothDevice);
391         }
392         return;
393     } else {
394         HILOGE("pimpl->pbapPceService_ null");
395         return;
396     }
397 }
398 
GetConnectedDevices()399 ::std::vector<BluetoothRawAddress> BluetoothPbapPceServer::GetConnectedDevices()
400 {
401     HILOGI("Enter!");
402     ::std::vector<BluetoothRawAddress> bluetoothDeviceList;
403     if (pimpl->pbapPceService_ != nullptr) {
404         const ::std::vector<int32_t> states{static_cast<int32_t>(BTConnectState::CONNECTED)};
405         std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->pbapPceService_->GetDevicesByStates(states);
406         for (auto &device : serviceDeviceList) {
407             BluetoothRawAddress bluetoothDevice(device.GetAddress());
408             bluetoothDeviceList.push_back(bluetoothDevice);
409         }
410         return bluetoothDeviceList;
411     } else {
412         HILOGE("pimpl->pbapPceService_ null");
413         return bluetoothDeviceList;
414     }
415 }
416 
RegisterObserver(const sptr<IBluetoothPbapPceObserver> & observer)417 void BluetoothPbapPceServer::RegisterObserver(const sptr<IBluetoothPbapPceObserver> &observer)
418 {
419     HILOGI("Enter!");
420     if (!observer) {
421         HILOGE("called with NULL observer. Ignoring.");
422         return;
423     }
424     pimpl->observers_.Register(observer);
425 }
426 
DeregisterObserver(const sptr<IBluetoothPbapPceObserver> & observer)427 void BluetoothPbapPceServer::DeregisterObserver(const sptr<IBluetoothPbapPceObserver> &observer)
428 {
429     HILOGI("Enter!");
430     if (!observer) {
431         HILOGE("called with NULL observer. Ignoring.");
432         return;
433     }
434     pimpl->observers_.Deregister(observer);
435 }
436 }  // namespace Bluetooth
437 }  // namespace OHOS