1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #ifndef BTM_API_TYPES_H
20 #define BTM_API_TYPES_H
21 
22 #include <base/strings/stringprintf.h>
23 
24 #include <cstdint>
25 #include <string>
26 
27 #include "stack/include/bt_dev_class.h"
28 #include "stack/include/bt_hdr.h"
29 #include "stack/include/hcidefs.h"
30 #include "stack/include/sdpdefs.h"
31 #include "types/raw_address.h"
32 
33 /* Structure returned with Vendor Specific Command complete callback */
34 typedef struct {
35   uint16_t opcode;
36   uint16_t param_len;
37   uint8_t* p_param_buf;
38 } tBTM_VSC_CMPL;
39 
40 /**************************************************
41  *  Device Control and General Callback Functions
42  **************************************************/
43 /* General callback function for notifying an application that a synchronous
44  * BTM function is complete. The pointer contains the address of any returned
45  * data.
46  */
47 typedef void(tBTM_CMPL_CB)(void* p1);
48 
49 /* VSC callback function for notifying an application that a synchronous
50  * BTM function is complete. The pointer contains the address of any returned
51  * data.
52  */
53 typedef void(tBTM_VSC_CMPL_CB)(tBTM_VSC_CMPL* p1);
54 
55 constexpr uint8_t PHY_LE_NO_PACKET = 0x00;
56 constexpr uint8_t PHY_LE_1M = 0x01;
57 constexpr uint8_t PHY_LE_2M = 0x02;
58 constexpr uint8_t PHY_LE_CODED = 0x04;
59 
60 constexpr uint8_t NO_ADI_PRESENT = 0xFF;
61 constexpr uint8_t TX_POWER_NOT_PRESENT = 0x7F;
62 
63 /******************
64  *  SCO Constants
65  ******************/
66 
67 /* Define an invalid SCO index and an invalid HCI handle */
68 #define BTM_INVALID_SCO_INDEX 0xFFFF
69 
70 #define BTM_SCO_LINK_ONLY_MASK \
71   (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | ESCO_PKT_TYPES_MASK_HV3)
72 
73 #define BTM_ESCO_LINK_ONLY_MASK \
74   (ESCO_PKT_TYPES_MASK_EV3 | ESCO_PKT_TYPES_MASK_EV4 | ESCO_PKT_TYPES_MASK_EV5)
75 
76 /***************
77  *  SCO Types
78  ***************/
79 #define BTM_LINK_TYPE_SCO HCI_LINK_TYPE_SCO
80 #define BTM_LINK_TYPE_ESCO HCI_LINK_TYPE_ESCO
81 typedef uint8_t tBTM_SCO_TYPE;
82 
83 /*******************
84  * SCO Codec Types
85  *******************/
86 // TODO(b/285458890) This should use common definitions
87 #define BTM_SCO_CODEC_NONE 0x0000
88 #define BTM_SCO_CODEC_CVSD 0x0001
89 #define BTM_SCO_CODEC_MSBC 0x0002
90 #define BTM_SCO_CODEC_LC3 0x0004
91 #define BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK 0x0008
92 #define BTA_AG_SCO_APTX_SWB_SETTINGS_Q1_MASK 0x0016
93 #define BTA_AG_SCO_APTX_SWB_SETTINGS_Q2_MASK 0x0032
94 #define BTA_AG_SCO_APTX_SWB_SETTINGS_Q3_MASK 0x0064
95 
96 /**
97  * enum value is defined based on HFP spec, Codec ID section
98  */
99 enum class tBTA_AG_UUID_CODEC : uint16_t {
100   BTA_AG_SCO_APTX_SWB_SETTINGS_Q0 = 0,
101   UUID_CODEC_NONE = 0,
102   UUID_CODEC_CVSD = 0x0001 /* CVSD */,
103   UUID_CODEC_MSBC = 0x0002 /* mSBC */,
104   UUID_CODEC_LC3 = 0x0003 /* LC3 */,
105 };
106 
107 typedef uint16_t tBTM_SCO_CODEC_TYPE;
108 
109 /***************************
110  *  SCO Callback Functions
111  ***************************/
112 typedef void(tBTM_SCO_CB)(uint16_t sco_inx);
113 
114 /***************
115  *  eSCO Types
116  ***************/
117 /* tBTM_ESCO_CBACK event types */
118 #define BTM_ESCO_CONN_REQ_EVT 2
119 typedef uint8_t tBTM_ESCO_EVT;
120 
121 /* Returned by BTM_ReadEScoLinkParms() */
122 struct tBTM_ESCO_DATA {
123   RawAddress bd_addr;
124   uint8_t link_type; /* BTM_LINK_TYPE_SCO or BTM_LINK_TYPE_ESCO */
125 };
126 
127 typedef struct {
128   uint16_t sco_inx;
129   RawAddress bd_addr;
130   DEV_CLASS dev_class;
131   tBTM_SCO_TYPE link_type;
132 } tBTM_ESCO_CONN_REQ_EVT_DATA;
133 
134 typedef union {
135   tBTM_ESCO_CONN_REQ_EVT_DATA conn_evt;
136 } tBTM_ESCO_EVT_DATA;
137 
138 /***************************
139  *  eSCO Callback Functions
140  ***************************/
141 typedef void(tBTM_ESCO_CBACK)(tBTM_ESCO_EVT event, tBTM_ESCO_EVT_DATA* p_data);
142 
143 /**************************
144  * SCO Types for Debugging and Testing
145  **************************/
146 
147 /* Define the structure for the WBS/SWB packet status dump.  */
148 typedef struct {
149   uint64_t begin_ts_raw_us;
150   uint64_t end_ts_raw_us;
151   std::string status_in_hex;
152   std::string status_in_binary;
153 } tBTM_SCO_PKT_STATUS_DATA;
154 
155 /* Returned by BTM_GetScoDebugDump */
156 typedef struct {
157   bool is_active;
158   uint16_t codec_id;
159   int total_num_decoded_frames;
160   double pkt_loss_ratio;
161   tBTM_SCO_PKT_STATUS_DATA latest_data;
162 } tBTM_SCO_DEBUG_DUMP;
163 
sco_codec_type_text(tBTM_SCO_CODEC_TYPE codec_type)164 inline std::string sco_codec_type_text(tBTM_SCO_CODEC_TYPE codec_type) {
165   switch (codec_type) {
166     case BTM_SCO_CODEC_CVSD:
167       return "CVSD";
168     case BTM_SCO_CODEC_MSBC:
169       return "MSBC";
170     case BTM_SCO_CODEC_LC3:
171       return "LC3";
172     default:
173       return "UNKNOWN";
174   }
175 }
176 
sco_codec_type_to_id(tBTM_SCO_CODEC_TYPE codec_type)177 inline uint16_t sco_codec_type_to_id(tBTM_SCO_CODEC_TYPE codec_type) {
178   switch (codec_type) {
179     case BTM_SCO_CODEC_CVSD:
180       return static_cast<std::underlying_type_t<tBTA_AG_UUID_CODEC>>(
181               tBTA_AG_UUID_CODEC::UUID_CODEC_CVSD);
182     case BTM_SCO_CODEC_MSBC:
183       return static_cast<std::underlying_type_t<tBTA_AG_UUID_CODEC>>(
184               tBTA_AG_UUID_CODEC::UUID_CODEC_MSBC);
185     case BTM_SCO_CODEC_LC3:
186       return static_cast<std::underlying_type_t<tBTA_AG_UUID_CODEC>>(
187               tBTA_AG_UUID_CODEC::UUID_CODEC_LC3);
188     default:
189       return 0;
190   }
191 }
192 
bta_ag_uuid_codec_text(const tBTA_AG_UUID_CODEC result)193 inline std::string bta_ag_uuid_codec_text(const tBTA_AG_UUID_CODEC result) {
194   switch (result) {
195     CASE_RETURN_TEXT(tBTA_AG_UUID_CODEC::UUID_CODEC_NONE);
196     CASE_RETURN_TEXT(tBTA_AG_UUID_CODEC::UUID_CODEC_CVSD);
197     CASE_RETURN_TEXT(tBTA_AG_UUID_CODEC::UUID_CODEC_MSBC);
198     CASE_RETURN_TEXT(tBTA_AG_UUID_CODEC::UUID_CODEC_LC3);
199     default:
200       return std::format("UNKNOWN Codec with id {}",
201                          static_cast<std::underlying_type_t<tBTA_AG_UUID_CODEC>>(result));
202   }
203 }
204 #endif  // BTM_API_TYPES_H
205