summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Rosenthal <jrosenth@chromium.org>2020-09-23 08:26:42 -0600
committerCommit Bot <commit-bot@chromium.org>2020-10-01 19:14:34 +0000
commitee4037ea734103169bd1cae24ab3b6a5e2e71f67 (patch)
tree9d5cee6b4d01b099e38822443bef181990cf81ce
parent1434fa7a1d5fed910c86f479aab75fee6521bb5d (diff)
downloadchrome-ec-ee4037ea734103169bd1cae24ab3b6a5e2e71f67.tar.gz
zephyr: shim in the zephyr shell as the EC console
This provides compatible macros for DECLARE_CONSOLE_COMMAND, DECLARE_SAFE_CONSOLE_COMMAND, and DECLARE_CONSOLE_COMMAND_FLAGS. Note: the concept of command flags and command restriction are not enabled currently for Zephyr. We simply define everything for now. These macros use the Zephyr shell subsystem as the backend for commands. In addition, cprints, cprintf, and cputs have been redirected to the shell for CC_CONSOLE channel outputs, and printk for all other outputs. We will look at using Zephyr's logging subsystem instead of printk for the other channels in the future. BUG=b:167590251 BRANCH=none TEST=run "gettime" and "timerinfo" commands with follow-up CLs Signed-off-by: Jack Rosenthal <jrosenth@chromium.org> Change-Id: I17caedcd0b84a21dd2b135312f683885eaf694af Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2427097 Reviewed-by: Jett Rink <jettrink@chromium.org>
-rw-r--r--include/console.h8
-rw-r--r--zephyr/shim/include/zephyr_console_shim.h58
-rw-r--r--zephyr/shim/src/CMakeLists.txt2
-rw-r--r--zephyr/shim/src/console.c89
4 files changed, 154 insertions, 3 deletions
diff --git a/include/console.h b/include/console.h
index 178309812c..2c8503f0ee 100644
--- a/include/console.h
+++ b/include/console.h
@@ -11,6 +11,10 @@
#include "common.h"
#include "config.h"
+#ifdef CONFIG_ZEPHYR
+#include "zephyr_console_shim.h"
+#endif
+
/*
* The EC code base has been using %h to print a hex buffer. Encode the
* parameters to do that in a pointer to a structure that's passed as the
@@ -164,14 +168,14 @@ void console_has_input(void);
* @param help String with one-line description of command, or NULL.
* @param flags Per-command flags, if needed.
*/
-#ifndef HAS_TASK_CONSOLE
+#if !defined(HAS_TASK_CONSOLE) && !defined(CONFIG_ZEPHYR)
#define DECLARE_CONSOLE_COMMAND(NAME, ROUTINE, ARGDESC, HELP) \
int (ROUTINE)(int argc, char **argv) __attribute__((unused))
#define DECLARE_SAFE_CONSOLE_COMMAND(NAME, ROUTINE, ARGDESC, HELP) \
int (ROUTINE)(int argc, char **argv) __attribute__((unused))
#define DECLARE_CONSOLE_COMMAND_FLAGS(NAME, ROUTINE, ARGDESC, HELP, FLAGS) \
int (ROUTINE)(int argc, char **argv) __attribute__((unused))
-#else
+#elif defined(HAS_TASK_CONSOLE)
/* We always provde help args, but we may discard them to save space. */
#if defined(CONFIG_CONSOLE_CMDHELP)
diff --git a/zephyr/shim/include/zephyr_console_shim.h b/zephyr/shim/include/zephyr_console_shim.h
new file mode 100644
index 0000000000..5f242b6830
--- /dev/null
+++ b/zephyr/shim/include/zephyr_console_shim.h
@@ -0,0 +1,58 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef __CROS_EC_ZEPHYR_CONSOLE_SHIM_H
+#define __CROS_EC_ZEPHYR_CONSOLE_SHIM_H
+
+#include <shell/shell.h>
+
+/**
+ * zshim_run_ec_console_command() - Dispatch a CrOS EC console command
+ * using Zephyr's shell
+ *
+ * @handler: A CrOS EC shell command handler.
+ * @shell: The Zephyr shell to run on.
+ * @argc: The number of command line arguments.
+ * @argv: The NULL-terminated list of arguments.
+ * @help_str: The help string to display when "-h" is passed.
+ * @argdesc: The string describing the arguments to the command.
+ *
+ * Return: the return value from the handler.
+ */
+int zshim_run_ec_console_command(int (*handler)(int argc, char **argv),
+ const struct shell *shell, size_t argc,
+ char **argv, const char *help_str,
+ const char *argdesc);
+
+/* Internal wrappers for DECLARE_CONSOLE_COMMAND_* macros. */
+#define _ZEPHYR_SHELL_COMMAND_SHIM_2(NAME, ROUTINE_ID, ARGDESC, HELP, \
+ WRAPPER_ID) \
+ static int WRAPPER_ID(const struct shell *shell, size_t argc, \
+ char **argv) \
+ { \
+ return zshim_run_ec_console_command(ROUTINE_ID, shell, argc, \
+ argv, HELP, ARGDESC); \
+ } \
+ SHELL_CMD_ARG_REGISTER(NAME, NULL, HELP, WRAPPER_ID, 0, \
+ SHELL_OPT_ARG_MAX)
+
+#define _ZEPHYR_SHELL_COMMAND_SHIM(NAME, ROUTINE_ID, ARGDESC, HELP) \
+ _ZEPHYR_SHELL_COMMAND_SHIM_2(NAME, ROUTINE_ID, ARGDESC, HELP, \
+ UTIL_CAT(zshim_wrapper_, ROUTINE_ID))
+
+/* These macros mirror the macros provided by the CrOS EC. */
+#define DECLARE_CONSOLE_COMMAND(NAME, ROUTINE, ARGDESC, HELP) \
+ _ZEPHYR_SHELL_COMMAND_SHIM(NAME, ROUTINE, ARGDESC, HELP)
+
+/*
+ * TODO(jrosenth): implement flags and restricted commands? We just
+ * discard this in the shim layer for now.
+ */
+#define DECLARE_CONSOLE_COMMAND_FLAGS(NAME, ROUTINE, ARGDESC, HELP, FLAGS) \
+ _ZEPHYR_SHELL_COMMAND_SHIM(NAME, ROUTINE, ARGDESC, HELP)
+#define DECLARE_SAFE_CONSOLE_COMMAND(NAME, ROUTINE, ARGDESC, HELP) \
+ _ZEPHYR_SHELL_COMMAND_SHIM(NAME, ROUTINE, ARGDESC, HELP)
+
+#endif /* __CROS_EC_ZEPHYR_CONSOLE_SHIM_H */
diff --git a/zephyr/shim/src/CMakeLists.txt b/zephyr/shim/src/CMakeLists.txt
index d3db874f71..0d54d82ad0 100644
--- a/zephyr/shim/src/CMakeLists.txt
+++ b/zephyr/shim/src/CMakeLists.txt
@@ -2,4 +2,4 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-# Nothing here yet.
+zephyr_sources(console.c)
diff --git a/zephyr/shim/src/console.c b/zephyr/shim/src/console.c
new file mode 100644
index 0000000000..17364c4870
--- /dev/null
+++ b/zephyr/shim/src/console.c
@@ -0,0 +1,89 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <kernel.h>
+#include <shell/shell.h>
+#include <string.h>
+#include <sys/printk.h>
+#include <zephyr.h>
+
+#include "console.h"
+
+int cputs(enum console_channel channel, const char *str)
+{
+ return cprintf(channel, "%s\n", str);
+}
+
+static const struct shell *current_shell;
+
+static void console_vprintf(enum console_channel channel, const char *format,
+ va_list args)
+{
+ if (current_shell && channel == CC_COMMAND) {
+ shell_vfprintf(current_shell, SHELL_NORMAL, format, args);
+ return;
+ }
+
+ /*
+ * TODO(jrosenth): investigate using the logging subsystem
+ * and generating modules for the channels instead of printk
+ */
+ vprintk(format, args);
+}
+
+__attribute__((__format__(__printf__, 2, 3))) int
+cprintf(enum console_channel channel, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ console_vprintf(channel, format, args);
+ va_end(args);
+ return 0;
+}
+
+__attribute__((__format__(__printf__, 2, 3))) int
+cprints(enum console_channel channel, const char *format, ...)
+{
+ va_list args;
+
+ cprintf(channel, "[%lld ", k_uptime_get());
+ va_start(args, format);
+ console_vprintf(channel, format, args);
+ va_end(args);
+ cprintf(channel, "]\n");
+ return 0;
+}
+
+void cflush(void)
+{
+ /*
+ * Do nothing. Output is sent immediately without buffering
+ * from a printk() in Zephyr.
+ */
+}
+
+int zshim_run_ec_console_command(int (*handler)(int argc, char **argv),
+ const struct shell *shell, size_t argc,
+ char **argv, const char *help_str,
+ const char *argdesc)
+{
+ for (int i = 1; i < argc; i++) {
+ if (!help_str && !argdesc)
+ break;
+ if (!strcmp(argv[i], "-h")) {
+ if (help_str)
+ shell_fprintf(shell, SHELL_NORMAL, "%s\n",
+ help_str);
+ if (argdesc)
+ shell_fprintf(shell, SHELL_NORMAL,
+ "Usage: %s\n", argdesc);
+ return 0;
+ }
+ }
+
+ current_shell = shell;
+ return handler(argc, argv);
+}