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