summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2011-10-17 14:27:33 -0700
committerRandall Spangler <rspangler@chromium.org>2011-10-17 14:59:01 -0700
commit2b3bef79683061e89d3e3c2823c87f592e2f2e23 (patch)
tree9f4312038f55decb19212d799efdc3586ac234c6
parent2266c2119e0dd75df824055b0e5f15beaea5d347 (diff)
downloadchrome-ec-2b3bef79683061e89d3e3c2823c87f592e2f2e23.tar.gz
Revised header files and moved in current implementation.
BUG=None TEST=cd cros_ec && make Change-Id: Ieac6a131f0aef0f8f3343d4f3cd9fba5a211bc09
-rw-r--r--cros_ec/Makefile18
-rw-r--r--cros_ec/chip_stub/ec_uart.c41
-rw-r--r--cros_ec/include/ec_common.h34
-rw-r--r--cros_ec/include/ec_console.h (renamed from ec_console.h)13
-rw-r--r--cros_ec/include/ec_keyboard.h (renamed from cros_ec/keyboard.h)9
-rw-r--r--cros_ec/include/ec_uart.h (renamed from ec_uart.h)70
-rw-r--r--cros_ec/lib/ec_console.c150
-rw-r--r--cros_ec/lib/ec_host_command.c (renamed from cros_ec/host_command.c)0
-rw-r--r--cros_ec/test/fakemain.c16
-rw-r--r--ec_common.h29
10 files changed, 308 insertions, 72 deletions
diff --git a/cros_ec/Makefile b/cros_ec/Makefile
new file mode 100644
index 0000000000..ff59ee3b50
--- /dev/null
+++ b/cros_ec/Makefile
@@ -0,0 +1,18 @@
+# Copyright (c) 2011 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.
+
+LIB_SRCS=\
+ lib/ec_console.c
+
+STUB_SRCS=\
+ chip_stub/ec_uart.c
+
+all: fakemain
+
+clean:
+ rm -f fakemain
+
+fakemain: test/fakemain.c $(LIB_SRCS) $(STUB_SRCS)
+ gcc -Wall -I include -o fakemain test/fakemain.c \
+ $(LIB_SRCS) $(STUB_SRCS)
diff --git a/cros_ec/chip_stub/ec_uart.c b/cros_ec/chip_stub/ec_uart.c
new file mode 100644
index 0000000000..f76691169a
--- /dev/null
+++ b/cros_ec/chip_stub/ec_uart.c
@@ -0,0 +1,41 @@
+/* Copyright (c) 2011 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.
+ */
+
+/* UART module for Chrome EC, empty implementation */
+
+#include "ec_uart.h"
+
+
+EcError EcUartPrintf(const char* format, ...) {
+ return EC_ERROR_UNIMPLEMENTED;
+}
+
+
+EcError EcUartPuts(const char* outstr) {
+ return EC_ERROR_UNIMPLEMENTED;
+}
+
+
+void EcUartFlush(void) {
+}
+
+
+void EcUartFlushInput(void) {
+}
+
+
+int EcUartPeek(int c) {
+ return -1;
+}
+
+
+int EcUartGets(char* dest, int size) {
+ *dest = 0;
+ return 0;
+}
+
+
+void EcUartRegisterHasInputCallback(UartHasInputCallback callback, int c) {
+}
diff --git a/cros_ec/include/ec_common.h b/cros_ec/include/ec_common.h
new file mode 100644
index 0000000000..615a88adcf
--- /dev/null
+++ b/cros_ec/include/ec_common.h
@@ -0,0 +1,34 @@
+/* Copyright (c) 2011 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.
+ */
+
+/* ec_common.h - Common includes for Chrome EC */
+
+#ifndef __CROS_EC_COMMON_H
+#define __CROS_EC_COMMON_H
+
+#include <stdint.h>
+
+/* Functions which return error return one of these. This is an
+ * integer instead of an enum to support module-internal error
+ * codes. */
+typedef int EcError;
+
+/* List of common EcError codes that can be returned */
+enum EcErrorList {
+ /* Success - no error */
+ EC_SUCCESS = 0,
+ /* Unknown error */
+ EC_ERROR_UNKNOWN = 1,
+ /* Function not implemented yet */
+ EC_ERROR_UNIMPLEMENTED = 2,
+ /* Overflow error; too much input provided. */
+ EC_ERROR_OVERFLOW = 3,
+
+ /* Module-internal error codes may use this range. */
+ EC_ERROR_INTERNAL_FIRST = 0x10000,
+ EC_ERROR_INTERNAL_LAST = 0x1FFFF
+};
+
+#endif /* __CROS_EC_COMMON_H */
diff --git a/ec_console.h b/cros_ec/include/ec_console.h
index 64e38eda09..827be9fc86 100644
--- a/ec_console.h
+++ b/cros_ec/include/ec_console.h
@@ -1,6 +1,9 @@
-/* ec_console.h - Debug console for Chrome EC
- *
- * (Chromium license) */
+/* Copyright (c) 2011 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.
+ */
+
+/* ec_console.h - Debug console for Chrome EC */
#ifndef __CROS_EC_CONSOLE_H
#define __CROS_EC_CONSOLE_H
@@ -25,10 +28,10 @@ typedef struct EcConsoleCommandGroup {
/* Initializes the console module. */
-EcError ConsoleInit(void);
+EcError EcConsoleInit(void);
/* Registers a group of console commands. */
-EcError ConsoleRegisterCommands(const EcConsoleCommandGroup* group);
+EcError EcConsoleRegisterCommands(const EcConsoleCommandGroup* group);
#endif /* __CROS_EC_CONSOLE_H */
diff --git a/cros_ec/keyboard.h b/cros_ec/include/ec_keyboard.h
index ad9ef6c220..c1c5d5e250 100644
--- a/cros_ec/keyboard.h
+++ b/cros_ec/include/ec_keyboard.h
@@ -1,6 +1,9 @@
-/* keyboard.h -
- *
- * (Chromium license) */
+/* Copyright (c) 2011 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.
+ */
+
+/* Keyboard emulation */
#ifndef __CROS_EC_KEYBOARD_H
#define __CROS_EC_KEYBOARD_H
diff --git a/ec_uart.h b/cros_ec/include/ec_uart.h
index 78d1b18764..8f49d43cce 100644
--- a/ec_uart.h
+++ b/cros_ec/include/ec_uart.h
@@ -1,14 +1,24 @@
-/* ec_uart.h - UART module for Chrome EC
- *
- * (Chromium license) */
+/* Copyright (c) 2011 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.
+ */
+
+/* ec_uart.h - UART module for Chrome EC */
#ifndef __CROS_EC_UART_H
#define __CROS_EC_UART_H
#include "ec_common.h"
+
+/* Initializes the UART module. */
+EcError EcUartInit(void);
+
/*****************************************************************************/
-/* Output functions */
+/* Output functions
+ *
+ * Output is buffered. If the buffer overflows, subsequent output is
+ * discarded. */
/* Print formatted output to the UART, like printf().
*
@@ -25,30 +35,33 @@
*
* Note: Floating point output (%f / %g) is not required.
*/
-EcError UartPrintf(const char* format, ...);
+EcError EcUartPrintf(const char* format, ...);
/* Put a null-terminated string to the UART, like puts().
*
* Returns error if output was truncated. */
-EcError UartPuts(const char* outstr);
+EcError EcUartPuts(const char* outstr);
/* Flushes output. Blocks until UART has transmitted all output. */
-void UartFlush(void);
+void EcUartFlushOutput(void);
/*****************************************************************************/
-/* Input functions */
+/* Input functions
+ *
+ * Input is buffered. If the buffer overflows, the oldest input in
+ * the buffer is discarded to make room for the new input.
+ *
+ * Input lines may be terminated by CR ('\r'), LF ('\n'), or CRLF; all
+ * are translated to newline. */
/* Flushes input buffer, discarding all input. */
-void UartFlushInput(void);
+void EcUartFlushInput(void);
/* Non-destructively checks for a character in the input buffer.
*
- * Returns non-zero if the character <c> is in the input buffer. If
- * c<0, returns non-zero if any character is in the input buffer. */
-/* TODO: add boolean type? */
-/* TODO: return offset of the character? That is, how many bytes of
- * input up to and including the character. */
-int UartPeek(int c);
+ * Returns the offset into the input buffer of character <c>, or -1 if
+ * it is not in the input buffer. */
+int EcUartPeek(int c);
/* Reads characters from the UART, similar to fgets().
*
@@ -63,12 +76,15 @@ int UartPeek(int c);
* callack that input is waiting, but then the input buffer overflows
* or someone else grabs the input before UartGets() is called.
*
- * Characters are stored in <dest> and are null-terminated, and
- * include the newline if present.
+ * Characters are stored in <dest> and are null-terminated.
+ * Characters include the newline if present, so that the caller can
+ * distinguish between a complete line and a truncated one. If the
+ * input buffer is empty, a null-terminated empty string ("") is
+ * returned.
*
* Returns the number of characters read (not counting the terminating
* null). */
-int UartGets(char* dest, int size);
+int EcUartGets(char* dest, int size);
/* Callback handler prototype, called when the UART has input. */
typedef void (*UartHasInputCallback)(void);
@@ -79,24 +95,8 @@ typedef void (*UartHasInputCallback)(void);
* Callback will be called whenever the UART receives character <c>.
* If c<0, callback will be called when the UART receives any
* character. */
-void UartRegisterHasInputCallback(UartHasInputCallback callback, int c);
-
-/* TODO: what to do about input overflow? That is, what if the input
- * buffer fills with 'A', and we're still waiting for a newline? Do
- * we drop the oldest output, flush the whole thing, or call the input
- * callback with a message code? Alternately, should the callback be able
- * to specify a buffer-full-threshold where it gets called?
- *
- * Simplest just to flush the input buffer on overflow. */
-/* YJLOU: are you talking about the UartRegisterHasInputCallback()?
- * If yes, this is not a problem because for every input key, we can drop it
- * if there is no callback registered for it.
- * Else if you are talking about the UartGets() function, I think you can add
- * the third return critiria: internal key buffer reaches full.
- */
+void EcUartRegisterHasInputCallback(UartHasInputCallback callback, int c);
/* TODO: getc(), putc() equivalents? */
-/* TODO: input handler assumes debug input new lines are '\n'-delimited? */
-
#endif /* __CROS_EC_UART_H */
diff --git a/cros_ec/lib/ec_console.c b/cros_ec/lib/ec_console.c
new file mode 100644
index 0000000000..0196f84a92
--- /dev/null
+++ b/cros_ec/lib/ec_console.c
@@ -0,0 +1,150 @@
+/* Copyright (c) 2011 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.
+ */
+
+/* Console module for Chrome EC */
+
+#include <ctype.h>
+#include <string.h>
+
+#include "ec_console.h"
+#include "ec_uart.h"
+
+#define MAX_COMMAND_GROUPS 20
+#define MAX_ARGS_PER_COMMAND 10
+
+/* Forward declarations for console commands */
+static EcError CommandHelp(int argc, char** argv);
+
+static const EcConsoleCommandGroup *group_list[MAX_COMMAND_GROUPS];
+static int group_count = 0;
+
+static const EcConsoleCommand console_commands[] = {
+ {"help", CommandHelp},
+ {"?", CommandHelp},
+};
+static const EcConsoleCommandGroup console_group = {
+ "Console", console_commands,
+ sizeof(console_commands) / sizeof(EcConsoleCommand)
+};
+
+
+static void HasInputHandler(void) {
+ /* TODO: if we had threads, this would wake the processor thread */
+}
+
+
+EcError EcConsoleInit(void) {
+ /* Register the input handler to handle newlines */
+ EcUartRegisterHasInputCallback(HasInputHandler, '\n');
+
+ /* Register our internal commands */
+ return EcConsoleRegisterCommands(&console_group);
+}
+
+
+/* Command handler - prints help. */
+static EcError CommandHelp(int argc, char** argv) {
+ const EcConsoleCommand *cmd;
+ int c, g;
+
+ EcUartPuts("Known commands:\n");
+
+ for (g = 0; g < group_count; g++) {
+ cmd = group_list[g]->commands;
+ EcUartPrintf("Group %s:\n", group_list[g]->group_name);
+ for (c = group_list[g]->command_count; c > 0; c--, cmd++)
+ EcUartPrintf(" %s\n", cmd->name);
+ }
+
+ return EC_SUCCESS;
+}
+
+
+EcError EcConsoleRegisterCommands(const EcConsoleCommandGroup* group) {
+ if (group_count >= MAX_COMMAND_GROUPS)
+ return EC_ERROR_OVERFLOW; /* No space for a new command group */
+
+ group_list[group_count++] = group;
+ return EC_SUCCESS;
+}
+
+
+/* Splits a line of input into words. Stores the count of words in
+ * <argc>. Stores pointers to the words in <argv>, which must be at
+ * least <max_argc> long. If more than <max_argc> words are found,
+ * discards the excess and returns EC_ERROR_OVERFLOW. */
+EcError SplitWords(char* input, int max_argc, int* argc, char** argv) {
+ char* c;
+ int in_word = 0;
+
+ /* Parse input into words */
+ for (c = input; c; c++) {
+ if (isspace(*c)) {
+ if (in_word) {
+ /* Ending a word */
+ *c = '\0';
+ ++*argc;
+ in_word = 0;
+ }
+ } else {
+ if (!in_word) {
+ /* Starting a new word */
+ if (*argc >= max_argc)
+ return EC_ERROR_OVERFLOW; /* More words than we can handle */
+
+ argv[*argc] = c;
+ in_word = 1;
+ }
+ }
+ }
+
+ return EC_SUCCESS;
+}
+
+
+/* Finds a command by name. Returns the command structure, or NULL if
+ * no match found. */
+const EcConsoleCommand* FindCommand(char* name) {
+ const EcConsoleCommand *cmd;
+ int c, g;
+
+ /* Find the command in the command groups */
+ for (g = 0; g < group_count; g++) {
+ cmd = group_list[g]->commands;
+ for (c = group_list[g]->command_count; c > 0; c--, cmd++) {
+ if (!strcasecmp(name, cmd->name))
+ return cmd;
+ }
+ }
+
+ return NULL;
+}
+
+
+/* Handles a line of input containing a single command.
+ *
+ * Modifies the input string during parsing. */
+EcError ConsoleHandleCommand(char* input) {
+ char* argv[MAX_ARGS_PER_COMMAND];
+ const EcConsoleCommand *cmd;
+ int argc;
+
+ /* Split input into words. Ignore words past our limit. */
+ SplitWords(input, MAX_ARGS_PER_COMMAND, &argc, argv);
+
+ /* If no command, nothing to do */
+ if (!argc)
+ return EC_SUCCESS;
+
+ cmd = FindCommand(argv[0]);
+ if (cmd)
+ return cmd->handler(argc, argv);
+
+ EcUartPrintf("Command '%s' not found.\n", argv[0]);
+ return EC_ERROR_UNKNOWN;
+}
+
+
+/* TODO: task function */
diff --git a/cros_ec/host_command.c b/cros_ec/lib/ec_host_command.c
index e69de29bb2..e69de29bb2 100644
--- a/cros_ec/host_command.c
+++ b/cros_ec/lib/ec_host_command.c
diff --git a/cros_ec/test/fakemain.c b/cros_ec/test/fakemain.c
new file mode 100644
index 0000000000..40316d08eb
--- /dev/null
+++ b/cros_ec/test/fakemain.c
@@ -0,0 +1,16 @@
+/* Copyright (c) 2011 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.
+ */
+
+/* Fake main routine, to make sure lib functions link */
+
+#include "ec_console.h"
+#include "ec_uart.h"
+
+int main(void) {
+
+
+
+ return 0;
+}
diff --git a/ec_common.h b/ec_common.h
deleted file mode 100644
index a7ce19b21c..0000000000
--- a/ec_common.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/* ec_common.h - Common includes for Chrome EC
- *
- * (Chromium license) */
-
-#ifndef __EC_COMMON_H
-#define __EC_COMMON_H
-
-#include <stdint.h>
-
-typedef int EcError; /* Functions which return error return one of these */
-/* YJLOU: why not "typedef enum EcErrorListe EcError"? */
-
-/* List of common EcError codes that can be returned */
-enum EcErrorList {
- /* Success - no error */
- EC_SUCCESS = 0,
- /* Unknown error */
- EC_ERROR_UNKNOWN = 1,
- /* Function not implemented yet */
- EC_ERROR_UNIMPLEMENTED = 2,
- /* Overflow error; too much input provided. */
- EC_ERROR_OVERFLOW = 3,
-
- /* Module-internal error codes may use this range. */
- EC_ERROR_INTERNAL_FIRST = 0x10000,
- EC_ERROR_INTERNAL_LAST = 0x1FFFF
-};
-
-#endif /* __EC_COMMON_H */