• 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 "bluetooth_host.h"
17 #include <memory>
18 #include <unistd.h>
19 #include "bluetooth_ble_peripheral_observer_stub.h"
20 #include "bluetooth_host_observer_stub.h"
21 #include "bluetooth_host_proxy.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_observer_list.h"
24 #include "bluetooth_remote_device_observer_stub.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 
28 namespace OHOS {
29 namespace Bluetooth {
30 struct BluetoothHost::impl {
31     impl();
32     ~impl();
33 
34     // host observer
35     class BluetoothHostObserverImp;
36     sptr<BluetoothHostObserverImp> observerImp_ = nullptr;
37     sptr<BluetoothHostObserverImp> bleObserverImp_ = nullptr;
38 
39     // remote device observer
40     class BluetoothRemoteDeviceObserverImp;
41     sptr<BluetoothRemoteDeviceObserverImp> remoteObserverImp_ = nullptr;
42 
43     // remote device observer
44     class BluetoothBlePeripheralCallbackImp;
45     sptr<BluetoothBlePeripheralCallbackImp> bleRemoteObserverImp_ = nullptr;
46 
47     // user regist observers
48     BluetoothObserverList<BluetoothHostObserver> observers_;
49 
50     // user regist remote observers
51     BluetoothObserverList<BluetoothRemoteDeviceObserver> remoteObservers_;
52 
53     sptr<IBluetoothHost> proxy_ = nullptr;
54 
55 private:
56     void GetProxy();
57 };
58 
59 class BluetoothHost::impl::BluetoothHostObserverImp : public BluetoothHostObserverStub {
60 public:
BluetoothHostObserverImp(BluetoothHost::impl & host)61     BluetoothHostObserverImp(BluetoothHost::impl &host) : host_(host){};
~BluetoothHostObserverImp()62     ~BluetoothHostObserverImp() override{};
63 
Register(std::shared_ptr<BluetoothHostObserver> & observer)64     void Register(std::shared_ptr<BluetoothHostObserver> &observer)
65     {
66         host_.observers_.Register(observer);
67     }
68 
Deregister(std::shared_ptr<BluetoothHostObserver> & observer)69     void Deregister(std::shared_ptr<BluetoothHostObserver> &observer)
70     {
71         host_.observers_.Deregister(observer);
72     }
73 
OnStateChanged(int32_t transport,int32_t status)74     void OnStateChanged(int32_t transport, int32_t status) override
75     {
76         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
77         host_.observers_.ForEach([transport, status](std::shared_ptr<BluetoothHostObserver> observer) {
78             observer->OnStateChanged(transport, status);
79         });
80     }
81 
OnDiscoveryStateChanged(int32_t status)82     void OnDiscoveryStateChanged(int32_t status) override
83     {
84         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
85         host_.observers_.ForEach(
86             [status](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDiscoveryStateChanged(status); });
87     }
88 
OnDiscoveryResult(const BluetoothRawAddress & device)89     void OnDiscoveryResult(const BluetoothRawAddress &device) override
90     {
91         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
92         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
93         host_.observers_.ForEach([remoteDevice](std::shared_ptr<BluetoothHostObserver> observer) {
94             observer->OnDiscoveryResult(remoteDevice);
95         });
96     }
97 
OnPairRequested(const int32_t transport,const BluetoothRawAddress & device)98     void OnPairRequested(const int32_t transport, const BluetoothRawAddress &device) override
99     {
100         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
101         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
102         host_.observers_.ForEach([remoteDevice](std::shared_ptr<BluetoothHostObserver> observer) {
103             observer->OnPairRequested(remoteDevice);
104         });
105     }
106 
OnPairConfirmed(const int32_t transport,const BluetoothRawAddress & device,int reqType,int number)107     void OnPairConfirmed(const int32_t transport, const BluetoothRawAddress &device, int reqType, int number) override
108     {
109         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
110         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
111         host_.observers_.ForEach([remoteDevice, reqType, number](std::shared_ptr<BluetoothHostObserver> observer) {
112             observer->OnPairConfirmed(remoteDevice, reqType, number);
113         });
114     }
115 
OnScanModeChanged(int mode)116     void OnScanModeChanged(int mode) override
117     {
118         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
119         host_.observers_.ForEach(
120             [mode](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnScanModeChanged(mode); });
121     }
122 
OnDeviceNameChanged(const std::string & deviceName)123     void OnDeviceNameChanged(const std::string &deviceName) override
124     {
125         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
126         host_.observers_.ForEach([deviceName](std::shared_ptr<BluetoothHostObserver> observer) {
127             observer->OnDeviceNameChanged(deviceName);
128         });
129     }
130 
OnDeviceAddrChanged(const std::string & address)131     void OnDeviceAddrChanged(const std::string &address) override
132     {
133         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
134         host_.observers_.ForEach(
135             [address](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDeviceAddrChanged(address); });
136     }
137 
138 private:
139     BluetoothHost::impl &host_;
140     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHostObserverImp);
141 };
142 
143 class BluetoothHost::impl::BluetoothRemoteDeviceObserverImp : public BluetoothRemoteDeviceObserverstub {
144 public:
BluetoothRemoteDeviceObserverImp(BluetoothHost::impl & host)145     BluetoothRemoteDeviceObserverImp(BluetoothHost::impl &host) : host_(host){};
146     ~BluetoothRemoteDeviceObserverImp() override = default;
147 
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int32_t status)148     void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device, int32_t status) override
149     {
150         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
151         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
152 
153         host_.remoteObservers_.ForEach([remoteDevice, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
154             observer->OnPairStatusChanged(remoteDevice, status);
155         });
156     }
157 
OnRemoteUuidChanged(const BluetoothRawAddress & device,const std::vector<bluetooth::Uuid> uuids)158     void OnRemoteUuidChanged(const BluetoothRawAddress &device, const std::vector<bluetooth::Uuid> uuids) override
159     {
160         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
161         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
162         host_.remoteObservers_.ForEach([remoteDevice, uuids](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
163             std::vector<ParcelUuid> parcelUuids;
164             for (auto &uuid : uuids) {
165                 ParcelUuid parcelUuid = UUID::ConvertFrom128Bits(uuid.ConvertTo128Bits());
166                 parcelUuids.push_back(parcelUuid);
167                 observer->OnRemoteUuidChanged(remoteDevice, parcelUuids);
168             }
169         });
170     }
171 
OnRemoteNameChanged(const BluetoothRawAddress & device,const std::string deviceName)172     void OnRemoteNameChanged(const BluetoothRawAddress &device, const std::string deviceName) override
173     {
174         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
175         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
176         host_.remoteObservers_.ForEach(
177             [remoteDevice, deviceName](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
178                 observer->OnRemoteNameChanged(remoteDevice, deviceName);
179             });
180     }
181 
OnRemoteAliasChanged(const BluetoothRawAddress & device,const std::string alias)182     void OnRemoteAliasChanged(const BluetoothRawAddress &device, const std::string alias) override
183     {
184         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
185         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
186         host_.remoteObservers_.ForEach([remoteDevice, alias](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
187             observer->OnRemoteAliasChanged(remoteDevice, alias);
188         });
189     }
190 
OnRemoteCodChanged(const BluetoothRawAddress & device,int32_t cod)191     void OnRemoteCodChanged(const BluetoothRawAddress &device, int32_t cod) override
192     {
193         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
194         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
195         BluetoothDeviceClass deviceClass(cod);
196         host_.remoteObservers_.ForEach(
197             [remoteDevice, deviceClass](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
198                 observer->OnRemoteCodChanged(remoteDevice, deviceClass);
199             });
200     }
201 
OnRemoteBatteryLevelChanged(const BluetoothRawAddress & device,int32_t batteryLevel)202     void OnRemoteBatteryLevelChanged(const BluetoothRawAddress &device, int32_t batteryLevel) override
203     {
204         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
205         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
206         host_.remoteObservers_.ForEach(
207             [remoteDevice, batteryLevel](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
208                 observer->OnRemoteBatteryLevelChanged(remoteDevice, batteryLevel);
209             });
210     }
211 
212 private:
213     BluetoothHost::impl &host_;
214     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteDeviceObserverImp);
215 };
216 
217 class BluetoothHost::impl::BluetoothBlePeripheralCallbackImp : public BluetoothBlePeripheralObserverStub {
218 public:
BluetoothBlePeripheralCallbackImp(BluetoothHost::impl & host)219     BluetoothBlePeripheralCallbackImp(BluetoothHost::impl &host) : host_(host){};
220     ~BluetoothBlePeripheralCallbackImp() override = default;
221 
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int status)222     void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device, int status) override
223     {
224         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
225         host_.remoteObservers_.ForEach([remoteDevice, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
226             observer->OnPairStatusChanged(remoteDevice, status);
227         });
228     }
229 
OnReadRemoteRssiEvent(const BluetoothRawAddress & device,int rssi,int status)230     void OnReadRemoteRssiEvent(const BluetoothRawAddress &device, int rssi, int status) override
231     {
232         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
233         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
234         host_.remoteObservers_.ForEach(
235             [remoteDevice, rssi, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
236                 observer->OnReadRemoteRssiEvent(remoteDevice, rssi, status);
237             });
238     }
239 
240 private:
241     BluetoothHost::impl &host_;
242     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBlePeripheralCallbackImp);
243 };
244 
impl()245 BluetoothHost::impl::impl()
246 {
247     HILOGD("BluetoothHost::impl::impl  starts");
248     observerImp_ = new (std::nothrow) BluetoothHostObserverImp(*this);
249     if (observerImp_ == nullptr) {
250         return;
251     }
252 
253     remoteObserverImp_ = new (std::nothrow) BluetoothRemoteDeviceObserverImp(*this);
254     if (remoteObserverImp_ == nullptr) {
255         return;
256     }
257 
258     bleRemoteObserverImp_ = new (std::nothrow) BluetoothBlePeripheralCallbackImp(*this);
259     if (bleRemoteObserverImp_ == nullptr) {
260         return;
261     }
262 
263     bleObserverImp_ = new (std::nothrow) BluetoothHostObserverImp(*this);
264     if (bleObserverImp_ == nullptr) {
265         return;
266     }
267 
268     GetProxy();
269 
270     proxy_->RegisterObserver(observerImp_);
271     proxy_->RegisterBleAdapterObserver(bleObserverImp_);
272     proxy_->RegisterRemoteDeviceObserver(remoteObserverImp_);
273     proxy_->RegisterBlePeripheralCallback(bleRemoteObserverImp_);
274 }
275 
~impl()276 BluetoothHost::impl::~impl()
277 {
278     HILOGD("BluetoothHost::impl::~impl  starts");
279     proxy_->DeregisterObserver(observerImp_);
280     proxy_->DeregisterBleAdapterObserver(observerImp_);
281     proxy_->DeregisterRemoteDeviceObserver(remoteObserverImp_);
282     proxy_->DeregisterBlePeripheralCallback(bleRemoteObserverImp_);
283 }
284 
GetProxy()285 void BluetoothHost::impl::GetProxy()
286 {
287     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
288     if (!samgr) {
289         HILOGE("BluetoothHost::impl::impl() error: no samgr");
290         return;
291     }
292 
293     sptr<IRemoteObject> remote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
294     if (!remote) {
295         int try_time = 5;
296         while ((!remote) && (try_time--) > 0) {
297             sleep(1);
298             remote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
299         }
300     }
301 
302     if (!remote) {
303         HILOGE("BluetoothHost::impl::impl() error:no remote");
304         return;
305     }
306 
307     proxy_ = iface_cast<IBluetoothHost>(remote);
308     if (!proxy_) {
309         HILOGE("BluetoothHost::impl::impl() error:no proxy");
310         return;
311     }
312 }
313 
314 BluetoothHost BluetoothHost::hostAdapter_;
315 
BluetoothHost()316 BluetoothHost::BluetoothHost()
317 {
318     pimpl = std::make_unique<impl>();
319     if (!pimpl) {
320         HILOGE("BluetoothHost::BluetoothHost fails: no pimpl");
321     }
322 }
323 
~BluetoothHost()324 BluetoothHost::~BluetoothHost()
325 {}
326 
GetDefaultHost()327 BluetoothHost &BluetoothHost::GetDefaultHost()
328 {
329     return hostAdapter_;
330 }
331 
RegisterObserver(BluetoothHostObserver & observer)332 void BluetoothHost::RegisterObserver(BluetoothHostObserver &observer)
333 {
334     if (!pimpl) {
335         HILOGE("BluetoothHost::RegisterObserver fails: no pimpl");
336         return;
337     }
338     std::shared_ptr<BluetoothHostObserver> pointer(&observer, [](BluetoothHostObserver *) {});
339     pimpl->observers_.Register(pointer);
340 }
341 
DeregisterObserver(BluetoothHostObserver & observer)342 void BluetoothHost::DeregisterObserver(BluetoothHostObserver &observer)
343 {
344     if (!pimpl) {
345         HILOGE("BluetoothHost::DeregisterObserver fails: no pimpl");
346         return;
347     }
348     std::shared_ptr<BluetoothHostObserver> pointer(&observer, [](BluetoothHostObserver *) {});
349     pimpl->observers_.Deregister(pointer);
350 }
351 
EnableBt()352 bool BluetoothHost::EnableBt()
353 {
354     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
355     if (!pimpl) {
356         HILOGE("BluetoothHost::EnableBt fails: no pimpl");
357         return false;
358     }
359     if (pimpl->proxy_ == nullptr) {
360         HILOGE("BluetoothHost::EnableBt fails: no proxy");
361         return false;
362     }
363 
364     return pimpl->proxy_->EnableBt();
365 }
366 
DisableBt()367 bool BluetoothHost::DisableBt()
368 {
369     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
370     if (!pimpl) {
371         HILOGE("BluetoothHost::DisableBt fails: no pimpl");
372         return false;
373     }
374     if (pimpl->proxy_ == nullptr) {
375         HILOGE("BluetoothHost::DisableBt fails: no proxy");
376         return false;
377     }
378     return pimpl->proxy_->DisableBt();
379 }
380 
GetBtState() const381 int BluetoothHost::GetBtState() const
382 {
383     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
384     if (!pimpl) {
385         HILOGE("BluetoothHost::GetBtState fails: no pimpl");
386         return INVALID_VALUE;
387     }
388     if (pimpl->proxy_ == nullptr) {
389         HILOGE("BluetoothHost::GetBtState fails: no proxy");
390         return INVALID_VALUE;
391     }
392     return pimpl->proxy_->GetBtState();
393 }
394 
BluetoothFactoryReset()395 bool BluetoothHost::BluetoothFactoryReset()
396 {
397     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
398     if (!pimpl) {
399         HILOGE("BluetoothHost::BluetoothFactoryReset fails: no pimpl");
400         return false;
401     }
402     if (pimpl->proxy_ == nullptr) {
403         HILOGE("BluetoothHost::BluetoothFactoryReset fails: no proxy");
404         return false;
405     }
406     return pimpl->proxy_->BluetoothFactoryReset();
407 }
408 
IsValidBluetoothAddr(const std::string & addr)409 bool BluetoothHost::IsValidBluetoothAddr(const std::string &addr)
410 {
411     if (addr.empty() || addr.length() != ADDRESS_LENGTH) {
412         return false;
413     }
414     for (int i = 0; i < ADDRESS_LENGTH; i++) {
415         char c = addr[i];
416         switch (i % ADDRESS_SEPARATOR_UNIT) {
417             case 0:
418             case 1:
419                 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
420                     break;
421                 }
422                 return false;
423             case ADDRESS_COLON_INDEX:
424             default:
425                 if (c == ':') {
426                     break;
427                 }
428                 return false;
429         }
430     }
431     return true;
432 }
433 
GetRemoteDevice(const std::string & addr,int transport) const434 BluetoothRemoteDevice BluetoothHost::GetRemoteDevice(const std::string &addr, int transport) const
435 {
436     BluetoothRemoteDevice remoteDevice(addr, transport);
437     return remoteDevice;
438 }
439 
EnableBle()440 bool BluetoothHost::EnableBle()
441 {
442     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
443     if (!pimpl) {
444         HILOGE("BluetoothHost::Enable BLE fails: no pimpl");
445         return false;
446     }
447     if (pimpl->proxy_ == nullptr) {
448         HILOGE("BluetoothHost::Enable fails: no proxy");
449         return false;
450     }
451     return pimpl->proxy_->EnableBle();
452 }
453 
DisableBle()454 bool BluetoothHost::DisableBle()
455 {
456     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
457     if (!pimpl) {
458         HILOGE("BluetoothHost::DisableBle BLE fails: no pimpl");
459         return false;
460     }
461     if (pimpl->proxy_ == nullptr) {
462         HILOGE("BluetoothHost::DisableBle fails: no proxy");
463         return false;
464     }
465     return pimpl->proxy_->DisableBle();
466 }
467 
IsBleEnabled() const468 bool BluetoothHost::IsBleEnabled() const
469 {
470     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
471     if (!pimpl) {
472         HILOGE("BluetoothHost::IsBleEnabled BLE fails: no pimpl");
473         return false;
474     }
475     if (pimpl->proxy_ == nullptr) {
476         HILOGE("BluetoothHost::IsBleEnabled fails: no proxy");
477         return false;
478     }
479     return pimpl->proxy_->IsBleEnabled();
480 }
481 
GetLocalAddress() const482 std::string BluetoothHost::GetLocalAddress() const
483 {
484     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
485     if (!pimpl) {
486         HILOGE("BluetoothHost::GetLocalAddress BLE fails: no pimpl");
487         return std::string();
488     }
489     if (pimpl->proxy_ == nullptr) {
490         HILOGE("BluetoothHost::GetLocalAddress fails: no proxy");
491         return std::string();
492     }
493     return pimpl->proxy_->GetLocalAddress();
494 }
495 
GetProfileList() const496 std::vector<uint32_t> BluetoothHost::GetProfileList() const
497 {
498     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
499     std::vector<uint32_t> profileList;
500     if (!pimpl) {
501         HILOGE("BluetoothHost::GetProfileList BLE fails: no pimpl");
502         return profileList;
503     }
504     if (pimpl->proxy_ == nullptr) {
505         HILOGE("BluetoothHost::GetProfileList fails: no proxy");
506         return profileList;
507     }
508     profileList = pimpl->proxy_->GetProfileList();
509     return profileList;
510 }
511 
GetMaxNumConnectedAudioDevices() const512 int BluetoothHost::GetMaxNumConnectedAudioDevices() const
513 {
514     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
515     if (!pimpl) {
516         HILOGE("BluetoothHost::GetMaxNumConnectedAudioDevices BLE fails: no pimpl");
517         return INVALID_VALUE;
518     }
519     if (pimpl->proxy_ == nullptr) {
520         HILOGE("BluetoothHost::GetMaxNumConnectedAudioDevices fails: no proxy");
521         return INVALID_VALUE;
522     }
523     return pimpl->proxy_->GetMaxNumConnectedAudioDevices();
524 }
525 
GetBtConnectionState() const526 int BluetoothHost::GetBtConnectionState() const
527 {
528     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
529     if (!pimpl) {
530         HILOGE("BluetoothHost::GetBtConnectionState BLE fails: no pimpl");
531         return INVALID_VALUE;
532     }
533     if (pimpl->proxy_ == nullptr) {
534         HILOGE("BluetoothHost::GetBtConnectionState fails: no proxy");
535         return INVALID_VALUE;
536     }
537     return pimpl->proxy_->GetBtConnectionState();
538 }
539 
GetBtProfileConnState(uint32_t profileId) const540 int BluetoothHost::GetBtProfileConnState(uint32_t profileId) const
541 {
542     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
543     if (!pimpl) {
544         HILOGE("BluetoothHost::GetBtProfileConnState BLE fails: no pimpl");
545         return INVALID_VALUE;
546     }
547     if (pimpl->proxy_ == nullptr) {
548         HILOGE("BluetoothHost::GetBtProfileConnState fails: no proxy");
549         return INVALID_VALUE;
550     }
551     return pimpl->proxy_->GetBtProfileConnState(profileId);
552 }
553 
GetLocalSupportedUuids(std::vector<ParcelUuid> & uuids)554 void BluetoothHost::GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids)
555 {
556     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
557     if (!pimpl) {
558         HILOGE("BluetoothHost::GetLocalSupportedUuids BLE fails: no pimpl");
559         return;
560     }
561     if (pimpl->proxy_ == nullptr) {
562         HILOGE("BluetoothHost::GetLocalSupportedUuids fails: no proxy");
563         return;
564     }
565     std::vector<std::string> stringUuids;
566     pimpl->proxy_->GetLocalSupportedUuids(stringUuids);
567     for (auto uuid : stringUuids) {
568         uuids.push_back(UUID::FromString(uuid));
569     }
570 }
571 
Start()572 bool BluetoothHost::Start()
573 {
574     /// This function only used for passthrough, so this is empty.
575     return true;
576 }
577 
Stop()578 void BluetoothHost::Stop()
579 {
580     /// This function only used for passthrough, so this is empty.
581 }
582 
GetLocalDeviceClass() const583 BluetoothDeviceClass BluetoothHost::GetLocalDeviceClass() const
584 {
585     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
586     if (!pimpl) {
587         HILOGE("BluetoothHost::GetLocalDeviceClass BLE fails: no pimpl");
588         return BluetoothDeviceClass(0);
589     }
590     if (pimpl->proxy_ == nullptr) {
591         HILOGE("BluetoothHost::GetLocalDeviceClass fails: no proxy");
592         return BluetoothDeviceClass(0);
593     }
594     int LocalDeviceClass = pimpl->proxy_->GetLocalDeviceClass();
595     return BluetoothDeviceClass(LocalDeviceClass);
596 }
597 
SetLocalDeviceClass(const BluetoothDeviceClass & deviceClass)598 bool BluetoothHost::SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass)
599 {
600     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
601     if (!pimpl) {
602         HILOGE("BluetoothHost::SetLocalDeviceClass BLE fails: no pimpl");
603         return false;
604     }
605     if (pimpl->proxy_ == nullptr) {
606         HILOGE("BluetoothHost::SetLocalDeviceClass fails: no proxy");
607         return false;
608     }
609     int cod = deviceClass.GetClassOfDevice();
610     return pimpl->proxy_->SetLocalDeviceClass(cod);
611 }
612 
GetLocalName() const613 std::string BluetoothHost::GetLocalName() const
614 {
615     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
616     if (!pimpl) {
617         HILOGE("BluetoothHost::GetLocalName BLE fails: no pimpl");
618         return std::string();
619     }
620     if (pimpl->proxy_ == nullptr) {
621         HILOGE("BluetoothHost::GetLocalName fails: no proxy");
622         return std::string();
623     }
624     return pimpl->proxy_->GetLocalName();
625 }
626 
SetLocalName(const std::string & name)627 bool BluetoothHost::SetLocalName(const std::string &name)
628 {
629     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
630     if (!pimpl) {
631         HILOGE("BluetoothHost::SetLocalName BLE fails: no pimpl");
632         return false;
633     }
634     if (pimpl->proxy_ == nullptr) {
635         HILOGE("BluetoothHost::SetLocalName fails: no proxy");
636         return false;
637     }
638     return pimpl->proxy_->SetLocalName(name);
639 }
640 
GetBtScanMode() const641 int BluetoothHost::GetBtScanMode() const
642 {
643     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
644     if (!pimpl) {
645         HILOGE("BluetoothHost::GetBtScanMode BLE fails: no pimpl");
646         return INVALID_VALUE;
647     }
648     if (pimpl->proxy_ == nullptr) {
649         HILOGE("BluetoothHost::GetBtScanMode fails: no proxy");
650         return INVALID_VALUE;
651     }
652     return pimpl->proxy_->GetBtScanMode();
653 }
654 
SetBtScanMode(int mode,int duration)655 bool BluetoothHost::SetBtScanMode(int mode, int duration)
656 {
657     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
658     if (!pimpl) {
659         HILOGE("BluetoothHost::SetBtScanMode BLE fails: no pimpl");
660         return false;
661     }
662     if (pimpl->proxy_ == nullptr) {
663         HILOGE("BluetoothHost::SetBtScanMode fails: no proxy");
664         return false;
665     }
666     return pimpl->proxy_->SetBtScanMode(mode, duration);
667 }
668 
GetBondableMode(int transport) const669 int BluetoothHost::GetBondableMode(int transport) const
670 {
671     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
672     if (!pimpl) {
673         HILOGE("BluetoothHost::GetBondableMode BLE fails: no pimpl");
674         return INVALID_VALUE;
675     }
676     if (pimpl->proxy_ == nullptr) {
677         HILOGE("BluetoothHost::GetBondableMode fails: no proxy");
678         return INVALID_VALUE;
679     }
680     return pimpl->proxy_->GetBondableMode(transport);
681 }
682 
SetBondableMode(int transport,int mode)683 bool BluetoothHost::SetBondableMode(int transport, int mode)
684 {
685     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
686     if (!pimpl) {
687         HILOGE("BluetoothHost::SetBondableMode BLE fails: no pimpl");
688         return false;
689     }
690     if (pimpl->proxy_ == nullptr) {
691         HILOGE("BluetoothHost::SetBondableMode fails: no proxy");
692         return false;
693     }
694     return pimpl->proxy_->SetBondableMode(transport, mode);
695 }
696 
StartBtDiscovery()697 bool BluetoothHost::StartBtDiscovery()
698 {
699     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
700     if (!pimpl) {
701         HILOGE("BluetoothHost::StartBtDiscovery BLE fails: no pimpl");
702         return false;
703     }
704     if (pimpl->proxy_ == nullptr) {
705         HILOGE("BluetoothHost::StartBtDiscovery fails: no proxy");
706         return false;
707     }
708     return pimpl->proxy_->StartBtDiscovery();
709 }
710 
CancelBtDiscovery()711 bool BluetoothHost::CancelBtDiscovery()
712 {
713     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
714     if (!pimpl) {
715         HILOGE("BluetoothHost::CancelBtDiscovery BLE fails: no pimpl");
716         return false;
717     }
718     if (pimpl->proxy_ == nullptr) {
719         HILOGE("BluetoothHost::CancelBtDiscovery fails: no proxy");
720         return false;
721     }
722     return pimpl->proxy_->CancelBtDiscovery();
723 }
724 
IsBtDiscovering(int transport) const725 bool BluetoothHost::IsBtDiscovering(int transport) const
726 {
727     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
728     if (!pimpl) {
729         HILOGE("BluetoothHost::IsBtDiscovering BLE fails: no pimpl");
730         return false;
731     }
732     if (pimpl->proxy_ == nullptr) {
733         HILOGE("BluetoothHost::IsBtDiscovering fails: no proxy");
734         return false;
735     }
736     return pimpl->proxy_->IsBtDiscovering(transport);
737 }
738 
GetBtDiscoveryEndMillis() const739 long BluetoothHost::GetBtDiscoveryEndMillis() const
740 {
741     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
742     if (!pimpl) {
743         HILOGE("BluetoothHost::GetBtDiscoveryEndMillis BLE fails: no pimpl");
744         return 0;
745     }
746     if (pimpl->proxy_ == nullptr) {
747         HILOGE("BluetoothHost::GetBtDiscoveryEndMillis fails: no proxy");
748         return 0;
749     }
750     return pimpl->proxy_->GetBtDiscoveryEndMillis();
751 }
752 
GetPairedDevices(int transport) const753 std::vector<BluetoothRemoteDevice> BluetoothHost::GetPairedDevices(int transport) const
754 {
755     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
756     std::vector<BluetoothRemoteDevice> pairedDevices;
757     if (!pimpl) {
758         HILOGE("BluetoothHost::GetPairedDevices BLE fails: no pimpl");
759         return pairedDevices;
760     }
761     if (pimpl->proxy_ == nullptr) {
762         HILOGE("BluetoothHost::GetPairedDevices fails: no proxy");
763         return pairedDevices;
764     }
765     std::vector<sptr<BluetoothRawAddress>> pairedAddr = pimpl->proxy_->GetPairedDevices(transport);
766 
767     for (auto it = pairedAddr.begin(); it != pairedAddr.end(); it++) {
768         if (*it != nullptr) {
769             BluetoothRemoteDevice device((*it)->GetAddress(), transport);
770             pairedDevices.emplace_back(device);
771         }
772     }
773     return pairedDevices;
774 }
775 
RemovePair(const BluetoothRemoteDevice & device)776 bool BluetoothHost::RemovePair(const BluetoothRemoteDevice &device)
777 {
778     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
779     if (!device.IsValidBluetoothRemoteDevice()) {
780         HILOGE("RemovePair::Invalid remote device.");
781         return false;
782     }
783     if (!pimpl) {
784         HILOGE("BluetoothHost::RemovePair BLE fails: no pimpl");
785         return false;
786     }
787     if (pimpl->proxy_ == nullptr) {
788         HILOGE("BluetoothHost::RemovePair fails: no proxy");
789         return false;
790     }
791     sptr<BluetoothRawAddress> rawAddrSptr = new BluetoothRawAddress(device.GetDeviceAddr());
792     return pimpl->proxy_->RemovePair(device.GetTransportType(), rawAddrSptr);
793 }
794 
RemoveAllPairs()795 bool BluetoothHost::RemoveAllPairs()
796 {
797     HILOGI("%{public}s: %{public}s(): Enter!", __FILE__, __FUNCTION__);
798     if (!pimpl) {
799         HILOGE("BluetoothHost::RemoveAllPairs BLE fails: no pimpl");
800         return false;
801     }
802     if (pimpl->proxy_ == nullptr) {
803         HILOGE("BluetoothHost::RemoveAllPairs fails: no proxy");
804         return false;
805     }
806     return pimpl->proxy_->RemoveAllPairs();
807 }
808 
RegisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver & observer)809 void BluetoothHost::RegisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver &observer)
810 {
811     if (!pimpl) {
812         HILOGE("BluetoothHost::RegisterRemoteDeviceObserver fails: no pimpl");
813         return;
814     }
815     std::shared_ptr<BluetoothRemoteDeviceObserver> pointer(&observer, [](BluetoothRemoteDeviceObserver *) {});
816     pimpl->remoteObservers_.Register(pointer);
817 }
818 
DeregisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver & observer)819 void BluetoothHost::DeregisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver &observer)
820 {
821     if (!pimpl) {
822         HILOGE("BluetoothHost::DeregisterRemoteDeviceObserver fails: no pimpl");
823         return;
824     }
825     std::shared_ptr<BluetoothRemoteDeviceObserver> pointer(&observer, [](BluetoothRemoteDeviceObserver *) {});
826     pimpl->remoteObservers_.Deregister(pointer);
827 }
828 
GetBleMaxAdvertisingDataLength() const829 int BluetoothHost::GetBleMaxAdvertisingDataLength() const
830 {
831     if (!pimpl) {
832         HILOGE("BluetoothHost::GetBleMaxAdvertisingDataLength BLE fails: no pimpl");
833         return INVALID_VALUE;
834     }
835     if (pimpl->proxy_ == nullptr) {
836         HILOGE("BluetoothHost::GetBleMaxAdvertisingDataLength fails: no proxy");
837         return INVALID_VALUE;
838     }
839     return pimpl->proxy_->GetBleMaxAdvertisingDataLength();
840 }
841 }  // namespace Bluetooth
842 }  // namespace OHOS
843