1e1c44949Sopenharmony_ci/*
2e1c44949Sopenharmony_ci * Copyright (C) 2021-2024 Huawei Device Co., Ltd.
3e1c44949Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e1c44949Sopenharmony_ci * you may not use this file except in compliance with the License.
5e1c44949Sopenharmony_ci * You may obtain a copy of the License at
6e1c44949Sopenharmony_ci *
7e1c44949Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e1c44949Sopenharmony_ci *
9e1c44949Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e1c44949Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e1c44949Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e1c44949Sopenharmony_ci * See the License for the specific language governing permissions and
13e1c44949Sopenharmony_ci * limitations under the License.
14e1c44949Sopenharmony_ci */
15e1c44949Sopenharmony_ci
16e1c44949Sopenharmony_ci#include "audio_device_manager.h"
17e1c44949Sopenharmony_ci
18e1c44949Sopenharmony_ci#include "audio_control_manager.h"
19e1c44949Sopenharmony_ci#include "bluetooth_call_manager.h"
20e1c44949Sopenharmony_ci#include "bluetooth_device_state.h"
21e1c44949Sopenharmony_ci#include "call_ability_report_proxy.h"
22e1c44949Sopenharmony_ci#include "call_object_manager.h"
23e1c44949Sopenharmony_ci#include "earpiece_device_state.h"
24e1c44949Sopenharmony_ci#include "inactive_device_state.h"
25e1c44949Sopenharmony_ci#include "speaker_device_state.h"
26e1c44949Sopenharmony_ci#include "telephony_log_wrapper.h"
27e1c44949Sopenharmony_ci#include "wired_headset_device_state.h"
28e1c44949Sopenharmony_ci#include "distributed_call_manager.h"
29e1c44949Sopenharmony_ci#include "audio_system_manager.h"
30e1c44949Sopenharmony_ci#include "audio_device_info.h"
31e1c44949Sopenharmony_ci#include "distributed_communication_manager.h"
32e1c44949Sopenharmony_ci
33e1c44949Sopenharmony_cinamespace OHOS {
34e1c44949Sopenharmony_cinamespace Telephony {
35e1c44949Sopenharmony_ciusing namespace AudioStandard;
36e1c44949Sopenharmony_ci
37e1c44949Sopenharmony_ciconstexpr int32_t DEVICE_ADDR_LEN = 7;
38e1c44949Sopenharmony_ciconstexpr int32_t ADDR_HEAD_VALID_LEN = 5;
39e1c44949Sopenharmony_ciconstexpr int32_t ADDR_TAIL_VALID_LEN = 2;
40e1c44949Sopenharmony_cibool AudioDeviceManager::isBtScoDevEnable_ = false;
41e1c44949Sopenharmony_cibool AudioDeviceManager::isDCallDevEnable_ = false;
42e1c44949Sopenharmony_cibool AudioDeviceManager::isSpeakerAvailable_ = true; // default available
43e1c44949Sopenharmony_cibool AudioDeviceManager::isEarpieceAvailable_ = true;
44e1c44949Sopenharmony_cibool AudioDeviceManager::isUpdateEarpieceDevice_ = false;
45e1c44949Sopenharmony_cibool AudioDeviceManager::isWiredHeadsetConnected_ = false;
46e1c44949Sopenharmony_cibool AudioDeviceManager::isBtScoConnected_ = false;
47e1c44949Sopenharmony_cibool AudioDeviceManager::isDCallDevConnected_ = false;
48e1c44949Sopenharmony_ci
49e1c44949Sopenharmony_ciAudioDeviceManager::AudioDeviceManager()
50e1c44949Sopenharmony_ci    : audioDeviceType_(AudioDeviceType::DEVICE_UNKNOWN), currentAudioDevice_(nullptr), isAudioActivated_(false)
51e1c44949Sopenharmony_ci{}
52e1c44949Sopenharmony_ci
53e1c44949Sopenharmony_ciAudioDeviceManager::~AudioDeviceManager()
54e1c44949Sopenharmony_ci{
55e1c44949Sopenharmony_ci    memberFuncMap_.clear();
56e1c44949Sopenharmony_ci}
57e1c44949Sopenharmony_ci
58e1c44949Sopenharmony_civoid AudioDeviceManager::Init()
59e1c44949Sopenharmony_ci{
60e1c44949Sopenharmony_ci    memberFuncMap_[AudioEvent::ENABLE_DEVICE_EARPIECE] = [this]() { return EnableEarpiece(); };
61e1c44949Sopenharmony_ci    memberFuncMap_[AudioEvent::ENABLE_DEVICE_SPEAKER] = [this]() { return EnableSpeaker(); };
62e1c44949Sopenharmony_ci    memberFuncMap_[AudioEvent::ENABLE_DEVICE_WIRED_HEADSET] = [this]() { return EnableWiredHeadset(); };
63e1c44949Sopenharmony_ci    memberFuncMap_[AudioEvent::ENABLE_DEVICE_BLUETOOTH] = [this]() { return EnableBtSco(); };
64e1c44949Sopenharmony_ci    currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
65e1c44949Sopenharmony_ci    if (currentAudioDevice_ == nullptr) {
66e1c44949Sopenharmony_ci        TELEPHONY_LOGE("current audio device nullptr");
67e1c44949Sopenharmony_ci    }
68e1c44949Sopenharmony_ci    if (memset_s(&info_, sizeof(AudioDeviceInfo), 0, sizeof(AudioDeviceInfo)) != EOK) {
69e1c44949Sopenharmony_ci        TELEPHONY_LOGE("memset_s address fail");
70e1c44949Sopenharmony_ci        return;
71e1c44949Sopenharmony_ci    }
72e1c44949Sopenharmony_ci    AudioDevice speaker = {
73e1c44949Sopenharmony_ci        .deviceType = AudioDeviceType::DEVICE_SPEAKER,
74e1c44949Sopenharmony_ci        .address = { 0 },
75e1c44949Sopenharmony_ci    };
76e1c44949Sopenharmony_ci    info_.audioDeviceList.push_back(speaker);
77e1c44949Sopenharmony_ci    AudioDevice earpiece = {
78e1c44949Sopenharmony_ci        .deviceType = AudioDeviceType::DEVICE_EARPIECE,
79e1c44949Sopenharmony_ci        .address = { 0 },
80e1c44949Sopenharmony_ci    };
81e1c44949Sopenharmony_ci    info_.audioDeviceList.push_back(earpiece);
82e1c44949Sopenharmony_ci}
83e1c44949Sopenharmony_ci
84e1c44949Sopenharmony_cibool AudioDeviceManager::IsSupportEarpiece()
85e1c44949Sopenharmony_ci{
86e1c44949Sopenharmony_ci    isUpdateEarpieceDevice_ = true;
87e1c44949Sopenharmony_ci    std::vector<std::unique_ptr<AudioDeviceDescriptor>> audioDeviceList =
88e1c44949Sopenharmony_ci        AudioStandard::AudioRoutingManager::GetInstance()->GetAvailableDevices(AudioDeviceUsage::CALL_OUTPUT_DEVICES);
89e1c44949Sopenharmony_ci    for (auto& audioDevice : audioDeviceList) {
90e1c44949Sopenharmony_ci        TELEPHONY_LOGI("available deviceType : %{public}d", audioDevice->deviceType_);
91e1c44949Sopenharmony_ci        if (audioDevice->deviceType_ == AudioStandard::DeviceType::DEVICE_TYPE_EARPIECE) {
92e1c44949Sopenharmony_ci            return true;
93e1c44949Sopenharmony_ci        }
94e1c44949Sopenharmony_ci    }
95e1c44949Sopenharmony_ci    return false;
96e1c44949Sopenharmony_ci}
97e1c44949Sopenharmony_ci
98e1c44949Sopenharmony_civoid AudioDeviceManager::UpdateEarpieceDevice()
99e1c44949Sopenharmony_ci{
100e1c44949Sopenharmony_ci    if (isUpdateEarpieceDevice_ || IsSupportEarpiece()) {
101e1c44949Sopenharmony_ci        return;
102e1c44949Sopenharmony_ci    }
103e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(infoMutex_);
104e1c44949Sopenharmony_ci    std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
105e1c44949Sopenharmony_ci    while (it != info_.audioDeviceList.end()) {
106e1c44949Sopenharmony_ci        if (it->deviceType == AudioDeviceType::DEVICE_EARPIECE) {
107e1c44949Sopenharmony_ci            it = info_.audioDeviceList.erase(it);
108e1c44949Sopenharmony_ci            TELEPHONY_LOGI("not support Earpice, remove Earpice device success");
109e1c44949Sopenharmony_ci            return;
110e1c44949Sopenharmony_ci        } else {
111e1c44949Sopenharmony_ci            ++it;
112e1c44949Sopenharmony_ci        }
113e1c44949Sopenharmony_ci    }
114e1c44949Sopenharmony_ci}
115e1c44949Sopenharmony_ci
116e1c44949Sopenharmony_civoid AudioDeviceManager::UpdateBluetoothDeviceName(const std::string &macAddress, const std::string &deviceName)
117e1c44949Sopenharmony_ci{
118e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(infoMutex_);
119e1c44949Sopenharmony_ci    std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
120e1c44949Sopenharmony_ci    while (it != info_.audioDeviceList.end()) {
121e1c44949Sopenharmony_ci        if (it->address == macAddress && it->deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
122e1c44949Sopenharmony_ci            if (deviceName.length() > kMaxDeviceNameLen) {
123e1c44949Sopenharmony_ci                TELEPHONY_LOGE("deviceName is too long");
124e1c44949Sopenharmony_ci                return;
125e1c44949Sopenharmony_ci            }
126e1c44949Sopenharmony_ci            if (memset_s(it->deviceName, sizeof(it->deviceName), 0, sizeof(it->deviceName)) != EOK) {
127e1c44949Sopenharmony_ci                TELEPHONY_LOGE("memset_s fail");
128e1c44949Sopenharmony_ci                return;
129e1c44949Sopenharmony_ci            }
130e1c44949Sopenharmony_ci            if (memcpy_s(it->deviceName, kMaxDeviceNameLen, deviceName.c_str(), deviceName.length()) != EOK) {
131e1c44949Sopenharmony_ci                TELEPHONY_LOGE("memcpy_s deviceName fail");
132e1c44949Sopenharmony_ci                return;
133e1c44949Sopenharmony_ci            }
134e1c44949Sopenharmony_ci            TELEPHONY_LOGI("UpdateBluetoothDeviceName");
135e1c44949Sopenharmony_ci            ReportAudioDeviceInfo();
136e1c44949Sopenharmony_ci            return;
137e1c44949Sopenharmony_ci        }
138e1c44949Sopenharmony_ci        ++it;
139e1c44949Sopenharmony_ci    }
140e1c44949Sopenharmony_ci}
141e1c44949Sopenharmony_ci
142e1c44949Sopenharmony_civoid AudioDeviceManager::AddAudioDeviceList(const std::string &address, AudioDeviceType deviceType,
143e1c44949Sopenharmony_ci    const std::string &deviceName)
144e1c44949Sopenharmony_ci{
145e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(infoMutex_);
146e1c44949Sopenharmony_ci    std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
147e1c44949Sopenharmony_ci    while (it != info_.audioDeviceList.end()) {
148e1c44949Sopenharmony_ci        if (it->address == address && it->deviceType == deviceType) {
149e1c44949Sopenharmony_ci            TELEPHONY_LOGI("device is already existenced");
150e1c44949Sopenharmony_ci            return;
151e1c44949Sopenharmony_ci        }
152e1c44949Sopenharmony_ci        if (deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET && it->deviceType == AudioDeviceType::DEVICE_EARPIECE) {
153e1c44949Sopenharmony_ci            it = info_.audioDeviceList.erase(it);
154e1c44949Sopenharmony_ci            TELEPHONY_LOGI("remove Earpiece device success");
155e1c44949Sopenharmony_ci        } else {
156e1c44949Sopenharmony_ci            ++it;
157e1c44949Sopenharmony_ci        }
158e1c44949Sopenharmony_ci    }
159e1c44949Sopenharmony_ci    AudioDevice audioDevice;
160e1c44949Sopenharmony_ci    if (memset_s(&audioDevice, sizeof(AudioDevice), 0, sizeof(AudioDevice)) != EOK) {
161e1c44949Sopenharmony_ci        TELEPHONY_LOGE("memset_s fail");
162e1c44949Sopenharmony_ci        return;
163e1c44949Sopenharmony_ci    }
164e1c44949Sopenharmony_ci    audioDevice.deviceType = deviceType;
165e1c44949Sopenharmony_ci    if (address.length() > kMaxAddressLen) {
166e1c44949Sopenharmony_ci        TELEPHONY_LOGE("address is too long");
167e1c44949Sopenharmony_ci        return;
168e1c44949Sopenharmony_ci    }
169e1c44949Sopenharmony_ci    if (memcpy_s(audioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
170e1c44949Sopenharmony_ci        TELEPHONY_LOGE("memcpy_s address fail");
171e1c44949Sopenharmony_ci        return;
172e1c44949Sopenharmony_ci    }
173e1c44949Sopenharmony_ci    if (deviceName.length() > kMaxDeviceNameLen) {
174e1c44949Sopenharmony_ci        TELEPHONY_LOGE("deviceName is too long");
175e1c44949Sopenharmony_ci        return;
176e1c44949Sopenharmony_ci    }
177e1c44949Sopenharmony_ci    if (memcpy_s(audioDevice.deviceName, kMaxDeviceNameLen, deviceName.c_str(), deviceName.length()) != EOK) {
178e1c44949Sopenharmony_ci        TELEPHONY_LOGE("memcpy_s deviceName fail");
179e1c44949Sopenharmony_ci        return;
180e1c44949Sopenharmony_ci    }
181e1c44949Sopenharmony_ci    info_.audioDeviceList.push_back(audioDevice);
182e1c44949Sopenharmony_ci    if (deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET) {
183e1c44949Sopenharmony_ci        SetDeviceAvailable(AudioDeviceType::DEVICE_WIRED_HEADSET, true);
184e1c44949Sopenharmony_ci    }
185e1c44949Sopenharmony_ci    if (deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
186e1c44949Sopenharmony_ci        SetDeviceAvailable(AudioDeviceType::DEVICE_BLUETOOTH_SCO, true);
187e1c44949Sopenharmony_ci    }
188e1c44949Sopenharmony_ci    if (IsDistributedAudioDeviceType(deviceType)) {
189e1c44949Sopenharmony_ci        SetDeviceAvailable(deviceType, true);
190e1c44949Sopenharmony_ci    }
191e1c44949Sopenharmony_ci    ReportAudioDeviceInfo();
192e1c44949Sopenharmony_ci    TELEPHONY_LOGI("AddAudioDeviceList success");
193e1c44949Sopenharmony_ci}
194e1c44949Sopenharmony_ci
195e1c44949Sopenharmony_civoid AudioDeviceManager::RemoveAudioDeviceList(const std::string &address, AudioDeviceType deviceType)
196e1c44949Sopenharmony_ci{
197e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(infoMutex_);
198e1c44949Sopenharmony_ci    bool needAddEarpiece = true;
199e1c44949Sopenharmony_ci    std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
200e1c44949Sopenharmony_ci    while (it != info_.audioDeviceList.end()) {
201e1c44949Sopenharmony_ci        if (it->deviceType == AudioDeviceType::DEVICE_EARPIECE) {
202e1c44949Sopenharmony_ci            needAddEarpiece = false;
203e1c44949Sopenharmony_ci        }
204e1c44949Sopenharmony_ci        if (it->address == address && it->deviceType == deviceType) {
205e1c44949Sopenharmony_ci            it = info_.audioDeviceList.erase(it);
206e1c44949Sopenharmony_ci        } else {
207e1c44949Sopenharmony_ci            ++it;
208e1c44949Sopenharmony_ci        }
209e1c44949Sopenharmony_ci    }
210e1c44949Sopenharmony_ci
211e1c44949Sopenharmony_ci    bool wiredHeadsetExist = false;
212e1c44949Sopenharmony_ci    bool blueToothScoExist = false;
213e1c44949Sopenharmony_ci    for (auto &elem : info_.audioDeviceList) {
214e1c44949Sopenharmony_ci        if (elem.deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET) {
215e1c44949Sopenharmony_ci            wiredHeadsetExist = true;
216e1c44949Sopenharmony_ci        }
217e1c44949Sopenharmony_ci        if (elem.deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
218e1c44949Sopenharmony_ci            blueToothScoExist = true;
219e1c44949Sopenharmony_ci        }
220e1c44949Sopenharmony_ci    }
221e1c44949Sopenharmony_ci    if (deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET && !wiredHeadsetExist) {
222e1c44949Sopenharmony_ci        SetDeviceAvailable(AudioDeviceType::DEVICE_WIRED_HEADSET, false);
223e1c44949Sopenharmony_ci    }
224e1c44949Sopenharmony_ci    if (deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO && !blueToothScoExist) {
225e1c44949Sopenharmony_ci        SetDeviceAvailable(AudioDeviceType::DEVICE_BLUETOOTH_SCO, false);
226e1c44949Sopenharmony_ci    }
227e1c44949Sopenharmony_ci    if (IsDistributedAudioDeviceType(deviceType)) {
228e1c44949Sopenharmony_ci        SetDeviceAvailable(deviceType, false);
229e1c44949Sopenharmony_ci    }
230e1c44949Sopenharmony_ci    if (needAddEarpiece && deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET && !wiredHeadsetExist) {
231e1c44949Sopenharmony_ci        AudioDevice audioDevice = {
232e1c44949Sopenharmony_ci            .deviceType = AudioDeviceType::DEVICE_EARPIECE,
233e1c44949Sopenharmony_ci            .address = { 0 },
234e1c44949Sopenharmony_ci        };
235e1c44949Sopenharmony_ci        info_.audioDeviceList.push_back(audioDevice);
236e1c44949Sopenharmony_ci        TELEPHONY_LOGI("add Earpiece device success");
237e1c44949Sopenharmony_ci    }
238e1c44949Sopenharmony_ci    sptr<CallBase> liveCall = CallObjectManager::GetForegroundLiveCall();
239e1c44949Sopenharmony_ci    if (liveCall != nullptr && (liveCall->GetVideoStateType() == VideoStateType::TYPE_VIDEO ||
240e1c44949Sopenharmony_ci        liveCall->GetCallType() == CallType::TYPE_SATELLITE)) {
241e1c44949Sopenharmony_ci        DelayedSingleton<AudioControlManager>::GetInstance()->UpdateDeviceTypeForVideoOrSatelliteCall();
242e1c44949Sopenharmony_ci    }
243e1c44949Sopenharmony_ci    ReportAudioDeviceInfo();
244e1c44949Sopenharmony_ci    TELEPHONY_LOGI("RemoveAudioDeviceList success");
245e1c44949Sopenharmony_ci}
246e1c44949Sopenharmony_ci
247e1c44949Sopenharmony_civoid AudioDeviceManager::ResetBtAudioDevicesList()
248e1c44949Sopenharmony_ci{
249e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(infoMutex_);
250e1c44949Sopenharmony_ci    std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
251e1c44949Sopenharmony_ci    bool hadBtActived = false;
252e1c44949Sopenharmony_ci    while (it != info_.audioDeviceList.end()) {
253e1c44949Sopenharmony_ci        if (it->deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
254e1c44949Sopenharmony_ci            hadBtActived = true;
255e1c44949Sopenharmony_ci            it = info_.audioDeviceList.erase(it);
256e1c44949Sopenharmony_ci        } else {
257e1c44949Sopenharmony_ci            ++it;
258e1c44949Sopenharmony_ci        }
259e1c44949Sopenharmony_ci    }
260e1c44949Sopenharmony_ci    SetDeviceAvailable(AudioDeviceType::DEVICE_BLUETOOTH_SCO, false);
261e1c44949Sopenharmony_ci    if (hadBtActived) {
262e1c44949Sopenharmony_ci        ReportAudioDeviceInfo();
263e1c44949Sopenharmony_ci    }
264e1c44949Sopenharmony_ci    TELEPHONY_LOGI("ResetBtAudioDevicesList success");
265e1c44949Sopenharmony_ci}
266e1c44949Sopenharmony_ci
267e1c44949Sopenharmony_civoid AudioDeviceManager::ResetDistributedCallDevicesList()
268e1c44949Sopenharmony_ci{
269e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(infoMutex_);
270e1c44949Sopenharmony_ci    std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
271e1c44949Sopenharmony_ci    while (it != info_.audioDeviceList.end()) {
272e1c44949Sopenharmony_ci        if (IsDistributedAudioDeviceType(it->deviceType)) {
273e1c44949Sopenharmony_ci            it = info_.audioDeviceList.erase(it);
274e1c44949Sopenharmony_ci        } else {
275e1c44949Sopenharmony_ci            ++it;
276e1c44949Sopenharmony_ci        }
277e1c44949Sopenharmony_ci    }
278e1c44949Sopenharmony_ci    SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE, false);
279e1c44949Sopenharmony_ci    SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_PHONE, false);
280e1c44949Sopenharmony_ci    SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_PAD, false);
281e1c44949Sopenharmony_ci    SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_PC, false);
282e1c44949Sopenharmony_ci    DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportAudioDeviceChange(info_);
283e1c44949Sopenharmony_ci    TELEPHONY_LOGI("Reset Distributed Audio Devices List success");
284e1c44949Sopenharmony_ci}
285e1c44949Sopenharmony_ci
286e1c44949Sopenharmony_cibool AudioDeviceManager::InitAudioDevice()
287e1c44949Sopenharmony_ci{
288e1c44949Sopenharmony_ci    // when audio deactivate interrupt , reinit
289e1c44949Sopenharmony_ci    // when external audio device connection state changed , reinit
290e1c44949Sopenharmony_ci    auto device = DelayedSingleton<AudioControlManager>::GetInstance()->GetInitAudioDeviceType();
291e1c44949Sopenharmony_ci    return SwitchDevice(device);
292e1c44949Sopenharmony_ci}
293e1c44949Sopenharmony_ci
294e1c44949Sopenharmony_cibool AudioDeviceManager::ProcessEvent(AudioEvent event)
295e1c44949Sopenharmony_ci{
296e1c44949Sopenharmony_ci    bool result = false;
297e1c44949Sopenharmony_ci    switch (event) {
298e1c44949Sopenharmony_ci        case AudioEvent::AUDIO_ACTIVATED:
299e1c44949Sopenharmony_ci        case AudioEvent::AUDIO_RINGING:
300e1c44949Sopenharmony_ci            if (!isAudioActivated_) {
301e1c44949Sopenharmony_ci                isAudioActivated_ = true;
302e1c44949Sopenharmony_ci                AudioDevice device = {
303e1c44949Sopenharmony_ci                    .deviceType = AudioDeviceType::DEVICE_EARPIECE,
304e1c44949Sopenharmony_ci                    .address = { 0 },
305e1c44949Sopenharmony_ci                };
306e1c44949Sopenharmony_ci                if (DelayedSingleton<AudioProxy>::GetInstance()->GetPreferredOutputAudioDevice(device) !=
307e1c44949Sopenharmony_ci                    TELEPHONY_SUCCESS) {
308e1c44949Sopenharmony_ci                    TELEPHONY_LOGE("current audio device nullptr");
309e1c44949Sopenharmony_ci                    return false;
310e1c44949Sopenharmony_ci                }
311e1c44949Sopenharmony_ci                SetCurrentAudioDevice(device.deviceType);
312e1c44949Sopenharmony_ci            }
313e1c44949Sopenharmony_ci            break;
314e1c44949Sopenharmony_ci        case AudioEvent::AUDIO_DEACTIVATED:
315e1c44949Sopenharmony_ci            if (isAudioActivated_) {
316e1c44949Sopenharmony_ci                isAudioActivated_ = false;
317e1c44949Sopenharmony_ci                result = InitAudioDevice();
318e1c44949Sopenharmony_ci            }
319e1c44949Sopenharmony_ci            break;
320e1c44949Sopenharmony_ci        case AudioEvent::INIT_AUDIO_DEVICE:
321e1c44949Sopenharmony_ci            result = InitAudioDevice();
322e1c44949Sopenharmony_ci            break;
323e1c44949Sopenharmony_ci        case AudioEvent::WIRED_HEADSET_DISCONNECTED: {
324e1c44949Sopenharmony_ci            if (!isAudioActivated_) {
325e1c44949Sopenharmony_ci                TELEPHONY_LOGE("call is not active, no need to connect sco");
326e1c44949Sopenharmony_ci                return false;
327e1c44949Sopenharmony_ci            }
328e1c44949Sopenharmony_ci            break;
329e1c44949Sopenharmony_ci        }
330e1c44949Sopenharmony_ci        default:
331e1c44949Sopenharmony_ci            break;
332e1c44949Sopenharmony_ci    }
333e1c44949Sopenharmony_ci    return result;
334e1c44949Sopenharmony_ci}
335e1c44949Sopenharmony_ci
336e1c44949Sopenharmony_cibool AudioDeviceManager::SwitchDevice(AudioEvent event)
337e1c44949Sopenharmony_ci{
338e1c44949Sopenharmony_ci    auto itFunc = memberFuncMap_.find(event);
339e1c44949Sopenharmony_ci    if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
340e1c44949Sopenharmony_ci        auto memberFunc = itFunc->second;
341e1c44949Sopenharmony_ci        return memberFunc();
342e1c44949Sopenharmony_ci    }
343e1c44949Sopenharmony_ci    return false;
344e1c44949Sopenharmony_ci}
345e1c44949Sopenharmony_ci
346e1c44949Sopenharmony_cibool AudioDeviceManager::SwitchDevice(AudioDeviceType device)
347e1c44949Sopenharmony_ci{
348e1c44949Sopenharmony_ci    bool result = false;
349e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
350e1c44949Sopenharmony_ci    switch (device) {
351e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_EARPIECE:
352e1c44949Sopenharmony_ci            result = EnableEarpiece();
353e1c44949Sopenharmony_ci            break;
354e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_SPEAKER:
355e1c44949Sopenharmony_ci            result = EnableSpeaker();
356e1c44949Sopenharmony_ci            break;
357e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_WIRED_HEADSET:
358e1c44949Sopenharmony_ci            result = EnableWiredHeadset();
359e1c44949Sopenharmony_ci            break;
360e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_BLUETOOTH_SCO:
361e1c44949Sopenharmony_ci            result = EnableBtSco();
362e1c44949Sopenharmony_ci            break;
363e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_DISABLE:
364e1c44949Sopenharmony_ci            result = DisableAll();
365e1c44949Sopenharmony_ci            break;
366e1c44949Sopenharmony_ci        default:
367e1c44949Sopenharmony_ci            break;
368e1c44949Sopenharmony_ci    }
369e1c44949Sopenharmony_ci    TELEPHONY_LOGI("switch device lock release");
370e1c44949Sopenharmony_ci    return result;
371e1c44949Sopenharmony_ci}
372e1c44949Sopenharmony_ci
373e1c44949Sopenharmony_cibool AudioDeviceManager::EnableSpeaker()
374e1c44949Sopenharmony_ci{
375e1c44949Sopenharmony_ci    if (isSpeakerAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetSpeakerDevActive(true)) {
376e1c44949Sopenharmony_ci        TELEPHONY_LOGI("speaker enabled , current audio device : speaker");
377e1c44949Sopenharmony_ci        SetCurrentAudioDevice(AudioDeviceType::DEVICE_SPEAKER);
378e1c44949Sopenharmony_ci        return true;
379e1c44949Sopenharmony_ci    }
380e1c44949Sopenharmony_ci    TELEPHONY_LOGI("enable speaker device failed");
381e1c44949Sopenharmony_ci    return false;
382e1c44949Sopenharmony_ci}
383e1c44949Sopenharmony_ci
384e1c44949Sopenharmony_cibool AudioDeviceManager::EnableEarpiece()
385e1c44949Sopenharmony_ci{
386e1c44949Sopenharmony_ci    if (isEarpieceAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetEarpieceDevActive()) {
387e1c44949Sopenharmony_ci        TELEPHONY_LOGI("earpiece enabled , current audio device : earpiece");
388e1c44949Sopenharmony_ci        SetCurrentAudioDevice(AudioDeviceType::DEVICE_EARPIECE);
389e1c44949Sopenharmony_ci        return true;
390e1c44949Sopenharmony_ci    }
391e1c44949Sopenharmony_ci    TELEPHONY_LOGI("enable earpiece device failed");
392e1c44949Sopenharmony_ci    return false;
393e1c44949Sopenharmony_ci}
394e1c44949Sopenharmony_ci
395e1c44949Sopenharmony_cibool AudioDeviceManager::EnableWiredHeadset()
396e1c44949Sopenharmony_ci{
397e1c44949Sopenharmony_ci    if (isWiredHeadsetConnected_ && DelayedSingleton<AudioProxy>::GetInstance()->SetWiredHeadsetDevActive()) {
398e1c44949Sopenharmony_ci        TELEPHONY_LOGI("wired headset enabled , current audio device : wired headset");
399e1c44949Sopenharmony_ci        SetCurrentAudioDevice(AudioDeviceType::DEVICE_WIRED_HEADSET);
400e1c44949Sopenharmony_ci        return true;
401e1c44949Sopenharmony_ci    }
402e1c44949Sopenharmony_ci    TELEPHONY_LOGI("enable wired headset device failed");
403e1c44949Sopenharmony_ci    return false;
404e1c44949Sopenharmony_ci}
405e1c44949Sopenharmony_ci
406e1c44949Sopenharmony_cibool AudioDeviceManager::EnableBtSco()
407e1c44949Sopenharmony_ci{
408e1c44949Sopenharmony_ci    if (IsBtActived()) {
409e1c44949Sopenharmony_ci        TELEPHONY_LOGI("bluetooth sco enabled , current audio device : bluetooth sco");
410e1c44949Sopenharmony_ci        SetCurrentAudioDevice(AudioDeviceType::DEVICE_BLUETOOTH_SCO);
411e1c44949Sopenharmony_ci        return true;
412e1c44949Sopenharmony_ci    }
413e1c44949Sopenharmony_ci    TELEPHONY_LOGI("enable bluetooth sco device failed");
414e1c44949Sopenharmony_ci    return false;
415e1c44949Sopenharmony_ci}
416e1c44949Sopenharmony_ci
417e1c44949Sopenharmony_cibool AudioDeviceManager::DisableAll()
418e1c44949Sopenharmony_ci{
419e1c44949Sopenharmony_ci    audioDeviceType_ = AudioDeviceType::DEVICE_UNKNOWN;
420e1c44949Sopenharmony_ci    isBtScoDevEnable_ = false;
421e1c44949Sopenharmony_ci    isDCallDevEnable_ = false;
422e1c44949Sopenharmony_ci    isWiredHeadsetDevEnable_ = false;
423e1c44949Sopenharmony_ci    isSpeakerDevEnable_ = false;
424e1c44949Sopenharmony_ci    isEarpieceDevEnable_ = false;
425e1c44949Sopenharmony_ci    currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
426e1c44949Sopenharmony_ci    if (currentAudioDevice_ == nullptr) {
427e1c44949Sopenharmony_ci        TELEPHONY_LOGE("make_unique InactiveDeviceState failed");
428e1c44949Sopenharmony_ci        return false;
429e1c44949Sopenharmony_ci    }
430e1c44949Sopenharmony_ci    TELEPHONY_LOGI("current audio device : all audio devices disabled");
431e1c44949Sopenharmony_ci    return true;
432e1c44949Sopenharmony_ci}
433e1c44949Sopenharmony_ci
434e1c44949Sopenharmony_civoid AudioDeviceManager::SetCurrentAudioDevice(AudioDeviceType deviceType)
435e1c44949Sopenharmony_ci{
436e1c44949Sopenharmony_ci    TELEPHONY_LOGI("set current audio device, deviceType: %{public}d.", deviceType);
437e1c44949Sopenharmony_ci    if (!IsDistributedAudioDeviceType(deviceType) && IsDistributedAudioDeviceType(audioDeviceType_)) {
438e1c44949Sopenharmony_ci        DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOffDCallDeviceSync();
439e1c44949Sopenharmony_ci    }
440e1c44949Sopenharmony_ci    if (deviceType == AudioDeviceType::DEVICE_EARPIECE && CallObjectManager::HasSatelliteCallExist()) {
441e1c44949Sopenharmony_ci        audioDeviceType_ = AudioDeviceType::DEVICE_SPEAKER;
442e1c44949Sopenharmony_ci        AudioStandard::AudioSystemManager::GetInstance()->
443e1c44949Sopenharmony_ci            SetDeviceActive(AudioStandard::ActiveDeviceType::SPEAKER, true);
444e1c44949Sopenharmony_ci        return;
445e1c44949Sopenharmony_ci    }
446e1c44949Sopenharmony_ci    AudioDevice device = {
447e1c44949Sopenharmony_ci        .deviceType = deviceType,
448e1c44949Sopenharmony_ci        .address = { 0 },
449e1c44949Sopenharmony_ci    };
450e1c44949Sopenharmony_ci    SetCurrentAudioDevice(device);
451e1c44949Sopenharmony_ci}
452e1c44949Sopenharmony_ci
453e1c44949Sopenharmony_civoid AudioDeviceManager::SetCurrentAudioDevice(const AudioDevice &device)
454e1c44949Sopenharmony_ci{
455e1c44949Sopenharmony_ci    AudioDeviceType deviceType = device.deviceType;
456e1c44949Sopenharmony_ci    TELEPHONY_LOGI("set current audio device, audioDeviceType = %{public}d.", deviceType);
457e1c44949Sopenharmony_ci    if (!IsDistributedAudioDeviceType(deviceType) && IsDistributedAudioDeviceType(audioDeviceType_)) {
458e1c44949Sopenharmony_ci        DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOffDCallDeviceSync();
459e1c44949Sopenharmony_ci    }
460e1c44949Sopenharmony_ci    audioDeviceType_ = deviceType;
461e1c44949Sopenharmony_ci    ReportAudioDeviceChange(device);
462e1c44949Sopenharmony_ci}
463e1c44949Sopenharmony_ci
464e1c44949Sopenharmony_cibool AudioDeviceManager::CheckAndSwitchDistributedAudioDevice()
465e1c44949Sopenharmony_ci{
466e1c44949Sopenharmony_ci    TELEPHONY_LOGI("check and switch distributed audio device.");
467e1c44949Sopenharmony_ci    std::lock_guard<std::mutex> lock(infoMutex_);
468e1c44949Sopenharmony_ci    DelayedSingleton<DistributedCallManager>::GetInstance()->SetCallState(true);
469e1c44949Sopenharmony_ci    std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
470e1c44949Sopenharmony_ci    while (it != info_.audioDeviceList.end()) {
471e1c44949Sopenharmony_ci        if (it->deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE &&
472e1c44949Sopenharmony_ci            DelayedSingleton<DistributedCallManager>::GetInstance()->IsSelectVirtualModem()) {
473e1c44949Sopenharmony_ci            DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOnDCallDeviceAsync(*it);
474e1c44949Sopenharmony_ci            return true;
475e1c44949Sopenharmony_ci        } else {
476e1c44949Sopenharmony_ci            ++it;
477e1c44949Sopenharmony_ci        }
478e1c44949Sopenharmony_ci    }
479e1c44949Sopenharmony_ci    return false;
480e1c44949Sopenharmony_ci}
481e1c44949Sopenharmony_ci
482e1c44949Sopenharmony_civoid AudioDeviceManager::OnActivedCallDisconnected()
483e1c44949Sopenharmony_ci{
484e1c44949Sopenharmony_ci    DelayedSingleton<DistributedCallManager>::GetInstance()->SetCallState(false);
485e1c44949Sopenharmony_ci    DelayedSingleton<DistributedCallManager>::GetInstance()->DealDisconnectCall();
486e1c44949Sopenharmony_ci}
487e1c44949Sopenharmony_ci
488e1c44949Sopenharmony_ciint32_t AudioDeviceManager::ReportAudioDeviceChange(const AudioDevice &device)
489e1c44949Sopenharmony_ci{
490e1c44949Sopenharmony_ci    if (audioDeviceType_ == AudioDeviceType::DEVICE_UNKNOWN) {
491e1c44949Sopenharmony_ci        audioDeviceType_ = DelayedSingleton<AudioControlManager>::GetInstance()->GetInitAudioDeviceType();
492e1c44949Sopenharmony_ci        info_.currentAudioDevice.deviceType = audioDeviceType_;
493e1c44949Sopenharmony_ci    } else {
494e1c44949Sopenharmony_ci        info_.currentAudioDevice.deviceType = audioDeviceType_;
495e1c44949Sopenharmony_ci    }
496e1c44949Sopenharmony_ci    std::string address = device.address;
497e1c44949Sopenharmony_ci    std::string deviceName = device.deviceName;
498e1c44949Sopenharmony_ci    if (audioDeviceType_ == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
499e1c44949Sopenharmony_ci        if (address.empty() || deviceName.empty()) {
500e1c44949Sopenharmony_ci            std::unique_ptr<AudioStandard::AudioDeviceDescriptor> activeBluetoothDevice =
501e1c44949Sopenharmony_ci                AudioStandard::AudioRoutingManager::GetInstance()->GetActiveBluetoothDevice();
502e1c44949Sopenharmony_ci            if (activeBluetoothDevice != nullptr && !activeBluetoothDevice->macAddress_.empty()) {
503e1c44949Sopenharmony_ci                address = activeBluetoothDevice->macAddress_;
504e1c44949Sopenharmony_ci                deviceName = activeBluetoothDevice->deviceName_;
505e1c44949Sopenharmony_ci            }
506e1c44949Sopenharmony_ci        }
507e1c44949Sopenharmony_ci    } else if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->IsDistributedDev(device)) {
508e1c44949Sopenharmony_ci        TELEPHONY_LOGI("audio device is distributed communication dev");
509e1c44949Sopenharmony_ci    } else if (IsDistributedAudioDeviceType(audioDeviceType_)) {
510e1c44949Sopenharmony_ci        address = DelayedSingleton<DistributedCallManager>::GetInstance()->GetConnectedDCallDeviceAddr();
511e1c44949Sopenharmony_ci    }
512e1c44949Sopenharmony_ci    if (address.length() > kMaxAddressLen) {
513e1c44949Sopenharmony_ci        TELEPHONY_LOGE("address is not too long");
514e1c44949Sopenharmony_ci        return TELEPHONY_ERR_ARGUMENT_INVALID;
515e1c44949Sopenharmony_ci    }
516e1c44949Sopenharmony_ci    if (memset_s(info_.currentAudioDevice.address, kMaxAddressLen + 1, 0, kMaxAddressLen + 1) != EOK) {
517e1c44949Sopenharmony_ci        TELEPHONY_LOGE("failed to memset_s currentAudioDevice.address");
518e1c44949Sopenharmony_ci        return TELEPHONY_ERR_MEMSET_FAIL;
519e1c44949Sopenharmony_ci    }
520e1c44949Sopenharmony_ci    if (memcpy_s(info_.currentAudioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
521e1c44949Sopenharmony_ci        TELEPHONY_LOGE("memcpy_s address fail");
522e1c44949Sopenharmony_ci        return TELEPHONY_ERR_MEMCPY_FAIL;
523e1c44949Sopenharmony_ci    }
524e1c44949Sopenharmony_ci    if (deviceName.length() > kMaxDeviceNameLen) {
525e1c44949Sopenharmony_ci        TELEPHONY_LOGE("deviceName is too long");
526e1c44949Sopenharmony_ci        return TELEPHONY_ERR_ARGUMENT_INVALID;
527e1c44949Sopenharmony_ci    }
528e1c44949Sopenharmony_ci    if (memset_s(info_.currentAudioDevice.deviceName, kMaxDeviceNameLen + 1, 0, kMaxDeviceNameLen + 1) != EOK) {
529e1c44949Sopenharmony_ci        TELEPHONY_LOGE("failed to memset_s currentAudioDevice.deviceName");
530e1c44949Sopenharmony_ci        return TELEPHONY_ERR_MEMSET_FAIL;
531e1c44949Sopenharmony_ci    }
532e1c44949Sopenharmony_ci    if (memcpy_s(info_.currentAudioDevice.deviceName, kMaxDeviceNameLen,
533e1c44949Sopenharmony_ci        deviceName.c_str(), deviceName.length()) != EOK) {
534e1c44949Sopenharmony_ci        TELEPHONY_LOGE("memcpy_s deviceName fail");
535e1c44949Sopenharmony_ci        return TELEPHONY_ERR_MEMCPY_FAIL;
536e1c44949Sopenharmony_ci    }
537e1c44949Sopenharmony_ci    return ReportAudioDeviceInfo();
538e1c44949Sopenharmony_ci}
539e1c44949Sopenharmony_ci
540e1c44949Sopenharmony_ciint32_t AudioDeviceManager::ReportAudioDeviceInfo()
541e1c44949Sopenharmony_ci{
542e1c44949Sopenharmony_ci    sptr<CallBase> liveCall = CallObjectManager::GetForegroundLiveCall();
543e1c44949Sopenharmony_ci    return ReportAudioDeviceInfo(liveCall);
544e1c44949Sopenharmony_ci}
545e1c44949Sopenharmony_ci
546e1c44949Sopenharmony_cistd::string AudioDeviceManager::ConvertAddress()
547e1c44949Sopenharmony_ci{
548e1c44949Sopenharmony_ci    std::string addr = info_.currentAudioDevice.address;
549e1c44949Sopenharmony_ci    if (info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
550e1c44949Sopenharmony_ci        if (!addr.empty() && addr.length() > DEVICE_ADDR_LEN) {
551e1c44949Sopenharmony_ci            return (addr.substr(0, ADDR_HEAD_VALID_LEN) + ":*:*:*:" +
552e1c44949Sopenharmony_ci                addr.substr(addr.length() - ADDR_TAIL_VALID_LEN));
553e1c44949Sopenharmony_ci        }
554e1c44949Sopenharmony_ci    } else if (info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE||
555e1c44949Sopenharmony_ci               info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PHONE||
556e1c44949Sopenharmony_ci               info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PAD) {
557e1c44949Sopenharmony_ci        addr = "";
558e1c44949Sopenharmony_ci    }
559e1c44949Sopenharmony_ci    return addr;
560e1c44949Sopenharmony_ci}
561e1c44949Sopenharmony_ci
562e1c44949Sopenharmony_ciint32_t AudioDeviceManager::ReportAudioDeviceInfo(sptr<CallBase> call)
563e1c44949Sopenharmony_ci{
564e1c44949Sopenharmony_ci    if (call != nullptr && call->GetCallType() == CallType::TYPE_VOIP) {
565e1c44949Sopenharmony_ci        info_.isMuted = call->IsMuted();
566e1c44949Sopenharmony_ci    } else {
567e1c44949Sopenharmony_ci        info_.isMuted = DelayedSingleton<AudioProxy>::GetInstance()->IsMicrophoneMute();
568e1c44949Sopenharmony_ci    }
569e1c44949Sopenharmony_ci    TELEPHONY_LOGI("report audio device info, currentAudioDeviceType:%{public}d, currentAddress:%{public}s, "
570e1c44949Sopenharmony_ci        "mute:%{public}d", info_.currentAudioDevice.deviceType, ConvertAddress().c_str(), info_.isMuted);
571e1c44949Sopenharmony_ci    return DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportAudioDeviceChange(info_);
572e1c44949Sopenharmony_ci}
573e1c44949Sopenharmony_ci
574e1c44949Sopenharmony_ciAudioDeviceType AudioDeviceManager::GetCurrentAudioDevice()
575e1c44949Sopenharmony_ci{
576e1c44949Sopenharmony_ci    return audioDeviceType_;
577e1c44949Sopenharmony_ci}
578e1c44949Sopenharmony_ci
579e1c44949Sopenharmony_cibool AudioDeviceManager::IsEarpieceDevEnable()
580e1c44949Sopenharmony_ci{
581e1c44949Sopenharmony_ci    return isEarpieceDevEnable_;
582e1c44949Sopenharmony_ci}
583e1c44949Sopenharmony_ci
584e1c44949Sopenharmony_cibool AudioDeviceManager::IsWiredHeadsetDevEnable()
585e1c44949Sopenharmony_ci{
586e1c44949Sopenharmony_ci    return isWiredHeadsetDevEnable_;
587e1c44949Sopenharmony_ci}
588e1c44949Sopenharmony_ci
589e1c44949Sopenharmony_cibool AudioDeviceManager::IsSpeakerDevEnable()
590e1c44949Sopenharmony_ci{
591e1c44949Sopenharmony_ci    return isSpeakerDevEnable_;
592e1c44949Sopenharmony_ci}
593e1c44949Sopenharmony_ci
594e1c44949Sopenharmony_cibool AudioDeviceManager::IsBtScoDevEnable()
595e1c44949Sopenharmony_ci{
596e1c44949Sopenharmony_ci    return isBtScoDevEnable_;
597e1c44949Sopenharmony_ci}
598e1c44949Sopenharmony_ci
599e1c44949Sopenharmony_cibool AudioDeviceManager::IsDCallDevEnable()
600e1c44949Sopenharmony_ci{
601e1c44949Sopenharmony_ci    return isDCallDevEnable_;
602e1c44949Sopenharmony_ci}
603e1c44949Sopenharmony_ci
604e1c44949Sopenharmony_cibool AudioDeviceManager::IsBtScoConnected()
605e1c44949Sopenharmony_ci{
606e1c44949Sopenharmony_ci    return isBtScoConnected_;
607e1c44949Sopenharmony_ci}
608e1c44949Sopenharmony_ci
609e1c44949Sopenharmony_cibool AudioDeviceManager::IsBtActived()
610e1c44949Sopenharmony_ci{
611e1c44949Sopenharmony_ci    std::unique_ptr<AudioStandard::AudioDeviceDescriptor> activeBluetoothDevice =
612e1c44949Sopenharmony_ci        AudioStandard::AudioRoutingManager::GetInstance()->GetActiveBluetoothDevice();
613e1c44949Sopenharmony_ci    if (activeBluetoothDevice != nullptr && !activeBluetoothDevice->macAddress_.empty()) {
614e1c44949Sopenharmony_ci        TELEPHONY_LOGI("has actived bt device");
615e1c44949Sopenharmony_ci        return true;
616e1c44949Sopenharmony_ci    }
617e1c44949Sopenharmony_ci    return false;
618e1c44949Sopenharmony_ci}
619e1c44949Sopenharmony_ci
620e1c44949Sopenharmony_cibool AudioDeviceManager::IsDistributedCallConnected()
621e1c44949Sopenharmony_ci{
622e1c44949Sopenharmony_ci    return isDCallDevConnected_;
623e1c44949Sopenharmony_ci}
624e1c44949Sopenharmony_ci
625e1c44949Sopenharmony_cibool AudioDeviceManager::IsWiredHeadsetConnected()
626e1c44949Sopenharmony_ci{
627e1c44949Sopenharmony_ci    return isWiredHeadsetConnected_;
628e1c44949Sopenharmony_ci}
629e1c44949Sopenharmony_ci
630e1c44949Sopenharmony_cibool AudioDeviceManager::IsEarpieceAvailable()
631e1c44949Sopenharmony_ci{
632e1c44949Sopenharmony_ci    return isEarpieceAvailable_;
633e1c44949Sopenharmony_ci}
634e1c44949Sopenharmony_ci
635e1c44949Sopenharmony_cibool AudioDeviceManager::IsSpeakerAvailable()
636e1c44949Sopenharmony_ci{
637e1c44949Sopenharmony_ci    return isSpeakerAvailable_;
638e1c44949Sopenharmony_ci}
639e1c44949Sopenharmony_ci
640e1c44949Sopenharmony_cibool AudioDeviceManager::IsDistributedAudioDeviceType(AudioDeviceType deviceType)
641e1c44949Sopenharmony_ci{
642e1c44949Sopenharmony_ci    if (((deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE) ||
643e1c44949Sopenharmony_ci        (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PHONE) ||
644e1c44949Sopenharmony_ci        (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PAD) ||
645e1c44949Sopenharmony_ci        (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PC))) {
646e1c44949Sopenharmony_ci        return true;
647e1c44949Sopenharmony_ci    }
648e1c44949Sopenharmony_ci    return false;
649e1c44949Sopenharmony_ci}
650e1c44949Sopenharmony_ci
651e1c44949Sopenharmony_civoid AudioDeviceManager::SetDeviceAvailable(AudioDeviceType deviceType, bool available)
652e1c44949Sopenharmony_ci{
653e1c44949Sopenharmony_ci    switch (deviceType) {
654e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_SPEAKER:
655e1c44949Sopenharmony_ci            isSpeakerAvailable_ = available;
656e1c44949Sopenharmony_ci            break;
657e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_EARPIECE:
658e1c44949Sopenharmony_ci            isEarpieceAvailable_ = available;
659e1c44949Sopenharmony_ci            break;
660e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_BLUETOOTH_SCO:
661e1c44949Sopenharmony_ci            isBtScoConnected_ = available;
662e1c44949Sopenharmony_ci            break;
663e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_WIRED_HEADSET:
664e1c44949Sopenharmony_ci            isWiredHeadsetConnected_ = available;
665e1c44949Sopenharmony_ci            break;
666e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE:
667e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_DISTRIBUTED_PHONE:
668e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_DISTRIBUTED_PAD:
669e1c44949Sopenharmony_ci        case AudioDeviceType::DEVICE_DISTRIBUTED_PC:
670e1c44949Sopenharmony_ci            isDCallDevConnected_ = available;
671e1c44949Sopenharmony_ci            break;
672e1c44949Sopenharmony_ci        default:
673e1c44949Sopenharmony_ci            break;
674e1c44949Sopenharmony_ci    }
675e1c44949Sopenharmony_ci}
676e1c44949Sopenharmony_ci} // namespace Telephony
677e1c44949Sopenharmony_ci} // namespace OHOS