diff --git a/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h b/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h index cc0127938e34..50bc3fb77809 100644 --- a/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h +++ b/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h @@ -78,6 +78,13 @@ enum ec_host_cmd_log_level { EC_HOST_CMD_DEBUG_MODES /* Number of host command debug modes */ }; +enum ec_host_cmd_state { + EC_HOST_CMD_STATE_DISABLED = 0, + EC_HOST_CMD_STATE_RECEIVING, + EC_HOST_CMD_STATE_PROCESSING, + EC_HOST_CMD_STATE_SENDING, +}; + typedef void (*ec_host_cmd_user_cb_t)(const struct ec_host_cmd_rx_ctx *rx_ctx, void *user_data); typedef enum ec_host_cmd_status (*ec_host_cmd_in_progress_cb_t)(void *user_data); @@ -98,6 +105,7 @@ struct ec_host_cmd { */ ec_host_cmd_user_cb_t user_cb; void *user_data; + enum ec_host_cmd_state state; #ifdef CONFIG_EC_HOST_CMD_DEDICATED_THREAD struct k_thread thread; #endif /* CONFIG_EC_HOST_CMD_DEDICATED_THREAD */ diff --git a/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c b/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c index b628c891a5bf..265c5bc854c2 100644 --- a/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c +++ b/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c @@ -292,6 +292,12 @@ int ec_host_cmd_send_response(enum ec_host_cmd_status status, struct ec_host_cmd *hc = &ec_host_cmd; struct ec_host_cmd_tx_buf *tx = &hc->tx; + if (hc->state != EC_HOST_CMD_STATE_PROCESSING) { + LOG_ERR("Unexpected state while sending"); + return -ENOTSUP; + } + hc->state = EC_HOST_CMD_STATE_SENDING; + if (status != EC_HOST_CMD_SUCCESS) { const struct ec_host_cmd_request_header *const rx_header = (const struct ec_host_cmd_request_header *const)hc->rx_ctx.buf; @@ -390,9 +396,13 @@ FUNC_NORETURN static void ec_host_cmd_thread(void *hc_handle, void *arg2, void * .reserved = NULL, }; + __ASSERT(hc->state != EC_HOST_CMD_STATE_DISABLED, "HC backend not initialized"); + while (1) { + hc->state = EC_HOST_CMD_STATE_RECEIVING; /* Wait until RX messages is received on host interface */ k_sem_take(&hc->rx_ready, K_FOREVER); + hc->state = EC_HOST_CMD_STATE_PROCESSING; ec_host_cmd_log_request(rx->buf); @@ -485,6 +495,8 @@ int ec_host_cmd_init(struct ec_host_cmd_backend *backend) return -EIO; } + hc->state = EC_HOST_CMD_STATE_RECEIVING; + /* Check if a backend uses provided buffers. The buffer pointers can be shifted within the * buffer to make space for preamble. Make sure the rx/tx pointers are within the provided * buffers ranges.