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