Commit 60e5a562 authored by Tomasz Bursztyka's avatar Tomasz Bursztyka Committed by Fabio Baltieri
Browse files

shell: Improve device list command with an optional filter



It might come in handy to filter the output of a device list
command, by adding a filter (auto-completion enabled as well).

Such as:
$ device list uart
devices:
- uart@2f8 (READY)
  DT node labels: uart1
- uart@3f8 (READY)
  DT node labels: uart0

Signed-off-by: default avatarTomasz Bursztyka <tobu@bang-olufsen.dk>
Co-authored-by: default avatarDimitris Karnikis <dika@bang-olufsen.dk>
Co-authored-by: default avatarHans Binderup <habi@bang-olufsen.dk>
parent 1cb1a277
Loading
Loading
Loading
Loading
+24 −4
Original line number Diff line number Diff line
@@ -56,9 +56,6 @@ static int cmd_device_list(const struct shell *sh,
	const struct device *devlist_end = devlist + devcnt;
	const struct device *dev;

	ARG_UNUSED(argc);
	ARG_UNUSED(argv);

	shell_fprintf(sh, SHELL_NORMAL, "devices:\n");

	for (dev = devlist; dev < devlist_end; dev++) {
@@ -67,6 +64,11 @@ static int cmd_device_list(const struct shell *sh,
		const char *state = "READY";
		int usage;

		if (argc == 2
		    && strstr(name, argv[1]) == NULL) {
			continue;
		}

		shell_fprintf(sh, SHELL_NORMAL, "- %s", name);
		if (!device_is_ready(dev)) {
			state = "DISABLED";
@@ -118,6 +120,20 @@ static int cmd_device_list(const struct shell *sh,
	return 0;
}

static void device_name_lookup(size_t idx,
			       struct shell_static_entry *entry)
{
	const struct device *dev = shell_device_lookup_all(idx, NULL);

	entry->syntax = dev != NULL ? dev->name : NULL;
	entry->handler = NULL;
	entry->help = "device";
	entry->subcmd = NULL;
}

SHELL_DYNAMIC_CMD_CREATE(dsub_device_name_lookup, device_name_lookup);


#ifdef CONFIG_DEVICE_SHELL_INIT_CMD
#ifdef CONFIG_DEVICE_DEPS
static int cmd_device_check_deps(const struct device *dev,
@@ -239,8 +255,12 @@ static int cmd_device_pm_toggle(const struct shell *sh,
#endif /* CONFIG_PM_DEVICE_RUNTIME  */


#define LIST_CMD_HELP SHELL_HELP("List configured devices, with an optional filter", \
				 "[<device filter>]")

SHELL_STATIC_SUBCMD_SET_CREATE(sub_device,
	SHELL_CMD(list, NULL, "List configured devices", cmd_device_list),
	SHELL_CMD_ARG(list, &dsub_device_name_lookup,
		      LIST_CMD_HELP, cmd_device_list, 1, 1),
	DEVICE_INIT_CMD
	PM_SHELL_CMD
	SHELL_SUBCMD_SET_END /* Array terminated. */