#include "bta_hh_int.h"
/*****************************************************************************
- * Constants and types
- ****************************************************************************/
-
-/* state machine action enumeration list */
-enum {
- BTA_HH_API_DISC_ACT, /* HID host process API close action */
- BTA_HH_OPEN_ACT, /* HID host process BTA_HH_EVT_OPEN */
- BTA_HH_CLOSE_ACT, /* HID host process BTA_HH_EVT_CLOSE */
- BTA_HH_DATA_ACT, /* HID host receive data report */
- BTA_HH_CTRL_DAT_ACT,
- BTA_HH_HANDSK_ACT,
- BTA_HH_START_SDP, /* HID host inquery */
- BTA_HH_SDP_CMPL,
- BTA_HH_WRITE_DEV_ACT,
- BTA_HH_GET_DSCP_ACT,
- BTA_HH_MAINT_DEV_ACT,
- BTA_HH_OPEN_CMPL_ACT,
- BTA_HH_OPEN_FAILURE,
- BTA_HH_GATT_CLOSE,
- BTA_HH_LE_OPEN_FAIL,
- BTA_HH_GATT_OPEN,
- BTA_HH_START_SEC,
- BTA_HH_SEC_CMPL,
- BTA_HH_GATT_ENC_CMPL,
- BTA_HH_NUM_ACTIONS
-};
-
-#define BTA_HH_IGNORE BTA_HH_NUM_ACTIONS
-
-/* type for action functions */
-typedef void (*tBTA_HH_ACTION)(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data);
-
-/* action functions */
-const tBTA_HH_ACTION bta_hh_action[] = {
- bta_hh_api_disc_act, bta_hh_open_act, bta_hh_close_act,
- bta_hh_data_act, bta_hh_ctrl_dat_act, bta_hh_handsk_act,
- bta_hh_start_sdp, bta_hh_sdp_cmpl, bta_hh_write_dev_act,
- bta_hh_get_dscp_act, bta_hh_maint_dev_act, bta_hh_open_cmpl_act,
- bta_hh_open_failure, bta_hh_gatt_close, bta_hh_le_open_fail,
- bta_hh_gatt_open, bta_hh_start_security, bta_hh_security_cmpl,
- bta_hh_le_notify_enc_cmpl};
-
-/* state table information */
-#define BTA_HH_ACTION 0 /* position of action */
-#define BTA_HH_NEXT_STATE 1 /* position of next state */
-#define BTA_HH_NUM_COLS 2 /* number of columns */
-
-/* state table for idle state */
-const uint8_t bta_hh_st_idle[][BTA_HH_NUM_COLS] = {
- /* Event Action Next state */
- /* BTA_HH_API_OPEN_EVT */ {BTA_HH_START_SDP, BTA_HH_W4_CONN_ST},
- /* BTA_HH_API_CLOSE_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_OPEN_EVT */ {BTA_HH_OPEN_ACT, BTA_HH_W4_CONN_ST},
- /* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_CLOSE_ACT, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_DATA_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_CTRL_DATA */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_IDLE_ST},
- /* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_OPEN_CMPL_ACT, BTA_HH_CONN_ST}
- /* BTA_HH_GATT_CLOSE_EVT */,
- {BTA_HH_IGNORE, BTA_HH_IDLE_ST}
- /* BTA_HH_GATT_OPEN_EVT */,
- {BTA_HH_GATT_OPEN, BTA_HH_W4_CONN_ST}
- /* BTA_HH_START_ENC_EVT */,
- {BTA_HH_IGNORE, BTA_HH_IDLE_ST}
- /* BTA_HH_ENC_CMPL_EVT */,
- {BTA_HH_IGNORE, BTA_HH_IDLE_ST}
- /* BTA_HH_GATT_ENC_CMPL_EVT */,
- {BTA_HH_IGNORE, BTA_HH_IDLE_ST}
-};
-
-const uint8_t bta_hh_st_w4_conn[][BTA_HH_NUM_COLS] = {
- /* Event Action Next state */
- /* BTA_HH_API_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST},
- /* BTA_HH_API_CLOSE_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_OPEN_EVT */ {BTA_HH_OPEN_ACT, BTA_HH_W4_CONN_ST},
- /* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_OPEN_FAILURE, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_DATA_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST},
- /* BTA_HH_INT_CTRL_DATA */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST},
- /* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST},
- /* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_SDP_CMPL, BTA_HH_W4_CONN_ST},
- /* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_WRITE_DEV_ACT, BTA_HH_W4_CONN_ST},
- /* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST},
- /* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_IDLE_ST},
- /* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_OPEN_CMPL_ACT, BTA_HH_CONN_ST}
- /* BTA_HH_GATT_CLOSE_EVT */,
- {BTA_HH_LE_OPEN_FAIL, BTA_HH_IDLE_ST}
- /* BTA_HH_GATT_OPEN_EVT */,
- {BTA_HH_GATT_OPEN, BTA_HH_W4_CONN_ST}
- /* BTA_HH_START_ENC_EVT */,
- {BTA_HH_START_SEC, BTA_HH_W4_SEC}
- /* BTA_HH_ENC_CMPL_EVT */,
- {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST}
- /* BTA_HH_GATT_ENC_CMPL_EVT */,
- {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST}
-};
-
-const uint8_t bta_hh_st_connected[][BTA_HH_NUM_COLS] = {
- /* Event Action Next state */
- /* BTA_HH_API_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_CONN_ST},
- /* BTA_HH_API_CLOSE_EVT */ {BTA_HH_API_DISC_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_INT_OPEN_EVT */ {BTA_HH_OPEN_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_CLOSE_ACT, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_DATA_EVT */ {BTA_HH_DATA_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_INT_CTRL_DATA */ {BTA_HH_CTRL_DAT_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_HANDSK_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_CONN_ST},
- /* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_WRITE_DEV_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_GET_DSCP_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_CONN_ST},
- /* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_CONN_ST}
- /* BTA_HH_GATT_CLOSE_EVT */,
- {BTA_HH_GATT_CLOSE, BTA_HH_IDLE_ST}
- /* BTA_HH_GATT_OPEN_EVT */,
- {BTA_HH_IGNORE, BTA_HH_CONN_ST}
- /* BTA_HH_START_ENC_EVT */,
- {BTA_HH_IGNORE, BTA_HH_CONN_ST}
- /* BTA_HH_ENC_CMPL_EVT */,
- {BTA_HH_IGNORE, BTA_HH_CONN_ST}
- /* BTA_HH_GATT_ENC_CMPL_EVT */,
- {BTA_HH_IGNORE, BTA_HH_CONN_ST}
-};
-
-const uint8_t bta_hh_st_w4_sec[][BTA_HH_NUM_COLS] = {
- /* Event Action Next state */
- /* BTA_HH_API_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_API_CLOSE_EVT */ {BTA_HH_API_DISC_ACT, BTA_HH_W4_SEC},
- /* BTA_HH_INT_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_OPEN_FAILURE, BTA_HH_IDLE_ST},
- /* BTA_HH_INT_DATA_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_INT_CTRL_DATA */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_W4_SEC},
- /* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_GATT_CLOSE_EVT */ {BTA_HH_LE_OPEN_FAIL, BTA_HH_IDLE_ST},
- /* BTA_HH_GATT_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_START_ENC_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC},
- /* BTA_HH_ENC_CMPL_EVT */ {BTA_HH_SEC_CMPL, BTA_HH_W4_CONN_ST},
- /* BTA_HH_GATT_ENC_CMPL_EVT */ {BTA_HH_GATT_ENC_CMPL, BTA_HH_W4_SEC}};
-
-/* type for state table */
-typedef const uint8_t (*tBTA_HH_ST_TBL)[BTA_HH_NUM_COLS];
-
-/* state table */
-const tBTA_HH_ST_TBL bta_hh_st_tbl[] = {bta_hh_st_idle, bta_hh_st_w4_conn,
- bta_hh_st_connected, bta_hh_st_w4_sec};
-
-/*****************************************************************************
* Global data
****************************************************************************/
tBTA_HH_CB bta_hh_cb;
static const char* bta_hh_state_code(tBTA_HH_STATE state_code);
#endif
+static void bta_hh_better_state_machine(tBTA_HH_DEV_CB* p_cb, uint16_t event,
+ tBTA_HH_DATA* p_data) {
+ switch (p_cb->state) {
+ case BTA_HH_IDLE_ST:
+ switch (event) {
+ case BTA_HH_API_OPEN_EVT:
+ p_cb->state = BTA_HH_W4_CONN_ST;
+ bta_hh_start_sdp(p_cb, p_data);
+ break;
+ case BTA_HH_INT_OPEN_EVT:
+ p_cb->state = BTA_HH_W4_CONN_ST;
+ bta_hh_open_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_CLOSE_EVT:
+ bta_hh_close_act(p_cb, p_data);
+ break;
+ case BTA_HH_API_MAINT_DEV_EVT:
+ bta_hh_maint_dev_act(p_cb, p_data);
+ break;
+ case BTA_HH_OPEN_CMPL_EVT:
+ p_cb->state = BTA_HH_CONN_ST;
+ bta_hh_open_cmpl_act(p_cb, p_data);
+ break;
+ case BTA_HH_GATT_OPEN_EVT:
+ p_cb->state = BTA_HH_W4_CONN_ST;
+ bta_hh_gatt_open(p_cb, p_data);
+ break;
+ }
+ break;
+ case BTA_HH_W4_CONN_ST:
+ switch (event) {
+ case BTA_HH_API_CLOSE_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ break;
+ case BTA_HH_INT_OPEN_EVT:
+ bta_hh_open_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_CLOSE_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ bta_hh_open_failure(p_cb, p_data);
+ break;
+ case BTA_HH_SDP_CMPL_EVT:
+ bta_hh_sdp_cmpl(p_cb, p_data);
+ break;
+ case BTA_HH_API_WRITE_DEV_EVT:
+ bta_hh_write_dev_act(p_cb, p_data);
+ break;
+ case BTA_HH_API_MAINT_DEV_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ bta_hh_maint_dev_act(p_cb, p_data);
+ break;
+ case BTA_HH_OPEN_CMPL_EVT:
+ p_cb->state = BTA_HH_CONN_ST;
+ bta_hh_open_cmpl_act(p_cb, p_data);
+ break;
+ case BTA_HH_GATT_CLOSE_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ bta_hh_le_open_fail(p_cb, p_data);
+ break;
+ case BTA_HH_GATT_OPEN_EVT:
+ bta_hh_gatt_open(p_cb, p_data);
+ break;
+ case BTA_HH_START_ENC_EVT:
+ p_cb->state = BTA_HH_W4_SEC;
+ bta_hh_start_security(p_cb, p_data);
+ break;
+ }
+ break;
+ case BTA_HH_CONN_ST:
+ switch (event) {
+ case BTA_HH_API_CLOSE_EVT:
+ bta_hh_api_disc_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_OPEN_EVT:
+ bta_hh_open_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_CLOSE_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ bta_hh_close_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_DATA_EVT:
+ bta_hh_data_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_CTRL_DATA:
+ bta_hh_ctrl_dat_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_HANDSK_EVT:
+ bta_hh_handsk_act(p_cb, p_data);
+ break;
+ case BTA_HH_API_WRITE_DEV_EVT:
+ bta_hh_write_dev_act(p_cb, p_data);
+ break;
+ case BTA_HH_API_GET_DSCP_EVT:
+ bta_hh_get_dscp_act(p_cb, p_data);
+ break;
+ case BTA_HH_API_MAINT_DEV_EVT:
+ bta_hh_maint_dev_act(p_cb, p_data);
+ break;
+ case BTA_HH_GATT_CLOSE_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ bta_hh_gatt_close(p_cb, p_data);
+ break;
+ }
+ break;
+ case BTA_HH_W4_SEC:
+ switch (event) {
+ case BTA_HH_API_CLOSE_EVT:
+ bta_hh_api_disc_act(p_cb, p_data);
+ break;
+ case BTA_HH_INT_CLOSE_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ bta_hh_open_failure(p_cb, p_data);
+ break;
+ case BTA_HH_API_MAINT_DEV_EVT:
+ bta_hh_maint_dev_act(p_cb, p_data);
+ break;
+ case BTA_HH_GATT_CLOSE_EVT:
+ p_cb->state = BTA_HH_IDLE_ST;
+ bta_hh_le_open_fail(p_cb, p_data);
+ break;
+ case BTA_HH_ENC_CMPL_EVT:
+ p_cb->state = BTA_HH_W4_CONN_ST;
+ bta_hh_security_cmpl(p_cb, p_data);
+ break;
+ case BTA_HH_GATT_ENC_CMPL_EVT:
+ bta_hh_le_notify_enc_cmpl(p_cb, p_data);
+ break;
+ }
+ break;
+ }
+}
+
/*******************************************************************************
*
* Function bta_hh_sm_execute
******************************************************************************/
void bta_hh_sm_execute(tBTA_HH_DEV_CB* p_cb, uint16_t event,
tBTA_HH_DATA* p_data) {
- tBTA_HH_ST_TBL state_table;
- uint8_t action;
tBTA_HH cback_data;
tBTA_HH_EVT cback_event = 0;
#if (BTA_HH_DEBUG == TRUE)
p_cb->state, event);
return;
}
- state_table = bta_hh_st_tbl[p_cb->state - 1];
-
- event &= 0xff;
- p_cb->state = state_table[event][BTA_HH_NEXT_STATE];
-
- action = state_table[event][BTA_HH_ACTION];
- if (action != BTA_HH_IGNORE) {
- (*bta_hh_action[action])(p_cb, p_data);
- }
+ bta_hh_better_state_machine(p_cb, event, p_data);
#if (BTA_HH_DEBUG == TRUE)
if (in_state != p_cb->state) {