Skip to content

Commit

Permalink
ec_host_cmd: introduce Host Command state
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
niedzwiecki-dawid authored and henrikbrixandersen committed Mar 6, 2024
1 parent 99fd397 commit 4d97dcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 */
Expand Down
12 changes: 12 additions & 0 deletions subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 4d97dcf

Please sign in to comment.