Lely core libraries  2.3.4
can_chan.c File Reference
#include "io.h"
#include "../can.h"
#include <lely/io2/ctx.h>
#include <lely/io2/linux/can.h>
#include <lely/io2/posix/poll.h>
#include <lely/util/diag.h>
#include <lely/util/spscring.h>
#include <lely/util/time.h>
#include <lely/util/util.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <linux/can/raw.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include "../posix/fd.h"
#include "can_attr.h"
#include "can_err.h"
#include "can_msg.h"
Include dependency graph for can_chan.c:

Go to the source code of this file.

Data Structures

struct  io_can_frame
 
struct  io_can_chan_impl
 The implementation of a CAN channel. More...
 

Macros

#define LELY_IO_CAN_RXLEN   1024
 The default SocketCAN receive queue length (in number of CAN frames).
 

Functions

io_can_chan_tio_can_chan_create (io_poll_t *poll, ev_exec_t *exec, size_t rxlen)
 Creates a new CAN channel. More...
 
void io_can_chan_destroy (io_can_chan_t *chan)
 Destroys a CAN channel. More...
 
int io_can_chan_get_handle (const io_can_chan_t *chan)
 Returns the SocketCAN file descriptor associated with a CAN channel, or -1 if the channel is closed.
 
int io_can_chan_open (io_can_chan_t *chan, const io_can_ctrl_t *ctrl, int flags)
 Opens a CAN channel. More...
 
int io_can_chan_assign (io_can_chan_t *chan, int fd)
 Assigns an existing SocketCAN file descriptor to a CAN channel. More...
 
int io_can_chan_release (io_can_chan_t *chan)
 Dissociates and returns the SocketCAN file descriptor from a CAN channel. More...
 
int io_can_chan_is_open (const io_can_chan_t *chan)
 Returns 1 if the CAN channel is open and 0 if not. More...
 
int io_can_chan_close (io_can_chan_t *chan)
 Closes the SocketCAN file descriptor associated with a CAN channel. More...
 

Detailed Description

This file is part of the I/O library; it contains the CAN channel implementation for Linux.

See also
lely/io2/linux/can.h
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 can_chan.c.

Function Documentation

◆ io_can_chan_create()

io_can_chan_t* io_can_chan_create ( io_poll_t poll,
ev_exec_t exec,
size_t  rxlen 
)

Creates a new CAN channel.

Parameters
polla pointer to the I/O polling instance used to monitor CAN bus events. If NULL, I/O operations MAY cause the event loop to block.
execa pointer to the executor used to execute asynchronous tasks.
rxlenthe receive queue length (in number of frames) of the CAN channel. If rxlen is 0, the default value LELY_IO_CAN_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 382 of file can_chan.c.

◆ io_can_chan_destroy()

void io_can_chan_destroy ( io_can_chan_t chan)

Destroys a CAN channel.

See also
io_can_chan_create()

Definition at line 409 of file can_chan.c.

◆ io_can_chan_open()

int io_can_chan_open ( io_can_chan_t chan,
const io_can_ctrl_t ctrl,
int  flags 
)

Opens a CAN channel.

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

Parameters
chana pointer to a CAN channel.
ctrla pointer to the a controller representing a SocketCAN network interface.
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).
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
Postcondition
on success, io_can_chan_is_open() returns 1.

Definition at line 433 of file can_chan.c.

◆ io_can_chan_assign()

int io_can_chan_assign ( io_can_chan_t chan,
int  fd 
)

Assigns an existing SocketCAN file descriptor to a CAN channel.

Before being assigned, the file descriptor will be modified in the following way:

  • reception of CAN frames sent by the socket is enabled with the CAN_RAW_LOOPBACK and CAN_RAW_RECV_OWN_MSGS socket options, and
  • the size of the kernel send buffer is set to its minimum value.

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

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
Postcondition
on success, io_can_chan_is_open() returns 1.

Definition at line 503 of file can_chan.c.

◆ io_can_chan_release()

int io_can_chan_release ( io_can_chan_t chan)

Dissociates and returns the SocketCAN file descriptor from a CAN channel.

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

Returns
a file descriptor, or -1 if the channel was closed.
Postcondition
io_can_chan_is_open() returns 0.

Definition at line 561 of file can_chan.c.

◆ io_can_chan_is_open()

int io_can_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_can_chan_get_handle(chan) != -1.

Definition at line 569 of file can_chan.c.

◆ io_can_chan_close()

int io_can_chan_close ( io_can_chan_t chan)

Closes the SocketCAN file descriptor associated with a CAN channel.

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

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc(). Note that the file descriptor is closed even when this function reports error.
Postcondition
io_can_chan_is_open() returns 0.

Definition at line 575 of file can_chan.c.