sensor_shell: fix hang when device is not a sensor
If `CONFIG_SENSOR_INFO` is enabled, use the `sensor_info` section to validate that the argument is a sensor before using, otherwise the shell command will hang the application. Signed-off-by: Yong Cong Sin <ycsin@meta.com>
This commit is contained in:
parent
e879cd0502
commit
eb37b8620b
1 changed files with 32 additions and 0 deletions
|
@ -144,6 +144,20 @@ static struct sample_stats sensor_stats[CONFIG_SENSOR_SHELL_MAX_TRIGGER_DEVICES]
|
|||
|
||||
static const struct device *sensor_trigger_devices[CONFIG_SENSOR_SHELL_MAX_TRIGGER_DEVICES];
|
||||
|
||||
static bool device_is_sensor(const struct device *dev)
|
||||
{
|
||||
#ifdef CONFIG_SENSOR_INFO
|
||||
STRUCT_SECTION_FOREACH(sensor_info, sensor) {
|
||||
if (sensor->dev == dev) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#else
|
||||
return true;
|
||||
#endif /* CONFIG_SENSOR_INFO */
|
||||
}
|
||||
|
||||
static int find_sensor_trigger_device(const struct device *sensor)
|
||||
{
|
||||
for (int i = 0; i < CONFIG_SENSOR_SHELL_MAX_TRIGGER_DEVICES; i++) {
|
||||
|
@ -529,6 +543,12 @@ static int cmd_get_sensor(const struct shell *sh, size_t argc, char *argv[])
|
|||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!device_is_sensor(dev)) {
|
||||
shell_error(sh, "Device is not a sensor (%s)", argv[1]);
|
||||
k_mutex_unlock(&cmd_get_mutex);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (argc == 2) {
|
||||
/* read all channel types */
|
||||
for (int i = 0; i < ARRAY_SIZE(iodev_sensor_shell_channels); ++i) {
|
||||
|
@ -591,6 +611,12 @@ static int cmd_sensor_attr_set(const struct shell *shell_ptr, size_t argc, char
|
|||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!device_is_sensor(dev)) {
|
||||
shell_error(shell_ptr, "Device is not a sensor (%s)", argv[1]);
|
||||
k_mutex_unlock(&cmd_get_mutex);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
for (size_t i = 2; i < argc; i += 3) {
|
||||
int channel = parse_named_int(argv[i], sensor_channel_name,
|
||||
ARRAY_SIZE(sensor_channel_name));
|
||||
|
@ -669,6 +695,12 @@ static int cmd_sensor_attr_get(const struct shell *shell_ptr, size_t argc, char
|
|||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!device_is_sensor(dev)) {
|
||||
shell_error(shell_ptr, "Device is not a sensor (%s)", argv[1]);
|
||||
k_mutex_unlock(&cmd_get_mutex);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (argc > 2) {
|
||||
for (size_t i = 2; i < argc; i += 2) {
|
||||
cmd_sensor_attr_get_handler(shell_ptr, dev, argv[i], argv[i + 1],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue