OSDN Git Service

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