Lely core libraries  2.2.5
msg.h
Go to the documentation of this file.
1 
22 #ifndef LELY_CAN_MSG_H_
23 #define LELY_CAN_MSG_H_
24 
25 #include <lely/features.h>
26 
27 #include <stddef.h>
28 #include <stdint.h>
29 
31 #define CAN_MASK_BID UINT32_C(0x000007ff)
32 
34 #define CAN_MASK_EID UINT32_C(0x1fffffff)
35 
37 enum {
43  CAN_FLAG_IDE = 1u << 0,
48  CAN_FLAG_RTR = 1u << 1,
49 #if !LELY_NO_CANFD
50 
54  CAN_FLAG_FDF = 1u << 2,
55  CAN_FLAG_EDL = CAN_FLAG_FDF,
62  CAN_FLAG_BRS = 1u << 3,
67  CAN_FLAG_ESI = 1u << 4
68 #endif // !LELY_NO_CANFD
69 };
70 
72 #define CAN_MAX_LEN 8
73 
74 #ifndef LELY_NO_CANFD
75 #define CANFD_MAX_LEN 64
77 #endif
78 
80 #if LELY_NO_CANFD
81 #define CAN_MSG_MAX_LEN CAN_MAX_LEN
82 #else
83 #define CAN_MSG_MAX_LEN CANFD_MAX_LEN
84 #endif
85 
87 struct can_msg {
89  uint_least32_t id;
94  uint_least8_t flags;
100  uint_least8_t len;
102  uint_least8_t data[CAN_MSG_MAX_LEN];
103 };
104 
106 #if LELY_NO_CANFD
107 #define CAN_MSG_INIT \
108  { \
109  0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } \
110  }
111 #else
112 // clang-format off
113 #define CAN_MSG_INIT \
114  { \
115  0, 0, 0, \
116  { \
117  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
118  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
119  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
120  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
121  } \
122  }
123 // clang-format on
124 #endif
125 
134 };
135 
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139 
151 int can_msg_bits(const struct can_msg *msg, enum can_msg_bits_mode mode);
152 
168 int snprintf_can_msg(char *s, size_t n, const struct can_msg *msg);
169 
183 int asprintf_can_msg(char **ps, const struct can_msg *msg);
184 
185 #ifdef __cplusplus
186 }
187 #endif
188 
189 #endif // !LELY_CAN_MSG_H_
A CAN or CAN FD format frame.
Definition: msg.h:87
Exact calculation based of frame content and CRC.
Definition: msg.h:133
uint_least32_t id
The identifier (11 or 29 bits, depending on the CAN_FLAG_IDE flag).
Definition: msg.h:89
uint_least8_t len
The number of bytes in data (or the requested number of bytes in case of a remote frame)...
Definition: msg.h:100
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.
Definition: msg.c:198
uint_least8_t data[CAN_MSG_MAX_LEN]
The frame payload (in case of a data frame).
Definition: msg.h:102
The Error State Indicator (ESI) flag (only available in CAN FD format frames).
Definition: msg.h:67
Simple calculation assuming no bit stuffing.
Definition: msg.h:129
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.
Definition: msg.c:62
The Remote Transmission Request (RTR) flag (unavailable in CAN FD format frames). ...
Definition: msg.h:48
The FD Format (FDF) flag, formerly known as Extended Data Length (EDL).
Definition: msg.h:54
This header file is part of the C11 and POSIX compatibility library; it includes <stddef.h> and defines any missing functionality.
#define CAN_MSG_MAX_LEN
The maximum number of bytes in the payload of a can_msg struct.
Definition: msg.h:83
This header file is part of the C11 and POSIX compatibility library; it includes <stdint.h> and defines any missing functionality.
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...
Definition: msg.h:94
The Identifier Extension (IDE) flag.
Definition: msg.h:43
can_msg_bits_mode
The method used to compute te size (in bits) of a CAN frame.
Definition: msg.h:127
This header file is part of the Lely libraries; it contains the compiler feature definitions.
Simple worst case estimate.
Definition: msg.h:131
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...
Definition: msg.c:263
The Bit Rate Switch (BRS) flag (only available in CAN FD format frames).
Definition: msg.h:62