• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 file contains action functions for the audio gateway.
22  *
23  ******************************************************************************/
24 
25 #include <cstring>
26 
27 #include "bta_ag_api.h"
28 #include "bta_ag_int.h"
29 #include "bta_api.h"
30 #include "bta_dm_api.h"
31 #include "bta_sys.h"
32 #include "btif_config.h"
33 #include "l2c_api.h"
34 #include "osi/include/osi.h"
35 #include "port_api.h"
36 #include "utl.h"
37 
38 /*****************************************************************************
39  *  Constants
40  ****************************************************************************/
41 
42 /* maximum length of data to read from RFCOMM */
43 #define BTA_AG_RFC_READ_MAX 512
44 
45 /* maximum AT command length */
46 #define BTA_AG_CMD_MAX 512
47 
48 const uint16_t bta_ag_uuid[BTA_AG_NUM_IDX] = {
49     UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, UUID_SERVCLASS_AG_HANDSFREE};
50 
51 const uint8_t bta_ag_sec_id[BTA_AG_NUM_IDX] = {BTM_SEC_SERVICE_HEADSET_AG,
52                                                BTM_SEC_SERVICE_AG_HANDSFREE};
53 
54 const tBTA_SERVICE_ID bta_ag_svc_id[BTA_AG_NUM_IDX] = {BTA_HSP_SERVICE_ID,
55                                                        BTA_HFP_SERVICE_ID};
56 
57 const tBTA_SERVICE_MASK bta_ag_svc_mask[BTA_AG_NUM_IDX] = {
58     BTA_HSP_SERVICE_MASK, BTA_HFP_SERVICE_MASK};
59 
60 typedef void (*tBTA_AG_ATCMD_CBACK)(tBTA_AG_SCB* p_scb, uint16_t cmd,
61                                     uint8_t arg_type, char* p_arg, char* p_end,
62                                     int16_t int_arg);
63 
64 const tBTA_AG_ATCMD_CBACK bta_ag_at_cback_tbl[BTA_AG_NUM_IDX] = {
65     bta_ag_at_hsp_cback, bta_ag_at_hfp_cback};
66 
67 /*******************************************************************************
68  *
69  * Function         bta_ag_cback_open
70  *
71  * Description      Send open callback event to application.
72  *
73  *
74  * Returns          void
75  *
76  ******************************************************************************/
bta_ag_cback_open(tBTA_AG_SCB * p_scb,const RawAddress & bd_addr,tBTA_AG_STATUS status)77 static void bta_ag_cback_open(tBTA_AG_SCB* p_scb, const RawAddress& bd_addr,
78                               tBTA_AG_STATUS status) {
79   tBTA_AG_OPEN open = {};
80 
81   /* call app callback with open event */
82   open.hdr.handle = bta_ag_scb_to_idx(p_scb);
83   open.hdr.app_id = p_scb->app_id;
84   open.status = status;
85   open.service_id = bta_ag_svc_id[p_scb->conn_service];
86   open.bd_addr = bd_addr;
87 
88   (*bta_ag_cb.p_cback)(BTA_AG_OPEN_EVT, (tBTA_AG*)&open);
89 }
90 
91 /*******************************************************************************
92  *
93  * Function         bta_ag_register
94  *
95  * Description      This function initializes values of the AG cb and sets up
96  *                  the SDP record for the services.
97  *
98  *
99  * Returns          void
100  *
101  ******************************************************************************/
bta_ag_register(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)102 void bta_ag_register(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
103   /* initialize control block */
104   p_scb->reg_services = data.api_register.services;
105   p_scb->serv_sec_mask = data.api_register.sec_mask;
106   p_scb->features = data.api_register.features;
107   p_scb->app_id = data.api_register.app_id;
108 
109   /* create SDP records */
110   bta_ag_create_records(p_scb, data);
111 
112   /* start RFCOMM servers */
113   bta_ag_start_servers(p_scb, p_scb->reg_services);
114 
115   /* call app callback with register event */
116   tBTA_AG_REGISTER reg = {};
117   reg.hdr.handle = bta_ag_scb_to_idx(p_scb);
118   reg.hdr.app_id = p_scb->app_id;
119   reg.status = BTA_AG_SUCCESS;
120   (*bta_ag_cb.p_cback)(BTA_AG_REGISTER_EVT, (tBTA_AG*)&reg);
121 }
122 
123 /*******************************************************************************
124  *
125  * Function         bta_ag_deregister
126  *
127  * Description      This function removes the sdp records, closes the RFCOMM
128  *                  servers, and deallocates the service control block.
129  *
130  *
131  * Returns          void
132  *
133  ******************************************************************************/
bta_ag_deregister(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)134 void bta_ag_deregister(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
135   /* set dealloc */
136   p_scb->dealloc = true;
137 
138   /* remove sdp records */
139   bta_ag_del_records(p_scb);
140 
141   /* remove rfcomm servers */
142   bta_ag_close_servers(p_scb, p_scb->reg_services);
143 
144   /* dealloc */
145   bta_ag_scb_dealloc(p_scb);
146 }
147 
148 /*******************************************************************************
149  *
150  * Function         bta_ag_start_dereg
151  *
152  * Description      Start a deregister event.
153  *
154  *
155  * Returns          void
156  *
157  ******************************************************************************/
bta_ag_start_dereg(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)158 void bta_ag_start_dereg(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
159   /* set dealloc */
160   p_scb->dealloc = true;
161 
162   /* remove sdp records */
163   bta_ag_del_records(p_scb);
164 }
165 
166 /*******************************************************************************
167  *
168  * Function         bta_ag_start_open
169  *
170  * Description      This starts an AG open.
171  *
172  *
173  * Returns          void
174  *
175  ******************************************************************************/
bta_ag_start_open(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)176 void bta_ag_start_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
177   p_scb->peer_addr = data.api_open.bd_addr;
178   p_scb->cli_sec_mask = data.api_open.sec_mask;
179   p_scb->open_services = p_scb->reg_services;
180 
181   /* Check if RFCOMM has any incoming connection to avoid collision. */
182   RawAddress pending_bd_addr = RawAddress::kEmpty;
183   if (PORT_IsOpening(&pending_bd_addr)) {
184     /* Let the incoming connection goes through.                        */
185     /* Issue collision for this scb for now.                            */
186     /* We will decide what to do when we find incoming connetion later. */
187     bta_ag_collision_cback(0, BTA_ID_AG, 0, p_scb->peer_addr);
188     return;
189   }
190 
191   /* close servers */
192   bta_ag_close_servers(p_scb, p_scb->reg_services);
193 
194   /* set role */
195   p_scb->role = BTA_AG_INT;
196 
197   /* do service search */
198   bta_ag_do_disc(p_scb, p_scb->open_services);
199 }
200 
201 /*******************************************************************************
202  *
203  * Function         bta_ag_disc_int_res
204  *
205  * Description      This function handles a discovery result when initiator.
206  *
207  *
208  * Returns          void
209  *
210  ******************************************************************************/
bta_ag_disc_int_res(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)211 void bta_ag_disc_int_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
212   uint16_t event = BTA_AG_DISC_FAIL_EVT;
213 
214   APPL_TRACE_DEBUG("bta_ag_disc_int_res: Status: %d", data.disc_result.status);
215 
216   /* if found service */
217   if (data.disc_result.status == SDP_SUCCESS ||
218       data.disc_result.status == SDP_DB_FULL) {
219     /* get attributes */
220     if (bta_ag_sdp_find_attr(p_scb, p_scb->open_services)) {
221       /* set connected service */
222       p_scb->conn_service = bta_ag_service_to_idx(p_scb->open_services);
223 
224       /* send ourselves sdp ok event */
225       event = BTA_AG_DISC_OK_EVT;
226     }
227   }
228 
229   /* free discovery db */
230   bta_ag_free_db(p_scb, data);
231 
232   /* if service not found check if we should search for other service */
233   if ((event == BTA_AG_DISC_FAIL_EVT) &&
234       (data.disc_result.status == SDP_SUCCESS ||
235        data.disc_result.status == SDP_DB_FULL ||
236        data.disc_result.status == SDP_NO_RECS_MATCH)) {
237     if ((p_scb->open_services & BTA_HFP_SERVICE_MASK) &&
238         (p_scb->open_services & BTA_HSP_SERVICE_MASK)) {
239       /* search for HSP */
240       p_scb->open_services &= ~BTA_HFP_SERVICE_MASK;
241       bta_ag_do_disc(p_scb, p_scb->open_services);
242     } else if ((p_scb->open_services & BTA_HSP_SERVICE_MASK) &&
243                (p_scb->hsp_version == HSP_VERSION_1_2)) {
244       /* search for UUID_SERVCLASS_HEADSET instead */
245       p_scb->hsp_version = HSP_VERSION_1_0;
246       bta_ag_do_disc(p_scb, p_scb->open_services);
247     } else {
248       /* send ourselves sdp ok/fail event */
249       bta_ag_sm_execute(p_scb, event, data);
250     }
251   } else {
252     /* send ourselves sdp ok/fail event */
253     bta_ag_sm_execute(p_scb, event, data);
254   }
255 }
256 
257 /*******************************************************************************
258  *
259  * Function         bta_ag_disc_acp_res
260  *
261  * Description      This function handles a discovery result when acceptor.
262  *
263  *
264  * Returns          void
265  *
266  ******************************************************************************/
bta_ag_disc_acp_res(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)267 void bta_ag_disc_acp_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
268   /* if found service */
269   if (data.disc_result.status == SDP_SUCCESS ||
270       data.disc_result.status == SDP_DB_FULL) {
271     /* get attributes */
272     bta_ag_sdp_find_attr(p_scb, bta_ag_svc_mask[p_scb->conn_service]);
273   }
274 
275   /* free discovery db */
276   bta_ag_free_db(p_scb, data);
277 }
278 
279 /*******************************************************************************
280  *
281  * Function         bta_ag_disc_fail
282  *
283  * Description      This function handles a discovery failure.
284  *
285  *
286  * Returns          void
287  *
288  ******************************************************************************/
bta_ag_disc_fail(tBTA_AG_SCB * p_scb,UNUSED_ATTR const tBTA_AG_DATA & data)289 void bta_ag_disc_fail(tBTA_AG_SCB* p_scb,
290                       UNUSED_ATTR const tBTA_AG_DATA& data) {
291   /* reopen registered servers */
292   bta_ag_start_servers(p_scb, p_scb->reg_services);
293 
294   /* reinitialize stuff */
295 
296   /* clear the remote BD address */
297   RawAddress peer_addr = p_scb->peer_addr;
298   p_scb->peer_addr = RawAddress::kEmpty;
299 
300   /* call open cback w. failure */
301   bta_ag_cback_open(p_scb, peer_addr, BTA_AG_FAIL_SDP);
302 }
303 
304 /*******************************************************************************
305  *
306  * Function         bta_ag_open_fail
307  *
308  * Description      open connection failed.
309  *
310  *
311  * Returns          void
312  *
313  ******************************************************************************/
bta_ag_open_fail(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)314 void bta_ag_open_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
315   /* call open cback w. failure */
316   bta_ag_cback_open(p_scb, data.api_open.bd_addr, BTA_AG_FAIL_RESOURCES);
317 }
318 
319 /*******************************************************************************
320  *
321  * Function         bta_ag_rfc_fail
322  *
323  * Description      RFCOMM connection failed.
324  *
325  *
326  * Returns          void
327  *
328  ******************************************************************************/
bta_ag_rfc_fail(tBTA_AG_SCB * p_scb,UNUSED_ATTR const tBTA_AG_DATA & data)329 void bta_ag_rfc_fail(tBTA_AG_SCB* p_scb, UNUSED_ATTR const tBTA_AG_DATA& data) {
330   RawAddress peer_addr = p_scb->peer_addr;
331   /* reinitialize stuff */
332   p_scb->conn_handle = 0;
333   p_scb->conn_service = 0;
334   p_scb->peer_features = 0;
335   p_scb->peer_codecs = BTA_AG_CODEC_CVSD;
336   p_scb->sco_codec = BTA_AG_CODEC_CVSD;
337   p_scb->role = 0;
338   p_scb->svc_conn = false;
339   p_scb->hsp_version = HSP_VERSION_1_2;
340   /*Clear the BD address*/
341   p_scb->peer_addr = RawAddress::kEmpty;
342 
343   /* reopen registered servers */
344   bta_ag_start_servers(p_scb, p_scb->reg_services);
345 
346   /* call open cback w. failure */
347   bta_ag_cback_open(p_scb, peer_addr, BTA_AG_FAIL_RFCOMM);
348 }
349 
350 /*******************************************************************************
351  *
352  * Function         bta_ag_rfc_close
353  *
354  * Description      RFCOMM connection closed.
355  *
356  *
357  * Returns          void
358  *
359  ******************************************************************************/
bta_ag_rfc_close(tBTA_AG_SCB * p_scb,UNUSED_ATTR const tBTA_AG_DATA & data)360 void bta_ag_rfc_close(tBTA_AG_SCB* p_scb,
361                       UNUSED_ATTR const tBTA_AG_DATA& data) {
362   tBTA_AG_CLOSE close = {};
363   tBTA_SERVICE_MASK services;
364   int i, num_active_conn = 0;
365 
366   /* reinitialize stuff */
367   p_scb->conn_service = 0;
368   p_scb->peer_features = 0;
369   p_scb->peer_codecs = BTA_AG_CODEC_CVSD;
370   p_scb->sco_codec = BTA_AG_CODEC_CVSD;
371   /* Clear these flags upon SLC teardown */
372   p_scb->codec_updated = false;
373   p_scb->codec_fallback = false;
374   p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
375   p_scb->role = 0;
376   p_scb->post_sco = BTA_AG_POST_SCO_NONE;
377   p_scb->svc_conn = false;
378   p_scb->hsp_version = HSP_VERSION_1_2;
379   bta_ag_at_reinit(&p_scb->at_cb);
380 
381   for (auto& peer_hf_indicator : p_scb->peer_hf_indicators) {
382     peer_hf_indicator = {};
383   }
384   for (auto& local_hf_indicator : p_scb->local_hf_indicators) {
385     local_hf_indicator = {};
386   }
387 
388   /* stop timers */
389   alarm_cancel(p_scb->ring_timer);
390   alarm_cancel(p_scb->codec_negotiation_timer);
391 
392   close.hdr.handle = bta_ag_scb_to_idx(p_scb);
393   close.hdr.app_id = p_scb->app_id;
394   close.bd_addr = p_scb->peer_addr;
395 
396   bta_sys_conn_close(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
397 
398   if (bta_ag_get_active_device() == p_scb->peer_addr) {
399     bta_clear_active_device();
400   }
401 
402   /* call close cback */
403   (*bta_ag_cb.p_cback)(BTA_AG_CLOSE_EVT, (tBTA_AG*)&close);
404 
405   /* if not deregistering (deallocating) reopen registered servers */
406   if (!p_scb->dealloc) {
407     /* Clear peer bd_addr so instance can be reused */
408     p_scb->peer_addr = RawAddress::kEmpty;
409 
410     /* start only unopened server */
411     services = p_scb->reg_services;
412     for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++) {
413       if (p_scb->serv_handle[i])
414         services &= ~((tBTA_SERVICE_MASK)1 << (BTA_HSP_SERVICE_ID + i));
415     }
416     bta_ag_start_servers(p_scb, services);
417 
418     p_scb->conn_handle = 0;
419 
420     /* Make sure SCO state is BTA_AG_SCO_SHUTDOWN_ST */
421     bta_ag_sco_shutdown(p_scb, tBTA_AG_DATA::kEmpty);
422 
423     /* Check if all the SLCs are down */
424     for (i = 0; i < BTA_AG_MAX_NUM_CLIENTS; i++) {
425       if (bta_ag_cb.scb[i].in_use && bta_ag_cb.scb[i].svc_conn)
426         num_active_conn++;
427     }
428 
429     if (!num_active_conn) {
430       bta_sys_sco_unuse(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
431     }
432 
433   }
434   /* else close port and deallocate scb */
435   else {
436     RFCOMM_RemoveServer(p_scb->conn_handle);
437     bta_ag_scb_dealloc(p_scb);
438   }
439 }
440 
441 /*******************************************************************************
442  *
443  * Function         bta_ag_rfc_open
444  *
445  * Description      Handle RFCOMM channel open.
446  *
447  *
448  * Returns          void
449  *
450  ******************************************************************************/
bta_ag_rfc_open(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)451 void bta_ag_rfc_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
452   /* initialize AT feature variables */
453   p_scb->clip_enabled = false;
454   p_scb->ccwa_enabled = false;
455   p_scb->cmer_enabled = false;
456   p_scb->cmee_enabled = false;
457   p_scb->inband_enabled =
458       ((p_scb->features & BTA_AG_FEAT_INBAND) == BTA_AG_FEAT_INBAND);
459   if (p_scb->conn_service == BTA_AG_HFP) {
460     size_t version_value_size = sizeof(p_scb->peer_version);
461     if (!btif_config_get_bin(
462             p_scb->peer_addr.ToString(), HFP_VERSION_CONFIG_KEY,
463             (uint8_t*)&p_scb->peer_version, &version_value_size)) {
464       APPL_TRACE_WARNING("%s: Failed read cached peer HFP version for %s",
465                          __func__, p_scb->peer_addr.ToString().c_str());
466       p_scb->peer_version = HFP_HSP_VERSION_UNKNOWN;
467     }
468     size_t sdp_features_size = sizeof(p_scb->peer_sdp_features);
469     if (btif_config_get_bin(
470             p_scb->peer_addr.ToString(), HFP_SDP_FEATURES_CONFIG_KEY,
471             (uint8_t*)&p_scb->peer_sdp_features, &sdp_features_size)) {
472       bool sdp_wbs_support = p_scb->peer_sdp_features & BTA_AG_FEAT_WBS_SUPPORT;
473       if (!p_scb->received_at_bac && sdp_wbs_support) {
474         p_scb->codec_updated = true;
475         p_scb->peer_codecs = BTA_AG_CODEC_CVSD & BTA_AG_CODEC_MSBC;
476         p_scb->sco_codec = UUID_CODEC_MSBC;
477       }
478     } else {
479       APPL_TRACE_WARNING("%s: Failed read cached peer HFP SDP features for %s",
480                          __func__, p_scb->peer_addr.ToString().c_str());
481     }
482   }
483 
484   /* set up AT command interpreter */
485   p_scb->at_cb.p_at_tbl = bta_ag_at_tbl[p_scb->conn_service];
486   p_scb->at_cb.p_cmd_cback = bta_ag_at_cback_tbl[p_scb->conn_service];
487   p_scb->at_cb.p_err_cback = bta_ag_at_err_cback;
488   p_scb->at_cb.p_user = p_scb;
489   p_scb->at_cb.cmd_max_len = BTA_AG_CMD_MAX;
490   bta_ag_at_init(&p_scb->at_cb);
491 
492   bta_sys_conn_open(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
493 
494   bta_ag_cback_open(p_scb, p_scb->peer_addr, BTA_AG_SUCCESS);
495 
496   if (p_scb->conn_service == BTA_AG_HFP) {
497     /* if hfp start timer for service level conn */
498     bta_sys_start_timer(p_scb->ring_timer, p_bta_ag_cfg->conn_tout,
499                         BTA_AG_SVC_TIMEOUT_EVT, bta_ag_scb_to_idx(p_scb));
500   } else {
501     /* else service level conn is open */
502     bta_ag_svc_conn_open(p_scb, data);
503   }
504 }
505 
506 /*******************************************************************************
507  *
508  * Function         bta_ag_rfc_acp_open
509  *
510  * Description      Handle RFCOMM channel open when accepting connection.
511  *
512  *
513  * Returns          void
514  *
515  ******************************************************************************/
bta_ag_rfc_acp_open(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)516 void bta_ag_rfc_acp_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
517   APPL_TRACE_DEBUG("%s: serv_handle0 = %d serv_handle = %d", __func__,
518                    p_scb->serv_handle[0], p_scb->serv_handle[1]);
519   /* set role */
520   p_scb->role = BTA_AG_ACP;
521 
522   /* get bd addr of peer */
523   uint16_t lcid = 0;
524   RawAddress dev_addr = RawAddress::kEmpty;
525   int status = PORT_CheckConnection(data.rfc.port_handle, &dev_addr, &lcid);
526   if (status != PORT_SUCCESS) {
527     LOG(ERROR) << __func__ << ", PORT_CheckConnection returned " << status;
528     return;
529   }
530 
531   /* Collision Handling */
532   for (tBTA_AG_SCB& ag_scb : bta_ag_cb.scb) {
533     // Cancel any pending collision timers
534     if (ag_scb.in_use && alarm_is_scheduled(ag_scb.collision_timer)) {
535       VLOG(1) << __func__ << ": cancel collision alarm for "
536               << ag_scb.peer_addr;
537       alarm_cancel(ag_scb.collision_timer);
538       if (dev_addr != ag_scb.peer_addr && p_scb != &ag_scb) {
539         // Resume outgoing connection if incoming is not on the same device
540         bta_ag_resume_open(&ag_scb);
541       }
542     }
543     if (dev_addr == ag_scb.peer_addr && p_scb != &ag_scb) {
544       VLOG(1) << __func__ << ": fail outgoing connection before accepting "
545               << ag_scb.peer_addr;
546       // Fail the outgoing connection to clean up any upper layer states
547       bta_ag_rfc_fail(&ag_scb, tBTA_AG_DATA::kEmpty);
548       // If client port is opened, close it
549       if (ag_scb.conn_handle > 0) {
550         status = RFCOMM_RemoveConnection(ag_scb.conn_handle);
551         if (status != PORT_SUCCESS) {
552           LOG(WARNING) << __func__ << ": RFCOMM_RemoveConnection failed for "
553                        << dev_addr << ", handle "
554                        << std::to_string(ag_scb.conn_handle) << ", error "
555                        << status;
556         }
557       }
558     }
559     VLOG(1) << __func__ << ": dev_addr=" << dev_addr
560             << ", peer_addr=" << ag_scb.peer_addr
561             << ", in_use=" << ag_scb.in_use
562             << ", index=" << bta_ag_scb_to_idx(p_scb);
563   }
564 
565   p_scb->peer_addr = dev_addr;
566 
567   /* determine connected service from port handle */
568   for (uint8_t i = 0; i < BTA_AG_NUM_IDX; i++) {
569     APPL_TRACE_DEBUG(
570         "bta_ag_rfc_acp_open: i = %d serv_handle = %d port_handle = %d", i,
571         p_scb->serv_handle[i], data.rfc.port_handle);
572 
573     if (p_scb->serv_handle[i] == data.rfc.port_handle) {
574       p_scb->conn_service = i;
575       p_scb->conn_handle = data.rfc.port_handle;
576       break;
577     }
578   }
579 
580   APPL_TRACE_DEBUG("bta_ag_rfc_acp_open: conn_service = %d conn_handle = %d",
581                    p_scb->conn_service, p_scb->conn_handle);
582 
583   /* close any unopened server */
584   bta_ag_close_servers(
585       p_scb, (p_scb->reg_services & ~bta_ag_svc_mask[p_scb->conn_service]));
586 
587   /* do service discovery to get features */
588   bta_ag_do_disc(p_scb, bta_ag_svc_mask[p_scb->conn_service]);
589 
590   /* continue with common open processing */
591   bta_ag_rfc_open(p_scb, data);
592 }
593 
594 /*******************************************************************************
595  *
596  * Function         bta_ag_rfc_data
597  *
598  * Description      Read and process data from RFCOMM.
599  *
600  *
601  * Returns          void
602  *
603  ******************************************************************************/
bta_ag_rfc_data(tBTA_AG_SCB * p_scb,UNUSED_ATTR const tBTA_AG_DATA & data)604 void bta_ag_rfc_data(tBTA_AG_SCB* p_scb, UNUSED_ATTR const tBTA_AG_DATA& data) {
605   uint16_t len;
606   char buf[BTA_AG_RFC_READ_MAX] = "";
607 
608   APPL_TRACE_DEBUG("%s", __func__);
609 
610   /* do the following */
611   for (;;) {
612     /* read data from rfcomm; if bad status, we're done */
613     if (PORT_ReadData(p_scb->conn_handle, buf, BTA_AG_RFC_READ_MAX, &len) !=
614         PORT_SUCCESS) {
615       LOG(ERROR) << __func__ << ": failed to read data " << p_scb->peer_addr;
616       break;
617     }
618 
619     /* if no data, we're done */
620     if (len == 0) {
621       LOG(WARNING) << __func__ << ": no data for " << p_scb->peer_addr;
622       break;
623     }
624 
625     /* run AT command interpreter on data */
626     bta_sys_busy(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
627     bta_ag_at_parse(&p_scb->at_cb, buf, len);
628     if ((p_scb->sco_idx != BTM_INVALID_SCO_INDEX) &&
629         bta_ag_sco_is_open(p_scb)) {
630       APPL_TRACE_DEBUG("%s change link policy for SCO", __func__);
631       bta_sys_sco_open(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
632     } else {
633       bta_sys_idle(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
634     }
635 
636     /* no more data to read, we're done */
637     if (len < BTA_AG_RFC_READ_MAX) {
638       break;
639     }
640   }
641 }
642 
643 /*******************************************************************************
644  *
645  * Function         bta_ag_start_close
646  *
647  * Description      Start the process of closing SCO and RFCOMM connection.
648  *
649  *
650  * Returns          void
651  *
652  ******************************************************************************/
bta_ag_start_close(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)653 void bta_ag_start_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
654   /* Take the link out of sniff and set L2C idle time to 0 */
655   bta_dm_pm_active(p_scb->peer_addr);
656   L2CA_SetIdleTimeoutByBdAddr(p_scb->peer_addr, 0, BT_TRANSPORT_BR_EDR);
657 
658   /* if SCO is open close SCO and wait on RFCOMM close */
659   if (bta_ag_sco_is_open(p_scb)) {
660     p_scb->post_sco = BTA_AG_POST_SCO_CLOSE_RFC;
661   } else {
662     p_scb->post_sco = BTA_AG_POST_SCO_NONE;
663     bta_ag_rfc_do_close(p_scb, data);
664   }
665 
666   /* always do SCO shutdown to handle all SCO corner cases */
667   bta_ag_sco_shutdown(p_scb, data);
668 }
669 
670 /*******************************************************************************
671  *
672  * Function         bta_ag_post_sco_open
673  *
674  * Description      Perform post-SCO open action, if any
675  *
676  *
677  * Returns          void
678  *
679  ******************************************************************************/
bta_ag_post_sco_open(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)680 void bta_ag_post_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
681   switch (p_scb->post_sco) {
682     case BTA_AG_POST_SCO_RING:
683       bta_ag_send_ring(p_scb, data);
684       p_scb->post_sco = BTA_AG_POST_SCO_NONE;
685       break;
686 
687     case BTA_AG_POST_SCO_CALL_CONN:
688       bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_CONN_RES);
689       p_scb->post_sco = BTA_AG_POST_SCO_NONE;
690       break;
691 
692     default:
693       break;
694   }
695 }
696 
697 /*******************************************************************************
698  *
699  * Function         bta_ag_post_sco_close
700  *
701  * Description      Perform post-SCO close action, if any
702  *
703  *
704  * Returns          void
705  *
706  ******************************************************************************/
bta_ag_post_sco_close(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)707 void bta_ag_post_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
708   switch (p_scb->post_sco) {
709     case BTA_AG_POST_SCO_CLOSE_RFC:
710       bta_ag_rfc_do_close(p_scb, data);
711       p_scb->post_sco = BTA_AG_POST_SCO_NONE;
712       break;
713 
714     case BTA_AG_POST_SCO_CALL_CONN:
715       bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_CONN_RES);
716       p_scb->post_sco = BTA_AG_POST_SCO_NONE;
717       break;
718 
719     case BTA_AG_POST_SCO_CALL_ORIG:
720       bta_ag_send_call_inds(p_scb, BTA_AG_OUT_CALL_ORIG_RES);
721       p_scb->post_sco = BTA_AG_POST_SCO_NONE;
722       break;
723 
724     case BTA_AG_POST_SCO_CALL_END:
725       bta_ag_send_call_inds(p_scb, BTA_AG_END_CALL_RES);
726       p_scb->post_sco = BTA_AG_POST_SCO_NONE;
727       break;
728 
729     case BTA_AG_POST_SCO_CALL_END_INCALL:
730       bta_ag_send_call_inds(p_scb, BTA_AG_END_CALL_RES);
731 
732       /* Sending callsetup IND and Ring were defered to after SCO close. */
733       bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_RES);
734 
735       if (bta_ag_inband_enabled(p_scb) &&
736           !(p_scb->features & BTA_AG_FEAT_NOSCO)) {
737         p_scb->post_sco = BTA_AG_POST_SCO_RING;
738         bta_ag_sco_open(p_scb, data);
739       } else {
740         p_scb->post_sco = BTA_AG_POST_SCO_NONE;
741         bta_ag_send_ring(p_scb, data);
742       }
743       break;
744 
745     default:
746       break;
747   }
748 }
749 
750 /*******************************************************************************
751  *
752  * Function         bta_ag_svc_conn_open
753  *
754  * Description      Service level connection opened
755  *
756  *
757  * Returns          void
758  *
759  ******************************************************************************/
bta_ag_svc_conn_open(tBTA_AG_SCB * p_scb,UNUSED_ATTR const tBTA_AG_DATA & data)760 void bta_ag_svc_conn_open(tBTA_AG_SCB* p_scb,
761                           UNUSED_ATTR const tBTA_AG_DATA& data) {
762   tBTA_AG_CONN evt = {};
763 
764   if (!p_scb->svc_conn) {
765     /* set state variable */
766     p_scb->svc_conn = true;
767 
768     /* Clear AT+BIA mask from previous SLC if any. */
769     p_scb->bia_masked_out = 0;
770 
771     alarm_cancel(p_scb->ring_timer);
772 
773     /* call callback */
774     evt.hdr.handle = bta_ag_scb_to_idx(p_scb);
775     evt.hdr.app_id = p_scb->app_id;
776     evt.peer_feat = p_scb->peer_features;
777     evt.bd_addr = p_scb->peer_addr;
778     evt.peer_codec = p_scb->peer_codecs;
779 
780     if ((p_scb->call_ind != BTA_AG_CALL_INACTIVE) ||
781         (p_scb->callsetup_ind != BTA_AG_CALLSETUP_NONE)) {
782       bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
783     }
784     if (bta_ag_get_active_device().IsEmpty()) {
785       bta_ag_api_set_active_device(p_scb->peer_addr);
786     }
787     (*bta_ag_cb.p_cback)(BTA_AG_CONN_EVT, (tBTA_AG*)&evt);
788   }
789 }
790 
791 /*******************************************************************************
792  *
793  * Function         bta_ag_setcodec
794  *
795  * Description      Handle API SetCodec
796  *
797  *
798  * Returns          void
799  *
800  ******************************************************************************/
bta_ag_setcodec(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)801 void bta_ag_setcodec(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
802   tBTA_AG_PEER_CODEC codec_type = data.api_setcodec.codec;
803   tBTA_AG_VAL val = {};
804   val.hdr.handle = bta_ag_scb_to_idx(p_scb);
805 
806   /* Check if the requested codec type is valid */
807   if ((codec_type != BTA_AG_CODEC_NONE) && (codec_type != BTA_AG_CODEC_CVSD) &&
808       (codec_type != BTA_AG_CODEC_MSBC)) {
809     val.num = codec_type;
810     val.hdr.status = BTA_AG_FAIL_RESOURCES;
811     APPL_TRACE_ERROR("bta_ag_setcodec error: unsupported codec type %d",
812                      codec_type);
813     (*bta_ag_cb.p_cback)(BTA_AG_WBS_EVT, (tBTA_AG*)&val);
814     return;
815   }
816 
817   if ((p_scb->peer_codecs & codec_type) || (codec_type == BTA_AG_CODEC_NONE) ||
818       (codec_type == BTA_AG_CODEC_CVSD)) {
819     p_scb->sco_codec = codec_type;
820     p_scb->codec_updated = true;
821     val.num = codec_type;
822     val.hdr.status = BTA_AG_SUCCESS;
823     APPL_TRACE_DEBUG("bta_ag_setcodec: Updated codec type %d", codec_type);
824   } else {
825     val.num = codec_type;
826     val.hdr.status = BTA_AG_FAIL_RESOURCES;
827     APPL_TRACE_ERROR("bta_ag_setcodec error: unsupported codec type %d",
828                      codec_type);
829   }
830 
831   (*bta_ag_cb.p_cback)(BTA_AG_WBS_EVT, (tBTA_AG*)&val);
832 }
833 
bta_ag_collision_timer_cback(void * data)834 static void bta_ag_collision_timer_cback(void* data) {
835   if (data == nullptr) {
836     LOG(ERROR) << __func__ << ": data should never be null in a timer callback";
837     return;
838   }
839   /* If the peer haven't opened AG connection     */
840   /* we will restart opening process.             */
841   bta_ag_resume_open(static_cast<tBTA_AG_SCB*>(data));
842 }
843 
bta_ag_handle_collision(tBTA_AG_SCB * p_scb,UNUSED_ATTR const tBTA_AG_DATA & data)844 void bta_ag_handle_collision(tBTA_AG_SCB* p_scb,
845                              UNUSED_ATTR const tBTA_AG_DATA& data) {
846   /* Cancel SDP if it had been started. */
847   if (p_scb->p_disc_db) {
848     SDP_CancelServiceSearch(p_scb->p_disc_db);
849     bta_ag_free_db(p_scb, tBTA_AG_DATA::kEmpty);
850   }
851 
852   /* reopen registered servers */
853   /* Collision may be detected before or after we close servers. */
854   if (bta_ag_is_server_closed(p_scb)) {
855     bta_ag_start_servers(p_scb, p_scb->reg_services);
856   }
857 
858   /* Start timer to han */
859   alarm_set_on_mloop(p_scb->collision_timer, BTA_AG_COLLISION_TIMEOUT_MS,
860                      bta_ag_collision_timer_cback, p_scb);
861 }
862