Lely core libraries  2.2.5
msg.c File Reference
#include "can.h"
#include <lely/can/msg.h>
#include <lely/util/bits.h>
#include <lely/util/errnum.h>
#include <lely/util/util.h>
#include <assert.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
Include dependency graph for msg.c:

Go to the source code of this file.

Functions

static 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...
 
static uint_least16_t can_crc_bits (uint_least16_t crc, uint_least8_t byte, int off, int bits)
 Computes a bitwise CRC-15-CAN checksum of a single byte. More...
 
static uint_least16_t can_crc_bytes (uint_least16_t crc, const unsigned char *bp, size_t n)
 Computes a CRC-15-CAN checksum. More...
 
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...
 

Detailed Description

This file is part of the CAN library; it contains the implementation of the CAN frame functions.

See also
lely/can/msg.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 msg.c.

Function Documentation

◆ can_crc()

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

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 286 of file msg.c.

◆ can_crc_bits()

static uint_least16_t can_crc_bits ( uint_least16_t  crc,
uint_least8_t  byte,
int  off,
int  bits 
)
static

Computes a bitwise CRC-15-CAN checksum of a single byte.

See also
can_crc()

Definition at line 316 of file msg.c.

◆ can_crc_bytes()

static uint_least16_t can_crc_bytes ( uint_least16_t  crc,
const unsigned char *  bp,
size_t  n 
)
static

Computes a CRC-15-CAN checksum.

See also
can_crc()

Definition at line 332 of file msg.c.

◆ 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 62 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 198 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 263 of file msg.c.