1 /****************************************************************************** 2 * 3 * Copyright 2003-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 /****************************************************************************** 20 * 21 * This is the private interface file for the BTA audio gateway. 22 * 23 ******************************************************************************/ 24 #ifndef BTA_AG_INT_H 25 #define BTA_AG_INT_H 26 27 #include <cstdint> 28 29 #include "bta/ag/bta_ag_at.h" 30 #include "bta/include/bta_ag_api.h" 31 #include "bta/include/bta_api.h" 32 #include "bta/sys/bta_sys.h" 33 #include "stack/include/bt_hdr.h" 34 #include "types/raw_address.h" 35 36 /***************************************************************************** 37 * Constants 38 ****************************************************************************/ 39 /* Time to wait for retry in case of collision */ 40 #ifndef BTA_AG_COLLISION_TIMEOUT_MS 41 #define BTA_AG_COLLISION_TIMEOUT_MS (2 * 1000) /* 2 seconds */ 42 #endif 43 44 /* RFCOMM MTU SIZE */ 45 #define BTA_AG_MTU 256 46 47 /* Max number of peer and local HF indicators */ 48 #define BTA_AG_MAX_NUM_PEER_HF_IND 20 49 #define BTA_AG_MAX_NUM_LOCAL_HF_IND 4 50 51 /* Internal profile indexes */ 52 #define BTA_AG_HSP 0 /* index for HSP */ 53 #define BTA_AG_HFP 1 /* index for HFP */ 54 #define BTA_AG_NUM_IDX 2 /* number of profile indexes */ 55 56 /* profile role for connection */ 57 #define BTA_AG_ACP 0 /* accepted connection */ 58 #define BTA_AG_INT 1 /* initiating connection */ 59 60 #define BTA_AG_SDP_FEAT_SPEC \ 61 (BTA_AG_FEAT_3WAY | BTA_AG_FEAT_ECNR | BTA_AG_FEAT_VREC | \ 62 BTA_AG_FEAT_INBAND | BTA_AG_FEAT_VTAG) 63 64 /* Timeout for alarm in 2018 toyota camry carkit workaround */ 65 #define BTA_AG_BIND_TIMEOUT_MS 500 66 67 enum { 68 /* these events are handled by the state machine */ 69 BTA_AG_API_REGISTER_EVT = BTA_SYS_EVT_START(BTA_ID_AG), 70 BTA_AG_API_DEREGISTER_EVT, 71 BTA_AG_API_OPEN_EVT, 72 BTA_AG_API_CLOSE_EVT, 73 BTA_AG_API_AUDIO_OPEN_EVT, 74 BTA_AG_API_AUDIO_CLOSE_EVT, 75 BTA_AG_API_RESULT_EVT, 76 BTA_AG_API_SETCODEC_EVT, 77 BTA_AG_RFC_OPEN_EVT, 78 BTA_AG_RFC_CLOSE_EVT, 79 BTA_AG_RFC_SRV_CLOSE_EVT, 80 BTA_AG_RFC_DATA_EVT, 81 BTA_AG_SCO_OPEN_EVT, 82 BTA_AG_SCO_CLOSE_EVT, 83 BTA_AG_DISC_ACP_RES_EVT, 84 BTA_AG_DISC_INT_RES_EVT, 85 BTA_AG_DISC_OK_EVT, 86 BTA_AG_DISC_FAIL_EVT, 87 BTA_AG_RING_TIMEOUT_EVT, 88 BTA_AG_SVC_TIMEOUT_EVT, 89 BTA_AG_COLLISION_EVT, 90 BTA_AG_MAX_EVT, 91 }; 92 93 /* Actions to perform after a SCO event */ 94 enum { 95 BTA_AG_POST_SCO_NONE, /* no action */ 96 BTA_AG_POST_SCO_CLOSE_RFC, /* close RFCOMM channel after SCO closes */ 97 BTA_AG_POST_SCO_RING, /* send RING result code after SCO opens */ 98 BTA_AG_POST_SCO_CALL_CONN, /* send call indicators after SCO opens/closes */ 99 BTA_AG_POST_SCO_CALL_ORIG, /* send call indicators after SCO closes */ 100 BTA_AG_POST_SCO_CALL_END, /* send call indicators after SCO closes */ 101 BTA_AG_POST_SCO_CALL_END_INCALL /* send call indicators for end call & 102 incoming call after SCO closes */ 103 }; 104 105 /* sco states */ 106 typedef enum : uint8_t { 107 BTA_AG_SCO_SHUTDOWN_ST, /* no sco listening, all sco connections closed */ 108 BTA_AG_SCO_LISTEN_ST, /* sco listening */ 109 BTA_AG_SCO_CODEC_ST, /* sco codec negotiation */ 110 BTA_AG_SCO_OPENING_ST, /* sco connection opening */ 111 BTA_AG_SCO_OPEN_CL_ST, /* opening sco connection being closed */ 112 BTA_AG_SCO_OPEN_XFER_ST, /* opening sco connection being transferred */ 113 BTA_AG_SCO_OPEN_ST, /* sco open */ 114 BTA_AG_SCO_CLOSING_ST, /* sco closing */ 115 BTA_AG_SCO_CLOSE_OP_ST, /* closing sco being opened */ 116 BTA_AG_SCO_CLOSE_XFER_ST, /* closing sco being transferred */ 117 BTA_AG_SCO_SHUTTING_ST /* sco shutting down */ 118 } tBTA_AG_SCO; 119 120 /***************************************************************************** 121 * Data types 122 ****************************************************************************/ 123 124 /* data type for BTA_AG_API_REGISTER_EVT */ 125 typedef struct { 126 char p_name[2][BTA_SERVICE_NAME_LEN + 1]; 127 tBTA_SERVICE_MASK services; 128 tBTA_AG_FEAT features; 129 uint8_t app_id; 130 } tBTA_AG_API_REGISTER; 131 132 /* data type for BTA_AG_API_OPEN_EVT */ 133 typedef struct { 134 RawAddress bd_addr; 135 } tBTA_AG_API_OPEN; 136 137 /* data type for BTA_AG_API_AUDIO_OPEN_EVT */ 138 typedef struct { 139 bool force_cvsd; 140 } tBTA_AG_API_AUDIO_OPEN; 141 142 /* data type for BTA_AG_API_RESULT_EVT */ 143 typedef struct { 144 tBTA_AG_RES result; 145 tBTA_AG_RES_DATA data; ToString__anon4e28512e0708146 std::string ToString() const { 147 return base::StringPrintf("result:%s", bta_ag_result_text(result).c_str()); 148 } 149 } tBTA_AG_API_RESULT; 150 151 /* data type for BTA_AG_API_SETCODEC_EVT */ 152 typedef struct { 153 tBTA_AG_PEER_CODEC codec; 154 } tBTA_AG_API_SETCODEC; 155 156 /* data type for BTA_AG_DISC_RESULT_EVT */ 157 typedef struct { 158 uint16_t status; 159 } tBTA_AG_DISC_RESULT; 160 161 /* data type for RFCOMM events */ 162 typedef struct { 163 uint16_t port_handle; 164 } tBTA_AG_RFC; 165 166 /* union of all event datatypes */ 167 union tBTA_AG_DATA { 168 tBTA_AG_API_REGISTER api_register; 169 tBTA_AG_API_OPEN api_open; 170 tBTA_AG_API_AUDIO_OPEN api_audio_open; 171 tBTA_AG_API_RESULT api_result; 172 tBTA_AG_API_SETCODEC api_setcodec; 173 tBTA_AG_DISC_RESULT disc_result; 174 tBTA_AG_RFC rfc; 175 static const tBTA_AG_DATA kEmpty; 176 177 /** 178 * Check if two tBTA_AG_DATA are equal in memory 179 * 180 * @param rhs other tBTA_AG_DATA 181 * @return true if both unions are equal in memory 182 */ 183 bool operator==(const tBTA_AG_DATA& rhs) const { 184 return (std::memcmp(this, &rhs, sizeof(tBTA_AG_DATA)) == 0); 185 } 186 187 /** 188 * Check if this union is empty by comparing it to the kEmpty constant 189 * 190 * @return true if this union is empty 191 */ IsEmpty()192 bool IsEmpty() const { return *this == kEmpty; } 193 }; 194 195 /* type for each profile */ 196 typedef struct { 197 uint32_t sdp_handle; 198 uint8_t scn; 199 } tBTA_AG_PROFILE; 200 201 typedef enum { 202 BTA_AG_SCO_MSBC_SETTINGS_T2 = 0, /* preferred/default when codec is mSBC */ 203 BTA_AG_SCO_MSBC_SETTINGS_T1, 204 } tBTA_AG_SCO_MSBC_SETTINGS; 205 206 typedef enum { 207 BTA_AG_SCO_LC3_SETTINGS_T2 = 0, /* preferred/default when codec is LC3 */ 208 BTA_AG_SCO_LC3_SETTINGS_T1, 209 } tBTA_AG_SCO_LC3_SETTINGS; 210 211 /* type for each service control block */ 212 struct tBTA_AG_SCB { 213 char clip[BTA_AG_AT_MAX_LEN + 1]; /* number string used for CLIP */ 214 uint16_t serv_handle[BTA_AG_NUM_IDX]; /* RFCOMM server handles */ 215 tBTA_AG_AT_CB at_cb; /* AT command interpreter */ 216 RawAddress peer_addr; /* peer bd address */ 217 tSDP_DISCOVERY_DB* p_disc_db; /* pointer to discovery database */ 218 tBTA_SERVICE_MASK reg_services; /* services specified in register API */ 219 tBTA_SERVICE_MASK open_services; /* services specified in open API */ 220 uint16_t conn_handle; /* RFCOMM handle of connected service */ 221 tBTA_AG_FEAT features; /* features registered by application */ 222 tBTA_AG_FEAT masked_features; /* local BRSF features for this connection */ 223 tBTA_AG_PEER_FEAT peer_features; /* peer device features */ 224 uint16_t peer_sdp_features; /* peer device SDP features */ 225 uint16_t peer_version; /* profile version of peer device */ 226 uint16_t hsp_version; /* HSP profile version before SDP */ 227 uint16_t sco_idx; /* SCO handle */ 228 bool in_use; /* scb in use */ 229 bool dealloc; /* true if service shutting down */ 230 bool clip_enabled; /* set to true if HF enables CLIP reporting */ 231 bool ccwa_enabled; /* set to true if HF enables CCWA reporting */ 232 bool cmer_enabled; /* set to true if HF enables CMER reporting */ 233 bool cmee_enabled; /* set to true if HF enables CME ERROR reporting */ 234 bool inband_enabled; /* set to true if inband ring enabled */ 235 bool svc_conn; /* set to true when service level connection up */ 236 uint8_t state; /* state machine state */ 237 uint8_t conn_service; /* connected service */ 238 uint8_t peer_scn; /* peer scn */ 239 uint8_t app_id; /* application id */ 240 uint8_t role; /* initiator/acceptor role */ 241 uint8_t post_sco; /* action to perform after sco event */ 242 uint8_t call_ind; /* CIEV call indicator value */ 243 uint8_t callsetup_ind; /* CIEV callsetup indicator value */ 244 uint8_t service_ind; /* CIEV service indicator value */ 245 uint8_t signal_ind; /* CIEV signal indicator value */ 246 uint8_t roam_ind; /* CIEV roam indicator value */ 247 uint8_t battchg_ind; /* CIEV battery charge indicator value */ 248 uint8_t callheld_ind; /* CIEV call held indicator value */ 249 uint32_t bia_masked_out; /* indicators HF does not want us to send */ 250 alarm_t* bind_timer; /* Timer for toyota camry 2018 carkit workaround */ 251 alarm_t* collision_timer; 252 alarm_t* ring_timer; 253 alarm_t* codec_negotiation_timer; 254 bool received_at_bac; /* indicate AT+BAC is received at least once */ 255 tBTA_AG_PEER_CODEC peer_codecs; /* codecs for eSCO supported by the peer */ 256 tBTA_AG_PEER_CODEC sco_codec; /* codec to be used for eSCO connection */ 257 tBTA_AG_PEER_CODEC 258 inuse_codec; /* codec being used for the current SCO connection */ 259 bool codec_updated; /* set to true whenever the app updates codec type */ 260 bool codec_fallback; /* If sco nego fails for mSBC, fallback to CVSD */ 261 tBTA_AG_SCO_MSBC_SETTINGS codec_msbc_settings; /* settings to be used for the 262 impending eSCO on WB */ 263 tBTA_AG_SCO_LC3_SETTINGS codec_lc3_settings; /* settings to be used for the 264 impending eSCO on SWB */ 265 266 tBTA_AG_HF_IND 267 peer_hf_indicators[BTA_AG_MAX_NUM_PEER_HF_IND]; /* Peer supported 268 HF indicators */ 269 tBTA_AG_HF_IND 270 local_hf_indicators[BTA_AG_MAX_NUM_LOCAL_HF_IND]; /* Local supported 271 HF indicators */ 272 ToStringtBTA_AG_SCB273 std::string ToString() const { 274 return base::StringPrintf( 275 "codec_updated=%d, codec_fallback=%d, " 276 "sco_codec=%d, peer_codec=%d, msbc_settings=%d, lc3_settings=%d, " 277 "device=%s", 278 codec_updated, codec_fallback, sco_codec, peer_codecs, 279 codec_msbc_settings, codec_lc3_settings, 280 ADDRESS_TO_LOGGABLE_CSTR(peer_addr)); 281 } 282 }; 283 284 /* type for sco data */ 285 typedef struct { 286 tBTM_ESCO_CONN_REQ_EVT_DATA conn_data; /* SCO data for pending conn request */ 287 tBTA_AG_SCB* p_curr_scb; /* SCB associated with SCO connection */ 288 tBTA_AG_SCB* p_xfer_scb; /* SCB associated with SCO transfer */ 289 uint16_t cur_idx; /* SCO handle */ 290 tBTA_AG_SCO state; /* SCO state variable */ 291 bool is_local; /* SCO connection initiated locally or remotely */ 292 } tBTA_AG_SCO_CB; 293 294 /* type for AG control block */ 295 typedef struct { 296 tBTA_AG_SCB scb[BTA_AG_MAX_NUM_CLIENTS]; /* service control blocks */ 297 tBTA_AG_PROFILE profile[BTA_AG_NUM_IDX]; /* profile-specific data */ 298 tBTA_AG_SCO_CB sco; /* SCO data */ 299 tBTA_AG_CBACK* p_cback; /* application callback */ 300 } tBTA_AG_CB; 301 302 /***************************************************************************** 303 * Global data 304 ****************************************************************************/ 305 306 /* constant lookup tables */ 307 extern const uint16_t bta_ag_uuid[BTA_AG_NUM_IDX]; 308 extern const uint8_t bta_ag_sec_id[BTA_AG_NUM_IDX]; 309 extern const tBTA_AG_AT_CMD* bta_ag_at_tbl[BTA_AG_NUM_IDX]; 310 311 /* control block declaration */ 312 extern tBTA_AG_CB bta_ag_cb; 313 314 /* config struct */ 315 extern const tBTA_AG_CFG* p_bta_ag_cfg; 316 extern const tBTA_AG_HF_IND bta_ag_local_hf_ind_cfg[]; 317 318 /***************************************************************************** 319 * Function prototypes 320 ****************************************************************************/ 321 bool bta_ag_hdl_event(BT_HDR_RIGID* p_msg); 322 323 /* API functions */ 324 void bta_ag_api_enable(tBTA_AG_CBACK* p_cback); 325 void bta_ag_api_disable(); 326 void bta_ag_api_set_active_device(const RawAddress& new_active_device); 327 void bta_ag_api_register(tBTA_SERVICE_MASK services, tBTA_AG_FEAT features, 328 const std::vector<std::string>& service_names, 329 uint8_t app_id); 330 void bta_ag_api_result(uint16_t handle, tBTA_AG_RES result, 331 const tBTA_AG_RES_DATA& result_data); 332 333 /* main functions */ 334 void bta_ag_scb_dealloc(tBTA_AG_SCB* p_scb); 335 uint16_t bta_ag_scb_to_idx(tBTA_AG_SCB* p_scb); 336 tBTA_AG_SCB* bta_ag_scb_by_idx(uint16_t idx); 337 uint8_t bta_ag_service_to_idx(tBTA_SERVICE_MASK services); 338 uint16_t bta_ag_idx_by_bdaddr(const RawAddress* peer_addr); 339 bool bta_ag_other_scb_open(tBTA_AG_SCB* p_curr_scb); 340 bool bta_ag_scb_open(tBTA_AG_SCB* p_curr_scb); 341 void bta_ag_sm_execute(tBTA_AG_SCB* p_scb, uint16_t event, 342 const tBTA_AG_DATA& data); 343 void bta_ag_sm_execute_by_handle(uint16_t handle, uint16_t event, 344 const tBTA_AG_DATA& data); 345 void bta_ag_collision_cback(tBTA_SYS_CONN_STATUS status, uint8_t id, 346 uint8_t app_id, const RawAddress& peer_addr); 347 void bta_ag_resume_open(tBTA_AG_SCB* p_scb); 348 349 /* SDP functions */ 350 bool bta_ag_add_record(uint16_t service_uuid, const char* p_service_name, 351 uint8_t scn, tBTA_AG_FEAT features, uint32_t sdp_handle); 352 void bta_ag_create_records(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 353 void bta_ag_del_records(tBTA_AG_SCB* p_scb); 354 bool bta_ag_sdp_find_attr(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service); 355 void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service); 356 void bta_ag_free_db(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 357 358 /* RFCOMM functions */ 359 void bta_ag_start_servers(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK services); 360 void bta_ag_close_servers(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK services); 361 bool bta_ag_is_server_closed(tBTA_AG_SCB* p_scb); 362 void bta_ag_rfc_do_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 363 void bta_ag_rfc_do_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 364 365 /* SCO functions */ 366 bool bta_ag_sco_is_active_device(const RawAddress& bd_addr); 367 bool bta_ag_sco_is_open(tBTA_AG_SCB* p_scb); 368 bool bta_ag_sco_is_opening(tBTA_AG_SCB* p_scb); 369 void bta_ag_sco_conn_rsp(tBTA_AG_SCB* p_scb, tBTM_ESCO_CONN_REQ_EVT_DATA* data); 370 371 /* AT command functions */ 372 void bta_ag_at_hsp_cback(tBTA_AG_SCB* p_scb, uint16_t cmd, uint8_t arg_type, 373 char* p_arg, char* p_end, int16_t int_arg); 374 void bta_ag_at_hfp_cback(tBTA_AG_SCB* p_scb, uint16_t cmd, uint8_t arg_type, 375 char* p_arg, char* p_end, int16_t int_arg); 376 void bta_ag_at_err_cback(tBTA_AG_SCB* p_scb, bool unknown, const char* p_arg); 377 bool bta_ag_inband_enabled(tBTA_AG_SCB* p_scb); 378 void bta_ag_send_call_inds(tBTA_AG_SCB* p_scb, tBTA_AG_RES result); 379 380 /* Action functions */ 381 void bta_ag_register(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 382 void bta_ag_deregister(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 383 void bta_ag_start_dereg(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 384 void bta_ag_start_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 385 void bta_ag_start_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 386 void bta_ag_disc_int_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 387 void bta_ag_disc_acp_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 388 void bta_ag_disc_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 389 void bta_ag_open_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 390 void bta_ag_rfc_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 391 void bta_ag_rfc_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 392 void bta_ag_rfc_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 393 void bta_ag_rfc_acp_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 394 void bta_ag_rfc_data(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 395 void bta_ag_sco_listen(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 396 void bta_ag_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 397 void bta_ag_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 398 void bta_ag_sco_shutdown(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 399 void bta_ag_sco_conn_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 400 void bta_ag_sco_conn_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 401 void bta_ag_post_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 402 void bta_ag_post_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 403 void bta_ag_svc_conn_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 404 void bta_ag_result(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 405 void bta_ag_setcodec(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 406 void bta_ag_send_ring(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 407 void bta_ag_handle_collision(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data); 408 409 /* Internal utility functions */ 410 void bta_ag_sco_codec_nego(tBTA_AG_SCB* p_scb, bool result); 411 void bta_ag_codec_negotiate(tBTA_AG_SCB* p_scb); 412 bool bta_ag_is_sco_open_allowed(tBTA_AG_SCB* p_scb, const std::string event); 413 void bta_ag_send_bcs(tBTA_AG_SCB* p_scb); 414 void bta_ag_set_sco_offload_enabled(bool value); 415 void bta_ag_set_sco_allowed(bool value); 416 const RawAddress& bta_ag_get_active_device(); 417 void bta_clear_active_device(); 418 419 #endif /* BTA_AG_INT_H */ 420