bta_hh_int.h revision 84baa7f16e830394408278dbb8c508dd9fa02887
1/******************************************************************************
2 *
3 *  Copyright (C) 2005-2012 Broadcom Corporation
4 *
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:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 *  This file contains BTA HID Host internal definitions
22 *
23 ******************************************************************************/
24
25#ifndef BTA_HH_INT_H
26#define BTA_HH_INT_H
27
28#include "bta_hh_api.h"
29#include "bta_sys.h"
30#include "utl.h"
31
32#if (BTA_HH_LE_INCLUDED == TRUE)
33#include "bta_gatt_api.h"
34#endif
35
36/* can be moved to bta_api.h */
37#define BTA_HH_MAX_RPT_CHARS 8
38
39#if (BTA_GATT_INCLUDED == FALSE)
40#undef BTA_HH_LE_INCLUDED
41#define BTA_HH_LE_INCLUDED false
42#endif
43
44/* state machine events, these events are handled by the state machine */
45enum {
46  BTA_HH_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_HH),
47  BTA_HH_API_CLOSE_EVT,
48  BTA_HH_INT_OPEN_EVT,
49  BTA_HH_INT_CLOSE_EVT,
50  BTA_HH_INT_DATA_EVT,
51  BTA_HH_INT_CTRL_DATA,
52  BTA_HH_INT_HANDSK_EVT,
53  BTA_HH_SDP_CMPL_EVT,
54  BTA_HH_API_WRITE_DEV_EVT,
55  BTA_HH_API_GET_DSCP_EVT,
56  BTA_HH_API_MAINT_DEV_EVT,
57  BTA_HH_OPEN_CMPL_EVT,
58#if (BTA_HH_LE_INCLUDED == TRUE)
59  BTA_HH_GATT_CLOSE_EVT,
60  BTA_HH_GATT_OPEN_EVT,
61  BTA_HH_START_ENC_EVT,
62  BTA_HH_ENC_CMPL_EVT,
63  BTA_HH_GATT_ENC_CMPL_EVT,
64#endif
65
66  /* not handled by execute state machine */
67  BTA_HH_API_ENABLE_EVT,
68  BTA_HH_API_DISABLE_EVT,
69  BTA_HH_DISC_CMPL_EVT
70};
71typedef uint16_t tBTA_HH_INT_EVT; /* HID host internal events */
72
73#define BTA_HH_INVALID_EVT (BTA_HH_DISC_CMPL_EVT + 1)
74
75/* event used to map between BTE event and BTA event */
76#define BTA_HH_FST_TRANS_CB_EVT BTA_HH_GET_RPT_EVT
77#define BTA_HH_FST_BTE_TRANS_EVT HID_TRANS_GET_REPORT
78
79/* sub event code used for device maintainence API call */
80#define BTA_HH_ADD_DEV 0
81#define BTA_HH_REMOVE_DEV 1
82
83/* state machine states */
84enum {
85  BTA_HH_NULL_ST,
86  BTA_HH_IDLE_ST,
87  BTA_HH_W4_CONN_ST,
88  BTA_HH_CONN_ST
89#if (BTA_HH_LE_INCLUDED == TRUE)
90  ,
91  BTA_HH_W4_SEC
92#endif
93  ,
94  BTA_HH_INVALID_ST /* Used to check invalid states before executing SM function
95                       */
96
97};
98typedef uint8_t tBTA_HH_STATE;
99
100/* data structure used to send a command/data to HID device */
101typedef struct {
102  BT_HDR hdr;
103  uint8_t t_type;
104  uint8_t param;
105  uint8_t rpt_id;
106#if (BTA_HH_LE_INCLUDED == TRUE)
107  uint8_t srvc_id;
108#endif
109  uint16_t data;
110  BT_HDR* p_data;
111} tBTA_HH_CMD_DATA;
112
113/* data type for BTA_HH_API_ENABLE_EVT */
114typedef struct {
115  BT_HDR hdr;
116  uint8_t sec_mask;
117  uint8_t service_name[BTA_SERVICE_NAME_LEN + 1];
118  tBTA_HH_CBACK* p_cback;
119} tBTA_HH_API_ENABLE;
120
121typedef struct {
122  BT_HDR hdr;
123  BD_ADDR bd_addr;
124  uint8_t sec_mask;
125  tBTA_HH_PROTO_MODE mode;
126} tBTA_HH_API_CONN;
127
128/* internal event data from BTE HID callback */
129typedef struct {
130  BT_HDR hdr;
131  BD_ADDR addr;
132  uint32_t data;
133  BT_HDR* p_data;
134} tBTA_HH_CBACK_DATA;
135
136typedef struct {
137  BT_HDR hdr;
138  BD_ADDR bda;
139  uint16_t attr_mask;
140  uint16_t sub_event;
141  uint8_t sub_class;
142  uint8_t app_id;
143  tBTA_HH_DEV_DSCP_INFO dscp_info;
144} tBTA_HH_MAINT_DEV;
145
146#if (BTA_HH_LE_INCLUDED == TRUE)
147typedef struct {
148  BT_HDR hdr;
149  uint16_t conn_id;
150  tBTA_GATT_REASON reason; /* disconnect reason code, not useful when connect
151                              event is reported */
152
153} tBTA_HH_LE_CLOSE;
154
155typedef struct {
156  BT_HDR hdr;
157  uint16_t scan_int;
158  uint16_t scan_win;
159} tBTA_HH_SCPP_UPDATE;
160#endif
161/* union of all event data types */
162typedef union {
163  BT_HDR hdr;
164  tBTA_HH_API_ENABLE api_enable;
165  tBTA_HH_API_CONN api_conn;
166  tBTA_HH_CMD_DATA api_sndcmd;
167  tBTA_HH_CBACK_DATA hid_cback;
168  tBTA_HH_STATUS status;
169  tBTA_HH_MAINT_DEV api_maintdev;
170#if (BTA_HH_LE_INCLUDED == TRUE)
171  tBTA_HH_LE_CLOSE le_close;
172  tBTA_GATTC_OPEN le_open;
173  tBTA_HH_SCPP_UPDATE le_scpp_update;
174  tBTA_GATTC_ENC_CMPL_CB le_enc_cmpl;
175#endif
176} tBTA_HH_DATA;
177
178#if (BTA_HH_LE_INCLUDED == TRUE)
179typedef struct {
180  uint8_t index;
181  bool in_use;
182  uint8_t srvc_inst_id;
183  uint8_t char_inst_id;
184  tBTA_HH_RPT_TYPE rpt_type;
185  uint16_t uuid;
186  uint8_t rpt_id;
187  bool client_cfg_exist;
188  uint16_t client_cfg_value;
189} tBTA_HH_LE_RPT;
190
191#ifndef BTA_HH_LE_RPT_MAX
192#define BTA_HH_LE_RPT_MAX 20
193#endif
194
195typedef struct {
196  bool in_use;
197  uint8_t srvc_inst_id;
198  tBTA_HH_LE_RPT report[BTA_HH_LE_RPT_MAX];
199
200  uint16_t proto_mode_handle;
201  uint8_t control_point_handle;
202
203  uint8_t
204      incl_srvc_inst; /* assuming only one included service : battery service */
205  uint8_t cur_expl_char_idx; /* currently discovering service index */
206  uint8_t* rpt_map;
207  uint16_t ext_rpt_ref;
208  tBTA_HH_DEV_DESCR descriptor;
209
210} tBTA_HH_LE_HID_SRVC;
211
212/* convert a HID handle to the LE CB index */
213#define BTA_HH_GET_LE_CB_IDX(x) (((x) >> 4) - 1)
214/* convert a GATT connection ID to HID device handle, it is the hi 4 bits of a
215 * uint8_t */
216#define BTA_HH_GET_LE_DEV_HDL(x) (uint8_t)(((x) + 1) << 4)
217/* check to see if th edevice handle is a LE device handle */
218#define BTA_HH_IS_LE_DEV_HDL(x) ((x)&0xf0)
219#define BTA_HH_IS_LE_DEV_HDL_VALID(x) (((x) >> 4) <= BTA_HH_LE_MAX_KNOWN)
220#endif
221
222/* device control block */
223typedef struct {
224  tBTA_HH_DEV_DSCP_INFO dscp_info; /* report descriptor and DI information */
225  BD_ADDR addr;                    /* BD-Addr of the HID device */
226  uint16_t attr_mask;              /* attribute mask */
227  uint16_t w4_evt;                 /* W4_handshake event name */
228  uint8_t index;                   /* index number referenced to handle index */
229  uint8_t sub_class;               /* Cod sub class */
230  uint8_t sec_mask;                /* security mask */
231  uint8_t app_id;                  /* application ID for this connection */
232  uint8_t hid_handle;          /* device handle : low 4 bits for regular HID:
233                                  HID_HOST_MAX_DEVICES can not exceed 15;
234                                                 high 4 bits for LE HID:
235                                  GATT_MAX_PHY_CHANNEL can not exceed 15 */
236  bool vp;                     /* virtually unplug flag */
237  bool in_use;                 /* control block currently in use */
238  bool incoming_conn;          /* is incoming connection? */
239  uint8_t incoming_hid_handle; /* temporary handle for incoming connection? */
240  bool opened; /* true if device successfully opened HID connection */
241  tBTA_HH_PROTO_MODE mode; /* protocol mode */
242  tBTA_HH_STATE state;     /* CB state */
243
244#if (BTA_HH_LE_INCLUDED == TRUE)
245#define BTA_HH_LE_DISC_NONE 0x00
246#define BTA_HH_LE_DISC_HIDS 0x01
247#define BTA_HH_LE_DISC_DIS 0x02
248#define BTA_HH_LE_DISC_SCPS 0x04
249
250  uint8_t disc_active;
251  tBTA_HH_STATUS status;
252  tBTA_GATT_REASON reason;
253  bool is_le_device;
254  tBTA_HH_LE_HID_SRVC hid_srvc;
255  uint16_t conn_id;
256  bool in_bg_conn;
257  uint8_t clt_cfg_idx;
258  uint16_t scan_refresh_char_handle;
259  bool scps_supported;
260
261#define BTA_HH_LE_SCPS_NOTIFY_NONE 0
262#define BTA_HH_LE_SCPS_NOTIFY_SPT 0x01
263#define BTA_HH_LE_SCPS_NOTIFY_ENB 0x02
264  uint8_t scps_notify; /* scan refresh supported/notification enabled */
265#endif
266
267  bool security_pending;
268} tBTA_HH_DEV_CB;
269
270/* key board parsing control block */
271typedef struct {
272  bool mod_key[4]; /* ctrl, shift(upper), Alt, GUI */
273  bool num_lock;
274  bool caps_lock;
275  uint8_t last_report[BTA_HH_MAX_RPT_CHARS];
276} tBTA_HH_KB_CB;
277
278/******************************************************************************
279 * Main Control Block
280 ******************************************************************************/
281typedef struct {
282  tBTA_HH_KB_CB kb_cb;                    /* key board control block,
283                                             suppose BTA will connect
284                                             to only one keyboard at
285                                              the same time */
286  tBTA_HH_DEV_CB kdev[BTA_HH_MAX_DEVICE]; /* device control block */
287  tBTA_HH_DEV_CB* p_cur;                  /* current device control
288                                                 block idx, used in sdp */
289  uint8_t cb_index[BTA_HH_MAX_KNOWN];     /* maintain a CB index
290                                        map to dev handle */
291#if (BTA_HH_LE_INCLUDED == TRUE)
292  uint8_t le_cb_index[BTA_HH_MAX_DEVICE]; /* maintain a CB index map to LE dev
293                                             handle */
294  tBTA_GATTC_IF gatt_if;
295#endif
296  tBTA_HH_CBACK* p_cback; /* Application callbacks */
297  tSDP_DISCOVERY_DB* p_disc_db;
298  uint8_t trace_level; /* tracing level */
299  uint8_t cnt_num;     /* connected device number */
300  bool w4_disable;     /* w4 disable flag */
301} tBTA_HH_CB;
302
303extern tBTA_HH_CB bta_hh_cb;
304
305/* from bta_hh_cfg.c */
306extern tBTA_HH_CFG* p_bta_hh_cfg;
307
308/*****************************************************************************
309 *  Function prototypes
310 ****************************************************************************/
311extern bool bta_hh_hdl_event(BT_HDR* p_msg);
312extern void bta_hh_sm_execute(tBTA_HH_DEV_CB* p_cb, uint16_t event,
313                              tBTA_HH_DATA* p_data);
314
315/* action functions */
316extern void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
317extern void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
318extern void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
319extern void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
320extern void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
321extern void bta_hh_start_sdp(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
322extern void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
323extern void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
324extern void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
325extern void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
326extern void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
327extern void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
328extern void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
329
330/* utility functions */
331extern uint8_t bta_hh_find_cb(BD_ADDR bda);
332extern void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT* p_kb_data,
333                                   uint8_t* p_report, uint16_t report_len);
334extern void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT* p_kb_data,
335                                  uint8_t* p_report, uint16_t report_len);
336extern bool bta_hh_tod_spt(tBTA_HH_DEV_CB* p_cb, uint8_t sub_class);
337extern void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB* p_cb);
338
339extern void bta_hh_add_device_to_list(tBTA_HH_DEV_CB* p_cb, uint8_t handle,
340                                      uint16_t attr_mask,
341                                      tHID_DEV_DSCP_INFO* p_dscp_info,
342                                      uint8_t sub_class, uint16_t max_latency,
343                                      uint16_t min_tout, uint8_t app_id);
344extern void bta_hh_update_di_info(tBTA_HH_DEV_CB* p_cb, uint16_t vendor_id,
345                                  uint16_t product_id, uint16_t version,
346                                  uint8_t flag);
347extern void bta_hh_cleanup_disable(tBTA_HH_STATUS status);
348
349extern uint8_t bta_hh_dev_handle_to_cb_idx(uint8_t dev_handle);
350
351/* action functions used outside state machine */
352extern void bta_hh_api_enable(tBTA_HH_DATA* p_data);
353extern void bta_hh_api_disable(void);
354extern void bta_hh_disc_cmpl(void);
355
356extern tBTA_HH_STATUS bta_hh_read_ssr_param(BD_ADDR bd_addr,
357                                            uint16_t* p_max_ssr_lat,
358                                            uint16_t* p_min_ssr_tout);
359
360/* functions for LE HID */
361extern void bta_hh_le_enable(void);
362extern bool bta_hh_le_is_hh_gatt_if(tBTA_GATTC_IF client_if);
363extern void bta_hh_le_deregister(void);
364extern bool bta_hh_is_le_device(tBTA_HH_DEV_CB* p_cb, BD_ADDR remote_bda);
365extern void bta_hh_le_open_conn(tBTA_HH_DEV_CB* p_cb, BD_ADDR remote_bda);
366extern void bta_hh_le_api_disc_act(tBTA_HH_DEV_CB* p_cb);
367extern void bta_hh_le_get_dscp_act(tBTA_HH_DEV_CB* p_cb);
368extern void bta_hh_le_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
369extern uint8_t bta_hh_le_add_device(tBTA_HH_DEV_CB* p_cb,
370                                    tBTA_HH_MAINT_DEV* p_dev_info);
371extern void bta_hh_le_remove_dev_bg_conn(tBTA_HH_DEV_CB* p_cb);
372extern void bta_hh_le_open_fail(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
373extern void bta_hh_gatt_open(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
374extern void bta_hh_gatt_close(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
375extern void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
376extern void bta_hh_start_srvc_discovery(tBTA_HH_DEV_CB* p_cb,
377                                        tBTA_HH_DATA* p_buf);
378extern void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
379extern void bta_hh_security_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
380extern void bta_hh_le_notify_enc_cmpl(tBTA_HH_DEV_CB* p_cb,
381                                      tBTA_HH_DATA* p_data);
382extern void bta_hh_ci_load_rpt(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_buf);
383
384#if (BTA_HH_DEBUG == TRUE)
385extern void bta_hh_trace_dev_db(void);
386#endif
387
388#endif
389