Skip to content

Commit 6f6f8bc

Browse files
niedzwiecki-dawidkartben
authored andcommitted
mgmt: ec_host_cmd: uart: improve error handling
Add an array with the states names and use it while logging an error messages. Additionally unify the error message format. Do not add "UART HOST CMD ERROR", because the LOG_ERR macro already informs that it is an error message. Signed-off-by: Dawid Niedzwiecki <[email protected]>
1 parent fa4ffc2 commit 6f6f8bc

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

subsys/mgmt/ec_host_cmd/backends/ec_host_cmd_backend_uart.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ enum uart_host_command_state {
6767
UART_HOST_CMD_RX_OVERRUN,
6868
};
6969

70+
static const char * const state_name[] = {
71+
[UART_HOST_CMD_STATE_DISABLED] = "DISABLED",
72+
[UART_HOST_CMD_READY_TO_RX] = "READY_TO_RX",
73+
[UART_HOST_CMD_RECEIVING] = "RECEIVING",
74+
[UART_HOST_CMD_PROCESSING] = "PROCESSING",
75+
[UART_HOST_CMD_SENDING] = "SENDING",
76+
[UART_HOST_CMD_RX_BAD] = "RX_BAD",
77+
[UART_HOST_CMD_RX_OVERRUN] = "RX_OVERRUN",
78+
};
79+
7080
struct ec_host_cmd_uart_ctx {
7181
const struct device *uart_dev;
7282
struct ec_host_cmd_rx_ctx *rx_ctx;
@@ -115,27 +125,7 @@ static void rx_timeout(struct k_work *work)
115125
CONTAINER_OF(dwork, struct ec_host_cmd_uart_ctx, timeout_work);
116126
int res;
117127

118-
switch (hc_uart->state) {
119-
case UART_HOST_CMD_RECEIVING:
120-
/* If state is receiving then timeout was hit due to underrun */
121-
LOG_ERR("Request underrun detected");
122-
break;
123-
case UART_HOST_CMD_RX_OVERRUN:
124-
/* If state is rx_overrun then timeout was hit because
125-
* process request was cancelled and extra rx bytes were
126-
* dropped
127-
*/
128-
LOG_ERR("Request overrun detected");
129-
break;
130-
case UART_HOST_CMD_RX_BAD:
131-
/* If state is rx_bad then packet header was bad and process
132-
* request was cancelled to drop all incoming bytes.
133-
*/
134-
LOG_ERR("Bad packet header detected");
135-
break;
136-
default:
137-
LOG_ERR("Request timeout mishandled, state: %d", hc_uart->state);
138-
}
128+
LOG_ERR("Request error in state: %s", state_name[hc_uart->state]);
139129

140130
res = uart_rx_disable(hc_uart->uart_dev);
141131
res = uart_rx_enable(hc_uart->uart_dev, hc_uart->rx_ctx->buf, hc_uart->rx_buf_size, 0);
@@ -157,7 +147,7 @@ static void uart_callback(const struct device *dev, struct uart_event *evt, void
157147
K_MSEC(CONFIG_EC_HOST_CMD_BACKEND_UART_TIMEOUT));
158148
} else if (hc_uart->state == UART_HOST_CMD_PROCESSING ||
159149
hc_uart->state == UART_HOST_CMD_SENDING) {
160-
LOG_ERR("UART HOST CMD ERROR: Received data while processing or sending");
150+
LOG_ERR("Received data while in state: %s", state_name[hc_uart->state]);
161151
return;
162152
} else if (hc_uart->state == UART_HOST_CMD_RX_BAD ||
163153
hc_uart->state == UART_HOST_CMD_RX_OVERRUN) {
@@ -215,13 +205,13 @@ static void uart_callback(const struct device *dev, struct uart_event *evt, void
215205
break;
216206
case UART_TX_DONE:
217207
if (hc_uart->state != UART_HOST_CMD_SENDING) {
218-
LOG_ERR("UART HOST CMD ERROR: unexpected end of sending");
208+
LOG_ERR("Unexpected end of sending");
219209
}
220210
/* Receiving is already enabled in the send function. */
221211
hc_uart->state = UART_HOST_CMD_READY_TO_RX;
222212
break;
223213
case UART_RX_STOPPED:
224-
LOG_ERR("UART HOST CMD ERROR: Receiving data stopped");
214+
LOG_ERR("Receiving data stopped");
225215
break;
226216
default:
227217
break;
@@ -271,7 +261,7 @@ static int ec_host_cmd_uart_send(const struct ec_host_cmd_backend *backend)
271261
int ret;
272262

273263
if (hc_uart->state != UART_HOST_CMD_PROCESSING) {
274-
LOG_ERR("UART HOST CMD ERROR: unexpected state while sending");
264+
LOG_ERR("Unexpected state while sending");
275265
}
276266

277267
/* The state is changed to UART_HOST_CMD_READY_TO_RX in the UART_TX_DONE event */
@@ -291,7 +281,7 @@ static int ec_host_cmd_uart_send(const struct ec_host_cmd_backend *backend)
291281
/* If sending fails, reset the state */
292282
if (ret) {
293283
hc_uart->state = UART_HOST_CMD_READY_TO_RX;
294-
LOG_ERR("UART HOST CMD ERROR: sending failed");
284+
LOG_ERR("Sending failed");
295285
}
296286

297287
return ret;

0 commit comments

Comments
 (0)