1 /******************************************************************************
2 *
3 * Copyright 2003-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This is the main implementation file for the BTA audio gateway.
22 *
23 ******************************************************************************/
24
25 #include <stack/include/bt_types.h>
26 #include <string.h>
27
28 #include "bta_ag_int.h"
29 #include "bta_api.h"
30 #include "bta_sys.h"
31 #include "osi/include/osi.h"
32 #include "utl.h"
33
34 /*****************************************************************************
35 * Constants and types
36 ****************************************************************************/
37 /* state machine states */
38 enum { BTA_AG_INIT_ST, BTA_AG_OPENING_ST, BTA_AG_OPEN_ST, BTA_AG_CLOSING_ST };
39
40 /* state machine action enumeration list */
41 enum {
42 BTA_AG_REGISTER,
43 BTA_AG_DEREGISTER,
44 BTA_AG_START_OPEN,
45 BTA_AG_RFC_DO_OPEN,
46 BTA_AG_RFC_DO_CLOSE,
47 BTA_AG_START_DEREG,
48 BTA_AG_START_CLOSE,
49 BTA_AG_RFC_OPEN,
50 BTA_AG_OPEN_FAIL,
51 BTA_AG_RFC_ACP_OPEN,
52 BTA_AG_RFC_CLOSE,
53 BTA_AG_RFC_FAIL,
54 BTA_AG_RFC_DATA,
55 BTA_AG_DISC_INT_RES,
56 BTA_AG_DISC_FAIL,
57 BTA_AG_DISC_ACP_RES,
58 BTA_AG_FREE_DB,
59 BTA_AG_SCO_CONN_OPEN,
60 BTA_AG_SCO_CONN_CLOSE,
61 BTA_AG_SCO_LISTEN,
62 BTA_AG_SCO_OPEN,
63 BTA_AG_SCO_CLOSE,
64 BTA_AG_SCO_SHUTDOWN,
65 BTA_AG_POST_SCO_OPEN,
66 BTA_AG_POST_SCO_CLOSE,
67 BTA_AG_SVC_CONN_OPEN,
68 BTA_AG_RESULT,
69 BTA_AG_SETCODEC,
70 BTA_AG_SEND_RING,
71 BTA_AG_HANDLE_COLLISION,
72 BTA_AG_NUM_ACTIONS
73 };
74
75 #define BTA_AG_IGNORE BTA_AG_NUM_ACTIONS
76
77 /* type for action functions */
78 typedef void (*tBTA_AG_ACTION)(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
79
80 #define CASE_RETURN_STR(const) \
81 case const: \
82 return #const;
83
bta_ag_res_str(tBTA_AG_RES result)84 static const char* bta_ag_res_str(tBTA_AG_RES result) {
85 switch (result) {
86 CASE_RETURN_STR(BTA_AG_SPK_RES)
87 CASE_RETURN_STR(BTA_AG_MIC_RES)
88 CASE_RETURN_STR(BTA_AG_INBAND_RING_RES)
89 CASE_RETURN_STR(BTA_AG_CIND_RES)
90 CASE_RETURN_STR(BTA_AG_BINP_RES)
91 CASE_RETURN_STR(BTA_AG_IND_RES)
92 CASE_RETURN_STR(BTA_AG_BVRA_RES)
93 CASE_RETURN_STR(BTA_AG_CNUM_RES)
94 CASE_RETURN_STR(BTA_AG_BTRH_RES)
95 CASE_RETURN_STR(BTA_AG_CLCC_RES)
96 CASE_RETURN_STR(BTA_AG_COPS_RES)
97 CASE_RETURN_STR(BTA_AG_IN_CALL_RES)
98 CASE_RETURN_STR(BTA_AG_IN_CALL_CONN_RES)
99 CASE_RETURN_STR(BTA_AG_CALL_WAIT_RES)
100 CASE_RETURN_STR(BTA_AG_OUT_CALL_ORIG_RES)
101 CASE_RETURN_STR(BTA_AG_OUT_CALL_ALERT_RES)
102 CASE_RETURN_STR(BTA_AG_OUT_CALL_CONN_RES)
103 CASE_RETURN_STR(BTA_AG_CALL_CANCEL_RES)
104 CASE_RETURN_STR(BTA_AG_END_CALL_RES)
105 CASE_RETURN_STR(BTA_AG_IN_CALL_HELD_RES)
106 CASE_RETURN_STR(BTA_AG_UNAT_RES)
107 CASE_RETURN_STR(BTA_AG_MULTI_CALL_RES)
108 CASE_RETURN_STR(BTA_AG_BIND_RES)
109 CASE_RETURN_STR(BTA_AG_IND_RES_ON_DEMAND)
110 default:
111 return "Unknown AG Result";
112 }
113 }
114
bta_ag_evt_str(uint16_t event)115 static const char* bta_ag_evt_str(uint16_t event) {
116 switch (event) {
117 CASE_RETURN_STR(BTA_AG_API_REGISTER_EVT)
118 CASE_RETURN_STR(BTA_AG_API_DEREGISTER_EVT)
119 CASE_RETURN_STR(BTA_AG_API_OPEN_EVT)
120 CASE_RETURN_STR(BTA_AG_API_CLOSE_EVT)
121 CASE_RETURN_STR(BTA_AG_API_AUDIO_OPEN_EVT)
122 CASE_RETURN_STR(BTA_AG_API_AUDIO_CLOSE_EVT)
123 CASE_RETURN_STR(BTA_AG_API_RESULT_EVT)
124 CASE_RETURN_STR(BTA_AG_API_SETCODEC_EVT)
125 CASE_RETURN_STR(BTA_AG_RFC_OPEN_EVT)
126 CASE_RETURN_STR(BTA_AG_RFC_CLOSE_EVT)
127 CASE_RETURN_STR(BTA_AG_RFC_SRV_CLOSE_EVT)
128 CASE_RETURN_STR(BTA_AG_RFC_DATA_EVT)
129 CASE_RETURN_STR(BTA_AG_SCO_OPEN_EVT)
130 CASE_RETURN_STR(BTA_AG_SCO_CLOSE_EVT)
131 CASE_RETURN_STR(BTA_AG_DISC_ACP_RES_EVT)
132 CASE_RETURN_STR(BTA_AG_DISC_INT_RES_EVT)
133 CASE_RETURN_STR(BTA_AG_DISC_OK_EVT)
134 CASE_RETURN_STR(BTA_AG_DISC_FAIL_EVT)
135 CASE_RETURN_STR(BTA_AG_RING_TIMEOUT_EVT)
136 CASE_RETURN_STR(BTA_AG_SVC_TIMEOUT_EVT)
137 CASE_RETURN_STR(BTA_AG_COLLISION_EVT)
138 default:
139 return "Unknown AG Event";
140 }
141 }
142
bta_ag_state_str(uint8_t state)143 static const char* bta_ag_state_str(uint8_t state) {
144 switch (state) {
145 CASE_RETURN_STR(BTA_AG_INIT_ST)
146 CASE_RETURN_STR(BTA_AG_OPENING_ST)
147 CASE_RETURN_STR(BTA_AG_OPEN_ST)
148 CASE_RETURN_STR(BTA_AG_CLOSING_ST)
149 default:
150 return "Unknown AG State";
151 }
152 }
153
154 /* action functions */
155 const tBTA_AG_ACTION bta_ag_action[] = {
156 bta_ag_register, bta_ag_deregister, bta_ag_start_open,
157 bta_ag_rfc_do_open, bta_ag_rfc_do_close, bta_ag_start_dereg,
158 bta_ag_start_close, bta_ag_rfc_open, bta_ag_open_fail,
159 bta_ag_rfc_acp_open, bta_ag_rfc_close, bta_ag_rfc_fail,
160 bta_ag_rfc_data, bta_ag_disc_int_res, bta_ag_disc_fail,
161 bta_ag_disc_acp_res, bta_ag_free_db, bta_ag_sco_conn_open,
162 bta_ag_sco_conn_close, bta_ag_sco_listen, bta_ag_sco_open,
163 bta_ag_sco_close, bta_ag_sco_shutdown, bta_ag_post_sco_open,
164 bta_ag_post_sco_close, bta_ag_svc_conn_open, bta_ag_result,
165 bta_ag_setcodec, bta_ag_send_ring, bta_ag_handle_collision};
166
167 static_assert(sizeof(bta_ag_action) / sizeof(tBTA_AG_ACTION) ==
168 BTA_AG_NUM_ACTIONS,
169 "bta_ag_action must handle all actions");
170
171 /* state table information */
172 #define BTA_AG_ACTIONS 2 /* number of actions */
173 #define BTA_AG_NEXT_STATE 2 /* position of next state */
174 #define BTA_AG_NUM_COLS 3 /* number of columns in state tables */
175
176 /* state table for init state */
177 const uint8_t bta_ag_st_init[][BTA_AG_NUM_COLS] = {
178 /* Event Action 1 Action 2 Next state */
179 /* API_REGISTER_EVT */ {BTA_AG_REGISTER, BTA_AG_IGNORE, BTA_AG_INIT_ST},
180 /* API_DEREGISTER_EVT */ {BTA_AG_DEREGISTER, BTA_AG_IGNORE, BTA_AG_INIT_ST},
181 /* API_OPEN_EVT */ {BTA_AG_START_OPEN, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
182 /* API_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
183 /* API_AUDIO_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
184 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
185 /* API_RESULT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
186 /* API_SETCODEC_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
187 /* RFC_OPEN_EVT */ {BTA_AG_RFC_ACP_OPEN, BTA_AG_SCO_LISTEN, BTA_AG_OPEN_ST},
188 /* RFC_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
189 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
190 /* RFC_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
191 /* SCO_OPEN_EVT */ {BTA_AG_SCO_CONN_OPEN, BTA_AG_IGNORE, BTA_AG_INIT_ST},
192 /* SCO_CLOSE_EVT */ {BTA_AG_SCO_CONN_CLOSE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
193 /* DISC_ACP_RES_EVT */ {BTA_AG_FREE_DB, BTA_AG_IGNORE, BTA_AG_INIT_ST},
194 /* DISC_INT_RES_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
195 /* DISC_OK_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
196 /* DISC_FAIL_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
197 /* RING_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
198 /* SVC_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
199 /* COLLISION_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}};
200
201 /* state table for opening state */
202 const uint8_t bta_ag_st_opening[][BTA_AG_NUM_COLS] = {
203 /* Event Action 1 Action 2 Next state */
204 /* API_REGISTER_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
205 /* API_DEREGISTER_EVT */
206 {BTA_AG_RFC_DO_CLOSE, BTA_AG_START_DEREG, BTA_AG_CLOSING_ST},
207 /* API_OPEN_EVT */ {BTA_AG_OPEN_FAIL, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
208 /* API_CLOSE_EVT */ {BTA_AG_RFC_DO_CLOSE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
209 /* API_AUDIO_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
210 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
211 /* API_RESULT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
212 /* API_SETCODEC_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
213 /* RFC_OPEN_EVT */ {BTA_AG_RFC_OPEN, BTA_AG_SCO_LISTEN, BTA_AG_OPEN_ST},
214 /* RFC_CLOSE_EVT */ {BTA_AG_RFC_FAIL, BTA_AG_IGNORE, BTA_AG_INIT_ST},
215 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
216 /* RFC_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
217 /* SCO_OPEN_EVT */ {BTA_AG_SCO_CONN_OPEN, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
218 /* SCO_CLOSE_EVT */
219 {BTA_AG_SCO_CONN_CLOSE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
220 /* DISC_ACP_RES_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
221 /* DISC_INT_RES_EVT */
222 {BTA_AG_DISC_INT_RES, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
223 /* DISC_OK_EVT */ {BTA_AG_RFC_DO_OPEN, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
224 /* DISC_FAIL_EVT */ {BTA_AG_DISC_FAIL, BTA_AG_IGNORE, BTA_AG_INIT_ST},
225 /* RING_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
226 /* SVC_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST},
227 /* COLLISION_EVT */
228 {BTA_AG_HANDLE_COLLISION, BTA_AG_IGNORE, BTA_AG_INIT_ST}};
229
230 /* state table for open state */
231 const uint8_t bta_ag_st_open[][BTA_AG_NUM_COLS] = {
232 /* Event Action 1 Action 2 Next state */
233 /* API_REGISTER_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
234 /* API_DEREGISTER_EVT */
235 {BTA_AG_START_CLOSE, BTA_AG_START_DEREG, BTA_AG_CLOSING_ST},
236 /* API_OPEN_EVT */ {BTA_AG_OPEN_FAIL, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
237 /* API_CLOSE_EVT */ {BTA_AG_START_CLOSE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
238 /* API_AUDIO_OPEN_EVT */ {BTA_AG_SCO_OPEN, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
239 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_SCO_CLOSE, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
240 /* API_RESULT_EVT */ {BTA_AG_RESULT, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
241 /* API_SETCODEC_EVT */ {BTA_AG_SETCODEC, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
242 /* RFC_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
243 /* RFC_CLOSE_EVT */ {BTA_AG_RFC_CLOSE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
244 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
245 /* RFC_DATA_EVT */ {BTA_AG_RFC_DATA, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
246 /* SCO_OPEN_EVT */
247 {BTA_AG_SCO_CONN_OPEN, BTA_AG_POST_SCO_OPEN, BTA_AG_OPEN_ST},
248 /* SCO_CLOSE_EVT */
249 {BTA_AG_SCO_CONN_CLOSE, BTA_AG_POST_SCO_CLOSE, BTA_AG_OPEN_ST},
250 /* DISC_ACP_RES_EVT */ {BTA_AG_DISC_ACP_RES, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
251 /* DISC_INT_RES_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
252 /* DISC_OK_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
253 /* DISC_FAIL_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
254 /* RING_TOUT_EVT */ {BTA_AG_SEND_RING, BTA_AG_IGNORE, BTA_AG_OPEN_ST},
255 /* SVC_TOUT_EVT */ {BTA_AG_START_CLOSE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
256 /* COLLISION_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}};
257
258 /* state table for closing state */
259 const uint8_t bta_ag_st_closing[][BTA_AG_NUM_COLS] = {
260 /* Event Action 1 Action 2 Next state */
261 /* API_REGISTER_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
262 /* API_DEREGISTER_EVT */
263 {BTA_AG_START_DEREG, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
264 /* API_OPEN_EVT */ {BTA_AG_OPEN_FAIL, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
265 /* API_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
266 /* API_AUDIO_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
267 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
268 /* API_RESULT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
269 /* API_SETCODEC_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
270 /* RFC_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
271 /* RFC_CLOSE_EVT */ {BTA_AG_RFC_CLOSE, BTA_AG_IGNORE, BTA_AG_INIT_ST},
272 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
273 /* RFC_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
274 /* SCO_OPEN_EVT */ {BTA_AG_SCO_CONN_OPEN, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
275 /* SCO_CLOSE_EVT */
276 {BTA_AG_SCO_CONN_CLOSE, BTA_AG_POST_SCO_CLOSE, BTA_AG_CLOSING_ST},
277 /* DISC_ACP_RES_EVT */ {BTA_AG_FREE_DB, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
278 /* DISC_INT_RES_EVT */ {BTA_AG_FREE_DB, BTA_AG_IGNORE, BTA_AG_INIT_ST},
279 /* DISC_OK_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
280 /* DISC_FAIL_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
281 /* RING_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
282 /* SVC_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST},
283 /* COLLISION_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}};
284
285 constexpr size_t BTA_AG_NUM_EVENTS =
286 BTA_AG_MAX_EVT - BTA_SYS_EVT_START(BTA_ID_AG);
287 static_assert(sizeof(bta_ag_st_init) / BTA_AG_NUM_COLS == BTA_AG_NUM_EVENTS,
288 "bta_ag_st_init must handle all AG events");
289 static_assert(sizeof(bta_ag_st_opening) / BTA_AG_NUM_COLS == BTA_AG_NUM_EVENTS,
290 "bta_ag_st_opening must handle all AG events");
291 static_assert(sizeof(bta_ag_st_open) / BTA_AG_NUM_COLS == BTA_AG_NUM_EVENTS,
292 "bta_ag_st_open must handle all AG events");
293 static_assert(sizeof(bta_ag_st_closing) / BTA_AG_NUM_COLS == BTA_AG_NUM_EVENTS,
294 "bta_ag_st_closing must handle all AG events");
295
296 /* type for state table */
297 typedef const uint8_t (*tBTA_AG_ST_TBL)[BTA_AG_NUM_COLS];
298
299 /* state table */
300 const tBTA_AG_ST_TBL bta_ag_st_tbl[] = {bta_ag_st_init, bta_ag_st_opening,
301 bta_ag_st_open, bta_ag_st_closing};
302
303 /*****************************************************************************
304 * Global data
305 ****************************************************************************/
306
307 /* AG control block */
308 tBTA_AG_CB bta_ag_cb;
309 const tBTA_AG_DATA tBTA_AG_DATA::kEmpty = {};
310
311 /*******************************************************************************
312 *
313 * Function bta_ag_scb_alloc
314 *
315 * Description Allocate an AG service control block.
316 *
317 *
318 * Returns pointer to the scb, or NULL if none could be allocated.
319 *
320 ******************************************************************************/
bta_ag_scb_alloc(void)321 static tBTA_AG_SCB* bta_ag_scb_alloc(void) {
322 tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
323 int i;
324
325 for (i = 0; i < BTA_AG_MAX_NUM_CLIENTS; i++, p_scb++) {
326 if (!p_scb->in_use) {
327 /* initialize variables */
328 p_scb->in_use = true;
329 p_scb->sco_idx = BTM_INVALID_SCO_INDEX;
330 p_scb->received_at_bac = false;
331 p_scb->codec_updated = false;
332 p_scb->codec_fallback = false;
333 p_scb->peer_codecs = BTA_AG_CODEC_CVSD;
334 p_scb->sco_codec = BTA_AG_CODEC_CVSD;
335 p_scb->peer_version = HFP_HSP_VERSION_UNKNOWN;
336 p_scb->hsp_version = HSP_VERSION_1_2;
337 p_scb->peer_sdp_features = 0;
338 /* set up timers */
339 p_scb->ring_timer = alarm_new("bta_ag.scb_ring_timer");
340 p_scb->collision_timer = alarm_new("bta_ag.scb_collision_timer");
341 p_scb->codec_negotiation_timer =
342 alarm_new("bta_ag.scb_codec_negotiation_timer");
343 /* set eSCO mSBC setting to T2 as the preferred */
344 p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
345 APPL_TRACE_DEBUG("bta_ag_scb_alloc %d", bta_ag_scb_to_idx(p_scb));
346 break;
347 }
348 }
349
350 if (i == BTA_AG_MAX_NUM_CLIENTS) {
351 /* out of scbs */
352 p_scb = nullptr;
353 APPL_TRACE_WARNING("%s: Out of scbs", __func__);
354 }
355 return p_scb;
356 }
357
358 /*******************************************************************************
359 *
360 * Function bta_ag_scb_dealloc
361 *
362 * Description Deallocate a service control block.
363 *
364 *
365 * Returns void
366 *
367 ******************************************************************************/
bta_ag_scb_dealloc(tBTA_AG_SCB * p_scb)368 void bta_ag_scb_dealloc(tBTA_AG_SCB* p_scb) {
369 uint8_t idx;
370 bool allocated = false;
371
372 APPL_TRACE_DEBUG("bta_ag_scb_dealloc %d", bta_ag_scb_to_idx(p_scb));
373
374 /* stop and free timers */
375 alarm_free(p_scb->ring_timer);
376 alarm_free(p_scb->codec_negotiation_timer);
377 alarm_free(p_scb->collision_timer);
378
379 /* initialize control block */
380 *p_scb = {};
381 p_scb->sco_idx = BTM_INVALID_SCO_INDEX;
382
383 /* If all scbs are deallocated, callback with disable event */
384 if (!bta_sys_is_register(BTA_ID_AG)) {
385 for (idx = 0; idx < BTA_AG_MAX_NUM_CLIENTS; idx++) {
386 if (bta_ag_cb.scb[idx].in_use) {
387 allocated = true;
388 break;
389 }
390 }
391
392 if (!allocated) {
393 (*bta_ag_cb.p_cback)(BTA_AG_DISABLE_EVT, nullptr);
394 }
395 }
396 }
397
398 /*******************************************************************************
399 *
400 * Function bta_ag_scb_to_idx
401 *
402 * Description Given a pointer to an scb, return its index.
403 *
404 *
405 * Returns Index of scb starting from 1
406 *
407 ******************************************************************************/
bta_ag_scb_to_idx(tBTA_AG_SCB * p_scb)408 uint16_t bta_ag_scb_to_idx(tBTA_AG_SCB* p_scb) {
409 /* use array arithmetic to determine index */
410 return static_cast<uint16_t>(p_scb - bta_ag_cb.scb + 1);
411 }
412
413 /*******************************************************************************
414 *
415 * Function bta_ag_scb_by_idx
416 *
417 * Description Given an scb index return pointer to scb.
418 *
419 *
420 * Returns Pointer to scb or NULL if not allocated.
421 *
422 ******************************************************************************/
bta_ag_scb_by_idx(uint16_t idx)423 tBTA_AG_SCB* bta_ag_scb_by_idx(uint16_t idx) {
424 tBTA_AG_SCB* p_scb;
425
426 /* verify index */
427 if (idx > 0 && idx <= BTA_AG_MAX_NUM_CLIENTS) {
428 p_scb = &bta_ag_cb.scb[idx - 1];
429 if (!p_scb->in_use) {
430 p_scb = nullptr;
431 APPL_TRACE_WARNING("ag scb idx %d not allocated", idx);
432 }
433 } else {
434 p_scb = nullptr;
435 APPL_TRACE_DEBUG("ag scb idx %d out of range", idx);
436 }
437 return p_scb;
438 }
439
440 /*******************************************************************************
441 *
442 * Function bta_ag_service_to_idx
443 *
444 * Description Given a BTA service mask convert to profile index.
445 *
446 *
447 * Returns Profile ndex of scb.
448 *
449 ******************************************************************************/
bta_ag_service_to_idx(tBTA_SERVICE_MASK services)450 uint8_t bta_ag_service_to_idx(tBTA_SERVICE_MASK services) {
451 if (services & BTA_HFP_SERVICE_MASK) {
452 return BTA_AG_HFP;
453 } else {
454 return BTA_AG_HSP;
455 }
456 }
457
458 /*******************************************************************************
459 *
460 * Function bta_ag_idx_by_bdaddr
461 *
462 * Description Find SCB associated with peer BD address.
463 *
464 *
465 * Returns Index of SCB or zero if none found.
466 *
467 ******************************************************************************/
bta_ag_idx_by_bdaddr(const RawAddress * peer_addr)468 uint16_t bta_ag_idx_by_bdaddr(const RawAddress* peer_addr) {
469 tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
470 if (peer_addr != nullptr) {
471 for (uint16_t i = 0; i < BTA_AG_MAX_NUM_CLIENTS; i++, p_scb++) {
472 if (p_scb->in_use && *peer_addr == p_scb->peer_addr) {
473 return (i + 1);
474 }
475 }
476 }
477
478 /* no scb found */
479 APPL_TRACE_WARNING("No ag scb for peer addr");
480 return 0;
481 }
482
483 /*******************************************************************************
484 *
485 * Function bta_ag_other_scb_open
486 *
487 * Description Check whether any other scb is in open state.
488 *
489 *
490 * Returns true if another scb is in open state, false otherwise.
491 *
492 ******************************************************************************/
bta_ag_other_scb_open(tBTA_AG_SCB * p_curr_scb)493 bool bta_ag_other_scb_open(tBTA_AG_SCB* p_curr_scb) {
494 tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
495 for (int i = 0; i < BTA_AG_MAX_NUM_CLIENTS; i++, p_scb++) {
496 if (p_scb->in_use && p_scb != p_curr_scb &&
497 p_scb->state == BTA_AG_OPEN_ST) {
498 return true;
499 }
500 }
501 /* no other scb found */
502 APPL_TRACE_DEBUG("No other ag scb open");
503 return false;
504 }
505
506 /*******************************************************************************
507 *
508 * Function bta_ag_scb_open
509 *
510 * Description Check whether given scb is in open state.
511 *
512 *
513 * Returns true if scb is in open state, false otherwise.
514 *
515 ******************************************************************************/
bta_ag_scb_open(tBTA_AG_SCB * p_curr_scb)516 bool bta_ag_scb_open(tBTA_AG_SCB* p_curr_scb) {
517 return p_curr_scb && p_curr_scb->in_use &&
518 p_curr_scb->state == BTA_AG_OPEN_ST;
519 }
520
521 /*******************************************************************************
522 *
523 * Function bta_ag_collision_cback
524 *
525 * Description Get notified about collision.
526 *
527 *
528 * Returns void
529 *
530 ******************************************************************************/
bta_ag_collision_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status,uint8_t id,UNUSED_ATTR uint8_t app_id,const RawAddress & peer_addr)531 void bta_ag_collision_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status, uint8_t id,
532 UNUSED_ATTR uint8_t app_id,
533 const RawAddress& peer_addr) {
534 /* Check if we have opening scb for the peer device. */
535 uint16_t handle = bta_ag_idx_by_bdaddr(&peer_addr);
536 tBTA_AG_SCB* p_scb = bta_ag_scb_by_idx(handle);
537
538 if (p_scb && (p_scb->state == BTA_AG_OPENING_ST)) {
539 if (id == BTA_ID_SYS) {
540 LOG(WARNING) << __func__ << ": AG found collision (ACL) for handle "
541 << unsigned(handle) << " device " << peer_addr;
542 } else if (id == BTA_ID_AG) {
543 LOG(WARNING) << __func__ << ": AG found collision (RFCOMM) for handle "
544 << unsigned(handle) << " device " << peer_addr;
545 } else {
546 LOG(WARNING) << __func__ << ": AG found collision (UNKNOWN) for handle "
547 << unsigned(handle) << " device " << peer_addr;
548 }
549 bta_ag_sm_execute(p_scb, BTA_AG_COLLISION_EVT, tBTA_AG_DATA::kEmpty);
550 }
551 }
552
553 /*******************************************************************************
554 *
555 * Function bta_ag_resume_open
556 *
557 * Description Resume opening process.
558 *
559 *
560 * Returns void
561 *
562 ******************************************************************************/
bta_ag_resume_open(tBTA_AG_SCB * p_scb)563 void bta_ag_resume_open(tBTA_AG_SCB* p_scb) {
564 if (p_scb->state == BTA_AG_INIT_ST) {
565 LOG(INFO) << __func__ << ": Resume connection to " << p_scb->peer_addr
566 << ", handle" << bta_ag_scb_to_idx(p_scb);
567 tBTA_AG_DATA open_data = {.api_open = {.bd_addr = p_scb->peer_addr,
568 .sec_mask = p_scb->cli_sec_mask}};
569 bta_ag_sm_execute(p_scb, BTA_AG_API_OPEN_EVT, open_data);
570 } else {
571 VLOG(1) << __func__ << ": device " << p_scb->peer_addr
572 << " is already in state " << std::to_string(p_scb->state);
573 }
574 }
575
576 /*******************************************************************************
577 *
578 * Function bta_ag_api_enable
579 *
580 * Description Handle an API enable event.
581 *
582 *
583 * Returns void
584 *
585 ******************************************************************************/
bta_ag_api_enable(tBTA_AG_CBACK * p_cback)586 void bta_ag_api_enable(tBTA_AG_CBACK* p_cback) {
587 /* initialize control block */
588 for (tBTA_AG_SCB& scb : bta_ag_cb.scb) {
589 alarm_free(scb.ring_timer);
590 alarm_free(scb.codec_negotiation_timer);
591 alarm_free(scb.collision_timer);
592 scb = {};
593 }
594
595 /* store callback function */
596 bta_ag_cb.p_cback = p_cback;
597
598 /* call init call-out */
599 BTM_WriteVoiceSettings(AG_VOICE_SETTINGS);
600
601 bta_sys_collision_register(BTA_ID_AG, bta_ag_collision_cback);
602
603 /* call callback with enable event */
604 (*bta_ag_cb.p_cback)(BTA_AG_ENABLE_EVT, nullptr);
605 }
606
607 /*******************************************************************************
608 *
609 * Function bta_ag_api_disable
610 *
611 * Description Handle an API disable event.
612 *
613 *
614 * Returns void
615 *
616 ******************************************************************************/
bta_ag_api_disable()617 void bta_ag_api_disable() {
618 /* deregister all scbs in use */
619 tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
620 bool do_dereg = false;
621 int i;
622
623 if (!bta_sys_is_register(BTA_ID_AG)) {
624 APPL_TRACE_ERROR("BTA AG is already disabled, ignoring ...");
625 return;
626 }
627
628 /* De-register with BTA system manager */
629 bta_sys_deregister(BTA_ID_AG);
630
631 for (i = 0; i < BTA_AG_MAX_NUM_CLIENTS; i++, p_scb++) {
632 if (p_scb->in_use) {
633 bta_ag_sm_execute(p_scb, BTA_AG_API_DEREGISTER_EVT, tBTA_AG_DATA::kEmpty);
634 do_dereg = true;
635 }
636 }
637
638 if (!do_dereg) {
639 /* Done, send callback evt to app */
640 (*bta_ag_cb.p_cback)(BTA_AG_DISABLE_EVT, nullptr);
641 }
642
643 bta_sys_collision_register(BTA_ID_AG, nullptr);
644 }
645
646 /*******************************************************************************
647 *
648 * Function bta_ag_api_register
649 *
650 * Description Handle an API event registers a new service.
651 *
652 *
653 * Returns void
654 *
655 ******************************************************************************/
bta_ag_api_register(tBTA_SERVICE_MASK services,tBTA_SEC sec_mask,tBTA_AG_FEAT features,const std::vector<std::string> & service_names,uint8_t app_id)656 void bta_ag_api_register(tBTA_SERVICE_MASK services, tBTA_SEC sec_mask,
657 tBTA_AG_FEAT features,
658 const std::vector<std::string>& service_names,
659 uint8_t app_id) {
660 tBTA_AG_SCB* p_scb = bta_ag_scb_alloc();
661 if (p_scb) {
662 APPL_TRACE_DEBUG("bta_ag_api_register: p_scb 0x%08x ", p_scb);
663 tBTA_AG_DATA data = {};
664 data.api_register.features = features;
665 data.api_register.sec_mask = sec_mask;
666 data.api_register.services = services;
667 data.api_register.app_id = app_id;
668 for (int i = 0; i < BTA_AG_NUM_IDX; i++) {
669 if (!service_names[i].empty()) {
670 strlcpy(data.api_register.p_name[i], service_names[i].c_str(),
671 BTA_SERVICE_NAME_LEN);
672 } else {
673 data.api_register.p_name[i][0] = 0;
674 }
675 }
676 bta_ag_sm_execute(p_scb, BTA_AG_API_REGISTER_EVT, data);
677 } else {
678 tBTA_AG bta_ag = {};
679 bta_ag.reg.status = BTA_AG_FAIL_RESOURCES;
680 (*bta_ag_cb.p_cback)(BTA_AG_REGISTER_EVT, &bta_ag);
681 }
682 }
683
684 /*******************************************************************************
685 *
686 * Function bta_ag_api_result
687 *
688 * Description Handle an API result event.
689 *
690 *
691 * Returns void
692 *
693 ******************************************************************************/
bta_ag_api_result(uint16_t handle,tBTA_AG_RES result,const tBTA_AG_RES_DATA & result_data)694 void bta_ag_api_result(uint16_t handle, tBTA_AG_RES result,
695 const tBTA_AG_RES_DATA& result_data) {
696 tBTA_AG_DATA event_data = {};
697 event_data.api_result.result = result;
698 event_data.api_result.data = result_data;
699 tBTA_AG_SCB* p_scb;
700 if (handle != BTA_AG_HANDLE_ALL) {
701 p_scb = bta_ag_scb_by_idx(handle);
702 if (p_scb) {
703 APPL_TRACE_DEBUG("bta_ag_api_result: p_scb 0x%08x ", p_scb);
704 bta_ag_sm_execute(p_scb, static_cast<uint16_t>(BTA_AG_API_RESULT_EVT),
705 event_data);
706 }
707 } else {
708 int i;
709 for (i = 0, p_scb = &bta_ag_cb.scb[0]; i < BTA_AG_MAX_NUM_CLIENTS;
710 i++, p_scb++) {
711 if (p_scb->in_use && p_scb->svc_conn) {
712 APPL_TRACE_DEBUG("bta_ag_api_result p_scb 0x%08x ", p_scb);
713 bta_ag_sm_execute(p_scb, static_cast<uint16_t>(BTA_AG_API_RESULT_EVT),
714 event_data);
715 }
716 }
717 }
718 }
719
720 /*******************************************************************************
721 *
722 * Function bta_ag_sm_execute
723 *
724 * Description State machine event handling function for AG
725 *
726 *
727 * Returns void
728 *
729 ******************************************************************************/
bta_ag_sm_execute(tBTA_AG_SCB * p_scb,uint16_t event,const tBTA_AG_DATA & data)730 void bta_ag_sm_execute(tBTA_AG_SCB* p_scb, uint16_t event,
731 const tBTA_AG_DATA& data) {
732 tBTA_AG_ST_TBL state_table;
733 uint8_t action;
734 int i;
735 uint16_t previous_event = event;
736 uint8_t previous_state = p_scb->state;
737
738 APPL_TRACE_EVENT(
739 "%s: handle=0x%04x, bd_addr=%s, state=%s(0x%02x), "
740 "event=%s(0x%04x), result=%s(0x%02x)",
741 __func__, bta_ag_scb_to_idx(p_scb), p_scb->peer_addr.ToString().c_str(),
742 bta_ag_state_str(p_scb->state), p_scb->state, bta_ag_evt_str(event),
743 event, bta_ag_res_str(data.api_result.result), data.api_result.result);
744
745 event &= 0x00FF;
746 if (event >= (BTA_AG_MAX_EVT & 0x00FF)) {
747 APPL_TRACE_ERROR("%s: event out of range, ignored", __func__);
748 return;
749 }
750
751 /* look up the state table for the current state */
752 state_table = bta_ag_st_tbl[p_scb->state];
753
754 /* set next state */
755 p_scb->state = state_table[event][BTA_AG_NEXT_STATE];
756
757 /* execute action functions */
758 for (i = 0; i < BTA_AG_ACTIONS; i++) {
759 action = state_table[event][i];
760 if (action != BTA_AG_IGNORE) {
761 (*bta_ag_action[action])(p_scb, data);
762 } else {
763 break;
764 }
765 }
766 if (p_scb->state != previous_state) {
767 APPL_TRACE_EVENT(
768 "%s: handle=0x%04x, bd_addr=%s, state_change[%s(0x%02x)]->[%s(0x%02x)],"
769 " event[%s(0x%04x)], result[%s(0x%02x)]",
770 __func__, bta_ag_scb_to_idx(p_scb), p_scb->peer_addr.ToString().c_str(),
771 bta_ag_state_str(previous_state), previous_state,
772 bta_ag_state_str(p_scb->state), p_scb->state,
773 bta_ag_evt_str(previous_event), previous_event,
774 bta_ag_res_str(data.api_result.result), data.api_result.result);
775 }
776 }
777
bta_ag_sm_execute_by_handle(uint16_t handle,uint16_t event,const tBTA_AG_DATA & data)778 void bta_ag_sm_execute_by_handle(uint16_t handle, uint16_t event,
779 const tBTA_AG_DATA& data) {
780 tBTA_AG_SCB* p_scb = bta_ag_scb_by_idx(handle);
781 if (p_scb) {
782 APPL_TRACE_DEBUG("%s: p_scb 0x%08x ", __func__, p_scb);
783 bta_ag_sm_execute(p_scb, event, data);
784 }
785 }
786
787 /**
788 * Handles event from bta_sys_sendmsg(). It is here to support legacy alarm
789 * implementation that is mainly for timeouts.
790 *
791 * @param p_msg event message
792 * @return True to free p_msg, or False if p_msg is freed within this function
793 */
bta_ag_hdl_event(BT_HDR * p_msg)794 bool bta_ag_hdl_event(BT_HDR* p_msg) {
795 switch (p_msg->event) {
796 case BTA_AG_RING_TIMEOUT_EVT:
797 case BTA_AG_SVC_TIMEOUT_EVT:
798 bta_ag_sm_execute_by_handle(p_msg->layer_specific, p_msg->event,
799 tBTA_AG_DATA::kEmpty);
800 break;
801 default:
802 LOG(FATAL) << __func__ << ": bad event " << p_msg->event
803 << " layer_specific=" << p_msg->layer_specific;
804 break;
805 }
806 return true;
807 }
808