shell: add function to get device by name or label

Added shell_device_get_binding() that wraps device_get_binding() plus
device_get_by_dt_nodelabel() so that a shell can easily get a device by
its full name or label.

Signed-off-by: Yishai Jaffe <yishai1999@gmail.com>
This commit is contained in:
Yishai Jaffe 2024-12-08 10:42:50 +02:00 committed by Benjamin Cabé
commit 59c7d5715b
3 changed files with 38 additions and 0 deletions

View file

@ -159,6 +159,28 @@ typedef bool (*shell_device_filter_t)(const struct device *dev);
const struct device *shell_device_filter(size_t idx,
shell_device_filter_t filter);
/**
* @brief Get a @ref device reference from its @ref device.name field or label.
*
* This function iterates through the devices on the system. If a device with
* the given @p name field is found, and that device initialized successfully at
* boot time, this function returns a pointer to the device.
*
* If no device has the given @p name, this function returns `NULL`.
*
* This function also returns NULL when a device is found, but it failed to
* initialize successfully at boot time. (To troubleshoot this case, set a
* breakpoint on your device driver's initialization function.)
*
* @param name device name to search for. A null pointer, or a pointer to an
* empty string, will cause NULL to be returned.
*
* @return pointer to device structure with the given name; `NULL` if the device
* is not found or if the device with that name's initialization function
* failed.
*/
const struct device *shell_device_get_binding(const char *name);
/**
* @brief Shell command handler prototype.
*

View file

@ -25,6 +25,11 @@ config SHELL_MINIMAL
defaults which favor reduced flash or memory requirements over extra
features.
config SHELL_DEVICE_HELPERS
bool "Shell device helpers"
imply DEVICE_DT_METADATA
default y if !SHELL_MINIMAL
config SHELL_THREAD_PRIORITY_OVERRIDE
bool "Override default shell thread priority"
help

View file

@ -536,6 +536,17 @@ const struct device *shell_device_lookup(size_t idx,
return shell_device_internal(idx, prefix, NULL);
}
const struct device *shell_device_get_binding(const char *name)
{
const struct device *dev = device_get_binding(name);
if (IS_ENABLED(CONFIG_DEVICE_DT_METADATA) && dev == NULL) {
dev = device_get_by_dt_nodelabel(name);
}
return dev;
}
long shell_strtol(const char *str, int base, int *err)
{
long val;