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_hfp_ag.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_utils.h"
2395489c19Sopenharmony_ci#include "bluetooth_observer_list.h"
2495489c19Sopenharmony_ci#include "bluetooth_phone_state.h"
2595489c19Sopenharmony_ci#include "i_bluetooth_hfp_ag.h"
2695489c19Sopenharmony_ci#include "bluetooth_hfp_ag_observer_stub.h"
2795489c19Sopenharmony_ci#include "i_bluetooth_host.h"
2895489c19Sopenharmony_ci#include "iservice_registry.h"
2995489c19Sopenharmony_ci#include "system_ability_definition.h"
3095489c19Sopenharmony_ci
3195489c19Sopenharmony_cinamespace OHOS {
3295489c19Sopenharmony_cinamespace Bluetooth {
3395489c19Sopenharmony_cistd::mutex g_hfpProxyMutex;
3495489c19Sopenharmony_ciclass AgServiceObserver : public BluetoothHfpAgObserverStub {
3595489c19Sopenharmony_cipublic:
3695489c19Sopenharmony_ci    explicit AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers) : observers_(observers)
3795489c19Sopenharmony_ci    {
3895489c19Sopenharmony_ci        HILOGD("enter");
3995489c19Sopenharmony_ci    }
4095489c19Sopenharmony_ci    ~AgServiceObserver() override
4195489c19Sopenharmony_ci    {
4295489c19Sopenharmony_ci        HILOGD("enter");
4395489c19Sopenharmony_ci    };
4495489c19Sopenharmony_ci
4595489c19Sopenharmony_ci    void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t cause) override
4695489c19Sopenharmony_ci    {
4795489c19Sopenharmony_ci        HILOGD("hfpAg conn state, device: %{public}s, state: %{public}s, cause: %{public}d",
4895489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), GetProfileConnStateName(state).c_str(), cause);
4995489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
5095489c19Sopenharmony_ci        observers_.ForEach([remoteDevice, state, cause](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
5195489c19Sopenharmony_ci            observer->OnConnectionStateChanged(remoteDevice, state, cause);
5295489c19Sopenharmony_ci        });
5395489c19Sopenharmony_ci    }
5495489c19Sopenharmony_ci
5595489c19Sopenharmony_ci    void OnScoStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t reason) override
5695489c19Sopenharmony_ci    {
5795489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s, state: %{public}u, reason: %{public}u",
5895489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), state, reason);
5995489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
6095489c19Sopenharmony_ci        observers_.ForEach([remoteDevice, state, reason](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
6195489c19Sopenharmony_ci            observer->OnScoStateChanged(remoteDevice, state, reason);
6295489c19Sopenharmony_ci        });
6395489c19Sopenharmony_ci    }
6495489c19Sopenharmony_ci
6595489c19Sopenharmony_ci    void OnActiveDeviceChanged(const BluetoothRawAddress &device) override
6695489c19Sopenharmony_ci    {
6795489c19Sopenharmony_ci        HILOGD("enter, device: %{public}s", GET_ENCRYPT_RAW_ADDR(device));
6895489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
6995489c19Sopenharmony_ci        observers_.ForEach([remoteDevice](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
7095489c19Sopenharmony_ci            observer->OnActiveDeviceChanged(remoteDevice);
7195489c19Sopenharmony_ci        });
7295489c19Sopenharmony_ci    }
7395489c19Sopenharmony_ci
7495489c19Sopenharmony_ci    void OnHfEnhancedDriverSafetyChanged(
7595489c19Sopenharmony_ci        const BluetoothRawAddress &device, int32_t indValue) override
7695489c19Sopenharmony_ci    {
7795489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s, indValue: %{public}d",
7895489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), indValue);
7995489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
8095489c19Sopenharmony_ci        observers_.ForEach([remoteDevice, indValue](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
8195489c19Sopenharmony_ci            observer->OnHfEnhancedDriverSafetyChanged(remoteDevice, indValue);
8295489c19Sopenharmony_ci        });
8395489c19Sopenharmony_ci    }
8495489c19Sopenharmony_ci
8595489c19Sopenharmony_ci    void OnHfpStackChanged(const BluetoothRawAddress &device, int32_t action) override
8695489c19Sopenharmony_ci    {
8795489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s, action: %{public}s",
8895489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), GetUpdateOutputStackActionName(action).c_str());
8995489c19Sopenharmony_ci        BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
9095489c19Sopenharmony_ci        observers_.ForEach([remoteDevice, action](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
9195489c19Sopenharmony_ci            observer->OnHfpStackChanged(remoteDevice, action);
9295489c19Sopenharmony_ci        });
9395489c19Sopenharmony_ci    }
9495489c19Sopenharmony_ci
9595489c19Sopenharmony_ci    void OnVirtualDeviceChanged(int32_t action, std::string address) override
9695489c19Sopenharmony_ci    {
9795489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s, action: %{public}d", GetEncryptAddr(address).c_str(), action);
9895489c19Sopenharmony_ci        observers_.ForEach([action, address](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
9995489c19Sopenharmony_ci            observer->OnVirtualDeviceChanged(action, address);
10095489c19Sopenharmony_ci        });
10195489c19Sopenharmony_ci    }
10295489c19Sopenharmony_ci
10395489c19Sopenharmony_ciprivate:
10495489c19Sopenharmony_ci    BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers_;
10595489c19Sopenharmony_ci    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AgServiceObserver);
10695489c19Sopenharmony_ci};
10795489c19Sopenharmony_ci
10895489c19Sopenharmony_cistd::string HfpAgServiceName = "bluetooth-hfp-ag-server";
10995489c19Sopenharmony_ci
11095489c19Sopenharmony_cistruct HandsFreeAudioGateway::impl {
11195489c19Sopenharmony_ci    impl();
11295489c19Sopenharmony_ci    ~impl();
11395489c19Sopenharmony_ci
11495489c19Sopenharmony_ci    int32_t GetConnectedDevices(std::vector<BluetoothRemoteDevice>& devices)
11595489c19Sopenharmony_ci    {
11695489c19Sopenharmony_ci        HILOGD("enter");
11795489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
11895489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
11995489c19Sopenharmony_ci        std::vector<BluetoothRawAddress> ori;
12095489c19Sopenharmony_ci        int32_t ret = proxy->GetConnectDevices(ori);
12195489c19Sopenharmony_ci        if (ret != BT_NO_ERROR) {
12295489c19Sopenharmony_ci            HILOGE("inner error.");
12395489c19Sopenharmony_ci            return ret;
12495489c19Sopenharmony_ci        }
12595489c19Sopenharmony_ci        for (auto it = ori.begin(); it != ori.end(); it++) {
12695489c19Sopenharmony_ci            devices.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
12795489c19Sopenharmony_ci        }
12895489c19Sopenharmony_ci        return BT_NO_ERROR;
12995489c19Sopenharmony_ci    }
13095489c19Sopenharmony_ci
13195489c19Sopenharmony_ci    std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
13295489c19Sopenharmony_ci    {
13395489c19Sopenharmony_ci        HILOGD("enter");
13495489c19Sopenharmony_ci        std::vector<BluetoothRemoteDevice> remoteDevices;
13595489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
13695489c19Sopenharmony_ci        if (proxy != nullptr) {
13795489c19Sopenharmony_ci            std::vector<BluetoothRawAddress> rawDevices;
13895489c19Sopenharmony_ci            std::vector<int32_t> tmpstates;
13995489c19Sopenharmony_ci            for (int state : states) {
14095489c19Sopenharmony_ci                int32_t value = (int32_t)state;
14195489c19Sopenharmony_ci                tmpstates.push_back(value);
14295489c19Sopenharmony_ci            }
14395489c19Sopenharmony_ci            proxy->GetDevicesByStates(tmpstates, rawDevices);
14495489c19Sopenharmony_ci            for (BluetoothRawAddress rawDevice : rawDevices) {
14595489c19Sopenharmony_ci                BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
14695489c19Sopenharmony_ci                remoteDevices.push_back(remoteDevice);
14795489c19Sopenharmony_ci            }
14895489c19Sopenharmony_ci        }
14995489c19Sopenharmony_ci        return remoteDevices;
15095489c19Sopenharmony_ci    }
15195489c19Sopenharmony_ci
15295489c19Sopenharmony_ci    int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
15395489c19Sopenharmony_ci    {
15495489c19Sopenharmony_ci        HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
15595489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
15695489c19Sopenharmony_ci        if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
15795489c19Sopenharmony_ci            HILOGE("invalid param.");
15895489c19Sopenharmony_ci            return BT_ERR_INVALID_PARAM;
15995489c19Sopenharmony_ci        }
16095489c19Sopenharmony_ci
16195489c19Sopenharmony_ci        return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
16295489c19Sopenharmony_ci    }
16395489c19Sopenharmony_ci
16495489c19Sopenharmony_ci    int32_t Connect(const BluetoothRemoteDevice &device)
16595489c19Sopenharmony_ci    {
16695489c19Sopenharmony_ci        HILOGI("hfp connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
16795489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
16895489c19Sopenharmony_ci        if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
16995489c19Sopenharmony_ci            HILOGE("invalid param.");
17095489c19Sopenharmony_ci            return BT_ERR_INVALID_PARAM;
17195489c19Sopenharmony_ci        }
17295489c19Sopenharmony_ci        return proxy->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
17395489c19Sopenharmony_ci    }
17495489c19Sopenharmony_ci
17595489c19Sopenharmony_ci    int32_t Disconnect(const BluetoothRemoteDevice &device)
17695489c19Sopenharmony_ci    {
17795489c19Sopenharmony_ci        HILOGI("hfp disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
17895489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
17995489c19Sopenharmony_ci        if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
18095489c19Sopenharmony_ci            HILOGE("invalid param.");
18195489c19Sopenharmony_ci            return BT_ERR_INVALID_PARAM;
18295489c19Sopenharmony_ci        }
18395489c19Sopenharmony_ci        return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
18495489c19Sopenharmony_ci    }
18595489c19Sopenharmony_ci
18695489c19Sopenharmony_ci    int GetScoState(const BluetoothRemoteDevice &device)
18795489c19Sopenharmony_ci    {
18895489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
18995489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
19095489c19Sopenharmony_ci        if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
19195489c19Sopenharmony_ci            return proxy->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
19295489c19Sopenharmony_ci        }
19395489c19Sopenharmony_ci        return HFP_AG_SCO_STATE_DISCONNECTED;
19495489c19Sopenharmony_ci    }
19595489c19Sopenharmony_ci
19695489c19Sopenharmony_ci    int32_t ConnectSco(uint8_t callType)
19795489c19Sopenharmony_ci    {
19895489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
19995489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
20095489c19Sopenharmony_ci        return proxy->ConnectSco(callType);
20195489c19Sopenharmony_ci    }
20295489c19Sopenharmony_ci
20395489c19Sopenharmony_ci    int32_t DisconnectSco(uint8_t callType)
20495489c19Sopenharmony_ci    {
20595489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
20695489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
20795489c19Sopenharmony_ci        return proxy->DisconnectSco(callType);
20895489c19Sopenharmony_ci    }
20995489c19Sopenharmony_ci
21095489c19Sopenharmony_ci    bool ConnectSco()
21195489c19Sopenharmony_ci    {
21295489c19Sopenharmony_ci        HILOGD("enter");
21395489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
21495489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, proxy->ConnectSco(), "failed: no proxy");
21595489c19Sopenharmony_ci        return false;
21695489c19Sopenharmony_ci    }
21795489c19Sopenharmony_ci
21895489c19Sopenharmony_ci    bool DisconnectSco()
21995489c19Sopenharmony_ci    {
22095489c19Sopenharmony_ci        HILOGD("enter");
22195489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
22295489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, proxy->DisconnectSco(), "failed: no proxy");
22395489c19Sopenharmony_ci        return false;
22495489c19Sopenharmony_ci    }
22595489c19Sopenharmony_ci
22695489c19Sopenharmony_ci    bool IsValidCallType(uint8_t callType)
22795489c19Sopenharmony_ci    {
22895489c19Sopenharmony_ci        if (callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_CELLULAR) ||
22995489c19Sopenharmony_ci            callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_VIRTUAL) ||
23095489c19Sopenharmony_ci            callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_RECOGNITION)) {
23195489c19Sopenharmony_ci            return true;
23295489c19Sopenharmony_ci        }
23395489c19Sopenharmony_ci        return false;
23495489c19Sopenharmony_ci    }
23595489c19Sopenharmony_ci
23695489c19Sopenharmony_ci    void PhoneStateChanged(BluetoothPhoneState &phoneState)
23795489c19Sopenharmony_ci    {
23895489c19Sopenharmony_ci        HILOGI("numActive: %{public}d, numHeld: %{public}d, callState: %{public}d, type: %{public}d",
23995489c19Sopenharmony_ci            phoneState.GetActiveNum(), phoneState.GetHeldNum(), phoneState.GetCallState(), phoneState.GetCallType());
24095489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
24195489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
24295489c19Sopenharmony_ci        proxy->PhoneStateChanged(phoneState);
24395489c19Sopenharmony_ci    }
24495489c19Sopenharmony_ci
24595489c19Sopenharmony_ci    void ClccResponse(int index, int direction, int status, int mode, bool mpty, std::string number, int type)
24695489c19Sopenharmony_ci    {
24795489c19Sopenharmony_ci        HILOGI("enter, index: %{public}d, direction: %{public}d, status: %{public}d, mode: %{public}d, mpty: "
24895489c19Sopenharmony_ci            "%{public}d, type: %{public}d", index, direction, status, mode, mpty, type);
24995489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
25095489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
25195489c19Sopenharmony_ci        proxy->ClccResponse(index, direction, status, mode, mpty, number, type);
25295489c19Sopenharmony_ci    }
25395489c19Sopenharmony_ci
25495489c19Sopenharmony_ci    bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
25595489c19Sopenharmony_ci    {
25695489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
25795489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
25895489c19Sopenharmony_ci        if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
25995489c19Sopenharmony_ci            return proxy->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
26095489c19Sopenharmony_ci        }
26195489c19Sopenharmony_ci        return false;
26295489c19Sopenharmony_ci    }
26395489c19Sopenharmony_ci
26495489c19Sopenharmony_ci    bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
26595489c19Sopenharmony_ci    {
26695489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
26795489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
26895489c19Sopenharmony_ci        if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
26995489c19Sopenharmony_ci            return proxy->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
27095489c19Sopenharmony_ci        }
27195489c19Sopenharmony_ci        return false;
27295489c19Sopenharmony_ci    }
27395489c19Sopenharmony_ci
27495489c19Sopenharmony_ci    bool SetActiveDevice(const BluetoothRemoteDevice &device)
27595489c19Sopenharmony_ci    {
27695489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
27795489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
27895489c19Sopenharmony_ci        if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
27995489c19Sopenharmony_ci            return proxy->SetActiveDevice(BluetoothRawAddress(device.GetDeviceAddr()));
28095489c19Sopenharmony_ci        }
28195489c19Sopenharmony_ci        return false;
28295489c19Sopenharmony_ci    }
28395489c19Sopenharmony_ci
28495489c19Sopenharmony_ci    bool IntoMock(const BluetoothRemoteDevice &device, int state)
28595489c19Sopenharmony_ci    {
28695489c19Sopenharmony_ci        HILOGD("enter");
28795489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
28895489c19Sopenharmony_ci        if (proxy != nullptr && IS_BT_ENABLED()) {
28995489c19Sopenharmony_ci            return proxy->IntoMock(BluetoothRawAddress(device.GetDeviceAddr()), state);
29095489c19Sopenharmony_ci        }
29195489c19Sopenharmony_ci        return false;
29295489c19Sopenharmony_ci    }
29395489c19Sopenharmony_ci
29495489c19Sopenharmony_ci    bool SendNoCarrier(const BluetoothRemoteDevice &device)
29595489c19Sopenharmony_ci    {
29695489c19Sopenharmony_ci        HILOGD("enter");
29795489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
29895489c19Sopenharmony_ci        if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
29995489c19Sopenharmony_ci            return proxy->SendNoCarrier(BluetoothRawAddress(device.GetDeviceAddr()));
30095489c19Sopenharmony_ci        }
30195489c19Sopenharmony_ci        return false;
30295489c19Sopenharmony_ci    }
30395489c19Sopenharmony_ci
30495489c19Sopenharmony_ci    BluetoothRemoteDevice GetActiveDevice()
30595489c19Sopenharmony_ci    {
30695489c19Sopenharmony_ci        HILOGD("enter");
30795489c19Sopenharmony_ci        BluetoothRemoteDevice device;
30895489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
30995489c19Sopenharmony_ci        if (proxy != nullptr) {
31095489c19Sopenharmony_ci            std::string address = proxy->GetActiveDevice();
31195489c19Sopenharmony_ci            BluetoothRemoteDevice remoteDevice(address, 0);
31295489c19Sopenharmony_ci            device = remoteDevice;
31395489c19Sopenharmony_ci        }
31495489c19Sopenharmony_ci        return device;
31595489c19Sopenharmony_ci    }
31695489c19Sopenharmony_ci
31795489c19Sopenharmony_ci    int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
31895489c19Sopenharmony_ci    {
31995489c19Sopenharmony_ci        HILOGD("enter");
32095489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
32195489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
32295489c19Sopenharmony_ci        return proxy->SetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
32395489c19Sopenharmony_ci    }
32495489c19Sopenharmony_ci
32595489c19Sopenharmony_ci    int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
32695489c19Sopenharmony_ci    {
32795489c19Sopenharmony_ci        HILOGD("enter");
32895489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
32995489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
33095489c19Sopenharmony_ci        return proxy->GetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
33195489c19Sopenharmony_ci    }
33295489c19Sopenharmony_ci
33395489c19Sopenharmony_ci    int IsInbandRingingEnabled(bool &isEnabled) const
33495489c19Sopenharmony_ci    {
33595489c19Sopenharmony_ci        HILOGD("enter");
33695489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
33795489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
33895489c19Sopenharmony_ci        return proxy->IsInbandRingingEnabled(isEnabled);
33995489c19Sopenharmony_ci    }
34095489c19Sopenharmony_ci
34195489c19Sopenharmony_ci    void CallDetailsChanged(int callId, int callState)
34295489c19Sopenharmony_ci    {
34395489c19Sopenharmony_ci        HILOGD("enter");
34495489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
34595489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
34695489c19Sopenharmony_ci        proxy->CallDetailsChanged(callId, callState);
34795489c19Sopenharmony_ci    }
34895489c19Sopenharmony_ci
34995489c19Sopenharmony_ci    int IsVgsSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
35095489c19Sopenharmony_ci    {
35195489c19Sopenharmony_ci        HILOGD("enter");
35295489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
35395489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
35495489c19Sopenharmony_ci        return proxy->IsVgsSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported);
35595489c19Sopenharmony_ci    }
35695489c19Sopenharmony_ci
35795489c19Sopenharmony_ci    void EnableBtCallLog(bool state)
35895489c19Sopenharmony_ci    {
35995489c19Sopenharmony_ci        HILOGD("enter");
36095489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
36195489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
36295489c19Sopenharmony_ci        proxy->EnableBtCallLog(state);
36395489c19Sopenharmony_ci    }
36495489c19Sopenharmony_ci
36595489c19Sopenharmony_ci    void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
36695489c19Sopenharmony_ci    {
36795489c19Sopenharmony_ci        HILOGD("enter");
36895489c19Sopenharmony_ci        observers_.Register(observer);
36995489c19Sopenharmony_ci    }
37095489c19Sopenharmony_ci
37195489c19Sopenharmony_ci    void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
37295489c19Sopenharmony_ci    {
37395489c19Sopenharmony_ci        HILOGD("enter");
37495489c19Sopenharmony_ci        observers_.Deregister(observer);
37595489c19Sopenharmony_ci        HILOGI("end");
37695489c19Sopenharmony_ci    }
37795489c19Sopenharmony_ci
37895489c19Sopenharmony_ci    int32_t profileRegisterId = 0;
37995489c19Sopenharmony_ciprivate:
38095489c19Sopenharmony_ci    const static int HFP_AG_SLC_STATE_DISCONNECTED = static_cast<int>(BTConnectState::DISCONNECTED);
38195489c19Sopenharmony_ci    const static int HFP_AG_SCO_STATE_DISCONNECTED = 3;
38295489c19Sopenharmony_ci
38395489c19Sopenharmony_ci    BluetoothObserverList<HandsFreeAudioGatewayObserver> observers_;
38495489c19Sopenharmony_ci    sptr<AgServiceObserver> serviceObserver_;
38595489c19Sopenharmony_ci};
38695489c19Sopenharmony_ci
38795489c19Sopenharmony_ciHandsFreeAudioGateway::impl::impl()
38895489c19Sopenharmony_ci{
38995489c19Sopenharmony_ci    serviceObserver_ = new AgServiceObserver(observers_);
39095489c19Sopenharmony_ci    profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_HFP_AG,
39195489c19Sopenharmony_ci        [this](sptr<IRemoteObject> remote) {
39295489c19Sopenharmony_ci        sptr<IBluetoothHfpAg> proxy = iface_cast<IBluetoothHfpAg>(remote);
39395489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
39495489c19Sopenharmony_ci        proxy->RegisterObserver(serviceObserver_);
39595489c19Sopenharmony_ci    });
39695489c19Sopenharmony_ci}
39795489c19Sopenharmony_ci
39895489c19Sopenharmony_ciHandsFreeAudioGateway::impl::~impl()
39995489c19Sopenharmony_ci{
40095489c19Sopenharmony_ci    HILOGD("enter");
40195489c19Sopenharmony_ci    BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
40295489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
40395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
40495489c19Sopenharmony_ci    proxy->DeregisterObserver(serviceObserver_);
40595489c19Sopenharmony_ci}
40695489c19Sopenharmony_ci
40795489c19Sopenharmony_ciHandsFreeAudioGateway *HandsFreeAudioGateway::GetProfile()
40895489c19Sopenharmony_ci{
40995489c19Sopenharmony_ci    HILOGD("enter");
41095489c19Sopenharmony_ci#ifdef DTFUZZ_TEST
41195489c19Sopenharmony_ci    static BluetoothNoDestructor<HandsFreeAudioGateway> instance;
41295489c19Sopenharmony_ci    return instance.get();
41395489c19Sopenharmony_ci#else
41495489c19Sopenharmony_ci    static HandsFreeAudioGateway instance;
41595489c19Sopenharmony_ci    return &instance;
41695489c19Sopenharmony_ci#endif
41795489c19Sopenharmony_ci}
41895489c19Sopenharmony_ci
41995489c19Sopenharmony_ciHandsFreeAudioGateway::HandsFreeAudioGateway()
42095489c19Sopenharmony_ci{
42195489c19Sopenharmony_ci    HILOGD("enter");
42295489c19Sopenharmony_ci    pimpl = std::make_unique<impl>();
42395489c19Sopenharmony_ci}
42495489c19Sopenharmony_ci
42595489c19Sopenharmony_ciHandsFreeAudioGateway::~HandsFreeAudioGateway()
42695489c19Sopenharmony_ci{
42795489c19Sopenharmony_ci    HILOGD("enter");
42895489c19Sopenharmony_ci}
42995489c19Sopenharmony_ci
43095489c19Sopenharmony_cistd::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetConnectedDevices() const
43195489c19Sopenharmony_ci{
43295489c19Sopenharmony_ci    std::vector<BluetoothRemoteDevice> devices;
43395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
43495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
43595489c19Sopenharmony_ci        return devices;
43695489c19Sopenharmony_ci    }
43795489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
43895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, devices, "failed: no proxy");
43995489c19Sopenharmony_ci
44095489c19Sopenharmony_ci    pimpl->GetConnectedDevices(devices);
44195489c19Sopenharmony_ci    return devices;
44295489c19Sopenharmony_ci}
44395489c19Sopenharmony_ci
44495489c19Sopenharmony_ciint32_t HandsFreeAudioGateway::GetConnectedDevices(std::vector<BluetoothRemoteDevice> &devices)
44595489c19Sopenharmony_ci{
44695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
44795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
44895489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
44995489c19Sopenharmony_ci    }
45095489c19Sopenharmony_ci
45195489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
45295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
45395489c19Sopenharmony_ci
45495489c19Sopenharmony_ci    return pimpl->GetConnectedDevices(devices);
45595489c19Sopenharmony_ci}
45695489c19Sopenharmony_ci
45795489c19Sopenharmony_cistd::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetDevicesByStates(std::vector<int> states)
45895489c19Sopenharmony_ci{
45995489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
46095489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
46195489c19Sopenharmony_ci        return std::vector<BluetoothRemoteDevice>();
46295489c19Sopenharmony_ci    }
46395489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
46495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
46595489c19Sopenharmony_ci
46695489c19Sopenharmony_ci    return pimpl->GetDevicesByStates(states);
46795489c19Sopenharmony_ci}
46895489c19Sopenharmony_ci
46995489c19Sopenharmony_ciint32_t HandsFreeAudioGateway::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
47095489c19Sopenharmony_ci{
47195489c19Sopenharmony_ci    HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
47295489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
47395489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
47495489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
47595489c19Sopenharmony_ci    }
47695489c19Sopenharmony_ci
47795489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
47895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
47995489c19Sopenharmony_ci
48095489c19Sopenharmony_ci    return pimpl->GetDeviceState(device, state);
48195489c19Sopenharmony_ci}
48295489c19Sopenharmony_ci
48395489c19Sopenharmony_ciint32_t HandsFreeAudioGateway::Connect(const BluetoothRemoteDevice &device)
48495489c19Sopenharmony_ci{
48595489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
48695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
48795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
48895489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
48995489c19Sopenharmony_ci    }
49095489c19Sopenharmony_ci
49195489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
49295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
49395489c19Sopenharmony_ci
49495489c19Sopenharmony_ci    return pimpl->Connect(device);
49595489c19Sopenharmony_ci}
49695489c19Sopenharmony_ci
49795489c19Sopenharmony_ciint32_t HandsFreeAudioGateway::Disconnect(const BluetoothRemoteDevice &device)
49895489c19Sopenharmony_ci{
49995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
50095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
50195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
50295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
50395489c19Sopenharmony_ci    }
50495489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
50595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
50695489c19Sopenharmony_ci
50795489c19Sopenharmony_ci    return pimpl->Disconnect(device);
50895489c19Sopenharmony_ci}
50995489c19Sopenharmony_ci
51095489c19Sopenharmony_ciint HandsFreeAudioGateway::GetScoState(const BluetoothRemoteDevice &device) const
51195489c19Sopenharmony_ci{
51295489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
51395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
51495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
51595489c19Sopenharmony_ci        return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
51695489c19Sopenharmony_ci    }
51795489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
51895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
51995489c19Sopenharmony_ci        static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED), "failed: no proxy");
52095489c19Sopenharmony_ci
52195489c19Sopenharmony_ci    return pimpl->GetScoState(device);
52295489c19Sopenharmony_ci}
52395489c19Sopenharmony_ci
52495489c19Sopenharmony_ciint32_t HandsFreeAudioGateway::ConnectSco(uint8_t callType)
52595489c19Sopenharmony_ci{
52695489c19Sopenharmony_ci    HILOGI("enter, callType: %{public}d", callType);
52795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
52895489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
52995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
53095489c19Sopenharmony_ci        "hfpAG proxy is nullptr.");
53195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
53295489c19Sopenharmony_ci        "connect sco call type error.");
53395489c19Sopenharmony_ci    return pimpl->ConnectSco(callType);
53495489c19Sopenharmony_ci}
53595489c19Sopenharmony_ci
53695489c19Sopenharmony_ciint32_t HandsFreeAudioGateway::DisconnectSco(uint8_t callType)
53795489c19Sopenharmony_ci{
53895489c19Sopenharmony_ci    HILOGI("enter, callType: %{public}d", callType);
53995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
54095489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
54195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
54295489c19Sopenharmony_ci        "hfpAG proxy is nullptr.");
54395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
54495489c19Sopenharmony_ci        "disconnect sco call type error.");
54595489c19Sopenharmony_ci    return pimpl->DisconnectSco(callType);
54695489c19Sopenharmony_ci}
54795489c19Sopenharmony_ci
54895489c19Sopenharmony_cibool HandsFreeAudioGateway::ConnectSco()
54995489c19Sopenharmony_ci{
55095489c19Sopenharmony_ci    return true;
55195489c19Sopenharmony_ci}
55295489c19Sopenharmony_ci
55395489c19Sopenharmony_cibool HandsFreeAudioGateway::DisconnectSco()
55495489c19Sopenharmony_ci{
55595489c19Sopenharmony_ci    return true;
55695489c19Sopenharmony_ci}
55795489c19Sopenharmony_ci
55895489c19Sopenharmony_civoid HandsFreeAudioGateway::PhoneStateChanged(
55995489c19Sopenharmony_ci    int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
56095489c19Sopenharmony_ci{
56195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
56295489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
56395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr.");
56495489c19Sopenharmony_ci
56595489c19Sopenharmony_ci    BluetoothPhoneState phoneState;
56695489c19Sopenharmony_ci    phoneState.SetActiveNum(numActive);
56795489c19Sopenharmony_ci    phoneState.SetHeldNum(numHeld);
56895489c19Sopenharmony_ci    phoneState.SetCallState(callState);
56995489c19Sopenharmony_ci    phoneState.SetNumber(number);
57095489c19Sopenharmony_ci    phoneState.SetCallType(type);
57195489c19Sopenharmony_ci    phoneState.SetName(name);
57295489c19Sopenharmony_ci    pimpl->PhoneStateChanged(phoneState);
57395489c19Sopenharmony_ci}
57495489c19Sopenharmony_ci
57595489c19Sopenharmony_civoid HandsFreeAudioGateway::ClccResponse(
57695489c19Sopenharmony_ci    int index, int direction, int status, int mode, bool mpty, const std::string &number, int type)
57795489c19Sopenharmony_ci{
57895489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
57995489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
58095489c19Sopenharmony_ci        return;
58195489c19Sopenharmony_ci    }
58295489c19Sopenharmony_ci
58395489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
58495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
58595489c19Sopenharmony_ci
58695489c19Sopenharmony_ci    pimpl->ClccResponse(index, direction, status, mode, mpty, number, type);
58795489c19Sopenharmony_ci}
58895489c19Sopenharmony_ci
58995489c19Sopenharmony_cibool HandsFreeAudioGateway::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
59095489c19Sopenharmony_ci{
59195489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
59295489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
59395489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
59495489c19Sopenharmony_ci        return false;
59595489c19Sopenharmony_ci    }
59695489c19Sopenharmony_ci
59795489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
59895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
59995489c19Sopenharmony_ci
60095489c19Sopenharmony_ci    return pimpl->OpenVoiceRecognition(device);
60195489c19Sopenharmony_ci}
60295489c19Sopenharmony_ci
60395489c19Sopenharmony_cibool HandsFreeAudioGateway::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
60495489c19Sopenharmony_ci{
60595489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
60695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
60795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
60895489c19Sopenharmony_ci        return false;
60995489c19Sopenharmony_ci    }
61095489c19Sopenharmony_ci
61195489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
61295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
61395489c19Sopenharmony_ci
61495489c19Sopenharmony_ci    return pimpl->CloseVoiceRecognition(device);
61595489c19Sopenharmony_ci}
61695489c19Sopenharmony_ci
61795489c19Sopenharmony_cibool HandsFreeAudioGateway::SetActiveDevice(const BluetoothRemoteDevice &device)
61895489c19Sopenharmony_ci{
61995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
62095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
62195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
62295489c19Sopenharmony_ci        return false;
62395489c19Sopenharmony_ci    }
62495489c19Sopenharmony_ci
62595489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
62695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
62795489c19Sopenharmony_ci
62895489c19Sopenharmony_ci    return pimpl->SetActiveDevice(device);
62995489c19Sopenharmony_ci}
63095489c19Sopenharmony_ci
63195489c19Sopenharmony_ciBluetoothRemoteDevice HandsFreeAudioGateway::GetActiveDevice() const
63295489c19Sopenharmony_ci{
63395489c19Sopenharmony_ci    BluetoothRemoteDevice device;
63495489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
63595489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
63695489c19Sopenharmony_ci        return device;
63795489c19Sopenharmony_ci    }
63895489c19Sopenharmony_ci
63995489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
64095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, device, "failed: no proxy");
64195489c19Sopenharmony_ci
64295489c19Sopenharmony_ci    device = pimpl->GetActiveDevice();
64395489c19Sopenharmony_ci    return device;
64495489c19Sopenharmony_ci}
64595489c19Sopenharmony_ci
64695489c19Sopenharmony_ciint HandsFreeAudioGateway::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
64795489c19Sopenharmony_ci{
64895489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
64995489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
65095489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
65195489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
65295489c19Sopenharmony_ci    }
65395489c19Sopenharmony_ci
65495489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
65595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
65695489c19Sopenharmony_ci
65795489c19Sopenharmony_ci    if ((!device.IsValidBluetoothRemoteDevice()) || (
65895489c19Sopenharmony_ci        (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
65995489c19Sopenharmony_ci        (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
66095489c19Sopenharmony_ci        HILOGI("input parameter error.");
66195489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
66295489c19Sopenharmony_ci    }
66395489c19Sopenharmony_ci    return pimpl->SetConnectStrategy(device, strategy);
66495489c19Sopenharmony_ci}
66595489c19Sopenharmony_ci
66695489c19Sopenharmony_ciint HandsFreeAudioGateway::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
66795489c19Sopenharmony_ci{
66895489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
66995489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
67095489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
67195489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
67295489c19Sopenharmony_ci    }
67395489c19Sopenharmony_ci
67495489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
67595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
67695489c19Sopenharmony_ci
67795489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
67895489c19Sopenharmony_ci        HILOGI("input parameter error.");
67995489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
68095489c19Sopenharmony_ci    }
68195489c19Sopenharmony_ci    return pimpl->GetConnectStrategy(device, strategy);
68295489c19Sopenharmony_ci}
68395489c19Sopenharmony_ci
68495489c19Sopenharmony_ciint HandsFreeAudioGateway::IsInbandRingingEnabled(bool &isEnabled) const
68595489c19Sopenharmony_ci{
68695489c19Sopenharmony_ci    HILOGI("enter");
68795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
68895489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
68995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "hfpAG proxy is nullptr");
69095489c19Sopenharmony_ci    return pimpl->IsInbandRingingEnabled(isEnabled);
69195489c19Sopenharmony_ci}
69295489c19Sopenharmony_ci
69395489c19Sopenharmony_civoid HandsFreeAudioGateway::CallDetailsChanged(int callId, int callState)
69495489c19Sopenharmony_ci{
69595489c19Sopenharmony_ci    HILOGI("enter, callId: %{public}d, callState: %{public}d", callId, callState);
69695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
69795489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
69895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr");
69995489c19Sopenharmony_ci    pimpl->CallDetailsChanged(callId, callState);
70095489c19Sopenharmony_ci}
70195489c19Sopenharmony_ci
70295489c19Sopenharmony_civoid HandsFreeAudioGateway::EnableBtCallLog(bool state)
70395489c19Sopenharmony_ci{
70495489c19Sopenharmony_ci    HILOGI("enter");
70595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
70695489c19Sopenharmony_ci    pimpl->EnableBtCallLog(state);
70795489c19Sopenharmony_ci}
70895489c19Sopenharmony_ci
70995489c19Sopenharmony_ciint HandsFreeAudioGateway::IsVgsSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
71095489c19Sopenharmony_ci{
71195489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
71295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
71395489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
71495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
71595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "input parameter error.");
71695489c19Sopenharmony_ci    return pimpl->IsVgsSupported(device, isSupported);
71795489c19Sopenharmony_ci}
71895489c19Sopenharmony_ci
71995489c19Sopenharmony_civoid HandsFreeAudioGateway::GetVirtualDeviceList(std::vector<std::string> &devices) const
72095489c19Sopenharmony_ci{
72195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
72295489c19Sopenharmony_ci    sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
72395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr");
72495489c19Sopenharmony_ci    proxy->GetVirtualDeviceList(devices);
72595489c19Sopenharmony_ci}
72695489c19Sopenharmony_ci
72795489c19Sopenharmony_civoid HandsFreeAudioGateway::RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
72895489c19Sopenharmony_ci{
72995489c19Sopenharmony_ci    HILOGD("enter");
73095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
73195489c19Sopenharmony_ci    pimpl->RegisterObserver(observer);
73295489c19Sopenharmony_ci}
73395489c19Sopenharmony_ci
73495489c19Sopenharmony_civoid HandsFreeAudioGateway::DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
73595489c19Sopenharmony_ci{
73695489c19Sopenharmony_ci    HILOGD("enter");
73795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
73895489c19Sopenharmony_ci    pimpl->DeregisterObserver(observer);
73995489c19Sopenharmony_ci}
74095489c19Sopenharmony_ci}  // namespace Bluetooth
74195489c19Sopenharmony_ci}  // namespace OHOS