From d8b86cba3cd210976fd9dfc3551f185edfedbfb9 Mon Sep 17 00:00:00 2001 From: Peter Bigot Date: Mon, 22 Jun 2020 10:01:39 -0500 Subject: [PATCH] device: add API to check whether a device is ready to use Currently this is useful only for some internal applications that iterate over the device table, since applications can't get access to a device that isn't ready, and devices can't be made unready. So it's introduced as internal API that may be exposed as device_ready() when those conditions change. Signed-off-by: Peter Bigot --- include/device.h | 11 +++++++++++ kernel/device.c | 6 ++---- subsys/shell/modules/device_service.c | 4 ++-- subsys/shell/shell_utils.c | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/device.h b/include/device.h index 3ad62e7a26..98c05e5911 100644 --- a/include/device.h +++ b/include/device.h @@ -240,6 +240,17 @@ __syscall struct device *device_get_binding(const char *name); */ size_t z_device_get_all_static(struct device **devices); +/** @brief Determine whether a device has been successfully initialized. + * + * @param dev pointer to the device in question. + * + * @return true if and only if the device is available for use. + */ +static inline bool z_device_ready(const struct device *dev) +{ + return dev->driver_api != NULL; +} + /** * @} */ diff --git a/kernel/device.c b/kernel/device.c index 24866cded7..b4fb179f85 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -86,15 +86,13 @@ struct device *z_impl_device_get_binding(const char *name) * performed. Reserve string comparisons for a fallback. */ for (dev = __device_start; dev != __device_end; dev++) { - if ((dev->driver_api != NULL) && - (dev->name == name)) { + if (z_device_ready(dev) && (dev->name == name)) { return dev; } } for (dev = __device_start; dev != __device_end; dev++) { - if ((dev->driver_api != NULL) && - (strcmp(name, dev->name) == 0)) { + if (z_device_ready(dev) && (strcmp(name, dev->name) == 0)) { return dev; } } diff --git a/subsys/shell/modules/device_service.c b/subsys/shell/modules/device_service.c index 2ca89968da..1c1fb425f2 100644 --- a/subsys/shell/modules/device_service.c +++ b/subsys/shell/modules/device_service.c @@ -39,7 +39,7 @@ static bool device_get_config_level(const struct shell *shell, int level) bool devices = false; for (dev = levels[level]; dev < levels[level+1]; dev++) { - if (dev->driver_api != NULL) { + if (z_device_ready(dev)) { devices = true; shell_fprintf(shell, SHELL_NORMAL, "- %s\n", dev->name); @@ -92,7 +92,7 @@ static int cmd_device_list(const struct shell *shell, shell_fprintf(shell, SHELL_NORMAL, "devices:\n"); for (dev = __device_start; dev != __device_end; dev++) { - if (dev->driver_api == NULL) { + if (!z_device_ready(dev)) { continue; } diff --git a/subsys/shell/shell_utils.c b/subsys/shell/shell_utils.c index 245030cab4..87c6f27b4c 100644 --- a/subsys/shell/shell_utils.c +++ b/subsys/shell/shell_utils.c @@ -470,7 +470,7 @@ struct device *shell_device_lookup(size_t idx, struct device *dev_end = dev + len; while (dev < dev_end) { - if ((dev->driver_api != NULL) + if (z_device_ready(dev) && (dev->name != NULL) && (strlen(dev->name) != 0) && ((prefix == NULL)