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 <bluetooth/log.h>
29 
30 #include <cstdint>
31 
32 #include "bta/include/bta_api.h"
33 #include "bta/include/bta_gatt_api.h"
34 #include "bta/include/bta_hh_api.h"
35 #include "bta/sys/bta_sys.h"
36 #include "stack/include/bt_hdr.h"
37 #include "types/raw_address.h"
38 
39 /* state machine events, these events are handled by the state machine */
40 enum tBTA_HH_INT_EVT : uint16_t {
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 }; /* HID host internal events */
59 
60 #define BTA_HH_INVALID_EVT (BTA_HH_GATT_ENC_CMPL_EVT + 1)
61 
62 /* state machine states */
63 enum {
64   BTA_HH_NULL_ST,
65   BTA_HH_IDLE_ST,
66   BTA_HH_W4_CONN_ST,
67   BTA_HH_CONN_ST,
68   BTA_HH_W4_SEC,
69   BTA_HH_INVALID_ST /* Used to check invalid states before executing SM function
70                      */
71 
72 };
73 typedef uint8_t tBTA_HH_STATE;
74 
75 /* data structure used to send a command/data to HID device */
76 typedef struct {
77   BT_HDR_RIGID hdr;
78   uint8_t t_type;
79   uint8_t param;
80   uint8_t rpt_id;
81   uint16_t data;
82   BT_HDR* p_data;
83 } tBTA_HH_CMD_DATA;
84 
85 typedef struct {
86   BT_HDR_RIGID hdr;
87   tAclLinkSpec link_spec;
88   tBTA_HH_PROTO_MODE mode;
89 } tBTA_HH_API_CONN;
90 
91 /* internal event data from BTE HID callback */
92 typedef struct {
93   BT_HDR_RIGID hdr;
94   tAclLinkSpec link_spec;
95   uint32_t data;
96   BT_HDR* p_data;
97 } tBTA_HH_CBACK_DATA;
98 
99 typedef struct {
100   BT_HDR_RIGID hdr;
101   tAclLinkSpec link_spec;
102   uint16_t attr_mask;
103   uint16_t sub_event;
104   uint8_t sub_class;
105   uint8_t app_id;
106   tBTA_HH_DEV_DSCP_INFO dscp_info;
107 } tBTA_HH_MAINT_DEV;
108 
109 typedef struct {
110   BT_HDR_RIGID hdr;
111   uint16_t conn_id;
112   tGATT_DISCONN_REASON reason;
113 } tBTA_HH_LE_CLOSE;
114 
115 typedef struct {
116   BT_HDR_RIGID hdr;
117   uint16_t scan_int;
118   uint16_t scan_win;
119 } tBTA_HH_SCPP_UPDATE;
120 
121 /* union of all event data types */
122 typedef union {
123   BT_HDR_RIGID hdr;
124   tBTA_HH_API_CONN api_conn;
125   tBTA_HH_CMD_DATA api_sndcmd;
126   tBTA_HH_CBACK_DATA hid_cback;
127   tBTA_HH_STATUS status;
128   tBTA_HH_MAINT_DEV api_maintdev;
129   tBTA_HH_LE_CLOSE le_close;
130   tBTA_GATTC_OPEN le_open;
131   tBTA_HH_SCPP_UPDATE le_scpp_update;
132   tBTA_GATTC_ENC_CMPL_CB le_enc_cmpl;
133 } tBTA_HH_DATA;
134 
135 typedef struct {
136   uint8_t index;
137   bool in_use;
138   uint8_t srvc_inst_id;
139   uint16_t char_inst_id;
140   tBTA_HH_RPT_TYPE rpt_type;
141   uint16_t uuid;
142   uint8_t rpt_id;
143   bool client_cfg_exist;
144   uint16_t client_cfg_value;
145 } tBTA_HH_LE_RPT;
146 
147 #ifndef BTA_HH_LE_RPT_MAX
148 #define BTA_HH_LE_RPT_MAX 20
149 #endif
150 
151 enum tBTA_HH_SERVICE_STATE {
152   BTA_HH_SERVICE_UNKNOWN,
153   BTA_HH_SERVICE_CHANGED,
154   BTA_HH_SERVICE_DISCOVERED,
155 };
156 
157 enum tBTA_HH_AVAILABLE {
158   BTA_HH_UNKNOWN = 0,
159   BTA_HH_AVAILABLE,
160   BTA_HH_UNAVAILABLE
161 };
162 
163 typedef struct {
164   tBTA_HH_SERVICE_STATE state;
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   tBTA_HH_AVAILABLE headtracker_support;
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   tAclLinkSpec link_spec; /* ACL link specification 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   tBTA_HH_LE_HID_SRVC hid_srvc;
218   uint16_t conn_id;
219   bool in_bg_conn;
220   uint8_t clt_cfg_idx;
221   bool scps_supported;
222 
223 #define BTA_HH_LE_SCPS_NOTIFY_NONE 0
224 #define BTA_HH_LE_SCPS_NOTIFY_SPT 0x01
225 #define BTA_HH_LE_SCPS_NOTIFY_ENB 0x02
226   uint8_t scps_notify; /* scan refresh supported/notification enabled */
227   bool security_pending;
228 } tBTA_HH_DEV_CB;
229 
230 /******************************************************************************
231  * Main Control Block
232  ******************************************************************************/
233 typedef struct {
234   tBTA_HH_DEV_CB kdev[BTA_HH_MAX_DEVICE]; /* device control block */
235   tBTA_HH_DEV_CB* p_cur;                  /* current device control
236                                                  block idx, used in sdp */
237   uint8_t cb_index[BTA_HH_MAX_KNOWN];     /* maintain a CB index
238                                         map to dev handle */
239   uint8_t le_cb_index[BTA_HH_LE_MAX_KNOWN]; /* maintain a CB index map to LE dev
240                                              handle */
241   tGATT_IF gatt_if;
242   tBTA_HH_CBACK* p_cback; /* Application callbacks */
243   tSDP_DISCOVERY_DB* p_disc_db;
244   uint8_t cnt_num;     /* connected device number */
245   bool w4_disable;     /* w4 disable flag */
246 } tBTA_HH_CB;
247 
248 extern tBTA_HH_CB bta_hh_cb;
249 
250 /* from bta_hh_cfg.c */
251 extern tBTA_HH_CFG* p_bta_hh_cfg;
252 
253 /*****************************************************************************
254  *  Function prototypes
255  ****************************************************************************/
256 bool bta_hh_hdl_event(const BT_HDR_RIGID* p_msg);
257 void bta_hh_sm_execute(tBTA_HH_DEV_CB* p_cb, uint16_t event,
258                        const tBTA_HH_DATA* p_data);
259 
260 /* action functions */
261 void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
262 void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
263 void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
264 void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
265 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
266 void bta_hh_connect(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
267 void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
268 void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
269 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
270 void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
271 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
272 void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
273 void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
274 
275 /* utility functions */
276 uint8_t bta_hh_find_cb(const tAclLinkSpec& link_spec);
277 tBTA_HH_DEV_CB* bta_hh_get_cb(const tAclLinkSpec& link_spec);
278 bool bta_hh_tod_spt(tBTA_HH_DEV_CB* p_cb, uint8_t sub_class);
279 void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB* p_cb);
280 
281 void bta_hh_add_device_to_list(tBTA_HH_DEV_CB* p_cb, uint8_t handle,
282                                uint16_t attr_mask,
283                                const tHID_DEV_DSCP_INFO* p_dscp_info,
284                                uint8_t sub_class, uint16_t max_latency,
285                                uint16_t min_tout, uint8_t app_id);
286 void bta_hh_update_di_info(tBTA_HH_DEV_CB* p_cb, uint16_t vendor_id,
287                            uint16_t product_id, uint16_t version, uint8_t flag,
288                            uint8_t ctry_code);
289 void bta_hh_cleanup_disable(tBTA_HH_STATUS status);
290 
291 uint8_t bta_hh_dev_handle_to_cb_idx(uint8_t dev_handle);
292 
293 /* action functions used outside state machine */
294 void bta_hh_api_enable(tBTA_HH_CBACK* p_cback, bool enable_hid,
295                        bool enable_hogp);
296 void bta_hh_api_disable(void);
297 void bta_hh_disc_cmpl(void);
298 
299 tBTA_HH_STATUS bta_hh_read_ssr_param(const tAclLinkSpec& link_spec,
300                                      uint16_t* p_max_ssr_lat,
301                                      uint16_t* p_min_ssr_tout);
302 
303 /* functions for LE HID */
304 void bta_hh_le_enable(void);
305 void bta_hh_le_deregister(void);
306 void bta_hh_le_open_conn(tBTA_HH_DEV_CB* p_cb, const tAclLinkSpec& link_spec);
307 void bta_hh_le_api_disc_act(tBTA_HH_DEV_CB* p_cb);
308 void bta_hh_le_get_dscp_act(tBTA_HH_DEV_CB* p_cb);
309 void bta_hh_le_write_dev_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
310 uint8_t bta_hh_le_add_device(tBTA_HH_DEV_CB* p_cb,
311                              const tBTA_HH_MAINT_DEV* p_dev_info);
312 void bta_hh_le_remove_dev_bg_conn(tBTA_HH_DEV_CB* p_cb);
313 void bta_hh_le_open_fail(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
314 void bta_hh_gatt_open(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
315 void bta_hh_gatt_close(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
316 void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_buf);
317 
318 void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_buf);
319 void bta_hh_security_cmpl(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_buf);
320 void bta_hh_le_notify_enc_cmpl(tBTA_HH_DEV_CB* p_cb,
321                                const tBTA_HH_DATA* p_data);
322 
323 tBTA_HH_LE_RPT* bta_hh_le_find_alloc_report_entry(tBTA_HH_DEV_CB* p_cb,
324                                                   uint8_t srvc_inst_id,
325                                                   uint16_t rpt_uuid,
326                                                   uint16_t inst_id);
327 void bta_hh_le_save_report_ref(tBTA_HH_DEV_CB* p_dev_cb, tBTA_HH_LE_RPT* p_rpt,
328                                uint8_t rpt_type, uint8_t rpt_id);
329 void bta_hh_le_srvc_init(tBTA_HH_DEV_CB* p_dev_cb, uint16_t handle);
330 void bta_hh_le_save_report_map(tBTA_HH_DEV_CB* p_dev_cb, uint16_t len,
331                                uint8_t* desc);
332 void bta_hh_le_service_parsed(tBTA_HH_DEV_CB* p_dev_cb, tGATT_STATUS status);
333 
334 void bta_hh_headtracker_parse_service(tBTA_HH_DEV_CB* p_dev_cb,
335                                       const gatt::Service* service);
336 bool bta_hh_headtracker_supported(tBTA_HH_DEV_CB* p_dev_cb);
337 uint16_t bta_hh_get_uuid16(tBTA_HH_DEV_CB* p_dev_cb, bluetooth::Uuid uuid);
338 
339 #if (BTA_HH_DEBUG == TRUE)
340 void bta_hh_trace_dev_db(void);
341 #endif
342 
343 namespace fmt {
344 template <>
345 struct formatter<tBTA_HH_SERVICE_STATE>
346     : enum_formatter<tBTA_HH_SERVICE_STATE> {};
347 }  // namespace fmt
348 
349 #endif
350