Skip to content

Commit

Permalink
mgmt: ec_host_cmd: add user callback for a new command
Browse files Browse the repository at this point in the history
Add a user possibility to set a callback for receiving a new function.
It allows instant performing some actions, that need to be done before
context switch.

Signed-off-by: Dawid Niedzwiecki <[email protected]>
  • Loading branch information
niedzwiecki-dawid committed Jul 3, 2023
1 parent d69d00c commit 84143a1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 50 deletions.
126 changes: 76 additions & 50 deletions include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,66 @@
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/iterable_sections.h>

/**
* @brief Host command response codes (16-bit).
*/
enum ec_host_cmd_status {
/** Host command was successful. */
EC_HOST_CMD_SUCCESS = 0,
/** The specified command id is not recognized or supported. */
EC_HOST_CMD_INVALID_COMMAND = 1,
/** Generic Error. */
EC_HOST_CMD_ERROR = 2,
/** One of more of the input request parameters is invalid. */
EC_HOST_CMD_INVALID_PARAM = 3,
/** Host command is not permitted. */
EC_HOST_CMD_ACCESS_DENIED = 4,
/** Response was invalid (e.g. not version 3 of header). */
EC_HOST_CMD_INVALID_RESPONSE = 5,
/** Host command id version unsupported. */
EC_HOST_CMD_INVALID_VERSION = 6,
/** Checksum did not match. */
EC_HOST_CMD_INVALID_CHECKSUM = 7,
/** A host command is currently being processed. */
EC_HOST_CMD_IN_PROGRESS = 8,
/** Requested information is currently unavailable. */
EC_HOST_CMD_UNAVAILABLE = 9,
/** Timeout during processing. */
EC_HOST_CMD_TIMEOUT = 10,
/** Data or table overflow. */
EC_HOST_CMD_OVERFLOW = 11,
/** Header is invalid or unsupported (e.g. not version 3 of header). */
EC_HOST_CMD_INVALID_HEADER = 12,
/** Did not receive all expected request data. */
EC_HOST_CMD_REQUEST_TRUNCATED = 13,
/** Response was too big to send within one response packet. */
EC_HOST_CMD_RESPONSE_TOO_BIG = 14,
/** Error on underlying communication bus. */
EC_HOST_CMD_BUS_ERROR = 15,
/** System busy. Should retry later. */
EC_HOST_CMD_BUSY = 16,
/** Header version invalid. */
EC_HOST_CMD_INVALID_HEADER_VERSION = 17,
/** Header CRC invalid. */
EC_HOST_CMD_INVALID_HEADER_CRC = 18,
/** Data CRC invalid. */
EC_HOST_CMD_INVALID_DATA_CRC = 19,
/** Can't resend response. */
EC_HOST_CMD_DUP_UNAVAILABLE = 20,

EC_HOST_CMD_MAX = UINT16_MAX /* Force enum to be 16 bits. */
} __packed;

enum ec_host_cmd_log_level {
EC_HOST_CMD_DEBUG_OFF, /* No Host Command debug output */
EC_HOST_CMD_DEBUG_NORMAL, /* Normal output mode; skips repeated commands */
EC_HOST_CMD_DEBUG_EVERY, /* Print every command */
EC_HOST_CMD_DEBUG_PARAMS, /* ... and print params for request/response */
EC_HOST_CMD_DEBUG_MODES /* Number of host command debug modes */
};

typedef void (*ec_host_cmd_user_cb_t)(const struct ec_host_cmd_rx_ctx *rx_ctx, void *user_data);

