• 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: %{private}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: %{private}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: %{private}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,bool isAsync)442     int EnableBluetooth(bool noAutoConnect, bool isAsync) 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, isAsync);
452     }
453 
DisableBluetooth(bool isAsync)454     int DisableBluetooth(bool isAsync) 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(isAsync);
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     CHECK_AND_RETURN_LOG(observer != nullptr, "observer is null.");
624     pimpl->observers_.Register(observer);
625 }
626 
DeregisterObserver(std::shared_ptr<BluetoothHostObserver> observer)627 void BluetoothHost::DeregisterObserver(std::shared_ptr<BluetoothHostObserver> observer)
628 {
629     HILOGD("enter");
630     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
631     pimpl->observers_.Deregister(observer);
632 }
633 
EnableBt()634 int BluetoothHost::EnableBt()
635 {
636     HILOGD("enter");
637     CHECK_AND_RETURN_LOG_RET(!IsBtProhibitedByEdm(), BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited");
638     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
639     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
640 
641     return proxy->EnableBt();
642 }
643 
DisableBt(bool isAsync)644 int BluetoothHost::DisableBt(bool isAsync)
645 {
646     HILOGI("enter");
647     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
648     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
649     return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH, isAsync);
650 }
651 
PublishBtSwitchRestrictBluetoothEvent(void)652 static void PublishBtSwitchRestrictBluetoothEvent(void)
653 {
654     OHOS::AAFwk::Want want;
655     want.SetAction("usual.event.bluetooth.BT_SWITCH_RESTRICT_BLUETOOTH");
656 
657     OHOS::EventFwk::CommonEventData data;
658     data.SetWant(want);
659 
660     OHOS::EventFwk::CommonEventPublishInfo publishInfo;
661     publishInfo.SetSubscriberPermissions({"ohos.permission.ACCESS_BLUETOOTH"});
662     bool ret = OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo);
663     if (!ret) {
664         HILOGE("Publish usual.event.bluetooth.BT_SWITCH_RESTRICT_BLUETOOTH event failed");
665         return;
666     }
667 }
668 
RestrictBluetooth()669 int BluetoothHost::RestrictBluetooth()
670 {
671     HILOGI("enter");
672     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
673     PublishBtSwitchRestrictBluetoothEvent();
674     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
675     int ret =  pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH);
676     if (ret != BT_NO_ERROR) {
677         return ret;
678     }
679     ret = pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE);
680     return ret;
681 }
682 
SatelliteControl(int type,int state)683 int BluetoothHost::SatelliteControl(int type, int state)
684 {
685     HILOGI("type: %{public}d, state: %{public}d", type, state);
686     if (type == static_cast<int>(SATELLITE_CONTROL_MODE::ANTENNA)) {
687         CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
688     } else if (type == static_cast<int>(SATELLITE_CONTROL_MODE::BLUETOOTH_SWITCH)) {
689         pimpl->LoadBluetoothHostService();
690     } else if (type == static_cast<int>(SATELLITE_CONTROL_MODE::SIGNALHUB_MAC_SWITCH)) {
691         CHECK_AND_RETURN_LOG_RET(IS_BLE_ENABLED(), BT_ERR_INVALID_STATE, "ble is off.");
692     } else {
693         HILOGE("Invalid control type: %{public}d", type);
694         return BT_ERR_INVALID_PARAM;
695     }
696     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
697     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
698     return proxy->SatelliteControl(type, state);
699 }
UpdateVirtualDevice(int32_t action,const std::string & address)700 void BluetoothHost::UpdateVirtualDevice(int32_t action, const std::string &address)
701 {
702     HILOGD("enter");
703     CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off");
704     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
705     CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
706     proxy->UpdateVirtualDevice(action, address);
707 }
708 
GetBtState() const709 int BluetoothHost::GetBtState() const
710 {
711     BluetoothState state = GetBluetoothState();
712     return pimpl->ConvertBluetoothStateToBtStateID(state);
713 }
714 
GetBtState(int & state) const715 int BluetoothHost::GetBtState(int &state) const
716 {
717     state = GetBtState();
718     return BT_NO_ERROR;
719 }
720 
GetBluetoothState(void) const721 BluetoothState BluetoothHost::GetBluetoothState(void) const
722 {
723     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
724     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BluetoothState::STATE_OFF, "proxy is nullptr");
725     int state = static_cast<int>(BluetoothState::STATE_OFF);
726     proxy->GetBtState(state);
727     return static_cast<BluetoothState>(state);
728 }
729 
EnableBluetoothAfterFactoryReset(void)730 int BluetoothHost::impl::EnableBluetoothAfterFactoryReset(void)
731 {
732     HILOGI("Attempt to enable bluetooth after factory reset");
733     isFactoryReseting_ = false;
734     SetParameter("persist.bluetooth.switch_enable", "2");  // 2 means bluetooth auto enter restricted mode
735     return BT_NO_ERROR;
736 }
737 
BluetoothFactoryReset()738 int BluetoothHost::BluetoothFactoryReset()
739 {
740     HILOGI("enter");
741     constexpr const char* BLUETOOTH_FACTORY_RESET_KEY = "persist.bluetooth.factoryreset";
742     int ret = SetParameter(BLUETOOTH_FACTORY_RESET_KEY, "true");
743     CHECK_AND_RETURN_LOG_RET(ret == 0, BT_ERR_INTERNAL_ERROR, "SetParameter failed");
744 
745     pimpl->isFactoryReseting_ = true;
746 
747     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
748     if (proxy == nullptr) {
749         return pimpl->EnableBluetoothAfterFactoryReset();
750     }
751     CHECK_AND_RETURN_LOG_RET(IS_BLE_ENABLED(), BT_NO_ERROR, "bluetooth is off.");
752     return proxy->BluetoothFactoryReset();
753 }
754 
IsValidBluetoothAddr(const std::string & addr)755 bool BluetoothHost::IsValidBluetoothAddr(const std::string &addr)
756 {
757 #if defined(IOS_PLATFORM)
758     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}$");
759     return regex_match(addr, deviceIdRegex);
760 #elif defined(ANDROID_PLATFORM)
761     const std::regex deviceIdRegex("^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$");
762     return regex_match(addr, deviceIdRegex);
763 #else
764     if (addr.length() != ADDRESS_LENGTH) {
765         HILOGD("invalid address len.");
766         return false;
767     }
768 
769     for (int i = 0; i < ADDRESS_LENGTH; i++) {
770         char c = addr[i];
771         switch (i % ADDRESS_SEPARATOR_UNIT) {
772             case 0:
773             case 1:
774                 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
775                     break;
776                 }
777                 return false;
778             case ADDRESS_COLON_INDEX:
779             default:
780                 if (c == ':') {
781                     break;
782                 }
783                 return false;
784         }
785     }
786     return true;
787 #endif
788 }
789 
GetRemoteDevice(const std::string & addr,int transport) const790 BluetoothRemoteDevice BluetoothHost::GetRemoteDevice(const std::string &addr, int transport) const
791 {
792     BluetoothRemoteDevice remoteDevice(addr, transport);
793     return remoteDevice;
794 }
795 
EnableBle(bool isAsync)796 int BluetoothHost::EnableBle(bool isAsync)
797 {
798     HILOGI("enter");
799     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
800     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
801     return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH, isAsync);
802 }
803 
EnableBluetoothNoAutoConnect()804 int BluetoothHost::EnableBluetoothNoAutoConnect()
805 {
806     HILOGI("enter");
807     pimpl->switchModule_->SetNoAutoConnect(true);
808     return EnableBle();
809 }
810 
DisableBle()811 int BluetoothHost::DisableBle()
812 {
813     HILOGD("enter");
814     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
815     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
816 
817     return proxy->DisableBle();
818 }
819 
EnableBluetoothToRestrictMode(void)820 int BluetoothHost::EnableBluetoothToRestrictMode(void)
821 {
822     HILOGI("enter");
823     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
824     CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
825     return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE);
826 }
827 
IsBrEnabled() const828 bool BluetoothHost::IsBrEnabled() const
829 {
830     BluetoothState state = GetBluetoothState();
831     return state == BluetoothState::STATE_ON;
832 }
833 
IsBleEnabled() const834 bool BluetoothHost::IsBleEnabled() const
835 {
836     BluetoothState state = GetBluetoothState();
837     return (state == BluetoothState::STATE_ON ||
838         state == BluetoothState::STATE_BLE_ON ||
839         state == BluetoothState::STATE_TURNING_ON ||
840         state == BluetoothState::STATE_TURNING_OFF);
841 }
842 
GetLocalAddress(std::string & addr) const843 int BluetoothHost::GetLocalAddress(std::string &addr) const
844 {
845     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
846     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
847 
848     return proxy->GetLocalAddress(addr);
849 }
850 
GetProfileList() const851 std::vector<uint32_t> BluetoothHost::GetProfileList() const
852 {
853     HILOGD("enter");
854     std::vector<uint32_t> profileList;
855     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
856     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, profileList, "proxy is nullptr");
857 
858     profileList = proxy->GetProfileList();
859     return profileList;
860 }
861 
GetMaxNumConnectedAudioDevices() const862 int BluetoothHost::GetMaxNumConnectedAudioDevices() const
863 {
864     HILOGD("enter");
865     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
866     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
867 
868     return proxy->GetMaxNumConnectedAudioDevices();
869 }
870 
GetBtConnectionState() const871 int BluetoothHost::GetBtConnectionState() const
872 {
873     HILOGD("enter");
874     int state = static_cast<int>(BTConnectState::DISCONNECTED);
875     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), state, "bluetooth is off.");
876 
877     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
878     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, state, "proxy is nullptr");
879 
880     proxy->GetBtConnectionState(state);
881     HILOGI("state: %{public}d", state);
882     return state;
883 }
884 
GetBtConnectionState(int & state) const885 int BluetoothHost::GetBtConnectionState(int &state) const
886 {
887     HILOGD("enter");
888     state = static_cast<int>(BTConnectState::DISCONNECTED);
889     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
890 
891     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
892     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
893 
894     int ret = proxy->GetBtConnectionState(state);
895     HILOGI("state: %{public}d", state);
896     return ret;
897 }
898 
GetBtProfileConnState(uint32_t profileId,int & state) const899 int BluetoothHost::GetBtProfileConnState(uint32_t profileId, int &state) const
900 {
901     HILOGI("profileId: %{public}d", profileId);
902     state = static_cast<int>(BTConnectState::DISCONNECTED);
903     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
904 
905     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
906     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
907 
908     return proxy->GetBtProfileConnState(profileId, state);
909 }
910 
GetLocalSupportedUuids(std::vector<ParcelUuid> & uuids)911 void BluetoothHost::GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids)
912 {
913     HILOGD("enter");
914     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
915     CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
916 
917     std::vector<std::string> stringUuids;
918     proxy->GetLocalSupportedUuids(stringUuids);
919 
920     for (auto uuid : stringUuids) {
921         uuids.push_back(UUID::FromString(uuid));
922     }
923 }
924 
Start()925 bool BluetoothHost::Start()
926 {
927     // / This function only used for passthrough, so this is empty.
928     return true;
929 }
930 
Stop()931 void BluetoothHost::Stop()
932 {
933     // / This function only used for passthrough, so this is empty.
934 }
935 
GetLocalDeviceClass() const936 BluetoothDeviceClass BluetoothHost::GetLocalDeviceClass() const
937 {
938     HILOGD("enter");
939     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
940     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BluetoothDeviceClass(0), "proxy is nullptr");
941 
942     int LocalDeviceClass = proxy->GetLocalDeviceClass();
943     return BluetoothDeviceClass(LocalDeviceClass);
944 }
945 
SetLocalDeviceClass(const BluetoothDeviceClass & deviceClass)946 bool BluetoothHost::SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass)
947 {
948     HILOGD("enter");
949     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
950     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
951 
952     int cod = deviceClass.GetClassOfDevice();
953     return proxy->SetLocalDeviceClass(cod);
954 }
955 
GetLocalName() const956 std::string BluetoothHost::GetLocalName() const
957 {
958     HILOGD("enter");
959     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
960     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::string(), "proxy is nullptr");
961 
962     std::string name = INVALID_NAME;
963     proxy->GetLocalName(name);
964     return name;
965 }
966 
GetLocalName(std::string & name) const967 int BluetoothHost::GetLocalName(std::string &name) const
968 {
969     HILOGD("enter");
970     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
971     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
972 
973     return proxy->GetLocalName(name);
974 }
975 
SetLocalName(const std::string & name)976 int BluetoothHost::SetLocalName(const std::string &name)
977 {
978     HILOGD("enter");
979     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
980 
981     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
982     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
983 
984     return proxy->SetLocalName(name);
985 }
986 
GetBtScanMode(int32_t & scanMode) const987 int BluetoothHost::GetBtScanMode(int32_t &scanMode) const
988 {
989     HILOGD("enter");
990     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
991 
992     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
993     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
994 
995     return proxy->GetBtScanMode(scanMode);
996 }
997 
SetBtScanMode(int mode,int duration)998 int BluetoothHost::SetBtScanMode(int mode, int duration)
999 {
1000     HILOGD("mode: %{public}d, duration: %{public}d", mode, duration);
1001     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1002 
1003     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1004     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1005 
1006     return proxy->SetBtScanMode(mode, duration);
1007 }
1008 
GetBondableMode(int transport) const1009 int BluetoothHost::GetBondableMode(int transport) const
1010 {
1011     HILOGI("transport: %{public}d", transport);
1012     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1013     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
1014 
1015     return proxy->GetBondableMode(transport);
1016 }
1017 
SetBondableMode(int transport,int mode)1018 bool BluetoothHost::SetBondableMode(int transport, int mode)
1019 {
1020     HILOGD("transport: %{public}d, mode: %{public}d", transport, mode);
1021     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1022     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
1023 
1024     return proxy->SetBondableMode(transport, mode);
1025 }
1026 
StartBtDiscovery()1027 int BluetoothHost::StartBtDiscovery()
1028 {
1029     HILOGD("enter");
1030     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1031 
1032     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1033     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1034 
1035     return proxy->StartBtDiscovery();
1036 }
1037 
CancelBtDiscovery()1038 int BluetoothHost::CancelBtDiscovery()
1039 {
1040     HILOGD("enter");
1041     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1042 
1043     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1044     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1045 
1046     return proxy->CancelBtDiscovery();
1047 }
1048 
IsBtDiscovering(bool & isDiscovering,int transport) const1049 int32_t BluetoothHost::IsBtDiscovering(bool &isDiscovering, int transport) const
1050 {
1051     HILOGI("transport: %{public}d", transport);
1052     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1053 
1054     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1055     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr");
1056 
1057     return proxy->IsBtDiscovering(isDiscovering, transport);
1058 }
1059 
GetBtDiscoveryEndMillis() const1060 long BluetoothHost::GetBtDiscoveryEndMillis() const
1061 {
1062     HILOGD("enter");
1063     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), 0, "bluetooth is off.");
1064 
1065     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1066     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, 0, "proxy is nullptr");
1067 
1068     return proxy->GetBtDiscoveryEndMillis();
1069 }
1070 
GetPairedDevices(int transport,std::vector<BluetoothRemoteDevice> & pairedDevices) const1071 int32_t BluetoothHost::GetPairedDevices(int transport, std::vector<BluetoothRemoteDevice> &pairedDevices) const
1072 {
1073     HILOGD("transport: %{public}d", transport);
1074     if (!IS_BT_ENABLED()) {
1075         HILOGD("bluetooth is off.");
1076         return BT_ERR_INVALID_STATE;
1077     }
1078 
1079     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1080     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1081 
1082     std::vector<BluetoothRawAddress> pairedAddr;
1083     int32_t ret = proxy->GetPairedDevices(pairedAddr);
1084 
1085     for (auto it = pairedAddr.begin(); it != pairedAddr.end(); it++) {
1086         BluetoothRemoteDevice device((*it).GetAddress(), transport);
1087         pairedDevices.emplace_back(device);
1088     }
1089     return ret;
1090 }
1091 
RemovePair(const BluetoothRemoteDevice & device)1092 int32_t BluetoothHost::RemovePair(const BluetoothRemoteDevice &device)
1093 {
1094     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
1095     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device.");
1096 
1097     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1098     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1099 
1100     sptr<BluetoothRawAddress> rawAddrSptr = new BluetoothRawAddress(device.GetDeviceAddr());
1101     return proxy->RemovePair(device.GetTransportType(), rawAddrSptr);
1102 }
1103 
RemoveAllPairs()1104 bool BluetoothHost::RemoveAllPairs()
1105 {
1106     HILOGD("enter");
1107     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1108     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
1109 
1110     return proxy->RemoveAllPairs();
1111 }
1112 
RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)1113 void BluetoothHost::RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)
1114 {
1115     HILOGD("enter");
1116     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1117 
1118     pimpl->remoteObservers_.Register(observer);
1119 }
1120 
DeregisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)1121 void BluetoothHost::DeregisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)
1122 {
1123     HILOGD("enter");
1124     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1125 
1126     pimpl->remoteObservers_.Deregister(observer);
1127 }
1128 
GetBleMaxAdvertisingDataLength() const1129 int BluetoothHost::GetBleMaxAdvertisingDataLength() const
1130 {
1131     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1132     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
1133 
1134     return proxy->GetBleMaxAdvertisingDataLength();
1135 }
1136 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)1137 void BluetoothHost::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
1138 {
1139     CHECK_AND_RETURN_LOG(pimpl, "pimpl is null.");
1140 
1141     pimpl->LoadSystemAbilitySuccess(remoteObject);
1142 }
1143 
LoadSystemAbilityFail()1144 void BluetoothHost::LoadSystemAbilityFail()
1145 {
1146     CHECK_AND_RETURN_LOG(pimpl, "pimpl is null.");
1147 
1148     pimpl->LoadSystemAbilityFail();
1149 }
1150 
GetLocalProfileUuids(std::vector<std::string> & uuids)1151 int32_t BluetoothHost::GetLocalProfileUuids(std::vector<std::string> &uuids)
1152 {
1153     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INTERNAL_ERROR, "bluetooth is off.");
1154 
1155     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1156     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1157 
1158     return proxy->GetLocalProfileUuids(uuids);
1159 }
1160 
SetFastScan(bool isEnable)1161 int BluetoothHost::SetFastScan(bool isEnable)
1162 {
1163     HILOGI("isEnable: %{public}d", isEnable);
1164     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1165 
1166     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1167     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1168 
1169     return proxy->SetFastScan(isEnable);
1170 }
1171 
GetRandomAddress(const std::string & realAddr,std::string & randomAddr,uint64_t tokenId) const1172 int BluetoothHost::GetRandomAddress(const std::string &realAddr, std::string &randomAddr, uint64_t tokenId) const
1173 {
1174     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1175     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1176     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1177     return proxy->GetRandomAddress(realAddr, randomAddr, tokenId);
1178 }
1179 
ConnectAllowedProfiles(const std::string & remoteAddr) const1180 int BluetoothHost::ConnectAllowedProfiles(const std::string &remoteAddr) const
1181 {
1182     HILOGI("enter");
1183     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1184 
1185     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1186     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1187 
1188     return proxy->ConnectAllowedProfiles(remoteAddr);
1189 }
1190 
DisconnectAllowedProfiles(const std::string & remoteAddr) const1191 int BluetoothHost::DisconnectAllowedProfiles(const std::string &remoteAddr) const
1192 {
1193     HILOGI("enter");
1194     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1195 
1196     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1197     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1198 
1199     return proxy->DisconnectAllowedProfiles(remoteAddr);
1200 }
1201 
RegisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)1202 void BluetoothHost::RegisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)
1203 {
1204     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1205 
1206     pimpl->resourceManagerObservers_.Register(observer);
1207 }
1208 
DeregisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)1209 void BluetoothHost::DeregisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)
1210 {
1211     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
1212 
1213     pimpl->resourceManagerObservers_.Deregister(observer);
1214 }
1215 
SetFastScanLevel(int level)1216 int BluetoothHost::SetFastScanLevel(int level)
1217 {
1218     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1219     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1220     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1221     return proxy->SetFastScanLevel(level);
1222 }
1223 
IsBtProhibitedByEdm(void)1224 bool BluetoothHost::IsBtProhibitedByEdm(void)
1225 {
1226     constexpr const char* BLUETOOTH_EDM_KEY = "persist.edm.prohibit_bluetooth";
1227     constexpr const uint32_t PARAM_TRUE_LEN = 4; // "true" 4bytes
1228     constexpr const uint32_t PARAM_FALSE_LEN = 5; // "false" 5bytes
1229     constexpr const char* PARAM_TRUE = "true";
1230     constexpr const char* PARAM_FALSE = "false";
1231 
1232     char result[PARAM_FALSE_LEN + 1] = {0};
1233     //  Returns the number of bytes of the system parameter if the operation is successful.
1234     int len = GetParameter(BLUETOOTH_EDM_KEY, PARAM_FALSE, result, PARAM_FALSE_LEN + 1);
1235     CHECK_AND_RETURN_LOG_RET(len == PARAM_FALSE_LEN || len == PARAM_TRUE_LEN, false, "GetParameter len is invalid.");
1236 
1237     if (strncmp(result, PARAM_TRUE, PARAM_TRUE_LEN) == 0) {
1238         HILOGW("bluetooth is prohibited by EDM. You won't be able to turn on bluetooth !");
1239         return true;
1240     }
1241     return false;
1242 }
1243 
IsBluetoothSystemAbilityOn(void)1244 static bool IsBluetoothSystemAbilityOn(void)
1245 {
1246     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1247     CHECK_AND_RETURN_LOG_RET(samgrProxy != nullptr, false, "samgrProxy is nullptr");
1248     auto object = samgrProxy->CheckSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
1249     return object != nullptr;
1250 }
1251 
OnRemoveBluetoothSystemAbility()1252 void BluetoothHost::OnRemoveBluetoothSystemAbility()
1253 {
1254     // Notify the upper layer that bluetooth is disabled.
1255     bool isBluetoothSystemAbilityOn = IsBluetoothSystemAbilityOn();
1256     if (isBluetoothSystemAbilityOn) {
1257         HILOGW("Bluetooth SA is started, the hap application may be freezed by rss");
1258         // Notify profile manager bluetooth off once.
1259         BluetoothProfileManager::GetInstance().NotifyBluetoothStateChange(
1260             BTTransport::ADAPTER_BLE, BTStateID::STATE_TURN_OFF);
1261     } else if (pimpl->observerImp_ && pimpl->bleObserverImp_) {
1262         HILOGD("bluetooth_servi died and send state off to app");
1263         pimpl->observerImp_->OnStateChanged(BTTransport::ADAPTER_BREDR, BTStateID::STATE_TURN_OFF);
1264         pimpl->bleObserverImp_->OnStateChanged(BTTransport::ADAPTER_BLE, BTStateID::STATE_TURN_OFF);
1265     }
1266     if (pimpl->isFactoryReseting_.load()) {
1267         pimpl->EnableBluetoothAfterFactoryReset();
1268     }
1269     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
1270     if (pimpl->switchModule_) {
1271         pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_OFF);
1272     }
1273 }
1274 
Close(void)1275 void BluetoothHost::Close(void)
1276 {
1277     std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
1278     pimpl->switchModule_ = nullptr;
1279 }
1280 
UpdateCloudBluetoothDevice(const std::vector<TrustPairDeviceParam> & cloudDevices)1281 int32_t BluetoothHost::UpdateCloudBluetoothDevice(const std::vector<TrustPairDeviceParam> &cloudDevices)
1282 {
1283     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1284     HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice enter");
1285     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1286     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
1287     std::vector<BluetoothTrustPairDevice> cloudDevicesVec {};
1288     for (auto &devParam : cloudDevices) {
1289         BluetoothTrustPairDevice trustPairDevice;
1290         trustPairDevice.SetMacAddress(devParam.macAddress_);
1291         trustPairDevice.SetDeviceName(devParam.deviceName_);
1292         trustPairDevice.SetUuid(devParam.uuids_);
1293         trustPairDevice.SetBluetoothClass(devParam.bluetoothClass_);
1294         trustPairDevice.SetToken(devParam.token_);
1295         trustPairDevice.SetSecureAdvertisingInfo(devParam.secureAdvertisingInfo_);
1296         HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice add device: %{public}s",
1297             GetEncryptAddr(trustPairDevice.GetMacAddress()).c_str());
1298         cloudDevicesVec.emplace_back(trustPairDevice);
1299     }
1300     return proxy->UpdateCloudBluetoothDevice(cloudDevicesVec);
1301 }
1302 
UpdateRefusePolicy(const int32_t protocolType,const int32_t pid,const int64_t prohibitedSecondsTime)1303 int BluetoothHost::UpdateRefusePolicy(const int32_t protocolType,
1304     const int32_t pid, const int64_t prohibitedSecondsTime)
1305 {
1306     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1307     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1308     return proxy->UpdateRefusePolicy(protocolType, pid, prohibitedSecondsTime);
1309 }
1310 
GetRefusePolicyProhibitedTime()1311 int64_t BluetoothHost::GetRefusePolicyProhibitedTime()
1312 {
1313     return pimpl->refusePolicyProhibitedTime_;
1314 }
1315 
ProcessRandomDeviceIdCommand(int32_t command,std::vector<std::string> & deviceIdVec,bool & isValid)1316 int32_t BluetoothHost::ProcessRandomDeviceIdCommand(
1317     int32_t command, std::vector<std::string> &deviceIdVec, bool &isValid)
1318 {
1319     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
1320     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1321     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
1322     return proxy->ProcessRandomDeviceIdCommand(command, deviceIdVec, isValid);
1323 }
1324 
GetCarKeyDfxData(std::string & dfxData) const1325 int BluetoothHost::GetCarKeyDfxData(std::string &dfxData) const
1326 {
1327     HILOGI("enter");
1328     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1329     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1330     return proxy->GetCarKeyDfxData(dfxData);
1331 }
1332 
SetCarKeyCardData(const std::string & address,int32_t action)1333 int BluetoothHost::SetCarKeyCardData(const std::string &address, int32_t action)
1334 {
1335     HILOGI("enter");
1336     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1337     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1338     return proxy->SetCarKeyCardData(address, action);
1339 }
1340 
NotifyDialogResult(uint32_t dialogType,bool dialogResult)1341 int BluetoothHost::NotifyDialogResult(uint32_t dialogType, bool dialogResult)
1342 {
1343     HILOGI("enter");
1344     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1345     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1346     return proxy->NotifyDialogResult(dialogType, dialogResult);
1347 }
1348 
SetCallingPackageName(const std::string & address,const std::string & packageName)1349 int32_t BluetoothHost::SetCallingPackageName(const std::string &address, const std::string &packageName)
1350 {
1351     HILOGI("enter");
1352     sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
1353     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
1354     proxy->SetCallingPackageName(address, packageName);
1355     return BT_NO_ERROR;
1356 }
1357 } // namespace Bluetooth
1358 } // namespace OHOS
1359