Lely core libraries 2.3.4
can_net.c File Reference

This file is part of the I/O library; it contains the implementation of the CAN network interface. More...

#include "io2.h"
#include <lely/can/net.h>
#include <lely/io2/can_net.h>
#include <lely/io2/ctx.h>
#include <lely/libc/stdlib.h>
#include <lely/libc/threads.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 dependency graph for can_net.c:

Go to the source code of this file.

Data Structures

struct  io_can_net
 The implementation of a CAN network interface. More...
 

Macros

#define LELY_IO_CAN_NET_TXLEN   1000
 The default length (in number of CAN frames) of the user-space transmit queue of a CAN network interface.
 
#define LELY_IO_CAN_NET_TXTIMEO   100
 The default timeout (in milliseconds) of a CAN network interface when waiting for a CAN frame write confirmation.
 

Functions

io_can_net_tio_can_net_create (ev_exec_t *exec, io_timer_t *timer, io_can_chan_t *chan, size_t txlen, int txtimeo)
 Creates a new CAN network interface.
 
void io_can_net_destroy (io_can_net_t *net)
 Destroys a CAN network interface.
 
void io_can_net_start (io_can_net_t *net)
 Starts a CAN network interface and begins processing CAN frames.
 
io_ctx_tio_can_net_get_ctx (const io_can_net_t *net)
 Returns a pointer to the I/O context with which the CAN network interface is registered.
 
ev_exec_tio_can_net_get_exec (const io_can_net_t *net)
 Returns a pointer to the executor used by the CAN network interface to execute asynchronous tasks.
 
io_clock_tio_can_net_get_clock (const io_can_net_t *net)
 Returns a pointer to the clock used by the CAN network interface.
 
io_tqueue_tio_can_net_get_tqueue (const io_can_net_t *net)
 Returns a pointer to the internal timer queue of a CAN network interface.
 
void io_can_net_get_on_read_error_func (const io_can_net_t *net, io_can_net_on_error_func_t **pfunc, void **parg)
 Retrieves the function invoked when a new CAN frame read error occurs, or when a read operation completes successfully after one or more errors.
 
void io_can_net_set_on_read_error_func (io_can_net_t *net, io_can_net_on_error_func_t *func, void *arg)
 Sets the function invoked when a new CAN frame read error occurs, or when a read operation completes successfully after one or more errors.
 
void io_can_net_get_on_queue_error_func (const io_can_net_t *net, io_can_net_on_error_func_t **pfunc, void **parg)
 Retrieves the function invoked when a CAN frame is dropped because the transmit queue is full, or when a frame is successfully queued after one or more errors.
 
void io_can_net_set_on_queue_error_func (io_can_net_t *net, io_can_net_on_error_func_t *func, void *arg)
 Sets the function invoked when a CAN frame is dropped because the transmit queue is full, or when a frame is successfully queued after one or more errors.
 
void io_can_net_get_on_write_error_func (const io_can_net_t *net, io_can_net_on_error_func_t **pfunc, void **parg)
 Retrieves the function invoked when a new CAN frame write error occurs, or when a write operation completes successfully after one or more errors.
 
void io_can_net_set_on_write_error_func (io_can_net_t *net, io_can_net_on_error_func_t *func, void *arg)
 Sets the function invoked when a new CAN frame write error occurs, or when a write operation completes successfully after one or more errors.
 
void io_can_net_get_on_can_state_func (const io_can_net_t *net, io_can_net_on_can_state_func_t **pfunc, void **parg)
 Retrieves the function invoked when a CAN bus state change is detected.
 
void io_can_net_set_on_can_state_func (io_can_net_t *net, io_can_net_on_can_state_func_t *func, void *arg)
 Sets the function to be invoked when a CAN bus state change is detected.
 
void io_can_net_get_on_can_error_func (const io_can_net_t *net, io_can_net_on_can_error_func_t **pfunc, void **parg)
 Retrieves the function invoked when a CAN bus error is detected.
 
void io_can_net_set_on_can_error_func (io_can_net_t *net, io_can_net_on_can_error_func_t *func, void *arg)
 Sets the function to be invoked when a CAN bus error is detected.
 
int io_can_net_lock (io_can_net_t *net)
 Locks the mutex protecting the CAN network interface.
 
