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