summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2013-03-29 12:40:07 -0700
committerChromeBot <chrome-bot@google.com>2013-03-29 15:39:34 -0700
commit97bf36c9d3ad3d96ad6dea7cd6e6f3e164297c43 (patch)
tree5d4ba65334beb071e4cc09d8a404e859bca398ac /common
parentc317992194ea089a97c4d54ef77ff4b1e283c248 (diff)
downloadchrome-ec-97bf36c9d3ad3d96ad6dea7cd6e6f3e164297c43.tar.gz
Add parse_bool() to centralize parsing boolean options
This way debug commands which previously took only yes/no or on/off or enable/disable can take any of those options. BUG=chrome-os-partner:18467 BRANCH=none TEST=Try "on", "off", "yes", "no", "true", "false", "ena", "disable", for each of the following commands: - ilim (spring) - pll (link) - power (spring/snow) - hcdebug (all) - kblog (link) - ksscan (all) - lp5562 (spring) Change-Id: Ie8e0fae3775b1da711864bcba6682ba5e68a06f1 Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/46900 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Diffstat (limited to 'common')
-rw-r--r--common/gaia_power.c12
-rw-r--r--common/host_command.c10
-rw-r--r--common/keyboard_8042.c26
-rw-r--r--common/keyboard_scan.c18
-rw-r--r--common/lp5562.c10
-rw-r--r--common/mock_x86_power.c8
-rw-r--r--common/util.c14
7 files changed, 52 insertions, 46 deletions
diff --git a/common/gaia_power.c b/common/gaia_power.c
index 8252240aae..7f95922deb 100644
--- a/common/gaia_power.c
+++ b/common/gaia_power.c
@@ -607,6 +607,8 @@ static const char * const state_name[] = {
static int command_power(int argc, char **argv)
{
+ int v;
+
if (argc < 2) {
enum power_state_t state;
@@ -622,14 +624,12 @@ static int command_power(int argc, char **argv)
return EC_SUCCESS;
}
- if (0 == strcasecmp(argv[1], "on"))
- power_request = POWER_REQ_ON;
- else if (0 == strcasecmp(argv[1], "off"))
- power_request = POWER_REQ_OFF;
- else
+ if (!parse_bool(argv[1], &v))
return EC_ERROR_PARAM1;
- ccprintf("[%T PB Requesting power %s]\n", power_req_name[power_request]);
+ power_request = v ? POWER_REQ_ON : POWER_REQ_OFF;
+ ccprintf("[%T PB Requesting power %s]\n",
+ power_req_name[power_request]);
task_wake(TASK_ID_CHIPSET);
return EC_SUCCESS;
diff --git a/common/host_command.c b/common/host_command.c
index 7dd0a2a90c..264c3dca8e 100644
--- a/common/host_command.c
+++ b/common/host_command.c
@@ -447,14 +447,8 @@ DECLARE_CONSOLE_COMMAND(hostcmd, command_host_command,
static int command_hcdebug(int argc, char **argv)
{
- if (argc > 1) {
- if (!strcasecmp(argv[1], "on"))
- hcdebug = 1;
- else if (!strcasecmp(argv[1], "off"))
- hcdebug = 0;
- else
- return EC_ERROR_PARAM1;
- }
+ if (argc > 1 && !parse_bool(argv[1], &hcdebug))
+ return EC_ERROR_PARAM1;
ccprintf("Host command debug is %s\n", hcdebug ? "on" : "off");
return EC_SUCCESS;
diff --git a/common/keyboard_8042.c b/common/keyboard_8042.c
index 248a51e2b8..4a058a4f58 100644
--- a/common/keyboard_8042.c
+++ b/common/keyboard_8042.c
@@ -992,6 +992,7 @@ static int command_keyboard_log(int argc, char **argv)
{
int i;
+ /* If no args, print log */
if (argc == 1) {
ccprintf("KBC log (len=%d):\n", kblog_len);
for (i = 0; kblog_buf && i < kblog_len; ++i) {
@@ -1003,7 +1004,14 @@ static int command_keyboard_log(int argc, char **argv)
}
}
ccputs("\n");
- } else if (argc == 2 && !strcasecmp("on", argv[1])) {
+ return EC_SUCCESS;
+ }
+
+ /* Otherwise, enable/disable */
+ if (!parse_bool(argv[1], &i))
+ return EC_ERROR_PARAM1;
+
+ if (i) {
if (!kblog_buf) {
int rv = shared_mem_acquire(
sizeof(*kblog_buf) * MAX_KBLOG,
@@ -1013,13 +1021,11 @@ static int command_keyboard_log(int argc, char **argv)
kblog_len = 0;
return rv;
}
- } else if (argc == 2 && !strcasecmp("off", argv[1])) {
+ } else {
kblog_len = 0;
if (kblog_buf)
shared_mem_release(kblog_buf);
kblog_buf = NULL;
- } else {
- return EC_ERROR_PARAM1;
}
return EC_SUCCESS;
@@ -1032,20 +1038,20 @@ DECLARE_CONSOLE_COMMAND(kblog, command_keyboard_log,
static int command_keyboard(int argc, char **argv)
{
+ int ena;
+
if (argc > 1) {
- if (!strcasecmp(argv[1], "enable"))
- keyboard_enable(1);
- else if (!strcasecmp(argv[1], "disable"))
- keyboard_enable(0);
- else
+ if (!parse_bool(argv[1], &ena))
return EC_ERROR_PARAM1;
+
+ keyboard_enable(ena);
}
ccprintf("Enabled: %d\n", keyboard_enabled);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(kbd, command_keyboard,
- "[enable | disable]",
+ "[0 | 1]",
"Print or toggle keyboard info",
NULL);
diff --git a/common/keyboard_scan.c b/common/keyboard_scan.c
index 45eb45a35a..7f17d8c237 100644
--- a/common/keyboard_scan.c
+++ b/common/keyboard_scan.c
@@ -587,18 +587,12 @@ DECLARE_HOST_COMMAND(EC_CMD_MKBP_SIMULATE_KEY,
static int command_ksstate(int argc, char **argv)
{
- if (argc > 1) {
- if (!strcasecmp(argv[1], "on"))
- print_state_changes = 1;
- else if (!strcasecmp(argv[1], "off"))
- print_state_changes = 0;
- else
- return EC_ERROR_PARAM1;
- } else {
- print_state(debounced_state, "debounced ");
- print_state(prev_state, "prev ");
- print_state(debouncing, "debouncing");
- }
+ if (argc > 1 && !parse_bool(argv[1], &print_state_changes))
+ return EC_ERROR_PARAM1;
+
+ print_state(debounced_state, "debounced ");
+ print_state(prev_state, "prev ");
+ print_state(debouncing, "debouncing");
ccprintf("Keyboard scan state printing %s\n",
print_state_changes ? "on" : "off");
diff --git a/common/lp5562.c b/common/lp5562.c
index a16c2e837a..dc16a20f79 100644
--- a/common/lp5562.c
+++ b/common/lp5562.c
@@ -121,11 +121,15 @@ static int command_lp5562(int argc, char **argv)
return lp5562_set_color((red << 16) | (green << 8) | blue);
} else if (argc == 2) {
- if (!strcasecmp(argv[1], "on"))
+ int v;
+
+ if (!parse_bool(argv[1], &v))
+ return EC_ERROR_PARAM1;
+
+ if (v)
return lp5562_poweron();
- else if (!strcasecmp(argv[1], "off"))
+ else
return lp5562_poweroff();
- return EC_ERROR_PARAM1;
}
return EC_ERROR_INVAL;
diff --git a/common/mock_x86_power.c b/common/mock_x86_power.c
index 0c84ac0508..bbd19c06e7 100644
--- a/common/mock_x86_power.c
+++ b/common/mock_x86_power.c
@@ -79,13 +79,7 @@ static int command_mock_power(int argc, char **argv)
if (argc != 2)
return EC_ERROR_PARAM_COUNT;
- if (!strcasecmp(argv[1], "on")) {
- mock_power_on = 1;
- }
- else if (!strcasecmp(argv[1], "off")) {
- mock_power_on = 0;
- }
- else
+ if (!parse_bool(argv[1], &mock_power_on))
return EC_ERROR_PARAM1;
return EC_SUCCESS;
diff --git a/common/util.c b/common/util.c
index a8e154b896..d8670759c1 100644
--- a/common/util.c
+++ b/common/util.c
@@ -138,6 +138,20 @@ int strtoi(const char *nptr, char **endptr, int base)
return neg ? -result : result;
}
+int parse_bool(const char *s, int *dest)
+{
+ if (!strcasecmp(s, "off") || !strncasecmp(s, "dis", 3) ||
+ tolower(*s) == 'f' || tolower(*s) == 'n') {
+ *dest = 0;
+ return 1;
+ } else if (!strcasecmp(s, "on") || !strncasecmp(s, "ena", 3) ||
+ tolower(*s) == 't' || tolower(*s) == 'y') {
+ *dest = 1;
+ return 1;
+ } else {
+ return 0;
+ }
+}
int memcmp(const void *s1, const void *s2, int len)
{