int io_can_net_unlock (io_can_net_t *net)
 Unlocks the mutex protecting the CAN network interface.
 
can_net_tio_can_net_get_net (const io_can_net_t *net)
 Returns a pointer to the internal interface of a CAN network interface.
 
int io_can_net_set_time (io_can_net_t *net)
 Updates the CAN network time.
 

Detailed Description

This file is part of the I/O library; it contains the implementation of the CAN network interface.

See also
lely/io2/can_net.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_net.c.

Function Documentation

◆ io_can_net_create()

io_can_net_t * io_can_net_create ( ev_exec_t exec,
io_timer_t timer,
io_can_chan_t chan,
size_t  txlen,
int  txtimeo 
)

Creates a new CAN network interface.

Parameters
execa pointer to the executor used to execute asynchronous tasks. If exec is NULL, the CAN channel executor is used.
timera pointer to a timer. This timer MUST NOT be used for any other purpose.
chana pointer to a CAN channel. This channel MUST NOT be used for any other purpose.
txlenthe length (in number of frames) of the user-space transmit queue length. If txlen is 0, the default value LELY_IO_CAN_NET_TXLEN is used.
txtimeothe timeout (in milliseconds) when waiting for a CAN frame write confirmation. If txtimeo is 0, the default value #LELY_IO_CAN_CTX_TXTIMEO is used. If txtimeo is negative. the timeout is disabled.
Returns
a pointer to a new CAN network interface, or NULL on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 384 of file can_net.c.

◆ io_can_net_start()

void io_can_net_start ( io_can_net_t net)

Starts a CAN network interface and begins processing CAN frames.

This function locks the mutex protecting the CAN network interface.

Definition at line 422 of file can_net.c.

◆ io_can_net_get_on_read_error_func()

void io_can_net_get_on_read_error_func ( const io_can_net_t net,
io_can_net_on_error_func_t **  pfunc,
void **  parg 
)

Retrieves the function invoked when a new CAN frame read error occurs, or when a read operation completes successfully after one or more errors.

Parameters
neta pointer to a CAN network interface.
pfuncthe address at which to store a pointer to the function (can be NULL).
pargthe address at which to store the user-specified argument (can be NULL).
See also
io_can_net_set_on_read_error_func()

Definition at line 479 of file can_net.c.

◆ io_can_net_set_on_read_error_func()

void io_can_net_set_on_read_error_func ( io_can_net_t net,
io_can_net_on_error_func_t func,
void *  arg 
)

Sets the function invoked when a new CAN frame read error occurs, or when a read operation completes successfully after one or more errors.

Parameters
neta pointer to a CAN network interface.
funca pointer to the function to be invoked. If func is NULL, the default implementation will be used.
argthe user-specified argument (can be NULL). arg is passed as the last argument to func.
See also
io_can_net_get_on_read_error_func()

Definition at line 497 of file can_net.c.

◆ io_can_net_get_on_queue_error_func()

void io_can_net_get_on_queue_error_func ( const io_can_net_t net,
io_can_net_on_error_func_t **  pfunc,
void **  parg 
)

Retrieves the function invoked when a CAN frame is dropped because the transmit queue is full, or when a frame is successfully queued after one or more errors.

Parameters
neta pointer to a CAN network interface.
pfuncthe address at which to store a pointer to the function (can be NULL).
pargthe address at which to store the user-specified argument (can be NULL).
See also
io_can_net_set_on_queue_error_func()

Definition at line 513 of file can_net.c.

◆ io_can_net_set_on_queue_error_func()

void io_can_net_set_on_queue_error_func ( io_can_net_t net,
io_can_net_on_error_func_t func,
void *  arg 
)

Sets the function invoked when a CAN frame is dropped because the transmit queue is full, or when a frame is successfully queued after one or more errors.

Parameters
neta pointer to a CAN network interface.
funca pointer to the function to be invoked. If func is NULL, the default implementation will be used.
argthe user-specified argument (can be NULL). arg is passed as the last argument to func.
See also
io_can_net_get_on_queue_error_func()

Definition at line 531 of file can_net.c.

◆ io_can_net_get_on_write_error_func()

void io_can_net_get_on_write_error_func ( const io_can_net_t net,
io_can_net_on_error_func_t **  pfunc,
void **  parg 
)

