From 97bf36c9d3ad3d96ad6dea7cd6e6f3e164297c43 Mon Sep 17 00:00:00 2001 From: Randall Spangler Date: Fri, 29 Mar 2013 12:40:07 -0700 Subject: 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 Reviewed-on: https://gerrit.chromium.org/gerrit/46900 Reviewed-by: Bill Richardson --- board/spring/usb_charging.c | 15 +++++++-------- chip/lm4/clock.c | 16 +++++++--------- common/gaia_power.c | 12 ++++++------ common/host_command.c | 10 ++-------- common/keyboard_8042.c | 26 ++++++++++++++++---------- common/keyboard_scan.c | 18 ++++++------------ common/lp5562.c | 10 +++++++--- common/mock_x86_power.c | 8 +------- common/util.c | 14 ++++++++++++++ include/util.h | 19 +++++++++++++++++++ 10 files changed, 85 insertions(+), 63 deletions(-) diff --git a/board/spring/usb_charging.c b/board/spring/usb_charging.c index d157cd895e..45babf5eb8 100644 --- a/board/spring/usb_charging.c +++ b/board/spring/usb_charging.c @@ -557,18 +557,17 @@ int board_get_usb_current_limit(void) static int command_ilim(int argc, char **argv) { char *e; - int percent; + int v; if (argc >= 2) { - if (strcasecmp(argv[1], "on") == 0) - board_ilim_config(ILIM_CONFIG_MANUAL_ON); - else if (strcasecmp(argv[1], "off") == 0) - board_ilim_config(ILIM_CONFIG_MANUAL_OFF); - else { - percent = strtoi(argv[1], &e, 0); + if (parse_bool(argv[1], &v)) { + board_ilim_config(v ? ILIM_CONFIG_MANUAL_ON : + ILIM_CONFIG_MANUAL_OFF); + } else { + v = strtoi(argv[1], &e, 0); if (*e) return EC_ERROR_PARAM1; - board_pwm_duty_cycle(percent); + board_pwm_duty_cycle(v); } } diff --git a/chip/lm4/clock.c b/chip/lm4/clock.c index 357efb276d..fea8873969 100644 --- a/chip/lm4/clock.c +++ b/chip/lm4/clock.c @@ -252,28 +252,26 @@ DECLARE_CONSOLE_COMMAND(sleep, command_sleep, static int command_pll(int argc, char **argv) { - int div = 1; + int v; /* Toggle the PLL */ if (argc > 1) { - if (!strcasecmp(argv[1], "off")) - clock_enable_pll(0, 1); - else if (!strcasecmp(argv[1], "on")) - clock_enable_pll(1, 1); - else { + if (parse_bool(argv[1], &v)) { + clock_enable_pll(v, 1); + } else { /* Disable PLL and set extra divider */ char *e; - div = strtoi(argv[1], &e, 10); + v = strtoi(argv[1], &e, 10); if (*e) return EC_ERROR_PARAM1; - LM4_SYSTEM_RCC = LM4_SYSTEM_RCC_SYSDIV(div - 1) | + LM4_SYSTEM_RCC = LM4_SYSTEM_RCC_SYSDIV(v - 1) | LM4_SYSTEM_RCC_BYPASS | LM4_SYSTEM_RCC_PWRDN | LM4_SYSTEM_RCC_OSCSRC(1) | LM4_SYSTEM_RCC_MOSCDIS; - freq = INTERNAL_CLOCK / div; + freq = INTERNAL_CLOCK / v; /* Notify modules of frequency change */ hook_notify(HOOK_FREQ_CHANGE); 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) { diff --git a/include/util.h b/include/util.h index eca264a3f7..c953e468f8 100644 --- a/include/util.h +++ b/include/util.h @@ -84,6 +84,25 @@ int strtoi(const char *nptr, char **endptr, int base); /* Like strncpy(), but guarantees null termination. */ char *strzcpy(char *dest, const char *src, int len); +/** + * Parses a boolean option from a string. + * + * Strings that set *dest=0 and return 1 (all case-insensitive): + * "off" + * "dis*" + * "n*" + * "f*" + * + * Strings that set *dest=1 and return 1 (all case-insensitive): + * "on" + * "ena*" + * "y*" + * "t*" + * + * Other strings return 0 and leave *dest unchanged. + */ +int parse_bool(const char *s, int *dest); + int tolower(int c); /* 64-bit divide-and-modulo. Does the equivalent of: -- cgit v1.2.1