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