1 /******************************************************************************
3 * Copyright (c) 2016 The Android Open Source Project
4 * Copyright 2003-2012 Broadcom Corporation
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 ******************************************************************************/
23 #include "bta/hf_client/bta_hf_client_int.h"
24 #include "bta/include/utl.h"
25 #include "osi/include/allocator.h"
26 #include "osi/include/osi.h" // UNUSED_ATTR
27 #include "stack/include/btm_api.h"
29 static const char* bta_hf_client_evt_str(uint16_t event);
30 static const char* bta_hf_client_state_str(uint8_t state);
31 void bta_hf_client_cb_init(tBTA_HF_CLIENT_CB* client_cb, uint16_t handle);
33 /* state machine states */
35 BTA_HF_CLIENT_INIT_ST,
36 BTA_HF_CLIENT_OPENING_ST,
37 BTA_HF_CLIENT_OPEN_ST,
38 BTA_HF_CLIENT_CLOSING_ST
41 /* state machine action enumeration list */
43 BTA_HF_CLIENT_RFC_DO_CLOSE,
44 BTA_HF_CLIENT_START_CLOSE,
45 BTA_HF_CLIENT_START_OPEN,
46 BTA_HF_CLIENT_RFC_ACP_OPEN,
47 BTA_HF_CLIENT_SCO_LISTEN,
48 BTA_HF_CLIENT_SCO_CONN_OPEN,
49 BTA_HF_CLIENT_SCO_CONN_CLOSE,
50 BTA_HF_CLIENT_SCO_OPEN,
51 BTA_HF_CLIENT_SCO_CLOSE,
52 BTA_HF_CLIENT_FREE_DB,
53 BTA_HF_CLIENT_OPEN_FAIL,
54 BTA_HF_CLIENT_RFC_OPEN,
55 BTA_HF_CLIENT_RFC_FAIL,
56 BTA_HF_CLIENT_DISC_INT_RES,
57 BTA_HF_CLIENT_RFC_DO_OPEN,
58 BTA_HF_CLIENT_DISC_FAIL,
59 BTA_HF_CLIENT_RFC_CLOSE,
60 BTA_HF_CLIENT_RFC_DATA,
61 BTA_HF_CLIENT_DISC_ACP_RES,
62 BTA_HF_CLIENT_SVC_CONN_OPEN,
63 BTA_HF_CLIENT_SEND_AT_CMD,
64 BTA_HF_CLIENT_NUM_ACTIONS,
67 #define BTA_HF_CLIENT_IGNORE BTA_HF_CLIENT_NUM_ACTIONS
69 /* type for action functions */
70 typedef void (*tBTA_HF_CLIENT_ACTION)(tBTA_HF_CLIENT_DATA* p_data);
72 /* action functions table, indexed with action enum */
73 const tBTA_HF_CLIENT_ACTION bta_hf_client_action[] = {
74 /* BTA_HF_CLIENT_RFC_DO_CLOSE */ bta_hf_client_rfc_do_close,
75 /* BTA_HF_CLIENT_START_CLOSE */ bta_hf_client_start_close,
76 /* BTA_HF_CLIENT_START_OPEN */ bta_hf_client_start_open,
77 /* BTA_HF_CLIENT_RFC_ACP_OPEN */ bta_hf_client_rfc_acp_open,
78 /* BTA_HF_CLIENT_SCO_LISTEN */ NULL,
79 /* BTA_HF_CLIENT_SCO_CONN_OPEN */ bta_hf_client_sco_conn_open,
80 /* BTA_HF_CLIENT_SCO_CONN_CLOSE*/ bta_hf_client_sco_conn_close,
81 /* BTA_HF_CLIENT_SCO_OPEN */ bta_hf_client_sco_open,
82 /* BTA_HF_CLIENT_SCO_CLOSE */ bta_hf_client_sco_close,
83 /* BTA_HF_CLIENT_FREE_DB */ bta_hf_client_free_db,
84 /* BTA_HF_CLIENT_OPEN_FAIL */ bta_hf_client_open_fail,
85 /* BTA_HF_CLIENT_RFC_OPEN */ bta_hf_client_rfc_open,
86 /* BTA_HF_CLIENT_RFC_FAIL */ bta_hf_client_rfc_fail,
87 /* BTA_HF_CLIENT_DISC_INT_RES */ bta_hf_client_disc_int_res,
88 /* BTA_HF_CLIENT_RFC_DO_OPEN */ bta_hf_client_rfc_do_open,
89 /* BTA_HF_CLIENT_DISC_FAIL */ bta_hf_client_disc_fail,
90 /* BTA_HF_CLIENT_RFC_CLOSE */ bta_hf_client_rfc_close,
91 /* BTA_HF_CLIENT_RFC_DATA */ bta_hf_client_rfc_data,
92 /* BTA_HF_CLIENT_DISC_ACP_RES */ bta_hf_client_disc_acp_res,
93 /* BTA_HF_CLIENT_SVC_CONN_OPEN */ bta_hf_client_svc_conn_open,
94 /* BTA_HF_CLIENT_SEND_AT_CMD */ bta_hf_client_send_at_cmd,
97 /* state table information */
98 #define BTA_HF_CLIENT_ACTIONS 2 /* number of actions */
99 #define BTA_HF_CLIENT_NEXT_STATE 2 /* position of next state */
100 #define BTA_HF_CLIENT_NUM_COLS 3 /* number of columns in state tables */
102 /* state table for init state */
103 const uint8_t bta_hf_client_st_init[][BTA_HF_CLIENT_NUM_COLS] = {
104 /* Event Action 1 Action 2
106 /* API_OPEN_EVT */ {BTA_HF_CLIENT_START_OPEN, BTA_HF_CLIENT_IGNORE,
107 BTA_HF_CLIENT_OPENING_ST},
108 /* API_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
109 BTA_HF_CLIENT_INIT_ST},
110 /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
111 BTA_HF_CLIENT_INIT_ST},
112 /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
113 BTA_HF_CLIENT_INIT_ST},
114 /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_RFC_ACP_OPEN, BTA_HF_CLIENT_IGNORE,
115 BTA_HF_CLIENT_OPEN_ST},
116 /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
117 BTA_HF_CLIENT_INIT_ST},
118 /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
119 BTA_HF_CLIENT_INIT_ST},
120 /* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
121 BTA_HF_CLIENT_INIT_ST},
122 /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
123 BTA_HF_CLIENT_INIT_ST},
124 /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
125 BTA_HF_CLIENT_INIT_ST},
126 /* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
127 BTA_HF_CLIENT_INIT_ST},
128 /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
129 BTA_HF_CLIENT_INIT_ST},
130 /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
131 BTA_HF_CLIENT_INIT_ST},
132 /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
133 BTA_HF_CLIENT_INIT_ST},
134 /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
135 BTA_HF_CLIENT_INIT_ST},
138 /* state table for opening state */
139 const uint8_t bta_hf_client_st_opening[][BTA_HF_CLIENT_NUM_COLS] = {
140 /* Event Action 1 Action 2
142 /* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
143 BTA_HF_CLIENT_OPENING_ST},
144 /* API_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_DO_CLOSE, BTA_HF_CLIENT_IGNORE,
145 BTA_HF_CLIENT_CLOSING_ST},
146 /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
147 BTA_HF_CLIENT_OPENING_ST},
148 /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
149 BTA_HF_CLIENT_OPENING_ST},
150 /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_RFC_OPEN, BTA_HF_CLIENT_IGNORE,
151 BTA_HF_CLIENT_OPEN_ST},
152 /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_FAIL, BTA_HF_CLIENT_IGNORE,
153 BTA_HF_CLIENT_INIT_ST},
154 /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
155 BTA_HF_CLIENT_OPENING_ST},
156 /* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
157 BTA_HF_CLIENT_OPENING_ST},
158 /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
159 BTA_HF_CLIENT_OPENING_ST},
160 /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_DISC_INT_RES, BTA_HF_CLIENT_IGNORE,
161 BTA_HF_CLIENT_OPENING_ST},
162 /* DISC_OK_EVT */ {BTA_HF_CLIENT_RFC_DO_OPEN, BTA_HF_CLIENT_IGNORE,
163 BTA_HF_CLIENT_OPENING_ST},
164 /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_DISC_FAIL, BTA_HF_CLIENT_IGNORE,
165 BTA_HF_CLIENT_INIT_ST},
166 /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
167 BTA_HF_CLIENT_OPENING_ST},
168 /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
169 BTA_HF_CLIENT_OPENING_ST},
170 /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
171 BTA_HF_CLIENT_OPENING_ST},
174 /* state table for open state */
175 const uint8_t bta_hf_client_st_open[][BTA_HF_CLIENT_NUM_COLS] = {
176 /* Event Action 1 Action 2
178 /* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
179 BTA_HF_CLIENT_OPEN_ST},
180 /* API_CLOSE_EVT */ {BTA_HF_CLIENT_START_CLOSE, BTA_HF_CLIENT_IGNORE,
181 BTA_HF_CLIENT_CLOSING_ST},
182 /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_SCO_OPEN, BTA_HF_CLIENT_IGNORE,
183 BTA_HF_CLIENT_OPEN_ST},
184 /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_SCO_CLOSE, BTA_HF_CLIENT_IGNORE,
185 BTA_HF_CLIENT_OPEN_ST},
186 /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
187 BTA_HF_CLIENT_OPEN_ST},
188 /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_CLOSE, BTA_HF_CLIENT_IGNORE,
189 BTA_HF_CLIENT_INIT_ST},
190 /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
191 BTA_HF_CLIENT_OPEN_ST},
192 /* RFC_DATA_EVT */ {BTA_HF_CLIENT_RFC_DATA, BTA_HF_CLIENT_IGNORE,
193 BTA_HF_CLIENT_OPEN_ST},
194 /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_DISC_ACP_RES, BTA_HF_CLIENT_IGNORE,
195 BTA_HF_CLIENT_OPEN_ST},
196 /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
197 BTA_HF_CLIENT_OPEN_ST},
198 /* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
199 BTA_HF_CLIENT_OPEN_ST},
200 /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
201 BTA_HF_CLIENT_OPEN_ST},
202 /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_SCO_CONN_OPEN, BTA_HF_CLIENT_IGNORE,
203 BTA_HF_CLIENT_OPEN_ST},
204 /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_SCO_CONN_CLOSE, BTA_HF_CLIENT_IGNORE,
205 BTA_HF_CLIENT_OPEN_ST},
206 /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_SEND_AT_CMD, BTA_HF_CLIENT_IGNORE,
207 BTA_HF_CLIENT_OPEN_ST},
210 /* state table for closing state */
211 const uint8_t bta_hf_client_st_closing[][BTA_HF_CLIENT_NUM_COLS] = {
212 /* Event Action 1 Action 2
214 /* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
215 BTA_HF_CLIENT_CLOSING_ST},
216 /* API_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
217 BTA_HF_CLIENT_CLOSING_ST},
218 /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
219 BTA_HF_CLIENT_CLOSING_ST},
220 /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
221 BTA_HF_CLIENT_CLOSING_ST},
222 /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
223 BTA_HF_CLIENT_CLOSING_ST},
224 /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_CLOSE, BTA_HF_CLIENT_IGNORE,
225 BTA_HF_CLIENT_INIT_ST},
226 /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
227 BTA_HF_CLIENT_CLOSING_ST},
228 /* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
229 BTA_HF_CLIENT_CLOSING_ST},
230 /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
231 BTA_HF_CLIENT_CLOSING_ST},
232 /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
233 BTA_HF_CLIENT_INIT_ST},
234 /* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
235 BTA_HF_CLIENT_CLOSING_ST},
236 /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
237 BTA_HF_CLIENT_CLOSING_ST},
238 /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
239 BTA_HF_CLIENT_CLOSING_ST},
240 /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
241 BTA_HF_CLIENT_CLOSING_ST},
242 /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
243 BTA_HF_CLIENT_CLOSING_ST},
246 /* type for state table */
247 typedef const uint8_t (*tBTA_HF_CLIENT_ST_TBL)[BTA_HF_CLIENT_NUM_COLS];
250 const tBTA_HF_CLIENT_ST_TBL bta_hf_client_st_tbl[] = {
251 bta_hf_client_st_init, bta_hf_client_st_opening, bta_hf_client_st_open,
252 bta_hf_client_st_closing};
254 /* HF Client control block */
255 tBTA_HF_CLIENT_CB_ARR bta_hf_client_cb_arr;
257 /* Event handler for the state machine */
258 static const tBTA_SYS_REG bta_hf_client_reg = {bta_hf_client_hdl_event,
259 BTA_HfClientDisable};
261 /*******************************************************************************
263 * Function bta_hf_client_cb_arr_init
265 * Description Initialize entire control block array set
270 ******************************************************************************/
271 void bta_hf_client_cb_arr_init() {
272 memset(&bta_hf_client_cb_arr, 0, sizeof(tBTA_HF_CLIENT_CB_ARR));
274 // reset the handles and make the CBs non-allocated
275 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
276 // Allocate the handles in increasing order of indices
277 bta_hf_client_cb_init(&(bta_hf_client_cb_arr.cb[i]), i);
278 bta_hf_client_cb_arr.cb[i].handle = BTA_HF_CLIENT_CB_FIRST_HANDLE + i;
282 /*******************************************************************************
284 * Function bta_hf_client_cb_init
286 * Description Initialize an HF_Client service control block. Assign the
287 * handle to cb->handle.
293 ******************************************************************************/
294 void bta_hf_client_cb_init(tBTA_HF_CLIENT_CB* client_cb, uint16_t handle) {
295 APPL_TRACE_DEBUG("%s", __func__);
297 // Free any memory we need to explicity release
298 alarm_free(client_cb->collision_timer);
300 // release unique pointers
301 client_cb->enabled_hf_indicators.clear();
302 client_cb->peer_hf_indicators.clear();
304 // Memset the rest of the block
305 // memset(client_cb, 0, sizeof(tBTA_HF_CLIENT_CB));
308 // Re allocate any variables required
309 client_cb->collision_timer = alarm_new("bta_hf_client.scb_collision_timer");
310 client_cb->handle = handle;
311 client_cb->sco_idx = BTM_INVALID_SCO_INDEX;
314 /*******************************************************************************
316 * Function bta_hf_client_resume_open
318 * Description Resume opening process.
323 ******************************************************************************/
324 void bta_hf_client_resume_open(tBTA_HF_CLIENT_CB* client_cb) {
325 APPL_TRACE_DEBUG("%s", __func__);
327 /* resume opening process. */
328 if (client_cb->state == BTA_HF_CLIENT_INIT_ST) {
329 client_cb->state = BTA_HF_CLIENT_OPENING_ST;
330 tBTA_HF_CLIENT_DATA msg;
331 msg.hdr.layer_specific = client_cb->handle;
332 msg.api_open.bd_addr = client_cb->peer_addr;
333 bta_hf_client_start_open(&msg);
337 /*******************************************************************************
339 * Function bta_hf_client_collision_timer_cback
341 * Description HF Client connection collision timer callback
346 ******************************************************************************/
347 static void bta_hf_client_collision_timer_cback(void* data) {
348 APPL_TRACE_DEBUG("%s", __func__);
349 tBTA_HF_CLIENT_CB* client_cb = (tBTA_HF_CLIENT_CB*)data;
351 /* If the peer haven't opened connection, restart opening process */
352 bta_hf_client_resume_open(client_cb);
355 /*******************************************************************************
357 * Function bta_hf_client_collision_cback
359 * Description Get notified about collision.
364 ******************************************************************************/
365 void bta_hf_client_collision_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status,
366 uint8_t id, UNUSED_ATTR uint8_t app_id,
367 const RawAddress& peer_addr) {
368 tBTA_HF_CLIENT_CB* client_cb = bta_hf_client_find_cb_by_bda(peer_addr);
369 if (client_cb != NULL && client_cb->state == BTA_HF_CLIENT_OPENING_ST) {
370 if (id == BTA_ID_SYS) /* ACL collision */
372 APPL_TRACE_WARNING("HF Client found collision (ACL) ...");
373 } else if (id == BTA_ID_HS) /* RFCOMM collision */
375 APPL_TRACE_WARNING("HF Client found collision (RFCOMM) ...");
377 APPL_TRACE_WARNING("HF Client found collision (\?\?\?) ...");
380 client_cb->state = BTA_HF_CLIENT_INIT_ST;
382 /* Cancel SDP if it had been started. */
383 if (client_cb->p_disc_db) {
384 (void)SDP_CancelServiceSearch(client_cb->p_disc_db);
385 osi_free_and_reset((void**)&client_cb->p_disc_db);
388 /* reopen registered server */
389 /* Collision may be detected before or after we close servers. */
390 bta_hf_client_start_server();
392 /* Start timer to handle connection opening restart */
393 alarm_set_on_mloop(client_cb->collision_timer,
394 BTA_HF_CLIENT_COLLISION_TIMER_MS,
395 bta_hf_client_collision_timer_cback, (void*)client_cb);
399 /*******************************************************************************
401 * Function bta_hf_client_api_enable
403 * Description Handle an API enable event.
408 ******************************************************************************/
409 tBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback,
410 tBTA_HF_CLIENT_FEAT features,
411 const char* p_service_name) {
412 /* If already registered then return error */
413 if (bta_sys_is_register(BTA_ID_HS)) {
414 APPL_TRACE_ERROR("BTA HF Client is already enabled, ignoring ...");
418 /* register with BTA system manager */
419 bta_sys_register(BTA_ID_HS, &bta_hf_client_reg);
421 /* reset the control blocks */
422 bta_hf_client_cb_arr_init();
424 bta_hf_client_cb_arr.p_cback = p_cback;
425 bta_hf_client_cb_arr.features = features;
427 /* create SDP records */
428 bta_hf_client_create_record(&bta_hf_client_cb_arr, p_service_name);
430 /* set same setting as AG does */
431 BTM_WriteVoiceSettings(AG_VOICE_SETTINGS);
433 bta_sys_collision_register(BTA_ID_HS, bta_hf_client_collision_cback);
435 /* Set the Audio service class bit */
437 cod.service = BTM_COD_SERVICE_AUDIO;
438 utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
440 /* start RFCOMM server */
441 bta_hf_client_start_server();
446 /*******************************************************************************
448 * Function bta_hf_client_find_cb_by_handle
450 * Description Finds the control block by handle provided
452 * handle: Handle as obtained from BTA_HfClientOpen call
455 * Returns Control block corresponding to the handle and NULL if
458 ******************************************************************************/
459 tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle) {
460 // Handles are limited from 1 through HF_CLIENT_MAX_DEVICES
461 if (handle < 1 || handle > HF_CLIENT_MAX_DEVICES) {
462 APPL_TRACE_ERROR("%s: handle out of range (%d, %d) %d", __func__, 1,
463 HF_CLIENT_MAX_DEVICES, handle);
467 // Check if the associated index is allocated. Index is (handle - 1).
468 if (bta_hf_client_cb_arr.cb[handle - 1].is_allocated)
469 return &(bta_hf_client_cb_arr.cb[handle - 1]);
471 APPL_TRACE_ERROR("%s: block not found for handle %d", __func__, handle);
475 /*******************************************************************************
477 * Function bta_hf_client_find_cb_by_bda
479 * Description Finds the control block by handle provided
481 * bda: address of the device to find the handle for.
482 * Since there can only be one HF connection for a device
483 * we should always find a unique block
485 * Returns Control block corresponding to the address and NULL if
488 ******************************************************************************/
489 tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(const RawAddress& peer_addr) {
490 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
491 // Check if the associated index is allocated and that BD ADDR matches
492 tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
493 if (client_cb->is_allocated && peer_addr == client_cb->peer_addr) {
496 APPL_TRACE_WARNING("%s: bdaddr mismatch for handle %d alloc %d", __func__,
497 i, client_cb->is_allocated);
500 APPL_TRACE_ERROR("%s: block not found", __func__);
504 /*******************************************************************************
506 * Function bta_hf_client_find_cb_by_rfc_handle
508 * Description Finds the control block by RFC handle provided.
510 * handle: RFC handle for the established connection
513 * Returns Control block corresponding to the handle and NULL if none
516 ******************************************************************************/
517 tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_rfc_handle(uint16_t handle) {
518 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
519 tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
520 bool is_allocated = client_cb->is_allocated;
521 uint16_t conn_handle = client_cb->conn_handle;
523 APPL_TRACE_DEBUG("%s: cb rfc_handle %d alloc %d conn_handle %d", __func__,
524 handle, is_allocated, conn_handle);
526 if (is_allocated && conn_handle == handle) {
530 APPL_TRACE_WARNING("%s: no cb yet %d alloc %d conn_handle %d", __func__,
531 handle, is_allocated, conn_handle);
534 APPL_TRACE_ERROR("%s: no cb found for rfc handle %d", __func__, handle);
538 /*******************************************************************************
540 * Function bta_hf_client_find_cb_by_sco_handle
542 * Description Finds the control block by sco handle provided
547 * Returns Control block corresponding to the sco handle and
548 * none if none exists
550 ******************************************************************************/
551 tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle) {
552 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
553 tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
554 if (client_cb->is_allocated && client_cb->sco_idx == handle) {
558 APPL_TRACE_ERROR("%s: block not found for handle %d", __func__, handle);
562 /*******************************************************************************
564 * Function bta_hf_client_allocate_handle
566 * Description Allocates a handle for the new BD ADDR that needs a new RF
567 * channel for HF connection. If the channel cannot be created
568 * for a reason then false is returned
570 * bd_addr: Address of the device for which this block is
571 * being created. Single device can only have one block.
572 * p_handle: OUT variable to store the outcome of allocate. If
573 * allocate failed then value is not valid
576 * Returns true if the creation of p_handle succeeded, false otherwise
578 ******************************************************************************/
579 bool bta_hf_client_allocate_handle(const RawAddress& bd_addr,
580 uint16_t* p_handle) {
581 tBTA_HF_CLIENT_CB* existing_cb = bta_hf_client_find_cb_by_bda(bd_addr);
582 if (existing_cb != NULL) {
583 BTIF_TRACE_ERROR("%s: cannot allocate handle since BDADDR already exists",
587 /* Check that we do not have a request to for same device in the control
589 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
590 tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
591 if (client_cb->is_allocated) {
592 APPL_TRACE_WARNING("%s: control block already used index %d", __func__,
597 // Reset the client control block
598 bta_hf_client_cb_init(client_cb, client_cb->handle);
600 *p_handle = client_cb->handle;
601 APPL_TRACE_DEBUG("%s: marking CB handle %d to true", __func__,
604 client_cb->is_allocated = true;
605 client_cb->peer_addr = bd_addr;
606 bta_hf_client_at_init(client_cb);
611 APPL_TRACE_ERROR("%s: all control blocks in use!", __func__);
614 /*******************************************************************************
616 * Function bta_hf_client_app_callback
618 * Description Calls the application callback
623 ******************************************************************************/
624 void bta_hf_client_app_callback(uint16_t event, tBTA_HF_CLIENT* data) {
625 if (bta_hf_client_cb_arr.p_cback != NULL) {
626 bta_hf_client_cb_arr.p_cback(event, data);
630 /*******************************************************************************
632 * Function bta_hf_client_api_disable
634 * Description Handle an API disable event.
639 ******************************************************************************/
640 void bta_hf_client_api_disable() {
641 if (!bta_sys_is_register(BTA_ID_HS)) {
642 APPL_TRACE_WARNING("BTA HF Client is already disabled, ignoring ...");
646 /* Remove the collision handler */
647 bta_sys_collision_register(BTA_ID_HS, NULL);
649 bta_hf_client_cb_arr.deregister = true;
651 /* remove sdp record */
652 bta_hf_client_del_record(&bta_hf_client_cb_arr);
654 /* remove rfcomm server */
655 bta_hf_client_close_server();
657 /* reinit the control block */
658 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
659 if (bta_hf_client_cb_arr.cb[i].is_allocated) {
660 bta_hf_client_cb_init(&(bta_hf_client_cb_arr.cb[i]), i);
664 /* De-register with BTA system manager */
665 bta_sys_deregister(BTA_ID_HS);
668 /*******************************************************************************
670 * Function bta_hf_client_hdl_event
672 * Description Data HF Client main event handling function.
677 ******************************************************************************/
678 bool bta_hf_client_hdl_event(BT_HDR_RIGID* p_msg) {
679 APPL_TRACE_DEBUG("%s: %s (0x%x)", __func__,
680 bta_hf_client_evt_str(p_msg->event), p_msg->event);
681 bta_hf_client_sm_execute(p_msg->event, (tBTA_HF_CLIENT_DATA*)p_msg);
685 /*******************************************************************************
687 * Function bta_hf_client_sm_execute
689 * Description State machine event handling function for HF Client
694 ******************************************************************************/
695 void bta_hf_client_sm_execute(uint16_t event, tBTA_HF_CLIENT_DATA* p_data) {
696 tBTA_HF_CLIENT_CB* client_cb =
697 bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
698 if (client_cb == NULL) {
699 APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
700 p_data->hdr.layer_specific);
704 tBTA_HF_CLIENT_ST_TBL state_table;
708 uint16_t in_event = event;
709 uint8_t in_state = client_cb->state;
711 /* Ignore displaying of AT results when not connected (Ignored in state
713 if (client_cb->state == BTA_HF_CLIENT_OPEN_ST) {
714 APPL_TRACE_EVENT("HF Client evt : State %d (%s), Event 0x%04x (%s)",
716 bta_hf_client_state_str(client_cb->state), event,
717 bta_hf_client_evt_str(event));
721 if (event >= (BTA_HF_CLIENT_MAX_EVT & 0x00FF)) {
722 APPL_TRACE_ERROR("HF Client evt out of range, ignoring...");
726 /* look up the state table for the current state */
727 state_table = bta_hf_client_st_tbl[client_cb->state];
730 client_cb->state = state_table[event][BTA_HF_CLIENT_NEXT_STATE];
732 /* execute action functions */
733 for (i = 0; i < BTA_HF_CLIENT_ACTIONS; i++) {
734 action = state_table[event][i];
735 if (action != BTA_HF_CLIENT_IGNORE) {
736 (*bta_hf_client_action[action])(p_data);
742 /* If the state has changed then notify the app of the corresponding change */
743 if (in_state != client_cb->state) {
744 VLOG(1) << __func__ << ": notifying state change to " << in_state << " -> "
745 << client_cb->state << " device " << client_cb->peer_addr;
747 memset(&evt, 0, sizeof(evt));
748 evt.bd_addr = client_cb->peer_addr;
749 if (client_cb->state == BTA_HF_CLIENT_INIT_ST) {
750 bta_hf_client_app_callback(BTA_HF_CLIENT_CLOSE_EVT, &evt);
751 APPL_TRACE_DEBUG("%s: marking CB handle %d to false", __func__, client_cb->handle);
752 client_cb->is_allocated = false;
753 } else if (client_cb->state == BTA_HF_CLIENT_OPEN_ST) {
754 evt.open.handle = client_cb->handle;
755 bta_hf_client_app_callback(BTA_HF_CLIENT_OPEN_EVT, &evt);
759 VLOG(2) << __func__ << ": device " << client_cb->peer_addr
760 << "state change: [" << bta_hf_client_state_str(in_state) << "] -> ["
761 << bta_hf_client_state_str(client_cb->state) << "] after Event ["
762 << bta_hf_client_evt_str(in_event) << "]";
765 static void send_post_slc_cmd(tBTA_HF_CLIENT_CB* client_cb) {
766 client_cb->at_cb.current_cmd = BTA_HF_CLIENT_AT_NONE;
768 tBTA_HF_CLIENT_DATA p_data;
769 p_data.hdr.layer_specific = client_cb->handle;
770 bta_hf_client_sco_listen(&p_data);
771 bta_hf_client_send_at_bia(client_cb);
772 bta_hf_client_send_at_ccwa(client_cb, true);
773 bta_hf_client_send_at_cmee(client_cb, true);
774 bta_hf_client_send_at_cops(client_cb, false);
775 bta_hf_client_send_at_btrh(client_cb, true, 0);
776 bta_hf_client_send_at_clip(client_cb, true);
779 /*******************************************************************************
781 * Function bta_hf_client_slc_seq
783 * Description Handles AT commands sequence required for SLC creation
788 ******************************************************************************/
789 void bta_hf_client_slc_seq(tBTA_HF_CLIENT_CB* client_cb, bool error) {
790 APPL_TRACE_DEBUG("bta_hf_client_slc_seq cmd: %u",
791 client_cb->at_cb.current_cmd);
794 /* SLC establishment error, sent close rfcomm event */
796 "HFPClient: Failed to create SLC due to AT error, disconnecting (%u)",
797 client_cb->at_cb.current_cmd);
799 tBTA_HF_CLIENT_DATA msg;
800 msg.hdr.layer_specific = client_cb->handle;
801 bta_hf_client_sm_execute(BTA_HF_CLIENT_API_CLOSE_EVT, &msg);
805 if (client_cb->svc_conn) {
806 APPL_TRACE_WARNING("%s: SLC already connected for CB handle %d", __func__,
811 switch (client_cb->at_cb.current_cmd) {
812 case BTA_HF_CLIENT_AT_NONE:
813 bta_hf_client_send_at_brsf(client_cb, bta_hf_client_cb_arr.features);
816 case BTA_HF_CLIENT_AT_BRSF:
817 if ((bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_CODEC) &&
818 (client_cb->peer_features & BTA_HF_CLIENT_PEER_CODEC)) {
819 bta_hf_client_send_at_bac(client_cb);
823 bta_hf_client_send_at_cind(client_cb, false);
826 case BTA_HF_CLIENT_AT_BAC:
827 bta_hf_client_send_at_cind(client_cb, false);
830 case BTA_HF_CLIENT_AT_CIND:
831 bta_hf_client_send_at_cind(client_cb, true);
834 case BTA_HF_CLIENT_AT_CIND_STATUS:
835 bta_hf_client_send_at_cmer(client_cb, true);
838 case BTA_HF_CLIENT_AT_CMER:
839 if (client_cb->peer_features & BTA_HF_CLIENT_PEER_FEAT_3WAY &&
840 bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_3WAY) {
841 bta_hf_client_send_at_chld(client_cb, '?', 0);
842 } else if (bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_HF_IND &&
843 client_cb->peer_features & BTA_HF_CLIENT_PEER_HF_IND) {
844 bta_hf_client_send_at_bind(client_cb, 0);
846 tBTA_HF_CLIENT_DATA msg;
847 msg.hdr.layer_specific = client_cb->handle;
848 bta_hf_client_svc_conn_open(&msg);
849 send_post_slc_cmd(client_cb);
853 case BTA_HF_CLIENT_AT_CHLD:
854 if (bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_HF_IND &&
855 client_cb->peer_features & BTA_HF_CLIENT_PEER_HF_IND) {
856 bta_hf_client_send_at_bind(client_cb, 0);
858 tBTA_HF_CLIENT_DATA msg;
859 msg.hdr.layer_specific = client_cb->handle;
860 bta_hf_client_svc_conn_open(&msg);
861 send_post_slc_cmd(client_cb);
865 case BTA_HF_CLIENT_AT_BIND_SET_IND:
866 bta_hf_client_send_at_bind(client_cb, 1);
869 case BTA_HF_CLIENT_AT_BIND_READ_SUPPORTED_IND:
870 bta_hf_client_send_at_bind(client_cb, 2);
873 case BTA_HF_CLIENT_AT_BIND_READ_ENABLED_IND:
874 tBTA_HF_CLIENT_DATA msg;
875 msg.hdr.layer_specific = client_cb->handle;
876 bta_hf_client_svc_conn_open(&msg);
877 send_post_slc_cmd(client_cb);
881 /* If happen there is a bug in SLC creation procedure... */
883 "HFPClient: Failed to create SLCdue to unexpected AT command, "
884 "disconnecting (%u)",
885 client_cb->at_cb.current_cmd);
887 tBTA_HF_CLIENT_DATA msg;
888 msg.hdr.layer_specific = client_cb->handle;
889 bta_hf_client_sm_execute(BTA_HF_CLIENT_API_CLOSE_EVT, &msg);
895 #ifndef CASE_RETURN_STR
896 #define CASE_RETURN_STR(const) \
901 static const char* bta_hf_client_evt_str(uint16_t event) {
903 CASE_RETURN_STR(BTA_HF_CLIENT_API_OPEN_EVT)
904 CASE_RETURN_STR(BTA_HF_CLIENT_API_CLOSE_EVT)
905 CASE_RETURN_STR(BTA_HF_CLIENT_API_AUDIO_OPEN_EVT)
906 CASE_RETURN_STR(BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT)
907 CASE_RETURN_STR(BTA_HF_CLIENT_RFC_OPEN_EVT)
908 CASE_RETURN_STR(BTA_HF_CLIENT_RFC_CLOSE_EVT)
909 CASE_RETURN_STR(BTA_HF_CLIENT_RFC_SRV_CLOSE_EVT)
910 CASE_RETURN_STR(BTA_HF_CLIENT_RFC_DATA_EVT)
911 CASE_RETURN_STR(BTA_HF_CLIENT_DISC_ACP_RES_EVT)
912 CASE_RETURN_STR(BTA_HF_CLIENT_DISC_INT_RES_EVT)
913 CASE_RETURN_STR(BTA_HF_CLIENT_DISC_OK_EVT)
914 CASE_RETURN_STR(BTA_HF_CLIENT_DISC_FAIL_EVT)
915 CASE_RETURN_STR(BTA_HF_CLIENT_API_ENABLE_EVT)
916 CASE_RETURN_STR(BTA_HF_CLIENT_API_DISABLE_EVT)
917 CASE_RETURN_STR(BTA_HF_CLIENT_SCO_OPEN_EVT)
918 CASE_RETURN_STR(BTA_HF_CLIENT_SCO_CLOSE_EVT)
919 CASE_RETURN_STR(BTA_HF_CLIENT_SEND_AT_CMD_EVT)
921 return "Unknown HF Client Event";
925 static const char* bta_hf_client_state_str(uint8_t state) {
927 CASE_RETURN_STR(BTA_HF_CLIENT_INIT_ST)
928 CASE_RETURN_STR(BTA_HF_CLIENT_OPENING_ST)
929 CASE_RETURN_STR(BTA_HF_CLIENT_OPEN_ST)
930 CASE_RETURN_STR(BTA_HF_CLIENT_CLOSING_ST)
932 return "Unknown HF Client State";
936 void bta_hf_client_dump_statistics(int fd) {
937 dprintf(fd, "\nBluetooth HF Client BTA Statistics\n");
939 // We dump statistics for all control blocks
940 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
941 tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
942 if (!client_cb->is_allocated) {
943 // Skip the blocks which are not allocated
947 dprintf(fd, " Control block #%d\n", i + 1);
949 uint8_t* a = client_cb->peer_addr.address;
951 dprintf(fd, " Peer Device: %02x:%02x:%02x:%02x:%02x:%02x\n", a[0], a[1],
952 a[2], a[3], a[4], a[5]);
954 // State machine state
955 dprintf(fd, " State Machine State: %s\n",
956 bta_hf_client_state_str(client_cb->state));
958 // Local RFC channelfor communication
959 dprintf(fd, " RFCOMM Channel (local) %d\n", client_cb->conn_handle);
961 // BTA Handle shared between BTA and client (ex BTIF)
962 dprintf(fd, " BTA Generated handle %d\n", client_cb->handle);