Retrieves the function invoked when a new CAN frame write error occurs, or when a write operation completes successfully after one or more errors.

Parameters
neta pointer to a CAN network interface.
pfuncthe address at which to store a pointer to the function (can be NULL).
pargthe address at which to store the user-specified argument (can be NULL).
See also
io_can_net_set_on_write_error_func()

Definition at line 547 of file can_net.c.

◆ io_can_net_set_on_write_error_func()

void io_can_net_set_on_write_error_func ( io_can_net_t net,
io_can_net_on_error_func_t func,
void *  arg 
)

Sets the function invoked when a new CAN frame write error occurs, or when a write operation completes successfully after one or more errors.

Parameters
neta pointer to a CAN network interface.
funca pointer to the function to be invoked. If func is NULL, the default implementation will be used.
argthe user-specified argument (can be NULL). arg is passed as the last argument to func.
See also
io_can_net_get_on_write_error_func()

Definition at line 565 of file can_net.c.

◆ io_can_net_get_on_can_state_func()

void io_can_net_get_on_can_state_func ( const io_can_net_t net,
io_can_net_on_can_state_func_t **  pfunc,
void **  parg 
)

Retrieves the function invoked when a CAN bus state change is detected.

Parameters
neta pointer to a CAN network interface.
pfuncthe address at which to store a pointer to the function (can be NULL).
pargthe address at which to store the user-specified argument (can be NULL).
See also
io_can_net_set_on_can_state_func()

Definition at line 581 of file can_net.c.

◆ io_can_net_set_on_can_state_func()

void io_can_net_set_on_can_state_func ( io_can_net_t net,
io_can_net_on_can_state_func_t func,
void *  arg 
)

Sets the function to be invoked when a CAN bus state change is detected.

Parameters
neta pointer to a CAN network interface.
funca pointer to the function to be invoked. If func is NULL, the default implementation will be used.
argthe user-specified argument (can be NULL). arg is passed as the last argument to func.
See also
io_can_net_get_on_can_state_func()

Definition at line 599 of file can_net.c.

◆ io_can_net_get_on_can_error_func()

void io_can_net_get_on_can_error_func ( const io_can_net_t net,
io_can_net_on_can_error_func_t **  pfunc,
void **  parg 
)

Retrieves the function invoked when a CAN bus error is detected.

Parameters
neta pointer to a CAN network interface.
pfuncthe address at which to store a pointer to the function (can be NULL).
pargthe address at which to store the user-specified argument (can be NULL).
See also
io_can_net_set_on_can_error_func()

Definition at line 615 of file can_net.c.

◆ io_can_net_set_on_can_error_func()

void io_can_net_set_on_can_error_func ( io_can_net_t net,
io_can_net_on_can_error_func_t func,
void *  arg 
)

Sets the function to be invoked when a CAN bus error is detected.

Parameters
neta pointer to a CAN network interface.
funca pointer to the function to be invoked. If func is NULL, the default implementation will be used.
argthe user-specified argument (can be NULL). arg is passed as the last argument to func.
See also
io_can_net_get_on_can_error_func()

Definition at line 633 of file can_net.c.

◆ io_can_net_lock()

int io_can_net_lock ( io_can_net_t net)

Locks the mutex protecting the CAN network interface.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc();
See also
io_can_net_unlock()

Definition at line 649 of file can_net.c.

◆ io_can_net_unlock()

int io_can_net_unlock ( io_can_net_t net)

Unlocks the mutex protecting the CAN network interface.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc();
See also
io_can_net_lock()

Definition at line 663 of file can_net.c.

◆ io_can_net_get_net()

can_net_t * io_can_net_get_net ( const io_can_net_t net)

Returns a pointer to the internal interface of a CAN network interface.

The mutex protecting the CAN network interface MUST be locked when using the internal interface.

See also
io_can_net_lock(), io_can_net_unlock()

Definition at line 678 of file can_net.c.

◆ io_can_net_set_time()

int io_can_net_set_time ( io_can_net_t net)

Updates the CAN network time.

The mutex protecting the CAN network interface MUST be locked for the duration of this call.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_can_net_lock(), io_can_net_unlock()

Definition at line 686 of file can_net.c.