• 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 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle);
218 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(
219     const RawAddress& bd_addr);
220 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_rfc_handle(uint16_t handle);
221 extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle);
222 extern bool bta_hf_client_hdl_event(BT_HDR_RIGID* p_msg);
223 extern void bta_hf_client_sm_execute(uint16_t event,
224                                      tBTA_HF_CLIENT_DATA* p_data);
225 extern void bta_hf_client_slc_seq(tBTA_HF_CLIENT_CB* client_cb, bool error);
226 extern bool bta_hf_client_allocate_handle(const RawAddress& bd_addr,
227                                           uint16_t* p_handle);
228 extern void bta_hf_client_app_callback(uint16_t event, tBTA_HF_CLIENT* data);
229 extern void bta_hf_client_collision_cback(tBTA_SYS_CONN_STATUS status,
230                                           uint8_t id, uint8_t app_id,
231                                           const RawAddress& peer_addr);
232 extern void bta_hf_client_resume_open(tBTA_HF_CLIENT_CB* client_cb);
233 extern tBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback,
234                                             tBTA_HF_CLIENT_FEAT features,
235                                             const char* p_service_name);
236 
237 extern void bta_hf_client_api_disable(void);
238 extern void bta_hf_client_dump_statistics(int fd);
239 extern void bta_hf_client_cb_arr_init(void);
240 
241 /* SDP functions */
242 extern bool bta_hf_client_add_record(char* p_service_name, uint8_t scn,
243                                      tBTA_HF_CLIENT_FEAT features,
244                                      uint32_t sdp_handle);
245 extern void bta_hf_client_create_record(tBTA_HF_CLIENT_CB_ARR* client_cb,
246                                         const char* p_data);
247 extern void bta_hf_client_del_record(tBTA_HF_CLIENT_CB_ARR* client_cb);
248 extern bool bta_hf_client_sdp_find_attr(tBTA_HF_CLIENT_CB* client_cb);
249 extern void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb);
250 extern void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA* p_data);
251 
252 /* RFCOMM functions */
253 extern void bta_hf_client_setup_port(uint16_t handle);
254 extern void bta_hf_client_start_server();
255 extern void bta_hf_client_close_server();
256 extern void bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA* p_data);
257 extern void bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA* p_data);
258 
259 /* SCO functions */
260 extern void bta_hf_client_sco_listen(tBTA_HF_CLIENT_DATA* p_data);
261 extern void bta_hf_client_sco_conn_open(tBTA_HF_CLIENT_DATA* p_data);
262 extern void bta_hf_client_sco_conn_close(tBTA_HF_CLIENT_DATA* p_data);
263 extern void bta_hf_client_sco_open(tBTA_HF_CLIENT_DATA* p_data);
264 extern void bta_hf_client_sco_close(tBTA_HF_CLIENT_DATA* p_data);
265 extern void bta_hf_client_sco_shutdown(tBTA_HF_CLIENT_CB* client_cb);
266 extern void bta_hf_client_cback_sco(tBTA_HF_CLIENT_CB* client_cb,
267                                     uint8_t event);
268 
269 /* AT command functions */
270 extern void bta_hf_client_at_parse(tBTA_HF_CLIENT_CB* client_cb, char* buf,
271                                    unsigned int len);
272 extern void bta_hf_client_send_at_brsf(tBTA_HF_CLIENT_CB* client_cb,
273                                        tBTA_HF_CLIENT_FEAT features);
274 extern void bta_hf_client_send_at_bac(tBTA_HF_CLIENT_CB* client_cb);
275 extern void bta_hf_client_send_at_cind(tBTA_HF_CLIENT_CB* client_cb,
276                                        bool status);
277 extern void bta_hf_client_send_at_cmer(tBTA_HF_CLIENT_CB* client_cb,
278                                        bool activate);
279 extern void bta_hf_client_send_at_chld(tBTA_HF_CLIENT_CB* client_cb, char cmd,
280                                        uint32_t idx);
281 extern void bta_hf_client_send_at_bind(tBTA_HF_CLIENT_CB* client_cb, int step);
282 extern void bta_hf_client_send_at_biev(tBTA_HF_CLIENT_CB* client_cb, int ind_id,
283                                        int value);
284 extern void bta_hf_client_send_at_clip(tBTA_HF_CLIENT_CB* client_cb,
285                                        bool activate);
286 extern void bta_hf_client_send_at_ccwa(tBTA_HF_CLIENT_CB* client_cb,
287                                        bool activate);
288 extern void bta_hf_client_send_at_cmee(tBTA_HF_CLIENT_CB* client_cb,
289                                        bool activate);
290 extern void bta_hf_client_send_at_cops(tBTA_HF_CLIENT_CB* client_cb,
291                                        bool query);
292 extern void bta_hf_client_send_at_clcc(tBTA_HF_CLIENT_CB* client_cb);
293 extern void bta_hf_client_send_at_bvra(tBTA_HF_CLIENT_CB* client_cb,
294                                        bool enable);
295 extern void bta_hf_client_send_at_vgs(tBTA_HF_CLIENT_CB* client_cb,
296                                       uint32_t volume);
297 extern void bta_hf_client_send_at_vgm(tBTA_HF_CLIENT_CB* client_cb,
298                                       uint32_t volume);
299 extern void bta_hf_client_send_at_atd(tBTA_HF_CLIENT_CB* client_cb,
300                                       char* number, uint32_t memory);
301 extern void bta_hf_client_send_at_bldn(tBTA_HF_CLIENT_CB* client_cb);
302 extern void bta_hf_client_send_at_ata(tBTA_HF_CLIENT_CB* client_cb);
303 extern void bta_hf_client_send_at_chup(tBTA_HF_CLIENT_CB* client_cb);
304 extern void bta_hf_client_send_at_btrh(tBTA_HF_CLIENT_CB* client_cb, bool query,
305                                        uint32_t val);
306 extern void bta_hf_client_send_at_vts(tBTA_HF_CLIENT_CB* client_cb, char code);
307 extern void bta_hf_client_send_at_bcc(tBTA_HF_CLIENT_CB* client_cb);
308 extern void bta_hf_client_send_at_bcs(tBTA_HF_CLIENT_CB* client_cb,
309                                       uint32_t codec);
310 extern void bta_hf_client_send_at_cnum(tBTA_HF_CLIENT_CB* client_cb);
311 extern void bta_hf_client_send_at_nrec(tBTA_HF_CLIENT_CB* client_cb);
312 extern void bta_hf_client_send_at_binp(tBTA_HF_CLIENT_CB* client_cb,
313                                        uint32_t action);
314 extern void bta_hf_client_send_at_bia(tBTA_HF_CLIENT_CB* client_cb);
315 
316 /* AT API Functions */
317 void bta_hf_client_at_init(tBTA_HF_CLIENT_CB* client_cb);
318 void bta_hf_client_at_reset(tBTA_HF_CLIENT_CB* client_cb);
319 extern void bta_hf_client_ind(tBTA_HF_CLIENT_CB* client_cb,
320                               tBTA_HF_CLIENT_IND_TYPE type, uint16_t value);
321 extern void bta_hf_client_evt_val(tBTA_HF_CLIENT_CB* client_cb,
322                                   tBTA_HF_CLIENT_EVT type, uint16_t value);
323 extern void bta_hf_client_operator_name(tBTA_HF_CLIENT_CB* client_name,
324                                         char* name);
325 extern void bta_hf_client_clip(tBTA_HF_CLIENT_CB* client_cb, char* number);
326 extern void bta_hf_client_ccwa(tBTA_HF_CLIENT_CB* client_cb, char* number);
327 extern void bta_hf_client_at_result(tBTA_HF_CLIENT_CB* client_cb,
328                                     tBTA_HF_CLIENT_AT_RESULT_TYPE type,
329                                     uint16_t cme);
330 extern void bta_hf_client_clcc(tBTA_HF_CLIENT_CB* client_cb, uint32_t idx,
331                                bool incoming, uint8_t status, bool mpty,
332                                char* number);
333 extern void bta_hf_client_cnum(tBTA_HF_CLIENT_CB* client_cb, char* number,
334                                uint16_t service);
335 extern void bta_hf_client_binp(tBTA_HF_CLIENT_CB* client_cb, char* number);
336 
337 /* Action functions */
338 extern void bta_hf_client_start_close(tBTA_HF_CLIENT_DATA* p_data);
339 extern void bta_hf_client_start_open(tBTA_HF_CLIENT_DATA* p_data);
340 extern void bta_hf_client_rfc_acp_open(tBTA_HF_CLIENT_DATA* p_data);
341 extern void bta_hf_client_rfc_open(tBTA_HF_CLIENT_DATA* p_data);
342 extern void bta_hf_client_rfc_fail(tBTA_HF_CLIENT_DATA* p_data);
343 extern void bta_hf_client_disc_fail(tBTA_HF_CLIENT_DATA* p_data);
344 extern void bta_hf_client_open_fail(tBTA_HF_CLIENT_DATA* p_data);
345 extern void bta_hf_client_rfc_close(tBTA_HF_CLIENT_DATA* p_data);
346 extern void bta_hf_client_disc_acp_res(tBTA_HF_CLIENT_DATA* p_data);
347 extern void bta_hf_client_rfc_data(tBTA_HF_CLIENT_DATA* p_data);
348 extern void bta_hf_client_disc_int_res(tBTA_HF_CLIENT_DATA* p_data);
349 extern void bta_hf_client_svc_conn_open(tBTA_HF_CLIENT_DATA* p_data);
350 
351 /* Commands handling functions */
352 extern void bta_hf_client_dial(tBTA_HF_CLIENT_DATA* p_data);
353 extern void bta_hf_client_send_at_cmd(tBTA_HF_CLIENT_DATA* p_data);
354