• 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 is the private interface file for the BTA audio gateway.
22  *
23  ******************************************************************************/
24 #ifndef BTA_AG_INT_H
25 #define BTA_AG_INT_H
26 
27 #include <cstdint>
28 
29 #include "bta/ag/bta_ag_at.h"
30 #include "bta/include/bta_ag_api.h"
31 #include "bta/include/bta_api.h"
32 #include "bta/sys/bta_sys.h"
33 
34 /*****************************************************************************
35  *  Constants
36  ****************************************************************************/
37 /* Time to wait for retry in case of collision */
38 #ifndef BTA_AG_COLLISION_TIMEOUT_MS
39 #define BTA_AG_COLLISION_TIMEOUT_MS (2 * 1000) /* 2 seconds */
40 #endif
41 
42 /* RFCOMM MTU SIZE */
43 #define BTA_AG_MTU 256
44 
45 /* Max number of peer and local HF indicators */
46 #define BTA_AG_MAX_NUM_PEER_HF_IND 20
47 #define BTA_AG_MAX_NUM_LOCAL_HF_IND 4
48 
49 /* Internal profile indexes */
50 #define BTA_AG_HSP 0     /* index for HSP */
51 #define BTA_AG_HFP 1     /* index for HFP */
52 #define BTA_AG_NUM_IDX 2 /* number of profile indexes */
53 
54 /* profile role for connection */
55 #define BTA_AG_ACP 0 /* accepted connection */
56 #define BTA_AG_INT 1 /* initiating connection */
57 
58 #define BTA_AG_SDP_FEAT_SPEC                                \
59   (BTA_AG_FEAT_3WAY | BTA_AG_FEAT_ECNR | BTA_AG_FEAT_VREC | \
60    BTA_AG_FEAT_INBAND | BTA_AG_FEAT_VTAG)
61 
62 /* Timeout for alarm in 2018 toyota camry carkit workaround */
63 #define BTA_AG_BIND_TIMEOUT_MS 500
64 
65 enum {
66   /* these events are handled by the state machine */
67   BTA_AG_API_REGISTER_EVT = BTA_SYS_EVT_START(BTA_ID_AG),
68   BTA_AG_API_DEREGISTER_EVT,
69   BTA_AG_API_OPEN_EVT,
70   BTA_AG_API_CLOSE_EVT,
71   BTA_AG_API_AUDIO_OPEN_EVT,
72   BTA_AG_API_AUDIO_CLOSE_EVT,
73   BTA_AG_API_RESULT_EVT,
74   BTA_AG_API_SETCODEC_EVT,
75   BTA_AG_RFC_OPEN_EVT,
76   BTA_AG_RFC_CLOSE_EVT,
77   BTA_AG_RFC_SRV_CLOSE_EVT,
78   BTA_AG_RFC_DATA_EVT,
79   BTA_AG_SCO_OPEN_EVT,
80   BTA_AG_SCO_CLOSE_EVT,
81   BTA_AG_DISC_ACP_RES_EVT,
82   BTA_AG_DISC_INT_RES_EVT,
83   BTA_AG_DISC_OK_EVT,
84   BTA_AG_DISC_FAIL_EVT,
85   BTA_AG_RING_TIMEOUT_EVT,
86   BTA_AG_SVC_TIMEOUT_EVT,
87   BTA_AG_COLLISION_EVT,
88   BTA_AG_MAX_EVT,
89 };
90 
91 /* Actions to perform after a SCO event */
92 enum {
93   BTA_AG_POST_SCO_NONE,      /* no action */
94   BTA_AG_POST_SCO_CLOSE_RFC, /* close RFCOMM channel after SCO closes */
95   BTA_AG_POST_SCO_RING,      /* send RING result code after SCO opens */
96   BTA_AG_POST_SCO_CALL_CONN, /* send call indicators after SCO opens/closes */
97   BTA_AG_POST_SCO_CALL_ORIG, /* send call indicators after SCO closes */
98   BTA_AG_POST_SCO_CALL_END,  /* send call indicators after SCO closes */
99   BTA_AG_POST_SCO_CALL_END_INCALL /* send call indicators for end call &
100                                      incoming call after SCO closes */
101 };
102 
103 /* sco states */
104 typedef enum : uint8_t {
105   BTA_AG_SCO_SHUTDOWN_ST,   /* no sco listening, all sco connections closed */
106   BTA_AG_SCO_LISTEN_ST,     /* sco listening */
107   BTA_AG_SCO_CODEC_ST,      /* sco codec negotiation */
108   BTA_AG_SCO_OPENING_ST,    /* sco connection opening */
109   BTA_AG_SCO_OPEN_CL_ST,    /* opening sco connection being closed */
110   BTA_AG_SCO_OPEN_XFER_ST,  /* opening sco connection being transferred */
111   BTA_AG_SCO_OPEN_ST,       /* sco open */
112   BTA_AG_SCO_CLOSING_ST,    /* sco closing */
113   BTA_AG_SCO_CLOSE_OP_ST,   /* closing sco being opened */
114   BTA_AG_SCO_CLOSE_XFER_ST, /* closing sco being transferred */
115   BTA_AG_SCO_SHUTTING_ST    /* sco shutting down */
116 } tBTA_AG_SCO;
117 
118 /*****************************************************************************
119  *  Data types
120  ****************************************************************************/
121 
122 /* data type for BTA_AG_API_REGISTER_EVT */
123 typedef struct {
124   char p_name[2][BTA_SERVICE_NAME_LEN + 1];
125   tBTA_SERVICE_MASK services;
126   tBTA_AG_FEAT features;
127   uint8_t app_id;
128 } tBTA_AG_API_REGISTER;
129 
130 /* data type for BTA_AG_API_OPEN_EVT */
131 typedef struct {
132   RawAddress bd_addr;
133 } tBTA_AG_API_OPEN;
134 
135 /* data type for BTA_AG_API_RESULT_EVT */
136 typedef struct {
137   tBTA_AG_RES result;
138   tBTA_AG_RES_DATA data;
ToString__anon593569fa0608139   std::string ToString() const {
140     return base::StringPrintf("result:%s", bta_ag_result_text(result).c_str());
141   }
142 } tBTA_AG_API_RESULT;
143 
144 /* data type for BTA_AG_API_SETCODEC_EVT */
145 typedef struct {
146   tBTA_AG_PEER_CODEC codec;
147 } tBTA_AG_API_SETCODEC;
148 
149 /* data type for BTA_AG_DISC_RESULT_EVT */
150 typedef struct {
151   uint16_t status;
152 } tBTA_AG_DISC_RESULT;
153 
154 /* data type for RFCOMM events */
155 typedef struct {
156   uint16_t port_handle;
157 } tBTA_AG_RFC;
158 
159 /* union of all event datatypes */
160 union tBTA_AG_DATA {
161   tBTA_AG_API_REGISTER api_register;
162   tBTA_AG_API_OPEN api_open;
163   tBTA_AG_API_RESULT api_result;
164   tBTA_AG_API_SETCODEC api_setcodec;
165   tBTA_AG_DISC_RESULT disc_result;
166   tBTA_AG_RFC rfc;
167   static const tBTA_AG_DATA kEmpty;
168 
169   /**
170    * Check if two tBTA_AG_DATA are equal in memory
171    *
172    * @param rhs other tBTA_AG_DATA
173    * @return true if both unions are equal in memory
174    */
175   bool operator==(const tBTA_AG_DATA& rhs) const {
176     return (std::memcmp(this, &rhs, sizeof(tBTA_AG_DATA)) == 0);
177   }
178 
179   /**
180    * Check if this union is empty by comparing it to the kEmpty constant
181    *
182    * @return true if this union is empty
183    */
IsEmpty()184   bool IsEmpty() const { return *this == kEmpty; }
185 };
186 
187 /* type for each profile */
188 typedef struct {
189   uint32_t sdp_handle;
190   uint8_t scn;
191 } tBTA_AG_PROFILE;
192 
193 typedef enum {
194   BTA_AG_SCO_MSBC_SETTINGS_T2 = 0, /* preferred/default when codec is mSBC */
195   BTA_AG_SCO_MSBC_SETTINGS_T1,
196 } tBTA_AG_SCO_MSBC_SETTINGS;
197 
198 /* type for each service control block */
199 struct tBTA_AG_SCB {
200   char clip[BTA_AG_AT_MAX_LEN + 1];     /* number string used for CLIP */
201   uint16_t serv_handle[BTA_AG_NUM_IDX]; /* RFCOMM server handles */
202   tBTA_AG_AT_CB at_cb;                  /* AT command interpreter */
203   RawAddress peer_addr;                 /* peer bd address */
204   tSDP_DISCOVERY_DB* p_disc_db;         /* pointer to discovery database */
205   tBTA_SERVICE_MASK reg_services;       /* services specified in register API */
206   tBTA_SERVICE_MASK open_services;      /* services specified in open API */
207   uint16_t conn_handle;                 /* RFCOMM handle of connected service */
208   tBTA_AG_FEAT features;                /* features registered by application */
209   tBTA_AG_FEAT masked_features;         /* local BRSF features for this connection */
210   tBTA_AG_PEER_FEAT peer_features;      /* peer device features */
211   uint16_t peer_sdp_features;           /* peer device SDP features */
212   uint16_t peer_version;                /* profile version of peer device */
213   uint16_t hsp_version;                 /* HSP profile version before SDP */
214   uint16_t sco_idx;                     /* SCO handle */
215   bool in_use;                          /* scb in use */
216   bool dealloc;                         /* true if service shutting down */
217   bool clip_enabled;        /* set to true if HF enables CLIP reporting */
218   bool ccwa_enabled;        /* set to true if HF enables CCWA reporting */
219   bool cmer_enabled;        /* set to true if HF enables CMER reporting */
220   bool cmee_enabled;        /* set to true if HF enables CME ERROR reporting */
221   bool inband_enabled;      /* set to true if inband ring enabled */
222   bool svc_conn;            /* set to true when service level connection up */
223   uint8_t state;            /* state machine state */
224   uint8_t conn_service;     /* connected service */
225   uint8_t peer_scn;         /* peer scn */
226   uint8_t app_id;           /* application id */
227   uint8_t role;             /* initiator/acceptor role */
228   uint8_t post_sco;         /* action to perform after sco event */
229   uint8_t call_ind;         /* CIEV call indicator value */
230   uint8_t callsetup_ind;    /* CIEV callsetup indicator value */
231   uint8_t service_ind;      /* CIEV service indicator value */
232   uint8_t signal_ind;       /* CIEV signal indicator value */
233   uint8_t roam_ind;         /* CIEV roam indicator value */
234   uint8_t battchg_ind;      /* CIEV battery charge indicator value */
235   uint8_t callheld_ind;     /* CIEV call held indicator value */
236   uint32_t bia_masked_out;  /* indicators HF does not want us to send */
237   alarm_t* bind_timer;      /* Timer for toyota camry 2018 carkit workaround */
238   alarm_t* collision_timer;
239   alarm_t* ring_timer;
240   alarm_t* codec_negotiation_timer;
241   bool received_at_bac; /* indicate AT+BAC is received at least once */
242   tBTA_AG_PEER_CODEC peer_codecs; /* codecs for eSCO supported by the peer */
243   tBTA_AG_PEER_CODEC sco_codec;   /* codec to be used for eSCO connection */
244   tBTA_AG_PEER_CODEC
245       inuse_codec;     /* codec being used for the current SCO connection */
246   bool codec_updated;  /* set to true whenever the app updates codec type */
247   bool codec_fallback; /* If sco nego fails for mSBC, fallback to CVSD */
248   tBTA_AG_SCO_MSBC_SETTINGS
249       codec_msbc_settings; /* settings to be used for the impending eSCO */
250 
251   tBTA_AG_HF_IND
252       peer_hf_indicators[BTA_AG_MAX_NUM_PEER_HF_IND]; /* Peer supported
253                                                   HF indicators */
254   tBTA_AG_HF_IND
255       local_hf_indicators[BTA_AG_MAX_NUM_LOCAL_HF_IND]; /* Local supported
256                                                     HF indicators */
257 
ToStringtBTA_AG_SCB258   std::string ToString() const {
259     return base::StringPrintf(
260         "codec_updated=%d, codec_fallback=%d, "
261         "sco_codec=%d, peer_codec=%d, msbc_settings=%d, device=%s",
262         codec_updated, codec_fallback, sco_codec, peer_codecs,
263         codec_msbc_settings, peer_addr.ToString().c_str());
264   }
265 };
266 
267 /* type for sco data */
268 typedef struct {
269   tBTM_ESCO_CONN_REQ_EVT_DATA conn_data; /* SCO data for pending conn request */
270   tBTA_AG_SCB* p_curr_scb;  /* SCB associated with SCO connection */
271   tBTA_AG_SCB* p_xfer_scb;  /* SCB associated with SCO transfer */
272   uint16_t cur_idx;         /* SCO handle */
273   tBTA_AG_SCO state;        /* SCO state variable */
274   bool is_local;            /* SCO connection initiated locally or remotely */
275 } tBTA_AG_SCO_CB;
276 
277 /* type for AG control block */
278 typedef struct {
279   tBTA_AG_SCB scb[BTA_AG_MAX_NUM_CLIENTS]; /* service control blocks */
280   tBTA_AG_PROFILE profile[BTA_AG_NUM_IDX]; /* profile-specific data */
281   tBTA_AG_SCO_CB sco;                      /* SCO data */
282   tBTA_AG_CBACK* p_cback;                  /* application callback */
283 } tBTA_AG_CB;
284 
285 /*****************************************************************************
286  *  Global data
287  ****************************************************************************/
288 
289 /* constant lookup tables */
290 extern const uint16_t bta_ag_uuid[BTA_AG_NUM_IDX];
291 extern const uint8_t bta_ag_sec_id[BTA_AG_NUM_IDX];
292 extern const tBTA_AG_AT_CMD* bta_ag_at_tbl[BTA_AG_NUM_IDX];
293 
294 /* control block declaration */
295 extern tBTA_AG_CB bta_ag_cb;
296 
297 /* config struct */
298 extern const tBTA_AG_CFG* p_bta_ag_cfg;
299 extern const tBTA_AG_HF_IND bta_ag_local_hf_ind_cfg[];
300 
301 /*****************************************************************************
302  *  Function prototypes
303  ****************************************************************************/
304 bool bta_ag_hdl_event(BT_HDR_RIGID* p_msg);
305 
306 /* API functions */
307 extern void bta_ag_api_enable(tBTA_AG_CBACK* p_cback);
308 extern void bta_ag_api_disable();
309 extern void bta_ag_api_set_active_device(const RawAddress& new_active_device);
310 extern void bta_ag_api_register(tBTA_SERVICE_MASK services,
311                                 tBTA_AG_FEAT features,
312                                 const std::vector<std::string>& service_names,
313                                 uint8_t app_id);
314 extern void bta_ag_api_result(uint16_t handle, tBTA_AG_RES result,
315                               const tBTA_AG_RES_DATA& result_data);
316 
317 /* main functions */
318 extern void bta_ag_scb_dealloc(tBTA_AG_SCB* p_scb);
319 extern uint16_t bta_ag_scb_to_idx(tBTA_AG_SCB* p_scb);
320 extern tBTA_AG_SCB* bta_ag_scb_by_idx(uint16_t idx);
321 extern uint8_t bta_ag_service_to_idx(tBTA_SERVICE_MASK services);
322 extern uint16_t bta_ag_idx_by_bdaddr(const RawAddress* peer_addr);
323 extern bool bta_ag_other_scb_open(tBTA_AG_SCB* p_curr_scb);
324 extern bool bta_ag_scb_open(tBTA_AG_SCB* p_curr_scb);
325 extern void bta_ag_sm_execute(tBTA_AG_SCB* p_scb, uint16_t event,
326                               const tBTA_AG_DATA& data);
327 extern void bta_ag_sm_execute_by_handle(uint16_t handle, uint16_t event,
328                                         const tBTA_AG_DATA& data);
329 extern void bta_ag_collision_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
330                                    uint8_t app_id, const RawAddress& peer_addr);
331 extern void bta_ag_resume_open(tBTA_AG_SCB* p_scb);
332 
333 /* SDP functions */
334 extern bool bta_ag_add_record(uint16_t service_uuid, const char* p_service_name,
335                               uint8_t scn, tBTA_AG_FEAT features,
336                               uint32_t sdp_handle);
337 extern void bta_ag_create_records(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
338 extern void bta_ag_del_records(tBTA_AG_SCB* p_scb);
339 extern bool bta_ag_sdp_find_attr(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service);
340 extern void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service);
341 extern void bta_ag_free_db(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
342 
343 /* RFCOMM functions */
344 extern void bta_ag_start_servers(tBTA_AG_SCB* p_scb,
345                                  tBTA_SERVICE_MASK services);
346 extern void bta_ag_close_servers(tBTA_AG_SCB* p_scb,
347                                  tBTA_SERVICE_MASK services);
348 extern bool bta_ag_is_server_closed(tBTA_AG_SCB* p_scb);
349 extern void bta_ag_rfc_do_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
350 extern void bta_ag_rfc_do_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
351 
352 /* SCO functions */
353 extern bool bta_ag_sco_is_active_device(const RawAddress& bd_addr);
354 extern bool bta_ag_sco_is_open(tBTA_AG_SCB* p_scb);
355 extern bool bta_ag_sco_is_opening(tBTA_AG_SCB* p_scb);
356 extern void bta_ag_sco_conn_rsp(tBTA_AG_SCB* p_scb,
357                                 tBTM_ESCO_CONN_REQ_EVT_DATA* data);
358 
359 /* AT command functions */
360 extern void bta_ag_at_hsp_cback(tBTA_AG_SCB* p_scb, uint16_t cmd,
361                                 uint8_t arg_type, char* p_arg, char* p_end,
362                                 int16_t int_arg);
363 extern void bta_ag_at_hfp_cback(tBTA_AG_SCB* p_scb, uint16_t cmd,
364                                 uint8_t arg_type, char* p_arg, char* p_end,
365                                 int16_t int_arg);
366 extern void bta_ag_at_err_cback(tBTA_AG_SCB* p_scb, bool unknown,
367                                 const char* p_arg);
368 extern bool bta_ag_inband_enabled(tBTA_AG_SCB* p_scb);
369 extern void bta_ag_send_call_inds(tBTA_AG_SCB* p_scb, tBTA_AG_RES result);
370 
371 /* Action functions */
372 extern void bta_ag_register(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
373 extern void bta_ag_deregister(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
374 extern void bta_ag_start_dereg(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
375 extern void bta_ag_start_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
376 extern void bta_ag_start_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
377 extern void bta_ag_disc_int_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
378 extern void bta_ag_disc_acp_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
379 extern void bta_ag_disc_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
380 extern void bta_ag_open_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
381 extern void bta_ag_rfc_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
382 extern void bta_ag_rfc_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
383 extern void bta_ag_rfc_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
384 extern void bta_ag_rfc_acp_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
385 extern void bta_ag_rfc_data(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
386 extern void bta_ag_sco_listen(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
387 extern void bta_ag_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
388 extern void bta_ag_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
389 extern void bta_ag_sco_shutdown(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
390 extern void bta_ag_sco_conn_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
391 extern void bta_ag_sco_conn_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
392 extern void bta_ag_post_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
393 extern void bta_ag_post_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
394 extern void bta_ag_svc_conn_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
395 extern void bta_ag_result(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
396 extern void bta_ag_setcodec(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
397 extern void bta_ag_send_ring(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
398 extern void bta_ag_handle_collision(tBTA_AG_SCB* p_scb,
399                                     const tBTA_AG_DATA& data);
400 
401 /* Internal utility functions */
402 extern void bta_ag_sco_codec_nego(tBTA_AG_SCB* p_scb, bool result);
403 extern void bta_ag_codec_negotiate(tBTA_AG_SCB* p_scb);
404 extern void bta_ag_send_bcs(tBTA_AG_SCB* p_scb);
405 extern void bta_ag_set_sco_allowed(bool value);
406 extern const RawAddress& bta_ag_get_active_device();
407 extern void bta_clear_active_device();
408 
409 #endif /* BTA_AG_INT_H */
410