struct ec_host_cmd {
struct ec_host_cmd_rx_ctx rx_ctx;
struct ec_host_cmd_tx_buf tx;
Expand All @@ -31,6 +91,12 @@ struct ec_host_cmd {
struct k_sem rx_ready;
/** Status of the rx data checked in the ec_host_cmd_send_received function. */
enum ec_host_cmd_status rx_status;
/**
* User callback after receiving a command. It is called by the ec_host_cmd_send_received
* function.
*/
ec_host_cmd_user_cb_t user_cb;
void *user_data;
#ifdef CONFIG_EC_HOST_CMD_DEDICATED_THREAD
struct k_thread thread;
#endif /* CONFIG_EC_HOST_CMD_DEDICATED_THREAD */
Expand Down Expand Up @@ -185,56 +251,6 @@ struct ec_host_cmd_response_header {
uint16_t reserved;
} __packed;

/*
* Host command response codes (16-bit).
*/
enum ec_host_cmd_status {
/** Host command was successful. */
EC_HOST_CMD_SUCCESS = 0,
/** The specified command id is not recognized or supported. */
EC_HOST_CMD_INVALID_COMMAND = 1,
/** Generic Error. */
EC_HOST_CMD_ERROR = 2,
/** One of more of the input request parameters is invalid. */
EC_HOST_CMD_INVALID_PARAM = 3,
/** Host command is not permitted. */
EC_HOST_CMD_ACCESS_DENIED = 4,
/** Response was invalid (e.g. not version 3 of header). */
EC_HOST_CMD_INVALID_RESPONSE = 5,
/** Host command id version unsupported. */
EC_HOST_CMD_INVALID_VERSION = 6,
/** Checksum did not match. */
EC_HOST_CMD_INVALID_CHECKSUM = 7,
/** A host command is currently being processed. */
EC_HOST_CMD_IN_PROGRESS = 8,
/** Requested information is currently unavailable. */
EC_HOST_CMD_UNAVAILABLE = 9,
/** Timeout during processing. */
EC_HOST_CMD_TIMEOUT = 10,
/** Data or table overflow. */
EC_HOST_CMD_OVERFLOW = 11,
/** Header is invalid or unsupported (e.g. not version 3 of header). */
EC_HOST_CMD_INVALID_HEADER = 12,
/** Did not receive all expected request data. */
EC_HOST_CMD_REQUEST_TRUNCATED = 13,
/** Response was too big to send within one response packet. */
EC_HOST_CMD_RESPONSE_TOO_BIG = 14,
/** Error on underlying communication bus. */
EC_HOST_CMD_BUS_ERROR = 15,
/** System busy. Should retry later. */
EC_HOST_CMD_BUSY = 16,
/** Header version invalid. */
EC_HOST_CMD_INVALID_HEADER_VERSION = 17,
/** Header CRC invalid. */
EC_HOST_CMD_INVALID_HEADER_CRC = 18,
/** Data CRC invalid. */
EC_HOST_CMD_INVALID_DATA_CRC = 19,
/** Can't resend response. */
EC_HOST_CMD_DUP_UNAVAILABLE = 20,

EC_HOST_CMD_MAX = UINT16_MAX /* Force enum to be 16 bits. */
} __packed;

/**
* @brief Initialize the host command subsystem
*
Expand Down Expand Up @@ -275,6 +291,16 @@ int ec_host_cmd_send_response(enum ec_host_cmd_status status,
*/
void ec_host_cmd_rx_notify(void);

/**
* @brief Install a user callback for receiving a host command
*
* It allows installing a custom procedure needed by a user after receiving a command.
*
* @param[in] cb A callback to be installed.
* @param[in] user_data User data to be passed to the callback.
*/
void ec_host_cmd_set_user_cb(ec_host_cmd_user_cb_t cb, void *user_data);

/**
* @brief Get the main ec host command structure
*
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 @@ -179,6 +179,14 @@ static enum ec_host_cmd_status prepare_response(struct ec_host_cmd_tx_buf *tx, u
return EC_HOST_CMD_SUCCESS;
}

void ec_host_cmd_set_user_cb(ec_host_cmd_user_cb_t cb, void *user_data)
{
struct ec_host_cmd *hc = &ec_host_cmd;

hc->user_cb = cb;
hc->user_data = user_data;
}

int ec_host_cmd_send_response(enum ec_host_cmd_status status,
const struct ec_host_cmd_handler_args *args)
{
Expand Down Expand Up @@ -246,6 +254,10 @@ void ec_host_cmd_rx_notify(void)

hc->rx_status = verify_rx(rx);

if (!hc->rx_status && hc->user_cb) {
hc->user_cb(rx, hc->user_data);
}

k_sem_give(&hc->rx_ready);
}

Expand Down

0 comments on commit 84143a1

Please sign in to comment.