• 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 <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   bool direct;
92 } tBTA_HH_API_CONN;
93 
94 /* internal event data from BTE HID callback */
95 typedef struct {
96   BT_HDR_RIGID hdr;
97   tAclLinkSpec link_spec;
98   uint32_t data;
99   BT_HDR* p_data;
100 } tBTA_HH_CBACK_DATA;
101 
102 typedef struct {
103   BT_HDR_RIGID hdr;
104   tAclLinkSpec link_spec;
105   uint16_t attr_mask;
106   uint16_t sub_event;
107   uint8_t sub_class;
108   uint8_t app_id;
109   tBTA_HH_DEV_DSCP_INFO dscp_info;
110 } tBTA_HH_MAINT_DEV;
111 
112 typedef struct {
113   BT_HDR_RIGID hdr;
114   tCONN_ID conn_id;
115   tGATT_DISCONN_REASON reason;
116 } tBTA_HH_LE_CLOSE;
117 
118 typedef struct {
119   BT_HDR_RIGID hdr;
120   uint16_t scan_int;
121   uint16_t scan_win;
122 } tBTA_HH_SCPP_UPDATE;
123 
124 /* union of all event data types */
125 typedef union {
126   BT_HDR_RIGID hdr;
127   tBTA_HH_API_CONN api_conn;
128   tBTA_HH_CMD_DATA api_sndcmd;
129   tBTA_HH_CBACK_DATA hid_cback;
130   tBTA_HH_STATUS status;
131   tBTA_HH_MAINT_DEV api_maintdev;
132   tBTA_HH_LE_CLOSE le_close;
133   tBTA_GATTC_OPEN le_open;
134   tBTA_HH_SCPP_UPDATE le_scpp_update;
135   tBTA_GATTC_ENC_CMPL_CB le_enc_cmpl;
136 } tBTA_HH_DATA;
137 
138 typedef struct {
139   uint8_t index;
140   bool in_use;
141   uint8_t srvc_inst_id;
142   uint16_t char_inst_id;
143   tBTA_HH_RPT_TYPE rpt_type;
144   uint16_t uuid;
145   uint8_t rpt_id;
146   bool client_cfg_exist;
147   uint16_t client_cfg_value;
148 } tBTA_HH_LE_RPT;
149 
150 #ifndef BTA_HH_LE_RPT_MAX
151 #define BTA_HH_LE_RPT_MAX 20
152 #endif
153 
154 enum tBTA_HH_SERVICE_STATE {
155   BTA_HH_SERVICE_UNKNOWN,
156   BTA_HH_SERVICE_CHANGED,
157   BTA_HH_SERVICE_DISCOVERED,
158 };
159 
160 enum tBTA_HH_AVAILABLE { BTA_HH_UNKNOWN = 0, BTA_HH_AVAILABLE, BTA_HH_UNAVAILABLE };
161 
162 typedef struct {
163   tBTA_HH_SERVICE_STATE state;
164   uint8_t srvc_inst_id;
165   tBTA_HH_LE_RPT report[BTA_HH_LE_RPT_MAX];
166 
167   uint16_t proto_mode_handle;
168   uint8_t control_point_handle;
169 
170   uint8_t 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   tBTA_HH_AVAILABLE headtracker_support;
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 the device 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   tAclLinkSpec link_spec;          /* ACL link specification 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   tBTA_HH_LE_HID_SRVC hid_srvc;
216   tCONN_ID conn_id;
217   bool in_bg_conn;
218   uint8_t clt_cfg_idx;
219   bool scps_supported;
220 
221 #define BTA_HH_LE_SCPS_NOTIFY_NONE 0
222 #define BTA_HH_LE_SCPS_NOTIFY_SPT 0x01
223 #define BTA_HH_LE_SCPS_NOTIFY_ENB 0x02
224   uint8_t scps_notify; /* scan refresh supported/notification enabled */
225   bool security_pending;
226 
227   tSDP_DISCOVERY_DB* p_disc_db;
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   uint8_t cb_index[BTA_HH_MAX_KNOWN];       /* maintain a CB index
236                                           map to dev handle */
237   uint8_t le_cb_index[BTA_HH_LE_MAX_KNOWN]; /* maintain a CB index map to LE dev
238                                              handle */
239   tGATT_IF gatt_if;
240   tBTA_HH_CBACK* p_cback; /* Application callbacks */
241   uint8_t cnt_num; /* connected device number */
242   bool w4_disable; /* w4 disable flag */
243 } tBTA_HH_CB;
244 
245 extern tBTA_HH_CB bta_hh_cb;
246 
247 /* from bta_hh_cfg.c */
248 extern tBTA_HH_CFG* p_bta_hh_cfg;
249 
250 /*****************************************************************************
251  *  Function prototypes
252  ****************************************************************************/
253 bool bta_hh_hdl_event(const BT_HDR_RIGID* p_msg);
254 void bta_hh_sm_execute(tBTA_HH_DEV_CB* p_cb, tBTA_HH_INT_EVT event, const tBTA_HH_DATA* p_data);
255 
256 /* action functions */
257 void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
258 void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
259 void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
260 void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
261 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
262 void bta_hh_connect(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
263 void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
264 void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
265 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
266 void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
267 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
268 void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
269 void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
270 
271 /* utility functions */
272 tBTA_HH_DEV_CB* bta_hh_find_cb(const tAclLinkSpec& link_spec);
273 tBTA_HH_DEV_CB* bta_hh_get_cb(const tAclLinkSpec& link_spec);
274 tBTA_HH_DEV_CB* bta_hh_find_cb_by_handle(uint8_t hid_handle);
275 bool bta_hh_tod_spt(tBTA_HH_DEV_CB* p_cb, uint8_t sub_class);
276 void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB* p_cb);
277 
278 void bta_hh_add_device_to_list(tBTA_HH_DEV_CB* p_cb, uint8_t handle, uint16_t attr_mask,
279                                const tHID_DEV_DSCP_INFO* p_dscp_info, uint8_t sub_class,
280                                uint16_t max_latency, uint16_t min_tout, uint8_t app_id);
281 void bta_hh_update_di_info(tBTA_HH_DEV_CB* p_cb, uint16_t vendor_id, uint16_t product_id,
282                            uint16_t version, uint8_t flag, uint8_t ctry_code);
283 void bta_hh_cleanup_disable(tBTA_HH_STATUS status);
284 
285 /* action functions used outside state machine */
286 void bta_hh_api_enable(tBTA_HH_CBACK* p_cback, bool enable_hid, bool enable_hogp);
287 void bta_hh_api_disable(void);
288 void bta_hh_disc_cmpl(void);
289 
290 tBTA_HH_STATUS bta_hh_read_ssr_param(const tAclLinkSpec& link_spec, uint16_t* p_max_ssr_lat,
291                                      uint16_t* p_min_ssr_tout);
292 
293 /* functions for LE HID */
294 void bta_hh_le_enable(void);
295 void bta_hh_le_deregister(void);
296 void bta_hh_le_open_conn(tBTA_HH_DEV_CB* p_cb, bool direct);
297 void bta_hh_le_api_disc_act(tBTA_HH_DEV_CB* p_cb);
298 void bta_hh_le_get_dscp_act(tBTA_HH_DEV_CB* p_cb);
299 void bta_hh_le_write_dev_act(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
300 uint8_t bta_hh_le_add_device(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_MAINT_DEV* p_dev_info);
301 void bta_hh_le_remove_dev_bg_conn(tBTA_HH_DEV_CB* p_cb);
302 void bta_hh_le_open_fail(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
303 void bta_hh_gatt_open(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
304 void bta_hh_gatt_close(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
305 void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_buf);
306 
307 void bta_hh_start_security(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_buf);
308 void bta_hh_security_cmpl(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_buf);
309 void bta_hh_le_notify_enc_cmpl(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_data);
310 
311 tBTA_HH_LE_RPT* bta_hh_le_find_alloc_report_entry(tBTA_HH_DEV_CB* p_cb, uint8_t srvc_inst_id,
312                                                   uint16_t rpt_uuid, uint16_t inst_id);
313 void bta_hh_le_save_report_ref(tBTA_HH_DEV_CB* p_dev_cb, tBTA_HH_LE_RPT* p_rpt, uint8_t rpt_type,
314                                uint8_t rpt_id);
315 void bta_hh_le_srvc_init(tBTA_HH_DEV_CB* p_dev_cb, uint16_t handle);
316 void bta_hh_le_save_report_map(tBTA_HH_DEV_CB* p_dev_cb, uint16_t len, uint8_t* desc);
317 void bta_hh_le_service_parsed(tBTA_HH_DEV_CB* p_dev_cb, tGATT_STATUS status);
318 
319 void bta_hh_headtracker_parse_service(tBTA_HH_DEV_CB* p_dev_cb, const gatt::Service* service);
320 bool bta_hh_headtracker_supported(tBTA_HH_DEV_CB* p_dev_cb);
321 uint16_t bta_hh_get_uuid16(tBTA_HH_DEV_CB* p_dev_cb, bluetooth::Uuid uuid);
322 
323 void bta_hh_dump(int fd);
324 
325 #if (BTA_HH_DEBUG == TRUE)
326 void bta_hh_trace_dev_db(void);
327 #endif
328 
329 namespace std {
330 template <>
331 struct formatter<tBTA_HH_SERVICE_STATE> : enum_formatter<tBTA_HH_SERVICE_STATE> {};
332 }  // namespace std
333 
334 #endif
335