Lely core libraries  2.3.4
msg.h File Reference

This header file is part of the CAN library; it contains the CAN frame declarations. More...

#include <lely/features.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for msg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  can_msg
 A CAN or CAN FD format frame. More...
 

Macros

#define CAN_MASK_BID   UINT32_C(0x000007ff)
 The mask used to extract the 11-bit Base Identifier from a CAN frame.
 
#define CAN_MASK_EID   UINT32_C(0x1fffffff)
 The mask used to extract the 29-bit Extended Identifier from a CAN frame.
 
#define CAN_MAX_LEN   8
 The maximum number of bytes in the payload of a CAN format frame.
 
#define CANFD_MAX_LEN   64
 The maximum number of bytes in the payload of a CAN FD format frame.
 
#define CAN_MSG_MAX_LEN   CANFD_MAX_LEN
 The maximum number of bytes in the payload of a can_msg struct.
 
#define CAN_MSG_INIT
 The static initializer for can_msg.
 

Enumerations

enum  {
  CAN_FLAG_IDE = 1u << 0 , CAN_FLAG_RTR = 1u << 1 , CAN_FLAG_FDF = 1u << 2 , CAN_FLAG_EDL = CAN_FLAG_FDF ,
  CAN_FLAG_BRS = 1u << 3 , CAN_FLAG_ESI = 1u << 4
}
 The flags of a CAN or CAN FD format frame. More...
 
enum  can_msg_bits_mode { CAN_MSG_BITS_MODE_NO_STUFF , CAN_MSG_BITS_MODE_WORST , CAN_MSG_BITS_MODE_EXACT }
 The method used to compute te size (in bits) of a CAN frame. More...
 

Functions

int can_msg_bits (const struct can_msg *msg, enum can_msg_bits_mode mode)
 Computes the size (in bits) of the specified CAN format frame on the CAN bus. More...
 
int snprintf_can_msg (char *s, size_t n, const struct can_msg *msg)
 Prints the contents of a CAN or CAN FD format frame to a string buffer. More...
 
int asprintf_can_msg (char **ps, const struct can_msg *msg)
 Equivalent to snprintf_can_msg(), except that it allocates a string large enough to hold the output, including the terminating null byte. More...
 
uint_least16_t can_crc (uint_least16_t crc, const void *ptr, int off, size_t bits)
 Computes a bitwise CRC-15-CAN checksum, based on the 0x4599 generator polynomial. More...
 

Detailed Description

This header file is part of the CAN library; it contains the CAN frame declarations.

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 msg.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

The flags of a CAN or CAN FD format frame.

Enumerator
CAN_FLAG_IDE 

The Identifier Extension (IDE) flag.

If this flag is set, the CAN Extended Format (with a 29-bit identifier) is used, otherwise the CAN Base Format (with an 11-bit identifier) is used.

CAN_FLAG_RTR 

The Remote Transmission Request (RTR) flag (unavailable in CAN FD format frames).

If this flag is set, the frame has no payload.

CAN_FLAG_FDF 

The FD Format (FDF) flag, formerly known as Extended Data Length (EDL).

This flag is set for CAN FD format frames.

CAN_FLAG_BRS 

The Bit Rate Switch (BRS) flag (only available in CAN FD format frames).

If this flag is set, the bit rate is switched from the standard bit rate of the arbitration phase to the preconfigured alternate bit rate of the data phase.

CAN_FLAG_ESI 

The Error State Indicator (ESI) flag (only available in CAN FD format frames).

Definition at line 37 of file msg.h.

◆ can_msg_bits_mode

The method used to compute te size (in bits) of a CAN frame.

Enumerator
CAN_MSG_BITS_MODE_NO_STUFF 

Simple calculation assuming no bit stuffing.

CAN_MSG_BITS_MODE_WORST 

Simple worst case estimate.

CAN_MSG_BITS_MODE_EXACT 

Exact calculation based of frame content and CRC.

Definition at line 127 of file msg.h.

Function Documentation

◆ can_msg_bits()

int can_msg_bits ( const struct can_msg msg,
enum can_msg_bits_mode  mode 
)

Computes the size (in bits) of the specified CAN format frame on the CAN bus.

Parameters
msga pointer to a CAN frame format frame.
modethe method used to compute the size (one of CAN_MSG_BITS_MODE_NO_STUFF, CAN_MSG_BITS_MODE_WORST or CAN_MSG_BITS_MODE_EXACT).
Returns
the number of bits on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 48 of file msg.c.

◆ snprintf_can_msg()

int snprintf_can_msg ( char *  s,
size_t  n,
const struct can_msg msg 
)

Prints the contents of a CAN or CAN FD format frame to a string buffer.

The output mimics that of candump.

Parameters
sthe address of the output buffer. If s is not NULL, at most n - 1 characters are written, plus a terminating null byte.
nthe size (in bytes) of the buffer at s. If n is zero, nothing is written.
msga pointer to the CAN frame to be printed.
Returns
the number of characters that would have been written had the buffer been sufficiently large, not counting the terminating null byte, or a negative number on error. In the latter case, the error number is stored in errno.

Definition at line 186 of file msg.c.

◆ asprintf_can_msg()

int asprintf_can_msg ( char **  ps,
const struct can_msg msg 
)

Equivalent to snprintf_can_msg(), except that it allocates a string large enough to hold the output, including the terminating null byte.

Parameters
psthe address of a value which, on success, contains a pointer to the allocated string. This pointer SHOULD be passed to free() to release the allocated storage.
msga pointer to the CAN frame to be printed.
Returns
the number of characters written, not counting the terminating null byte, or a negative number on error. In the latter case, the error number is stored in errno.

Definition at line 252 of file msg.c.

◆ can_crc()

uint_least16_t can_crc ( uint_least16_t  crc,
const void *  ptr,
int  off,
size_t  bits 
)

Computes a bitwise CRC-15-CAN checksum, based on the 0x4599 generator polynomial.

The implementation uses a table with precomputed values for efficiency.

Parameters
crcthe initial value.
ptra pointer to the bits to be hashed.
offthe offset (in bits) with respect to ptr of the first bit to be hashed.
bitsthe number of bits to hash.
Returns
the updated CRC.

Definition at line 278 of file msg.c.