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