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