Lely core libraries  2.2.5
vcan.h File Reference
#include <lely/io2/can.h>
#include <lely/io2/clock.h>
Include dependency graph for vcan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

io_can_ctrl_tio_vcan_ctrl_create (io_clock_t *clock, int flags, int nominal, int data, int state)
 Creates a new virtual CAN controller. More...
 
void io_vcan_ctrl_destroy (io_can_ctrl_t *ctrl)
 Destroys a virtual CAN controller. More...
 
void io_vcan_ctrl_set_state (io_can_ctrl_t *ctrl, int state)
 Sets the state of a virtual CAN bus: one of CAN_STATE_ACTIVE, CAN_STATE_PASSIVE, CAN_STATE_BUSOFF, CAN_STATE_SLEEPING or CAN_STATE_STOPPED. More...
 
int io_vcan_ctrl_write_msg (io_can_ctrl_t *ctrl, const struct can_msg *msg, int timeout)
 Writes a CAN frame to a all virtual CAN channels registered with a virtual CAN controller. More...
 
int io_vcan_ctrl_write_err (io_can_ctrl_t *ctrl, const struct can_err *err, int timeout)
 Writes a CAN error frame to a all virtual CAN channels registered with a virtual CAN controller. More...
 
io_can_chan_tio_vcan_chan_create (io_ctx_t *ctx, ev_exec_t *exec, size_t rxlen)
 Creates a new virtual CAN channel. More...
 
void io_vcan_chan_destroy (io_can_chan_t *chan)
 Destroys a virtual CAN channel. More...
 
io_can_ctrl_tio_vcan_chan_get_ctrl (const io_can_chan_t *chan)
 Returns a pointer to the virtual CAN controller with which a virtual CAN channel is registered, or NULL if the channel is closed.
 
void io_vcan_chan_open (io_can_chan_t *chan, io_can_ctrl_t *ctrl)
 Opens a virtual CAN channel by registering it with the specified virtual CAN controller. More...
 
int io_vcan_chan_is_open (const io_can_chan_t *chan)
 Returns 1 if the CAN channel is open and 0 if not. More...
 
void io_vcan_chan_close (io_can_chan_t *chan)
 Closes a virtual CAN channel. More...
 

Detailed Description

This header file is part of the I/O library; it contains the virtual CAN bus declarations.

A virtual CAN bus makes it possible to write platform-independent tests for CAN applications and to create controlled error conditions. It consists of a virtual CAN controller with which one or more virtual CAN channels are registered. When a virtual channel sends a message, it is placed into the receive queue of all other channels. Each channel only as a receive queue. Send operations succeed if every (other) channel has a slot available in their receive queue; otherwise they block or time out.

Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file vcan.h.

Function Documentation

◆ io_vcan_ctrl_create()

io_can_ctrl_t* io_vcan_ctrl_create ( io_clock_t clock,
int  flags,
int  nominal,
int  data,
int  state 
)

Creates a new virtual CAN controller.

Parameters
clocka pointer to the clock used to obtain the timestamp when sending CAN frames.
flagsthe flags specifying which CAN bus features MUST be enabled (any combination of IO_CAN_BUS_FLAG_ERR, IO_CAN_BUS_FLAG_FDF and IO_CAN_BUS_FLAG_BRS).
nominalthe nominal bitrate. For the CAN FD protocol, this is the bit rate of the arbitration phase. If nominal is 0, the default value LELY_IO_VCAN_BITRATE is used.
datathe data bitrate. This bit rate is only defined for the CAN FD protocol; the value is ignored otherwise. If data is 0, the data bitrate equals the nominal bitrate.
statethe initial state of the virtual CAN bus (one of CAN_STATE_ACTIVE, CAN_STATE_PASSIVE, CAN_STATE_BUSOFF, CAN_STATE_SLEEPING or CAN_STATE_STOPPED). If state is CAN_STATE_STOPPED, the controller is stopped.
Returns
a pointer to a new CAN controller, or NULL on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 341 of file vcan.c.

◆ io_vcan_ctrl_destroy()

void io_vcan_ctrl_destroy ( io_can_ctrl_t ctrl)

Destroys a virtual CAN controller.

See also
io_vcan_ctrl_create()

