Skip to content

Commit

Permalink
drivers: can: Check CAN device API in shell commands
Browse files Browse the repository at this point in the history
Update CAN shell commands to filter and verify the device's API type.

Signed-off-by: Pieter De Gendt <[email protected]>
  • Loading branch information
pdgendt committed Nov 28, 2024
1 parent f7e694a commit e9c92f4
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions drivers/can/can_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ static struct k_poll_event can_shell_rx_msgq_events[] = {
static void can_shell_tx_msgq_triggered_work_handler(struct k_work *work);
static void can_shell_rx_msgq_triggered_work_handler(struct k_work *work);

static bool can_device_check(const struct device *dev)
{
return DEVICE_API_IS(can, dev) && device_is_ready(dev);
}

#ifdef CONFIG_CAN_SHELL_SCRIPTING_FRIENDLY
static void can_shell_dummy_bypass_cb(const struct shell *sh, uint8_t *data, size_t len)
{
Expand Down Expand Up @@ -279,7 +284,7 @@ static int cmd_can_start(const struct shell *sh, size_t argc, char **argv)
const struct device *dev = device_get_binding(argv[1]);
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand All @@ -300,7 +305,7 @@ static int cmd_can_stop(const struct shell *sh, size_t argc, char **argv)
const struct device *dev = device_get_binding(argv[1]);
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -331,7 +336,7 @@ static int cmd_can_show(const struct shell *sh, size_t argc, char **argv)
can_mode_t cap;
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -436,7 +441,7 @@ static int cmd_can_bitrate_set(const struct shell *sh, size_t argc, char **argv)
char *endptr;
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -507,7 +512,7 @@ static int cmd_can_dbitrate_set(const struct shell *sh, size_t argc, char **argv
char *endptr;
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -613,7 +618,7 @@ static int cmd_can_timing_set(const struct shell *sh, size_t argc, char **argv)
struct can_timing timing = { 0 };
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -642,7 +647,7 @@ static int cmd_can_dtiming_set(const struct shell *sh, size_t argc, char **argv)
struct can_timing timing = { 0 };
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -675,7 +680,7 @@ static int cmd_can_mode_set(const struct shell *sh, size_t argc, char **argv)
int i;
int j;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -727,7 +732,7 @@ static int cmd_can_send(const struct shell *sh, size_t argc, char **argv)
int err;
int i;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -844,7 +849,7 @@ static int cmd_can_filter_add(const struct shell *sh, size_t argc, char **argv)
char *endptr;
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -940,7 +945,7 @@ static int cmd_can_filter_remove(const struct shell *sh, size_t argc, char **arg
int filter_id;
char *endptr;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand All @@ -966,7 +971,7 @@ static int cmd_can_recover(const struct shell *sh, size_t argc, char **argv)
char *endptr;
int err;

if (!device_is_ready(dev)) {
if (!can_device_check(dev)) {
shell_error(sh, "device %s not ready", argv[1]);
return -ENODEV;
}
Expand Down Expand Up @@ -996,7 +1001,7 @@ static int cmd_can_recover(const struct shell *sh, size_t argc, char **argv)

static void cmd_can_device_name(size_t idx, struct shell_static_entry *entry)
{
const struct device *dev = shell_device_lookup(idx, NULL);
const struct device *dev = shell_device_filter(idx, can_device_check);

entry->syntax = (dev != NULL) ? dev->name : NULL;
entry->handler = NULL;
Expand Down

0 comments on commit e9c92f4

Please sign in to comment.