195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2023 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_audio_manager.h"
1795489c19Sopenharmony_ci#include "bluetooth_errorcode.h"
1895489c19Sopenharmony_ci#include "bluetooth_log.h"
1995489c19Sopenharmony_ci#include "bluetooth_host.h"
2095489c19Sopenharmony_ci#include "i_bluetooth_audio_manager.h"
2195489c19Sopenharmony_ci#include "i_bluetooth_host.h"
2295489c19Sopenharmony_ci#include "bluetooth_utils.h"
2395489c19Sopenharmony_ci#include "bluetooth_profile_manager.h"
2495489c19Sopenharmony_ci
2595489c19Sopenharmony_cinamespace OHOS {
2695489c19Sopenharmony_cinamespace Bluetooth {
2795489c19Sopenharmony_cistruct BluetoothAudioManager::impl {
2895489c19Sopenharmony_ci    impl();
2995489c19Sopenharmony_ci    int EnableWearDetection(const std::string &deviceId);
3095489c19Sopenharmony_ci    int DisableWearDetection(const std::string &deviceId);
3195489c19Sopenharmony_ci    int GetWearDetectionState(const std::string &deviceId, int32_t &ability);
3295489c19Sopenharmony_ci    int IsDeviceWearing(const BluetoothRemoteDevice &device);
3395489c19Sopenharmony_ci    int IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported);
3495489c19Sopenharmony_ci    int SendDeviceSelection(const BluetoothRemoteDevice &device, int useA2dp, int useHfp, int userSelection);
3595489c19Sopenharmony_ci};
3695489c19Sopenharmony_ci
3795489c19Sopenharmony_ciBluetoothAudioManager::impl::impl()
3895489c19Sopenharmony_ci{}
3995489c19Sopenharmony_ci
4095489c19Sopenharmony_ciBluetoothAudioManager::BluetoothAudioManager():pimpl(std::make_unique<impl>())
4195489c19Sopenharmony_ci{}
4295489c19Sopenharmony_ci
4395489c19Sopenharmony_ciint BluetoothAudioManager::impl::EnableWearDetection(const std::string &deviceId)
4495489c19Sopenharmony_ci{
4595489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
4695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
4795489c19Sopenharmony_ci    return proxy->EnableWearDetection(deviceId);
4895489c19Sopenharmony_ci}
4995489c19Sopenharmony_ci
5095489c19Sopenharmony_ciint BluetoothAudioManager::impl::DisableWearDetection(const std::string &deviceId)
5195489c19Sopenharmony_ci{
5295489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
5395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
5495489c19Sopenharmony_ci    return proxy->DisableWearDetection(deviceId);
5595489c19Sopenharmony_ci}
5695489c19Sopenharmony_ci
5795489c19Sopenharmony_ciint BluetoothAudioManager::impl::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
5895489c19Sopenharmony_ci{
5995489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
6095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
6195489c19Sopenharmony_ci    return proxy->GetWearDetectionState(deviceId, ability);
6295489c19Sopenharmony_ci}
6395489c19Sopenharmony_ci
6495489c19Sopenharmony_ciint BluetoothAudioManager::impl::IsDeviceWearing(const BluetoothRemoteDevice &device)
6595489c19Sopenharmony_ci{
6695489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
6795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
6895489c19Sopenharmony_ci    return proxy->IsDeviceWearing(BluetoothRawAddress(device.GetDeviceAddr()));
6995489c19Sopenharmony_ci}
7095489c19Sopenharmony_ci
7195489c19Sopenharmony_ciint BluetoothAudioManager::impl::IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)
7295489c19Sopenharmony_ci{
7395489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
7495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
7595489c19Sopenharmony_ci    return proxy->IsWearDetectionSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported);
7695489c19Sopenharmony_ci}
7795489c19Sopenharmony_ci
7895489c19Sopenharmony_ciint BluetoothAudioManager::impl::SendDeviceSelection(const BluetoothRemoteDevice &device,
7995489c19Sopenharmony_ci    int useA2dp, int useHfp, int userSelection)
8095489c19Sopenharmony_ci{
8195489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
8295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
8395489c19Sopenharmony_ci    return proxy->SendDeviceSelection(BluetoothRawAddress(device.GetDeviceAddr()), useA2dp, useHfp, userSelection);
8495489c19Sopenharmony_ci}
8595489c19Sopenharmony_ci
8695489c19Sopenharmony_ciint BluetoothAudioManager::EnableWearDetection(const std::string &deviceId)
8795489c19Sopenharmony_ci{
8895489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
8995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
9095489c19Sopenharmony_ci    return pimpl->EnableWearDetection(deviceId);
9195489c19Sopenharmony_ci}
9295489c19Sopenharmony_ci
9395489c19Sopenharmony_ciint BluetoothAudioManager::DisableWearDetection(const std::string &deviceId)
9495489c19Sopenharmony_ci{
9595489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
9695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
9795489c19Sopenharmony_ci    return pimpl->DisableWearDetection(deviceId);
9895489c19Sopenharmony_ci}
9995489c19Sopenharmony_ci
10095489c19Sopenharmony_ciint BluetoothAudioManager::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
10195489c19Sopenharmony_ci{
10295489c19Sopenharmony_ci    sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
10395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
10495489c19Sopenharmony_ci    return pimpl->GetWearDetectionState(deviceId, ability);
10595489c19Sopenharmony_ci}
10695489c19Sopenharmony_ci
10795489c19Sopenharmony_ciint BluetoothAudioManager::IsDeviceWearing(const BluetoothRemoteDevice &device)
10895489c19Sopenharmony_ci{
10995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
11095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
11195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_STATE, "input parameter error");
11295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_INVALID_STATE, "pimpl is null");
11395489c19Sopenharmony_ci
11495489c19Sopenharmony_ci    return pimpl->IsDeviceWearing(device);
11595489c19Sopenharmony_ci}
11695489c19Sopenharmony_ci
11795489c19Sopenharmony_ciint BluetoothAudioManager::IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)
11895489c19Sopenharmony_ci{
11995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
12095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
12195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
12295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
12395489c19Sopenharmony_ci    }
12495489c19Sopenharmony_ci
12595489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
12695489c19Sopenharmony_ci        HILOGE("input parameter error.");
12795489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
12895489c19Sopenharmony_ci    }
12995489c19Sopenharmony_ci
13095489c19Sopenharmony_ci    if (pimpl == nullptr) {
13195489c19Sopenharmony_ci        HILOGE("pimpl is null");
13295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
13395489c19Sopenharmony_ci    }
13495489c19Sopenharmony_ci    return pimpl->IsWearDetectionSupported(device, isSupported);
13595489c19Sopenharmony_ci}
13695489c19Sopenharmony_ci
13795489c19Sopenharmony_ciint BluetoothAudioManager::SendDeviceSelection(const BluetoothRemoteDevice &device,
13895489c19Sopenharmony_ci    int useA2dp, int useHfp, int userSelection) const
13995489c19Sopenharmony_ci{
14095489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s, useA2dp: %{public}d, useHfp: %{public}d, userSelection:%{public}d",
14195489c19Sopenharmony_ci        GET_ENCRYPT_ADDR(device), useA2dp, useHfp, userSelection);
14295489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
14395489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
14495489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
14595489c19Sopenharmony_ci    }
14695489c19Sopenharmony_ci
14795489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
14895489c19Sopenharmony_ci        HILOGE("input parameter error.");
14995489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
15095489c19Sopenharmony_ci    }
15195489c19Sopenharmony_ci
15295489c19Sopenharmony_ci    if (pimpl == nullptr) {
15395489c19Sopenharmony_ci        HILOGE("pimpl is null");
15495489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
15595489c19Sopenharmony_ci    }
15695489c19Sopenharmony_ci    return pimpl->SendDeviceSelection(device, useA2dp, useHfp, userSelection);
15795489c19Sopenharmony_ci}
15895489c19Sopenharmony_ci
15995489c19Sopenharmony_ciBluetoothAudioManager &BluetoothAudioManager::GetInstance()
16095489c19Sopenharmony_ci{
16195489c19Sopenharmony_ci#ifdef DTFUZZ_TEST
16295489c19Sopenharmony_ci    static BluetoothNoDestructor<BluetoothAudioManager> instance;
16395489c19Sopenharmony_ci    return *instance;
16495489c19Sopenharmony_ci#else
16595489c19Sopenharmony_ci    static BluetoothAudioManager instance;
16695489c19Sopenharmony_ci    return instance;
16795489c19Sopenharmony_ci#endif
16895489c19Sopenharmony_ci}
16995489c19Sopenharmony_ci
17095489c19Sopenharmony_ci}  // namespace Bluetooth
17195489c19Sopenharmony_ci}  // namespace OHOS
172