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