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