195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 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_hid_host.h"
1795489c19Sopenharmony_ci#include <unistd.h>
1895489c19Sopenharmony_ci#include "bluetooth_device.h"
1995489c19Sopenharmony_ci#include "bluetooth_host.h"
2095489c19Sopenharmony_ci#include "bluetooth_profile_manager.h"
2195489c19Sopenharmony_ci#include "bluetooth_log.h"
2295489c19Sopenharmony_ci#include "bluetooth_observer_list.h"
2395489c19Sopenharmony_ci#include "bluetooth_hid_host_observer_stub.h"
2495489c19Sopenharmony_ci#include "bluetooth_utils.h"
2595489c19Sopenharmony_ci#include "i_bluetooth_hid_host.h"
2695489c19Sopenharmony_ci#include "i_bluetooth_host.h"
2795489c19Sopenharmony_ci#include "iservice_registry.h"
2895489c19Sopenharmony_ci#include "system_ability_definition.h"
2995489c19Sopenharmony_ci
3095489c19Sopenharmony_cinamespace OHOS {
3195489c19Sopenharmony_cinamespace Bluetooth {
3295489c19Sopenharmony_cistd::mutex g_hidProxyMutex;
3395489c19Sopenharmony_ciclass HidHostInnerObserver : public BluetoothHidHostObserverStub {
3495489c19Sopenharmony_cipublic:
3595489c19Sopenharmony_ci    explicit HidHostInnerObserver(BluetoothObserverList<HidHostObserver> &observers) : observers_(observers)
3695489c19Sopenharmony_ci    {
3795489c19Sopenharmony_ci        HILOGD("Enter!");
3895489c19Sopenharmony_ci    }
3995489c19Sopenharmony_ci    ~HidHostInnerObserver() override
4095489c19Sopenharmony_ci    {
4195489c19Sopenharmony_ci        HILOGD("Enter!");
4295489c19Sopenharmony_ci    }
4395489c19Sopenharmony_ci
4495489c19Sopenharmony_ci    ErrCode OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t cause) override
4595489c19Sopenharmony_ci    {
4695489c19Sopenharmony_ci        HILOGD("hid conn state, device: %{public}s, state: %{public}s, cause: %{public}d",
4795489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), GetProfileConnStateName(state).c_str(), cause);
4895489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
4995489c19Sopenharmony_ci        observers_.ForEach([remoteDevice, state, cause](std::shared_ptr<HidHostObserver> observer) {
5095489c19Sopenharmony_ci            observer->OnConnectionStateChanged(remoteDevice, state, cause);
5195489c19Sopenharmony_ci        });
5295489c19Sopenharmony_ci        return NO_ERROR;
5395489c19Sopenharmony_ci    }
5495489c19Sopenharmony_ci
5595489c19Sopenharmony_ciprivate:
5695489c19Sopenharmony_ci    BluetoothObserverList<HidHostObserver> &observers_;
5795489c19Sopenharmony_ci    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HidHostInnerObserver);
5895489c19Sopenharmony_ci};
5995489c19Sopenharmony_ci
6095489c19Sopenharmony_cistruct HidHost::impl {
6195489c19Sopenharmony_ci    impl();
6295489c19Sopenharmony_ci    ~impl();
6395489c19Sopenharmony_ci
6495489c19Sopenharmony_ci    int32_t GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice>& result)
6595489c19Sopenharmony_ci    {
6695489c19Sopenharmony_ci        HILOGI("Enter!");
6795489c19Sopenharmony_ci        std::vector<BluetoothRawAddress> rawDevices;
6895489c19Sopenharmony_ci        std::vector<int32_t> tmpStates;
6995489c19Sopenharmony_ci        for (int32_t state : states) {
7095489c19Sopenharmony_ci            tmpStates.push_back((int32_t)state);
7195489c19Sopenharmony_ci        }
7295489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
7395489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
7495489c19Sopenharmony_ci        int32_t ret = proxy->GetDevicesByStates(tmpStates, rawDevices);
7595489c19Sopenharmony_ci        if (ret != BT_NO_ERROR) {
7695489c19Sopenharmony_ci            HILOGE("inner error.");
7795489c19Sopenharmony_ci            return ret;
7895489c19Sopenharmony_ci        }
7995489c19Sopenharmony_ci
8095489c19Sopenharmony_ci        for (BluetoothRawAddress rawDevice : rawDevices) {
8195489c19Sopenharmony_ci            BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 1);
8295489c19Sopenharmony_ci            result.push_back(remoteDevice);
8395489c19Sopenharmony_ci        }
8495489c19Sopenharmony_ci
8595489c19Sopenharmony_ci        return BT_NO_ERROR;
8695489c19Sopenharmony_ci    }
8795489c19Sopenharmony_ci
8895489c19Sopenharmony_ci    int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
8995489c19Sopenharmony_ci    {
9095489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
9195489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
9295489c19Sopenharmony_ci        if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
9395489c19Sopenharmony_ci            HILOGE("invalid param.");
9495489c19Sopenharmony_ci            return BT_ERR_INVALID_PARAM;
9595489c19Sopenharmony_ci        }
9695489c19Sopenharmony_ci
9795489c19Sopenharmony_ci        return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
9895489c19Sopenharmony_ci    }
9995489c19Sopenharmony_ci
10095489c19Sopenharmony_ci    int32_t Connect(const BluetoothRemoteDevice &device)
10195489c19Sopenharmony_ci    {
10295489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
10395489c19Sopenharmony_ci        if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
10495489c19Sopenharmony_ci            HILOGE("invalid param.");
10595489c19Sopenharmony_ci            return BT_ERR_INVALID_PARAM;
10695489c19Sopenharmony_ci        }
10795489c19Sopenharmony_ci        HILOGI("hid connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
10895489c19Sopenharmony_ci        return proxy->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
10995489c19Sopenharmony_ci    }
11095489c19Sopenharmony_ci
11195489c19Sopenharmony_ci    int32_t Disconnect(const BluetoothRemoteDevice &device)
11295489c19Sopenharmony_ci    {
11395489c19Sopenharmony_ci        HILOGI("hid disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
11495489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
11595489c19Sopenharmony_ci        if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
11695489c19Sopenharmony_ci            HILOGE("invalid param.");
11795489c19Sopenharmony_ci            return BT_ERR_INVALID_PARAM;
11895489c19Sopenharmony_ci        }
11995489c19Sopenharmony_ci
12095489c19Sopenharmony_ci        return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
12195489c19Sopenharmony_ci    }
12295489c19Sopenharmony_ci
12395489c19Sopenharmony_ci    void RegisterObserver(std::shared_ptr<HidHostObserver> observer)
12495489c19Sopenharmony_ci    {
12595489c19Sopenharmony_ci        HILOGD("Enter!");
12695489c19Sopenharmony_ci        observers_.Register(observer);
12795489c19Sopenharmony_ci    }
12895489c19Sopenharmony_ci
12995489c19Sopenharmony_ci    void DeregisterObserver(std::shared_ptr<HidHostObserver> observer)
13095489c19Sopenharmony_ci    {
13195489c19Sopenharmony_ci        HILOGI("Enter!");
13295489c19Sopenharmony_ci        observers_.Deregister(observer);
13395489c19Sopenharmony_ci    }
13495489c19Sopenharmony_ci
13595489c19Sopenharmony_ci    void HidHostVCUnplug(std::string device, uint8_t id, uint16_t size, uint8_t type)
13695489c19Sopenharmony_ci    {
13795489c19Sopenharmony_ci        HILOGI("Enter!");
13895489c19Sopenharmony_ci        int result;
13995489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
14095489c19Sopenharmony_ci        if (proxy != nullptr) {
14195489c19Sopenharmony_ci            proxy->HidHostVCUnplug(device, id, size, type, result);
14295489c19Sopenharmony_ci        }
14395489c19Sopenharmony_ci    }
14495489c19Sopenharmony_ci
14595489c19Sopenharmony_ci    void HidHostSendData(std::string device, uint8_t id, uint16_t size, uint8_t type)
14695489c19Sopenharmony_ci    {
14795489c19Sopenharmony_ci        HILOGI("Enter!");
14895489c19Sopenharmony_ci        int result;
14995489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
15095489c19Sopenharmony_ci        if (proxy != nullptr) {
15195489c19Sopenharmony_ci            proxy->HidHostSendData(device, id, size, type, result);
15295489c19Sopenharmony_ci        }
15395489c19Sopenharmony_ci    }
15495489c19Sopenharmony_ci
15595489c19Sopenharmony_ci    void HidHostSetReport(std::string device, uint8_t type, std::string &report)
15695489c19Sopenharmony_ci    {
15795489c19Sopenharmony_ci        HILOGI("Enter!");
15895489c19Sopenharmony_ci        int result;
15995489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
16095489c19Sopenharmony_ci        if (proxy != nullptr) {
16195489c19Sopenharmony_ci            proxy->HidHostSetReport(device, type, report, result);
16295489c19Sopenharmony_ci        }
16395489c19Sopenharmony_ci    }
16495489c19Sopenharmony_ci
16595489c19Sopenharmony_ci    void HidHostGetReport(std::string device, uint8_t id, uint16_t size, uint8_t type)
16695489c19Sopenharmony_ci    {
16795489c19Sopenharmony_ci        HILOGI("Enter!");
16895489c19Sopenharmony_ci        int result;
16995489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
17095489c19Sopenharmony_ci        if (proxy != nullptr && IS_BT_ENABLED()) {
17195489c19Sopenharmony_ci            proxy->HidHostGetReport(device, id, size, type, result);
17295489c19Sopenharmony_ci        }
17395489c19Sopenharmony_ci    }
17495489c19Sopenharmony_ci
17595489c19Sopenharmony_ci    int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
17695489c19Sopenharmony_ci    {
17795489c19Sopenharmony_ci        HILOGI("enter");
17895489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
17995489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
18095489c19Sopenharmony_ci        return proxy->SetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
18195489c19Sopenharmony_ci    }
18295489c19Sopenharmony_ci
18395489c19Sopenharmony_ci    int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
18495489c19Sopenharmony_ci    {
18595489c19Sopenharmony_ci        HILOGI("enter");
18695489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
18795489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
18895489c19Sopenharmony_ci        return proxy->GetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
18995489c19Sopenharmony_ci    }
19095489c19Sopenharmony_ci
19195489c19Sopenharmony_ci    int32_t profileRegisterId = 0;
19295489c19Sopenharmony_ciprivate:
19395489c19Sopenharmony_ci    BluetoothObserverList<HidHostObserver> observers_;
19495489c19Sopenharmony_ci    sptr<HidHostInnerObserver> innerObserver_;
19595489c19Sopenharmony_ci};
19695489c19Sopenharmony_ci
19795489c19Sopenharmony_ciHidHost::impl::impl()
19895489c19Sopenharmony_ci{
19995489c19Sopenharmony_ci    innerObserver_ = new HidHostInnerObserver(observers_);
20095489c19Sopenharmony_ci    profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_HID_HOST_SERVER,
20195489c19Sopenharmony_ci        [this](sptr<IRemoteObject> remote) {
20295489c19Sopenharmony_ci        sptr<IBluetoothHidHost> proxy = iface_cast<IBluetoothHidHost>(remote);
20395489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
20495489c19Sopenharmony_ci        proxy->RegisterObserver(innerObserver_);
20595489c19Sopenharmony_ci    });
20695489c19Sopenharmony_ci}
20795489c19Sopenharmony_ci
20895489c19Sopenharmony_ciHidHost::impl::~impl()
20995489c19Sopenharmony_ci{
21095489c19Sopenharmony_ci    HILOGD("start");
21195489c19Sopenharmony_ci    BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
21295489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
21395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
21495489c19Sopenharmony_ci    proxy->DeregisterObserver(innerObserver_);
21595489c19Sopenharmony_ci}
21695489c19Sopenharmony_ci
21795489c19Sopenharmony_ciHidHost::HidHost()
21895489c19Sopenharmony_ci{
21995489c19Sopenharmony_ci    pimpl = std::make_unique<impl>();
22095489c19Sopenharmony_ci}
22195489c19Sopenharmony_ci
22295489c19Sopenharmony_ciHidHost::~HidHost() {}
22395489c19Sopenharmony_ci
22495489c19Sopenharmony_ciHidHost *HidHost::GetProfile()
22595489c19Sopenharmony_ci{
22695489c19Sopenharmony_ci#ifdef DTFUZZ_TEST
22795489c19Sopenharmony_ci    static BluetoothNoDestructor<HidHost> instance;
22895489c19Sopenharmony_ci    return instance.get();
22995489c19Sopenharmony_ci#else
23095489c19Sopenharmony_ci    static HidHost instance;
23195489c19Sopenharmony_ci    return &instance;
23295489c19Sopenharmony_ci#endif
23395489c19Sopenharmony_ci}
23495489c19Sopenharmony_ci
23595489c19Sopenharmony_ciint32_t HidHost::GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice> &result)
23695489c19Sopenharmony_ci{
23795489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
23895489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
23995489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
24095489c19Sopenharmony_ci    }
24195489c19Sopenharmony_ci
24295489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
24395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
24495489c19Sopenharmony_ci
24595489c19Sopenharmony_ci    return pimpl->GetDevicesByStates(states, result);
24695489c19Sopenharmony_ci}
24795489c19Sopenharmony_ci
24895489c19Sopenharmony_ciint32_t HidHost::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
24995489c19Sopenharmony_ci{
25095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
25195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
25295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
25395489c19Sopenharmony_ci    }
25495489c19Sopenharmony_ci
25595489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
25695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
25795489c19Sopenharmony_ci
25895489c19Sopenharmony_ci    return pimpl->GetDeviceState(device, state);
25995489c19Sopenharmony_ci}
26095489c19Sopenharmony_ci
26195489c19Sopenharmony_ciint32_t HidHost::Connect(const BluetoothRemoteDevice &device)
26295489c19Sopenharmony_ci{
26395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
26495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
26595489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
26695489c19Sopenharmony_ci    }
26795489c19Sopenharmony_ci
26895489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
26995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
27095489c19Sopenharmony_ci
27195489c19Sopenharmony_ci    return pimpl->Connect(device);
27295489c19Sopenharmony_ci}
27395489c19Sopenharmony_ci
27495489c19Sopenharmony_ciint32_t HidHost::Disconnect(const BluetoothRemoteDevice &device)
27595489c19Sopenharmony_ci{
27695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
27795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
27895489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
27995489c19Sopenharmony_ci    }
28095489c19Sopenharmony_ci
28195489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
28295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
28395489c19Sopenharmony_ci
28495489c19Sopenharmony_ci    return pimpl->Disconnect(device);
28595489c19Sopenharmony_ci}
28695489c19Sopenharmony_ci
28795489c19Sopenharmony_ciint HidHost::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
28895489c19Sopenharmony_ci{
28995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
29095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
29195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
29295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
29395489c19Sopenharmony_ci    }
29495489c19Sopenharmony_ci
29595489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
29695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
29795489c19Sopenharmony_ci
29895489c19Sopenharmony_ci    if ((!device.IsValidBluetoothRemoteDevice()) || (
29995489c19Sopenharmony_ci        (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
30095489c19Sopenharmony_ci        (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
30195489c19Sopenharmony_ci        HILOGI("input parameter error.");
30295489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
30395489c19Sopenharmony_ci    }
30495489c19Sopenharmony_ci    return pimpl->SetConnectStrategy(device, strategy);
30595489c19Sopenharmony_ci}
30695489c19Sopenharmony_ci
30795489c19Sopenharmony_ciint HidHost::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
30895489c19Sopenharmony_ci{
30995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
31095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
31195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
31295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
31395489c19Sopenharmony_ci    }
31495489c19Sopenharmony_ci
31595489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
31695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
31795489c19Sopenharmony_ci
31895489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
31995489c19Sopenharmony_ci        HILOGI("input parameter error.");
32095489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
32195489c19Sopenharmony_ci    }
32295489c19Sopenharmony_ci    return pimpl->GetConnectStrategy(device, strategy);
32395489c19Sopenharmony_ci}
32495489c19Sopenharmony_ci
32595489c19Sopenharmony_civoid HidHost::RegisterObserver(std::shared_ptr<HidHostObserver> observer)
32695489c19Sopenharmony_ci{
32795489c19Sopenharmony_ci    HILOGD("enter");
32895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
32995489c19Sopenharmony_ci    pimpl->RegisterObserver(observer);
33095489c19Sopenharmony_ci}
33195489c19Sopenharmony_ci
33295489c19Sopenharmony_civoid HidHost::DeregisterObserver(std::shared_ptr<HidHostObserver> observer)
33395489c19Sopenharmony_ci{
33495489c19Sopenharmony_ci    HILOGD("enter");
33595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
33695489c19Sopenharmony_ci    pimpl->DeregisterObserver(observer);
33795489c19Sopenharmony_ci}
33895489c19Sopenharmony_ci
33995489c19Sopenharmony_civoid HidHost::HidHostVCUnplug(std::string device, uint8_t id, uint16_t size, uint8_t type)
34095489c19Sopenharmony_ci{
34195489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
34295489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
34395489c19Sopenharmony_ci        return;
34495489c19Sopenharmony_ci    }
34595489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
34695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
34795489c19Sopenharmony_ci
34895489c19Sopenharmony_ci    return pimpl->HidHostVCUnplug(device, id, size, type);
34995489c19Sopenharmony_ci}
35095489c19Sopenharmony_ci
35195489c19Sopenharmony_civoid HidHost::HidHostSendData(std::string device, uint8_t id, uint16_t size, uint8_t type)
35295489c19Sopenharmony_ci{
35395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
35495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
35595489c19Sopenharmony_ci        return;
35695489c19Sopenharmony_ci    }
35795489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
35895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
35995489c19Sopenharmony_ci
36095489c19Sopenharmony_ci    return pimpl->HidHostSendData(device, id, size, type);
36195489c19Sopenharmony_ci}
36295489c19Sopenharmony_ci
36395489c19Sopenharmony_civoid HidHost::HidHostSetReport(std::string device, uint8_t type, std::string &report)
36495489c19Sopenharmony_ci{
36595489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
36695489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
36795489c19Sopenharmony_ci        return;
36895489c19Sopenharmony_ci    }
36995489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
37095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
37195489c19Sopenharmony_ci
37295489c19Sopenharmony_ci    return pimpl->HidHostSetReport(device, type, report);
37395489c19Sopenharmony_ci}
37495489c19Sopenharmony_ci
37595489c19Sopenharmony_civoid HidHost::HidHostGetReport(std::string device, uint8_t id, uint16_t size, uint8_t type)
37695489c19Sopenharmony_ci{
37795489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
37895489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
37995489c19Sopenharmony_ci        return;
38095489c19Sopenharmony_ci    }
38195489c19Sopenharmony_ci    sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
38295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
38395489c19Sopenharmony_ci
38495489c19Sopenharmony_ci    return pimpl->HidHostGetReport(device, id, size, type);
38595489c19Sopenharmony_ci}
38695489c19Sopenharmony_ci} // namespace Bluetooth
38795489c19Sopenharmony_ci} // namespace OHOS