bta_hf_client_main.cc revision bdcfc045f9d21afc1510cdad4fddd97a93c26477
13fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/******************************************************************************
23fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *
31025687c6679608fe9df918f37011414ffc72b62Hemant Gupta *  Copyright (c) 2014 The Android Open Source Project
43fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  Copyright (C) 2003-2012 Broadcom Corporation
53fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *
63fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  Licensed under the Apache License, Version 2.0 (the "License");
73fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  you may not use this file except in compliance with the License.
83fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  You may obtain a copy of the License at:
93fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *
103fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  http://www.apache.org/licenses/LICENSE-2.0
113fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *
123fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  Unless required by applicable law or agreed to in writing, software
133fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  distributed under the License is distributed on an "AS IS" BASIS,
143fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  See the License for the specific language governing permissions and
163fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *  limitations under the License.
173fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta *
183fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta ******************************************************************************/
193fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
201025687c6679608fe9df918f37011414ffc72b62Hemant Gupta#include <stdlib.h>
21cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#include <string.h>
22303b6ff16e1d257a491d2ae4fac2cd1b78850f67Arman Uguray
231025687c6679608fe9df918f37011414ffc72b62Hemant Gupta#include "bt_utils.h"
243fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta#include "bta_api.h"
251025687c6679608fe9df918f37011414ffc72b62Hemant Gupta#include "bta_hf_client_api.h"
261025687c6679608fe9df918f37011414ffc72b62Hemant Gupta#include "bta_hf_client_int.h"
27cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#include "bta_sys.h"
28cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#include "osi/include/osi.h"
29cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#include "osi/include/properties.h"
30bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal#include "utl.h"
313fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
32cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonextern fixed_queue_t* btu_bta_alarm_queue;
3378bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov
34bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalstatic const char* bta_hf_client_evt_str(uint16_t event);
35bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalstatic const char* bta_hf_client_state_str(uint8_t state);
36bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
37bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal/* control block declaration */
38bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalextern tBTA_HF_CLIENT_CB bta_hf_client_cb;
393fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
403fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state machine states */
41cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonenum {
42cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_INIT_ST,
43cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_OPENING_ST,
44cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_OPEN_ST,
45cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_CLOSING_ST
463fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta};
473fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
483fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state machine action enumeration list */
49cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonenum {
50cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_RFC_DO_CLOSE,
51cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_START_CLOSE,
52cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_START_OPEN,
53cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_RFC_ACP_OPEN,
54cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_SCO_LISTEN,
55cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_SCO_CONN_OPEN,
56cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_SCO_CONN_CLOSE,
57cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_SCO_OPEN,
58cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_SCO_CLOSE,
59cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_FREE_DB,
60cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_OPEN_FAIL,
61cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_RFC_OPEN,
62cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_RFC_FAIL,
63cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_DISC_INT_RES,
64cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_RFC_DO_OPEN,
65cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_DISC_FAIL,
66cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_RFC_CLOSE,
67cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_RFC_DATA,
68cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_DISC_ACP_RES,
69cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_SVC_CONN_OPEN,
70cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_SEND_AT_CMD,
71cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTA_HF_CLIENT_NUM_ACTIONS,
723fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta};
733fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
741025687c6679608fe9df918f37011414ffc72b62Hemant Gupta#define BTA_HF_CLIENT_IGNORE BTA_HF_CLIENT_NUM_ACTIONS
753fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
763fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* type for action functions */
77cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsontypedef void (*tBTA_HF_CLIENT_ACTION)(tBTA_HF_CLIENT_DATA* p_data);
783fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
791025687c6679608fe9df918f37011414ffc72b62Hemant Gupta/* action functions table, indexed with action enum */
80cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonconst tBTA_HF_CLIENT_ACTION bta_hf_client_action[] = {
81cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_RFC_DO_CLOSE */ bta_hf_client_rfc_do_close,
82cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_START_CLOSE */ bta_hf_client_start_close,
83cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_START_OPEN */ bta_hf_client_start_open,
84cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_RFC_ACP_OPEN */ bta_hf_client_rfc_acp_open,
85cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_SCO_LISTEN */ bta_hf_client_sco_listen,
86cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_SCO_CONN_OPEN */ bta_hf_client_sco_conn_open,
87cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_SCO_CONN_CLOSE*/ bta_hf_client_sco_conn_close,
88cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_SCO_OPEN */ bta_hf_client_sco_open,
89cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_SCO_CLOSE */ bta_hf_client_sco_close,
90cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_FREE_DB */ bta_hf_client_free_db,
91cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_OPEN_FAIL */ bta_hf_client_open_fail,
92cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_RFC_OPEN */ bta_hf_client_rfc_open,
93cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_RFC_FAIL */ bta_hf_client_rfc_fail,
94cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_DISC_INT_RES */ bta_hf_client_disc_int_res,
95cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_RFC_DO_OPEN */ bta_hf_client_rfc_do_open,
96cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_DISC_FAIL */ bta_hf_client_disc_fail,
97cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_RFC_CLOSE */ bta_hf_client_rfc_close,
98cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_RFC_DATA */ bta_hf_client_rfc_data,
99cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_DISC_ACP_RES */ bta_hf_client_disc_acp_res,
100cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_SVC_CONN_OPEN */ bta_hf_client_svc_conn_open,
101cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* BTA_HF_CLIENT_SEND_AT_CMD */ bta_hf_client_send_at_cmd,
1023fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta};
1033fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
1043fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state table information */
105cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#define BTA_HF_CLIENT_ACTIONS 2    /* number of actions */
106cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#define BTA_HF_CLIENT_NEXT_STATE 2 /* position of next state */
107cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#define BTA_HF_CLIENT_NUM_COLS 3   /* number of columns in state tables */
1083fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
1093fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state table for init state */
110cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonconst uint8_t bta_hf_client_st_init[][BTA_HF_CLIENT_NUM_COLS] = {
111cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* Event                    Action 1                       Action 2
112cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson       Next state */
113cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_OPEN_EVT */ {BTA_HF_CLIENT_START_OPEN, BTA_HF_CLIENT_IGNORE,
114cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPENING_ST},
115cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
116cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
117cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
118cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                              BTA_HF_CLIENT_INIT_ST},
119cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
120cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                               BTA_HF_CLIENT_INIT_ST},
121cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_RFC_ACP_OPEN, BTA_HF_CLIENT_SCO_LISTEN,
122cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPEN_ST},
123cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
124cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
125cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
126cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                             BTA_HF_CLIENT_INIT_ST},
127cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
128cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_INIT_ST},
129cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
130cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_INIT_ST},
131cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
132cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_INIT_ST},
133cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
134cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                       BTA_HF_CLIENT_INIT_ST},
135cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
136cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
137cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
138cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_INIT_ST},
139cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
140cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
141cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
142cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                           BTA_HF_CLIENT_INIT_ST},
1433fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta};
1443fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
1453fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state table for opening state */
146cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonconst uint8_t bta_hf_client_st_opening[][BTA_HF_CLIENT_NUM_COLS] = {
147cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* Event                    Action 1                       Action 2
148cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson       Next state */
149cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
150cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPENING_ST},
151cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_DO_CLOSE, BTA_HF_CLIENT_IGNORE,
152cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_CLOSING_ST},
153cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
154cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                              BTA_HF_CLIENT_OPENING_ST},
155cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
156cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                               BTA_HF_CLIENT_OPENING_ST},
157cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_RFC_OPEN, BTA_HF_CLIENT_SCO_LISTEN,
158cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPEN_ST},
159cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_FAIL, BTA_HF_CLIENT_IGNORE,
160cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
161cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
162cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                             BTA_HF_CLIENT_OPENING_ST},
163cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
164cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPENING_ST},
165cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
166cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_OPENING_ST},
167cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_DISC_INT_RES, BTA_HF_CLIENT_IGNORE,
168cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_OPENING_ST},
169cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_OK_EVT */ {BTA_HF_CLIENT_RFC_DO_OPEN, BTA_HF_CLIENT_IGNORE,
170cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                       BTA_HF_CLIENT_OPENING_ST},
171cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_DISC_FAIL, BTA_HF_CLIENT_IGNORE,
172cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
173cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
174cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPENING_ST},
175cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
176cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_OPENING_ST},
177cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
178cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                           BTA_HF_CLIENT_OPENING_ST},
1793fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta};
1803fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
1813fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state table for open state */
182cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonconst uint8_t bta_hf_client_st_open[][BTA_HF_CLIENT_NUM_COLS] = {
183cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* Event                    Action 1                       Action 2
184cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson       Next state */
185cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
186cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPEN_ST},
187cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_CLOSE_EVT */ {BTA_HF_CLIENT_START_CLOSE, BTA_HF_CLIENT_IGNORE,
188cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_CLOSING_ST},
189cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_SCO_OPEN, BTA_HF_CLIENT_IGNORE,
190cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                              BTA_HF_CLIENT_OPEN_ST},
191cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_SCO_CLOSE, BTA_HF_CLIENT_IGNORE,
192cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                               BTA_HF_CLIENT_OPEN_ST},
193cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
194cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPEN_ST},
195cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_CLOSE, BTA_HF_CLIENT_IGNORE,
196cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
197cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
198cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                             BTA_HF_CLIENT_OPEN_ST},
199cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_DATA_EVT */ {BTA_HF_CLIENT_RFC_DATA, BTA_HF_CLIENT_IGNORE,
200cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPEN_ST},
201cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_DISC_ACP_RES, BTA_HF_CLIENT_IGNORE,
202cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_OPEN_ST},
203cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
204cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_OPEN_ST},
205cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
206cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                       BTA_HF_CLIENT_OPEN_ST},
207cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
208cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_OPEN_ST},
209cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_SCO_CONN_OPEN, BTA_HF_CLIENT_IGNORE,
210cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_OPEN_ST},
211cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_SCO_CONN_CLOSE, BTA_HF_CLIENT_IGNORE,
212cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_OPEN_ST},
213cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_SEND_AT_CMD, BTA_HF_CLIENT_IGNORE,
214cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                           BTA_HF_CLIENT_OPEN_ST},
2153fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta};
2163fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
2173fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state table for closing state */
218cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonconst uint8_t bta_hf_client_st_closing[][BTA_HF_CLIENT_NUM_COLS] = {
219cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* Event                    Action 1                       Action 2
220cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson       Next state */
221cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
222cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_CLOSING_ST},
223cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
224cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_CLOSING_ST},
225cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
226cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                              BTA_HF_CLIENT_CLOSING_ST},
227cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
228cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                               BTA_HF_CLIENT_CLOSING_ST},
229cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
230cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_CLOSING_ST},
231cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_CLOSE, BTA_HF_CLIENT_IGNORE,
232cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_INIT_ST},
233cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
234cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                             BTA_HF_CLIENT_CLOSING_ST},
235cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
236cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_CLOSING_ST},
237cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
238cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_CLOSING_ST},
239cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
240cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                            BTA_HF_CLIENT_INIT_ST},
241cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
242cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                       BTA_HF_CLIENT_CLOSING_ST},
243cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
244cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_CLOSING_ST},
245cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
246cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                        BTA_HF_CLIENT_CLOSING_ST},
247cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
248cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                         BTA_HF_CLIENT_CLOSING_ST},
249cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
250cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                           BTA_HF_CLIENT_CLOSING_ST},
2513fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta};
2523fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
2533fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* type for state table */
254e9e58ced195ec2c983c7723c9cbdabd45eb0f2fdMarie Janssentypedef const uint8_t (*tBTA_HF_CLIENT_ST_TBL)[BTA_HF_CLIENT_NUM_COLS];
2553fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
2563fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/* state table */
257cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonconst tBTA_HF_CLIENT_ST_TBL bta_hf_client_st_tbl[] = {
258cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    bta_hf_client_st_init, bta_hf_client_st_opening, bta_hf_client_st_open,
259cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    bta_hf_client_st_closing};
2603fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
2611025687c6679608fe9df918f37011414ffc72b62Hemant Gupta/* HF Client control block */
262cd1fd07f1306e08fe048682dd5918987e579f937Myles WatsontBTA_HF_CLIENT_CB bta_hf_client_cb;
2633fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
264bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal/* Event handler for the state machine */
265bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalstatic const tBTA_SYS_REG bta_hf_client_reg = {bta_hf_client_hdl_event,
266bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                                               BTA_HfClientDisable};
267bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
2683fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
2698af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2708af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_scb_init
2718af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2728af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      Initialize an HF_Client service control block.
2738af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2748af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2758af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
2768af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2778af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
278cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonvoid bta_hf_client_scb_init(void) {
279cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  APPL_TRACE_DEBUG("%s", __func__);
280cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
281cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  alarm_free(bta_hf_client_cb.scb.collision_timer);
282cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  alarm_free(bta_hf_client_cb.scb.at_cb.resp_timer);
283cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  alarm_free(bta_hf_client_cb.scb.at_cb.hold_timer);
284cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  memset(&bta_hf_client_cb.scb, 0, sizeof(tBTA_HF_CLIENT_SCB));
285cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  bta_hf_client_cb.scb.collision_timer =
28678bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov      alarm_new("bta_hf_client.scb_collision_timer");
287cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  bta_hf_client_cb.scb.sco_idx = BTM_INVALID_SCO_INDEX;
288cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  bta_hf_client_cb.scb.negotiated_codec = BTM_SCO_CODEC_CVSD;
2893fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
2903fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
2913fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
2928af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2938af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_resume_open
2948af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2958af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      Resume opening process.
2968af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2978af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
2988af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
2998af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3008af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
301cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonvoid bta_hf_client_resume_open(void) {
302cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  APPL_TRACE_DEBUG("%s", __func__);
303cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
304cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* resume opening process.  */
305cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (bta_hf_client_cb.scb.state == BTA_HF_CLIENT_INIT_ST) {
306cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    bta_hf_client_cb.scb.state = BTA_HF_CLIENT_OPENING_ST;
307bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    tBTA_HF_CLIENT_DATA msg;
308bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    msg.hdr.layer_specific = bta_hf_client_cb.scb.handle;
309bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    bta_hf_client_start_open(&msg);
310cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
3113fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
3123fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
3133fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
3148af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3158af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_collision_timer_cback
3168af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3178af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      HF Client connection collision timer callback
3188af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3198af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3208af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
3218af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3228af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
323cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonstatic void bta_hf_client_collision_timer_cback(UNUSED_ATTR void* data) {
324cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  APPL_TRACE_DEBUG("%s", __func__);
3253fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
326cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* If the peer haven't opened connection, restart opening process */
327cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  bta_hf_client_resume_open();
3283fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
3293fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
3303fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
3318af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3328af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_collision_cback
3338af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3348af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      Get notified about collision.
3358af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3368af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3378af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
3388af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3398af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
340cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonvoid bta_hf_client_collision_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status,
341cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                                   uint8_t id, UNUSED_ATTR uint8_t app_id,
342cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                                   UNUSED_ATTR BD_ADDR peer_addr) {
343cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (bta_hf_client_cb.scb.state == BTA_HF_CLIENT_OPENING_ST) {
344cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    if (id == BTA_ID_SYS) /* ACL collision */
345cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    {
346cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      APPL_TRACE_WARNING("HF Client found collision (ACL) ...");
347cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    } else if (id == BTA_ID_HS) /* RFCOMM collision */
3483fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta    {
349cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      APPL_TRACE_WARNING("HF Client found collision (RFCOMM) ...");
350cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    } else {
351cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      APPL_TRACE_WARNING("HF Client found collision (\?\?\?) ...");
352cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    }
353cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
354cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    bta_hf_client_cb.scb.state = BTA_HF_CLIENT_INIT_ST;
355cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
356cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* Cancel SDP if it had been started. */
357cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    if (bta_hf_client_cb.scb.p_disc_db) {
358cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      (void)SDP_CancelServiceSearch(bta_hf_client_cb.scb.p_disc_db);
359cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      bta_hf_client_free_db(NULL);
3603fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta    }
361cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
362cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* reopen registered server */
363cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* Collision may be detected before or after we close servers. */
364bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    bta_hf_client_start_server(&bta_hf_client_cb);
365cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
366cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* Start timer to handle connection opening restart */
367cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    alarm_set_on_queue(
368cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson        bta_hf_client_cb.scb.collision_timer, BTA_HF_CLIENT_COLLISION_TIMER_MS,
369cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson        bta_hf_client_collision_timer_cback, NULL, btu_bta_alarm_queue);
370cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
3713fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
3723fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
3733fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
3748af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3758af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_api_enable
3768af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3778af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      Handle an API enable event.
3788af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3798af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3808af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
3818af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
3828af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
383bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket AgarwaltBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback,
384bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                                     tBTA_SEC sec_mask,
385bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                                     tBTA_HF_CLIENT_FEAT features,
386bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                                     const char* p_service_name) {
387bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* If already registered then return error */
388bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  if (bta_sys_is_register(BTA_ID_HS)) {
389bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    APPL_TRACE_ERROR("%s: BTA HF Client is already enabled, ignoring ...",
390bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                     __func__);
391bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    return BTA_FAILURE;
392bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  }
393bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
394bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* register with BTA system manager */
395bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_sys_register(BTA_ID_HS, &bta_hf_client_reg);
3961025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
397bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* zero the control block */
398cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  memset(&bta_hf_client_cb, 0, sizeof(tBTA_HF_CLIENT_CB));
3993fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
400bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* reset the timers and set fields to invalid */
401bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_scb_init();
402bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
403bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* Set control block to be ready to use */
404bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.p_cback = p_cback;
405bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.scb.handle = BTA_HF_CLIENT_CB_FIRST_HANDLE;
406bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.scb.is_allocated = false;
407bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.scb.serv_sec_mask = sec_mask;
408bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.scb.features = features;
409bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.scb.negotiated_codec = BTM_SCO_CODEC_CVSD;
4101025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
411cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* check if mSBC support enabled */
412bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  char value[PROPERTY_VALUE_MAX];
413cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  osi_property_get("ro.bluetooth.hfp.ver", value, "0");
414cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (strcmp(value, "1.6") == 0) {
415cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    bta_hf_client_cb.msbc_enabled = true;
416cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
4173fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
418cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* set same setting as AG does */
419cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  BTM_WriteVoiceSettings(AG_VOICE_SETTINGS);
4201025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
421cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  bta_sys_collision_register(BTA_ID_HS, bta_hf_client_collision_cback);
4223fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
423bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* initialize AT control block */
424bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_at_init(&bta_hf_client_cb);
425bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
426bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* create SDP records */
427bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_create_record(&bta_hf_client_cb, p_service_name);
428bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
429bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* Set the Audio service class bit */
430bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  tBTA_UTL_COD cod;
431bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  cod.service = BTM_COD_SERVICE_AUDIO;
432bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
433bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
434bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* start RFCOMM server */
435bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_start_server(&bta_hf_client_cb);
436bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
437bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  return BTA_SUCCESS;
438bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal}
439bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
440bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal/*******************************************************************************
441bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
442bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Function         bta_hf_client_find_cb_by_handle
443bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
444bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Description      Finds the control block by handle provided
445bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
446bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  handle: Handle as obtained from BTA_HfClientOpen call
447bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
448bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
449bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Returns          Control block corresponding to the handle and NULL if
450bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  none exists
451bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
452bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal ******************************************************************************/
453bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket AgarwaltBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle) {
454bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  // Currently we have only one control block
455bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  if (bta_hf_client_cb.scb.is_allocated &&
456bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_cb.scb.handle == handle) {
457bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    return &(bta_hf_client_cb);
458bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  }
459bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  APPL_TRACE_ERROR("%s: block not found for handle %d alloc: %d saved %d",
460bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   __func__, handle, bta_hf_client_cb.scb.is_allocated,
461bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   bta_hf_client_cb.scb.handle);
462bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  return NULL;
463bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal}
464bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
465bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal/*******************************************************************************
466bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
467bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Function         bta_hf_client_find_cb_by_rfc_handle
468bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
469bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Description      Finds the control block by RFC handle provided.
470bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  RFC handle is either in conn_handle (i.e. RFC handle
471bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  provided by the lower layer, stack/rfc) or in serv_handle
472bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  if the port is an incoming server. In case of incoming
473bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  request a block is allocated.
474bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
475bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  handle: RFC handle for either the outgoing connection
476bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  or the server connection
477bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
478bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
479bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Returns          Control block corresponding to the handle and NULL if none
480bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  exists
481bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
482bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal ******************************************************************************/
483bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket AgarwaltBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_rfc_handle(uint16_t handle) {
484bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  // Currently we have only one control block
485bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bool is_allocated = bta_hf_client_cb.scb.is_allocated;
486bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  uint16_t conn_handle = bta_hf_client_cb.scb.conn_handle;
487bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  uint16_t serv_handle = bta_hf_client_cb.scb.serv_handle;
488bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
489bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  APPL_TRACE_DEBUG("%s: cb handle %d alloc %d conn_handle %d serv_handle %d",
490bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   __func__, handle, is_allocated, conn_handle, serv_handle);
491bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
492bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  if (is_allocated && (conn_handle == handle || serv_handle == handle)) {
493bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    return &(bta_hf_client_cb);
494bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  } else if (!is_allocated && serv_handle == handle) {
495bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    uint16_t tmp_handle;
496bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    if (bta_hf_client_allocate_handle(&tmp_handle)) {
497bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      tBTA_HF_CLIENT_CB* client_cb =
498bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal          bta_hf_client_find_cb_by_handle(tmp_handle);
499bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      // Allocation for incoming channel happens only on
500bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      // connection request. Also the code uses conn_handle
501bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      // for PORT_{Write/Read}Data. So setting this equal to
502bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      // serv_handle fixes that issue.
503bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      client_cb->scb.conn_handle = client_cb->scb.serv_handle;
504bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      return client_cb;
505bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    }
506bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  } else {
507bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    APPL_TRACE_ERROR("%s: no cb %d alloc %d conn_handle %d serv_handle %d",
508bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                     __func__, handle, is_allocated, conn_handle, serv_handle);
509bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  }
510bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  return NULL;
511bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal}
512bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
513bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal/*******************************************************************************
514bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
515bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Function         bta_hf_client_find_cb_by_sco_handle
516bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
517bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Description      Finds the control block by sco handle provided
518bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
519bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  handle: sco handle
520bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
521bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
522bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Returns          Control block corresponding to the sco handle and
523bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  none if none exists
524bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
525bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal ******************************************************************************/
526bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket AgarwaltBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle) {
527bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  // Currently we have only one control block
528bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  if (bta_hf_client_cb.scb.is_allocated &&
529bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_cb.scb.sco_idx == handle) {
530bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    return &(bta_hf_client_cb);
531bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  }
532bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  APPL_TRACE_ERROR("%s: block not found for handle %d", __func__, handle);
533bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  return NULL;
534bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal}
535bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
536bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal/*******************************************************************************
537bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
538bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Function         bta_hf_client_allocate_handle
539bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
540bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Description      Allocates a handle for the new BD ADDR that needs a new RF
541bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  channel for HF connection. If the channel cannot be created
542bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  for a reason then false is returned
543bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
544bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  p_handle: OUT variable to store the outcome of allocate. If
545bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *                  allocate failed then value is not valid
546bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
547bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
548bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal * Returns          true if the creation of handle succeeded, false otherwise
549bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal *
550bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal ******************************************************************************/
551bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalbool bta_hf_client_allocate_handle(uint16_t* p_handle) {
552bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* Check that we do not have a request to for same device in the control
553bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal   * blocks */
554bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  if (bta_hf_client_cb.scb.is_allocated) {
555bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    APPL_TRACE_ERROR("%s: all control blocks already used", __func__);
556bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    return false;
557bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  }
558bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
559bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  *p_handle = bta_hf_client_cb.scb.handle;
560bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.scb.is_allocated = true;
561bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  return true;
5623fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
5633fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
5643fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
5658af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
5668af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_api_disable
5678af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
5688af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      Handle an API disable event.
5698af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
5708af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
5718af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
5728af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
5738af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
574bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalvoid bta_hf_client_api_disable() {
575cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (!bta_sys_is_register(BTA_ID_HS)) {
576bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    APPL_TRACE_WARNING("BTA HF Client is already disabled, ignoring ...");
577cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    return;
578cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
5793fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
580bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* Remove the collision handler */
581bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_sys_collision_register(BTA_ID_HS, NULL);
5823fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
583bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_cb.scb.deregister = true;
5843fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
585bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* remove sdp record */
586bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_del_record(&bta_hf_client_cb);
587bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
588bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* remove rfcomm server */
589bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_close_server(&bta_hf_client_cb);
590bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
591bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* reinit the control block */
592bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_scb_init();
593bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
594bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* De-register with BTA system manager */
595bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_sys_deregister(BTA_ID_HS);
5963fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
5973fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
5983fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
5998af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6008af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_hdl_event
6018af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6028af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      Data HF Client main event handling function.
6038af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6048af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6058af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          bool
6068af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6078af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
608cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonbool bta_hf_client_hdl_event(BT_HDR* p_msg) {
609bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  APPL_TRACE_DEBUG("%s: %s (0x%x)", __func__,
610cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                   bta_hf_client_evt_str(p_msg->event), p_msg->event);
611bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_sm_execute(p_msg->event, (tBTA_HF_CLIENT_DATA*)p_msg);
612cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  return true;
6133fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
6143fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
6153fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
6168af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6178af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_sm_execute
6188af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6198af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      State machine event handling function for HF Client
6208af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6218af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6228af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
6238af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
6248af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
625cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonvoid bta_hf_client_sm_execute(uint16_t event, tBTA_HF_CLIENT_DATA* p_data) {
626cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  tBTA_HF_CLIENT_ST_TBL state_table;
627cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  uint8_t action;
628cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  int i;
6293fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
630cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  uint16_t in_event = event;
631cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  uint8_t in_state = bta_hf_client_cb.scb.state;
632cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
633cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* Ignore displaying of AT results when not connected (Ignored in state
634cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson   * machine) */
635cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (bta_hf_client_cb.scb.state == BTA_HF_CLIENT_OPEN_ST) {
636cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    APPL_TRACE_EVENT("HF Client evt : State %d (%s), Event 0x%04x (%s)",
637cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                     bta_hf_client_cb.scb.state,
638cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                     bta_hf_client_state_str(bta_hf_client_cb.scb.state), event,
639cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson                     bta_hf_client_evt_str(event));
640cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
6413fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
642cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  event &= 0x00FF;
643cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (event >= (BTA_HF_CLIENT_MAX_EVT & 0x00FF)) {
644cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    APPL_TRACE_ERROR("HF Client evt out of range, ignoring...");
645cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    return;
646cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
6473fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
648cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* look up the state table for the current state */
649cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  state_table = bta_hf_client_st_tbl[bta_hf_client_cb.scb.state];
6503fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
651cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* set next state */
652cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  bta_hf_client_cb.scb.state = state_table[event][BTA_HF_CLIENT_NEXT_STATE];
6533fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
654bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  APPL_TRACE_DEBUG("%s: before alloc %d conn %d serv %d", __func__,
655bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   bta_hf_client_cb.scb.is_allocated,
656bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   bta_hf_client_cb.scb.conn_handle,
657bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   bta_hf_client_cb.scb.serv_handle);
658bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
659cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  /* execute action functions */
660cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  for (i = 0; i < BTA_HF_CLIENT_ACTIONS; i++) {
661f33b6f434f086b20fabe5913016bc423ac975057Marie Janssen    action = state_table[event][i];
662f33b6f434f086b20fabe5913016bc423ac975057Marie Janssen    if (action != BTA_HF_CLIENT_IGNORE) {
663cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      (*bta_hf_client_action[action])(p_data);
664cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    } else {
665cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
6663fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta    }
667cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
6681025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
669bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  /* if the next state is INIT then release the cb for future use */
670bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  if (bta_hf_client_cb.scb.state == BTA_HF_CLIENT_INIT_ST) {
671bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal    bta_hf_client_cb.scb.is_allocated = false;
672bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  }
673bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
674bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  APPL_TRACE_DEBUG("%s: after alloc %d conn %d serv %d", __func__,
675bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   bta_hf_client_cb.scb.is_allocated,
676bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   bta_hf_client_cb.scb.conn_handle,
677bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   bta_hf_client_cb.scb.serv_handle);
678bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal
679cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (bta_hf_client_cb.scb.state != in_state) {
680cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    APPL_TRACE_EVENT(
681cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson        "BTA HF Client State Change: [%s] -> [%s] after Event [%s]",
682cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson        bta_hf_client_state_str(in_state),
683cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson        bta_hf_client_state_str(bta_hf_client_cb.scb.state),
684cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson        bta_hf_client_evt_str(in_event));
685cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
6863fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
6873fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
688cd1fd07f1306e08fe048682dd5918987e579f937Myles Watsonstatic void send_post_slc_cmd(void) {
689cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  bta_hf_client_cb.scb.at_cb.current_cmd = BTA_HF_CLIENT_AT_NONE;
6901025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
691bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_send_at_bia(&bta_hf_client_cb);
692bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_send_at_ccwa(&bta_hf_client_cb, true);
693bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_send_at_cmee(&bta_hf_client_cb, true);
694bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_send_at_cops(&bta_hf_client_cb, false);
695bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_send_at_btrh(&bta_hf_client_cb, true, 0);
696bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  bta_hf_client_send_at_clip(&bta_hf_client_cb, true);
6971025687c6679608fe9df918f37011414ffc72b62Hemant Gupta}
6981025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
6993fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta/*******************************************************************************
7008af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
7018af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Function         bta_hf_client_slc_seq
7028af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
7038af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Description      Handles AT commands sequence required for SLC creation
7048af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
7058af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
7068af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson * Returns          void
7078af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson *
7088af480e24549ba51a3f6858d9d9af504715e0beaMyles Watson ******************************************************************************/
709bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalvoid bta_hf_client_slc_seq(tBTA_HF_CLIENT_CB* client_cb, bool error) {
710cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  APPL_TRACE_DEBUG("bta_hf_client_slc_seq cmd: %u",
711bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal                   client_cb->scb.at_cb.current_cmd);
7123fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
713cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  if (error) {
714cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    /* SLC establishment error, sent close rfcomm event */
715cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    APPL_TRACE_ERROR(
716cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson        "HFPClient: Failed to create SLC due to AT error, disconnecting (%u)",
717bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal        client_cb->scb.at_cb.current_cmd);
7181025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
719cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    bta_hf_client_sm_execute(BTA_HF_CLIENT_API_CLOSE_EVT, NULL);
720cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    return;
721cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
7221025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
723bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  if (client_cb->scb.svc_conn) return;
7241025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
725bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal  switch (client_cb->scb.at_cb.current_cmd) {
7261025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    case BTA_HF_CLIENT_AT_NONE:
727bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_send_at_brsf(&bta_hf_client_cb);
728cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
7293fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
7301025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    case BTA_HF_CLIENT_AT_BRSF:
731bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      if ((client_cb->scb.features & BTA_HF_CLIENT_FEAT_CODEC) &&
732bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal          (client_cb->scb.peer_features & BTA_HF_CLIENT_PEER_CODEC)) {
733bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal        bta_hf_client_send_at_bac(&bta_hf_client_cb);
7341025687c6679608fe9df918f37011414ffc72b62Hemant Gupta        break;
735cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      }
736cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
737bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_send_at_cind(&bta_hf_client_cb, false);
738cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
7393fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
7401025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    case BTA_HF_CLIENT_AT_BAC:
741bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_send_at_cind(&bta_hf_client_cb, false);
742cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
7433fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
7441025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    case BTA_HF_CLIENT_AT_CIND:
745bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_send_at_cind(&bta_hf_client_cb, true);
746cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
7471025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
7481025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    case BTA_HF_CLIENT_AT_CIND_STATUS:
749bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_send_at_cmer(&bta_hf_client_cb, true);
750cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
7511025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
7521025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    case BTA_HF_CLIENT_AT_CMER:
753bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      if (client_cb->scb.peer_features & BTA_HF_CLIENT_PEER_FEAT_3WAY &&
754bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal          client_cb->scb.features & BTA_HF_CLIENT_FEAT_3WAY) {
755bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal        bta_hf_client_send_at_chld(&bta_hf_client_cb, '?', 0);
756cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      } else {
757bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal        tBTA_HF_CLIENT_DATA msg;
758bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal        msg.hdr.layer_specific = bta_hf_client_cb.scb.handle;
759bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal        bta_hf_client_svc_conn_open(&msg);
7601025687c6679608fe9df918f37011414ffc72b62Hemant Gupta        send_post_slc_cmd();
761cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      }
762cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
7631025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
764cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    case BTA_HF_CLIENT_AT_CHLD:
765bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      tBTA_HF_CLIENT_DATA msg;
766bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      msg.hdr.layer_specific = bta_hf_client_cb.scb.handle;
767bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal      bta_hf_client_svc_conn_open(&msg);
768cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      send_post_slc_cmd();
769cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
7701025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
771cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    default:
772cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      /* If happen there is a bug in SLC creation procedure... */
773cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      APPL_TRACE_ERROR(
774cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson          "HFPClient: Failed to create SLCdue to unexpected AT command, "
775cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson          "disconnecting (%u)",
776bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwal          client_cb->scb.at_cb.current_cmd);
777cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson
778cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      bta_hf_client_sm_execute(BTA_HF_CLIENT_API_CLOSE_EVT, NULL);
779cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      break;
780cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
7813fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
7823fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
7831025687c6679608fe9df918f37011414ffc72b62Hemant Gupta#ifndef CASE_RETURN_STR
784cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson#define CASE_RETURN_STR(const) \
785cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  case const:                  \
786cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson    return #const;
7871025687c6679608fe9df918f37011414ffc72b62Hemant Gupta#endif
7881025687c6679608fe9df918f37011414ffc72b62Hemant Gupta
789bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalstatic const char* bta_hf_client_evt_str(uint16_t event) {
790cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  switch (event) {
7911025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_API_OPEN_EVT)
7921025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_API_CLOSE_EVT)
7931025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_API_AUDIO_OPEN_EVT)
7941025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT)
7951025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_RFC_OPEN_EVT)
7961025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_RFC_CLOSE_EVT)
7971025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_RFC_SRV_CLOSE_EVT)
7981025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_RFC_DATA_EVT)
7991025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_DISC_ACP_RES_EVT)
8001025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_DISC_INT_RES_EVT)
8011025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_DISC_OK_EVT)
8021025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_DISC_FAIL_EVT)
8031025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_API_ENABLE_EVT)
8041025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_API_DISABLE_EVT)
8051025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_SCO_OPEN_EVT)
8061025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_SCO_CLOSE_EVT)
8071025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_SEND_AT_CMD_EVT)
8083fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta    default:
809cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      return "Unknown HF Client Event";
810cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
8113fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
8123fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta
813bdcfc045f9d21afc1510cdad4fddd97a93c26477Sanket Agarwalstatic const char* bta_hf_client_state_str(uint8_t state) {
814cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  switch (state) {
8151025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_INIT_ST)
8161025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_OPENING_ST)
8171025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_OPEN_ST)
8181025687c6679608fe9df918f37011414ffc72b62Hemant Gupta    CASE_RETURN_STR(BTA_HF_CLIENT_CLOSING_ST)
8193fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta    default:
820cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson      return "Unknown HF Client State";
821cd1fd07f1306e08fe048682dd5918987e579f937Myles Watson  }
8223fe1b49071420903cb8e3082ec04e76296352690Hemant Gupta}
823