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(
139 const bluetooth::RawAddress &remoteAddr, 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 = serviceManager->GetProfileService(bluetooth::PROFILE_NAME_PBAP_PCE);
185 if (profileService) {
186 impl_->pbapPceService_ = (bluetooth::IProfilePbapPce *)profileService;
187 impl_->pbapPceService_->RegisterObserver(*(impl_->observerImp_)); // re regist observer
188 } else {
189 HILOGI("can't find PbapPceService from ProfileServiceManager!");
190 }
191 } else {
192 HILOGI("can't find ProfileServiceManager!");
193 }
194 } break;
195 case bluetooth::BTSystemState::OFF:
196 impl_->pbapPceService_ = nullptr;
197 break;
198 default:
199 break;
200 }
201 }
202
203 private:
204 BluetoothPbapPceServer::impl *impl_{nullptr};
205 };
206
impl()207 BluetoothPbapPceServer::impl::impl()
208 {
209 systemStateObserver_ = std::make_unique<SystemStateObserver>(this);
210 bluetooth::IAdapterManager::GetInstance()->RegisterSystemStateObserver(*systemStateObserver_);
211 }
212
BluetoothPbapPceServer()213 BluetoothPbapPceServer::BluetoothPbapPceServer()
214 {
215 pimpl = std::make_unique<impl>();
216 pimpl->observerImp_->SetObserver(&(pimpl->observers_));
217 bluetooth::IProfileManager *serviceManager = bluetooth::IProfileManager::GetInstance();
218 if (serviceManager != nullptr) {
219 bluetooth::IProfile *profileService = serviceManager->GetProfileService(bluetooth::PROFILE_NAME_PBAP_PCE);
220 if (profileService) {
221 pimpl->pbapPceService_ = (bluetooth::IProfilePbapPce *)profileService;
222 // register service callback
223 pimpl->pbapPceService_->RegisterObserver(*(pimpl->observerImp_));
224 } else {
225 HILOGE("can't find PbapPceService from ProfileServiceManager!");
226 }
227 } else {
228 HILOGE("can't find ProfileServiceManager!");
229 }
230 }
231
~BluetoothPbapPceServer()232 BluetoothPbapPceServer::~BluetoothPbapPceServer()
233 {}
234
GetDeviceState(const BluetoothRawAddress & device)235 int BluetoothPbapPceServer::GetDeviceState(const BluetoothRawAddress &device)
236 {
237 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
238 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
239 HILOGE("false, check permission failed");
240 return BT_FAILURE;
241 }
242 if (pimpl->pbapPceService_) {
243 return pimpl->pbapPceService_->GetDeviceState(device);
244 } else {
245 HILOGE("pimpl->pbapPceService_ null");
246 return RET_BAD_STATUS;
247 }
248 }
249
Connect(const BluetoothRawAddress & device)250 int BluetoothPbapPceServer::Connect(const BluetoothRawAddress &device)
251 {
252 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
253 if (pimpl->pbapPceService_) {
254 return pimpl->pbapPceService_->Connect(device);
255 } else {
256 HILOGE("pimpl->pbapPceService_ null");
257 return RET_BAD_STATUS;
258 }
259 }
260
Disconnect(const BluetoothRawAddress & device)261 int BluetoothPbapPceServer::Disconnect(const BluetoothRawAddress &device)
262 {
263 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
264 if (pimpl->pbapPceService_) {
265 return pimpl->pbapPceService_->Disconnect(device);
266 } else {
267 HILOGE("pimpl->pbapPceService_ null");
268 return RET_BAD_STATUS;
269 }
270 }
271
PullPhoneBook(const BluetoothRawAddress & device,const BluetoothIPbapPullPhoneBookParam & param)272 int BluetoothPbapPceServer::PullPhoneBook(const BluetoothRawAddress &device,
273 const BluetoothIPbapPullPhoneBookParam ¶m)
274 {
275 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
276 if (pimpl->pbapPceService_) {
277 return pimpl->pbapPceService_->PullPhoneBook(device, IPbapPullPhoneBookParamAdapter(param));
278 } else {
279 HILOGE("pimpl->pbapPceService_ null");
280 return RET_BAD_STATUS;
281 }
282 }
283
SetPhoneBook(const BluetoothRawAddress & device,const std::u16string & name,int32_t flag)284 int BluetoothPbapPceServer::SetPhoneBook(const BluetoothRawAddress &device, const std::u16string &name, int32_t flag)
285 {
286 HILOGI("device: %{public}s, flag: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), flag);
287 if (pimpl->pbapPceService_) {
288 return pimpl->pbapPceService_->SetPhoneBook(device, name.c_str(), flag);
289 } else {
290 HILOGE("pimpl->pbapPceService_ null");
291 return RET_BAD_STATUS;
292 }
293 }
294
PullvCardListing(const BluetoothRawAddress & device,const BluetoothIPbapPullvCardListingParam & param)295 int BluetoothPbapPceServer::PullvCardListing(const BluetoothRawAddress &device,
296 const BluetoothIPbapPullvCardListingParam ¶m)
297 {
298 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
299 if (pimpl->pbapPceService_) {
300 return pimpl->pbapPceService_->PullvCardListing(device, IPbapPullvCardListingParamAdapter(param));
301 } else {
302 HILOGE("pimpl->pbapPceService_ null");
303 return RET_BAD_STATUS;
304 }
305 }
306
PullvCardEntry(const BluetoothRawAddress & device,const BluetoothIPbapPullvCardEntryParam & param)307 int BluetoothPbapPceServer::PullvCardEntry(const BluetoothRawAddress &device,
308 const BluetoothIPbapPullvCardEntryParam ¶m)
309 {
310 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
311 if (pimpl->pbapPceService_) {
312 return pimpl->pbapPceService_->PullvCardEntry(device, IPbapPullvCardEntryParamAdapter(param));
313 } else {
314 HILOGE("pimpl->pbapPceService_ null");
315 return RET_BAD_STATUS;
316 }
317 }
318
IsDownloading(const BluetoothRawAddress & device)319 bool BluetoothPbapPceServer::IsDownloading(const BluetoothRawAddress &device)
320 {
321 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
322 if (pimpl->pbapPceService_) {
323 return pimpl->pbapPceService_->IsDownloading(device);
324 } else {
325 HILOGE("pimpl->pbapPceService_ null");
326 return false;
327 }
328 }
329
AbortDownloading(const BluetoothRawAddress & device)330 int BluetoothPbapPceServer::AbortDownloading(const BluetoothRawAddress &device)
331 {
332 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
333 if (pimpl->pbapPceService_) {
334 return pimpl->pbapPceService_->AbortDownloading(device);
335 } else {
336 HILOGE("pimpl->pbapPceService_ null");
337 return RET_BAD_STATUS;
338 }
339 }
340
SetDevicePassword(const BluetoothRawAddress & device,const std::string & password,const std::string & userId)341 int BluetoothPbapPceServer::SetDevicePassword(const BluetoothRawAddress &device,
342 const std::string &password, const std::string &userId)
343 {
344 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
345 if (pimpl->pbapPceService_) {
346 std::string pwdTmp = password;
347 std::string usrIdTmp = userId;
348 return pimpl->pbapPceService_->SetDevicePassword(device, pwdTmp, usrIdTmp);
349 } else {
350 HILOGE("pimpl->pbapPceService_ null");
351 return RET_BAD_STATUS;
352 }
353 }
354
SetConnectionStrategy(const BluetoothRawAddress & device,int32_t strategy)355 int BluetoothPbapPceServer::SetConnectionStrategy(const BluetoothRawAddress &device, int32_t strategy)
356 {
357 HILOGI("device: %{public}s, strategy: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), strategy);
358 if (pimpl->pbapPceService_) {
359 return pimpl->pbapPceService_->SetConnectionStrategy(device, strategy);
360 } else {
361 HILOGE("pimpl->pbapPceService_ null");
362 return RET_BAD_STATUS;
363 }
364 }
365
GetConnectionStrategy(const BluetoothRawAddress & device)366 int BluetoothPbapPceServer::GetConnectionStrategy(const BluetoothRawAddress &device)
367 {
368 HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
369 if (pimpl->pbapPceService_) {
370 return pimpl->pbapPceService_->GetConnectionStrategy(device);
371 } else {
372 HILOGE("pimpl->pbapPceService_ null");
373 return RET_BAD_STATUS;
374 }
375 }
376
GetDevicesByStates(const::std::vector<int32_t> states,std::vector<BluetoothRawAddress> & rawDevices)377 void BluetoothPbapPceServer::GetDevicesByStates(const ::std::vector<int32_t> states,
378 std::vector<BluetoothRawAddress> &rawDevices)
379 {
380 HILOGI("Enter!");
381 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
382 HILOGE("false, check permission failed");
383 return;
384 }
385 if (pimpl->pbapPceService_ != nullptr) {
386 std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->pbapPceService_->GetDevicesByStates(states);
387 for (auto &device : serviceDeviceList) {
388 BluetoothRawAddress bluetoothDevice(device.GetAddress());
389 rawDevices.push_back(bluetoothDevice);
390 }
391 return;
392 } else {
393 HILOGE("pimpl->pbapPceService_ null");
394 return;
395 }
396 }
397
GetConnectedDevices()398 ::std::vector<BluetoothRawAddress> BluetoothPbapPceServer::GetConnectedDevices()
399 {
400 HILOGI("Enter!");
401 ::std::vector<BluetoothRawAddress> bluetoothDeviceList;
402 if (pimpl->pbapPceService_ != nullptr) {
403 const ::std::vector<int32_t> states{static_cast<int32_t>(BTConnectState::CONNECTED)};
404 std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->pbapPceService_->GetDevicesByStates(states);
405 for (auto &device : serviceDeviceList) {
406 BluetoothRawAddress bluetoothDevice(device.GetAddress());
407 bluetoothDeviceList.push_back(bluetoothDevice);
408 }
409 return bluetoothDeviceList;
410 } else {
411 HILOGE("pimpl->pbapPceService_ null");
412 return bluetoothDeviceList;
413 }
414 }
415
RegisterObserver(const sptr<IBluetoothPbapPceObserver> & observer)416 void BluetoothPbapPceServer::RegisterObserver(const sptr<IBluetoothPbapPceObserver> &observer)
417 {
418 HILOGI("Enter!");
419 if (!observer) {
420 HILOGE("called with NULL observer. Ignoring.");
421 return;
422 }
423 pimpl->observers_.Register(observer);
424 }
425
DeregisterObserver(const sptr<IBluetoothPbapPceObserver> & observer)426 void BluetoothPbapPceServer::DeregisterObserver(const sptr<IBluetoothPbapPceObserver> &observer)
427 {
428 HILOGI("Enter!");
429 if (!observer) {
430 HILOGE("called with NULL observer. Ignoring.");
431 return;
432 }
433 pimpl->observers_.Deregister(observer);
434 }
435 } // namespace Bluetooth
436 } // namespace OHOS