1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright (C) 2003-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 #include "bta/bta_sys.h"
21 #include "bta/bta_api.h"
22 #include "bta/bta_hf_client_api.h"
23 #include "bta_hf_client_at.h"
24 
25 #if (BTA_HF_INCLUDED == TRUE)
26 /*****************************************************************************
27 **  Constants
28 *****************************************************************************/
29 #define HFP_VERSION_1_1         0x0101
30 #define HFP_VERSION_1_5         0x0105
31 #define HFP_VERSION_1_6         0x0106
32 #define HFP_VERSION_1_7         0x0107
33 
34 /* RFCOMM MTU SIZE */
35 #define BTA_HF_CLIENT_MTU       256
36 
37 /* profile role for connection */
38 #define BTA_HF_CLIENT_ACP       0       /* accepted connection */
39 #define BTA_HF_CLIENT_INT       1       /* initiating connection */
40 
41 /* Timer to wait for retry in case of collision */
42 #ifndef BTA_HF_CLIENT_COLLISION_TIMER
43 #define BTA_HF_CLIENT_COLLISION_TIMER  2411
44 #endif
45 
46 enum {
47     /* these events are handled by the state machine */
48     BTA_HF_CLIENT_API_REGISTER_EVT = BTA_SYS_EVT_START(BTA_ID_HS),
49     BTA_HF_CLIENT_API_DEREGISTER_EVT,
50     BTA_HF_CLIENT_API_OPEN_EVT,
51     BTA_HF_CLIENT_API_CLOSE_EVT,
52     BTA_HF_CLIENT_API_AUDIO_OPEN_EVT,
53     BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT,
54     BTA_HF_CLIENT_RFC_OPEN_EVT,
55     BTA_HF_CLIENT_RFC_CLOSE_EVT,
56     BTA_HF_CLIENT_RFC_SRV_CLOSE_EVT,
57     BTA_HF_CLIENT_RFC_DATA_EVT,
58     BTA_HF_CLIENT_DISC_ACP_RES_EVT,
59     BTA_HF_CLIENT_DISC_INT_RES_EVT,
60     BTA_HF_CLIENT_DISC_OK_EVT,
61     BTA_HF_CLIENT_DISC_FAIL_EVT,
62     BTA_HF_CLIENT_SCO_OPEN_EVT,
63     BTA_HF_CLIENT_SCO_CLOSE_EVT,
64     BTA_HF_CLIENT_SEND_AT_CMD_EVT,
65 #if (BTM_SCO_HCI_INCLUDED == TRUE )
66     BTA_HF_CLIENT_CI_SCO_DATA_EVT,
67 #endif /* (BTM_SCO_HCI_INCLUDED == TRUE ) */
68     BTA_HF_CLIENT_MAX_EVT,
69 
70     /* these events are handled outside of the state machine */
71     BTA_HF_CLIENT_API_ENABLE_EVT,
72     BTA_HF_CLIENT_API_DISABLE_EVT
73 };
74 
75 /*****************************************************************************
76 **  Data types
77 *****************************************************************************/
78 
79 /* data type for BTA_HF_CLIENT_API_ENABLE_EVT */
80 typedef struct {
81     BT_HDR                     hdr;
82     tBTA_HF_CLIENT_CBACK      *p_cback;
83 } tBTA_HF_CLIENT_API_ENABLE;
84 
85 /* data type for BTA_HF_CLIENT_API_REGISTER_EVT */
86 typedef struct {
87     BT_HDR                     hdr;
88     tBTA_HF_CLIENT_CBACK      *p_cback;
89     tBTA_SEC                   sec_mask;
90     tBTA_HF_CLIENT_FEAT        features;
91     char                       name[BTA_SERVICE_NAME_LEN + 1];
92 } tBTA_HF_CLIENT_API_REGISTER;
93 
94 /* data type for BTA_HF_CLIENT_API_OPEN_EVT */
95 typedef struct {
96     BT_HDR              hdr;
97     BD_ADDR             bd_addr;
98     tBTA_SEC            sec_mask;
99 } tBTA_HF_CLIENT_API_OPEN;
100 
101 /* data type for BTA_HF_CLIENT_DISC_RESULT_EVT */
102 typedef struct {
103     BT_HDR          hdr;
104     UINT16          status;
105 } tBTA_HF_CLIENT_DISC_RESULT;
106 
107 /* data type for RFCOMM events */
108 typedef struct {
109     BT_HDR          hdr;
110     UINT16          port_handle;
111 } tBTA_HF_CLIENT_RFC;
112 
113 /* generic purpose data type for other events */
114 typedef struct {
115     BT_HDR          hdr;
116     BOOLEAN         bool_val;
117     UINT8           uint8_val;
118     UINT32          uint32_val1;
119     UINT32          uint32_val2;
120     char            str[BTA_HF_CLIENT_NUMBER_LEN + 1];
121 } tBTA_HF_CLIENT_DATA_VAL;
122 
123 /* union of all event datatypes */
124 typedef union {
125     BT_HDR                         hdr;
126     tBTA_HF_CLIENT_API_ENABLE      api_enable;
127     tBTA_HF_CLIENT_API_REGISTER    api_register;
128     tBTA_HF_CLIENT_API_OPEN        api_open;
129     tBTA_HF_CLIENT_DISC_RESULT     disc_result;
130     tBTA_HF_CLIENT_RFC             rfc;
131     tBTA_HF_CLIENT_DATA_VAL        val;
132 
133 } tBTA_HF_CLIENT_DATA;
134 
135 /* type for each service control block */
136 typedef struct {
137     UINT16              serv_handle;    /* RFCOMM server handle */
138     BD_ADDR             peer_addr;      /* peer bd address */
139     tSDP_DISCOVERY_DB   *p_disc_db;     /* pointer to discovery database */
140     UINT16              conn_handle;    /* RFCOMM handle of connected service */
141     tBTA_SEC            serv_sec_mask;  /* server security mask */
142     tBTA_SEC            cli_sec_mask;   /* client security mask */
143     tBTA_HF_CLIENT_FEAT        features;       /* features registered by application */
144     tBTA_HF_CLIENT_PEER_FEAT   peer_features;  /* peer device features */
145     tBTA_HF_CLIENT_CHLD_FEAT   chld_features;  /* call handling features */
146     UINT16              peer_version;   /* profile version of peer device */
147     UINT8               peer_scn;       /* peer scn */
148     UINT8               role;           /* initiator/acceptor role */
149     UINT16              sco_idx;        /* SCO handle */
150     UINT8               sco_state;      /* SCO state variable */
151     BOOLEAN             sco_close_rfc;   /* TRUE if also close RFCOMM after SCO */
152     BOOLEAN             retry_with_sco_only;
153     BOOLEAN             deregister;     /* TRUE if service shutting down */
154     BOOLEAN             svc_conn;       /* set to TRUE when service level connection is up */
155     BOOLEAN             send_at_reply;  /* set to TRUE to notify framework about AT results */
156     tBTA_HF_CLIENT_AT_CB at_cb;         /* AT Parser control block */
157     UINT8               state;          /* state machine state */
158     tBTM_SCO_CODEC_TYPE negotiated_codec; /* negotiated codec */
159     TIMER_LIST_ENT      colli_timer;    /* Collision timer */
160     BOOLEAN             colli_tmr_on;   /* TRUE if collision timer is active */
161 
162     UINT16              in_pkt_len;
163     UINT16              out_pkt_len;
164     UINT8               link_type;      /* BTM_LINK_TYPE_SCO or BTM_LINK_TYPE_ESCO */
165     UINT8               tx_interval;
166     UINT8               retrans_window;
167     UINT8               air_mode;
168 } tBTA_HF_CLIENT_SCB;
169 
170 /* sco states */
171 enum {
172     BTA_HF_CLIENT_SCO_SHUTDOWN_ST,  /* no listening, no connection */
173     BTA_HF_CLIENT_SCO_LISTEN_ST,    /* listening */
174     BTA_HF_CLIENT_SCO_OPENING_ST,   /* connection opening */
175     BTA_HF_CLIENT_SCO_OPEN_CL_ST,   /* opening connection being closed */
176     BTA_HF_CLIENT_SCO_OPEN_ST,      /* open */
177     BTA_HF_CLIENT_SCO_CLOSING_ST,   /* closing */
178     BTA_HF_CLIENT_SCO_CLOSE_OP_ST,  /* closing sco being opened */
179     BTA_HF_CLIENT_SCO_SHUTTING_ST   /* sco shutting down */
180 };
181 
182 /* type for AG control block */
183 typedef struct {
184     tBTA_HF_CLIENT_SCB         scb;             /* service control block */
185     UINT32                     sdp_handle;
186     UINT8                      scn;
187     tBTA_HF_CLIENT_CBACK       *p_cback;        /* application callback */
188     BOOLEAN                    msbc_enabled;
189 } tBTA_HF_CLIENT_CB;
190 
191 /*****************************************************************************
192 **  Global data
193 *****************************************************************************/
194 
195 /* control block declaration */
196 #if BTA_DYNAMIC_MEMORY == FALSE
197 extern tBTA_HF_CLIENT_CB bta_hf_client_cb;
198 #else
199 extern tBTA_HF_CLIENT_CB *bta_hf_client_cb_ptr;
200 #define bta_hf_client_cb (*bta_hf_client_cb_ptr)
201 #endif
202 
203 /*****************************************************************************
204 **  Function prototypes
205 *****************************************************************************/
206 
207 /* main functions */
208 extern void bta_hf_client_scb_init(void);
209 extern void bta_hf_client_scb_disable(void);
210 extern BOOLEAN bta_hf_client_hdl_event(BT_HDR *p_msg);
211 extern void bta_hf_client_sm_execute(UINT16 event,
212                                      tBTA_HF_CLIENT_DATA *p_data);
213 extern void bta_hf_client_slc_seq(BOOLEAN error);
214 extern void bta_hf_client_collision_cback (tBTA_SYS_CONN_STATUS status, UINT8 id,
215         UINT8 app_id, BD_ADDR peer_addr);
216 extern void bta_hf_client_resume_open (void);
217 
218 /* SDP functions */
219 extern BOOLEAN bta_hf_client_add_record(char *p_service_name,
220                                         UINT8 scn, tBTA_HF_CLIENT_FEAT features,
221                                         UINT32 sdp_handle);
222 extern void bta_hf_client_create_record(tBTA_HF_CLIENT_DATA *p_data);
223 extern void bta_hf_client_del_record(tBTA_HF_CLIENT_DATA *p_data);
224 extern BOOLEAN bta_hf_client_sdp_find_attr(void);
225 extern void bta_hf_client_do_disc(void);
226 extern void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA *p_data);
227 
228 /* RFCOMM functions */
229 extern void bta_hf_client_setup_port(UINT16 handle);
230 extern void bta_hf_client_start_server(void);
231 extern void bta_hf_client_close_server(void);
232 extern void bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA *p_data);
233 extern void bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA *p_data);
234 
235 /* SCO functions */
236 extern void bta_hf_client_sco_listen(tBTA_HF_CLIENT_DATA *p_data);
237 extern void bta_hf_client_sco_shutdown(tBTA_HF_CLIENT_DATA *p_data);
238 extern void bta_hf_client_sco_conn_open(tBTA_HF_CLIENT_DATA *p_data);
239 extern void bta_hf_client_sco_conn_close(tBTA_HF_CLIENT_DATA *p_data);
240 extern void bta_hf_client_sco_open(tBTA_HF_CLIENT_DATA *p_data);
241 extern void bta_hf_client_sco_close(tBTA_HF_CLIENT_DATA *p_data);
242 extern void bta_hf_client_cback_sco(UINT8 event);
243 
244 /* AT command functions */
245 extern void bta_hf_client_at_parse(char *buf, unsigned int len);
246 extern void bta_hf_client_send_at_brsf(void);
247 extern void bta_hf_client_send_at_bac(void);
248 extern void bta_hf_client_send_at_cind(BOOLEAN status);
249 extern void bta_hf_client_send_at_cmer(BOOLEAN activate);
250 extern void bta_hf_client_send_at_chld(char cmd, UINT32 idx);
251 extern void bta_hf_client_send_at_clip(BOOLEAN activate);
252 extern void bta_hf_client_send_at_ccwa(BOOLEAN activate);
253 extern void bta_hf_client_send_at_cmee(BOOLEAN activate);
254 extern void bta_hf_client_send_at_cops(BOOLEAN query);
255 extern void bta_hf_client_send_at_clcc(void);
256 extern void bta_hf_client_send_at_bvra(BOOLEAN enable);
257 extern void bta_hf_client_send_at_vgs(UINT32 volume);
258 extern void bta_hf_client_send_at_vgm(UINT32 volume);
259 extern void bta_hf_client_send_at_atd(char *number, UINT32 memory);
260 extern void bta_hf_client_send_at_bldn(void);
261 extern void bta_hf_client_send_at_ata(void);
262 extern void bta_hf_client_send_at_chup(void);
263 extern void bta_hf_client_send_at_btrh(BOOLEAN query, UINT32 val);
264 extern void bta_hf_client_send_at_vts(char code);
265 extern void bta_hf_client_send_at_bcc(void);
266 extern void bta_hf_client_send_at_bcs(UINT32 codec);
267 extern void bta_hf_client_send_at_cnum(void);
268 extern void bta_hf_client_send_at_nrec(void);
269 extern void bta_hf_client_send_at_binp(UINT32 action);
270 extern void bta_hf_client_send_at_bia(void);
271 
272 /* Action functions */
273 extern void bta_hf_client_register(tBTA_HF_CLIENT_DATA *p_data);
274 extern void bta_hf_client_deregister(tBTA_HF_CLIENT_DATA *p_data);
275 extern void bta_hf_client_start_dereg(tBTA_HF_CLIENT_DATA *p_data);
276 extern void bta_hf_client_start_close(tBTA_HF_CLIENT_DATA *p_data);
277 extern void bta_hf_client_start_open(tBTA_HF_CLIENT_DATA *p_data);
278 extern void bta_hf_client_rfc_acp_open(tBTA_HF_CLIENT_DATA *p_data);
279 extern void bta_hf_client_rfc_open(tBTA_HF_CLIENT_DATA *p_data);
280 extern void bta_hf_client_rfc_fail(tBTA_HF_CLIENT_DATA *p_data);
281 extern void bta_hf_client_disc_fail(tBTA_HF_CLIENT_DATA *p_data);
282 extern void bta_hf_client_open_fail(tBTA_HF_CLIENT_DATA *p_data);
283 extern void bta_hf_client_rfc_close(tBTA_HF_CLIENT_DATA *p_data);
284 extern void bta_hf_client_disc_acp_res(tBTA_HF_CLIENT_DATA *p_data);
285 extern void bta_hf_client_rfc_data(tBTA_HF_CLIENT_DATA *p_data);
286 extern void bta_hf_client_disc_int_res(tBTA_HF_CLIENT_DATA *p_data);
287 extern void bta_hf_client_svc_conn_open(tBTA_HF_CLIENT_DATA *p_data);
288 extern void bta_hf_client_ind(tBTA_HF_CLIENT_IND_TYPE type, UINT16 value);
289 extern void bta_hf_client_evt_val(tBTA_HF_CLIENT_EVT type, UINT16 value);
290 extern void bta_hf_client_operator_name(char *name);
291 extern void bta_hf_client_clip(char *number);
292 extern void bta_hf_client_ccwa(char *number);
293 extern void bta_hf_client_at_result(tBTA_HF_CLIENT_AT_RESULT_TYPE type, UINT16 cme);
294 extern void bta_hf_client_clcc(UINT32 idx, BOOLEAN incoming, UINT8 status, BOOLEAN mpty, char *number);
295 extern void bta_hf_client_cnum(char *number, UINT16 service);
296 extern void bta_hf_client_binp(char *number);
297 
298 /* Commands handling functions */
299 extern void bta_hf_client_dial(tBTA_HF_CLIENT_DATA *p_data);
300 extern void bta_hf_client_send_at_cmd(tBTA_HF_CLIENT_DATA *p_data);
301 #if (BTM_SCO_HCI_INCLUDED == TRUE )
302 extern void bta_hf_client_ci_sco_data(tBTA_HF_CLIENT_DATA *p_data);
303 #endif /* (BTM_SCO_HCI_INCLUDED == TRUE ) */
304 #endif /* #if (BTA_HF_INCLUDED == TRUE) */
305