shell: device_service: add dependency information

Refactor the output of device list to use standard API to retrieve the
list of devices, and to always display a status rather than hiding
disabled/failed devices.

Add API to associate a distinct identifier with any "device" that does
not have a name.

Where a device has requires dependencies display the devices on which
it depends.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2020-12-08 09:03:57 -06:00 committed by Kumar Gala
commit a3af137c26

View file

@ -8,9 +8,9 @@
#include <shell/shell.h> #include <shell/shell.h>
#include <init.h> #include <init.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include <device.h> #include <device.h>
extern const struct device __device_start[];
extern const struct device __device_PRE_KERNEL_1_start[]; extern const struct device __device_PRE_KERNEL_1_start[];
extern const struct device __device_PRE_KERNEL_2_start[]; extern const struct device __device_PRE_KERNEL_2_start[];
extern const struct device __device_POST_KERNEL_start[]; extern const struct device __device_POST_KERNEL_start[];
@ -82,33 +82,62 @@ static int cmd_device_levels(const struct shell *shell,
return 0; return 0;
} }
static const char *get_device_name(const struct device *dev,
char *buf,
size_t len)
{
const char *name = dev->name;
if ((name == NULL) || (name[0] == 0)) {
snprintf(buf, len, "[%p]", dev);
name = buf;
}
return name;
}
static int cmd_device_list(const struct shell *shell, static int cmd_device_list(const struct shell *shell,
size_t argc, char **argv) size_t argc, char **argv)
{ {
const struct device *devlist;
size_t devcnt = z_device_get_all_static(&devlist);
const struct device *devlist_end = devlist + devcnt;
const struct device *dev; const struct device *dev;
ARG_UNUSED(argc); ARG_UNUSED(argc);
ARG_UNUSED(argv); ARG_UNUSED(argv);
shell_fprintf(shell, SHELL_NORMAL, "devices:\n"); shell_fprintf(shell, SHELL_NORMAL, "devices:\n");
for (dev = __device_start; dev != __device_end; dev++) { for (dev = devlist; dev < devlist_end; dev++) {
char buf[20];
const char *name = get_device_name(dev, buf, sizeof(buf));
const char *state = "READY";
size_t nhdls = 0;
const device_handle_t *hdls =
device_get_requires_handles(dev, &nhdls);
shell_fprintf(shell, SHELL_NORMAL, "- %s", name);
if (!device_is_ready(dev)) { if (!device_is_ready(dev)) {
continue; state = "DISABLED";
} } else {
shell_fprintf(shell, SHELL_NORMAL, "- %s", dev->name);
#ifdef CONFIG_PM_DEVICE #ifdef CONFIG_PM_DEVICE
uint32_t state = DEVICE_PM_ACTIVE_STATE; uint32_t st = DEVICE_PM_ACTIVE_STATE;
int err; int err = device_get_power_state(dev, &st);
err = device_get_power_state(dev, &state); if (!err) {
if (!err) { state = device_pm_state_str(st);
shell_fprintf(shell, SHELL_NORMAL, " (%s)", }
device_pm_state_str(state));
}
#endif /* CONFIG_PM_DEVICE */ #endif /* CONFIG_PM_DEVICE */
shell_fprintf(shell, SHELL_NORMAL, "\n"); }
shell_fprintf(shell, SHELL_NORMAL, " (%s)\n", state);
for (size_t di = 0; di < nhdls; ++di) {
device_handle_t dh = hdls[di];
const struct device *rdp = device_from_handle(dh);
shell_fprintf(shell, SHELL_NORMAL, " requires: %s\n",
get_device_name(rdp, buf, sizeof(buf)));
}
} }
return 0; return 0;