summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Barnes <robbarnes@google.com>2022-09-12 14:54:47 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-09-15 15:09:17 +0000
commit2e1bcfce333f478aa259d8d3065caaafcca6e637 (patch)
treeabbf36b013d410540f66f58c04f0c0d1ced134a1
parent9e271aa66ca3fa77c5c12352bc0b5c10ed827b8a (diff)
downloadchrome-ec-2e1bcfce333f478aa259d8d3065caaafcca6e637.tar.gz
zephyr/console: Return number of bytes consumed by console buffer
Return the number of bytes consumed by the console buffer. This is not the same as the number of bytes written since null bytes are not written. For now this will always be 0 or len. This change will allow callers to perform an action when the request fails, such as printing a warning or retrying. BUG=b:243709788 BRANCH=None TEST=Boot skyrim, observe buffered console, pass default.console test. Change-Id: I97bf5a0776c123abbaed84b5ca7e58e903c0f20d Signed-off-by: Rob Barnes <robbarnes@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3892209 Reviewed-by: Keith Short <keithshort@chromium.org> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
-rw-r--r--zephyr/shim/include/zephyr_console_shim.h4
-rw-r--r--zephyr/shim/src/console_buffer.c7
-rw-r--r--zephyr/test/drivers/default/src/console.c6
3 files changed, 12 insertions, 5 deletions
diff --git a/zephyr/shim/include/zephyr_console_shim.h b/zephyr/shim/include/zephyr_console_shim.h
index 8c8f34e742..42ac3e693b 100644
--- a/zephyr/shim/include/zephyr_console_shim.h
+++ b/zephyr/shim/include/zephyr_console_shim.h
@@ -76,7 +76,9 @@ int zshim_run_ec_console_command(const struct zephyr_console_command *command,
*
* @s: The pointer to the string.
* @len: The size of the string.
+ *
+ * Return: the number of bytes consumed.
*/
-void console_buf_notify_chars(const char *s, size_t len);
+size_t console_buf_notify_chars(const char *s, size_t len);
#endif /* __CROS_EC_ZEPHYR_CONSOLE_SHIM_H */
diff --git a/zephyr/shim/src/console_buffer.c b/zephyr/shim/src/console_buffer.c
index fc7de6cd27..dad0031267 100644
--- a/zephyr/shim/src/console_buffer.c
+++ b/zephyr/shim/src/console_buffer.c
@@ -23,7 +23,7 @@ static inline uint32_t next_idx(uint32_t cur_idx)
K_MUTEX_DEFINE(console_write_lock);
-void console_buf_notify_chars(const char *s, size_t len)
+size_t console_buf_notify_chars(const char *s, size_t len)
{
/*
* This is just notifying of console characters for debugging
@@ -31,9 +31,9 @@ void console_buf_notify_chars(const char *s, size_t len)
* then just drop the string.
*/
if (k_mutex_lock(&console_write_lock, K_NO_WAIT))
- return;
+ return 0;
/* We got the mutex. */
- while (len--) {
+ for (size_t i = 0; i < len; i++) {
/* Don't copy null byte into buffer */
if (!(*s)) {
s++;
@@ -58,6 +58,7 @@ void console_buf_notify_chars(const char *s, size_t len)
tail_idx = new_tail;
}
k_mutex_unlock(&console_write_lock);
+ return len;
}
enum ec_status uart_console_read_buffer_init(void)
diff --git a/zephyr/test/drivers/default/src/console.c b/zephyr/test/drivers/default/src/console.c
index 3787a1f803..c74fd3ea1c 100644
--- a/zephyr/test/drivers/default/src/console.c
+++ b/zephyr/test/drivers/default/src/console.c
@@ -33,12 +33,16 @@ ZTEST_USER(console, buf_notify_null)
{
char buffer[100];
uint16_t write_count;
+ size_t consumed_count;
/* Flush the console buffer before we start. */
zassert_ok(uart_console_read_buffer_init(), NULL);
/* Write a nul char to the buffer. */
- console_buf_notify_chars("ab\0c", 4);
+ consumed_count = console_buf_notify_chars("ab\0c", 4);
+
+ /* Check if all bytes were consumed by console buffer */
+ zassert_equal(consumed_count, 4, "got %d", consumed_count);
/* Check if the nul is present in the buffer. */
zassert_ok(uart_console_read_buffer_init(), NULL);