Lely core libraries  1.9.2
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/can/can.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 
41 #define CAN_FLAG_IDE 0x01
42 
47 #define CAN_FLAG_RTR 0x02
48 
49 #if !LELY_NO_CANFD
50 
55 #define CAN_FLAG_EDL 0x04
56 
62 #define CAN_FLAG_BRS 0x08
63 
68 #define CAN_FLAG_ESI 0x10
69 
70 #endif // !LELY_NO_CANFD
71 
73 #define CAN_MAX_LEN 8
74 
75 #ifndef LELY_NO_CANFD
76 #define CANFD_MAX_LEN 64
78 #endif
79 
81 #if LELY_NO_CANFD
82 #define CAN_MSG_MAX_LEN CAN_MAX_LEN
83 #else
84 #define CAN_MSG_MAX_LEN CANFD_MAX_LEN
85 #endif
86 
88 struct can_msg {
90  uint_least32_t id;
95  uint_least8_t flags;
101  uint_least8_t len;
103  uint_least8_t data[CAN_MSG_MAX_LEN];
104 };
105 
107 #if LELY_NO_CANFD
108 #define CAN_MSG_INIT \
109  { \
110  0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } \
111  }
112 #else
113 // clang-format off
114 #define CAN_MSG_INIT \
115  { \
116  0, 0, 0, \
117  { \
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  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
122  } \
123  }
124 // clang-format on
125 #endif
126 
135 };
136 
137 #ifdef __cplusplus
138 extern "C" {
139 #endif
140 
152 int can_msg_bits(const struct can_msg *msg, enum can_msg_bits_mode mode);
153 
169 int snprintf_can_msg(char *s, size_t n, const struct can_msg *msg);
170 
184 int asprintf_can_msg(char **ps, const struct can_msg *msg);
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 #endif // !LELY_CAN_MSG_H_
A CAN or CAN FD format frame.
Definition: msg.h:88
uint_least32_t id
The identifier (11 or 29 bits, depending on the CAN_FLAG_IDE flag).
Definition: msg.h:90
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:101
Exact calculation based of frame content and CRC.
Definition: msg.h:134
Simple worst case estimate.
Definition: msg.h:132
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:103
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
Simple calculation assuming no bit stuffing.
Definition: msg.h:130
This header file is part of the C11 and POSIX compatibility library; it includes <stddef.h> and defines any missing functionality.
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:261
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_EDL, CAN_FLAG_BRS and CAN_FLAG_ESI...
Definition: msg.h:95
can_msg_bits_mode
The method used to compute te size (in bits) of a CAN frame.
Definition: msg.h:128
#define CAN_MSG_MAX_LEN
The maximum number of bytes in the payload of a can_msg struct.
Definition: msg.h:84
This is the public header file of the CAN library.