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_Bluetooth"
18 #include <android-base/logging.h>
19
20 #include "core-impl/Bluetooth.h"
21
22 using aidl::android::hardware::audio::core::VendorParameter;
23 using aidl::android::media::audio::common::Boolean;
24 using aidl::android::media::audio::common::Float;
25 using aidl::android::media::audio::common::Int;
26
27 namespace aidl::android::hardware::audio::core {
28
Bluetooth()29 Bluetooth::Bluetooth() {
30 mScoConfig.isEnabled = Boolean{false};
31 mScoConfig.isNrecEnabled = Boolean{false};
32 mScoConfig.mode = ScoConfig::Mode::SCO;
33 mHfpConfig.isEnabled = Boolean{false};
34 mHfpConfig.sampleRate = Int{8000};
35 mHfpConfig.volume = Float{HfpConfig::VOLUME_MAX};
36 }
37
setScoConfig(const ScoConfig & in_config,ScoConfig * _aidl_return)38 ndk::ScopedAStatus Bluetooth::setScoConfig(const ScoConfig& in_config, ScoConfig* _aidl_return) {
39 if (in_config.isEnabled.has_value()) {
40 mScoConfig.isEnabled = in_config.isEnabled;
41 }
42 if (in_config.isNrecEnabled.has_value()) {
43 mScoConfig.isNrecEnabled = in_config.isNrecEnabled;
44 }
45 if (in_config.mode != ScoConfig::Mode::UNSPECIFIED) {
46 mScoConfig.mode = in_config.mode;
47 }
48 if (in_config.debugName.has_value()) {
49 mScoConfig.debugName = in_config.debugName;
50 }
51 *_aidl_return = mScoConfig;
52 LOG(DEBUG) << __func__ << ": received " << in_config.toString() << ", returning "
53 << _aidl_return->toString();
54 return ndk::ScopedAStatus::ok();
55 }
56
setHfpConfig(const HfpConfig & in_config,HfpConfig * _aidl_return)57 ndk::ScopedAStatus Bluetooth::setHfpConfig(const HfpConfig& in_config, HfpConfig* _aidl_return) {
58 if (in_config.sampleRate.has_value() && in_config.sampleRate.value().value <= 0) {
59 LOG(ERROR) << __func__ << ": invalid sample rate: " << in_config.sampleRate.value().value;
60 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
61 }
62 if (in_config.volume.has_value() && (in_config.volume.value().value < HfpConfig::VOLUME_MIN ||
63 in_config.volume.value().value > HfpConfig::VOLUME_MAX)) {
64 LOG(ERROR) << __func__ << ": invalid volume: " << in_config.volume.value().value;
65 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
66 }
67
68 if (in_config.isEnabled.has_value()) {
69 mHfpConfig.isEnabled = in_config.isEnabled;
70 }
71 if (in_config.sampleRate.has_value()) {
72 mHfpConfig.sampleRate = in_config.sampleRate;
73 }
74 if (in_config.volume.has_value()) {
75 mHfpConfig.volume = in_config.volume;
76 }
77 *_aidl_return = mHfpConfig;
78 LOG(DEBUG) << __func__ << ": received " << in_config.toString() << ", returning "
79 << _aidl_return->toString();
80 return ndk::ScopedAStatus::ok();
81 }
82
isEnabled(bool * _aidl_return)83 ndk::ScopedAStatus BluetoothA2dp::isEnabled(bool* _aidl_return) {
84 *_aidl_return = mEnabled;
85 return ndk::ScopedAStatus::ok();
86 }
87
setEnabled(bool in_enabled)88 ndk::ScopedAStatus BluetoothA2dp::setEnabled(bool in_enabled) {
89 mEnabled = in_enabled;
90 LOG(DEBUG) << __func__ << ": " << mEnabled;
91 if (mHandler) return mHandler();
92 return ndk::ScopedAStatus::ok();
93 }
94
supportsOffloadReconfiguration(bool * _aidl_return)95 ndk::ScopedAStatus BluetoothA2dp::supportsOffloadReconfiguration(bool* _aidl_return) {
96 *_aidl_return = false;
97 return ndk::ScopedAStatus::ok();
98 }
99
reconfigureOffload(const std::vector<::aidl::android::hardware::audio::core::VendorParameter> & in_parameters __unused)100 ndk::ScopedAStatus BluetoothA2dp::reconfigureOffload(
101 const std::vector<::aidl::android::hardware::audio::core::VendorParameter>& in_parameters
102 __unused) {
103 LOG(DEBUG) << __func__ << ": " << ::android::internal::ToString(in_parameters);
104 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
105 }
106
isEnabled(bool * _aidl_return)107 ndk::ScopedAStatus BluetoothLe::isEnabled(bool* _aidl_return) {
108 *_aidl_return = mEnabled;
109 return ndk::ScopedAStatus::ok();
110 }
111
setEnabled(bool in_enabled)112 ndk::ScopedAStatus BluetoothLe::setEnabled(bool in_enabled) {
113 mEnabled = in_enabled;
114 if (mHandler) return mHandler();
115 return ndk::ScopedAStatus::ok();
116 }
117
supportsOffloadReconfiguration(bool * _aidl_return)118 ndk::ScopedAStatus BluetoothLe::supportsOffloadReconfiguration(bool* _aidl_return) {
119 *_aidl_return = false;
120 return ndk::ScopedAStatus::ok();
121 }
122
reconfigureOffload(const std::vector<::aidl::android::hardware::audio::core::VendorParameter> & in_parameters __unused)123 ndk::ScopedAStatus BluetoothLe::reconfigureOffload(
124 const std::vector<::aidl::android::hardware::audio::core::VendorParameter>& in_parameters
125 __unused) {
126 LOG(DEBUG) << __func__ << ": " << ::android::internal::ToString(in_parameters);
127 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
128 }
129
130 } // namespace aidl::android::hardware::audio::core
131