51 uint_least16_t crc,
const void *ptr,
int off,
size_t bits);
55 uint_least16_t crc, uint_least8_t byte,
int off,
int bits);
59 uint_least16_t crc,
const unsigned char *bp,
size_t n);
88 bits += msg->
len * 10;
95 uint_least8_t
data[16] = { 0 };
96 uint_least8_t *bp =
data;
110 *bp++ = (
id >> 23) & 0x3f;
113 *bp++ = ((
id >> 15) & 0xf8)
115 | ((
id >> 17) & 0x01);
118 *bp++ = (
id >> 9) & 0xff;
121 *bp++ = (
id >> 1) & 0xff;
124 *bp++ = ((
id << 7) & 0x80)
137 *bp++ = (
id >> 9) & 0x03;
140 *bp++ = (
id >> 1) & 0xff;
143 *bp++ = ((
id << 7) & 0x80)
150 for (uint_least8_t i = 0; i < msg->
len; i++, bits += 8)
151 *bp++ = msg->
data[i] & 0xff;
154 uint_least16_t crc =
can_crc(0, data, off, bits);
155 assert(!((off + bits) % 8));
156 *bp++ = (crc >> 7) & 0xff;
157 *bp++ = (crc << 1) & 0xff;
162 uint_least8_t mask = 0x1f;
163 uint_least8_t same = mask;
164 for (
int i = off; i < off + bits;) {
166 same = same ? 0 : mask;
169 uint_least8_t five = (((uint_least16_t)data[i / 8] << 8)
170 | data[i / 8 + 1]) >> (16 - 5 - i % 8);
172 int n =
clz8((five & mask) ^ same) - 3;
206 uint_least8_t
len = msg->
len;
225 r =
MIN((
size_t)r, n);
231 r = snprintf(s, n,
" [%02d] ", len);
234 r = snprintf(s, n,
" [%d] ", len);
238 r =
MIN((
size_t)r, n);
243 r = snprintf(s, n,
" remote request");
248 for (uint_least8_t i = 0; i <
len; i++) {
249 int r = snprintf(s, n,
" %02X", msg->
data[i]);
253 r =
MIN((
size_t)r, n);
269 char *s = malloc(n + 1);
285 static uint_least16_t
286 can_crc(uint_least16_t crc,
const void *ptr,
int off,
size_t bits)
288 assert(ptr || !bits);
290 const uint_least8_t *bp = ptr;
299 int n =
MIN((
size_t)(8 - off), bits);
315 static uint_least16_t
316 can_crc_bits(uint_least16_t crc, uint_least8_t byte,
int off,
int bits)
320 assert(off + bits <= 8);
322 for (byte <<= off; bits--; byte <<= 1) {
323 if ((byte ^ (crc >> 7)) & 0x80)
324 crc = (crc << 1) ^ 0x4599;
331 static uint_least16_t
352 static const uint_least16_t tab[] = {
353 0x0000, 0x4599, 0x4eab, 0x0b32, 0x58cf, 0x1d56, 0x1664, 0x53fd,
354 0x7407, 0x319e, 0x3aac, 0x7f35, 0x2cc8, 0x6951, 0x6263, 0x27fa,
355 0x2d97, 0x680e, 0x633c, 0x26a5, 0x7558, 0x30c1, 0x3bf3, 0x7e6a,
356 0x5990, 0x1c09, 0x173b, 0x52a2, 0x015f, 0x44c6, 0x4ff4, 0x0a6d,
357 0x5b2e, 0x1eb7, 0x1585, 0x501c, 0x03e1, 0x4678, 0x4d4a, 0x08d3,
358 0x2f29, 0x6ab0, 0x6182, 0x241b, 0x77e6, 0x327f, 0x394d, 0x7cd4,
359 0x76b9, 0x3320, 0x3812, 0x7d8b, 0x2e76, 0x6bef, 0x60dd, 0x2544,
360 0x02be, 0x4727, 0x4c15, 0x098c, 0x5a71, 0x1fe8, 0x14da, 0x5143,
361 0x73c5, 0x365c, 0x3d6e, 0x78f7, 0x2b0a, 0x6e93, 0x65a1, 0x2038,
362 0x07c2, 0x425b, 0x4969, 0x0cf0, 0x5f0d, 0x1a94, 0x11a6, 0x543f,
363 0x5e52, 0x1bcb, 0x10f9, 0x5560, 0x069d, 0x4304, 0x4836, 0x0daf,
364 0x2a55, 0x6fcc, 0x64fe, 0x2167, 0x729a, 0x3703, 0x3c31, 0x79a8,
365 0x28eb, 0x6d72, 0x6640, 0x23d9, 0x7024, 0x35bd, 0x3e8f, 0x7b16,
366 0x5cec, 0x1975, 0x1247, 0x57de, 0x0423, 0x41ba, 0x4a88, 0x0f11,
367 0x057c, 0x40e5, 0x4bd7, 0x0e4e, 0x5db3, 0x182a, 0x1318, 0x5681,
368 0x717b, 0x34e2, 0x3fd0, 0x7a49, 0x29b4, 0x6c2d, 0x671f, 0x2286,
369 0x2213, 0x678a, 0x6cb8, 0x2921, 0x7adc, 0x3f45, 0x3477, 0x71ee,
370 0x5614, 0x138d, 0x18bf, 0x5d26, 0x0edb, 0x4b42, 0x4070, 0x05e9,
371 0x0f84, 0x4a1d, 0x412f, 0x04b6, 0x574b, 0x12d2, 0x19e0, 0x5c79,
372 0x7b83, 0x3e1a, 0x3528, 0x70b1, 0x234c, 0x66d5, 0x6de7, 0x287e,
373 0x793d, 0x3ca4, 0x3796, 0x720f, 0x21f2, 0x646b, 0x6f59, 0x2ac0,
374 0x0d3a, 0x48a3, 0x4391, 0x0608, 0x55f5, 0x106c, 0x1b5e, 0x5ec7,
375 0x54aa, 0x1133, 0x1a01, 0x5f98, 0x0c65, 0x49fc, 0x42ce, 0x0757,
376 0x20ad, 0x6534, 0x6e06, 0x2b9f, 0x7862, 0x3dfb, 0x36c9, 0x7350,
377 0x51d6, 0x144f, 0x1f7d, 0x5ae4, 0x0919, 0x4c80, 0x47b2, 0x022b,
378 0x25d1, 0x6048, 0x6b7a, 0x2ee3, 0x7d1e, 0x3887, 0x33b5, 0x762c,
379 0x7c41, 0x39d8, 0x32ea, 0x7773, 0x248e, 0x6117, 0x6a25, 0x2fbc,
380 0x0846, 0x4ddf, 0x46ed, 0x0374, 0x5089, 0x1510, 0x1e22, 0x5bbb,
381 0x0af8, 0x4f61, 0x4453, 0x01ca, 0x5237, 0x17ae, 0x1c9c, 0x5905,
382 0x7eff, 0x3b66, 0x3054, 0x75cd, 0x2630, 0x63a9, 0x689b, 0x2d02,
383 0x276f, 0x62f6, 0x69c4, 0x2c5d, 0x7fa0, 0x3a39, 0x310b, 0x7492,
384 0x5368, 0x16f1, 0x1dc3, 0x585a, 0x0ba7, 0x4e3e, 0x450c, 0x0095
389 crc = (tab[(*bp++ ^ (crc >> 7)) & 0xff] ^ (crc << 8)) & 0x7fff;
A CAN or CAN FD format frame.
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.
Exact calculation based of frame content and CRC.
int clz8(uint_least8_t x)
Counts the number of leading zero bits in the unsigned 8-bit integer x.
uint_least32_t id
The identifier (11 or 29 bits, depending on the CAN_FLAG_IDE flag).
uint_least8_t len
The number of bytes in data (or the requested number of bytes in case of a remote frame)...
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.
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.
void set_errnum(errnum_t errnum)
Sets the current (thread-specific) platform-independent error number to errnum.
#define MIN(a, b)
Returns the minimum of a and b.
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. ...
#define CAN_MASK_BID
The mask used to extract the 11-bit Base Identifier from a CAN frame.
static uint_least16_t can_crc_bytes(uint_least16_t crc, const unsigned char *bp, size_t n)
Computes a CRC-15-CAN checksum.
uint_least8_t data[CAN_MSG_MAX_LEN]
The frame payload (in case of a data frame).
This is the internal header file of the CAN library.
This header file is part of the utilities library; it contains the native and platform-independent er...
Simple calculation assuming no bit stuffing.
The Remote Transmission Request (RTR) flag (unavailable in CAN FD format frames). ...
#define CAN_MAX_LEN
The maximum number of bytes in the payload of a CAN format frame.
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...
The FD Format (FDF) flag, formerly known as Extended Data Length (EDL).
#define CANFD_MAX_LEN
The maximum number of bytes in the payload of a CAN FD format frame.
uint_least8_t flags
The flags (any combination of CAN_FLAG_IDE, CAN_FLAG_RTR, CAN_FLAG_FDF, CAN_FLAG_BRS and CAN_FLAG_ESI...
The Identifier Extension (IDE) flag.
This header file is part of the C11 and POSIX compatibility library; it includes <stdio.h> and defines any missing functionality.
This header file is part of the utilities library; it contains the bit function definitions.
can_msg_bits_mode
The method used to compute te size (in bits) of a CAN frame.
This header file is part of the C11 and POSIX compatibility library; it includes <stdlib.h> and defines any missing functionality.
This header file is part of the CAN library; it contains the CAN frame declarations.
Simple worst case estimate.
This is the public header file of the utilities library.
#define CAN_MASK_EID
The mask used to extract the 29-bit Extended Identifier from a CAN frame.