• 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_host"
17 #endif
18 
19 #include "bluetooth_host.h"
20 #include <memory>
21 #include <mutex>
22 #include <unistd.h>
23 #include <thread>
24 #include <cinttypes>
25 #include "bluetooth_ble_peripheral_observer_stub.h"
26 #include "bluetooth_host_load_callback.h"
27 #include "bluetooth_host_observer_stub.h"
28 #include "bluetooth_host_proxy.h"
29 #include "bluetooth_profile_manager.h"
30 #include "bluetooth_log.h"
31 #include "bluetooth_utils.h"
32 #include "bluetooth_observer_list.h"
33 #include "bluetooth_remote_device_observer_stub.h"
34 #include "bluetooth_resource_manager_observer_stub.h"
35 #include "iservice_registry.h"
36 #include "parameter.h"
37 #include "system_ability_definition.h"
38 #include "bluetooth_switch_module.h"
39 #include "ffrt_inner.h"
40 #include "common_event.h"
41 #include "common_event_data.h"
42 #include "common_event_manager.h"
43 
44 using namespace OHOS::EventFwk;
45 
46 namespace OHOS {
47 namespace Bluetooth {
48 namespace {
49 constexpr int32_t LOAD_SA_TIMEOUT_MS = 4000;
50 }
51 
52 struct BluetoothHost::impl {
53     impl();
54     ~impl();
55 
56     bool LoadBluetoothHostService();
57     void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
58     void LoadSystemAbilityFail();
59     int EnableBluetoothAfterFactoryReset(void);
60 
61     // host observer
62     class BluetoothHostObserverImp;
63     sptr<BluetoothHostObserverImp> observerImp_ = nullptr;
64     sptr<BluetoothHostObserverImp> bleObserverImp_ = nullptr;
65 
66     // remote device observer
67     class BluetoothRemoteDeviceObserverImp;
68     sptr<BluetoothRemoteDeviceObserverImp> remoteObserverImp_ = nullptr;
69 
70     // remote device observer
71     class BluetoothBlePeripheralCallbackImp;
72     sptr<BluetoothBlePeripheralCallbackImp> bleRemoteObserverImp_ = nullptr;
73 
74     // bluetooth resource manager observer
75     class BluetoothResourceManagerObserverImp;
76     sptr<BluetoothResourceManagerObserverImp> resourceManagerObserverImp_ = nullptr;
77 
78     // user regist observers
79     BluetoothObserverList<BluetoothHostObserver> observers_;
80 
81     // user regist remote observers
82     BluetoothObserverList<BluetoothRemoteDeviceObserver> remoteObservers_;
83 
84     // user regist resource manager observers
85     BluetoothObserverList<BluetoothResourceManagerObserver> resourceManagerObservers_;
86 
87     void SyncRandomAddrToService(void);
88     int ConvertBluetoothStateToBtStateID(BluetoothState state);
89 
90     std::mutex proxyMutex_;
91     std::string stagingRealAddr_;
92     std::string stagingRandomAddr_;
93     int32_t profileRegisterId = 0;
94     std::atomic_bool isFactoryReseting_ { false };
95 
96     class BluetoothSwitchAction;
97     std::mutex switchModuleMutex_ {};  // used for serial execute enableBluetoothToRestrictMode.
98     std::shared_ptr<BluetoothSwitchModule> switchModule_ { nullptr };
99     int64_t refusePolicyProhibitedTime_ = 0;
100 
101 private:
102     SaManagerStatus saManagerStatus_ = SaManagerStatus::WAIT_NOTIFY;
103     std::condition_variable proxyConVar_;
104 };
105 
106 class BluetoothHost::impl::BluetoothHostObserverImp : public BluetoothHostObserverStub {
107 public:
BluetoothHostObserverImp(BluetoothHost::impl & host)108     explicit BluetoothHostObserverImp(BluetoothHost::impl &host) : host_(host){};
~BluetoothHostObserverImp()109     ~BluetoothHostObserverImp() override{};
110 
Register(std::shared_ptr<BluetoothHostObserver> & observer)111     void Register(std::shared_ptr<BluetoothHostObserver> &observer)
112     {
113         host_.observers_.Register(observer);
114     }
115 
Deregister(std::shared_ptr<BluetoothHostObserver> & observer)116     void Deregister(std::shared_ptr<BluetoothHostObserver> &observer)
117     {
118         host_.observers_.Deregister(observer);
119     }
120 
OnStateChanged(int32_t transport,int32_t status)121     void OnStateChanged(int32_t transport, int32_t status) override
122     {
123         if (status == BTStateID::STATE_TURN_ON) {
124             host_.SyncRandomAddrToService();
125         }
126         CHECK_AND_RETURN_LOG(!isNeedInterceptSwitchStatus(transport, status), "No Need transform same status");
127         BluetoothProfileManager::GetInstance().NotifyBluetoothStateChange(transport, status);
128         host_.observers_.ForEach([transport, status](std::shared_ptr<BluetoothHostObserver> observer) {
129             observer->OnStateChanged(transport, status);
130         });
131     }
132 
OnBluetoothStateChanged(int32_t state)133     void OnBluetoothStateChanged(int32_t state) override
134     {
135         std::lock_guard<std::mutex> lock(host_.switchModuleMutex_);
136         CHECK_AND_RETURN_LOG(host_.switchModule_, "switchModule is nullptr");
137         if (state == bluetooth::BluetoothSwitchState::STATE_ON) {
138             host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_ON);
139         }
140         if (state == bluetooth::BluetoothSwitchState::STATE_OFF) {
141             host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_OFF);
142         }
143         if (state == bluetooth::BluetoothSwitchState::STATE_HALF) {
144             host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_HALF);
145         }
146     }
147 
OnDiscoveryStateChanged(int32_t status)148     void OnDiscoveryStateChanged(int32_t status) override
149     {
150         HILOGD("enter, status: %{public}d", status);
151         host_.observers_.ForEach(
152             [status](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDiscoveryStateChanged(status); });
153     }
154 
OnDiscoveryResult(const BluetoothRawAddress & device,int rssi,const std::string deviceName,int deviceClass)155     void OnDiscoveryResult(
156         const BluetoothRawAddress &device, int rssi, const std::string deviceName, int deviceClass) override
157     {
158         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
159         host_.observers_.ForEach([remoteDevice, rssi, deviceName, deviceClass](
160             std::shared_ptr<BluetoothHostObserver> observer) {
161             observer->OnDiscoveryResult(remoteDevice, rssi, deviceName, deviceClass);
162         });
163     }
164 
OnPairRequested(const int32_t transport,const BluetoothRawAddress & device)165     void OnPairRequested(const int32_t transport, const BluetoothRawAddress &device) override
166     {
167         HILOGI("enter, transport: %{public}d, device: %{public}s",
168             transport, GET_ENCRYPT_RAW_ADDR(device));
169         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
170         host_.observers_.ForEach([remoteDevice](std::shared_ptr<BluetoothHostObserver> observer) {
171             observer->OnPairRequested(remoteDevice);
172         });
173     }
174 
OnPairConfirmed(const int32_t transport,const BluetoothRawAddress & device,int reqType,int number)175     void OnPairConfirmed(const int32_t transport, const BluetoothRawAddress &device, int reqType, int number) override
176     {
177         HILOGI("enter, transport: %{public}d, device: %{public}s, reqType: %{public}d, number: %{public}d",
178             transport, GET_ENCRYPT_RAW_ADDR(device), reqType, number);
179         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
180         host_.observers_.ForEach([remoteDevice, reqType, number](std::shared_ptr<BluetoothHostObserver> observer) {
181             observer->OnPairConfirmed(remoteDevice, reqType, number);
182         });
183     }
184 
OnScanModeChanged(int mode)185     void OnScanModeChanged(int mode) override
186     {
187         HILOGI("enter, mode: %{public}d", mode);
188         host_.observers_.ForEach(
189             [mode](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnScanModeChanged(mode); });
190     }
191 
OnDeviceNameChanged(const std::string & deviceName)192     void OnDeviceNameChanged(const std::string &deviceName) override
193     {
194         HILOGI("enter, deviceName: %{public}s", deviceName.c_str());
195         host_.observers_.ForEach([deviceName](std::shared_ptr<BluetoothHostObserver> observer) {
196             observer->OnDeviceNameChanged(deviceName);
197         });
198     }
199 
OnDeviceAddrChanged(const std::string & address)200     void OnDeviceAddrChanged(const std::string &address) override
201     {
202         HILOGD("enter");
203         host_.observers_.ForEach(
204             [address](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDeviceAddrChanged(address); });
205     }
206 
OnRefusePolicyChanged(const int32_t pid,const int64_t prohibitedSecondsTime)207     void OnRefusePolicyChanged(const int32_t pid, const int64_t prohibitedSecondsTime) override
208     {
209         HILOGI("OnRefusePolicyChanged, pid: %{public}d time %{public}" PRId64"", pid, prohibitedSecondsTime);
210         host_.refusePolicyProhibitedTime_ = prohibitedSecondsTime;
211     }
212 private:
isNeedInterceptSwitchStatus(int32_t transport,int32_t status)213     bool isNeedInterceptSwitchStatus(int32_t transport, int32_t status)
214     {
215         bool isBluetoothSeriviceOn = BluetoothProfileManager::GetInstance().IsBluetoothServiceOn();
216         if (status == BTStateID::STATE_TURN_OFF) {
217             if (transport == BTTransport::ADAPTER_BLE &&
218                 preBleState_ == BTStateID::STATE_TURN_OFF && !isBluetoothSeriviceOn) {
219                 return true;
220             }
221             if (transport == BTTransport::ADAPTER_BREDR &&
222                 preBrState_ == BTStateID::STATE_TURN_OFF && !isBluetoothSeriviceOn) {
223                 return true;
224             }
225         }
226         if (transport == BTTransport::ADAPTER_BREDR) {
227             preBrState_ = status;
228         } else {
229             preBleState_ = status;
230         }
231         return false;
232     }
233     BluetoothHost::impl &host_;
234     const int32_t INVALID_STATE = -1;
235     int32_t preBrState_ = INVALID_STATE;
236     int32_t preBleState_ = INVALID_STATE;
237     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHostObserverImp);
238 };
239 
240 class BluetoothHost::impl::BluetoothRemoteDeviceObserverImp : public BluetoothRemoteDeviceObserverstub {
241 public:
BluetoothRemoteDeviceObserverImp(BluetoothHost::impl & host)242     explicit BluetoothRemoteDeviceObserverImp(BluetoothHost::impl &host) : host_(host){};
243     ~BluetoothRemoteDeviceObserverImp() override = default;
244 
OnAclStateChanged(const BluetoothRawAddress & device,int32_t state,uint32_t reason)245     void OnAclStateChanged(const BluetoothRawAddress &device, int32_t state, uint32_t reason) override
246     {
247         HILOGD("enter, device: %{public}s, state: %{public}d, reason: %{public}u",
248             GET_ENCRYPT_RAW_ADDR(device), state, reason);
249         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
250         host_.remoteObservers_.ForEach(
251             [remoteDevice, state, reason](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
252                 observer->OnAclStateChanged(remoteDevice, state, reason);
253             });
254     }
255 
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int32_t status,int32_t cause)256     void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device,
257         int32_t status, int32_t cause) override
258     {
259         HILOGD("enter, transport: %{public}d, device: %{public}s, status: %{public}d, cause: %{public}d",
260             transport, GET_ENCRYPT_RAW_ADDR(device), status, cause);
261         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
262         host_.remoteObservers_.ForEach(
263             [remoteDevice, status, cause](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
264                 observer->OnPairStatusChanged(remoteDevice, status, cause);
265             });
266     }
267 
OnRemoteUuidChanged(const BluetoothRawAddress & device,const std::vector<bluetooth::Uuid> uuids)268     void OnRemoteUuidChanged(const BluetoothRawAddress &device, const std::vector<bluetooth::Uuid> uuids) override
269     {
270         HILOGD("enter, device: %{public}s", GET_ENCRYPT_RAW_ADDR(device));
271         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
272         host_.remoteObservers_.ForEach(
273             [remoteDevice, uuids](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
274                 std::vector<ParcelUuid> parcelUuids;
275                 for (auto &uuid : uuids) {
276                     ParcelUuid parcelUuid = UUID::ConvertFrom128Bits(uuid.ConvertTo128Bits());
277                     parcelUuids.push_back(parcelUuid);
278                 }
279                 observer->OnRemoteUuidChanged(remoteDevice, parcelUuids);
280             });
281     }
282 
OnRemoteNameChanged(const BluetoothRawAddress & device,const std::string deviceName)283     void OnRemoteNameChanged(const BluetoothRawAddress &device, const std::string deviceName) override
284     {
285         HILOGD("enter, device: %{public}s, deviceName: %{public}s",
286             GET_ENCRYPT_RAW_ADDR(device), deviceName.c_str());
287         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
288         host_.remoteObservers_.ForEach(
289             [remoteDevice, deviceName](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
290                 observer->OnRemoteNameChanged(remoteDevice, deviceName);
291             });
292     }
293 
OnRemoteAliasChanged(const BluetoothRawAddress & device,const std::string alias)294     void OnRemoteAliasChanged(const BluetoothRawAddress &device, const std::string alias) override
295     {
296         HILOGI("enter, device: %{public}s, alias: %{public}s",
297             GET_ENCRYPT_RAW_ADDR(device), alias.c_str());
298         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
299         host_.remoteObservers_.ForEach(
300             [remoteDevice, alias](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
301                 observer->OnRemoteAliasChanged(remoteDevice, alias);
302             });
303     }
304 
OnRemoteCodChanged(const BluetoothRawAddress & device,int32_t cod)305     void OnRemoteCodChanged(const BluetoothRawAddress &device, int32_t cod) override
306     {
307         HILOGD("enter, device: %{public}s, cod: %{public}d", GET_ENCRYPT_RAW_ADDR(device), cod);
308         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
309         BluetoothDeviceClass deviceClass(cod);
310         host_.remoteObservers_.ForEach(
311             [remoteDevice, deviceClass](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
312                 observer->OnRemoteCodChanged(remoteDevice, deviceClass);
313             });
314     }
315 
OnRemoteBatteryChanged(const BluetoothRawAddress & device,const BluetoothBatteryInfo & batteryInfo)316     void OnRemoteBatteryChanged(const BluetoothRawAddress &device, const BluetoothBatteryInfo &batteryInfo) override
317     {
318         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
319         DeviceBatteryInfo info;
320         info.deviceId_ = device.GetAddress();
321         info.batteryLevel_ = batteryInfo.batteryLevel_;
322         info.leftEarBatteryLevel_ = batteryInfo.leftEarBatteryLevel_;
323         info.leftEarChargeState_ = static_cast<DeviceChargeState>(batteryInfo.leftEarChargeState_);
324         info.rightEarBatteryLevel_ = batteryInfo.rightEarBatteryLevel_;
325         info.rightEarChargeState_ = static_cast<DeviceChargeState>(batteryInfo.rightEarChargeState_);
326         info.boxBatteryLevel_ = batteryInfo.boxBatteryLevel_;
327         info.boxChargeState_ = static_cast<DeviceChargeState>(batteryInfo.boxChargeState_);
328         host_.remoteObservers_.ForEach(
329             [remoteDevice, info](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
330                 observer->OnRemoteBatteryChanged(remoteDevice, info);
331             });
332     }
333 
OnRemoteDeviceCommonInfoReport(const BluetoothRawAddress & device,const std::vector<uint8_t> & value)334     void OnRemoteDeviceCommonInfoReport(const BluetoothRawAddress &device, const std::vector<uint8_t> &value) override
335     {
336         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
337         host_.remoteObservers_.ForEach(
338             [remoteDevice, value](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
339                 observer->OnRemoteDeviceCommonInfoReport(remoteDevice, value);
340             });
341     }
342 
343 private:
344     BluetoothHost::impl &host_;
345     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteDeviceObserverImp);
346 };
347 
348 class BluetoothHost::impl::BluetoothBlePeripheralCallbackImp : public BluetoothBlePeripheralObserverStub {
349 public:
BluetoothBlePeripheralCallbackImp(BluetoothHost::impl & host)350     explicit BluetoothBlePeripheralCallbackImp(BluetoothHost::impl &host) : host_(host){};
351     ~BluetoothBlePeripheralCallbackImp() override = default;
352 
OnAclStateChanged(const BluetoothRawAddress & device,int state,unsigned int reason)353     void OnAclStateChanged(const BluetoothRawAddress &device, int state, unsigned int reason) override
354     {
355         HILOGD("enter, device: %{public}s, state: %{public}d, reason: %{public}u",
356             GET_ENCRYPT_RAW_ADDR(device), state, reason);
357         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BLE);
358         host_.remoteObservers_.ForEach(
359             [remoteDevice, state, reason](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
360                 observer->OnAclStateChanged(remoteDevice, state, reason);
361             });
362     }
363 
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int status,int cause)364     void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device, int status, int cause) override
365     {
366         HILOGI("enter, transport: %{public}d, device: %{public}s, status: %{public}d, cause: %{public}d",
367             transport, GET_ENCRYPT_RAW_ADDR(device), status, cause);
368         BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
369         host_.remoteObservers_.ForEach(
370             [remoteDevice, status, cause](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
371                 observer->OnPairStatusChanged(remoteDevice, status, cause);
372             });
373     }
374 
OnReadRemoteRssiEvent(const BluetoothRawAddress & device,int rssi,int status)375     void OnReadRemoteRssiEvent(const BluetoothRawAddress &device, int rssi, int status) override
376     {
377         HILOGI("enter, device: %{public}s, rssi: %{public}d, status: %{public}d",
378             GET_ENCRYPT_RAW_ADDR(device), rssi, status);
379         BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BLE);
380         host_.remoteObservers_.ForEach(
381             [remoteDevice, rssi, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
382                 observer->OnReadRemoteRssiEvent(remoteDevice, rssi, status);
383             });
384     }
385 
386 private:
387     BluetoothHost::impl &host_;
388     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBlePeripheralCallbackImp);
389 };
390 
391 class BluetoothHost::impl::BluetoothResourceManagerObserverImp : public BluetoothResourceManagerObserverStub {
392 public:
BluetoothResourceManagerObserverImp(BluetoothHost::impl & host)393     explicit BluetoothResourceManagerObserverImp(BluetoothHost::impl &host) : host_(host){};
394     ~BluetoothResourceManagerObserverImp() override = default;
395 
OnSensingStateChanged(uint8_t eventId,const BluetoothSensingInfo & info)396     void OnSensingStateChanged(uint8_t eventId, const BluetoothSensingInfo &info) override
397     {
398         HILOGD("enter, eventId: %{public}d", eventId);
399         SensingInfo sensingInfo;
400         sensingInfo.addr_ = info.addr_;
401         sensingInfo.uuid_ = info.uuid_;
402         sensingInfo.resourceId_ = info.resourceId_;
403         sensingInfo.pkgName_ = info.pkgName_;
404         sensingInfo.isServer_ = info.isServer_;
405         sensingInfo.interval_ = info.interval_;
406         sensingInfo.connectable_ = info.connectable_;
407         sensingInfo.payloadLen_ = info.payloadLen_;
408         sensingInfo.bussinessType_ = info.bussinessType_;
409         sensingInfo.scanMode_ = info.scanMode_;
410         host_.resourceManagerObservers_.ForEach(
411             [eventId, sensingInfo](std::shared_ptr<BluetoothResourceManagerObserver> observer) {
412                 observer->OnSensingStateChanged(eventId, sensingInfo);
413             });
414     }
415 
OnBluetoothResourceDecision(uint8_t eventId,const BluetoothSensingInfo & info,uint32_t & result)416     void OnBluetoothResourceDecision(uint8_t eventId, const BluetoothSensingInfo &info, uint32_t &result) override
417     {
418         HILOGD("enter, eventId: %{public}d, result: %{public}d", eventId, result);
419         SensingInfo sensingInfo;
420         sensingInfo.addr_ = info.addr_;
421         sensingInfo.uuid_ = info.uuid_;
422         sensingInfo.resourceId_ = info.resourceId_;
423         sensingInfo.pkgName_ = info.pkgName_;
424         sensingInfo.isServer_ = info.isServer_;
425         sensingInfo.interval_ = info.interval_;
426         host_.resourceManagerObservers_.ForEach(
427             [eventId, sensingInfo, &result](std::shared_ptr<BluetoothResourceManagerObserver> observer) {
428                 observer->OnBluetoothResourceDecision(eventId, sensingInfo, result);
429             });
430     }
431 
432 private:
433     BluetoothHost::impl &host_;
434     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothResourceManagerObserverImp);
435 };
436 
437 class BluetoothHost::impl::BluetoothSwitchAction : public IBluetoothSwitchAction {
438 public:
439     BluetoothSwitchAction() = default;
440     ~BluetoothSwitchAction() override = default;
441 
EnableBluetooth(bool noAutoConnect)442     int EnableBluetooth(bool noAutoConnect) override
443     {
444         CHECK_AND_RETURN_LOG_RET(!BluetoothHost::GetDefaultHost().IsBtProhibitedByEdm(),
445             BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited !");
446         CHECK_AND_RETURN_LOG_RET(BluetoothHost::GetDefaultHost().pimpl->LoadBluetoothHostService(),
447             BT_ERR_INTERNAL_ERROR, "pimpl is null or load bluetooth service failed.");
448 
449         sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
450         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
451         return proxy->EnableBle(noAutoConnect);
452     }
453 
DisableBluetooth(void)454     int DisableBluetooth(void) override
455     {
456         sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
457         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
458         return proxy->DisableBt();
459     }
460 
EnableBluetoothToRestrictMode(void)461     int EnableBluetoothToRestrictMode(void) override
462     {
463         CHECK_AND_RETURN_LOG_RET(!BluetoothHost::GetDefaultHost().IsBtProhibitedByEdm(),
464             BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited !");
465         CHECK_AND_RETURN_LOG_RET(BluetoothHost::GetDefaultHost().pimpl->LoadBluetoothHostService(),
466             BT_ERR_INTERNAL_ERROR, "pimpl is null or load bluetooth service failed.");
467 
468         sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
469         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
470         return proxy->EnableBluetoothToRestrictMode();
471     }
472 };
473 
impl()474 BluetoothHost::impl::impl()
475 {
476     observerImp_ = new BluetoothHostObserverImp(*this);
477     remoteObserverImp_ = new BluetoothRemoteDeviceObserverImp(*this);
478     bleRemoteObserverImp_ = new BluetoothBlePeripheralCallbackImp(*this);
479     bleObserverImp_ = new BluetoothHostObserverImp(*this);
480     resourceManagerObserverImp_ = new BluetoothResourceManagerObserverImp(*this);
481 
482     auto switchActionPtr = std::make_unique<BluetoothSwitchAction>();
483     switchModule_ = std::make_shared<BluetoothSwitchModule>(std::move(switchActionPtr));
484 
485     profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(BLUETOOTH_HOST,
486         [this](sptr<IRemoteObject> remote) {
487         sptr<IBluetoothHost> proxy = iface_cast<IBluetoothHost>(remote);
488         CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
489         proxy->RegisterObserver(observerImp_);
490         proxy->RegisterBleAdapterObserver(bleObserverImp_);
491         proxy->RegisterRemoteDeviceObserver(remoteObserverImp_);
492         proxy->RegisterBlePeripheralCallback(bleRemoteObserverImp_);
493         proxy->RegisterBtResourceManagerObserver(resourceManagerObserverImp_);
494     });
495 }
496 
~impl()497 BluetoothHost::impl::~impl()
498 {
499     HILOGI("starts");
500     BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
501     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
502     CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
503     proxy->DeregisterObserver(observerImp_);
504     proxy->DeregisterBleAdapterObserver(bleObserverImp_);
505     proxy->DeregisterRemoteDeviceObserver(remoteObserverImp_);
506     proxy->DeregisterBlePeripheralCallback(bleRemoteObserverImp_);
507     proxy->DeregisterBtResourceManagerObserver(resourceManagerObserverImp_);
508 }
509 
LoadBluetoothHostService()510 bool BluetoothHost::impl::LoadBluetoothHostService()
511 {
512     std::unique_lock<std::mutex> lock(proxyMutex_);
513     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
514     if (samgrProxy == nullptr) {
515         HILOGE("samgrProxy is nullptr.");
516         return false;
517     }
518     sptr<IRemoteObject> hostRemote = BluetoothProfileManager::GetInstance().GetProfileRemote(BLUETOOTH_HOST);
519     //当蓝牙服务已经起来的时候。这时的hostRemote不为空, 不需要进行后续的从sa拉起蓝牙服务的动作
520     if (hostRemote != nullptr) {
521         return true;
522     }
523 
524     sptr<BluetoothHostLoadCallBack> loadCallback = new BluetoothHostLoadCallBack();
525     if (loadCallback == nullptr) {
526         HILOGE("loadCallback is nullptr.");
527         return false;
528     }
529     int32_t ret = samgrProxy->LoadSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID, loadCallback);
530     if (ret != ERR_OK) {
531         HILOGE("Failed to load bluetooth systemAbility");
532         return false;
533     }
534     auto waitStatus = proxyConVar_.wait_for(
535         lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), [this]() {
536             HILOGI("bluetooth_service load systemAbility finished");
537             sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
538             return proxy != nullptr || saManagerStatus_ == SaManagerStatus::LOAD_FAIL;
539         });
540     if (!waitStatus) {
541         HILOGE("load bluetooth systemAbility timeout");
542         return false;
543     }
544     if (saManagerStatus_ == SaManagerStatus::LOAD_FAIL) {
545         HILOGE("load bluetooth_service fail");
546         saManagerStatus_ = SaManagerStatus::WAIT_NOTIFY;
547         return false;
548     }
549     saManagerStatus_ = SaManagerStatus::WAIT_NOTIFY;
550     return true;
551 }
552 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)553 void BluetoothHost::impl::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
554 {
555     HILOGI("LoadSystemAbilitySuccess FinishStart SA");
556     saManagerStatus_ = SaManagerStatus::LOAD_SUCCESS;
557     proxyConVar_.notify_one();
558 }
559 
LoadSystemAbilityFail()560 void BluetoothHost::impl::LoadSystemAbilityFail()
561 {
562     HILOGI("LoadSystemAbilityFail FinishStart SA");
563     saManagerStatus_ = SaManagerStatus::LOAD_FAIL;
564     proxyConVar_.notify_one();
565 }
566 
SyncRandomAddrToService(void)567 void BluetoothHost::impl::SyncRandomAddrToService(void)
568 {
569     if (!IsValidBluetoothAddr(stagingRealAddr_)) {
570         HILOGD("stagingRealAddr_ is invalid.");
571         return;
572     }
573     if (!IsValidBluetoothAddr(stagingRandomAddr_)) {
574         HILOGE("stagingRandomAddr_ is invalid.");
575         return;
576     }
577     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
578     CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
579     proxy->SyncRandomAddress(stagingRealAddr_, stagingRandomAddr_);
580     stagingRealAddr_ = "";
581     stagingRandomAddr_ = "";
582 }
583 
ConvertBluetoothStateToBtStateID(BluetoothState state)584 int BluetoothHost::impl::ConvertBluetoothStateToBtStateID(BluetoothState state)
585 {
586     int ret = BTStateID::STATE_TURN_OFF;
587     switch (state) {
588         case BluetoothState::STATE_ON: ret = BTStateID::STATE_TURN_ON; break;
589         case BluetoothState::STATE_TURNING_ON: ret = BTStateID::STATE_TURNING_ON; break;
590         case BluetoothState::STATE_TURNING_OFF: ret = BTStateID::STATE_TURNING_OFF; break;
591         case BluetoothState::STATE_OFF: ret = BTStateID::STATE_TURN_OFF; break;
592         default: break;
593     }
594     return ret;
595 }
596 
BluetoothHost()597 BluetoothHost::BluetoothHost()
598 {
599     pimpl = std::make_unique<impl>();
600     if (!pimpl) {
601         HILOGE("fails: no pimpl");
602     }
603 }
604 
~BluetoothHost()605 BluetoothHost::~BluetoothHost() {}
606 
GetDefaultHost()607 BluetoothHost &BluetoothHost::GetDefaultHost()
608 {
609 #ifdef DTFUZZ_TEST
610     static BluetoothNoDestructor<BluetoothHost> instance;
611     return *instance;
612 #else
613     // C++11 static local variable initialization is thread-safe.
614     static BluetoothHost hostAdapter;
615     return hostAdapter;
616 #endif
617 }
618 
RegisterObserver(std::shared_ptr<BluetoothHostObserver> observer)619 void BluetoothHost::RegisterObserver(std::shared_ptr<BluetoothHostObserver> observer)
620 {
621     HILOGD("enter");
622     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
623     pimpl->observers_.Register(observer);
624 }
625 
DeregisterObserver(std::shared_ptr<BluetoothHostObserver> observer)626 void BluetoothHost::DeregisterObserver(std::shared_ptr<BluetoothHostObserver> observer)
627 {
628     HILOGD("enter");
629     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
630     pimpl->observers_.Deregister(observer);
631 }
632 
EnableBt()633 int BluetoothHost::EnableBt()
634 {
635     HILOGD("enter");
636     CHECK_AND_RETURN_LOG_RET(!IsBtProhibitedByEdm(), BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited");
637     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
638     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
639 
640     return proxy->EnableBt();
641 }
642 
DisableBt()643 int BluetoothHost::DisableBt()
644 {
645     HILOGI("enter");
646     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
647     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
648     return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH);
649 }
650 
PublishBtSwitchRestrictBluetoothEvent(void)651 static void PublishBtSwitchRestrictBluetoothEvent(void)
652 {
653     OHOS::AAFwk::Want want;
654     want.SetAction("usual.event.bluetooth.BT_SWITCH_RESTRICT_BLUETOOTH");
655 
656     OHOS::EventFwk::CommonEventData data;
657     data.SetWant(want);
658 
659     OHOS::EventFwk::CommonEventPublishInfo publishInfo;
660     publishInfo.SetSubscriberPermissions({"ohos.permission.ACCESS_BLUETOOTH"});
661     bool ret = OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo);
662     if (!ret) {
663         HILOGE("Publish usual.event.bluetooth.BT_SWITCH_RESTRICT_BLUETOOTH event failed");
664         return;
665     }
666 }
667 
RestrictBluetooth()668 int BluetoothHost::RestrictBluetooth()
669 {
670     HILOGI("enter");
671     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
672     PublishBtSwitchRestrictBluetoothEvent();
673     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
674     int ret =  pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH);
675     if (ret != BT_NO_ERROR) {
676         return ret;
677     }
678     ret = pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE);
679     return ret;
680 }
681 
SatelliteControl(int type,int state)682 int BluetoothHost::SatelliteControl(int type, int state)
683 {
684     HILOGI("type: %{public}d, state: %{public}d", type, state);
685     if (type == static_cast<int>(SATELLITE_CONTROL_MODE::ANTENNA)) {
686         CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
687     } else if (type == static_cast<int>(SATELLITE_CONTROL_MODE::BLUETOOTH_SWITCH)) {
688         pimpl->LoadBluetoothHostService();
689     } else {
690         HILOGE("Invalid control type: %{public}d", type);
691         return BT_ERR_INVALID_PARAM;
692     }
693     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
694     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
695     return proxy->SatelliteControl(type, state);
696 }
697 
UpdateVirtualDevice(int32_t action,const std::string & address)698 void BluetoothHost::UpdateVirtualDevice(int32_t action, const std::string &address)
699 {
700     HILOGD("enter");
701     CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off");
702     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
703     CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
704     proxy->UpdateVirtualDevice(action, address);
705 }
706 
GetBtState() const707 int BluetoothHost::GetBtState() const
708 {
709     BluetoothState state = GetBluetoothState();
710     return pimpl->ConvertBluetoothStateToBtStateID(state);
711 }
712 
GetBtState(int & state) const713 int BluetoothHost::GetBtState(int &state) const
714 {
715     state = GetBtState();
716     return BT_NO_ERROR;
717 }
718 
GetBluetoothState(void) const719 BluetoothState BluetoothHost::GetBluetoothState(void) const
720 {
721     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
722     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BluetoothState::STATE_OFF, "proxy is nullptr");
723     int state = static_cast<int>(BluetoothState::STATE_OFF);
724     proxy->GetBtState(state);
725     return static_cast<BluetoothState>(state);
726 }
727 
EnableBluetoothAfterFactoryReset(void)728 int BluetoothHost::impl::EnableBluetoothAfterFactoryReset(void)
729 {
730     HILOGI("Attempt to enable bluetooth after factory reset");
731     isFactoryReseting_ = false;
732     SetParameter("persist.bluetooth.switch_enable", "2");  // 2 means bluetooth auto enter restricted mode
733     return BT_NO_ERROR;
734 }
735 
BluetoothFactoryReset()736 int BluetoothHost::BluetoothFactoryReset()
737 {
738     HILOGI("enter");
739     constexpr const char* BLUETOOTH_FACTORY_RESET_KEY = "persist.bluetooth.factoryreset";
740     int ret = SetParameter(BLUETOOTH_FACTORY_RESET_KEY, "true");
741     CHECK_AND_RETURN_LOG_RET(ret == 0, BT_ERR_INTERNAL_ERROR, "SetParameter failed");
742 
743     pimpl->isFactoryReseting_ = true;
744 
745     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
746     if (proxy == nullptr) {
747         return pimpl->EnableBluetoothAfterFactoryReset();
748     }
749     CHECK_AND_RETURN_LOG_RET(IS_BLE_ENABLED(), BT_NO_ERROR, "bluetooth is off.");
750     return proxy->BluetoothFactoryReset();
751 }
752 
IsValidBluetoothAddr(const std::string & addr)753 bool BluetoothHost::IsValidBluetoothAddr(const std::string &addr)
754 {
755 #if defined(IOS_PLATFORM)
756     const std::regex deviceIdRegex("^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$");
757     return regex_match(addr, deviceIdRegex);
758 #elif defined(ANDROID_PLATFORM)
759     const std::regex deviceIdRegex("^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$");
760     return regex_match(addr, deviceIdRegex);
761 #else
762     if (addr.length() != ADDRESS_LENGTH) {
763         HILOGD("invalid address len.");
764         return false;
765     }
766 
767     for (int i = 0; i < ADDRESS_LENGTH; i++) {
768         char c = addr[i];
769         switch (i % ADDRESS_SEPARATOR_UNIT) {
770             case 0:
771             case 1:
772                 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
773                     break;
774                 }
775                 return false;
776             case ADDRESS_COLON_INDEX:
777             default:
778                 if (c == ':') {
779                     break;
780                 }
781                 return false;
782         }
783     }
784     return true;
785 #endif
786 }
787 
GetRemoteDevice(const std::string & addr,int transport) const788 BluetoothRemoteDevice BluetoothHost::GetRemoteDevice(const std::string &addr, int transport) const
789 {
790     BluetoothRemoteDevice remoteDevice(addr, transport);
791     return remoteDevice;
792 }
793 
EnableBle()794 int BluetoothHost::EnableBle()
795 {
796     HILOGI("enter");
797     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
798     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
799     return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH);
800 }
801 
EnableBluetoothNoAutoConnect()802 int BluetoothHost::EnableBluetoothNoAutoConnect()
803 {
804     HILOGI("enter");
805     pimpl->switchModule_->SetNoAutoConnect(true);
806     return EnableBle();
807 }
808 
DisableBle()809 int BluetoothHost::DisableBle()
810 {
811     HILOGD("enter");
812     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
813     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
814 
815     return proxy->DisableBle();
816 }
817 
EnableBluetoothToRestrictMode(void)818 int BluetoothHost::EnableBluetoothToRestrictMode(void)
819 {
820     HILOGI("enter");
821     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
822     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
823     return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE);
824 }
825 
IsBrEnabled() const826 bool BluetoothHost::IsBrEnabled() const
827 {
828     BluetoothState state = GetBluetoothState();
829     return state == BluetoothState::STATE_ON;
830 }
831 
IsBleEnabled() const832 bool BluetoothHost::IsBleEnabled() const
833 {
834     BluetoothState state = GetBluetoothState();
835     return (state == BluetoothState::STATE_ON ||
836         state == BluetoothState::STATE_BLE_ON ||
837         state == BluetoothState::STATE_TURNING_ON ||
838         state == BluetoothState::STATE_TURNING_OFF);
839 }
840 
GetLocalAddress(std::string & addr) const841 int BluetoothHost::GetLocalAddress(std::string &addr) const
842 {
843     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
844     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
845 
846     return proxy->GetLocalAddress(addr);
847 }
848 
GetProfileList() const849 std::vector<uint32_t> BluetoothHost::GetProfileList() const
850 {
851     HILOGD("enter");
852     std::vector<uint32_t> profileList;
853     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
854     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, profileList, "proxy is nullptr");
855 
856     profileList = proxy->GetProfileList();
857     return profileList;
858 }
859 
GetMaxNumConnectedAudioDevices() const860 int BluetoothHost::GetMaxNumConnectedAudioDevices() const
861 {
862     HILOGD("enter");
863     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
864     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
865 
866     return proxy->GetMaxNumConnectedAudioDevices();
867 }
868 
GetBtConnectionState() const869 int BluetoothHost::GetBtConnectionState() const
870 {
871     HILOGD("enter");
872     int state = static_cast<int>(BTConnectState::DISCONNECTED);
873     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), state, "bluetooth is off.");
874 
875     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
876     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, state, "proxy is nullptr");
877 
878     proxy->GetBtConnectionState(state);
879     HILOGI("state: %{public}d", state);
880     return state;
881 }
882 
GetBtConnectionState(int & state) const883 int BluetoothHost::GetBtConnectionState(int &state) const
884 {
885     HILOGD("enter");
886     state = static_cast<int>(BTConnectState::DISCONNECTED);
887     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
888 
889     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
890     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
891 
892     int ret = proxy->GetBtConnectionState(state);
893     HILOGI("state: %{public}d", state);
894     return ret;
895 }
896 
GetBtProfileConnState(uint32_t profileId,int & state) const897 int BluetoothHost::GetBtProfileConnState(uint32_t profileId, int &state) const
898 {
899     HILOGI("profileId: %{public}d", profileId);
900     state = static_cast<int>(BTConnectState::DISCONNECTED);
901     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
902 
903     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
904     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
905 
906     return proxy->GetBtProfileConnState(profileId, state);
907 }
908 
GetLocalSupportedUuids(std::vector<ParcelUuid> & uuids)909 void BluetoothHost::GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids)
910 {
911     HILOGD("enter");
912     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
913     CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
914 
915     std::vector<std::string> stringUuids;
916     proxy->GetLocalSupportedUuids(stringUuids);
917 
918     for (auto uuid : stringUuids) {
919         uuids.push_back(UUID::FromString(uuid));
920     }
921 }
922 
Start()923 bool BluetoothHost::Start()
924 {
925     // / This function only used for passthrough, so this is empty.
926     return true;
927 }
928 
Stop()929 void BluetoothHost::Stop()
930 {
931     // / This function only used for passthrough, so this is empty.
932 }
933 
GetLocalDeviceClass() const934 BluetoothDeviceClass BluetoothHost::GetLocalDeviceClass() const
935 {
936     HILOGD("enter");
937     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
938     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BluetoothDeviceClass(0), "proxy is nullptr");
939 
940     int LocalDeviceClass = proxy->GetLocalDeviceClass();
941     return BluetoothDeviceClass(LocalDeviceClass);
942 }
943 
SetLocalDeviceClass(const BluetoothDeviceClass & deviceClass)944 bool BluetoothHost::SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass)
945 {
946     HILOGD("enter");
947     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
948     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
949 
950     int cod = deviceClass.GetClassOfDevice();
951     return proxy->SetLocalDeviceClass(cod);
952 }
953 
GetLocalName() const954 std::string BluetoothHost::GetLocalName() const
955 {
956     HILOGD("enter");
957     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
958     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::string(), "proxy is nullptr");
959 
960     std::string name = INVALID_NAME;
961     proxy->GetLocalName(name);
962     return name;
963 }
964 
GetLocalName(std::string & name) const965 int BluetoothHost::GetLocalName(std::string &name) const
966 {
967     HILOGD("enter");
968     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
969     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
970 
971     return proxy->GetLocalName(name);
972 }
973 
SetLocalName(const std::string & name)974 int BluetoothHost::SetLocalName(const std::string &name)
975 {
976     HILOGD("enter");
977     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
978 
979     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
980     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
981 
982     return proxy->SetLocalName(name);
983 }
984 
GetBtScanMode(int32_t & scanMode) const985 int BluetoothHost::GetBtScanMode(int32_t &scanMode) const
986 {
987     HILOGD("enter");
988     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
989 
990     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
991     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
992 
993     return proxy->GetBtScanMode(scanMode);
994 }
995 
SetBtScanMode(int mode,int duration)996 int BluetoothHost::SetBtScanMode(int mode, int duration)
997 {
998     HILOGD("mode: %{public}d, duration: %{public}d", mode, duration);
999     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1000 
1001     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1002     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1003 
1004     return proxy->SetBtScanMode(mode, duration);
1005 }
1006 
GetBondableMode(int transport) const1007 int BluetoothHost::GetBondableMode(int transport) const
1008 {
1009     HILOGI("transport: %{public}d", transport);
1010     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1011     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
1012 
1013     return proxy->GetBondableMode(transport);
1014 }
1015 
SetBondableMode(int transport,int mode)1016 bool BluetoothHost::SetBondableMode(int transport, int mode)
1017 {
1018     HILOGD("transport: %{public}d, mode: %{public}d", transport, mode);
1019     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1020     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
1021 
1022     return proxy->SetBondableMode(transport, mode);
1023 }
1024 
StartBtDiscovery()1025 int BluetoothHost::StartBtDiscovery()
1026 {
1027     HILOGD("enter");
1028     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1029 
1030     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1031     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1032 
1033     return proxy->StartBtDiscovery();
1034 }
1035 
CancelBtDiscovery()1036 int BluetoothHost::CancelBtDiscovery()
1037 {
1038     HILOGD("enter");
1039     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1040 
1041     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1042     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1043 
1044     return proxy->CancelBtDiscovery();
1045 }
1046 
IsBtDiscovering(bool & isDiscovering,int transport) const1047 int32_t BluetoothHost::IsBtDiscovering(bool &isDiscovering, int transport) const
1048 {
1049     HILOGI("transport: %{public}d", transport);
1050     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1051 
1052     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1053     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr");
1054 
1055     return proxy->IsBtDiscovering(isDiscovering, transport);
1056 }
1057 
GetBtDiscoveryEndMillis() const1058 long BluetoothHost::GetBtDiscoveryEndMillis() const
1059 {
1060     HILOGD("enter");
1061     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), 0, "bluetooth is off.");
1062 
1063     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1064     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, 0, "proxy is nullptr");
1065 
1066     return proxy->GetBtDiscoveryEndMillis();
1067 }
1068 
GetPairedDevices(int transport,std::vector<BluetoothRemoteDevice> & pairedDevices) const1069 int32_t BluetoothHost::GetPairedDevices(int transport, std::vector<BluetoothRemoteDevice> &pairedDevices) const
1070 {
1071     HILOGI("transport: %{public}d", transport);
1072     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1073 
1074     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1075     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1076 
1077     std::vector<BluetoothRawAddress> pairedAddr;
1078     int32_t ret = proxy->GetPairedDevices(pairedAddr);
1079 
1080     for (auto it = pairedAddr.begin(); it != pairedAddr.end(); it++) {
1081         BluetoothRemoteDevice device((*it).GetAddress(), transport);
1082         pairedDevices.emplace_back(device);
1083     }
1084     return ret;
1085 }
1086 
RemovePair(const BluetoothRemoteDevice & device)1087 int32_t BluetoothHost::RemovePair(const BluetoothRemoteDevice &device)
1088 {
1089     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
1090     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device.");
1091 
1092     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1093     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1094 
1095     sptr<BluetoothRawAddress> rawAddrSptr = new BluetoothRawAddress(device.GetDeviceAddr());
1096     return proxy->RemovePair(device.GetTransportType(), rawAddrSptr);
1097 }
1098 
RemoveAllPairs()1099 bool BluetoothHost::RemoveAllPairs()
1100 {
1101     HILOGD("enter");
1102     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1103     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
1104 
1105     return proxy->RemoveAllPairs();
1106 }
1107 
RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)1108 void BluetoothHost::RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)
1109 {
1110     HILOGD("enter");
1111     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1112 
1113     pimpl->remoteObservers_.Register(observer);
1114 }
1115 
DeregisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)1116 void BluetoothHost::DeregisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)
1117 {
1118     HILOGD("enter");
1119     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1120 
1121     pimpl->remoteObservers_.Deregister(observer);
1122 }
1123 
GetBleMaxAdvertisingDataLength() const1124 int BluetoothHost::GetBleMaxAdvertisingDataLength() const
1125 {
1126     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1127     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
1128 
1129     return proxy->GetBleMaxAdvertisingDataLength();
1130 }
1131 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)1132 void BluetoothHost::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
1133 {
1134     CHECK_AND_RETURN_LOG(pimpl, "pimpl is null.");
1135 
1136     pimpl->LoadSystemAbilitySuccess(remoteObject);
1137 }
1138 
LoadSystemAbilityFail()1139 void BluetoothHost::LoadSystemAbilityFail()
1140 {
1141     CHECK_AND_RETURN_LOG(pimpl, "pimpl is null.");
1142 
1143     pimpl->LoadSystemAbilityFail();
1144 }
1145 
GetLocalProfileUuids(std::vector<std::string> & uuids)1146 int32_t BluetoothHost::GetLocalProfileUuids(std::vector<std::string> &uuids)
1147 {
1148     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INTERNAL_ERROR, "bluetooth is off.");
1149 
1150     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1151     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1152 
1153     return proxy->GetLocalProfileUuids(uuids);
1154 }
1155 
SetFastScan(bool isEnable)1156 int BluetoothHost::SetFastScan(bool isEnable)
1157 {
1158     HILOGI("isEnable: %{public}d", isEnable);
1159     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1160 
1161     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1162     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1163 
1164     return proxy->SetFastScan(isEnable);
1165 }
1166 
GetRandomAddress(const std::string & realAddr,std::string & randomAddr) const1167 int BluetoothHost::GetRandomAddress(const std::string &realAddr, std::string &randomAddr) const
1168 {
1169     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1170     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1171     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1172     return proxy->GetRandomAddress(realAddr, randomAddr);
1173 }
1174 
ConnectAllowedProfiles(const std::string & remoteAddr) const1175 int BluetoothHost::ConnectAllowedProfiles(const std::string &remoteAddr) const
1176 {
1177     HILOGI("enter");
1178     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1179 
1180     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1181     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1182 
1183     return proxy->ConnectAllowedProfiles(remoteAddr);
1184 }
1185 
DisconnectAllowedProfiles(const std::string & remoteAddr) const1186 int BluetoothHost::DisconnectAllowedProfiles(const std::string &remoteAddr) const
1187 {
1188     HILOGI("enter");
1189     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1190 
1191     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1192     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1193 
1194     return proxy->DisconnectAllowedProfiles(remoteAddr);
1195 }
1196 
RegisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)1197 void BluetoothHost::RegisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)
1198 {
1199     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1200 
1201     pimpl->resourceManagerObservers_.Register(observer);
1202 }
1203 
DeregisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)1204 void BluetoothHost::DeregisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)
1205 {
1206     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1207 
1208     pimpl->resourceManagerObservers_.Deregister(observer);
1209 }
1210 
SetFastScanLevel(int level)1211 int BluetoothHost::SetFastScanLevel(int level)
1212 {
1213     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1214     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1215     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1216     return proxy->SetFastScanLevel(level);
1217 }
1218 
IsBtProhibitedByEdm(void)1219 bool BluetoothHost::IsBtProhibitedByEdm(void)
1220 {
1221     constexpr const char* BLUETOOTH_EDM_KEY = "persist.edm.prohibit_bluetooth";
1222     constexpr const uint32_t PARAM_TRUE_LEN = 4; // "true" 4bytes
1223     constexpr const uint32_t PARAM_FALSE_LEN = 5; // "false" 5bytes
1224     constexpr const char* PARAM_TRUE = "true";
1225     constexpr const char* PARAM_FALSE = "false";
1226 
1227     char result[PARAM_FALSE_LEN + 1] = {0};
1228     //  Returns the number of bytes of the system parameter if the operation is successful.
1229     int len = GetParameter(BLUETOOTH_EDM_KEY, PARAM_FALSE, result, PARAM_FALSE_LEN + 1);
1230     CHECK_AND_RETURN_LOG_RET(len == PARAM_FALSE_LEN || len == PARAM_TRUE_LEN, false, "GetParameter len is invalid.");
1231 
1232     if (strncmp(result, PARAM_TRUE, PARAM_TRUE_LEN) == 0) {
1233         HILOGW("bluetooth is prohibited by EDM. You won't be able to turn on bluetooth !");
1234         return true;
1235     }
1236     return false;
1237 }
1238 
IsBluetoothSystemAbilityOn(void)1239 static bool IsBluetoothSystemAbilityOn(void)
1240 {
1241     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1242     CHECK_AND_RETURN_LOG_RET(samgrProxy != nullptr, false, "samgrProxy is nullptr");
1243     auto object = samgrProxy->CheckSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
1244     return object != nullptr;
1245 }
1246 
OnRemoveBluetoothSystemAbility()1247 void BluetoothHost::OnRemoveBluetoothSystemAbility()
1248 {
1249     // Notify the upper layer that bluetooth is disabled.
1250     bool isBluetoothSystemAbilityOn = IsBluetoothSystemAbilityOn();
1251     if (isBluetoothSystemAbilityOn) {
1252         HILOGW("Bluetooth SA is started, the hap application may be freezed by rss");
1253         // Notify profile manager bluetooth off once.
1254         BluetoothProfileManager::GetInstance().NotifyBluetoothStateChange(
1255             BTTransport::ADAPTER_BLE, BTStateID::STATE_TURN_OFF);
1256     } else if (pimpl->observerImp_ && pimpl->bleObserverImp_) {
1257         HILOGD("bluetooth_servi died and send state off to app");
1258         pimpl->observerImp_->OnStateChanged(BTTransport::ADAPTER_BREDR, BTStateID::STATE_TURN_OFF);
1259         pimpl->bleObserverImp_->OnStateChanged(BTTransport::ADAPTER_BLE, BTStateID::STATE_TURN_OFF);
1260     }
1261     if (pimpl->isFactoryReseting_.load()) {
1262         pimpl->EnableBluetoothAfterFactoryReset();
1263     }
1264     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
1265     if (pimpl->switchModule_) {
1266         pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_OFF);
1267     }
1268 }
1269 
Close(void)1270 void BluetoothHost::Close(void)
1271 {
1272     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
1273     pimpl->switchModule_ = nullptr;
1274 }
1275 
UpdateCloudBluetoothDevice(const std::vector<TrustPairDeviceParam> & cloudDevices)1276 int32_t BluetoothHost::UpdateCloudBluetoothDevice(const std::vector<TrustPairDeviceParam> &cloudDevices)
1277 {
1278     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1279     HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice enter");
1280     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1281     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1282     std::vector<BluetoothTrustPairDevice> cloudDevicesVec {};
1283     for (auto &devParam : cloudDevices) {
1284         BluetoothTrustPairDevice trustPairDevice;
1285         trustPairDevice.SetMacAddress(devParam.macAddress_);
1286         trustPairDevice.SetDeviceName(devParam.deviceName_);
1287         trustPairDevice.SetUuid(devParam.uuids_);
1288         trustPairDevice.SetBluetoothClass(devParam.bluetoothClass_);
1289         trustPairDevice.SetToken(devParam.token_);
1290         trustPairDevice.SetSecureAdvertisingInfo(devParam.secureAdvertisingInfo_);
1291         HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice add device: %{public}s",
1292             GetEncryptAddr(trustPairDevice.GetMacAddress()).c_str());
1293         cloudDevicesVec.emplace_back(trustPairDevice);
1294     }
1295     return proxy->UpdateCloudBluetoothDevice(cloudDevicesVec);
1296 }
1297 
UpdateRefusePolicy(const int32_t pid,const int64_t prohibitedSecondsTime)1298 int BluetoothHost::UpdateRefusePolicy(const int32_t pid, const int64_t prohibitedSecondsTime)
1299 {
1300     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1301     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
1302     return proxy->UpdateRefusePolicy(pid, prohibitedSecondsTime);
1303 }
1304 
GetRefusePolicyProhibitedTime()1305 int64_t BluetoothHost::GetRefusePolicyProhibitedTime()
1306 {
1307     return pimpl->refusePolicyProhibitedTime_;
1308 }
1309 
ProcessRandomDeviceIdCommand(int32_t command,std::vector<std::string> & deviceIdVec,bool & isValid)1310 int32_t BluetoothHost::ProcessRandomDeviceIdCommand(
1311     int32_t command, std::vector<std::string> &deviceIdVec, bool &isValid)
1312 {
1313     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1314     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1315     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
1316     return proxy->ProcessRandomDeviceIdCommand(command, deviceIdVec, isValid);
1317 }
1318 } // namespace Bluetooth
1319 } // namespace OHOS
1320