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