1 /******************************************************************************
3 * Copyright 2005-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the HID host main functions and state machine.
23 ******************************************************************************/
25 #include <string.h> // memset
28 #include "bt_target.h" // Must be first to define build configuration
30 #if (BTA_HH_INCLUDED == TRUE)
32 #include "bta/hh/bta_hh_int.h"
34 /*****************************************************************************
36 ****************************************************************************/
39 /*****************************************************************************
41 ****************************************************************************/
42 static const char* bta_hh_evt_code(tBTA_HH_INT_EVT evt_code);
43 static const char* bta_hh_state_code(tBTA_HH_STATE state_code);
45 static void bta_hh_better_state_machine(tBTA_HH_DEV_CB* p_cb, uint16_t event,
46 tBTA_HH_DATA* p_data) {
47 switch (p_cb->state) {
50 case BTA_HH_API_OPEN_EVT:
51 p_cb->state = BTA_HH_W4_CONN_ST;
52 bta_hh_start_sdp(p_cb, p_data);
54 case BTA_HH_INT_OPEN_EVT:
55 p_cb->state = BTA_HH_W4_CONN_ST;
56 bta_hh_open_act(p_cb, p_data);
58 case BTA_HH_INT_CLOSE_EVT:
59 bta_hh_close_act(p_cb, p_data);
61 case BTA_HH_API_MAINT_DEV_EVT:
62 bta_hh_maint_dev_act(p_cb, p_data);
64 case BTA_HH_OPEN_CMPL_EVT:
65 p_cb->state = BTA_HH_CONN_ST;
66 bta_hh_open_cmpl_act(p_cb, p_data);
68 case BTA_HH_GATT_OPEN_EVT:
69 p_cb->state = BTA_HH_W4_CONN_ST;
70 bta_hh_gatt_open(p_cb, p_data);
74 case BTA_HH_W4_CONN_ST:
76 case BTA_HH_API_CLOSE_EVT:
77 p_cb->state = BTA_HH_IDLE_ST;
79 case BTA_HH_INT_OPEN_EVT:
80 bta_hh_open_act(p_cb, p_data);
82 case BTA_HH_INT_CLOSE_EVT:
83 p_cb->state = BTA_HH_IDLE_ST;
84 bta_hh_open_failure(p_cb, p_data);
86 case BTA_HH_SDP_CMPL_EVT:
87 bta_hh_sdp_cmpl(p_cb, p_data);
89 case BTA_HH_API_WRITE_DEV_EVT:
90 bta_hh_write_dev_act(p_cb, p_data);
92 case BTA_HH_API_MAINT_DEV_EVT:
93 p_cb->state = BTA_HH_IDLE_ST;
94 bta_hh_maint_dev_act(p_cb, p_data);
96 case BTA_HH_OPEN_CMPL_EVT:
97 p_cb->state = BTA_HH_CONN_ST;
98 bta_hh_open_cmpl_act(p_cb, p_data);
100 case BTA_HH_GATT_CLOSE_EVT:
101 p_cb->state = BTA_HH_IDLE_ST;
102 bta_hh_le_open_fail(p_cb, p_data);
104 case BTA_HH_GATT_OPEN_EVT:
105 bta_hh_gatt_open(p_cb, p_data);
107 case BTA_HH_START_ENC_EVT:
108 p_cb->state = BTA_HH_W4_SEC;
109 bta_hh_start_security(p_cb, p_data);
115 case BTA_HH_API_CLOSE_EVT:
116 bta_hh_api_disc_act(p_cb, p_data);
118 case BTA_HH_INT_OPEN_EVT:
119 bta_hh_open_act(p_cb, p_data);
121 case BTA_HH_INT_CLOSE_EVT:
122 p_cb->state = BTA_HH_IDLE_ST;
123 bta_hh_close_act(p_cb, p_data);
125 case BTA_HH_INT_DATA_EVT:
126 bta_hh_data_act(p_cb, p_data);
128 case BTA_HH_INT_CTRL_DATA:
129 bta_hh_ctrl_dat_act(p_cb, p_data);
131 case BTA_HH_INT_HANDSK_EVT:
132 bta_hh_handsk_act(p_cb, p_data);
134 case BTA_HH_API_WRITE_DEV_EVT:
135 bta_hh_write_dev_act(p_cb, p_data);
137 case BTA_HH_API_GET_DSCP_EVT:
138 bta_hh_get_dscp_act(p_cb, p_data);
140 case BTA_HH_API_MAINT_DEV_EVT:
141 bta_hh_maint_dev_act(p_cb, p_data);
143 case BTA_HH_GATT_CLOSE_EVT:
144 p_cb->state = BTA_HH_IDLE_ST;
145 bta_hh_gatt_close(p_cb, p_data);
151 case BTA_HH_API_CLOSE_EVT:
152 bta_hh_api_disc_act(p_cb, p_data);
154 case BTA_HH_INT_CLOSE_EVT:
155 p_cb->state = BTA_HH_IDLE_ST;
156 bta_hh_open_failure(p_cb, p_data);
158 case BTA_HH_API_MAINT_DEV_EVT:
159 bta_hh_maint_dev_act(p_cb, p_data);
161 case BTA_HH_GATT_CLOSE_EVT:
162 p_cb->state = BTA_HH_IDLE_ST;
163 bta_hh_le_open_fail(p_cb, p_data);
165 case BTA_HH_ENC_CMPL_EVT:
166 p_cb->state = BTA_HH_W4_CONN_ST;
167 bta_hh_security_cmpl(p_cb, p_data);
169 case BTA_HH_GATT_ENC_CMPL_EVT:
170 bta_hh_le_notify_enc_cmpl(p_cb, p_data);
177 /*******************************************************************************
179 * Function bta_hh_sm_execute
181 * Description State machine event handling function for HID Host
186 ******************************************************************************/
187 void bta_hh_sm_execute(tBTA_HH_DEV_CB* p_cb, uint16_t event,
188 tBTA_HH_DATA* p_data) {
190 tBTA_HH_EVT cback_event = 0;
191 tBTA_HH_STATE in_state;
192 uint16_t debug_event = event;
194 memset(&cback_data, 0, sizeof(tBTA_HH));
196 /* handle exception, no valid control block was found */
198 /* BTA HH enabled already? otherwise ignore the event although it's bad*/
199 if (bta_hh_cb.p_cback != NULL) {
201 /* no control block available for new connection */
202 case BTA_HH_API_OPEN_EVT:
203 cback_event = BTA_HH_OPEN_EVT;
204 /* build cback data */
205 cback_data.conn.bda = ((tBTA_HH_API_CONN*)p_data)->bd_addr;
206 cback_data.conn.status = BTA_HH_ERR_DB_FULL;
207 cback_data.conn.handle = BTA_HH_INVALID_HANDLE;
209 /* DB full, BTA_HhAddDev */
210 case BTA_HH_API_MAINT_DEV_EVT:
211 cback_event = p_data->api_maintdev.sub_event;
213 if (p_data->api_maintdev.sub_event == BTA_HH_ADD_DEV_EVT) {
214 cback_data.dev_info.bda = p_data->api_maintdev.bda;
215 cback_data.dev_info.status = BTA_HH_ERR_DB_FULL;
216 cback_data.dev_info.handle = BTA_HH_INVALID_HANDLE;
218 cback_data.dev_info.status = BTA_HH_ERR_HDL;
219 cback_data.dev_info.handle =
220 (uint8_t)p_data->api_maintdev.hdr.layer_specific;
223 case BTA_HH_API_WRITE_DEV_EVT:
224 cback_event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
225 BTA_HH_FST_TRANS_CB_EVT;
226 osi_free_and_reset((void**)&p_data->api_sndcmd.p_data);
227 if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL ||
228 p_data->api_sndcmd.t_type == HID_TRANS_SET_REPORT ||
229 p_data->api_sndcmd.t_type == HID_TRANS_SET_IDLE) {
230 cback_data.dev_status.status = BTA_HH_ERR_HDL;
231 cback_data.dev_status.handle =
232 (uint8_t)p_data->api_sndcmd.hdr.layer_specific;
233 } else if (p_data->api_sndcmd.t_type != HID_TRANS_DATA &&
234 p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
235 cback_data.hs_data.handle =
236 (uint8_t)p_data->api_sndcmd.hdr.layer_specific;
237 cback_data.hs_data.status = BTA_HH_ERR_HDL;
238 /* hs_data.rsp_data will be all zero, which is not valid value */
239 } else if (p_data->api_sndcmd.t_type == HID_TRANS_CONTROL &&
240 p_data->api_sndcmd.param ==
241 BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG) {
242 cback_data.status = BTA_HH_ERR_HDL;
243 cback_event = BTA_HH_VC_UNPLUG_EVT;
248 case BTA_HH_API_CLOSE_EVT:
249 cback_event = BTA_HH_CLOSE_EVT;
251 cback_data.dev_status.status = BTA_HH_ERR_HDL;
252 cback_data.dev_status.handle =
253 (uint8_t)p_data->api_sndcmd.hdr.layer_specific;
257 /* invalid handle, call bad API event */
258 APPL_TRACE_ERROR("wrong device handle: [%d]",
259 p_data->hdr.layer_specific);
260 /* Free the callback buffer now */
262 osi_free_and_reset((void**)&p_data->hid_cback.p_data);
265 if (cback_event) (*bta_hh_cb.p_cback)(cback_event, &cback_data);
268 /* corresponding CB is found, go to state machine */
270 in_state = p_cb->state;
271 APPL_TRACE_EVENT("bta_hh_sm_execute: State 0x%02x [%s], Event [%s]",
272 in_state, bta_hh_state_code(in_state),
273 bta_hh_evt_code(debug_event));
275 if ((p_cb->state == BTA_HH_NULL_ST) || (p_cb->state >= BTA_HH_INVALID_ST)) {
277 "bta_hh_sm_execute: Invalid state State = 0x%x, Event = %d",
282 bta_hh_better_state_machine(p_cb, event, p_data);
284 if (in_state != p_cb->state) {
285 APPL_TRACE_DEBUG("HH State Change: [%s] -> [%s] after Event [%s]",
286 bta_hh_state_code(in_state),
287 bta_hh_state_code(p_cb->state),
288 bta_hh_evt_code(debug_event));
294 /*******************************************************************************
296 * Function bta_hh_hdl_event
298 * Description HID host main event handling function.
303 ******************************************************************************/
304 bool bta_hh_hdl_event(BT_HDR_RIGID* p_msg) {
305 uint8_t index = BTA_HH_IDX_INVALID;
306 tBTA_HH_DEV_CB* p_cb = NULL;
308 switch (p_msg->event) {
309 case BTA_HH_API_ENABLE_EVT:
310 bta_hh_api_enable((tBTA_HH_DATA*)p_msg);
313 case BTA_HH_API_DISABLE_EVT:
314 bta_hh_api_disable();
317 case BTA_HH_DISC_CMPL_EVT: /* disable complete */
322 /* all events processed in state machine need to find corresponding
324 if (p_msg->event == BTA_HH_API_OPEN_EVT) {
325 index = bta_hh_find_cb(((tBTA_HH_API_CONN*)p_msg)->bd_addr);
326 } else if (p_msg->event == BTA_HH_API_MAINT_DEV_EVT) {
328 if (((tBTA_HH_MAINT_DEV*)p_msg)->sub_event == BTA_HH_ADD_DEV_EVT) {
329 index = bta_hh_find_cb(((tBTA_HH_MAINT_DEV*)p_msg)->bda);
330 } else /* else remove device by handle */
332 index = bta_hh_dev_handle_to_cb_idx((uint8_t)p_msg->layer_specific);
333 /* If BT disable is done while the HID device is connected and
334 * Link_Key uses unauthenticated combination
335 * then we can get into a situation where remove_bonding is called
336 * with the index set to 0 (without getting
337 * cleaned up). Only when VIRTUAL_UNPLUG is called do we cleanup the
338 * index and make it MAX_KNOWN.
339 * So if REMOVE_DEVICE is called and in_use is false then we should
340 * treat this as a NULL p_cb. Hence we
341 * force the index to be IDX_INVALID
343 if ((index != BTA_HH_IDX_INVALID) &&
344 (!bta_hh_cb.kdev[index].in_use)) {
345 index = BTA_HH_IDX_INVALID;
348 } else if (p_msg->event == BTA_HH_INT_OPEN_EVT) {
349 index = bta_hh_find_cb(((tBTA_HH_CBACK_DATA*)p_msg)->addr);
351 index = bta_hh_dev_handle_to_cb_idx((uint8_t)p_msg->layer_specific);
353 if (index != BTA_HH_IDX_INVALID) p_cb = &bta_hh_cb.kdev[index];
355 APPL_TRACE_DEBUG("bta_hh_hdl_event:: handle = %d dev_cb[%d] ",
356 p_msg->layer_specific, index);
357 bta_hh_sm_execute(p_cb, p_msg->event, (tBTA_HH_DATA*)p_msg);
362 /*****************************************************************************
364 ****************************************************************************/
365 /*******************************************************************************
367 * Function bta_hh_evt_code
373 ******************************************************************************/
374 static const char* bta_hh_evt_code(tBTA_HH_INT_EVT evt_code) {
376 case BTA_HH_API_DISABLE_EVT:
377 return "BTA_HH_API_DISABLE_EVT";
378 case BTA_HH_API_ENABLE_EVT:
379 return "BTA_HH_API_ENABLE_EVT";
380 case BTA_HH_API_OPEN_EVT:
381 return "BTA_HH_API_OPEN_EVT";
382 case BTA_HH_API_CLOSE_EVT:
383 return "BTA_HH_API_CLOSE_EVT";
384 case BTA_HH_INT_OPEN_EVT:
385 return "BTA_HH_INT_OPEN_EVT";
386 case BTA_HH_INT_CLOSE_EVT:
387 return "BTA_HH_INT_CLOSE_EVT";
388 case BTA_HH_INT_HANDSK_EVT:
389 return "BTA_HH_INT_HANDSK_EVT";
390 case BTA_HH_INT_DATA_EVT:
391 return "BTA_HH_INT_DATA_EVT";
392 case BTA_HH_INT_CTRL_DATA:
393 return "BTA_HH_INT_CTRL_DATA";
394 case BTA_HH_API_WRITE_DEV_EVT:
395 return "BTA_HH_API_WRITE_DEV_EVT";
396 case BTA_HH_SDP_CMPL_EVT:
397 return "BTA_HH_SDP_CMPL_EVT";
398 case BTA_HH_DISC_CMPL_EVT:
399 return "BTA_HH_DISC_CMPL_EVT";
400 case BTA_HH_API_MAINT_DEV_EVT:
401 return "BTA_HH_API_MAINT_DEV_EVT";
402 case BTA_HH_API_GET_DSCP_EVT:
403 return "BTA_HH_API_GET_DSCP_EVT";
404 case BTA_HH_OPEN_CMPL_EVT:
405 return "BTA_HH_OPEN_CMPL_EVT";
406 case BTA_HH_GATT_CLOSE_EVT:
407 return "BTA_HH_GATT_CLOSE_EVT";
408 case BTA_HH_GATT_OPEN_EVT:
409 return "BTA_HH_GATT_OPEN_EVT";
410 case BTA_HH_START_ENC_EVT:
411 return "BTA_HH_START_ENC_EVT";
412 case BTA_HH_ENC_CMPL_EVT:
413 return "BTA_HH_ENC_CMPL_EVT";
415 return "unknown HID Host event code";
419 /*******************************************************************************
421 * Function bta_hh_state_code
423 * Description get string representation of HID host state code.
427 ******************************************************************************/
428 static const char* bta_hh_state_code(tBTA_HH_STATE state_code) {
429 switch (state_code) {
431 return "BTA_HH_NULL_ST";
433 return "BTA_HH_IDLE_ST";
434 case BTA_HH_W4_CONN_ST:
435 return "BTA_HH_W4_CONN_ST";
437 return "BTA_HH_CONN_ST";
439 return "BTA_HH_W4_SEC";
441 return "unknown HID Host state";
445 #endif /* BTA_HH_INCLUDED */