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