• 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 <mutex>
19 #include <unistd.h>
20 #include "bluetooth_ble_peripheral_observer_stub.h"
21 #include "bluetooth_host_observer_stub.h"
22 #include "bluetooth_host_proxy.h"
23 #include "bluetooth_log.h"
24 #include "bluetooth_utils.h"
25 #include "bluetooth_observer_list.h"
26 #include "bluetooth_remote_device_observer_stub.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 #include "bluetooth_errorcode.h"
30 
31 namespace OHOS {
32 namespace Bluetooth {
33 struct BluetoothHost::impl {
34     impl();
35     ~impl();
36 
37     // host observer
38     class BluetoothHostObserverImp;
39     sptr<BluetoothHostObserverImp> observerImp_ = nullptr;
40     sptr<BluetoothHostObserverImp> bleObserverImp_ = nullptr;
41 
42     // remote device observer
43     class BluetoothRemoteDeviceObserverImp;
44     sptr<BluetoothRemoteDeviceObserverImp> remoteObserverImp_ = nullptr;
45 
46     // remote device observer
47     class BluetoothBlePeripheralCallbackImp;
48     sptr<BluetoothBlePeripheralCallbackImp> bleRemoteObserverImp_ = nullptr;
49 
50     // user regist observers
51     BluetoothObserverList<BluetoothHostObserver> observers_;
52 
53     // user regist remote observers
54     BluetoothObserverList<BluetoothRemoteDeviceObserver> remoteObservers_;
55 
56     sptr<IBluetoothHost> proxy_ = nullptr;
57 
58 private:
59     void GetHostProxy();
60     class BluetoothHostDeathRecipient;
61     sptr<BluetoothHostDeathRecipient> deathRecipient_ = nullptr;
62     std::mutex proxyMutex_;
63 };
64 
65 class BluetoothHost::impl::BluetoothHostObserverImp : public BluetoothHostObserverStub {
66 public:
BluetoothHostObserverImp(BluetoothHost::impl & host)67     BluetoothHostObserverImp(BluetoothHost::impl &host) : host_(host){};
~BluetoothHostObserverImp()68     ~BluetoothHostObserverImp() override{};
69 
Register(std::shared_ptr<BluetoothHostObserver> & observer)70     void Register(std::shared_ptr<BluetoothHostObserver> &observer)
71     {
72         host_.observers_.Register(observer);
73     }
74 
Deregister(std::shared_ptr<BluetoothHostObserver> & observer)75     void Deregister(std::shared_ptr<BluetoothHostObserver> &observer)
76     {
77         host_.observers_.Deregister(observer);
78     }
79 
OnStateChanged(int32_t transport,int32_t status)80     void OnStateChanged(int32_t transport, int32_t status) override
81     {
82         HILOGI("bluetooth state, transport: %{public}s, status: %{public}s",
83             GetBtTransportName(transport).c_str(), GetBtStateName(status).c_str());
84         host_.observers_.ForEach([transport, status](std::shared_ptr<BluetoothHostObserver> observer) {
85             observer->OnStateChanged(transport, status);
86         });
87     }
88 
OnDiscoveryStateChanged(int32_t status)89     void OnDiscoveryStateChanged(int32_t status) override
90     {
91         HILOGI("enter, status: %{public}d", status);
92         host_.observers_.ForEach(
93             [status](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDiscoveryStateChanged(status); });
94     }
95 
OnDiscoveryResult(const BluetoothRawAddress & device)96     void OnDiscoveryResult(const BluetoothRawAddress &device) override
97     {
98         HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
99         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
100         host_.observers_.ForEach([remoteDevice](std::shared_ptr<BluetoothHostObserver> observer) {
101             observer->OnDiscoveryResult(remoteDevice);
102         });
103     }
104 
OnPairRequested(const int32_t transport,const BluetoothRawAddress & device)105     void OnPairRequested(const int32_t transport, const BluetoothRawAddress &device) override
106     {
107         HILOGI("enter, transport: %{public}d, device: %{public}s",
108             transport, GetEncryptAddr((device).GetAddress()).c_str());
109         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
110         host_.observers_.ForEach([remoteDevice](std::shared_ptr<BluetoothHostObserver> observer) {
111             observer->OnPairRequested(remoteDevice);
112         });
113     }
114 
OnPairConfirmed(const int32_t transport,const BluetoothRawAddress & device,int reqType,int number)115     void OnPairConfirmed(const int32_t transport, const BluetoothRawAddress &device, int reqType, int number) override
116     {
117         HILOGI("enter, transport: %{public}d, device: %{public}s, reqType: %{public}d, number: %{public}d",
118             transport, GetEncryptAddr((device).GetAddress()).c_str(), reqType, number);
119         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
120         host_.observers_.ForEach([remoteDevice, reqType, number](std::shared_ptr<BluetoothHostObserver> observer) {
121             observer->OnPairConfirmed(remoteDevice, reqType, number);
122         });
123     }
124 
OnScanModeChanged(int mode)125     void OnScanModeChanged(int mode) override
126     {
127         HILOGI("enter, mode: %{public}d", mode);
128         host_.observers_.ForEach(
129             [mode](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnScanModeChanged(mode); });
130     }
131 
OnDeviceNameChanged(const std::string & deviceName)132     void OnDeviceNameChanged(const std::string &deviceName) override
133     {
134         HILOGI("enter, deviceName: %{public}s", deviceName.c_str());
135         host_.observers_.ForEach([deviceName](std::shared_ptr<BluetoothHostObserver> observer) {
136             observer->OnDeviceNameChanged(deviceName);
137         });
138     }
139 
OnDeviceAddrChanged(const std::string & address)140     void OnDeviceAddrChanged(const std::string &address) override
141     {
142         HILOGI("enter");
143         host_.observers_.ForEach(
144             [address](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDeviceAddrChanged(address); });
145     }
146 
147 private:
148     BluetoothHost::impl &host_;
149     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHostObserverImp);
150 };
151 
152 class BluetoothHost::impl::BluetoothRemoteDeviceObserverImp : public BluetoothRemoteDeviceObserverstub {
153 public:
BluetoothRemoteDeviceObserverImp(BluetoothHost::impl & host)154     BluetoothRemoteDeviceObserverImp(BluetoothHost::impl &host) : host_(host){};
155     ~BluetoothRemoteDeviceObserverImp() override = default;
156 
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int32_t status)157     void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device, int32_t status) override
158     {
159         HILOGI("enter, transport: %{public}d, device: %{public}s, status: %{public}d",
160             transport, GetEncryptAddr((device).GetAddress()).c_str(), status);
161         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
162 
163         host_.remoteObservers_.ForEach([remoteDevice, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
164             observer->OnPairStatusChanged(remoteDevice, status);
165         });
166     }
167 
OnRemoteUuidChanged(const BluetoothRawAddress & device,const std::vector<bluetooth::Uuid> uuids)168     void OnRemoteUuidChanged(const BluetoothRawAddress &device, const std::vector<bluetooth::Uuid> uuids) override
169     {
170         HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
171         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
172         host_.remoteObservers_.ForEach([remoteDevice, uuids](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
173             std::vector<ParcelUuid> parcelUuids;
174             for (auto &uuid : uuids) {
175                 ParcelUuid parcelUuid = UUID::ConvertFrom128Bits(uuid.ConvertTo128Bits());
176                 parcelUuids.push_back(parcelUuid);
177                 observer->OnRemoteUuidChanged(remoteDevice, parcelUuids);
178             }
179         });
180     }
181 
OnRemoteNameChanged(const BluetoothRawAddress & device,const std::string deviceName)182     void OnRemoteNameChanged(const BluetoothRawAddress &device, const std::string deviceName) override
183     {
184         HILOGI("enter, device: %{public}s, deviceName: %{public}s",
185             GetEncryptAddr((device).GetAddress()).c_str(), deviceName.c_str());
186         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
187         host_.remoteObservers_.ForEach(
188             [remoteDevice, deviceName](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
189                 observer->OnRemoteNameChanged(remoteDevice, deviceName);
190             });
191     }
192 
OnRemoteAliasChanged(const BluetoothRawAddress & device,const std::string alias)193     void OnRemoteAliasChanged(const BluetoothRawAddress &device, const std::string alias) override
194     {
195         HILOGI("enter, device: %{public}s, alias: %{public}s",
196             GetEncryptAddr((device).GetAddress()).c_str(), alias.c_str());
197         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
198         host_.remoteObservers_.ForEach([remoteDevice, alias](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
199             observer->OnRemoteAliasChanged(remoteDevice, alias);
200         });
201     }
202 
OnRemoteCodChanged(const BluetoothRawAddress & device,int32_t cod)203     void OnRemoteCodChanged(const BluetoothRawAddress &device, int32_t cod) override
204     {
205         HILOGI("enter, device: %{public}s, cod: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), cod);
206         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
207         BluetoothDeviceClass deviceClass(cod);
208         host_.remoteObservers_.ForEach(
209             [remoteDevice, deviceClass](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
210                 observer->OnRemoteCodChanged(remoteDevice, deviceClass);
211             });
212     }
213 
OnRemoteBatteryLevelChanged(const BluetoothRawAddress & device,int32_t batteryLevel)214     void OnRemoteBatteryLevelChanged(const BluetoothRawAddress &device, int32_t batteryLevel) override
215     {
216         HILOGI("enter, device: %{public}s, batteryLevel: %{public}d",
217             GetEncryptAddr((device).GetAddress()).c_str(), batteryLevel);
218         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
219         host_.remoteObservers_.ForEach(
220             [remoteDevice, batteryLevel](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
221                 observer->OnRemoteBatteryLevelChanged(remoteDevice, batteryLevel);
222             });
223     }
224 
225 private:
226     BluetoothHost::impl &host_;
227     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteDeviceObserverImp);
228 };
229 
230 class BluetoothHost::impl::BluetoothBlePeripheralCallbackImp : public BluetoothBlePeripheralObserverStub {
231 public:
BluetoothBlePeripheralCallbackImp(BluetoothHost::impl & host)232     BluetoothBlePeripheralCallbackImp(BluetoothHost::impl &host) : host_(host){};
233     ~BluetoothBlePeripheralCallbackImp() override = default;
234 
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int status)235     void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device, int status) override
236     {
237         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
238         host_.remoteObservers_.ForEach([remoteDevice, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
239             observer->OnPairStatusChanged(remoteDevice, status);
240         });
241     }
242 
OnReadRemoteRssiEvent(const BluetoothRawAddress & device,int rssi,int status)243     void OnReadRemoteRssiEvent(const BluetoothRawAddress &device, int rssi, int status) override
244     {
245         HILOGI("enter, device: %{public}s, rssi: %{public}d, status: %{public}d",
246             GetEncryptAddr((device).GetAddress()).c_str(), rssi, status);
247         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BLE);
248         host_.remoteObservers_.ForEach(
249             [remoteDevice, rssi, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
250                 observer->OnReadRemoteRssiEvent(remoteDevice, rssi, status);
251             });
252     }
253 
254 private:
255     BluetoothHost::impl &host_;
256     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBlePeripheralCallbackImp);
257 };
258 
259 class BluetoothHost::impl::BluetoothHostDeathRecipient final : public IRemoteObject::DeathRecipient {
260 public:
BluetoothHostDeathRecipient(BluetoothHost::impl & impl)261     BluetoothHostDeathRecipient(BluetoothHost::impl &impl) : impl_(impl)
262     {};
263     ~BluetoothHostDeathRecipient() final = default;
264     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHostDeathRecipient);
265 
OnRemoteDied(const wptr<IRemoteObject> & remote)266     void OnRemoteDied(const wptr<IRemoteObject> &remote) final
267     {
268         HILOGI("bluetooth_servi died and then re-registered");
269         std::lock_guard<std::mutex> lock(impl_.proxyMutex_);
270         impl_.proxy_->AsObject()->RemoveDeathRecipient(impl_.deathRecipient_);
271         impl_.proxy_ = nullptr;
272 
273         // Notify the upper layer that bluetooth is disabled.
274         if (impl_.observerImp_ != nullptr && impl_.bleObserverImp_ != nullptr) {
275             HILOGI("bluetooth_servi died and send state off to app");
276             impl_.observerImp_->OnStateChanged(BTTransport::ADAPTER_BLE, BTStateID::STATE_TURN_OFF);
277             impl_.bleObserverImp_->OnStateChanged(BTTransport::ADAPTER_BREDR, BTStateID::STATE_TURN_OFF);
278         }
279 
280         // Re-obtain the proxy and register the observer.
281         if (!ResetHostProxy()) {
282             HILOGE("proxy reset failed");
283             return;
284         }
285         HILOGE("proxy reset succeded");
286     }
287 
288 private:
ResetHostProxy()289     bool ResetHostProxy()
290     {
291         if (impl_.proxy_ != nullptr) {
292             return true;
293         }
294         HILOGI("proxy is null, try to get proxy.");
295         impl_.GetHostProxy();
296 
297         if (impl_.proxy_ == nullptr) {
298             HILOGI("try to get proxy failed.");
299             return false;
300         }
301         impl_.proxy_->RegisterObserver(impl_.observerImp_);
302         impl_.proxy_->RegisterBleAdapterObserver(impl_.bleObserverImp_);
303         impl_.proxy_->RegisterRemoteDeviceObserver(impl_.remoteObserverImp_);
304         impl_.proxy_->RegisterBlePeripheralCallback(impl_.bleRemoteObserverImp_);
305         impl_.proxy_->AsObject()->AddDeathRecipient(impl_.deathRecipient_);
306         return true;
307     }
308     BluetoothHost::impl &impl_;
309 };
310 
impl()311 BluetoothHost::impl::impl()
312 {
313     HILOGI("starts");
314     observerImp_ = new (std::nothrow) BluetoothHostObserverImp(*this);
315     if (observerImp_ == nullptr) {
316         HILOGE("observerImp_ is null");
317         return;
318     }
319 
320     remoteObserverImp_ = new (std::nothrow) BluetoothRemoteDeviceObserverImp(*this);
321     if (remoteObserverImp_ == nullptr) {
322         HILOGE("remoteObserverImp_ is null");
323         return;
324     }
325 
326     bleRemoteObserverImp_ = new (std::nothrow) BluetoothBlePeripheralCallbackImp(*this);
327     if (bleRemoteObserverImp_ == nullptr) {
328         HILOGE("bleRemoteObserverImp_ is null");
329         return;
330     }
331 
332     bleObserverImp_ = new (std::nothrow) BluetoothHostObserverImp(*this);
333     if (bleObserverImp_ == nullptr) {
334         HILOGE("bleObserverImp_ is null");
335         return;
336     }
337 
338     GetHostProxy();
339 
340     if (proxy_ == nullptr) {
341         HILOGE("proxy_ is null");
342         return;
343     }
344 
345     proxy_->RegisterObserver(observerImp_);
346     proxy_->RegisterBleAdapterObserver(bleObserverImp_);
347     proxy_->RegisterRemoteDeviceObserver(remoteObserverImp_);
348     proxy_->RegisterBlePeripheralCallback(bleRemoteObserverImp_);
349 
350     deathRecipient_ = new BluetoothHostDeathRecipient(*this);
351     if (deathRecipient_ == nullptr) {
352         HILOGE("deathRecipient_ is null");
353         return;
354     }
355     proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
356 }
357 
~impl()358 BluetoothHost::impl::~impl()
359 {
360     HILOGI("starts");
361     if (proxy_ == nullptr) {
362         HILOGE("proxy_ is null");
363         return;
364     }
365     proxy_->DeregisterObserver(observerImp_);
366     proxy_->DeregisterBleAdapterObserver(bleObserverImp_);
367     proxy_->DeregisterRemoteDeviceObserver(remoteObserverImp_);
368     proxy_->DeregisterBlePeripheralCallback(bleRemoteObserverImp_);
369     proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
370 }
371 
GetHostProxy()372 void BluetoothHost::impl::GetHostProxy()
373 {
374     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
375     if (!samgr) {
376         HILOGE("error: no samgr");
377         return;
378     }
379 
380     sptr<IRemoteObject> remote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
381     if (!remote) {
382         int try_time = 5;
383         while ((!remote) && try_time > 0) {
384             sleep(GET_PROXY_SLEEP_SEC);
385             try_time--;
386             remote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
387         }
388     }
389 
390     if (!remote) {
391         HILOGE("error:no remote");
392         return;
393     }
394 
395     proxy_ = iface_cast<IBluetoothHost>(remote);
396     if (!proxy_) {
397         HILOGE("error:no proxy");
398         return;
399     }
400 }
401 
402 BluetoothHost BluetoothHost::hostAdapter_;
403 
BluetoothHost()404 BluetoothHost::BluetoothHost()
405 {
406     pimpl = std::make_unique<impl>();
407     if (!pimpl) {
408         HILOGE("fails: no pimpl");
409     }
410 }
411 
~BluetoothHost()412 BluetoothHost::~BluetoothHost()
413 {}
414 
GetDefaultHost()415 BluetoothHost &BluetoothHost::GetDefaultHost()
416 {
417     return hostAdapter_;
418 }
419 
RegisterObserver(BluetoothHostObserver & observer)420 void BluetoothHost::RegisterObserver(BluetoothHostObserver &observer)
421 {
422     if (!pimpl) {
423         HILOGE("fails: no pimpl");
424         return;
425     }
426     std::shared_ptr<BluetoothHostObserver> pointer(&observer, [](BluetoothHostObserver *) {});
427     pimpl->observers_.Register(pointer);
428 }
429 
DeregisterObserver(BluetoothHostObserver & observer)430 void BluetoothHost::DeregisterObserver(BluetoothHostObserver &observer)
431 {
432     if (!pimpl) {
433         HILOGE("fails: no pimpl");
434         return;
435     }
436     std::shared_ptr<BluetoothHostObserver> pointer(&observer, [](BluetoothHostObserver *) {});
437     pimpl->observers_.Deregister(pointer);
438 }
439 
EnableBt()440 bool BluetoothHost::EnableBt()
441 {
442     HILOGI("enter");
443     if (!pimpl) {
444         HILOGE("fails: no pimpl");
445         return false;
446     }
447     if (pimpl->proxy_ == nullptr) {
448         HILOGE("fails: no proxy");
449         return false;
450     }
451 
452     return pimpl->proxy_->EnableBt();
453 }
454 
DisableBt()455 int BluetoothHost::DisableBt()
456 {
457     HILOGI("enter");
458     if (!pimpl) {
459         HILOGE("fails: no pimpl");
460         return false;
461     }
462     if (pimpl->proxy_ == nullptr) {
463         HILOGE("fails: no proxy");
464         return false;
465     }
466     return pimpl->proxy_->DisableBt();
467 }
468 
GetBtState() const469 int BluetoothHost::GetBtState() const
470 {
471     HILOGI("enter");
472     if (!pimpl) {
473         HILOGE("failed: no pimpl");
474         return INVALID_VALUE;
475     }
476     if (pimpl->proxy_ == nullptr) {
477         HILOGE("failed: no proxy");
478         return INVALID_VALUE;
479     }
480     int state = BTStateID::STATE_TURN_OFF;
481     pimpl->proxy_->GetBtState(state);
482     HILOGI("state: %{public}d", state);
483     return state;
484 }
485 
GetBtState(int & state) const486 int BluetoothHost::GetBtState(int &state) const
487 {
488     HILOGI("enter");
489     if (!pimpl) {
490         HILOGE("failed: no pimpl");
491         return BT_ERR_INTERNAL_ERROR;
492     }
493     if (pimpl->proxy_ == nullptr) {
494         HILOGE("failed: no proxy");
495         return BT_ERR_SERVICE_DISCONNECTED;
496     }
497     int ret = pimpl->proxy_->GetBtState(state);
498     HILOGI("state: %{public}d", state);
499     return ret;
500 }
501 
BluetoothFactoryReset()502 bool BluetoothHost::BluetoothFactoryReset()
503 {
504     HILOGI("enter");
505     if (!pimpl) {
506         HILOGE("fails: no pimpl");
507         return false;
508     }
509     if (pimpl->proxy_ == nullptr) {
510         HILOGE("fails: no proxy");
511         return false;
512     }
513     return pimpl->proxy_->BluetoothFactoryReset();
514 }
515 
IsValidBluetoothAddr(const std::string & addr)516 bool BluetoothHost::IsValidBluetoothAddr(const std::string &addr)
517 {
518     if (addr.empty() || addr.length() != ADDRESS_LENGTH) {
519         return false;
520     }
521     for (int i = 0; i < ADDRESS_LENGTH; i++) {
522         char c = addr[i];
523         switch (i % ADDRESS_SEPARATOR_UNIT) {
524             case 0:
525             case 1:
526                 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
527                     break;
528                 }
529                 return false;
530             case ADDRESS_COLON_INDEX:
531             default:
532                 if (c == ':') {
533                     break;
534                 }
535                 return false;
536         }
537     }
538     return true;
539 }
540 
GetRemoteDevice(const std::string & addr,int transport) const541 BluetoothRemoteDevice BluetoothHost::GetRemoteDevice(const std::string &addr, int transport) const
542 {
543     BluetoothRemoteDevice remoteDevice(addr, transport);
544     return remoteDevice;
545 }
546 
EnableBle()547 int BluetoothHost::EnableBle()
548 {
549     HILOGI("enter");
550     if (!pimpl) {
551         HILOGE("fails: no pimpl");
552         return BT_ERR_INTERNAL_ERROR;
553     }
554     if (pimpl->proxy_ == nullptr) {
555         HILOGE("fails: no proxy");
556         return BT_ERR_INTERNAL_ERROR;
557     }
558     return pimpl->proxy_->EnableBle();
559 }
560 
DisableBle()561 int BluetoothHost::DisableBle()
562 {
563     HILOGI("enter");
564     if (!pimpl) {
565         HILOGE("fails: no pimpl");
566         return BT_ERR_INTERNAL_ERROR;
567     }
568     if (pimpl->proxy_ == nullptr) {
569         HILOGE("fails: no proxy");
570         return BT_ERR_INTERNAL_ERROR;
571     }
572     return pimpl->proxy_->DisableBle();
573 }
574 
IsBrEnabled() const575 bool BluetoothHost::IsBrEnabled() const
576 {
577     if (!pimpl) {
578         HILOGE("fails: no pimpl");
579         return false;
580     }
581     if (pimpl->proxy_ == nullptr) {
582         HILOGE("fails: no proxy");
583         return false;
584     }
585     return pimpl->proxy_->IsBrEnabled();
586 }
587 
IsBleEnabled() const588 bool BluetoothHost::IsBleEnabled() const
589 {
590     HILOGI("enter");
591     if (!pimpl) {
592         HILOGE("fails: no pimpl");
593         return false;
594     }
595     if (pimpl->proxy_ == nullptr) {
596         HILOGE("fails: no proxy");
597         return false;
598     }
599     return pimpl->proxy_->IsBleEnabled();
600 }
601 
GetLocalAddress() const602 std::string BluetoothHost::GetLocalAddress() const
603 {
604     HILOGI("enter");
605     if (!pimpl) {
606         HILOGE("fails: no pimpl");
607         return std::string();
608     }
609     if (pimpl->proxy_ == nullptr) {
610         HILOGE("fails: no proxy");
611         return std::string();
612     }
613     return pimpl->proxy_->GetLocalAddress();
614 }
615 
GetProfileList() const616 std::vector<uint32_t> BluetoothHost::GetProfileList() const
617 {
618     HILOGI("enter");
619     std::vector<uint32_t> profileList;
620     if (!pimpl) {
621         HILOGE("fails: no pimpl");
622         return profileList;
623     }
624     if (pimpl->proxy_ == nullptr) {
625         HILOGE("fails: no proxy");
626         return profileList;
627     }
628     profileList = pimpl->proxy_->GetProfileList();
629     return profileList;
630 }
631 
GetMaxNumConnectedAudioDevices() const632 int BluetoothHost::GetMaxNumConnectedAudioDevices() const
633 {
634     HILOGI("enter");
635     if (!pimpl) {
636         HILOGE("fails: no pimpl");
637         return INVALID_VALUE;
638     }
639     if (pimpl->proxy_ == nullptr) {
640         HILOGE("fails: no proxy");
641         return INVALID_VALUE;
642     }
643     return pimpl->proxy_->GetMaxNumConnectedAudioDevices();
644 }
645 
GetBtConnectionState() const646 int BluetoothHost::GetBtConnectionState() const
647 {
648     HILOGI("enter");
649     if (!pimpl) {
650         HILOGE("fails: no pimpl");
651         return INVALID_VALUE;
652     }
653     if (pimpl->proxy_ == nullptr) {
654         HILOGE("fails: no proxy");
655         return INVALID_VALUE;
656     }
657     int state = static_cast<int>(BTConnectState::DISCONNECTED);
658     pimpl->proxy_->GetBtConnectionState(state);
659     HILOGI("state: %{public}d", state);
660     return state;
661 }
662 
GetBtConnectionState(int & state) const663 int BluetoothHost::GetBtConnectionState(int &state) const
664 {
665     HILOGI("enter");
666     if (!pimpl) {
667         HILOGE("fails: no pimpl");
668         return BT_ERR_INTERNAL_ERROR;
669     }
670     if (pimpl->proxy_ == nullptr) {
671         HILOGE("fails: no proxy");
672         return BT_ERR_SERVICE_DISCONNECTED;
673     }
674     int ret = pimpl->proxy_->GetBtConnectionState(state);
675     HILOGI("state: %{public}d", state);
676     return ret;
677 }
678 
GetBtProfileConnState(uint32_t profileId,int & state) const679 int BluetoothHost::GetBtProfileConnState(uint32_t profileId, int &state) const
680 {
681     HILOGI("enter, profileId: %{public}d", profileId);
682     if (!pimpl) {
683         HILOGE("fails: no pimpl");
684         return BT_ERR_INTERNAL_ERROR;
685     }
686     if (pimpl->proxy_ == nullptr) {
687         HILOGE("fails: no proxy");
688         return BT_ERR_INTERNAL_ERROR;
689     }
690     return pimpl->proxy_->GetBtProfileConnState(profileId, state);
691 }
692 
GetLocalSupportedUuids(std::vector<ParcelUuid> & uuids)693 void BluetoothHost::GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids)
694 {
695     HILOGI("enter");
696     if (!pimpl) {
697         HILOGE("fails: no pimpl");
698         return;
699     }
700     if (pimpl->proxy_ == nullptr) {
701         HILOGE("fails: no proxy");
702         return;
703     }
704     std::vector<std::string> stringUuids;
705     pimpl->proxy_->GetLocalSupportedUuids(stringUuids);
706     for (auto uuid : stringUuids) {
707         uuids.push_back(UUID::FromString(uuid));
708     }
709 }
710 
Start()711 bool BluetoothHost::Start()
712 {
713     /// This function only used for passthrough, so this is empty.
714     return true;
715 }
716 
Stop()717 void BluetoothHost::Stop()
718 {
719     /// This function only used for passthrough, so this is empty.
720 }
721 
GetLocalDeviceClass() const722 BluetoothDeviceClass BluetoothHost::GetLocalDeviceClass() const
723 {
724     HILOGI("enter");
725     if (!pimpl) {
726         HILOGE("fails: no pimpl");
727         return BluetoothDeviceClass(0);
728     }
729     if (pimpl->proxy_ == nullptr) {
730         HILOGE("fails: no proxy");
731         return BluetoothDeviceClass(0);
732     }
733     int LocalDeviceClass = pimpl->proxy_->GetLocalDeviceClass();
734     return BluetoothDeviceClass(LocalDeviceClass);
735 }
736 
SetLocalDeviceClass(const BluetoothDeviceClass & deviceClass)737 bool BluetoothHost::SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass)
738 {
739     HILOGI("enter");
740     if (!pimpl) {
741         HILOGE("fails: no pimpl");
742         return false;
743     }
744     if (pimpl->proxy_ == nullptr) {
745         HILOGE("fails: no proxy");
746         return false;
747     }
748     int cod = deviceClass.GetClassOfDevice();
749     return pimpl->proxy_->SetLocalDeviceClass(cod);
750 }
751 
GetLocalName() const752 std::string BluetoothHost::GetLocalName() const
753 {
754     HILOGI("enter");
755     if (!pimpl) {
756         HILOGE("fails: no pimpl");
757         return std::string();
758     }
759     if (pimpl->proxy_ == nullptr) {
760         HILOGE("fails: no proxy");
761         return std::string();
762     }
763     std::string name = INVALID_NAME;
764     pimpl->proxy_->GetLocalName(name);
765     return name;
766 }
767 
GetLocalName(std::string & name) const768 int BluetoothHost::GetLocalName(std::string &name) const
769 {
770     HILOGI("enter");
771     if (!pimpl) {
772         HILOGE("fails: no pimpl");
773         return BT_ERR_INTERNAL_ERROR;
774     }
775     if (pimpl->proxy_ == nullptr) {
776         HILOGE("fails: no proxy");
777         return BT_ERR_INTERNAL_ERROR;
778     }
779     return pimpl->proxy_->GetLocalName(name);
780 }
781 
SetLocalName(const std::string & name)782 int BluetoothHost::SetLocalName(const std::string &name)
783 {
784     HILOGI("enter");
785     if (!pimpl) {
786         HILOGE("fails: no pimpl");
787         return BT_ERR_INTERNAL_ERROR;
788     }
789     if (pimpl->proxy_ == nullptr) {
790         HILOGE("fails: no proxy");
791         return BT_ERR_INTERNAL_ERROR;
792     }
793     return pimpl->proxy_->SetLocalName(name);
794 }
795 
GetBtScanMode(int32_t & scanMode) const796 int BluetoothHost::GetBtScanMode(int32_t &scanMode) const
797 {
798     HILOGI("enter");
799     if (!pimpl) {
800         HILOGE("fails: no pimpl");
801         return BT_ERR_INTERNAL_ERROR;
802     }
803     if (pimpl->proxy_ == nullptr) {
804         HILOGE("fails: no proxy");
805         return BT_ERR_INTERNAL_ERROR;
806     }
807     return pimpl->proxy_->GetBtScanMode(scanMode);
808 }
809 
SetBtScanMode(int mode,int duration)810 int BluetoothHost::SetBtScanMode(int mode, int duration)
811 {
812     HILOGI("enter, mode: %{public}d, duration: %{public}d", mode, duration);
813     if (!pimpl) {
814         HILOGE("fails: no pimpl");
815         return BT_ERR_INTERNAL_ERROR;
816     }
817     if (pimpl->proxy_ == nullptr) {
818         HILOGE("fails: no proxy");
819         return BT_ERR_INTERNAL_ERROR;
820     }
821     return pimpl->proxy_->SetBtScanMode(mode, duration);
822 }
823 
GetBondableMode(int transport) const824 int BluetoothHost::GetBondableMode(int transport) const
825 {
826     HILOGI("enter, transport: %{public}d", transport);
827     if (!pimpl) {
828         HILOGE("fails: no pimpl");
829         return INVALID_VALUE;
830     }
831     if (pimpl->proxy_ == nullptr) {
832         HILOGE("fails: no proxy");
833         return INVALID_VALUE;
834     }
835     return pimpl->proxy_->GetBondableMode(transport);
836 }
837 
SetBondableMode(int transport,int mode)838 bool BluetoothHost::SetBondableMode(int transport, int mode)
839 {
840     HILOGI("enter, transport: %{public}d, mode: %{public}d", transport, mode);
841     if (!pimpl) {
842         HILOGE("fails: no pimpl");
843         return false;
844     }
845     if (pimpl->proxy_ == nullptr) {
846         HILOGE("fails: no proxy");
847         return false;
848     }
849     return pimpl->proxy_->SetBondableMode(transport, mode);
850 }
851 
StartBtDiscovery()852 int BluetoothHost::StartBtDiscovery()
853 {
854     HILOGI("enter");
855     if (!pimpl) {
856         HILOGE("fails: no pimpl");
857         return BT_ERR_INTERNAL_ERROR;
858     }
859     if (pimpl->proxy_ == nullptr) {
860         HILOGE("fails: no proxy");
861         return BT_ERR_INTERNAL_ERROR;
862     }
863     return pimpl->proxy_->StartBtDiscovery();
864 }
865 
CancelBtDiscovery()866 int BluetoothHost::CancelBtDiscovery()
867 {
868     HILOGI("enter");
869     if (!pimpl) {
870         HILOGE("fails: no pimpl");
871         return BT_ERR_INTERNAL_ERROR;
872     }
873     if (pimpl->proxy_ == nullptr) {
874         HILOGE("fails: no proxy");
875         return BT_ERR_INTERNAL_ERROR;
876     }
877     return pimpl->proxy_->CancelBtDiscovery();
878 }
879 
IsBtDiscovering(int transport) const880 bool BluetoothHost::IsBtDiscovering(int transport) const
881 {
882     HILOGI("enter, transport: %{public}d", transport);
883     if (!pimpl) {
884         HILOGE("fails: no pimpl");
885         return false;
886     }
887     if (pimpl->proxy_ == nullptr) {
888         HILOGE("fails: no proxy");
889         return false;
890     }
891     return pimpl->proxy_->IsBtDiscovering(transport);
892 }
893 
GetBtDiscoveryEndMillis() const894 long BluetoothHost::GetBtDiscoveryEndMillis() const
895 {
896     HILOGI("enter");
897     if (!pimpl) {
898         HILOGE("fails: no pimpl");
899         return 0;
900     }
901     if (pimpl->proxy_ == nullptr) {
902         HILOGE("fails: no proxy");
903         return 0;
904     }
905     return pimpl->proxy_->GetBtDiscoveryEndMillis();
906 }
907 
GetPairedDevices(int transport,std::vector<BluetoothRemoteDevice> & pairedDevices) const908 int32_t BluetoothHost::GetPairedDevices(int transport, std::vector<BluetoothRemoteDevice> &pairedDevices) const
909 {
910     HILOGI("enter, transport: %{public}d", transport);
911     if (!pimpl) {
912         HILOGE("fails: no pimpl");
913         return BT_ERR_INTERNAL_ERROR;
914     }
915     if (pimpl->proxy_ == nullptr) {
916         HILOGE("fails: no proxy");
917         return BT_ERR_INTERNAL_ERROR;
918     }
919     std::vector<BluetoothRawAddress> pairedAddr;
920     int32_t ret = pimpl->proxy_->GetPairedDevices(transport, pairedAddr);
921 
922     for (auto it = pairedAddr.begin(); it != pairedAddr.end(); it++) {
923         BluetoothRemoteDevice device((*it).GetAddress(), transport);
924         pairedDevices.emplace_back(device);
925     }
926     return ret;
927 }
928 
RemovePair(const BluetoothRemoteDevice & device)929 int32_t BluetoothHost::RemovePair(const BluetoothRemoteDevice &device)
930 {
931     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
932     if (!device.IsValidBluetoothRemoteDevice()) {
933         HILOGE("Invalid remote device.");
934         return BT_ERR_INTERNAL_ERROR;
935     }
936     if (!pimpl) {
937         HILOGE("fails: no pimpl");
938         return BT_ERR_INTERNAL_ERROR;
939     }
940     if (pimpl->proxy_ == nullptr) {
941         HILOGE("fails: no proxy");
942         return BT_ERR_INTERNAL_ERROR;
943     }
944     sptr<BluetoothRawAddress> rawAddrSptr = new BluetoothRawAddress(device.GetDeviceAddr());
945     return pimpl->proxy_->RemovePair(device.GetTransportType(), rawAddrSptr);
946 }
947 
RemoveAllPairs()948 bool BluetoothHost::RemoveAllPairs()
949 {
950     HILOGI("enter");
951     if (!pimpl) {
952         HILOGE("fails: no pimpl");
953         return false;
954     }
955     if (pimpl->proxy_ == nullptr) {
956         HILOGE("fails: no proxy");
957         return false;
958     }
959     return pimpl->proxy_->RemoveAllPairs();
960 }
961 
RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)962 void BluetoothHost::RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)
963 {
964     if (!pimpl) {
965         HILOGE("fails: no pimpl");
966         return;
967     }
968     pimpl->remoteObservers_.Register(observer);
969 }
970 
DeregisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver & observer)971 void BluetoothHost::DeregisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver &observer)
972 {
973     if (!pimpl) {
974         HILOGE("fails: no pimpl");
975         return;
976     }
977     std::shared_ptr<BluetoothRemoteDeviceObserver> pointer(&observer, [](BluetoothRemoteDeviceObserver *) {});
978     pimpl->remoteObservers_.Deregister(pointer);
979 }
980 
GetBleMaxAdvertisingDataLength() const981 int BluetoothHost::GetBleMaxAdvertisingDataLength() const
982 {
983     if (!pimpl) {
984         HILOGE("fails: no pimpl");
985         return INVALID_VALUE;
986     }
987     if (pimpl->proxy_ == nullptr) {
988         HILOGE("fails: no proxy");
989         return INVALID_VALUE;
990     }
991     return pimpl->proxy_->GetBleMaxAdvertisingDataLength();
992 }
993 }  // namespace Bluetooth
994 }  // namespace OHOS
995