Skip to content

Commit e16dece

Browse files
pdgendtfabiobaltieri
authored andcommitted
drivers: sensor: shell: Check sensor API type
Update sensor shell commands to filter and verify the device's API type. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent a553af7 commit e16dece

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

drivers/sensor/sensor_shell.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ static int find_sensor_trigger_device(const struct device *sensor)
169169
return -1;
170170
}
171171

172+
static bool sensor_device_check(const struct device *dev)
173+
{
174+
return DEVICE_API_IS(sensor, dev);
175+
}
176+
172177
/* Forward declaration */
173178
static void data_ready_trigger_handler(const struct device *sensor,
174179
const struct sensor_trigger *trigger);
@@ -544,8 +549,8 @@ static int cmd_get_sensor(const struct shell *sh, size_t argc, char *argv[])
544549
}
545550

546551
dev = device_get_binding(argv[1]);
547-
if (dev == NULL) {
548-
shell_error(sh, "Device unknown (%s)", argv[1]);
552+
if (dev == NULL || !sensor_device_check(dev)) {
553+
shell_error(sh, "Sensor device unknown (%s)", argv[1]);
549554
k_mutex_unlock(&cmd_get_mutex);
550555
return -ENODEV;
551556
}
@@ -613,8 +618,8 @@ static int cmd_sensor_attr_set(const struct shell *shell_ptr, size_t argc, char
613618
int rc;
614619

615620
dev = device_get_binding(argv[1]);
616-
if (dev == NULL) {
617-
shell_error(shell_ptr, "Device unknown (%s)", argv[1]);
621+
if (dev == NULL || !sensor_device_check(dev)) {
622+
shell_error(shell_ptr, "Sensor device unknown (%s)", argv[1]);
618623
return -ENODEV;
619624
}
620625

@@ -697,8 +702,8 @@ static int cmd_sensor_attr_get(const struct shell *shell_ptr, size_t argc, char
697702
const struct device *dev;
698703

699704
dev = device_get_binding(argv[1]);
700-
if (dev == NULL) {
701-
shell_error(shell_ptr, "Device unknown (%s)", argv[1]);
705+
if (dev == NULL || !sensor_device_check(dev)) {
706+
shell_error(shell_ptr, "Sensor device unknown (%s)", argv[1]);
702707
return -ENODEV;
703708
}
704709

@@ -848,7 +853,7 @@ SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
848853

849854
static void device_name_get(size_t idx, struct shell_static_entry *entry)
850855
{
851-
const struct device *dev = shell_device_lookup(idx, NULL);
856+
const struct device *dev = shell_device_filter(idx, sensor_device_check);
852857

853858
current_cmd_ctx = CTX_GET;
854859
entry->syntax = (dev != NULL) ? dev->name : NULL;
@@ -1047,8 +1052,8 @@ static int cmd_trig_sensor(const struct shell *sh, size_t argc, char **argv)
10471052

10481053
/* Parse device name */
10491054
dev = device_get_binding(argv[1]);
1050-
if (dev == NULL) {
1051-
shell_error(sh, "Device unknown (%s)", argv[1]);
1055+
if (dev == NULL || !sensor_device_check(dev)) {
1056+
shell_error(sh, "Sensor device unknown (%s)", argv[1]);
10521057
return -ENODEV;
10531058
}
10541059

0 commit comments

Comments
 (0)