Skip to content

Commit

Permalink
posix: can: if name from command-line
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Dalachowsky committed Jul 4, 2024
1 parent 2437832 commit 189cf20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
6 changes: 5 additions & 1 deletion boards/native/native_sim/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,14 @@ The following peripherals are currently provided with this board:
:ref:`its section <nsim_per_disp_sdl>`.

**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 CAN interface which will be used by the app using the ``--can-if``
command-line option. This option overrides **every** CAN driver to use the specified
interface.

.. _native_ptty_uart:

PTTY UART
Expand Down
35 changes: 33 additions & 2 deletions drivers/can/can_native_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <cmdline.h>
#include <posix_native_task.h>

#include <zephyr/drivers/can.h>
#include <zephyr/kernel.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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_INF("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;
}

Expand Down Expand Up @@ -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 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);

0 comments on commit 189cf20

Please sign in to comment.