1 /*
2  * Copyright 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 #include "btm_sco_hfp_hal.h"
18 
19 #include <vector>
20 
21 #include "common/init_flags.h"
22 #include "device/include/esco_parameters.h"
23 #include "internal_include/bt_target.h"
24 #include "osi/include/properties.h"
25 
26 namespace hfp_hal_interface {
27 namespace {
28 bool offload_supported = true;
29 bool offload_enabled = true;
30 std::vector<bt_codec> cached_codecs;
31 }  // namespace
32 
33 // Android implementation only has consts. Initialize CVSD and MSBC to PCM
34 // offloaded defaults.
init()35 void init() {
36   bt_codec cvsd = {
37       .codec = codec::CVSD,
38       .data_path = ESCO_DATA_PATH_PCM,
39   };
40 
41   bt_codec msbc = {
42       .codec = codec::MSBC,
43       .data_path = ESCO_DATA_PATH_PCM,
44   };
45 
46   cached_codecs.clear();
47   cached_codecs.emplace_back(cvsd);
48   cached_codecs.emplace_back(msbc);
49 }
50 
51 // This is not used in Android.
is_coding_format_supported(esco_coding_format_t)52 bool is_coding_format_supported(esco_coding_format_t /* coding_format */) {
53   return true;
54 }
55 
56 // Android statically compiles WBS support.
get_wbs_supported()57 bool get_wbs_supported() { return true; }
58 
get_swb_supported()59 bool get_swb_supported() {
60   return osi_property_get_bool("bluetooth.hfp.swb.supported", false);
61 }
62 
63 // Checks the supported codecs
get_codec_capabilities(uint64_t codecs)64 bt_codecs get_codec_capabilities(uint64_t codecs) {
65   bt_codecs codec_list = {.offload_capable = offload_supported};
66 
67   for (auto c : cached_codecs) {
68     if (c.codec & codecs) {
69       codec_list.codecs.push_back(c);
70     }
71   }
72 
73   return codec_list;
74 }
75 
76 // Check if hardware offload is supported
get_offload_supported()77 bool get_offload_supported() { return offload_supported; }
78 
79 // Check if hardware offload is enabled
get_offload_enabled()80 bool get_offload_enabled() { return offload_supported && offload_enabled; }
81 
82 // Set offload enable/disable
enable_offload(bool enable)83 bool enable_offload(bool enable) {
84   if (!offload_supported) {
85     return false;
86   }
87   offload_enabled = enable;
88   return true;
89 }
90 
91 // On Android, this is a no-op because the settings default to offloaded case.
set_codec_datapath(int)92 void set_codec_datapath(int /* codec_uuid */) {}
93 
94 // No packet size limits on Android since it will be offloaded.
get_packet_size(int)95 size_t get_packet_size(int /* codec */) { return kDefaultPacketSize; }
96 
notify_sco_connection_change(RawAddress,bool,int)97 void notify_sco_connection_change(RawAddress /* device */,
98                                   bool /* is_connected */, int /* codec */) {
99   // Do nothing since this is handled by Android's audio hidl.
100 }
101 
102 // On Android, this is a no-op because the settings default to work for Android.
update_esco_parameters(enh_esco_params_t *)103 void update_esco_parameters(enh_esco_params_t* /* p_parms */) {}
104 }  // namespace hfp_hal_interface
105