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 <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2020-06-22 10:01:39 -05:00 committed by Carles Cufí
commit d8b86cba3c
4 changed files with 16 additions and 7 deletions

View file

@ -240,6 +240,17 @@ __syscall struct device *device_get_binding(const char *name);
*/ */
size_t z_device_get_all_static(struct device **devices); 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;
}
/** /**
* @} * @}
*/ */

View file

@ -86,15 +86,13 @@ struct device *z_impl_device_get_binding(const char *name)
* performed. Reserve string comparisons for a fallback. * performed. Reserve string comparisons for a fallback.
*/ */
for (dev = __device_start; dev != __device_end; dev++) { for (dev = __device_start; dev != __device_end; dev++) {
if ((dev->driver_api != NULL) && if (z_device_ready(dev) && (dev->name == name)) {
(dev->name == name)) {
return dev; return dev;
} }
} }
for (dev = __device_start; dev != __device_end; dev++) { for (dev = __device_start; dev != __device_end; dev++) {
if ((dev->driver_api != NULL) && if (z_device_ready(dev) && (strcmp(name, dev->name) == 0)) {
(strcmp(name, dev->name) == 0)) {
return dev; return dev;
} }
} }

View file

@ -39,7 +39,7 @@ static bool device_get_config_level(const struct shell *shell, int level)
bool devices = false; bool devices = false;
for (dev = levels[level]; dev < levels[level+1]; dev++) { for (dev = levels[level]; dev < levels[level+1]; dev++) {
if (dev->driver_api != NULL) { if (z_device_ready(dev)) {
devices = true; devices = true;
shell_fprintf(shell, SHELL_NORMAL, "- %s\n", dev->name); 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"); shell_fprintf(shell, SHELL_NORMAL, "devices:\n");
for (dev = __device_start; dev != __device_end; dev++) { for (dev = __device_start; dev != __device_end; dev++) {
if (dev->driver_api == NULL) { if (!z_device_ready(dev)) {
continue; continue;
} }

View file

@ -470,7 +470,7 @@ struct device *shell_device_lookup(size_t idx,
struct device *dev_end = dev + len; struct device *dev_end = dev + len;
while (dev < dev_end) { while (dev < dev_end) {
if ((dev->driver_api != NULL) if (z_device_ready(dev)
&& (dev->name != NULL) && (dev->name != NULL)
&& (strlen(dev->name) != 0) && (strlen(dev->name) != 0)
&& ((prefix == NULL) && ((prefix == NULL)