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 <dalachowsky@gmail.com>
This commit is contained in:
Kacper Dalach 2024-07-04 08:37:30 +02:00 committed by Anas Nashif
commit 32eb346e05
3 changed files with 42 additions and 4 deletions

View file

@ -500,10 +500,14 @@ The following peripherals are currently provided with this board:
:ref:`its section <nsim_per_disp_sdl>`. :ref:`its section <nsim_per_disp_sdl>`.
**CAN controller** **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 enabled with :kconfig:option:`CONFIG_CAN_NATIVE_LINUX` and configured with the device tree binding
:dtcompatible:`zephyr,native-linux-can`. :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: .. _native_ptty_uart:
PTTY UART PTTY UART

View file

@ -9,6 +9,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <cmdline.h>
#include <posix_native_task.h>
#include <zephyr/drivers/can.h> #include <zephyr/drivers/can.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
@ -46,6 +48,8 @@ struct can_native_linux_config {
const char *if_name; const char *if_name;
}; };
static const char *if_name_cmd_opt;
static void dispatch_frame(const struct device *dev, struct can_frame *frame) static void dispatch_frame(const struct device *dev, struct can_frame *frame)
{ {
struct can_native_linux_data *data = dev->data; 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; const struct can_native_linux_config *cfg = dev->config;
struct can_native_linux_data *data = dev->data; struct can_native_linux_data *data = dev->data;
const char *if_name;
k_mutex_init(&data->filter_mutex); k_mutex_init(&data->filter_mutex);
k_sem_init(&data->tx_idle, 1, 1); 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) { 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; return -ENODEV;
} }
@ -482,3 +494,22 @@ CAN_DEVICE_DT_INST_DEFINE(inst, can_native_linux_init, NULL, \
&can_native_linux_driver_api); &can_native_linux_driver_api);
DT_INST_FOREACH_STATUS_OKAY(CAN_NATIVE_LINUX_INIT) 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);

View file

@ -11,4 +11,7 @@ properties:
host-interface: host-interface:
type: string type: string
required: true 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.