From b9e7a77609549e7d7a52c79d4f291e558cd0139e Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Tue, 10 Mar 2020 16:40:11 -0700 Subject: gpio_commands: reduce number of vararg parameters The extended GPIO state support command uses a lot of format elements when trying to report properties of the GPIOs. The upcoming source code processing scripts do not support more than eight format elements per format string. On top of that there is no need to even try to generate the string in case extended GPIO command mode is supported. Let's move generating the string with properties into the conditionally compiled section, to make sure that it is generated/processed only when needed, and let's use snprintf() first to reduce the number of format elements in the following ccprintf invocation. BRANCH=cr50, cr50-mp BUG=b:149964350 TEST=verified proper 'gpiog' command output when code is compiled both with and without CONFIG_CMD_GPIO_EXTENDED Change-Id: I0836a350d1f787c84d2079f10de3652523a8a5a9 Signed-off-by: Vadim Bendebury Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2097442 Reviewed-by: Mary Ruthven Reviewed-by: Namyoon Woo --- common/gpio_commands.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/common/gpio_commands.c b/common/gpio_commands.c index f66b76a9dc..7986d747a4 100644 --- a/common/gpio_commands.c +++ b/common/gpio_commands.c @@ -9,6 +9,7 @@ #include "console.h" #include "gpio.h" #include "host_command.h" +#include "printf.h" #include "system.h" #include "util.h" @@ -79,33 +80,39 @@ static enum ec_error_list set(const char *name, int value) static void print_gpio_info(int gpio) { - int changed, v, flags; + int changed, v; + char flags_str[33]; if (!gpio_is_implemented(gpio)) return; /* Skip unsupported signals */ v = gpio_get_level(gpio); #ifdef CONFIG_CMD_GPIO_EXTENDED - flags = gpio_get_flags(gpio); + { + int flags; + + flags = gpio_get_flags(gpio); + snprintf(flags_str, sizeof(flags_str), + "%s%s%s%s%s%s%s%s%s%s%s", + flags & GPIO_INPUT ? "I " : "", + flags & GPIO_OUTPUT ? "O " : "", + flags & GPIO_LOW ? "L " : "", + flags & GPIO_HIGH ? "H " : "", + flags & GPIO_ANALOG ? "A " : "", + flags & GPIO_OPEN_DRAIN ? "ODR " : "", + flags & GPIO_PULL_UP ? "PU " : "", + flags & GPIO_PULL_DOWN ? "PD " : "", + flags & GPIO_ALTERNATE ? "ALT " : "", + flags & GPIO_SEL_1P8V ? "1P8 " : "", + flags & GPIO_LOCKED ? "LCK " : ""); + } #else - flags = 0; + flags_str[0] = 0; #endif changed = last_val_changed(gpio, v); - ccprintf(" %d%c %s%s%s%s%s%s%s%s%s%s%s%s\n", v, - (changed ? '*' : ' '), - (flags & GPIO_INPUT ? "I " : ""), - (flags & GPIO_OUTPUT ? "O " : ""), - (flags & GPIO_LOW ? "L " : ""), - (flags & GPIO_HIGH ? "H " : ""), - (flags & GPIO_ANALOG ? "A " : ""), - (flags & GPIO_OPEN_DRAIN ? "ODR " : ""), - (flags & GPIO_PULL_UP ? "PU " : ""), - (flags & GPIO_PULL_DOWN ? "PD " : ""), - (flags & GPIO_ALTERNATE ? "ALT " : ""), - (flags & GPIO_SEL_1P8V ? "1P8 " : ""), - (flags & GPIO_LOCKED ? "LCK " : ""), - gpio_get_name(gpio)); + ccprintf(" %d%c %s%s\n", v, + (changed ? '*' : ' '), flags_str, gpio_get_name(gpio)); /* Flush console to avoid truncating output */ cflush(); -- cgit v1.2.1