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 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <vector>
22 
23 #include "btm_api_types.h"
24 #include "device/include/esco_parameters.h"
25 #include "internal_include/bt_target.h"
26 #include "types/raw_address.h"
27 
28 // Used by the Bluetooth stack to get WBS supported and codec, or notify SCO
29 // connection change to lower layer (kernel) when SCO-over-HCI is used. So far
30 // ChromeOS uses SCO-over-HCI; usually Android phone uses hardware SCO route so
31 // it doesn't apply here.
32 namespace hfp_hal_interface {
33 enum codec : uint64_t {
34   CVSD = 1 << 0,
35   MSBC_TRANSPARENT = 1 << 1,
36   MSBC = 1 << 2,
37   LC3 = 1 << 3,
38 };
39 
40 struct bt_codec {
41   codec codec;
42   uint8_t data_path;
43   std::vector<uint8_t> data;
44 };
45 
46 struct bt_codecs {
47   bool offload_capable;
48   std::vector<bt_codec> codecs;
49 };
50 
51 // Use default packet size for codec if this value is given.
52 constexpr size_t kDefaultPacketSize = 0;
53 
esco_coding_to_codec(esco_coding_format_t esco_coding)54 constexpr inline int esco_coding_to_codec(esco_coding_format_t esco_coding) {
55   switch (esco_coding) {
56     case ESCO_CODING_FORMAT_TRANSPNT:
57       return codec::MSBC_TRANSPARENT;
58     case ESCO_CODING_FORMAT_MSBC:
59       return codec::MSBC;
60     case ESCO_CODING_FORMAT_LC3:
61       return codec::LC3;
62 
63     // Default to CVSD encoding if unknown format.
64     case ESCO_CODING_FORMAT_CVSD:
65     default:
66       return codec::CVSD;
67   }
68 }
69 
70 // Initialize the SCO HFP HAL module
71 void init();
72 
73 // Check if specified coding format is supported by the adapter.
74 bool is_coding_format_supported(esco_coding_format_t coding_format);
75 
76 // Check if wideband speech is supported on local device.
77 bool get_wbs_supported();
78 
79 // Check if super wideband speech is supported on local device.
80 bool get_swb_supported();
81 
82 // Checks the details of the codecs (specified as a bitmask of enum codec).
83 bt_codecs get_codec_capabilities(uint64_t codecs);
84 
85 // Check if hardware offload is supported.
86 bool get_offload_supported();
87 
88 // Check if hardware offload is enabled.
89 bool get_offload_enabled();
90 
91 // Set offload enable/disable.
92 bool enable_offload(bool enable);
93 
94 // Notify the codec datapath to lower layer for offload mode.
95 void set_codec_datapath(tBTA_AG_UUID_CODEC codec_uuid);
96 
97 // Get the maximum supported packet size from the lower layer.
98 size_t get_packet_size(int codec);
99 
100 // Notify the lower layer about SCO connection change.
101 void notify_sco_connection_change(RawAddress device, bool is_connected, int codec);
102 
103 // Update eSCO parameters
104 void update_esco_parameters(enh_esco_params_t* p_parms);
105 }  // namespace hfp_hal_interface
106