1 /****************************************************************************** 2 * 3 * Copyright (c) 2014 The Android Open Source Project 4 * Copyright 2003-2012 Broadcom Corporation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 ******************************************************************************/ 19 20 #include <cstdint> 21 #include <unordered_set> 22 23 #include "bta/hf_client/bta_hf_client_at.h" 24 #include "bta/include/bta_hf_client_api.h" 25 #include "bta/sys/bta_sys.h" 26 #include "osi/include/alarm.h" 27 #include "stack/include/bt_types.h" 28 #include "types/raw_address.h" 29 30 /***************************************************************************** 31 * Constants 32 ****************************************************************************/ 33 34 /* RFCOMM MTU SIZE */ 35 #define BTA_HF_CLIENT_MTU 256 36 37 /* profile role for connection */ 38 #define BTA_HF_CLIENT_ACP 0 /* accepted connection */ 39 #define BTA_HF_CLIENT_INT 1 /* initiating connection */ 40 41 /* Time (in milliseconds) to wait for retry in case of collision */ 42 #ifndef BTA_HF_CLIENT_COLLISION_TIMER_MS 43 #define BTA_HF_CLIENT_COLLISION_TIMER_MS 2411 44 #endif 45 46 /* Maximum number of HF devices supported simultaneously */ 47 #define HF_CLIENT_MAX_DEVICES 10 48 49 enum { 50 /* these events are handled by the state machine */ 51 BTA_HF_CLIENT_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_HS), 52 BTA_HF_CLIENT_API_CLOSE_EVT, 53 BTA_HF_CLIENT_API_AUDIO_OPEN_EVT, 54 BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT, 55 BTA_HF_CLIENT_RFC_OPEN_EVT, 56 BTA_HF_CLIENT_RFC_CLOSE_EVT, 57 BTA_HF_CLIENT_RFC_SRV_CLOSE_EVT, 58 BTA_HF_CLIENT_RFC_DATA_EVT, 59 BTA_HF_CLIENT_DISC_ACP_RES_EVT, 60 BTA_HF_CLIENT_DISC_INT_RES_EVT, 61 BTA_HF_CLIENT_DISC_OK_EVT, 62 BTA_HF_CLIENT_DISC_FAIL_EVT, 63 BTA_HF_CLIENT_SCO_OPEN_EVT, 64 BTA_HF_CLIENT_SCO_CLOSE_EVT, 65 BTA_HF_CLIENT_SEND_AT_CMD_EVT, 66 BTA_HF_CLIENT_MAX_EVT, 67 68 /* these events are handled outside of the state machine */ 69 BTA_HF_CLIENT_API_ENABLE_EVT, 70 BTA_HF_CLIENT_API_DISABLE_EVT 71 }; 72 73 /* AT Command enum */ 74 enum { 75 BTA_HF_CLIENT_AT_NONE, 76 BTA_HF_CLIENT_AT_BRSF, 77 BTA_HF_CLIENT_AT_BAC, 78 BTA_HF_CLIENT_AT_CIND, 79 BTA_HF_CLIENT_AT_CIND_STATUS, 80 BTA_HF_CLIENT_AT_CMER, 81 BTA_HF_CLIENT_AT_CHLD, 82 BTA_HF_CLIENT_AT_CMEE, 83 BTA_HF_CLIENT_AT_BIA, 84 BTA_HF_CLIENT_AT_CLIP, 85 BTA_HF_CLIENT_AT_CCWA, 86 BTA_HF_CLIENT_AT_COPS, 87 BTA_HF_CLIENT_AT_CLCC, 88 BTA_HF_CLIENT_AT_BVRA, 89 BTA_HF_CLIENT_AT_VGS, 90 BTA_HF_CLIENT_AT_VGM, 91 BTA_HF_CLIENT_AT_ATD, 92 BTA_HF_CLIENT_AT_BLDN, 93 BTA_HF_CLIENT_AT_ATA, 94 BTA_HF_CLIENT_AT_CHUP, 95 BTA_HF_CLIENT_AT_BTRH, 96 BTA_HF_CLIENT_AT_VTS, 97 BTA_HF_CLIENT_AT_BCC, 98 BTA_HF_CLIENT_AT_BCS, 99 BTA_HF_CLIENT_AT_CNUM, 100 BTA_HF_CLIENT_AT_NREC, 101 BTA_HF_CLIENT_AT_BINP, 102 BTA_HF_CLIENT_AT_BIND_SET_IND, 103 BTA_HF_CLIENT_AT_BIND_READ_SUPPORTED_IND, 104 BTA_HF_CLIENT_AT_BIND_READ_ENABLED_IND, 105 BTA_HF_CLIENT_AT_BIEV, 106 BTA_HF_CLIENT_AT_VENDOR_SPECIFIC, 107 }; 108 109 /***************************************************************************** 110 * Data types 111 ****************************************************************************/ 112 /* data type for BTA_HF_CLIENT_API_OPEN_EVT */ 113 typedef struct { 114 BT_HDR_RIGID hdr; 115 RawAddress bd_addr; 116 uint16_t* handle; 117 } tBTA_HF_CLIENT_API_OPEN; 118 119 /* data type for BTA_HF_CLIENT_DISC_RESULT_EVT */ 120 typedef struct { 121 BT_HDR_RIGID hdr; 122 uint16_t status; 123 } tBTA_HF_CLIENT_DISC_RESULT; 124 125 /* data type for RFCOMM events */ 126 typedef struct { 127 BT_HDR_RIGID hdr; 128 uint16_t port_handle; 129 } tBTA_HF_CLIENT_RFC; 130 131 /* generic purpose data type for other events */ 132 typedef struct { 133 BT_HDR_RIGID hdr; 134 bool bool_val; 135 uint8_t uint8_val; 136 uint32_t uint32_val1; 137 uint32_t uint32_val2; 138 char str[BTA_HF_CLIENT_NUMBER_LEN + 1]; 139 } tBTA_HF_CLIENT_DATA_VAL; 140 141 /* union of all event datatypes */ 142 typedef union { 143 BT_HDR_RIGID hdr; 144 tBTA_HF_CLIENT_API_OPEN api_open; 145 tBTA_HF_CLIENT_DISC_RESULT disc_result; 146 tBTA_HF_CLIENT_RFC rfc; 147 tBTA_HF_CLIENT_DATA_VAL val; 148 149 } tBTA_HF_CLIENT_DATA; 150 151 /* First handle for the control block */ 152 #define BTA_HF_CLIENT_CB_FIRST_HANDLE 1 153 154 /* sco states */ 155 enum { 156 BTA_HF_CLIENT_SCO_SHUTDOWN_ST, /* no listening, no connection */ 157 BTA_HF_CLIENT_SCO_LISTEN_ST, /* listening */ 158 BTA_HF_CLIENT_SCO_OPENING_ST, /* connection opening */ 159 BTA_HF_CLIENT_SCO_OPEN_CL_ST, /* opening connection being closed */ 160 BTA_HF_CLIENT_SCO_OPEN_ST, /* open */ 161 BTA_HF_CLIENT_SCO_CLOSING_ST, /* closing */ 162 BTA_HF_CLIENT_SCO_CLOSE_OP_ST, /* closing sco being opened */ 163 BTA_HF_CLIENT_SCO_SHUTTING_ST /* sco shutting down */ 164 }; 165 166 /* type for HF control block */ 167 typedef struct { 168 // Fields useful for particular control block. 169 uint8_t handle; /* Handle of the control block to be 170 used by upper layer */ 171 RawAddress peer_addr; /* peer bd address */ 172 tSDP_DISCOVERY_DB* p_disc_db; /* pointer to discovery database */ 173 uint16_t conn_handle; /* RFCOMM handle of connected service */ 174 tBTA_HF_CLIENT_PEER_FEAT peer_features; /* peer device features */ 175 tBTA_HF_CLIENT_CHLD_FEAT chld_features; /* call handling features */ 176 uint16_t peer_version; /* profile version of peer device */ 177 uint8_t peer_scn; /* peer scn */ 178 uint8_t role; /* initiator/acceptor role */ 179 uint16_t sco_idx; /* SCO handle */ 180 uint8_t sco_state; /* SCO state variable */ 181 bool sco_close_rfc; /* true if also close RFCOMM after SCO */ 182 tBTM_SCO_CODEC_TYPE negotiated_codec; /* negotiated codec */ 183 bool svc_conn; /* set to true when service level connection is up */ 184 bool send_at_reply; /* set to true to notify framework about AT results */ 185 tBTA_HF_CLIENT_AT_CB at_cb; /* AT Parser control block */ 186 uint8_t state; /* state machine state */ 187 bool is_allocated; /* if the control block is already allocated */ 188 alarm_t* collision_timer; /* Collision timer */ 189 std::unordered_set<int> 190 peer_hf_indicators; /* peer supported hf indicator indices (HFP1.7) */ 191 std::unordered_set<int> 192 enabled_hf_indicators; /* enabled hf indicator indices (HFP1.7) */ 193 } tBTA_HF_CLIENT_CB; 194 195 typedef struct { 196 // Common fields, should be taken out. 197 uint32_t sdp_handle; 198 uint8_t scn; 199 tBTA_HF_CLIENT_CBACK* p_cback; /* application callback */ 200 tBTA_HF_CLIENT_FEAT features; /* features registered by application */ 201 uint16_t serv_handle; /* RFCOMM server handle */ 202 bool deregister; /* true if service shutting down */ 203 204 // Maximum number of control blocks supported by the BTA layer. 205 tBTA_HF_CLIENT_CB cb[HF_CLIENT_MAX_DEVICES]; 206 } tBTA_HF_CLIENT_CB_ARR; 207 208 extern tBTA_HF_CLIENT_CB_ARR bta_hf_client_cb_arr; 209 210 /***************************************************************************** 211 * Function prototypes 212 ****************************************************************************/ 213 214 /* main functions */ 215 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle); 216 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda( 217 const RawAddress& bd_addr); 218 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_rfc_handle(uint16_t handle); 219 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle); 220 extern bool bta_hf_client_hdl_event(BT_HDR_RIGID* p_msg); 221 extern void bta_hf_client_sm_execute(uint16_t event, 222 tBTA_HF_CLIENT_DATA* p_data); 223 extern void bta_hf_client_slc_seq(tBTA_HF_CLIENT_CB* client_cb, bool error); 224 extern bool bta_hf_client_allocate_handle(const RawAddress& bd_addr, 225 uint16_t* p_handle); 226 extern void bta_hf_client_app_callback(uint16_t event, tBTA_HF_CLIENT* data); 227 extern void bta_hf_client_collision_cback(tBTA_SYS_CONN_STATUS status, 228 uint8_t id, uint8_t app_id, 229 const RawAddress& peer_addr); 230 extern void bta_hf_client_resume_open(tBTA_HF_CLIENT_CB* client_cb); 231 extern tBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback, 232 tBTA_HF_CLIENT_FEAT features, 233 const char* p_service_name); 234 235 extern void bta_hf_client_api_disable(void); 236 extern void bta_hf_client_dump_statistics(int fd); 237 extern void bta_hf_client_cb_arr_init(void); 238 239 /* SDP functions */ 240 extern bool bta_hf_client_add_record(char* p_service_name, uint8_t scn, 241 tBTA_HF_CLIENT_FEAT features, 242 uint32_t sdp_handle); 243 extern void bta_hf_client_create_record(tBTA_HF_CLIENT_CB_ARR* client_cb, 244 const char* p_data); 245 extern void bta_hf_client_del_record(tBTA_HF_CLIENT_CB_ARR* client_cb); 246 extern bool bta_hf_client_sdp_find_attr(tBTA_HF_CLIENT_CB* client_cb); 247 extern void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb); 248 extern void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA* p_data); 249 250 /* RFCOMM functions */ 251 extern void bta_hf_client_setup_port(uint16_t handle); 252 extern void bta_hf_client_start_server(); 253 extern void bta_hf_client_close_server(); 254 extern void bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA* p_data); 255 extern void bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA* p_data); 256 257 /* SCO functions */ 258 extern void bta_hf_client_sco_listen(tBTA_HF_CLIENT_DATA* p_data); 259 extern void bta_hf_client_sco_conn_open(tBTA_HF_CLIENT_DATA* p_data); 260 extern void bta_hf_client_sco_conn_close(tBTA_HF_CLIENT_DATA* p_data); 261 extern void bta_hf_client_sco_open(tBTA_HF_CLIENT_DATA* p_data); 262 extern void bta_hf_client_sco_close(tBTA_HF_CLIENT_DATA* p_data); 263 extern void bta_hf_client_sco_shutdown(tBTA_HF_CLIENT_CB* client_cb); 264 extern void bta_hf_client_cback_sco(tBTA_HF_CLIENT_CB* client_cb, 265 uint8_t event); 266 267 /* AT command functions */ 268 extern void bta_hf_client_at_parse(tBTA_HF_CLIENT_CB* client_cb, char* buf, 269 unsigned int len); 270 extern void bta_hf_client_send_at_brsf(tBTA_HF_CLIENT_CB* client_cb, 271 tBTA_HF_CLIENT_FEAT features); 272 extern void bta_hf_client_send_at_bac(tBTA_HF_CLIENT_CB* client_cb); 273 extern void bta_hf_client_send_at_cind(tBTA_HF_CLIENT_CB* client_cb, 274 bool status); 275 extern void bta_hf_client_send_at_cmer(tBTA_HF_CLIENT_CB* client_cb, 276 bool activate); 277 extern void bta_hf_client_send_at_chld(tBTA_HF_CLIENT_CB* client_cb, char cmd, 278 uint32_t idx); 279 extern void bta_hf_client_send_at_bind(tBTA_HF_CLIENT_CB* client_cb, int step); 280 extern void bta_hf_client_send_at_biev(tBTA_HF_CLIENT_CB* client_cb, int ind_id, 281 int value); 282 extern void bta_hf_client_send_at_clip(tBTA_HF_CLIENT_CB* client_cb, 283 bool activate); 284 extern void bta_hf_client_send_at_ccwa(tBTA_HF_CLIENT_CB* client_cb, 285 bool activate); 286 extern void bta_hf_client_send_at_cmee(tBTA_HF_CLIENT_CB* client_cb, 287 bool activate); 288 extern void bta_hf_client_send_at_cops(tBTA_HF_CLIENT_CB* client_cb, 289 bool query); 290 extern void bta_hf_client_send_at_clcc(tBTA_HF_CLIENT_CB* client_cb); 291 extern void bta_hf_client_send_at_bvra(tBTA_HF_CLIENT_CB* client_cb, 292 bool enable); 293 extern void bta_hf_client_send_at_vgs(tBTA_HF_CLIENT_CB* client_cb, 294 uint32_t volume); 295 extern void bta_hf_client_send_at_vgm(tBTA_HF_CLIENT_CB* client_cb, 296 uint32_t volume); 297 extern void bta_hf_client_send_at_atd(tBTA_HF_CLIENT_CB* client_cb, 298 char* number, uint32_t memory); 299 extern void bta_hf_client_send_at_bldn(tBTA_HF_CLIENT_CB* client_cb); 300 extern void bta_hf_client_send_at_ata(tBTA_HF_CLIENT_CB* client_cb); 301 extern void bta_hf_client_send_at_chup(tBTA_HF_CLIENT_CB* client_cb); 302 extern void bta_hf_client_send_at_btrh(tBTA_HF_CLIENT_CB* client_cb, bool query, 303 uint32_t val); 304 extern void bta_hf_client_send_at_vts(tBTA_HF_CLIENT_CB* client_cb, char code); 305 extern void bta_hf_client_send_at_bcc(tBTA_HF_CLIENT_CB* client_cb); 306 extern void bta_hf_client_send_at_bcs(tBTA_HF_CLIENT_CB* client_cb, 307 uint32_t codec); 308 extern void bta_hf_client_send_at_cnum(tBTA_HF_CLIENT_CB* client_cb); 309 extern void bta_hf_client_send_at_nrec(tBTA_HF_CLIENT_CB* client_cb); 310 extern void bta_hf_client_send_at_binp(tBTA_HF_CLIENT_CB* client_cb, 311 uint32_t action); 312 extern void bta_hf_client_send_at_bia(tBTA_HF_CLIENT_CB* client_cb); 313 314 /* AT API Functions */ 315 void bta_hf_client_at_init(tBTA_HF_CLIENT_CB* client_cb); 316 void bta_hf_client_at_reset(tBTA_HF_CLIENT_CB* client_cb); 317 extern void bta_hf_client_ind(tBTA_HF_CLIENT_CB* client_cb, 318 tBTA_HF_CLIENT_IND_TYPE type, uint16_t value); 319 extern void bta_hf_client_evt_val(tBTA_HF_CLIENT_CB* client_cb, 320 tBTA_HF_CLIENT_EVT type, uint16_t value); 321 extern void bta_hf_client_operator_name(tBTA_HF_CLIENT_CB* client_name, 322 char* name); 323 extern void bta_hf_client_clip(tBTA_HF_CLIENT_CB* client_cb, char* number); 324 extern void bta_hf_client_ccwa(tBTA_HF_CLIENT_CB* client_cb, char* number); 325 extern void bta_hf_client_at_result(tBTA_HF_CLIENT_CB* client_cb, 326 tBTA_HF_CLIENT_AT_RESULT_TYPE type, 327 uint16_t cme); 328 extern void bta_hf_client_clcc(tBTA_HF_CLIENT_CB* client_cb, uint32_t idx, 329 bool incoming, uint8_t status, bool mpty, 330 char* number); 331 extern void bta_hf_client_cnum(tBTA_HF_CLIENT_CB* client_cb, char* number, 332 uint16_t service); 333 extern void bta_hf_client_binp(tBTA_HF_CLIENT_CB* client_cb, char* number); 334 335 /* Action functions */ 336 extern void bta_hf_client_start_close(tBTA_HF_CLIENT_DATA* p_data); 337 extern void bta_hf_client_start_open(tBTA_HF_CLIENT_DATA* p_data); 338 extern void bta_hf_client_rfc_acp_open(tBTA_HF_CLIENT_DATA* p_data); 339 extern void bta_hf_client_rfc_open(tBTA_HF_CLIENT_DATA* p_data); 340 extern void bta_hf_client_rfc_fail(tBTA_HF_CLIENT_DATA* p_data); 341 extern void bta_hf_client_disc_fail(tBTA_HF_CLIENT_DATA* p_data); 342 extern void bta_hf_client_open_fail(tBTA_HF_CLIENT_DATA* p_data); 343 extern void bta_hf_client_rfc_close(tBTA_HF_CLIENT_DATA* p_data); 344 extern void bta_hf_client_disc_acp_res(tBTA_HF_CLIENT_DATA* p_data); 345 extern void bta_hf_client_rfc_data(tBTA_HF_CLIENT_DATA* p_data); 346 extern void bta_hf_client_disc_int_res(tBTA_HF_CLIENT_DATA* p_data); 347 extern void bta_hf_client_svc_conn_open(tBTA_HF_CLIENT_DATA* p_data); 348 349 /* Commands handling functions */ 350 extern void bta_hf_client_dial(tBTA_HF_CLIENT_DATA* p_data); 351 extern void bta_hf_client_send_at_cmd(tBTA_HF_CLIENT_DATA* p_data); 352