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