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