1 /******************************************************************************
3 * Copyright 2005-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains BTA HID Host internal definitions
23 ******************************************************************************/
30 #include "bta/include/bta_api.h"
31 #include "bta/include/bta_gatt_api.h"
32 #include "bta/include/bta_hh_api.h"
33 #include "bta/sys/bta_sys.h"
34 #include "stack/include/bt_types.h"
36 /* can be moved to bta_api.h */
37 #define BTA_HH_MAX_RPT_CHARS 8
39 /* state machine events, these events are handled by the state machine */
41 BTA_HH_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_HH),
47 BTA_HH_INT_HANDSK_EVT,
49 BTA_HH_API_WRITE_DEV_EVT,
50 BTA_HH_API_GET_DSCP_EVT,
51 BTA_HH_API_MAINT_DEV_EVT,
53 BTA_HH_GATT_CLOSE_EVT,
57 BTA_HH_GATT_ENC_CMPL_EVT,
59 /* not handled by execute state machine */
60 BTA_HH_API_ENABLE_EVT,
61 BTA_HH_API_DISABLE_EVT,
64 typedef uint16_t tBTA_HH_INT_EVT; /* HID host internal events */
66 #define BTA_HH_INVALID_EVT (BTA_HH_DISC_CMPL_EVT + 1)
68 /* event used to map between BTE event and BTA event */
69 #define BTA_HH_FST_TRANS_CB_EVT BTA_HH_GET_RPT_EVT
70 #define BTA_HH_FST_BTE_TRANS_EVT HID_TRANS_GET_REPORT
72 /* sub event code used for device maintainence API call */
73 #define BTA_HH_ADD_DEV 0
74 #define BTA_HH_REMOVE_DEV 1
76 /* state machine states */
83 BTA_HH_INVALID_ST /* Used to check invalid states before executing SM function
87 typedef uint8_t tBTA_HH_STATE;
89 /* data structure used to send a command/data to HID device */
99 /* data type for BTA_HH_API_ENABLE_EVT */
102 uint8_t service_name[BTA_SERVICE_NAME_LEN + 1];
103 tBTA_HH_CBACK* p_cback;
104 } tBTA_HH_API_ENABLE;
109 tBTA_HH_PROTO_MODE mode;
112 /* internal event data from BTE HID callback */
118 } tBTA_HH_CBACK_DATA;
127 tBTA_HH_DEV_DSCP_INFO dscp_info;
133 tBTA_GATT_REASON reason; /* disconnect reason code, not useful when connect
142 } tBTA_HH_SCPP_UPDATE;
144 /* union of all event data types */
147 tBTA_HH_API_ENABLE api_enable;
148 tBTA_HH_API_CONN api_conn;
149 tBTA_HH_CMD_DATA api_sndcmd;
150 tBTA_HH_CBACK_DATA hid_cback;
151 tBTA_HH_STATUS status;
152 tBTA_HH_MAINT_DEV api_maintdev;
153 tBTA_HH_LE_CLOSE le_close;
154 tBTA_GATTC_OPEN le_open;
155 tBTA_HH_SCPP_UPDATE le_scpp_update;
156 tBTA_GATTC_ENC_CMPL_CB le_enc_cmpl;
162 uint8_t srvc_inst_id;
163 uint16_t char_inst_id;
164 tBTA_HH_RPT_TYPE rpt_type;
167 bool client_cfg_exist;
168 uint16_t client_cfg_value;
171 #ifndef BTA_HH_LE_RPT_MAX
172 #define BTA_HH_LE_RPT_MAX 20
177 uint8_t srvc_inst_id;
178 tBTA_HH_LE_RPT report[BTA_HH_LE_RPT_MAX];
180 uint16_t proto_mode_handle;
181 uint8_t control_point_handle;
184 incl_srvc_inst; /* assuming only one included service : battery service */
185 uint8_t cur_expl_char_idx; /* currently discovering service index */
187 uint16_t ext_rpt_ref;
188 tBTA_HH_DEV_DESCR descriptor;
190 } tBTA_HH_LE_HID_SRVC;
192 /* convert a HID handle to the LE CB index */
193 #define BTA_HH_GET_LE_CB_IDX(x) (((x) >> 4) - 1)
194 /* convert a GATT connection ID to HID device handle, it is the hi 4 bits of a
196 #define BTA_HH_GET_LE_DEV_HDL(x) (uint8_t)(((x) + 1) << 4)
197 /* check to see if th edevice handle is a LE device handle */
198 #define BTA_HH_IS_LE_DEV_HDL(x) ((x)&0xf0)
199 #define BTA_HH_IS_LE_DEV_HDL_VALID(x) (((x) >> 4) <= BTA_HH_LE_MAX_KNOWN)
201 /* device control block */
203 tBTA_HH_DEV_DSCP_INFO dscp_info; /* report descriptor and DI information */
204 RawAddress addr; /* BD-Addr of the HID device */
205 uint16_t attr_mask; /* attribute mask */
206 uint16_t w4_evt; /* W4_handshake event name */
207 uint8_t index; /* index number referenced to handle index */
208 uint8_t sub_class; /* Cod sub class */
209 uint8_t app_id; /* application ID for this connection */
210 uint8_t hid_handle; /* device handle : low 4 bits for regular HID:
211 HID_HOST_MAX_DEVICES can not exceed 15;
212 high 4 bits for LE HID:
213 GATT_MAX_PHY_CHANNEL can not exceed 15 */
214 bool vp; /* virtually unplug flag */
215 bool in_use; /* control block currently in use */
216 bool incoming_conn; /* is incoming connection? */
217 uint8_t incoming_hid_handle; /* temporary handle for incoming connection? */
218 bool opened; /* true if device successfully opened HID connection */
219 tBTA_HH_PROTO_MODE mode; /* protocol mode */
220 tBTA_HH_STATE state; /* CB state */
222 #define BTA_HH_LE_DISC_NONE 0x00
223 #define BTA_HH_LE_DISC_HIDS 0x01
224 #define BTA_HH_LE_DISC_DIS 0x02
225 #define BTA_HH_LE_DISC_SCPS 0x04
228 tBTA_HH_STATUS status;
229 tBTA_GATT_REASON reason;
231 tBTA_HH_LE_HID_SRVC hid_srvc;
235 uint16_t scan_refresh_char_handle;
238 #define BTA_HH_LE_SCPS_NOTIFY_NONE 0
239 #define BTA_HH_LE_SCPS_NOTIFY_SPT 0x01
240 #define BTA_HH_LE_SCPS_NOTIFY_ENB 0x02
241 uint8_t scps_notify; /* scan refresh supported/notification enabled */
243 bool security_pending;
246 /* key board parsing control block */
248 bool mod_key[4]; /* ctrl, shift(upper), Alt, GUI */
251 uint8_t last_report[BTA_HH_MAX_RPT_CHARS];
254 /******************************************************************************
256 ******************************************************************************/
258 tBTA_HH_KB_CB kb_cb; /* key board control block,
259 suppose BTA will connect
260 to only one keyboard at
262 tBTA_HH_DEV_CB kdev[BTA_HH_MAX_DEVICE]; /* device control block */
263 tBTA_HH_DEV_CB* p_cur; /* current device control
264 block idx, used in sdp */
265 uint8_t cb_index[BTA_HH_MAX_KNOWN]; /* maintain a CB index
267 uint8_t le_cb_index[BTA_HH_LE_MAX_KNOWN]; /* maintain a CB index map to LE dev
270 tBTA_HH_CBACK* p_cback; /* Application callbacks */
271 tSDP_DISCOVERY_DB* p_disc_db;
272 uint8_t trace_level; /* tracing level */
273 uint8_t cnt_num; /* connected device number */
274 bool w4_disable; /* w4 disable flag */
277 extern tBTA_HH_CB bta_hh_cb;
279 /* from bta_hh_cfg.c */
280 extern tBTA_HH_CFG* p_bta_hh_cfg;
282 /*****************************************************************************
283 * Function prototypes
284 ****************************************************************************/
285 extern bool bta_hh_hdl_event(BT_HDR_RIGID* p_msg);
286 extern void bta_hh_sm_execute(tBTA_HH_DEV_CB* p_cb, uint16_t event,
287 tBTA_HH_DATA* p_data);
289 /* action functions */
290 extern void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
291 extern void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
292 extern void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
293 extern void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
294 extern void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
295 extern void bta_hh_start_sdp(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
296 extern void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
297 extern void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
298 extern void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
299 extern void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
300 extern void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
301 extern void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
302 extern void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
304 /* utility functions */
305 extern uint8_t bta_hh_find_cb(const RawAddress& bda);
306 extern void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT* p_kb_data,
307 uint8_t* p_report, uint16_t report_len);
308 extern void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT* p_kb_data,
309 uint8_t* p_report, uint16_t report_len);
310 extern bool bta_hh_tod_spt(tBTA_HH_DEV_CB* p_cb, uint8_t sub_class);
311 extern void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB* p_cb);
313 extern void bta_hh_add_device_to_list(tBTA_HH_DEV_CB* p_cb, uint8_t handle,
315 tHID_DEV_DSCP_INFO* p_dscp_info,
316 uint8_t sub_class, uint16_t max_latency,
317 uint16_t min_tout, uint8_t app_id);
318 extern void bta_hh_update_di_info(tBTA_HH_DEV_CB* p_cb, uint16_t vendor_id,
319 uint16_t product_id, uint16_t version,
321 extern void bta_hh_cleanup_disable(tBTA_HH_STATUS status);
323 extern uint8_t bta_hh_dev_handle_to_cb_idx(uint8_t dev_handle);
325 /* action functions used outside state machine */
326 extern void bta_hh_api_enable(tBTA_HH_DATA* p_data);
327 extern void bta_hh_api_disable(void);
328 extern void bta_hh_disc_cmpl(void);
330 extern tBTA_HH_STATUS bta_hh_read_ssr_param(const RawAddress& bd_addr,
331 uint16_t* p_max_ssr_lat,
332 uint16_t* p_min_ssr_tout);
334 /* functions for LE HID */
335 extern void bta_hh_le_enable(void);
336 extern bool bta_hh_le_is_hh_gatt_if(tGATT_IF client_if);
337 extern void bta_hh_le_deregister(void);
338 extern bool bta_hh_is_le_device(tBTA_HH_DEV_CB* p_cb,
339 const RawAddress& remote_bda);
340 extern void bta_hh_le_open_conn(tBTA_HH_DEV_CB* p_cb,
341 const RawAddress& remote_bda);
342 extern void bta_hh_le_api_disc_act(tBTA_HH_DEV_CB* p_cb);
343 extern void bta_hh_le_get_dscp_act(tBTA_HH_DEV_CB* p_cb);
344 extern void bta_hh_le_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
345 extern uint8_t bta_hh_le_add_device(tBTA_HH_DEV_CB* p_cb,
346 tBTA_HH_MAINT_DEV* p_dev_info);
347 extern void bta_hh_le_remove_dev_bg_conn(tBTA_HH_DEV_CB* p_cb);
348 extern void bta_hh_le_open_fail(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
349 extern void bta_hh_gatt_open(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
350 extern void bta_hh_gatt_close(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
351 extern void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
353 extern void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
354 extern void bta_hh_security_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
355 extern void bta_hh_le_notify_enc_cmpl(tBTA_HH_DEV_CB* p_cb,
356 tBTA_HH_DATA* p_data);
358 #if (BTA_HH_DEBUG == TRUE)
359 extern void bta_hh_trace_dev_db(void);