ec_host_cmd: introduce Host Command state

Introduce a current state of Host Command subsystem.

It makes sures that a backend has been initilized and doesn't allow
sending a response twice. There is a possibility, that a command handler
that calls ec_host_cmd_send_response function returns anyway (which is a
mistake).

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
This commit is contained in:
Dawid Niedzwiecki 2024-02-28 12:33:00 +01:00 committed by Henrik Brix Andersen
parent 99fd397147
commit 4d97dcf115
2 changed files with 20 additions and 0 deletions

View file

@ -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 */

View file

@ -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.