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