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