1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright 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_api.h"
21 #include "bta_hf_client_api.h"
22 #include "bta_hf_client_at.h"
23 #include "bta_sys.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 #define HFP_VERSION_1_7 0x0107
32 
33 /* RFCOMM MTU SIZE */
34 #define BTA_HF_CLIENT_MTU 256
35 
36 /* profile role for connection */
37 #define BTA_HF_CLIENT_ACP 0 /* accepted connection */
38 #define BTA_HF_CLIENT_INT 1 /* initiating connection */
39 
40 /* Time (in milliseconds) to wait for retry in case of collision */
41 #ifndef BTA_HF_CLIENT_COLLISION_TIMER_MS
42 #define BTA_HF_CLIENT_COLLISION_TIMER_MS 2411
43 #endif
44 
45 /* Maximum number of HF devices supported simultaneously */
46 #define HF_CLIENT_MAX_DEVICES 10
47 
48 enum {
49   /* these events are handled by the state machine */
50   BTA_HF_CLIENT_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_HS),
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   BTA_HF_CLIENT_MAX_EVT,
66 
67   /* these events are handled outside of the state machine */
68   BTA_HF_CLIENT_API_ENABLE_EVT,
69   BTA_HF_CLIENT_API_DISABLE_EVT
70 };
71 
72 /* AT Command enum */
73 enum {
74   BTA_HF_CLIENT_AT_NONE,
75   BTA_HF_CLIENT_AT_BRSF,
76   BTA_HF_CLIENT_AT_BAC,
77   BTA_HF_CLIENT_AT_CIND,
78   BTA_HF_CLIENT_AT_CIND_STATUS,
79   BTA_HF_CLIENT_AT_CMER,
80   BTA_HF_CLIENT_AT_CHLD,
81   BTA_HF_CLIENT_AT_CMEE,
82   BTA_HF_CLIENT_AT_BIA,
83   BTA_HF_CLIENT_AT_CLIP,
84   BTA_HF_CLIENT_AT_CCWA,
85   BTA_HF_CLIENT_AT_COPS,
86   BTA_HF_CLIENT_AT_CLCC,
87   BTA_HF_CLIENT_AT_BVRA,
88   BTA_HF_CLIENT_AT_VGS,
89   BTA_HF_CLIENT_AT_VGM,
90   BTA_HF_CLIENT_AT_ATD,
91   BTA_HF_CLIENT_AT_BLDN,
92   BTA_HF_CLIENT_AT_ATA,
93   BTA_HF_CLIENT_AT_CHUP,
94   BTA_HF_CLIENT_AT_BTRH,
95   BTA_HF_CLIENT_AT_VTS,
96   BTA_HF_CLIENT_AT_BCC,
97   BTA_HF_CLIENT_AT_BCS,
98   BTA_HF_CLIENT_AT_CNUM,
99   BTA_HF_CLIENT_AT_NREC,
100   BTA_HF_CLIENT_AT_BINP,
101   BTA_HF_CLIENT_AT_VENDOR_SPECIFIC,
102 };
103 
104 /*****************************************************************************
105  *  Data types
106  ****************************************************************************/
107 /* data type for BTA_HF_CLIENT_API_OPEN_EVT */
108 typedef struct {
109   BT_HDR hdr;
110   RawAddress bd_addr;
111   uint16_t* handle;
112   tBTA_SEC sec_mask;
113 } tBTA_HF_CLIENT_API_OPEN;
114 
115 /* data type for BTA_HF_CLIENT_DISC_RESULT_EVT */
116 typedef struct {
117   BT_HDR hdr;
118   uint16_t status;
119 } tBTA_HF_CLIENT_DISC_RESULT;
120 
121 /* data type for RFCOMM events */
122 typedef struct {
123   BT_HDR hdr;
124   uint16_t port_handle;
125 } tBTA_HF_CLIENT_RFC;
126 
127 /* generic purpose data type for other events */
128 typedef struct {
129   BT_HDR hdr;
130   bool bool_val;
131   uint8_t uint8_val;
132   uint32_t uint32_val1;
133   uint32_t uint32_val2;
134   char str[BTA_HF_CLIENT_NUMBER_LEN + 1];
135 } tBTA_HF_CLIENT_DATA_VAL;
136 
137 /* union of all event datatypes */
138 typedef union {
139   BT_HDR hdr;
140   tBTA_HF_CLIENT_API_OPEN api_open;
141   tBTA_HF_CLIENT_DISC_RESULT disc_result;
142   tBTA_HF_CLIENT_RFC rfc;
143   tBTA_HF_CLIENT_DATA_VAL val;
144 
145 } tBTA_HF_CLIENT_DATA;
146 
147 /* First handle for the control block */
148 #define BTA_HF_CLIENT_CB_FIRST_HANDLE 1
149 
150 /* sco states */
151 enum {
152   BTA_HF_CLIENT_SCO_SHUTDOWN_ST, /* no listening, no connection */
153   BTA_HF_CLIENT_SCO_LISTEN_ST,   /* listening */
154   BTA_HF_CLIENT_SCO_OPENING_ST,  /* connection opening */
155   BTA_HF_CLIENT_SCO_OPEN_CL_ST,  /* opening connection being closed */
156   BTA_HF_CLIENT_SCO_OPEN_ST,     /* open */
157   BTA_HF_CLIENT_SCO_CLOSING_ST,  /* closing */
158   BTA_HF_CLIENT_SCO_CLOSE_OP_ST, /* closing sco being opened */
159   BTA_HF_CLIENT_SCO_SHUTTING_ST  /* sco shutting down */
160 };
161 
162 /* type for HF control block */
163 typedef struct {
164   // Fields useful for particular control block.
165   uint8_t handle;               /* Handle of the control block to be
166                                    used by upper layer */
167   RawAddress peer_addr;         /* peer bd address */
168   tSDP_DISCOVERY_DB* p_disc_db; /* pointer to discovery database */
169   uint16_t conn_handle;         /* RFCOMM handle of connected service */
170   tBTA_SEC cli_sec_mask;        /* client security mask */
171   tBTA_HF_CLIENT_PEER_FEAT peer_features; /* peer device features */
172   tBTA_HF_CLIENT_CHLD_FEAT chld_features; /* call handling features */
173   uint16_t peer_version;                  /* profile version of peer device */
174   uint8_t peer_scn;                       /* peer scn */
175   uint8_t role;                           /* initiator/acceptor role */
176   uint16_t sco_idx;                       /* SCO handle */
177   uint8_t sco_state;                      /* SCO state variable */
178   bool sco_close_rfc; /* true if also close RFCOMM after SCO */
179   tBTM_SCO_CODEC_TYPE negotiated_codec; /* negotiated codec */
180   bool svc_conn;      /* set to true when service level connection is up */
181   bool send_at_reply; /* set to true to notify framework about AT results */
182   tBTA_HF_CLIENT_AT_CB at_cb;           /* AT Parser control block */
183   uint8_t state;                        /* state machine state */
184   bool is_allocated; /* if the control block is already allocated */
185   alarm_t* collision_timer;             /* Collision timer */
186 } tBTA_HF_CLIENT_CB;
187 
188 typedef struct {
189   // Common fields, should be taken out.
190   uint32_t sdp_handle;
191   uint8_t scn;
192   tBTA_HF_CLIENT_CBACK* p_cback; /* application callback */
193   tBTA_HF_CLIENT_FEAT features;  /* features registered by application */
194   tBTA_SEC serv_sec_mask;        /* server security mask */
195   uint16_t serv_handle;          /* RFCOMM server handle */
196   bool deregister;               /* true if service shutting down */
197 
198   // Maximum number of control blocks supported by the BTA layer.
199   tBTA_HF_CLIENT_CB cb[HF_CLIENT_MAX_DEVICES];
200 } tBTA_HF_CLIENT_CB_ARR;
201 
202 extern tBTA_HF_CLIENT_CB_ARR bta_hf_client_cb_arr;
203 
204 /*****************************************************************************
205  *  Function prototypes
206  ****************************************************************************/
207 
208 /* main functions */
209 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle);
210 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(
211     const RawAddress& bd_addr);
212 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_rfc_handle(uint16_t handle);
213 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle);
214 extern bool bta_hf_client_hdl_event(BT_HDR* p_msg);
215 extern void bta_hf_client_sm_execute(uint16_t event,
216                                      tBTA_HF_CLIENT_DATA* p_data);
217 extern void bta_hf_client_slc_seq(tBTA_HF_CLIENT_CB* client_cb, bool error);
218 extern bool bta_hf_client_allocate_handle(const RawAddress& bd_addr,
219                                           uint16_t* p_handle);
220 extern void bta_hf_client_app_callback(uint16_t event, tBTA_HF_CLIENT* data);
221 extern void bta_hf_client_collision_cback(tBTA_SYS_CONN_STATUS status,
222                                           uint8_t id, uint8_t app_id,
223                                           const RawAddress& peer_addr);
224 extern void bta_hf_client_resume_open(tBTA_HF_CLIENT_CB* client_cb);
225 extern tBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback,
226                                             tBTA_SEC sec_mask,
227                                             tBTA_HF_CLIENT_FEAT features,
228                                             const char* p_service_name);
229 
230 extern void bta_hf_client_api_disable(void);
231 extern void bta_hf_client_dump_statistics(int fd);
232 extern void bta_hf_client_cb_arr_init(void);
233 
234 /* SDP functions */
235 extern bool bta_hf_client_add_record(char* p_service_name, uint8_t scn,
236                                      tBTA_HF_CLIENT_FEAT features,
237                                      uint32_t sdp_handle);
238 extern void bta_hf_client_create_record(tBTA_HF_CLIENT_CB_ARR* client_cb,
239                                         const char* p_data);
240 extern void bta_hf_client_del_record(tBTA_HF_CLIENT_CB_ARR* client_cb);
241 extern bool bta_hf_client_sdp_find_attr(tBTA_HF_CLIENT_CB* client_cb);
242 extern void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb);
243 extern void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA* p_data);
244 
245 /* RFCOMM functions */
246 extern void bta_hf_client_setup_port(uint16_t handle);
247 extern void bta_hf_client_start_server();
248 extern void bta_hf_client_close_server();
249 extern void bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA* p_data);
250 extern void bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA* p_data);
251 
252 /* SCO functions */
253 extern void bta_hf_client_sco_listen(tBTA_HF_CLIENT_DATA* p_data);
254 extern void bta_hf_client_sco_conn_open(tBTA_HF_CLIENT_DATA* p_data);
255 extern void bta_hf_client_sco_conn_close(tBTA_HF_CLIENT_DATA* p_data);
256 extern void bta_hf_client_sco_open(tBTA_HF_CLIENT_DATA* p_data);
257 extern void bta_hf_client_sco_close(tBTA_HF_CLIENT_DATA* p_data);
258 extern void bta_hf_client_sco_shutdown(tBTA_HF_CLIENT_CB* client_cb);
259 extern void bta_hf_client_cback_sco(tBTA_HF_CLIENT_CB* client_cb,
260                                     uint8_t event);
261 
262 /* AT command functions */
263 extern void bta_hf_client_at_parse(tBTA_HF_CLIENT_CB* client_cb, char* buf,
264                                    unsigned int len);
265 extern void bta_hf_client_send_at_brsf(tBTA_HF_CLIENT_CB* client_cb,
266                                        tBTA_HF_CLIENT_FEAT features);
267 extern void bta_hf_client_send_at_bac(tBTA_HF_CLIENT_CB* client_cb);
268 extern void bta_hf_client_send_at_cind(tBTA_HF_CLIENT_CB* client_cb,
269                                        bool status);
270 extern void bta_hf_client_send_at_cmer(tBTA_HF_CLIENT_CB* client_cb,
271                                        bool activate);
272 extern void bta_hf_client_send_at_chld(tBTA_HF_CLIENT_CB* client_cb, char cmd,
273                                        uint32_t idx);
274 extern void bta_hf_client_send_at_clip(tBTA_HF_CLIENT_CB* client_cb,
275                                        bool activate);
276 extern void bta_hf_client_send_at_ccwa(tBTA_HF_CLIENT_CB* client_cb,
277                                        bool activate);
278 extern void bta_hf_client_send_at_cmee(tBTA_HF_CLIENT_CB* client_cb,
279                                        bool activate);
280 extern void bta_hf_client_send_at_cops(tBTA_HF_CLIENT_CB* client_cb,
281                                        bool query);
282 extern void bta_hf_client_send_at_clcc(tBTA_HF_CLIENT_CB* client_cb);
283 extern void bta_hf_client_send_at_bvra(tBTA_HF_CLIENT_CB* client_cb,
284                                        bool enable);
285 extern void bta_hf_client_send_at_vgs(tBTA_HF_CLIENT_CB* client_cb,
286                                       uint32_t volume);
287 extern void bta_hf_client_send_at_vgm(tBTA_HF_CLIENT_CB* client_cb,
288                                       uint32_t volume);
289 extern void bta_hf_client_send_at_atd(tBTA_HF_CLIENT_CB* client_cb,
290                                       char* number, uint32_t memory);
291 extern void bta_hf_client_send_at_bldn(tBTA_HF_CLIENT_CB* client_cb);
292 extern void bta_hf_client_send_at_ata(tBTA_HF_CLIENT_CB* client_cb);
293 extern void bta_hf_client_send_at_chup(tBTA_HF_CLIENT_CB* client_cb);
294 extern void bta_hf_client_send_at_btrh(tBTA_HF_CLIENT_CB* client_cb, bool query,
295                                        uint32_t val);
296 extern void bta_hf_client_send_at_vts(tBTA_HF_CLIENT_CB* client_cb, char code);
297 extern void bta_hf_client_send_at_bcc(tBTA_HF_CLIENT_CB* client_cb);
298 extern void bta_hf_client_send_at_bcs(tBTA_HF_CLIENT_CB* client_cb,
299                                       uint32_t codec);
300 extern void bta_hf_client_send_at_cnum(tBTA_HF_CLIENT_CB* client_cb);
301 extern void bta_hf_client_send_at_nrec(tBTA_HF_CLIENT_CB* client_cb);
302 extern void bta_hf_client_send_at_binp(tBTA_HF_CLIENT_CB* client_cb,
303                                        uint32_t action);
304 extern void bta_hf_client_send_at_bia(tBTA_HF_CLIENT_CB* client_cb);
305 
306 /* AT API Functions */
307 void bta_hf_client_at_init(tBTA_HF_CLIENT_CB* client_cb);
308 void bta_hf_client_at_reset(tBTA_HF_CLIENT_CB* client_cb);
309 extern void bta_hf_client_ind(tBTA_HF_CLIENT_CB* client_cb,
310                               tBTA_HF_CLIENT_IND_TYPE type, uint16_t value);
311 extern void bta_hf_client_evt_val(tBTA_HF_CLIENT_CB* client_cb,
312                                   tBTA_HF_CLIENT_EVT type, uint16_t value);
313 extern void bta_hf_client_operator_name(tBTA_HF_CLIENT_CB* client_name,
314                                         char* name);
315 extern void bta_hf_client_clip(tBTA_HF_CLIENT_CB* client_cb, char* number);
316 extern void bta_hf_client_ccwa(tBTA_HF_CLIENT_CB* client_cb, char* number);
317 extern void bta_hf_client_at_result(tBTA_HF_CLIENT_CB* client_cb,
318                                     tBTA_HF_CLIENT_AT_RESULT_TYPE type,
319                                     uint16_t cme);
320 extern void bta_hf_client_clcc(tBTA_HF_CLIENT_CB* client_cb, uint32_t idx,
321                                bool incoming, uint8_t status, bool mpty,
322                                char* number);
323 extern void bta_hf_client_cnum(tBTA_HF_CLIENT_CB* client_cb, char* number,
324                                uint16_t service);
325 extern void bta_hf_client_binp(tBTA_HF_CLIENT_CB* client_cb, char* number);
326 
327 /* Action functions */
328 extern void bta_hf_client_start_close(tBTA_HF_CLIENT_DATA* p_data);
329 extern void bta_hf_client_start_open(tBTA_HF_CLIENT_DATA* p_data);
330 extern void bta_hf_client_rfc_acp_open(tBTA_HF_CLIENT_DATA* p_data);
331 extern void bta_hf_client_rfc_open(tBTA_HF_CLIENT_DATA* p_data);
332 extern void bta_hf_client_rfc_fail(tBTA_HF_CLIENT_DATA* p_data);
333 extern void bta_hf_client_disc_fail(tBTA_HF_CLIENT_DATA* p_data);
334 extern void bta_hf_client_open_fail(tBTA_HF_CLIENT_DATA* p_data);
335 extern void bta_hf_client_rfc_close(tBTA_HF_CLIENT_DATA* p_data);
336 extern void bta_hf_client_disc_acp_res(tBTA_HF_CLIENT_DATA* p_data);
337 extern void bta_hf_client_rfc_data(tBTA_HF_CLIENT_DATA* p_data);
338 extern void bta_hf_client_disc_int_res(tBTA_HF_CLIENT_DATA* p_data);
339 extern void bta_hf_client_svc_conn_open(tBTA_HF_CLIENT_DATA* p_data);
340 
341 /* Commands handling functions */
342 extern void bta_hf_client_dial(tBTA_HF_CLIENT_DATA* p_data);
343 extern void bta_hf_client_send_at_cmd(tBTA_HF_CLIENT_DATA* p_data);
344