diff options
author | Vic Yang <victoryang@chromium.org> | 2012-07-20 14:38:37 +0800 |
---|---|---|
committer | Gerrit <chrome-bot@google.com> | 2012-07-23 18:13:58 -0700 |
commit | 0653aa011ac5ce05f2e0d70bae90b5b7d126ea73 (patch) | |
tree | ec093337b7edcbdacfa3ab59a636ed89a293e861 | |
parent | 97131838e89063187d3021aedb07b6229871fa13 (diff) | |
download | chrome-ec-0653aa011ac5ce05f2e0d70bae90b5b7d126ea73.tar.gz |
Add console command to inject host command
For easier testing.
BUG=chrome-os-partner:11350
TEST=Try host command 0x01=CMD_HELLO.
> hostcmd 0x01 0 00000000
04030201
> hostcmd 0x01 0 01020304
05050505
Change-Id: I18becbd7d26241fa673fd46d9ce23a984ac47026
Signed-off-by: Vic Yang <victoryang@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/28059
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r-- | common/host_command.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/common/host_command.c b/common/host_command.c index 1a1c21fd40..212152efa1 100644 --- a/common/host_command.c +++ b/common/host_command.c @@ -234,3 +234,90 @@ void host_command_task(void) } } } + +/*****************************************************************************/ +/* Console commands*/ + +static int parse_byte(char *b, uint8_t *out) +{ + int i; + *out = 0; + for (i = 0; i < 2; ++i) { + *out *= 16; + if (*b >= '0' && *b <= '9') + *out += *b - '0'; + else if (*b >= 'a' && *b <= 'f') + *out += *b - 'a' + 10; + else if (*b >= 'A' && *b <= 'F') + *out += *b - 'A' + 10; + else + return EC_ERROR_INVAL; + ++b; + } + return EC_SUCCESS; +} + +static int parse_params(char *s, uint8_t *params) +{ + int len = 0; + + while (*s) { + if (parse_byte(s, params)) + return -1; + s += 2; + params++; + len++; + } + return len; +} + +static void dump_hex(char *resp, int size) +{ + int i; + for (i = 0; i < size; ++i) { + ccprintf("%02x", (int)resp[i]); + if ((i & 15) == 15) + ccputs("\n"); + } + ccputs("\n"); +} + +static int command_host_command(int argc, char **argv) +{ + struct host_cmd_handler_args args; + uint8_t cmd_params[EC_HOST_PARAM_SIZE]; + enum ec_status res; + char *e; + int rv; + + if (argc != 4) + return EC_ERROR_PARAM_COUNT; + + args.command = strtoi(argv[1], &e, 0); + if (*e) + return EC_ERROR_PARAM1; + args.version = strtoi(argv[2], &e, 0); + if (*e) + return EC_ERROR_PARAM2; + args.params = cmd_params; + rv = parse_params(argv[3], cmd_params); + if (rv < 0) + return EC_ERROR_PARAM3; + args.params_size = rv; + args.response = cmd_params; + args.response_max = EC_HOST_PARAM_SIZE; + args.response_size = 0; + + res = host_command_process(&args); + + if (res != EC_RES_SUCCESS) + ccprintf("Command returned %d\n", res); + else + dump_hex(cmd_params, args.response_size); + + return EC_SUCCESS; +} +DECLARE_CONSOLE_COMMAND(hostcmd, command_host_command, + "cmd ver param", + "Fake host command", + NULL); |