195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
495489c19Sopenharmony_ci * you may not use this file except in compliance with the License.
595489c19Sopenharmony_ci * You may obtain a copy of the License at
695489c19Sopenharmony_ci *
795489c19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
895489c19Sopenharmony_ci *
995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and
1395489c19Sopenharmony_ci * limitations under the License.
1495489c19Sopenharmony_ci */
1595489c19Sopenharmony_ci
1695489c19Sopenharmony_ci#include "bluetooth_host.h"
1795489c19Sopenharmony_ci#include <memory>
1895489c19Sopenharmony_ci#include <mutex>
1995489c19Sopenharmony_ci#include <unistd.h>
2095489c19Sopenharmony_ci#include <thread>
2195489c19Sopenharmony_ci#include "bluetooth_ble_peripheral_observer_stub.h"
2295489c19Sopenharmony_ci#include "bluetooth_host_load_callback.h"
2395489c19Sopenharmony_ci#include "bluetooth_host_observer_stub.h"
2495489c19Sopenharmony_ci#include "bluetooth_host_proxy.h"
2595489c19Sopenharmony_ci#include "bluetooth_profile_manager.h"
2695489c19Sopenharmony_ci#include "bluetooth_log.h"
2795489c19Sopenharmony_ci#include "bluetooth_utils.h"
2895489c19Sopenharmony_ci#include "bluetooth_observer_list.h"
2995489c19Sopenharmony_ci#include "bluetooth_remote_device_observer_stub.h"
3095489c19Sopenharmony_ci#include "bluetooth_resource_manager_observer_stub.h"
3195489c19Sopenharmony_ci#include "iservice_registry.h"
3295489c19Sopenharmony_ci#include "parameter.h"
3395489c19Sopenharmony_ci#include "system_ability_definition.h"
3495489c19Sopenharmony_ci#include "bluetooth_switch_module.h"
3595489c19Sopenharmony_ci#include "ffrt_inner.h"
3695489c19Sopenharmony_ci#include "common_event.h"
3795489c19Sopenharmony_ci#include "common_event_data.h"
3895489c19Sopenharmony_ci#include "common_event_manager.h"
3995489c19Sopenharmony_ci
4095489c19Sopenharmony_ciusing namespace OHOS::EventFwk;
4195489c19Sopenharmony_ci
4295489c19Sopenharmony_cinamespace OHOS {
4395489c19Sopenharmony_cinamespace Bluetooth {
4495489c19Sopenharmony_cinamespace {
4595489c19Sopenharmony_ciconstexpr int32_t LOAD_SA_TIMEOUT_MS = 4000;
4695489c19Sopenharmony_ci}
4795489c19Sopenharmony_ci
4895489c19Sopenharmony_cistruct BluetoothHost::impl {
4995489c19Sopenharmony_ci    impl();
5095489c19Sopenharmony_ci    ~impl();
5195489c19Sopenharmony_ci
5295489c19Sopenharmony_ci    bool LoadBluetoothHostService();
5395489c19Sopenharmony_ci    void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
5495489c19Sopenharmony_ci    void LoadSystemAbilityFail();
5595489c19Sopenharmony_ci    int EnableBluetoothAfterFactoryReset(void);
5695489c19Sopenharmony_ci
5795489c19Sopenharmony_ci    // host observer
5895489c19Sopenharmony_ci    class BluetoothHostObserverImp;
5995489c19Sopenharmony_ci    sptr<BluetoothHostObserverImp> observerImp_ = nullptr;
6095489c19Sopenharmony_ci    sptr<BluetoothHostObserverImp> bleObserverImp_ = nullptr;
6195489c19Sopenharmony_ci
6295489c19Sopenharmony_ci    // remote device observer
6395489c19Sopenharmony_ci    class BluetoothRemoteDeviceObserverImp;
6495489c19Sopenharmony_ci    sptr<BluetoothRemoteDeviceObserverImp> remoteObserverImp_ = nullptr;
6595489c19Sopenharmony_ci
6695489c19Sopenharmony_ci    // remote device observer
6795489c19Sopenharmony_ci    class BluetoothBlePeripheralCallbackImp;
6895489c19Sopenharmony_ci    sptr<BluetoothBlePeripheralCallbackImp> bleRemoteObserverImp_ = nullptr;
6995489c19Sopenharmony_ci
7095489c19Sopenharmony_ci    // bluetooth resource manager observer
7195489c19Sopenharmony_ci    class BluetoothResourceManagerObserverImp;
7295489c19Sopenharmony_ci    sptr<BluetoothResourceManagerObserverImp> resourceManagerObserverImp_ = nullptr;
7395489c19Sopenharmony_ci
7495489c19Sopenharmony_ci    // user regist observers
7595489c19Sopenharmony_ci    BluetoothObserverList<BluetoothHostObserver> observers_;
7695489c19Sopenharmony_ci
7795489c19Sopenharmony_ci    // user regist remote observers
7895489c19Sopenharmony_ci    BluetoothObserverList<BluetoothRemoteDeviceObserver> remoteObservers_;
7995489c19Sopenharmony_ci
8095489c19Sopenharmony_ci    // user regist resource manager observers
8195489c19Sopenharmony_ci    BluetoothObserverList<BluetoothResourceManagerObserver> resourceManagerObservers_;
8295489c19Sopenharmony_ci
8395489c19Sopenharmony_ci    void SyncRandomAddrToService(void);
8495489c19Sopenharmony_ci
8595489c19Sopenharmony_ci    std::mutex proxyMutex_;
8695489c19Sopenharmony_ci    std::string stagingRealAddr_;
8795489c19Sopenharmony_ci    std::string stagingRandomAddr_;
8895489c19Sopenharmony_ci    int32_t profileRegisterId = 0;
8995489c19Sopenharmony_ci    std::atomic_bool isFactoryReseting_ { false };
9095489c19Sopenharmony_ci
9195489c19Sopenharmony_ci    class BluetoothSwitchAction;
9295489c19Sopenharmony_ci    std::mutex switchModuleMutex_ {};  // used for serial execute enableBluetoothToRestrictMode.
9395489c19Sopenharmony_ci    std::shared_ptr<BluetoothSwitchModule> switchModule_ { nullptr };
9495489c19Sopenharmony_ci
9595489c19Sopenharmony_ciprivate:
9695489c19Sopenharmony_ci    SaManagerStatus saManagerStatus_ = SaManagerStatus::WAIT_NOTIFY;
9795489c19Sopenharmony_ci    std::condition_variable proxyConVar_;
9895489c19Sopenharmony_ci};
9995489c19Sopenharmony_ci
10095489c19Sopenharmony_ciclass BluetoothHost::impl::BluetoothHostObserverImp : public BluetoothHostObserverStub {
10195489c19Sopenharmony_cipublic:
10295489c19Sopenharmony_ci    explicit BluetoothHostObserverImp(BluetoothHost::impl &host) : host_(host){};
10395489c19Sopenharmony_ci    ~BluetoothHostObserverImp() override{};
10495489c19Sopenharmony_ci
10595489c19Sopenharmony_ci    void Register(std::shared_ptr<BluetoothHostObserver> &observer)
10695489c19Sopenharmony_ci    {
10795489c19Sopenharmony_ci        host_.observers_.Register(observer);
10895489c19Sopenharmony_ci    }
10995489c19Sopenharmony_ci
11095489c19Sopenharmony_ci    void Deregister(std::shared_ptr<BluetoothHostObserver> &observer)
11195489c19Sopenharmony_ci    {
11295489c19Sopenharmony_ci        host_.observers_.Deregister(observer);
11395489c19Sopenharmony_ci    }
11495489c19Sopenharmony_ci
11595489c19Sopenharmony_ci    void OnStateChanged(int32_t transport, int32_t status) override
11695489c19Sopenharmony_ci    {
11795489c19Sopenharmony_ci        if (status == BTStateID::STATE_TURN_ON) {
11895489c19Sopenharmony_ci            host_.SyncRandomAddrToService();
11995489c19Sopenharmony_ci        }
12095489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(!isNeedInterceptSwitchStatus(transport, status), "No Need transform same status");
12195489c19Sopenharmony_ci        BluetoothProfileManager::GetInstance().NotifyBluetoothStateChange(transport, status);
12295489c19Sopenharmony_ci        host_.observers_.ForEach([transport, status](std::shared_ptr<BluetoothHostObserver> observer) {
12395489c19Sopenharmony_ci            observer->OnStateChanged(transport, status);
12495489c19Sopenharmony_ci        });
12595489c19Sopenharmony_ci    }
12695489c19Sopenharmony_ci
12795489c19Sopenharmony_ci    void OnBluetoothStateChanged(int32_t state) override
12895489c19Sopenharmony_ci    {
12995489c19Sopenharmony_ci        std::lock_guard<std::mutex> lock(host_.switchModuleMutex_);
13095489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(host_.switchModule_, "switchModule is nullptr");
13195489c19Sopenharmony_ci        if (state == bluetooth::BluetoothSwitchState::STATE_ON) {
13295489c19Sopenharmony_ci            host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_ON);
13395489c19Sopenharmony_ci        }
13495489c19Sopenharmony_ci        if (state == bluetooth::BluetoothSwitchState::STATE_OFF) {
13595489c19Sopenharmony_ci            host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_OFF);
13695489c19Sopenharmony_ci        }
13795489c19Sopenharmony_ci        if (state == bluetooth::BluetoothSwitchState::STATE_HALF) {
13895489c19Sopenharmony_ci            host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_HALF);
13995489c19Sopenharmony_ci        }
14095489c19Sopenharmony_ci    }
14195489c19Sopenharmony_ci
14295489c19Sopenharmony_ci    void OnDiscoveryStateChanged(int32_t status) override
14395489c19Sopenharmony_ci    {
14495489c19Sopenharmony_ci        HILOGD("enter, status: %{public}d", status);
14595489c19Sopenharmony_ci        host_.observers_.ForEach(
14695489c19Sopenharmony_ci            [status](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDiscoveryStateChanged(status); });
14795489c19Sopenharmony_ci    }
14895489c19Sopenharmony_ci
14995489c19Sopenharmony_ci    void OnDiscoveryResult(
15095489c19Sopenharmony_ci        const BluetoothRawAddress &device, int rssi, const std::string deviceName, int deviceClass) override
15195489c19Sopenharmony_ci    {
15295489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
15395489c19Sopenharmony_ci        host_.observers_.ForEach([remoteDevice, rssi, deviceName, deviceClass](
15495489c19Sopenharmony_ci            std::shared_ptr<BluetoothHostObserver> observer) {
15595489c19Sopenharmony_ci            observer->OnDiscoveryResult(remoteDevice, rssi, deviceName, deviceClass);
15695489c19Sopenharmony_ci        });
15795489c19Sopenharmony_ci    }
15895489c19Sopenharmony_ci
15995489c19Sopenharmony_ci    void OnPairRequested(const int32_t transport, const BluetoothRawAddress &device) override
16095489c19Sopenharmony_ci    {
16195489c19Sopenharmony_ci        HILOGI("enter, transport: %{public}d, device: %{public}s",
16295489c19Sopenharmony_ci            transport, GET_ENCRYPT_RAW_ADDR(device));
16395489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
16495489c19Sopenharmony_ci        host_.observers_.ForEach([remoteDevice](std::shared_ptr<BluetoothHostObserver> observer) {
16595489c19Sopenharmony_ci            observer->OnPairRequested(remoteDevice);
16695489c19Sopenharmony_ci        });
16795489c19Sopenharmony_ci    }
16895489c19Sopenharmony_ci
16995489c19Sopenharmony_ci    void OnPairConfirmed(const int32_t transport, const BluetoothRawAddress &device, int reqType, int number) override
17095489c19Sopenharmony_ci    {
17195489c19Sopenharmony_ci        HILOGI("enter, transport: %{public}d, device: %{public}s, reqType: %{public}d, number: %{public}d",
17295489c19Sopenharmony_ci            transport, GET_ENCRYPT_RAW_ADDR(device), reqType, number);
17395489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
17495489c19Sopenharmony_ci        host_.observers_.ForEach([remoteDevice, reqType, number](std::shared_ptr<BluetoothHostObserver> observer) {
17595489c19Sopenharmony_ci            observer->OnPairConfirmed(remoteDevice, reqType, number);
17695489c19Sopenharmony_ci        });
17795489c19Sopenharmony_ci    }
17895489c19Sopenharmony_ci
17995489c19Sopenharmony_ci    void OnScanModeChanged(int mode) override
18095489c19Sopenharmony_ci    {
18195489c19Sopenharmony_ci        HILOGI("enter, mode: %{public}d", mode);
18295489c19Sopenharmony_ci        host_.observers_.ForEach(
18395489c19Sopenharmony_ci            [mode](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnScanModeChanged(mode); });
18495489c19Sopenharmony_ci    }
18595489c19Sopenharmony_ci
18695489c19Sopenharmony_ci    void OnDeviceNameChanged(const std::string &deviceName) override
18795489c19Sopenharmony_ci    {
18895489c19Sopenharmony_ci        HILOGI("enter, deviceName: %{public}s", deviceName.c_str());
18995489c19Sopenharmony_ci        host_.observers_.ForEach([deviceName](std::shared_ptr<BluetoothHostObserver> observer) {
19095489c19Sopenharmony_ci            observer->OnDeviceNameChanged(deviceName);
19195489c19Sopenharmony_ci        });
19295489c19Sopenharmony_ci    }
19395489c19Sopenharmony_ci
19495489c19Sopenharmony_ci    void OnDeviceAddrChanged(const std::string &address) override
19595489c19Sopenharmony_ci    {
19695489c19Sopenharmony_ci        HILOGD("enter");
19795489c19Sopenharmony_ci        host_.observers_.ForEach(
19895489c19Sopenharmony_ci            [address](std::shared_ptr<BluetoothHostObserver> observer) { observer->OnDeviceAddrChanged(address); });
19995489c19Sopenharmony_ci    }
20095489c19Sopenharmony_ci
20195489c19Sopenharmony_ciprivate:
20295489c19Sopenharmony_ci    bool isNeedInterceptSwitchStatus(int32_t transport, int32_t status)
20395489c19Sopenharmony_ci    {
20495489c19Sopenharmony_ci        bool isBluetoothSeriviceOn = BluetoothProfileManager::GetInstance().IsBluetoothServiceOn();
20595489c19Sopenharmony_ci        if (status == BTStateID::STATE_TURN_OFF) {
20695489c19Sopenharmony_ci            if (transport == BTTransport::ADAPTER_BLE &&
20795489c19Sopenharmony_ci                preBleState_ == BTStateID::STATE_TURN_OFF && !isBluetoothSeriviceOn) {
20895489c19Sopenharmony_ci                return true;
20995489c19Sopenharmony_ci            }
21095489c19Sopenharmony_ci            if (transport == BTTransport::ADAPTER_BREDR &&
21195489c19Sopenharmony_ci                preBrState_ == BTStateID::STATE_TURN_OFF && !isBluetoothSeriviceOn) {
21295489c19Sopenharmony_ci                return true;
21395489c19Sopenharmony_ci            }
21495489c19Sopenharmony_ci        }
21595489c19Sopenharmony_ci        if (transport == BTTransport::ADAPTER_BREDR) {
21695489c19Sopenharmony_ci            preBrState_ = status;
21795489c19Sopenharmony_ci        } else {
21895489c19Sopenharmony_ci            preBleState_ = status;
21995489c19Sopenharmony_ci        }
22095489c19Sopenharmony_ci        return false;
22195489c19Sopenharmony_ci    }
22295489c19Sopenharmony_ci    BluetoothHost::impl &host_;
22395489c19Sopenharmony_ci    const int32_t INVALID_STATE = -1;
22495489c19Sopenharmony_ci    int32_t preBrState_ = INVALID_STATE;
22595489c19Sopenharmony_ci    int32_t preBleState_ = INVALID_STATE;
22695489c19Sopenharmony_ci    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHostObserverImp);
22795489c19Sopenharmony_ci};
22895489c19Sopenharmony_ci
22995489c19Sopenharmony_ciclass BluetoothHost::impl::BluetoothRemoteDeviceObserverImp : public BluetoothRemoteDeviceObserverstub {
23095489c19Sopenharmony_cipublic:
23195489c19Sopenharmony_ci    explicit BluetoothRemoteDeviceObserverImp(BluetoothHost::impl &host) : host_(host){};
23295489c19Sopenharmony_ci    ~BluetoothRemoteDeviceObserverImp() override = default;
23395489c19Sopenharmony_ci
23495489c19Sopenharmony_ci    void OnAclStateChanged(const BluetoothRawAddress &device, int32_t state, uint32_t reason) override
23595489c19Sopenharmony_ci    {
23695489c19Sopenharmony_ci        HILOGD("enter, device: %{public}s, state: %{public}d, reason: %{public}u",
23795489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), state, reason);
23895489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
23995489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
24095489c19Sopenharmony_ci            [remoteDevice, state, reason](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
24195489c19Sopenharmony_ci                observer->OnAclStateChanged(remoteDevice, state, reason);
24295489c19Sopenharmony_ci            });
24395489c19Sopenharmony_ci    }
24495489c19Sopenharmony_ci
24595489c19Sopenharmony_ci    void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device,
24695489c19Sopenharmony_ci        int32_t status, int32_t cause) override
24795489c19Sopenharmony_ci    {
24895489c19Sopenharmony_ci        HILOGD("enter, transport: %{public}d, device: %{public}s, status: %{public}d, cause: %{public}d",
24995489c19Sopenharmony_ci            transport, GET_ENCRYPT_RAW_ADDR(device), status, cause);
25095489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
25195489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
25295489c19Sopenharmony_ci            [remoteDevice, status, cause](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
25395489c19Sopenharmony_ci                observer->OnPairStatusChanged(remoteDevice, status, cause);
25495489c19Sopenharmony_ci            });
25595489c19Sopenharmony_ci    }
25695489c19Sopenharmony_ci
25795489c19Sopenharmony_ci    void OnRemoteUuidChanged(const BluetoothRawAddress &device, const std::vector<bluetooth::Uuid> uuids) override
25895489c19Sopenharmony_ci    {
25995489c19Sopenharmony_ci        HILOGD("enter, device: %{public}s", GET_ENCRYPT_RAW_ADDR(device));
26095489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
26195489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
26295489c19Sopenharmony_ci            [remoteDevice, uuids](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
26395489c19Sopenharmony_ci                std::vector<ParcelUuid> parcelUuids;
26495489c19Sopenharmony_ci                for (auto &uuid : uuids) {
26595489c19Sopenharmony_ci                    ParcelUuid parcelUuid = UUID::ConvertFrom128Bits(uuid.ConvertTo128Bits());
26695489c19Sopenharmony_ci                    parcelUuids.push_back(parcelUuid);
26795489c19Sopenharmony_ci                }
26895489c19Sopenharmony_ci                observer->OnRemoteUuidChanged(remoteDevice, parcelUuids);
26995489c19Sopenharmony_ci            });
27095489c19Sopenharmony_ci    }
27195489c19Sopenharmony_ci
27295489c19Sopenharmony_ci    void OnRemoteNameChanged(const BluetoothRawAddress &device, const std::string deviceName) override
27395489c19Sopenharmony_ci    {
27495489c19Sopenharmony_ci        HILOGD("enter, device: %{public}s, deviceName: %{public}s",
27595489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), deviceName.c_str());
27695489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
27795489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
27895489c19Sopenharmony_ci            [remoteDevice, deviceName](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
27995489c19Sopenharmony_ci                observer->OnRemoteNameChanged(remoteDevice, deviceName);
28095489c19Sopenharmony_ci            });
28195489c19Sopenharmony_ci    }
28295489c19Sopenharmony_ci
28395489c19Sopenharmony_ci    void OnRemoteAliasChanged(const BluetoothRawAddress &device, const std::string alias) override
28495489c19Sopenharmony_ci    {
28595489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s, alias: %{public}s",
28695489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), alias.c_str());
28795489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
28895489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
28995489c19Sopenharmony_ci            [remoteDevice, alias](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
29095489c19Sopenharmony_ci                observer->OnRemoteAliasChanged(remoteDevice, alias);
29195489c19Sopenharmony_ci            });
29295489c19Sopenharmony_ci    }
29395489c19Sopenharmony_ci
29495489c19Sopenharmony_ci    void OnRemoteCodChanged(const BluetoothRawAddress &device, int32_t cod) override
29595489c19Sopenharmony_ci    {
29695489c19Sopenharmony_ci        HILOGD("enter, device: %{public}s, cod: %{public}d", GET_ENCRYPT_RAW_ADDR(device), cod);
29795489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
29895489c19Sopenharmony_ci        BluetoothDeviceClass deviceClass(cod);
29995489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
30095489c19Sopenharmony_ci            [remoteDevice, deviceClass](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
30195489c19Sopenharmony_ci                observer->OnRemoteCodChanged(remoteDevice, deviceClass);
30295489c19Sopenharmony_ci            });
30395489c19Sopenharmony_ci    }
30495489c19Sopenharmony_ci
30595489c19Sopenharmony_ci    void OnRemoteBatteryChanged(const BluetoothRawAddress &device, const BluetoothBatteryInfo &batteryInfo) override
30695489c19Sopenharmony_ci    {
30795489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
30895489c19Sopenharmony_ci        DeviceBatteryInfo info;
30995489c19Sopenharmony_ci        info.deviceId_ = device.GetAddress();
31095489c19Sopenharmony_ci        info.batteryLevel_ = batteryInfo.batteryLevel_;
31195489c19Sopenharmony_ci        info.leftEarBatteryLevel_ = batteryInfo.leftEarBatteryLevel_;
31295489c19Sopenharmony_ci        info.leftEarChargeState_ = static_cast<DeviceChargeState>(batteryInfo.leftEarChargeState_);
31395489c19Sopenharmony_ci        info.rightEarBatteryLevel_ = batteryInfo.rightEarBatteryLevel_;
31495489c19Sopenharmony_ci        info.rightEarChargeState_ = static_cast<DeviceChargeState>(batteryInfo.rightEarChargeState_);
31595489c19Sopenharmony_ci        info.boxBatteryLevel_ = batteryInfo.boxBatteryLevel_;
31695489c19Sopenharmony_ci        info.boxChargeState_ = static_cast<DeviceChargeState>(batteryInfo.boxChargeState_);
31795489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
31895489c19Sopenharmony_ci            [remoteDevice, info](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
31995489c19Sopenharmony_ci                observer->OnRemoteBatteryChanged(remoteDevice, info);
32095489c19Sopenharmony_ci            });
32195489c19Sopenharmony_ci    }
32295489c19Sopenharmony_ci
32395489c19Sopenharmony_ci    void OnRemoteDeviceCommonInfoReport(const BluetoothRawAddress &device, const std::vector<uint8_t> &value) override
32495489c19Sopenharmony_ci    {
32595489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BREDR);
32695489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
32795489c19Sopenharmony_ci            [remoteDevice, value](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
32895489c19Sopenharmony_ci                observer->OnRemoteDeviceCommonInfoReport(remoteDevice, value);
32995489c19Sopenharmony_ci            });
33095489c19Sopenharmony_ci    }
33195489c19Sopenharmony_ci
33295489c19Sopenharmony_ciprivate:
33395489c19Sopenharmony_ci    BluetoothHost::impl &host_;
33495489c19Sopenharmony_ci    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteDeviceObserverImp);
33595489c19Sopenharmony_ci};
33695489c19Sopenharmony_ci
33795489c19Sopenharmony_ciclass BluetoothHost::impl::BluetoothBlePeripheralCallbackImp : public BluetoothBlePeripheralObserverStub {
33895489c19Sopenharmony_cipublic:
33995489c19Sopenharmony_ci    explicit BluetoothBlePeripheralCallbackImp(BluetoothHost::impl &host) : host_(host){};
34095489c19Sopenharmony_ci    ~BluetoothBlePeripheralCallbackImp() override = default;
34195489c19Sopenharmony_ci
34295489c19Sopenharmony_ci    void OnAclStateChanged(const BluetoothRawAddress &device, int state, unsigned int reason) override
34395489c19Sopenharmony_ci    {
34495489c19Sopenharmony_ci        HILOGD("enter, device: %{public}s, state: %{public}d, reason: %{public}u",
34595489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), state, reason);
34695489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BLE);
34795489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
34895489c19Sopenharmony_ci            [remoteDevice, state, reason](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
34995489c19Sopenharmony_ci                observer->OnAclStateChanged(remoteDevice, state, reason);
35095489c19Sopenharmony_ci            });
35195489c19Sopenharmony_ci    }
35295489c19Sopenharmony_ci
35395489c19Sopenharmony_ci    void OnPairStatusChanged(const int32_t transport, const BluetoothRawAddress &device, int status, int cause) override
35495489c19Sopenharmony_ci    {
35595489c19Sopenharmony_ci        HILOGI("enter, transport: %{public}d, device: %{public}s, status: %{public}d, cause: %{public}d",
35695489c19Sopenharmony_ci            transport, GET_ENCRYPT_RAW_ADDR(device), status, cause);
35795489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), transport);
35895489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
35995489c19Sopenharmony_ci            [remoteDevice, status, cause](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
36095489c19Sopenharmony_ci                observer->OnPairStatusChanged(remoteDevice, status, cause);
36195489c19Sopenharmony_ci            });
36295489c19Sopenharmony_ci    }
36395489c19Sopenharmony_ci
36495489c19Sopenharmony_ci    void OnReadRemoteRssiEvent(const BluetoothRawAddress &device, int rssi, int status) override
36595489c19Sopenharmony_ci    {
36695489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s, rssi: %{public}d, status: %{public}d",
36795489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), rssi, status);
36895489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), BTTransport::ADAPTER_BLE);
36995489c19Sopenharmony_ci        host_.remoteObservers_.ForEach(
37095489c19Sopenharmony_ci            [remoteDevice, rssi, status](std::shared_ptr<BluetoothRemoteDeviceObserver> observer) {
37195489c19Sopenharmony_ci                observer->OnReadRemoteRssiEvent(remoteDevice, rssi, status);
37295489c19Sopenharmony_ci            });
37395489c19Sopenharmony_ci    }
37495489c19Sopenharmony_ci
37595489c19Sopenharmony_ciprivate:
37695489c19Sopenharmony_ci    BluetoothHost::impl &host_;
37795489c19Sopenharmony_ci    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBlePeripheralCallbackImp);
37895489c19Sopenharmony_ci};
37995489c19Sopenharmony_ci
38095489c19Sopenharmony_ciclass BluetoothHost::impl::BluetoothResourceManagerObserverImp : public BluetoothResourceManagerObserverStub {
38195489c19Sopenharmony_cipublic:
38295489c19Sopenharmony_ci    explicit BluetoothResourceManagerObserverImp(BluetoothHost::impl &host) : host_(host){};
38395489c19Sopenharmony_ci    ~BluetoothResourceManagerObserverImp() override = default;
38495489c19Sopenharmony_ci
38595489c19Sopenharmony_ci    void OnSensingStateChanged(uint8_t eventId, const BluetoothSensingInfo &info) override
38695489c19Sopenharmony_ci    {
38795489c19Sopenharmony_ci        HILOGD("enter, eventId: %{public}d", eventId);
38895489c19Sopenharmony_ci        SensingInfo sensingInfo;
38995489c19Sopenharmony_ci        sensingInfo.addr_ = info.addr_;
39095489c19Sopenharmony_ci        sensingInfo.uuid_ = info.uuid_;
39195489c19Sopenharmony_ci        sensingInfo.resourceId_ = info.resourceId_;
39295489c19Sopenharmony_ci        sensingInfo.pkgName_ = info.pkgName_;
39395489c19Sopenharmony_ci        sensingInfo.isServer_ = info.isServer_;
39495489c19Sopenharmony_ci        sensingInfo.interval_ = info.interval_;
39595489c19Sopenharmony_ci        host_.resourceManagerObservers_.ForEach(
39695489c19Sopenharmony_ci            [eventId, sensingInfo](std::shared_ptr<BluetoothResourceManagerObserver> observer) {
39795489c19Sopenharmony_ci                observer->OnSensingStateChanged(eventId, sensingInfo);
39895489c19Sopenharmony_ci            });
39995489c19Sopenharmony_ci    }
40095489c19Sopenharmony_ci
40195489c19Sopenharmony_ci    void OnBluetoothResourceDecision(uint8_t eventId, const BluetoothSensingInfo &info, uint32_t &result) override
40295489c19Sopenharmony_ci    {
40395489c19Sopenharmony_ci        HILOGD("enter, eventId: %{public}d, result: %{public}d", eventId, result);
40495489c19Sopenharmony_ci        SensingInfo sensingInfo;
40595489c19Sopenharmony_ci        sensingInfo.addr_ = info.addr_;
40695489c19Sopenharmony_ci        sensingInfo.uuid_ = info.uuid_;
40795489c19Sopenharmony_ci        sensingInfo.resourceId_ = info.resourceId_;
40895489c19Sopenharmony_ci        sensingInfo.pkgName_ = info.pkgName_;
40995489c19Sopenharmony_ci        sensingInfo.isServer_ = info.isServer_;
41095489c19Sopenharmony_ci        sensingInfo.interval_ = info.interval_;
41195489c19Sopenharmony_ci        host_.resourceManagerObservers_.ForEach(
41295489c19Sopenharmony_ci            [eventId, sensingInfo, &result](std::shared_ptr<BluetoothResourceManagerObserver> observer) {
41395489c19Sopenharmony_ci                observer->OnBluetoothResourceDecision(eventId, sensingInfo, result);
41495489c19Sopenharmony_ci            });
41595489c19Sopenharmony_ci    }
41695489c19Sopenharmony_ci
41795489c19Sopenharmony_ciprivate:
41895489c19Sopenharmony_ci    BluetoothHost::impl &host_;
41995489c19Sopenharmony_ci    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothResourceManagerObserverImp);
42095489c19Sopenharmony_ci};
42195489c19Sopenharmony_ci
42295489c19Sopenharmony_ciclass BluetoothHost::impl::BluetoothSwitchAction : public IBluetoothSwitchAction {
42395489c19Sopenharmony_cipublic:
42495489c19Sopenharmony_ci    BluetoothSwitchAction() = default;
42595489c19Sopenharmony_ci    ~BluetoothSwitchAction() override = default;
42695489c19Sopenharmony_ci
42795489c19Sopenharmony_ci    int EnableBluetooth(void) override
42895489c19Sopenharmony_ci    {
42995489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(!BluetoothHost::GetDefaultHost().IsBtProhibitedByEdm(),
43095489c19Sopenharmony_ci            BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited !");
43195489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(BluetoothHost::GetDefaultHost().pimpl->LoadBluetoothHostService(),
43295489c19Sopenharmony_ci            BT_ERR_INTERNAL_ERROR, "pimpl is null or load bluetooth service failed.");
43395489c19Sopenharmony_ci
43495489c19Sopenharmony_ci        sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
43595489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
43695489c19Sopenharmony_ci        return proxy->EnableBle();
43795489c19Sopenharmony_ci    }
43895489c19Sopenharmony_ci
43995489c19Sopenharmony_ci    int DisableBluetooth(void) override
44095489c19Sopenharmony_ci    {
44195489c19Sopenharmony_ci        sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
44295489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
44395489c19Sopenharmony_ci        return proxy->DisableBt();
44495489c19Sopenharmony_ci    }
44595489c19Sopenharmony_ci
44695489c19Sopenharmony_ci    int EnableBluetoothToRestrictMode(void) override
44795489c19Sopenharmony_ci    {
44895489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(!BluetoothHost::GetDefaultHost().IsBtProhibitedByEdm(),
44995489c19Sopenharmony_ci            BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited !");
45095489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(BluetoothHost::GetDefaultHost().pimpl->LoadBluetoothHostService(),
45195489c19Sopenharmony_ci            BT_ERR_INTERNAL_ERROR, "pimpl is null or load bluetooth service failed.");
45295489c19Sopenharmony_ci
45395489c19Sopenharmony_ci        sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
45495489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
45595489c19Sopenharmony_ci        return proxy->EnableBluetoothToRestrictMode();
45695489c19Sopenharmony_ci    }
45795489c19Sopenharmony_ci};
45895489c19Sopenharmony_ci
45995489c19Sopenharmony_ciBluetoothHost::impl::impl()
46095489c19Sopenharmony_ci{
46195489c19Sopenharmony_ci    observerImp_ = new BluetoothHostObserverImp(*this);
46295489c19Sopenharmony_ci    remoteObserverImp_ = new BluetoothRemoteDeviceObserverImp(*this);
46395489c19Sopenharmony_ci    bleRemoteObserverImp_ = new BluetoothBlePeripheralCallbackImp(*this);
46495489c19Sopenharmony_ci    bleObserverImp_ = new BluetoothHostObserverImp(*this);
46595489c19Sopenharmony_ci    resourceManagerObserverImp_ = new BluetoothResourceManagerObserverImp(*this);
46695489c19Sopenharmony_ci
46795489c19Sopenharmony_ci    auto switchActionPtr = std::make_unique<BluetoothSwitchAction>();
46895489c19Sopenharmony_ci    switchModule_ = std::make_shared<BluetoothSwitchModule>(std::move(switchActionPtr));
46995489c19Sopenharmony_ci
47095489c19Sopenharmony_ci    profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(BLUETOOTH_HOST,
47195489c19Sopenharmony_ci        [this](sptr<IRemoteObject> remote) {
47295489c19Sopenharmony_ci        sptr<IBluetoothHost> proxy = iface_cast<IBluetoothHost>(remote);
47395489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
47495489c19Sopenharmony_ci        proxy->RegisterObserver(observerImp_);
47595489c19Sopenharmony_ci        proxy->RegisterBleAdapterObserver(bleObserverImp_);
47695489c19Sopenharmony_ci        proxy->RegisterRemoteDeviceObserver(remoteObserverImp_);
47795489c19Sopenharmony_ci        proxy->RegisterBlePeripheralCallback(bleRemoteObserverImp_);
47895489c19Sopenharmony_ci        proxy->RegisterBtResourceManagerObserver(resourceManagerObserverImp_);
47995489c19Sopenharmony_ci    });
48095489c19Sopenharmony_ci}
48195489c19Sopenharmony_ci
48295489c19Sopenharmony_ciBluetoothHost::impl::~impl()
48395489c19Sopenharmony_ci{
48495489c19Sopenharmony_ci    HILOGI("starts");
48595489c19Sopenharmony_ci    BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
48695489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
48795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
48895489c19Sopenharmony_ci    proxy->DeregisterObserver(observerImp_);
48995489c19Sopenharmony_ci    proxy->DeregisterBleAdapterObserver(bleObserverImp_);
49095489c19Sopenharmony_ci    proxy->DeregisterRemoteDeviceObserver(remoteObserverImp_);
49195489c19Sopenharmony_ci    proxy->DeregisterBlePeripheralCallback(bleRemoteObserverImp_);
49295489c19Sopenharmony_ci    proxy->DeregisterBtResourceManagerObserver(resourceManagerObserverImp_);
49395489c19Sopenharmony_ci}
49495489c19Sopenharmony_ci
49595489c19Sopenharmony_cibool BluetoothHost::impl::LoadBluetoothHostService()
49695489c19Sopenharmony_ci{
49795489c19Sopenharmony_ci    std::unique_lock<std::mutex> lock(proxyMutex_);
49895489c19Sopenharmony_ci    auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49995489c19Sopenharmony_ci    if (samgrProxy == nullptr) {
50095489c19Sopenharmony_ci        HILOGE("samgrProxy is nullptr.");
50195489c19Sopenharmony_ci        return false;
50295489c19Sopenharmony_ci    }
50395489c19Sopenharmony_ci    sptr<IRemoteObject> hostRemote = BluetoothProfileManager::GetInstance().GetProfileRemote(BLUETOOTH_HOST);
50495489c19Sopenharmony_ci    //当蓝牙服务已经起来的时候。这时的hostRemote不为空, 不需要进行后续的从sa拉起蓝牙服务的动作
50595489c19Sopenharmony_ci    if (hostRemote != nullptr) {
50695489c19Sopenharmony_ci        return true;
50795489c19Sopenharmony_ci    }
50895489c19Sopenharmony_ci
50995489c19Sopenharmony_ci    sptr<BluetoothHostLoadCallBack> loadCallback = new BluetoothHostLoadCallBack();
51095489c19Sopenharmony_ci    if (loadCallback == nullptr) {
51195489c19Sopenharmony_ci        HILOGE("loadCallback is nullptr.");
51295489c19Sopenharmony_ci        return false;
51395489c19Sopenharmony_ci    }
51495489c19Sopenharmony_ci    int32_t ret = samgrProxy->LoadSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID, loadCallback);
51595489c19Sopenharmony_ci    if (ret != ERR_OK) {
51695489c19Sopenharmony_ci        HILOGE("Failed to load bluetooth systemAbility");
51795489c19Sopenharmony_ci        return false;
51895489c19Sopenharmony_ci    }
51995489c19Sopenharmony_ci    auto waitStatus = proxyConVar_.wait_for(
52095489c19Sopenharmony_ci        lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), [this]() {
52195489c19Sopenharmony_ci            HILOGI("bluetooth_service load systemAbility finished");
52295489c19Sopenharmony_ci            sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
52395489c19Sopenharmony_ci            return proxy != nullptr || saManagerStatus_ == SaManagerStatus::LOAD_FAIL;
52495489c19Sopenharmony_ci        });
52595489c19Sopenharmony_ci    if (!waitStatus) {
52695489c19Sopenharmony_ci        HILOGE("load bluetooth systemAbility timeout");
52795489c19Sopenharmony_ci        return false;
52895489c19Sopenharmony_ci    }
52995489c19Sopenharmony_ci    if (saManagerStatus_ == SaManagerStatus::LOAD_FAIL) {
53095489c19Sopenharmony_ci        HILOGE("load bluetooth_service fail");
53195489c19Sopenharmony_ci        saManagerStatus_ = SaManagerStatus::WAIT_NOTIFY;
53295489c19Sopenharmony_ci        return false;
53395489c19Sopenharmony_ci    }
53495489c19Sopenharmony_ci    saManagerStatus_ = SaManagerStatus::WAIT_NOTIFY;
53595489c19Sopenharmony_ci    return true;
53695489c19Sopenharmony_ci}
53795489c19Sopenharmony_ci
53895489c19Sopenharmony_civoid BluetoothHost::impl::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
53995489c19Sopenharmony_ci{
54095489c19Sopenharmony_ci    HILOGI("LoadSystemAbilitySuccess FinishStart SA");
54195489c19Sopenharmony_ci    saManagerStatus_ = SaManagerStatus::LOAD_SUCCESS;
54295489c19Sopenharmony_ci    proxyConVar_.notify_one();
54395489c19Sopenharmony_ci}
54495489c19Sopenharmony_ci
54595489c19Sopenharmony_civoid BluetoothHost::impl::LoadSystemAbilityFail()
54695489c19Sopenharmony_ci{
54795489c19Sopenharmony_ci    HILOGI("LoadSystemAbilityFail FinishStart SA");
54895489c19Sopenharmony_ci    saManagerStatus_ = SaManagerStatus::LOAD_FAIL;
54995489c19Sopenharmony_ci    proxyConVar_.notify_one();
55095489c19Sopenharmony_ci}
55195489c19Sopenharmony_ci
55295489c19Sopenharmony_civoid BluetoothHost::impl::SyncRandomAddrToService(void)
55395489c19Sopenharmony_ci{
55495489c19Sopenharmony_ci    if (!IsValidBluetoothAddr(stagingRealAddr_)) {
55595489c19Sopenharmony_ci        HILOGD("stagingRealAddr_ is invalid.");
55695489c19Sopenharmony_ci        return;
55795489c19Sopenharmony_ci    }
55895489c19Sopenharmony_ci    if (!IsValidBluetoothAddr(stagingRandomAddr_)) {
55995489c19Sopenharmony_ci        HILOGE("stagingRandomAddr_ is invalid.");
56095489c19Sopenharmony_ci        return;
56195489c19Sopenharmony_ci    }
56295489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
56395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
56495489c19Sopenharmony_ci    proxy->SyncRandomAddress(stagingRealAddr_, stagingRandomAddr_);
56595489c19Sopenharmony_ci    stagingRealAddr_ = "";
56695489c19Sopenharmony_ci    stagingRandomAddr_ = "";
56795489c19Sopenharmony_ci}
56895489c19Sopenharmony_ci
56995489c19Sopenharmony_ciBluetoothHost::BluetoothHost()
57095489c19Sopenharmony_ci{
57195489c19Sopenharmony_ci    pimpl = std::make_unique<impl>();
57295489c19Sopenharmony_ci    if (!pimpl) {
57395489c19Sopenharmony_ci        HILOGE("fails: no pimpl");
57495489c19Sopenharmony_ci    }
57595489c19Sopenharmony_ci}
57695489c19Sopenharmony_ci
57795489c19Sopenharmony_ciBluetoothHost::~BluetoothHost() {}
57895489c19Sopenharmony_ci
57995489c19Sopenharmony_ciBluetoothHost &BluetoothHost::GetDefaultHost()
58095489c19Sopenharmony_ci{
58195489c19Sopenharmony_ci#ifdef DTFUZZ_TEST
58295489c19Sopenharmony_ci    static BluetoothNoDestructor<BluetoothHost> instance;
58395489c19Sopenharmony_ci    return *instance;
58495489c19Sopenharmony_ci#else
58595489c19Sopenharmony_ci    // C++11 static local variable initialization is thread-safe.
58695489c19Sopenharmony_ci    static BluetoothHost hostAdapter;
58795489c19Sopenharmony_ci    return hostAdapter;
58895489c19Sopenharmony_ci#endif
58995489c19Sopenharmony_ci}
59095489c19Sopenharmony_ci
59195489c19Sopenharmony_civoid BluetoothHost::RegisterObserver(std::shared_ptr<BluetoothHostObserver> observer)
59295489c19Sopenharmony_ci{
59395489c19Sopenharmony_ci    HILOGD("enter");
59495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
59595489c19Sopenharmony_ci    pimpl->observers_.Register(observer);
59695489c19Sopenharmony_ci}
59795489c19Sopenharmony_ci
59895489c19Sopenharmony_civoid BluetoothHost::DeregisterObserver(std::shared_ptr<BluetoothHostObserver> observer)
59995489c19Sopenharmony_ci{
60095489c19Sopenharmony_ci    HILOGD("enter");
60195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
60295489c19Sopenharmony_ci    pimpl->observers_.Deregister(observer);
60395489c19Sopenharmony_ci}
60495489c19Sopenharmony_ci
60595489c19Sopenharmony_ciint BluetoothHost::EnableBt()
60695489c19Sopenharmony_ci{
60795489c19Sopenharmony_ci    HILOGD("enter");
60895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(!IsBtProhibitedByEdm(), BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited");
60995489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
61095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
61195489c19Sopenharmony_ci
61295489c19Sopenharmony_ci    return proxy->EnableBt();
61395489c19Sopenharmony_ci}
61495489c19Sopenharmony_ci
61595489c19Sopenharmony_ciint BluetoothHost::DisableBt()
61695489c19Sopenharmony_ci{
61795489c19Sopenharmony_ci    HILOGI("enter");
61895489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
61995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
62095489c19Sopenharmony_ci    return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH);
62195489c19Sopenharmony_ci}
62295489c19Sopenharmony_ci
62395489c19Sopenharmony_cistatic void PublishBtSwitchRestrictBluetoothEvent(void)
62495489c19Sopenharmony_ci{
62595489c19Sopenharmony_ci    OHOS::AAFwk::Want want;
62695489c19Sopenharmony_ci    want.SetAction("usual.event.bluetooth.BT_SWITCH_RESTRICT_BLUETOOTH");
62795489c19Sopenharmony_ci
62895489c19Sopenharmony_ci    OHOS::EventFwk::CommonEventData data;
62995489c19Sopenharmony_ci    data.SetWant(want);
63095489c19Sopenharmony_ci
63195489c19Sopenharmony_ci    OHOS::EventFwk::CommonEventPublishInfo publishInfo;
63295489c19Sopenharmony_ci    publishInfo.SetSubscriberPermissions({"ohos.permission.ACCESS_BLUETOOTH"});
63395489c19Sopenharmony_ci    bool ret = OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo);
63495489c19Sopenharmony_ci    if (!ret) {
63595489c19Sopenharmony_ci        HILOGE("Publish usual.event.bluetooth.BT_SWITCH_RESTRICT_BLUETOOTH event failed");
63695489c19Sopenharmony_ci        return;
63795489c19Sopenharmony_ci    }
63895489c19Sopenharmony_ci}
63995489c19Sopenharmony_ci
64095489c19Sopenharmony_ciint BluetoothHost::RestrictBluetooth()
64195489c19Sopenharmony_ci{
64295489c19Sopenharmony_ci    HILOGI("enter");
64395489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
64495489c19Sopenharmony_ci    PublishBtSwitchRestrictBluetoothEvent();
64595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
64695489c19Sopenharmony_ci    int ret =  pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH);
64795489c19Sopenharmony_ci    if (ret != BT_NO_ERROR) {
64895489c19Sopenharmony_ci        return ret;
64995489c19Sopenharmony_ci    }
65095489c19Sopenharmony_ci    ret = pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE);
65195489c19Sopenharmony_ci    return ret;
65295489c19Sopenharmony_ci}
65395489c19Sopenharmony_ci
65495489c19Sopenharmony_civoid BluetoothHost::UpdateVirtualDevice(int32_t action, const std::string &address)
65595489c19Sopenharmony_ci{
65695489c19Sopenharmony_ci    HILOGD("enter");
65795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off");
65895489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
65995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
66095489c19Sopenharmony_ci    proxy->UpdateVirtualDevice(action, address);
66195489c19Sopenharmony_ci}
66295489c19Sopenharmony_ci
66395489c19Sopenharmony_ciint BluetoothHost::SatelliteControl(int type, int state)
66495489c19Sopenharmony_ci{
66595489c19Sopenharmony_ci    HILOGI("type: %{public}d, state: %{public}d", type, state);
66695489c19Sopenharmony_ci    if (type == static_cast<int>(SATELLITE_CONTROL_MODE::ANTENNA)) {
66795489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
66895489c19Sopenharmony_ci    } else if (type == static_cast<int>(SATELLITE_CONTROL_MODE::BLUETOOTH_SWITCH)) {
66995489c19Sopenharmony_ci        pimpl->LoadBluetoothHostService();
67095489c19Sopenharmony_ci    } else {
67195489c19Sopenharmony_ci        HILOGE("Invalid control type: %{public}d", type);
67295489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
67395489c19Sopenharmony_ci    }
67495489c19Sopenharmony_ci
67595489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
67695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
67795489c19Sopenharmony_ci    return proxy->SatelliteControl(type, state);
67895489c19Sopenharmony_ci}
67995489c19Sopenharmony_ci
68095489c19Sopenharmony_ciint BluetoothHost::GetBtState() const
68195489c19Sopenharmony_ci{
68295489c19Sopenharmony_ci    HILOGD("enter");
68395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
68495489c19Sopenharmony_ci        HILOGD("bluetooth is off.");
68595489c19Sopenharmony_ci        return BTStateID::STATE_TURN_OFF;
68695489c19Sopenharmony_ci    }
68795489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
68895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BTStateID::STATE_TURN_OFF, "proxy is nullptr");
68995489c19Sopenharmony_ci
69095489c19Sopenharmony_ci    int state = BTStateID::STATE_TURN_OFF;
69195489c19Sopenharmony_ci    proxy->GetBtState(state);
69295489c19Sopenharmony_ci    HILOGD("state: %{public}d", state);
69395489c19Sopenharmony_ci    return state;
69495489c19Sopenharmony_ci}
69595489c19Sopenharmony_ci
69695489c19Sopenharmony_ciint BluetoothHost::GetBtState(int &state) const
69795489c19Sopenharmony_ci{
69895489c19Sopenharmony_ci    HILOGD("enter");
69995489c19Sopenharmony_ci    state = BTStateID::STATE_TURN_OFF;
70095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_NO_ERROR, "bluetooth is off.");
70195489c19Sopenharmony_ci
70295489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
70395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr");
70495489c19Sopenharmony_ci
70595489c19Sopenharmony_ci    int ret = proxy->GetBtState(state);
70695489c19Sopenharmony_ci    HILOGI("state: %{public}d", state);
70795489c19Sopenharmony_ci    return ret;
70895489c19Sopenharmony_ci}
70995489c19Sopenharmony_ci
71095489c19Sopenharmony_ciint BluetoothHost::impl::EnableBluetoothAfterFactoryReset(void)
71195489c19Sopenharmony_ci{
71295489c19Sopenharmony_ci    HILOGI("Attempt to enable bluetooth after factory reset");
71395489c19Sopenharmony_ci    isFactoryReseting_ = false;
71495489c19Sopenharmony_ci    SetParameter("persist.bluetooth.switch_enable", "2");  // 2 means bluetooth auto enter restricted mode
71595489c19Sopenharmony_ci    return BT_NO_ERROR;
71695489c19Sopenharmony_ci}
71795489c19Sopenharmony_ci
71895489c19Sopenharmony_ciint BluetoothHost::BluetoothFactoryReset()
71995489c19Sopenharmony_ci{
72095489c19Sopenharmony_ci    HILOGI("enter");
72195489c19Sopenharmony_ci    constexpr const char* BLUETOOTH_FACTORY_RESET_KEY = "persist.bluetooth.factoryreset";
72295489c19Sopenharmony_ci    int ret = SetParameter(BLUETOOTH_FACTORY_RESET_KEY, "true");
72395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(ret == 0, BT_ERR_INTERNAL_ERROR, "SetParameter failed");
72495489c19Sopenharmony_ci
72595489c19Sopenharmony_ci    pimpl->isFactoryReseting_ = true;
72695489c19Sopenharmony_ci
72795489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
72895489c19Sopenharmony_ci    if (proxy == nullptr) {
72995489c19Sopenharmony_ci        return pimpl->EnableBluetoothAfterFactoryReset();
73095489c19Sopenharmony_ci    }
73195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BLE_ENABLED(), BT_NO_ERROR, "bluetooth is off.");
73295489c19Sopenharmony_ci    return proxy->BluetoothFactoryReset();
73395489c19Sopenharmony_ci}
73495489c19Sopenharmony_ci
73595489c19Sopenharmony_cibool BluetoothHost::IsValidBluetoothAddr(const std::string &addr)
73695489c19Sopenharmony_ci{
73795489c19Sopenharmony_ci#if defined(IOS_PLATFORM)
73895489c19Sopenharmony_ci    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}$");
73995489c19Sopenharmony_ci    return regex_match(addr, deviceIdRegex);
74095489c19Sopenharmony_ci#elif defined(ANDROID_PLATFORM)
74195489c19Sopenharmony_ci    const std::regex deviceIdRegex("^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$");
74295489c19Sopenharmony_ci    return regex_match(addr, deviceIdRegex);
74395489c19Sopenharmony_ci#else
74495489c19Sopenharmony_ci    if (addr.length() != ADDRESS_LENGTH) {
74595489c19Sopenharmony_ci        HILOGD("invalid address len.");
74695489c19Sopenharmony_ci        return false;
74795489c19Sopenharmony_ci    }
74895489c19Sopenharmony_ci
74995489c19Sopenharmony_ci    for (int i = 0; i < ADDRESS_LENGTH; i++) {
75095489c19Sopenharmony_ci        char c = addr[i];
75195489c19Sopenharmony_ci        switch (i % ADDRESS_SEPARATOR_UNIT) {
75295489c19Sopenharmony_ci            case 0:
75395489c19Sopenharmony_ci            case 1:
75495489c19Sopenharmony_ci                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
75595489c19Sopenharmony_ci                    break;
75695489c19Sopenharmony_ci                }
75795489c19Sopenharmony_ci                return false;
75895489c19Sopenharmony_ci            case ADDRESS_COLON_INDEX:
75995489c19Sopenharmony_ci            default:
76095489c19Sopenharmony_ci                if (c == ':') {
76195489c19Sopenharmony_ci                    break;
76295489c19Sopenharmony_ci                }
76395489c19Sopenharmony_ci                return false;
76495489c19Sopenharmony_ci        }
76595489c19Sopenharmony_ci    }
76695489c19Sopenharmony_ci    return true;
76795489c19Sopenharmony_ci#endif
76895489c19Sopenharmony_ci}
76995489c19Sopenharmony_ci
77095489c19Sopenharmony_ciBluetoothRemoteDevice BluetoothHost::GetRemoteDevice(const std::string &addr, int transport) const
77195489c19Sopenharmony_ci{
77295489c19Sopenharmony_ci    BluetoothRemoteDevice remoteDevice(addr, transport);
77395489c19Sopenharmony_ci    return remoteDevice;
77495489c19Sopenharmony_ci}
77595489c19Sopenharmony_ci
77695489c19Sopenharmony_ciint BluetoothHost::EnableBle()
77795489c19Sopenharmony_ci{
77895489c19Sopenharmony_ci    HILOGI("enter");
77995489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
78095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
78195489c19Sopenharmony_ci    return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH);
78295489c19Sopenharmony_ci}
78395489c19Sopenharmony_ci
78495489c19Sopenharmony_ciint BluetoothHost::DisableBle()
78595489c19Sopenharmony_ci{
78695489c19Sopenharmony_ci    HILOGD("enter");
78795489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
78895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
78995489c19Sopenharmony_ci
79095489c19Sopenharmony_ci    return proxy->DisableBle();
79195489c19Sopenharmony_ci}
79295489c19Sopenharmony_ci
79395489c19Sopenharmony_ciint BluetoothHost::EnableBluetoothToRestrictMode(void)
79495489c19Sopenharmony_ci{
79595489c19Sopenharmony_ci    HILOGI("enter");
79695489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
79795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr");
79895489c19Sopenharmony_ci    return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE);
79995489c19Sopenharmony_ci}
80095489c19Sopenharmony_ci
80195489c19Sopenharmony_cibool BluetoothHost::IsBrEnabled() const
80295489c19Sopenharmony_ci{
80395489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
80495489c19Sopenharmony_ci    if (proxy == nullptr) {
80595489c19Sopenharmony_ci        HILOGD("proxy is nullptr");
80695489c19Sopenharmony_ci        return false;
80795489c19Sopenharmony_ci    }
80895489c19Sopenharmony_ci
80995489c19Sopenharmony_ci    return proxy->IsBrEnabled();
81095489c19Sopenharmony_ci}
81195489c19Sopenharmony_ci
81295489c19Sopenharmony_cibool BluetoothHost::IsBleEnabled() const
81395489c19Sopenharmony_ci{
81495489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
81595489c19Sopenharmony_ci    if (proxy == nullptr) {
81695489c19Sopenharmony_ci        HILOGD("proxy is nullptr");
81795489c19Sopenharmony_ci        return false;
81895489c19Sopenharmony_ci    }
81995489c19Sopenharmony_ci
82095489c19Sopenharmony_ci    return proxy->IsBleEnabled();
82195489c19Sopenharmony_ci}
82295489c19Sopenharmony_ci
82395489c19Sopenharmony_ciint BluetoothHost::GetLocalAddress(std::string &addr) const
82495489c19Sopenharmony_ci{
82595489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
82695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
82795489c19Sopenharmony_ci
82895489c19Sopenharmony_ci    return proxy->GetLocalAddress(addr);
82995489c19Sopenharmony_ci}
83095489c19Sopenharmony_ci
83195489c19Sopenharmony_cistd::vector<uint32_t> BluetoothHost::GetProfileList() const
83295489c19Sopenharmony_ci{
83395489c19Sopenharmony_ci    HILOGD("enter");
83495489c19Sopenharmony_ci    std::vector<uint32_t> profileList;
83595489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
83695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, profileList, "proxy is nullptr");
83795489c19Sopenharmony_ci
83895489c19Sopenharmony_ci    profileList = proxy->GetProfileList();
83995489c19Sopenharmony_ci    return profileList;
84095489c19Sopenharmony_ci}
84195489c19Sopenharmony_ci
84295489c19Sopenharmony_ciint BluetoothHost::GetMaxNumConnectedAudioDevices() const
84395489c19Sopenharmony_ci{
84495489c19Sopenharmony_ci    HILOGD("enter");
84595489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
84695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
84795489c19Sopenharmony_ci
84895489c19Sopenharmony_ci    return proxy->GetMaxNumConnectedAudioDevices();
84995489c19Sopenharmony_ci}
85095489c19Sopenharmony_ci
85195489c19Sopenharmony_ciint BluetoothHost::GetBtConnectionState() const
85295489c19Sopenharmony_ci{
85395489c19Sopenharmony_ci    HILOGD("enter");
85495489c19Sopenharmony_ci    int state = static_cast<int>(BTConnectState::DISCONNECTED);
85595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), state, "bluetooth is off.");
85695489c19Sopenharmony_ci
85795489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
85895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, state, "proxy is nullptr");
85995489c19Sopenharmony_ci
86095489c19Sopenharmony_ci    proxy->GetBtConnectionState(state);
86195489c19Sopenharmony_ci    HILOGI("state: %{public}d", state);
86295489c19Sopenharmony_ci    return state;
86395489c19Sopenharmony_ci}
86495489c19Sopenharmony_ci
86595489c19Sopenharmony_ciint BluetoothHost::GetBtConnectionState(int &state) const
86695489c19Sopenharmony_ci{
86795489c19Sopenharmony_ci    HILOGD("enter");
86895489c19Sopenharmony_ci    state = static_cast<int>(BTConnectState::DISCONNECTED);
86995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
87095489c19Sopenharmony_ci
87195489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
87295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
87395489c19Sopenharmony_ci
87495489c19Sopenharmony_ci    int ret = proxy->GetBtConnectionState(state);
87595489c19Sopenharmony_ci    HILOGI("state: %{public}d", state);
87695489c19Sopenharmony_ci    return ret;
87795489c19Sopenharmony_ci}
87895489c19Sopenharmony_ci
87995489c19Sopenharmony_ciint BluetoothHost::GetBtProfileConnState(uint32_t profileId, int &state) const
88095489c19Sopenharmony_ci{
88195489c19Sopenharmony_ci    HILOGI("profileId: %{public}d", profileId);
88295489c19Sopenharmony_ci    state = static_cast<int>(BTConnectState::DISCONNECTED);
88395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
88495489c19Sopenharmony_ci
88595489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
88695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
88795489c19Sopenharmony_ci
88895489c19Sopenharmony_ci    return proxy->GetBtProfileConnState(profileId, state);
88995489c19Sopenharmony_ci}
89095489c19Sopenharmony_ci
89195489c19Sopenharmony_civoid BluetoothHost::GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids)
89295489c19Sopenharmony_ci{
89395489c19Sopenharmony_ci    HILOGD("enter");
89495489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
89595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
89695489c19Sopenharmony_ci
89795489c19Sopenharmony_ci    std::vector<std::string> stringUuids;
89895489c19Sopenharmony_ci    proxy->GetLocalSupportedUuids(stringUuids);
89995489c19Sopenharmony_ci
90095489c19Sopenharmony_ci    for (auto uuid : stringUuids) {
90195489c19Sopenharmony_ci        uuids.push_back(UUID::FromString(uuid));
90295489c19Sopenharmony_ci    }
90395489c19Sopenharmony_ci}
90495489c19Sopenharmony_ci
90595489c19Sopenharmony_cibool BluetoothHost::Start()
90695489c19Sopenharmony_ci{
90795489c19Sopenharmony_ci    // / This function only used for passthrough, so this is empty.
90895489c19Sopenharmony_ci    return true;
90995489c19Sopenharmony_ci}
91095489c19Sopenharmony_ci
91195489c19Sopenharmony_civoid BluetoothHost::Stop()
91295489c19Sopenharmony_ci{
91395489c19Sopenharmony_ci    // / This function only used for passthrough, so this is empty.
91495489c19Sopenharmony_ci}
91595489c19Sopenharmony_ci
91695489c19Sopenharmony_ciBluetoothDeviceClass BluetoothHost::GetLocalDeviceClass() const
91795489c19Sopenharmony_ci{
91895489c19Sopenharmony_ci    HILOGD("enter");
91995489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
92095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BluetoothDeviceClass(0), "proxy is nullptr");
92195489c19Sopenharmony_ci
92295489c19Sopenharmony_ci    int LocalDeviceClass = proxy->GetLocalDeviceClass();
92395489c19Sopenharmony_ci    return BluetoothDeviceClass(LocalDeviceClass);
92495489c19Sopenharmony_ci}
92595489c19Sopenharmony_ci
92695489c19Sopenharmony_cibool BluetoothHost::SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass)
92795489c19Sopenharmony_ci{
92895489c19Sopenharmony_ci    HILOGD("enter");
92995489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
93095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
93195489c19Sopenharmony_ci
93295489c19Sopenharmony_ci    int cod = deviceClass.GetClassOfDevice();
93395489c19Sopenharmony_ci    return proxy->SetLocalDeviceClass(cod);
93495489c19Sopenharmony_ci}
93595489c19Sopenharmony_ci
93695489c19Sopenharmony_cistd::string BluetoothHost::GetLocalName() const
93795489c19Sopenharmony_ci{
93895489c19Sopenharmony_ci    HILOGD("enter");
93995489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
94095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::string(), "proxy is nullptr");
94195489c19Sopenharmony_ci
94295489c19Sopenharmony_ci    std::string name = INVALID_NAME;
94395489c19Sopenharmony_ci    proxy->GetLocalName(name);
94495489c19Sopenharmony_ci    return name;
94595489c19Sopenharmony_ci}
94695489c19Sopenharmony_ci
94795489c19Sopenharmony_ciint BluetoothHost::GetLocalName(std::string &name) const
94895489c19Sopenharmony_ci{
94995489c19Sopenharmony_ci    HILOGD("enter");
95095489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
95195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
95295489c19Sopenharmony_ci
95395489c19Sopenharmony_ci    return proxy->GetLocalName(name);
95495489c19Sopenharmony_ci}
95595489c19Sopenharmony_ci
95695489c19Sopenharmony_ciint BluetoothHost::SetLocalName(const std::string &name)
95795489c19Sopenharmony_ci{
95895489c19Sopenharmony_ci    HILOGD("enter");
95995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
96095489c19Sopenharmony_ci
96195489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
96295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
96395489c19Sopenharmony_ci
96495489c19Sopenharmony_ci    return proxy->SetLocalName(name);
96595489c19Sopenharmony_ci}
96695489c19Sopenharmony_ci
96795489c19Sopenharmony_ciint BluetoothHost::GetBtScanMode(int32_t &scanMode) const
96895489c19Sopenharmony_ci{
96995489c19Sopenharmony_ci    HILOGD("enter");
97095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
97195489c19Sopenharmony_ci
97295489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
97395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
97495489c19Sopenharmony_ci
97595489c19Sopenharmony_ci    return proxy->GetBtScanMode(scanMode);
97695489c19Sopenharmony_ci}
97795489c19Sopenharmony_ci
97895489c19Sopenharmony_ciint BluetoothHost::SetBtScanMode(int mode, int duration)
97995489c19Sopenharmony_ci{
98095489c19Sopenharmony_ci    HILOGD("mode: %{public}d, duration: %{public}d", mode, duration);
98195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
98295489c19Sopenharmony_ci
98395489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
98495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
98595489c19Sopenharmony_ci
98695489c19Sopenharmony_ci    return proxy->SetBtScanMode(mode, duration);
98795489c19Sopenharmony_ci}
98895489c19Sopenharmony_ci
98995489c19Sopenharmony_ciint BluetoothHost::GetBondableMode(int transport) const
99095489c19Sopenharmony_ci{
99195489c19Sopenharmony_ci    HILOGI("transport: %{public}d", transport);
99295489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
99395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
99495489c19Sopenharmony_ci
99595489c19Sopenharmony_ci    return proxy->GetBondableMode(transport);
99695489c19Sopenharmony_ci}
99795489c19Sopenharmony_ci
99895489c19Sopenharmony_cibool BluetoothHost::SetBondableMode(int transport, int mode)
99995489c19Sopenharmony_ci{
100095489c19Sopenharmony_ci    HILOGD("transport: %{public}d, mode: %{public}d", transport, mode);
100195489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
100295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
100395489c19Sopenharmony_ci
100495489c19Sopenharmony_ci    return proxy->SetBondableMode(transport, mode);
100595489c19Sopenharmony_ci}
100695489c19Sopenharmony_ci
100795489c19Sopenharmony_ciint BluetoothHost::StartBtDiscovery()
100895489c19Sopenharmony_ci{
100995489c19Sopenharmony_ci    HILOGD("enter");
101095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
101195489c19Sopenharmony_ci
101295489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
101395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
101495489c19Sopenharmony_ci
101595489c19Sopenharmony_ci    return proxy->StartBtDiscovery();
101695489c19Sopenharmony_ci}
101795489c19Sopenharmony_ci
101895489c19Sopenharmony_ciint BluetoothHost::CancelBtDiscovery()
101995489c19Sopenharmony_ci{
102095489c19Sopenharmony_ci    HILOGD("enter");
102195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
102295489c19Sopenharmony_ci
102395489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
102495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
102595489c19Sopenharmony_ci
102695489c19Sopenharmony_ci    return proxy->CancelBtDiscovery();
102795489c19Sopenharmony_ci}
102895489c19Sopenharmony_ci
102995489c19Sopenharmony_ciint32_t BluetoothHost::IsBtDiscovering(bool &isDiscovering, int transport) const
103095489c19Sopenharmony_ci{
103195489c19Sopenharmony_ci    HILOGI("transport: %{public}d", transport);
103295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
103395489c19Sopenharmony_ci
103495489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
103595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr");
103695489c19Sopenharmony_ci
103795489c19Sopenharmony_ci    return proxy->IsBtDiscovering(isDiscovering, transport);
103895489c19Sopenharmony_ci}
103995489c19Sopenharmony_ci
104095489c19Sopenharmony_cilong BluetoothHost::GetBtDiscoveryEndMillis() const
104195489c19Sopenharmony_ci{
104295489c19Sopenharmony_ci    HILOGD("enter");
104395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), 0, "bluetooth is off.");
104495489c19Sopenharmony_ci
104595489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
104695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, 0, "proxy is nullptr");
104795489c19Sopenharmony_ci
104895489c19Sopenharmony_ci    return proxy->GetBtDiscoveryEndMillis();
104995489c19Sopenharmony_ci}
105095489c19Sopenharmony_ci
105195489c19Sopenharmony_ciint32_t BluetoothHost::GetPairedDevices(int transport, std::vector<BluetoothRemoteDevice> &pairedDevices) const
105295489c19Sopenharmony_ci{
105395489c19Sopenharmony_ci    HILOGI("transport: %{public}d", transport);
105495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
105595489c19Sopenharmony_ci
105695489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
105795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
105895489c19Sopenharmony_ci
105995489c19Sopenharmony_ci    std::vector<BluetoothRawAddress> pairedAddr;
106095489c19Sopenharmony_ci    int32_t ret = proxy->GetPairedDevices(pairedAddr);
106195489c19Sopenharmony_ci
106295489c19Sopenharmony_ci    for (auto it = pairedAddr.begin(); it != pairedAddr.end(); it++) {
106395489c19Sopenharmony_ci        BluetoothRemoteDevice device((*it).GetAddress(), transport);
106495489c19Sopenharmony_ci        pairedDevices.emplace_back(device);
106595489c19Sopenharmony_ci    }
106695489c19Sopenharmony_ci    return ret;
106795489c19Sopenharmony_ci}
106895489c19Sopenharmony_ci
106995489c19Sopenharmony_ciint32_t BluetoothHost::RemovePair(const BluetoothRemoteDevice &device)
107095489c19Sopenharmony_ci{
107195489c19Sopenharmony_ci    HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
107295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device.");
107395489c19Sopenharmony_ci
107495489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
107595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
107695489c19Sopenharmony_ci
107795489c19Sopenharmony_ci    sptr<BluetoothRawAddress> rawAddrSptr = new BluetoothRawAddress(device.GetDeviceAddr());
107895489c19Sopenharmony_ci    return proxy->RemovePair(device.GetTransportType(), rawAddrSptr);
107995489c19Sopenharmony_ci}
108095489c19Sopenharmony_ci
108195489c19Sopenharmony_cibool BluetoothHost::RemoveAllPairs()
108295489c19Sopenharmony_ci{
108395489c19Sopenharmony_ci    HILOGD("enter");
108495489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
108595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr");
108695489c19Sopenharmony_ci
108795489c19Sopenharmony_ci    return proxy->RemoveAllPairs();
108895489c19Sopenharmony_ci}
108995489c19Sopenharmony_ci
109095489c19Sopenharmony_civoid BluetoothHost::RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)
109195489c19Sopenharmony_ci{
109295489c19Sopenharmony_ci    HILOGD("enter");
109395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
109495489c19Sopenharmony_ci
109595489c19Sopenharmony_ci    pimpl->remoteObservers_.Register(observer);
109695489c19Sopenharmony_ci}
109795489c19Sopenharmony_ci
109895489c19Sopenharmony_civoid BluetoothHost::DeregisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer)
109995489c19Sopenharmony_ci{
110095489c19Sopenharmony_ci    HILOGD("enter");
110195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
110295489c19Sopenharmony_ci
110395489c19Sopenharmony_ci    pimpl->remoteObservers_.Deregister(observer);
110495489c19Sopenharmony_ci}
110595489c19Sopenharmony_ci
110695489c19Sopenharmony_ciint BluetoothHost::GetBleMaxAdvertisingDataLength() const
110795489c19Sopenharmony_ci{
110895489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
110995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, INVALID_VALUE, "proxy is nullptr");
111095489c19Sopenharmony_ci
111195489c19Sopenharmony_ci    return proxy->GetBleMaxAdvertisingDataLength();
111295489c19Sopenharmony_ci}
111395489c19Sopenharmony_ci
111495489c19Sopenharmony_civoid BluetoothHost::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
111595489c19Sopenharmony_ci{
111695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl, "pimpl is null.");
111795489c19Sopenharmony_ci
111895489c19Sopenharmony_ci    pimpl->LoadSystemAbilitySuccess(remoteObject);
111995489c19Sopenharmony_ci}
112095489c19Sopenharmony_ci
112195489c19Sopenharmony_civoid BluetoothHost::LoadSystemAbilityFail()
112295489c19Sopenharmony_ci{
112395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl, "pimpl is null.");
112495489c19Sopenharmony_ci
112595489c19Sopenharmony_ci    pimpl->LoadSystemAbilityFail();
112695489c19Sopenharmony_ci}
112795489c19Sopenharmony_ci
112895489c19Sopenharmony_ciint32_t BluetoothHost::GetLocalProfileUuids(std::vector<std::string> &uuids)
112995489c19Sopenharmony_ci{
113095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INTERNAL_ERROR, "bluetooth is off.");
113195489c19Sopenharmony_ci
113295489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
113395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
113495489c19Sopenharmony_ci
113595489c19Sopenharmony_ci    return proxy->GetLocalProfileUuids(uuids);
113695489c19Sopenharmony_ci}
113795489c19Sopenharmony_ci
113895489c19Sopenharmony_ciint BluetoothHost::SetFastScan(bool isEnable)
113995489c19Sopenharmony_ci{
114095489c19Sopenharmony_ci    HILOGI("isEnable: %{public}d", isEnable);
114195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
114295489c19Sopenharmony_ci
114395489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
114495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
114595489c19Sopenharmony_ci
114695489c19Sopenharmony_ci    return proxy->SetFastScan(isEnable);
114795489c19Sopenharmony_ci}
114895489c19Sopenharmony_ci
114995489c19Sopenharmony_ciint BluetoothHost::GetRandomAddress(const std::string &realAddr, std::string &randomAddr) const
115095489c19Sopenharmony_ci{
115195489c19Sopenharmony_ci    randomAddr = realAddr;
115295489c19Sopenharmony_ci    return BT_NO_ERROR;
115395489c19Sopenharmony_ci}
115495489c19Sopenharmony_ci
115595489c19Sopenharmony_ciint BluetoothHost::ConnectAllowedProfiles(const std::string &remoteAddr) const
115695489c19Sopenharmony_ci{
115795489c19Sopenharmony_ci    HILOGI("enter");
115895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
115995489c19Sopenharmony_ci
116095489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
116195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
116295489c19Sopenharmony_ci
116395489c19Sopenharmony_ci    return proxy->ConnectAllowedProfiles(remoteAddr);
116495489c19Sopenharmony_ci}
116595489c19Sopenharmony_ci
116695489c19Sopenharmony_ciint BluetoothHost::DisconnectAllowedProfiles(const std::string &remoteAddr) const
116795489c19Sopenharmony_ci{
116895489c19Sopenharmony_ci    HILOGI("enter");
116995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
117095489c19Sopenharmony_ci
117195489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
117295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
117395489c19Sopenharmony_ci
117495489c19Sopenharmony_ci    return proxy->DisconnectAllowedProfiles(remoteAddr);
117595489c19Sopenharmony_ci}
117695489c19Sopenharmony_ci
117795489c19Sopenharmony_civoid BluetoothHost::RegisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)
117895489c19Sopenharmony_ci{
117995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
118095489c19Sopenharmony_ci
118195489c19Sopenharmony_ci    pimpl->resourceManagerObservers_.Register(observer);
118295489c19Sopenharmony_ci}
118395489c19Sopenharmony_ci
118495489c19Sopenharmony_civoid BluetoothHost::DeregisterBtResourceManagerObserver(std::shared_ptr<BluetoothResourceManagerObserver> observer)
118595489c19Sopenharmony_ci{
118695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
118795489c19Sopenharmony_ci
118895489c19Sopenharmony_ci    pimpl->resourceManagerObservers_.Deregister(observer);
118995489c19Sopenharmony_ci}
119095489c19Sopenharmony_ci
119195489c19Sopenharmony_ciint BluetoothHost::SetFastScanLevel(int level)
119295489c19Sopenharmony_ci{
119395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
119495489c19Sopenharmony_ci    sptr<IBluetoothHost> proxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
119595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr");
119695489c19Sopenharmony_ci    return proxy->SetFastScanLevel(level);
119795489c19Sopenharmony_ci}
119895489c19Sopenharmony_ci
119995489c19Sopenharmony_cibool BluetoothHost::IsBtProhibitedByEdm(void)
120095489c19Sopenharmony_ci{
120195489c19Sopenharmony_ci    constexpr const char* BLUETOOTH_EDM_KEY = "persist.edm.prohibit_bluetooth";
120295489c19Sopenharmony_ci    constexpr const uint32_t PARAM_TRUE_LEN = 4; // "true" 4bytes
120395489c19Sopenharmony_ci    constexpr const uint32_t PARAM_FALSE_LEN = 5; // "false" 5bytes
120495489c19Sopenharmony_ci    constexpr const char* PARAM_TRUE = "true";
120595489c19Sopenharmony_ci    constexpr const char* PARAM_FALSE = "false";
120695489c19Sopenharmony_ci
120795489c19Sopenharmony_ci    char result[PARAM_FALSE_LEN + 1] = {0};
120895489c19Sopenharmony_ci    //  Returns the number of bytes of the system parameter if the operation is successful.
120995489c19Sopenharmony_ci    int len = GetParameter(BLUETOOTH_EDM_KEY, PARAM_FALSE, result, PARAM_FALSE_LEN + 1);
121095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(len == PARAM_FALSE_LEN || len == PARAM_TRUE_LEN, false, "GetParameter len is invalid.");
121195489c19Sopenharmony_ci
121295489c19Sopenharmony_ci    if (strncmp(result, PARAM_TRUE, PARAM_TRUE_LEN) == 0) {
121395489c19Sopenharmony_ci        HILOGW("bluetooth is prohibited by EDM. You won't be able to turn on bluetooth !");
121495489c19Sopenharmony_ci        return true;
121595489c19Sopenharmony_ci    }
121695489c19Sopenharmony_ci    return false;
121795489c19Sopenharmony_ci}
121895489c19Sopenharmony_ci
121995489c19Sopenharmony_cistatic bool IsBluetoothSystemAbilityOn(void)
122095489c19Sopenharmony_ci{
122195489c19Sopenharmony_ci    auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
122295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(samgrProxy != nullptr, false, "samgrProxy is nullptr");
122395489c19Sopenharmony_ci    auto object = samgrProxy->CheckSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
122495489c19Sopenharmony_ci    return object != nullptr;
122595489c19Sopenharmony_ci}
122695489c19Sopenharmony_ci
122795489c19Sopenharmony_civoid BluetoothHost::OnRemoveBluetoothSystemAbility()
122895489c19Sopenharmony_ci{
122995489c19Sopenharmony_ci    // Notify the upper layer that bluetooth is disabled.
123095489c19Sopenharmony_ci    bool isBluetoothSystemAbilityOn = IsBluetoothSystemAbilityOn();
123195489c19Sopenharmony_ci    if (isBluetoothSystemAbilityOn) {
123295489c19Sopenharmony_ci        HILOGW("Bluetooth SA is started, the hap application may be freezed by rss");
123395489c19Sopenharmony_ci    }
123495489c19Sopenharmony_ci    bool isNeedNotifyBluetoothOffState = pimpl->observerImp_ && pimpl->bleObserverImp_ && !isBluetoothSystemAbilityOn;
123595489c19Sopenharmony_ci    if (isNeedNotifyBluetoothOffState) {
123695489c19Sopenharmony_ci        HILOGD("bluetooth_servi died and send state off to app");
123795489c19Sopenharmony_ci        pimpl->observerImp_->OnStateChanged(BTTransport::ADAPTER_BREDR, BTStateID::STATE_TURN_OFF);
123895489c19Sopenharmony_ci        pimpl->bleObserverImp_->OnStateChanged(BTTransport::ADAPTER_BLE, BTStateID::STATE_TURN_OFF);
123995489c19Sopenharmony_ci    }
124095489c19Sopenharmony_ci    if (pimpl->isFactoryReseting_.load()) {
124195489c19Sopenharmony_ci        pimpl->EnableBluetoothAfterFactoryReset();
124295489c19Sopenharmony_ci    }
124395489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
124495489c19Sopenharmony_ci    if (pimpl->switchModule_) {
124595489c19Sopenharmony_ci        pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::BLUETOOTH_OFF);
124695489c19Sopenharmony_ci    }
124795489c19Sopenharmony_ci}
124895489c19Sopenharmony_ci
124995489c19Sopenharmony_civoid BluetoothHost::Close(void)
125095489c19Sopenharmony_ci{
125195489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->switchModuleMutex_);
125295489c19Sopenharmony_ci    pimpl->switchModule_ = nullptr;
125395489c19Sopenharmony_ci}
125495489c19Sopenharmony_ci} // namespace Bluetooth
125595489c19Sopenharmony_ci} // namespace OHOS
1256