1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "AHAL_Telephony"
18 #include <android-base/logging.h>
19 #include <android/binder_to_string.h>
20 
21 #include <Utils.h>
22 
23 #include "core-impl/Telephony.h"
24 
25 using aidl::android::hardware::audio::common::isValidAudioMode;
26 using aidl::android::media::audio::common::AudioMode;
27 using aidl::android::media::audio::common::Boolean;
28 using aidl::android::media::audio::common::Float;
29 
30 namespace aidl::android::hardware::audio::core {
31 
Telephony()32 Telephony::Telephony() {
33     mTelecomConfig.voiceVolume = Float{TelecomConfig::VOICE_VOLUME_MAX};
34     mTelecomConfig.ttyMode = TelecomConfig::TtyMode::OFF;
35     mTelecomConfig.isHacEnabled = Boolean{false};
36 }
37 
getSupportedAudioModes(std::vector<AudioMode> * _aidl_return)38 ndk::ScopedAStatus Telephony::getSupportedAudioModes(std::vector<AudioMode>* _aidl_return) {
39     *_aidl_return = mSupportedAudioModes;
40     LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
41     return ndk::ScopedAStatus::ok();
42 }
43 
switchAudioMode(AudioMode in_mode)44 ndk::ScopedAStatus Telephony::switchAudioMode(AudioMode in_mode) {
45     if (!isValidAudioMode(in_mode)) {
46         LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
47         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
48     }
49     if (std::find(mSupportedAudioModes.begin(), mSupportedAudioModes.end(), in_mode) !=
50         mSupportedAudioModes.end()) {
51         LOG(DEBUG) << __func__ << ": " << toString(in_mode);
52         return ndk::ScopedAStatus::ok();
53     }
54     LOG(ERROR) << __func__ << ": unsupported mode " << toString(in_mode);
55     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
56 }
57 
setTelecomConfig(const TelecomConfig & in_config,TelecomConfig * _aidl_return)58 ndk::ScopedAStatus Telephony::setTelecomConfig(const TelecomConfig& in_config,
59                                                TelecomConfig* _aidl_return) {
60     if (in_config.voiceVolume.has_value() &&
61         (in_config.voiceVolume.value().value < TelecomConfig::VOICE_VOLUME_MIN ||
62          in_config.voiceVolume.value().value > TelecomConfig::VOICE_VOLUME_MAX)) {
63         LOG(ERROR) << __func__
64                    << ": voice volume value is invalid: " << in_config.voiceVolume.value().value;
65         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
66     }
67     if (in_config.voiceVolume.has_value()) {
68         mTelecomConfig.voiceVolume = in_config.voiceVolume;
69     }
70     if (in_config.ttyMode != TelecomConfig::TtyMode::UNSPECIFIED) {
71         mTelecomConfig.ttyMode = in_config.ttyMode;
72     }
73     if (in_config.isHacEnabled.has_value()) {
74         mTelecomConfig.isHacEnabled = in_config.isHacEnabled;
75     }
76     *_aidl_return = mTelecomConfig;
77     LOG(DEBUG) << __func__ << ": received " << in_config.toString() << ", returning "
78                << _aidl_return->toString();
79     return ndk::ScopedAStatus::ok();
80 }
81 
82 }  // namespace aidl::android::hardware::audio::core
83