From 32eb346e05763ae6fc71dd9bcad5e02d62f48ce5 Mon Sep 17 00:00:00 2001 From: Kacper Dalach Date: Thu, 4 Jul 2024 08:37:30 +0200 Subject: [PATCH] posix: can: if name from command-line This commit introduces the ability to set the CAN interface from command-line. This is helpful if we want to run multiple instances of the app with different CAN interfaces without making separate compilations for each instance. Signed-off-by: Kacper Dalach --- boards/native/native_sim/doc/index.rst | 6 +++- drivers/can/can_native_linux.c | 35 +++++++++++++++++-- dts/bindings/can/zephyr,native-linux-can.yaml | 5 ++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/boards/native/native_sim/doc/index.rst b/boards/native/native_sim/doc/index.rst index 0a304ca941c..e8aea1ad4f0 100644 --- a/boards/native/native_sim/doc/index.rst +++ b/boards/native/native_sim/doc/index.rst @@ -500,10 +500,14 @@ The following peripherals are currently provided with this board: :ref:`its section `. **CAN controller** - It is possible to use a host CAN controller with the native SockerCAN Linux driver. It can be + It is possible to use a host CAN controller with the native SocketCAN Linux driver. It can be enabled with :kconfig:option:`CONFIG_CAN_NATIVE_LINUX` and configured with the device tree binding :dtcompatible:`zephyr,native-linux-can`. + It is possible to specify which CAN interface will be used by the app using the ``--can-if`` + command-line option. This option overrides **every** Linux SocketCAN driver instance to use the specified + interface. + .. _native_ptty_uart: PTTY UART diff --git a/drivers/can/can_native_linux.c b/drivers/can/can_native_linux.c index 040e6080726..c62faa4f165 100644 --- a/drivers/can/can_native_linux.c +++ b/drivers/can/can_native_linux.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include @@ -46,6 +48,8 @@ struct can_native_linux_config { const char *if_name; }; +static const char *if_name_cmd_opt; + static void dispatch_frame(const struct device *dev, struct can_frame *frame) { struct can_native_linux_data *data = dev->data; @@ -445,13 +449,21 @@ static int can_native_linux_init(const struct device *dev) { const struct can_native_linux_config *cfg = dev->config; struct can_native_linux_data *data = dev->data; + const char *if_name; k_mutex_init(&data->filter_mutex); k_sem_init(&data->tx_idle, 1, 1); - data->dev_fd = linux_socketcan_iface_open(cfg->if_name); + if (if_name_cmd_opt != NULL) { + if_name = if_name_cmd_opt; + } else { + if_name = cfg->if_name; + } + + LOG_DBG("Opening %s", if_name); + data->dev_fd = linux_socketcan_iface_open(if_name); if (data->dev_fd < 0) { - LOG_ERR("Cannot open %s (%d)", cfg->if_name, data->dev_fd); + LOG_ERR("Cannot open %s (%d)", if_name, data->dev_fd); return -ENODEV; } @@ -482,3 +494,22 @@ CAN_DEVICE_DT_INST_DEFINE(inst, can_native_linux_init, NULL, \ &can_native_linux_driver_api); DT_INST_FOREACH_STATUS_OKAY(CAN_NATIVE_LINUX_INIT) + +static void add_native_posix_options(void) +{ + static struct args_struct_t can_native_posix_options[] = { + { + .is_mandatory = false, + .option = "can-if", + .name = "name", + .type = 's', + .dest = (void *)&if_name_cmd_opt, + .descript = "Name of the host CAN interface to use", + }, + ARG_TABLE_ENDMARKER, + }; + + native_add_command_line_opts(can_native_posix_options); +} + +NATIVE_TASK(add_native_posix_options, PRE_BOOT_1, 10); diff --git a/dts/bindings/can/zephyr,native-linux-can.yaml b/dts/bindings/can/zephyr,native-linux-can.yaml index b854984ece0..1dea7d9144a 100644 --- a/dts/bindings/can/zephyr,native-linux-can.yaml +++ b/dts/bindings/can/zephyr,native-linux-can.yaml @@ -11,4 +11,7 @@ properties: host-interface: type: string required: true - description: Linux host interface name (e.g. zcan0, vcan0, can0, ...) + description: | + Linux host interface name (e.g. zcan0, vcan0, can0, ...). + This property can be overridden using the --can-if command-line + option. Note that it applies for every instance of this driver.