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