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