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 ******************************************************************************/
28 #include "bta_hh_api.h"
32 #include "bta_gatt_api.h"
34 /* can be moved to bta_api.h */
35 #define BTA_HH_MAX_RPT_CHARS 8
37 /* state machine events, these events are handled by the state machine */
39 BTA_HH_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_HH),
45 BTA_HH_INT_HANDSK_EVT,
47 BTA_HH_API_WRITE_DEV_EVT,
48 BTA_HH_API_GET_DSCP_EVT,
49 BTA_HH_API_MAINT_DEV_EVT,
51 BTA_HH_GATT_CLOSE_EVT,
55 BTA_HH_GATT_ENC_CMPL_EVT,
57 /* not handled by execute state machine */
58 BTA_HH_API_ENABLE_EVT,
59 BTA_HH_API_DISABLE_EVT,
62 typedef uint16_t tBTA_HH_INT_EVT; /* HID host internal events */
64 #define BTA_HH_INVALID_EVT (BTA_HH_DISC_CMPL_EVT + 1)
66 /* event used to map between BTE event and BTA event */
67 #define BTA_HH_FST_TRANS_CB_EVT BTA_HH_GET_RPT_EVT
68 #define BTA_HH_FST_BTE_TRANS_EVT HID_TRANS_GET_REPORT
70 /* sub event code used for device maintainence API call */
71 #define BTA_HH_ADD_DEV 0
72 #define BTA_HH_REMOVE_DEV 1
74 /* state machine states */
81 BTA_HH_INVALID_ST /* Used to check invalid states before executing SM function
85 typedef uint8_t tBTA_HH_STATE;
87 /* data structure used to send a command/data to HID device */
98 /* data type for BTA_HH_API_ENABLE_EVT */
101 uint8_t service_name[BTA_SERVICE_NAME_LEN + 1];
102 tBTA_HH_CBACK* p_cback;
103 } tBTA_HH_API_ENABLE;
108 tBTA_HH_PROTO_MODE mode;
111 /* internal event data from BTE HID callback */
117 } tBTA_HH_CBACK_DATA;
126 tBTA_HH_DEV_DSCP_INFO dscp_info;
132 tBTA_GATT_REASON reason; /* disconnect reason code, not useful when connect
141 } tBTA_HH_SCPP_UPDATE;
143 /* union of all event data types */
146 tBTA_HH_API_ENABLE api_enable;
147 tBTA_HH_API_CONN api_conn;
148 tBTA_HH_CMD_DATA api_sndcmd;
149 tBTA_HH_CBACK_DATA hid_cback;
150 tBTA_HH_STATUS status;
151 tBTA_HH_MAINT_DEV api_maintdev;
152 tBTA_HH_LE_CLOSE le_close;
153 tBTA_GATTC_OPEN le_open;
154 tBTA_HH_SCPP_UPDATE le_scpp_update;
155 tBTA_GATTC_ENC_CMPL_CB le_enc_cmpl;
161 uint8_t srvc_inst_id;
162 uint16_t char_inst_id;
163 tBTA_HH_RPT_TYPE rpt_type;
166 bool client_cfg_exist;
167 uint16_t client_cfg_value;
170 #ifndef BTA_HH_LE_RPT_MAX
171 #define BTA_HH_LE_RPT_MAX 20
176 uint8_t srvc_inst_id;
177 tBTA_HH_LE_RPT report[BTA_HH_LE_RPT_MAX];
179 uint16_t proto_mode_handle;
180 uint8_t control_point_handle;
183 incl_srvc_inst; /* assuming only one included service : battery service */
184 uint8_t cur_expl_char_idx; /* currently discovering service index */
186 uint16_t ext_rpt_ref;
187 tBTA_HH_DEV_DESCR descriptor;
189 } tBTA_HH_LE_HID_SRVC;
191 /* convert a HID handle to the LE CB index */
192 #define BTA_HH_GET_LE_CB_IDX(x) (((x) >> 4) - 1)
193 /* convert a GATT connection ID to HID device handle, it is the hi 4 bits of a
195 #define BTA_HH_GET_LE_DEV_HDL(x) (uint8_t)(((x) + 1) << 4)
196 /* check to see if th edevice handle is a LE device handle */
197 #define BTA_HH_IS_LE_DEV_HDL(x) ((x)&0xf0)
198 #define BTA_HH_IS_LE_DEV_HDL_VALID(x) (((x) >> 4) <= BTA_HH_LE_MAX_KNOWN)
200 /* device control block */
202 tBTA_HH_DEV_DSCP_INFO dscp_info; /* report descriptor and DI information */
203 RawAddress addr; /* BD-Addr of the HID device */
204 uint16_t attr_mask; /* attribute mask */
205 uint16_t w4_evt; /* W4_handshake event name */
206 uint8_t index; /* index number referenced to handle index */
207 uint8_t sub_class; /* Cod sub class */
208 uint8_t app_id; /* application ID for this connection */
209 uint8_t hid_handle; /* device handle : low 4 bits for regular HID:
210 HID_HOST_MAX_DEVICES can not exceed 15;
211 high 4 bits for LE HID:
212 GATT_MAX_PHY_CHANNEL can not exceed 15 */
213 bool vp; /* virtually unplug flag */
214 bool in_use; /* control block currently in use */
215 bool incoming_conn; /* is incoming connection? */
216 uint8_t incoming_hid_handle; /* temporary handle for incoming connection? */
217 bool opened; /* true if device successfully opened HID connection */
218 tBTA_HH_PROTO_MODE mode; /* protocol mode */
219 tBTA_HH_STATE state; /* CB state */
221 #define BTA_HH_LE_DISC_NONE 0x00
222 #define BTA_HH_LE_DISC_HIDS 0x01
223 #define BTA_HH_LE_DISC_DIS 0x02
224 #define BTA_HH_LE_DISC_SCPS 0x04
227 tBTA_HH_STATUS status;
228 tBTA_GATT_REASON reason;
230 tBTA_HH_LE_HID_SRVC hid_srvc;
234 uint16_t scan_refresh_char_handle;
237 #define BTA_HH_LE_SCPS_NOTIFY_NONE 0
238 #define BTA_HH_LE_SCPS_NOTIFY_SPT 0x01
239 #define BTA_HH_LE_SCPS_NOTIFY_ENB 0x02
240 uint8_t scps_notify; /* scan refresh supported/notification enabled */
242 bool security_pending;
245 /* key board parsing control block */
247 bool mod_key[4]; /* ctrl, shift(upper), Alt, GUI */
250 uint8_t last_report[BTA_HH_MAX_RPT_CHARS];
253 /******************************************************************************
255 ******************************************************************************/
257 tBTA_HH_KB_CB kb_cb; /* key board control block,
258 suppose BTA will connect
259 to only one keyboard at
261 tBTA_HH_DEV_CB kdev[BTA_HH_MAX_DEVICE]; /* device control block */
262 tBTA_HH_DEV_CB* p_cur; /* current device control
263 block idx, used in sdp */
264 uint8_t cb_index[BTA_HH_MAX_KNOWN]; /* maintain a CB index
266 uint8_t le_cb_index[BTA_HH_LE_MAX_KNOWN]; /* maintain a CB index map to LE dev
269 tBTA_HH_CBACK* p_cback; /* Application callbacks */
270 tSDP_DISCOVERY_DB* p_disc_db;
271 uint8_t trace_level; /* tracing level */
272 uint8_t cnt_num; /* connected device number */
273 bool w4_disable; /* w4 disable flag */
276 extern tBTA_HH_CB bta_hh_cb;
278 /* from bta_hh_cfg.c */
279 extern tBTA_HH_CFG* p_bta_hh_cfg;
281 /*****************************************************************************
282 * Function prototypes
283 ****************************************************************************/
284 extern bool bta_hh_hdl_event(BT_HDR* p_msg);
285 extern void bta_hh_sm_execute(tBTA_HH_DEV_CB* p_cb, uint16_t event,
286 tBTA_HH_DATA* p_data);
288 /* action functions */
289 extern void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
290 extern void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
291 extern void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
292 extern void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
293 extern void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
294 extern void bta_hh_start_sdp(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
295 extern void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
296 extern void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
297 extern void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
298 extern void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
299 extern void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
300 extern void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
301 extern void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
303 /* utility functions */
304 extern uint8_t bta_hh_find_cb(const RawAddress& bda);
305 extern void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT* p_kb_data,
306 uint8_t* p_report, uint16_t report_len);
307 extern void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT* p_kb_data,
308 uint8_t* p_report, uint16_t report_len);
309 extern bool bta_hh_tod_spt(tBTA_HH_DEV_CB* p_cb, uint8_t sub_class);
310 extern void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB* p_cb);
312 extern void bta_hh_add_device_to_list(tBTA_HH_DEV_CB* p_cb, uint8_t handle,
314 tHID_DEV_DSCP_INFO* p_dscp_info,
315 uint8_t sub_class, uint16_t max_latency,
316 uint16_t min_tout, uint8_t app_id);
317 extern void bta_hh_update_di_info(tBTA_HH_DEV_CB* p_cb, uint16_t vendor_id,
318 uint16_t product_id, uint16_t version,
320 extern void bta_hh_cleanup_disable(tBTA_HH_STATUS status);
322 extern uint8_t bta_hh_dev_handle_to_cb_idx(uint8_t dev_handle);
324 /* action functions used outside state machine */
325 extern void bta_hh_api_enable(tBTA_HH_DATA* p_data);
326 extern void bta_hh_api_disable(void);
327 extern void bta_hh_disc_cmpl(void);
329 extern tBTA_HH_STATUS bta_hh_read_ssr_param(const RawAddress& bd_addr,
330 uint16_t* p_max_ssr_lat,
331 uint16_t* p_min_ssr_tout);
333 /* functions for LE HID */
334 extern void bta_hh_le_enable(void);
335 extern bool bta_hh_le_is_hh_gatt_if(tGATT_IF client_if);
336 extern void bta_hh_le_deregister(void);
337 extern bool bta_hh_is_le_device(tBTA_HH_DEV_CB* p_cb,
338 const RawAddress& remote_bda);
339 extern void bta_hh_le_open_conn(tBTA_HH_DEV_CB* p_cb,
340 const RawAddress& remote_bda);
341 extern void bta_hh_le_api_disc_act(tBTA_HH_DEV_CB* p_cb);
342 extern void bta_hh_le_get_dscp_act(tBTA_HH_DEV_CB* p_cb);
343 extern void bta_hh_le_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
344 extern uint8_t bta_hh_le_add_device(tBTA_HH_DEV_CB* p_cb,
345 tBTA_HH_MAINT_DEV* p_dev_info);
346 extern void bta_hh_le_remove_dev_bg_conn(tBTA_HH_DEV_CB* p_cb);
347 extern void bta_hh_le_open_fail(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
348 extern void bta_hh_gatt_open(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
349 extern void bta_hh_gatt_close(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
350 extern void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
352 extern void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
353 extern void bta_hh_security_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
354 extern void bta_hh_le_notify_enc_cmpl(tBTA_HH_DEV_CB* p_cb,
355 tBTA_HH_DATA* p_data);
357 #if (BTA_HH_DEBUG == TRUE)
358 extern void bta_hh_trace_dev_db(void);