1 /*
2  * Copyright 2024 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 "AIDLProviderInfo"
18 
19 #include "provider_info.h"
20 
21 #include <android/binder_manager.h>
22 #include <com_android_bluetooth_flags.h>
23 
24 #include <optional>
25 
26 #include "client_interface_aidl.h"
27 
28 namespace bluetooth::audio::aidl {
29 using ::aidl::android::hardware::bluetooth::audio::CodecId;
30 using ::aidl::android::hardware::bluetooth::audio::CodecInfo;
31 using ::aidl::android::hardware::bluetooth::audio::SessionType;
32 
recordHfpCodecInfo(CodecInfo codecInfo)33 ::hfp::sco_config recordHfpCodecInfo(CodecInfo codecInfo) {
34   auto hfp_transport = codecInfo.transport.get<CodecInfo::Transport::hfp>();
35   ::hfp::sco_config config{
36       .inputDataPath = hfp_transport.inputDataPath,
37       .outputDataPath = hfp_transport.outputDataPath,
38       .useControllerCodec = hfp_transport.useControllerCodec,
39   };
40   return config;
41 }
42 
43 std::unique_ptr<ProviderInfo>
GetProviderInfo(SessionType sessionType)44 bluetooth::audio::aidl::ProviderInfo::GetProviderInfo(SessionType sessionType) {
45   auto provider_info =
46       BluetoothAudioClientInterface::GetProviderInfo(sessionType);
47 
48   std::vector<CodecInfo> codecInfos;
49   if (provider_info.has_value()) {
50     codecInfos = std::move(provider_info->codecInfos);
51   }
52 
53   return std::make_unique<ProviderInfo>(sessionType, std::move(codecInfos));
54 }
55 
ProviderInfo(SessionType sessionType,std::vector<CodecInfo> codecs)56 ProviderInfo::ProviderInfo(SessionType sessionType,
57                            std::vector<CodecInfo> codecs)
58     : codecInfos(std::move(codecs)) {
59   for (auto codecInfo : codecInfos) {
60     if (codecInfo.id == CodecId::Core::CVSD) {
61       hfpScoConfigMap[UUID_CODEC_CVSD] = recordHfpCodecInfo(codecInfo);
62     } else if (codecInfo.id == CodecId::Core::MSBC) {
63       hfpScoConfigMap[UUID_CODEC_MSBC] = recordHfpCodecInfo(codecInfo);
64     } else if (codecInfo.id == CodecId::Core::LC3) {
65       if (sessionType == SessionType::HFP_HARDWARE_OFFLOAD_DATAPATH ||
66           sessionType == SessionType::HFP_SOFTWARE_ENCODING_DATAPATH ||
67           sessionType == SessionType::HFP_SOFTWARE_DECODING_DATAPATH) {
68         hfpScoConfigMap[UUID_CODEC_LC3] = recordHfpCodecInfo(codecInfo);
69       }
70     }
71   }
72 }
73 
74 const std::unordered_map<int, ::hfp::sco_config>&
GetHfpScoConfig()75 ProviderInfo::GetHfpScoConfig() {
76   return hfpScoConfigMap;
77 }
78 }  // namespace bluetooth::audio::aidl
79