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