From 3775e197e62f3a737cc77a48b3af984b9ecc45a7 Mon Sep 17 00:00:00 2001 From: Yishai Jaffe Date: Sun, 1 Dec 2024 17:59:08 +0200 Subject: [PATCH] pwm: shell: filter device lookup using DEVICE_API macros Filter for PWM devices when looking them up in dynamic shell commands. Signed-off-by: Yishai Jaffe --- drivers/pwm/pwm_shell.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/drivers/pwm/pwm_shell.c b/drivers/pwm/pwm_shell.c index 1caecad939f..1dfcfe72f3b 100644 --- a/drivers/pwm/pwm_shell.c +++ b/drivers/pwm/pwm_shell.c @@ -126,12 +126,29 @@ static int cmd_nsec(const struct shell *sh, size_t argc, char **argv) return 0; } +static bool device_is_pwm_and_ready(const struct device *dev) +{ + return device_is_ready(dev) && DEVICE_API_IS(pwm, dev); +} + +static void device_name_get(size_t idx, struct shell_static_entry *entry) +{ + const struct device *dev = shell_device_filter(idx, device_is_pwm_and_ready); + + entry->syntax = (dev != NULL) ? dev->name : NULL; + entry->handler = NULL; + entry->help = NULL; + entry->subcmd = NULL; +} + +SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get); + SHELL_STATIC_SUBCMD_SET_CREATE(pwm_cmds, - SHELL_CMD_ARG(cycles, NULL, " " + SHELL_CMD_ARG(cycles, &dsub_device_name, " " " [flags]", cmd_cycles, 5, 1), - SHELL_CMD_ARG(usec, NULL, " " + SHELL_CMD_ARG(usec, &dsub_device_name, " " " [flags]", cmd_usec, 5, 1), - SHELL_CMD_ARG(nsec, NULL, " " + SHELL_CMD_ARG(nsec, &dsub_device_name, " " " [flags]", cmd_nsec, 5, 1), SHELL_SUBCMD_SET_END );