Browse Source

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>
pull/76381/head
Kacper Dalach 1 year ago committed by Anas Nashif
parent
commit
32eb346e05
  1. 6
      boards/native/native_sim/doc/index.rst
  2. 35
      drivers/can/can_native_linux.c
  3. 5
      dts/bindings/can/zephyr,native-linux-can.yaml

6
boards/native/native_sim/doc/index.rst

@ -500,10 +500,14 @@ The following peripherals are currently provided with this board: @@ -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 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

35
drivers/can/can_native_linux.c

@ -9,6 +9,8 @@ @@ -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>
@ -46,6 +48,8 @@ struct can_native_linux_config { @@ -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) @@ -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, \ @@ -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);

5
dts/bindings/can/zephyr,native-linux-can.yaml

@ -11,4 +11,7 @@ properties: @@ -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.

Loading…
Cancel
Save