Definition at line 370 of file vcan.c.

◆ io_vcan_ctrl_set_state()

void io_vcan_ctrl_set_state ( io_can_ctrl_t ctrl,
int  state 
)

Sets the state of a virtual CAN bus: one of CAN_STATE_ACTIVE, CAN_STATE_PASSIVE, CAN_STATE_BUSOFF, CAN_STATE_SLEEPING or CAN_STATE_STOPPED.

If the requested state is CAN_STATE_STOPPED, the controller will be stopped as if by io_can_ctrl_stop() and all pending and future I/O operations will fail with error number ERRNUM_NETDOWN. Subsequent calls to this function have no effect until the controller is restarted with io_can_ctrl_restart().

If the requested state differs from the current state and is not CAN_STATE_STOPPED, and the controller supports error frames (IO_CAN_BUS_FLAG_ERR), an error frame with the new state is sent to all registered virtual CAN channels.

See also
io_can_ctrl_get_state()

Definition at line 379 of file vcan.c.

◆ io_vcan_ctrl_write_msg()

int io_vcan_ctrl_write_msg ( io_can_ctrl_t ctrl,
const struct can_msg msg,
int  timeout 
)

Writes a CAN frame to a all virtual CAN channels registered with a virtual CAN controller.

This function blocks until the frame is written or an error occurs.

Parameters
ctrla pointer to a virtual CAN controller.
msga pointer to the CAN frame to be written.
timeoutthe maximum number of milliseconds this function will block. If timeout is negative, this function will block indefinitely.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_vcan_ctrl_write_err()

Definition at line 408 of file vcan.c.

◆ io_vcan_ctrl_write_err()

int io_vcan_ctrl_write_err ( io_can_ctrl_t ctrl,
const struct can_err err,
int  timeout 
)

Writes a CAN error frame to a all virtual CAN channels registered with a virtual CAN controller.

This function blocks until the error frame is written or an error occurs. If the IO_CAN_BUS_FLAG_ERR flag was not specified when the controller was created with io_vcan_ctrl_create(), the controller does not support error frames and this function will fail.

Parameters
ctrla pointer to a virtual CAN controller.
erra pointer to the CAN error frame to be written.
timeoutthe maximum number of milliseconds this function will block. If timeout is negative, this function will block indefinitely.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_vcan_ctrl_write_msg()

Definition at line 415 of file vcan.c.

◆ io_vcan_chan_create()

io_can_chan_t* io_vcan_chan_create ( io_ctx_t ctx,
ev_exec_t exec,
size_t  rxlen 
)

Creates a new virtual CAN channel.

Parameters
ctxa pointer to the I/O context with which the channel should be registered.
execa pointer to the executor used to execute asynchronous tasks.
rxlenthe receive queue length (in number of frames) of the channel. If rxlen is 0, the default value LELY_IO_VCAN_RXLEN is used.
Returns
a pointer to a new CAN channel, or NULL on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 556 of file vcan.c.

◆ io_vcan_chan_destroy()

void io_vcan_chan_destroy ( io_can_chan_t chan)

Destroys a virtual CAN channel.

See also
io_vcan_chan_create()

Definition at line 583 of file vcan.c.

◆ io_vcan_chan_open()

void io_vcan_chan_open ( io_can_chan_t chan,
io_can_ctrl_t ctrl 
)

Opens a virtual CAN channel by registering it with the specified virtual CAN controller.

If the channel was already open, it is first closed as if by io_vcan_chan_close().

Postcondition
io_vcan_chan_is_open() returns 1.

Definition at line 608 of file vcan.c.

◆ io_vcan_chan_is_open()

int io_vcan_chan_is_open ( const io_can_chan_t chan)

Returns 1 if the CAN channel is open and 0 if not.

This function is equivalent to io_vcan_chan_get_ctrl(chan) != NULL.

Definition at line 616 of file vcan.c.

◆ io_vcan_chan_close()

void io_vcan_chan_close ( io_can_chan_t chan)

Closes a virtual CAN channel.

Any pending read or write operations are canceled as if by io_can_chan_cancel_read() and io_can_chan_cancel_write().

Postcondition
io_vcan_chan_is_open() returns 0.

Definition at line 622 of file vcan.c.