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