Lely core libraries 2.3.4
socket.c
Go to the documentation of this file.
1
24#include "can.h"
25
26#if !LELY_NO_STDIO && LELY_HAVE_SOCKET_CAN
27
28#include <lely/can/socket.h>
29#include <lely/util/errnum.h>
30#include <lely/util/util.h>
31
32#include <assert.h>
33#include <string.h>
34
35#ifdef HAVE_LINUX_CAN_H
36#include <linux/can.h>
37#endif
38#ifdef HAVE_LINUX_CAN_ERROR_H
39#include <linux/can/error.h>
40#endif
41
42int
43can_frame_is_error(const struct can_frame *frame, enum can_state *pstate,
44 enum can_error *perror)
45{
46 assert(frame);
47
48 if (!(frame->can_id & CAN_ERR_FLAG))
49 return 0;
50
51 enum can_state state = pstate ? *pstate : CAN_STATE_ACTIVE;
52 enum can_error error = perror ? *perror : 0;
53
54#ifdef HAVE_LINUX_CAN_ERROR_H
55 if (frame->can_dlc != CAN_ERR_DLC) {
57 return -1;
58 }
59
60 if (frame->can_id & CAN_ERR_RESTARTED)
61 state = CAN_STATE_ACTIVE;
62
63 if (frame->can_id & CAN_ERR_TX_TIMEOUT)
64 error |= CAN_ERROR_OTHER;
65
66 if (frame->can_id & CAN_ERR_CRTL) {
67#ifdef CAN_ERR_CRTL_ACTIVE
68 if (frame->data[1] & CAN_ERR_CRTL_ACTIVE)
69 state = CAN_STATE_ACTIVE;
70#endif
71 // clang-format off
72 if (frame->data[1] & (CAN_ERR_CRTL_RX_PASSIVE
73 | CAN_ERR_CRTL_TX_PASSIVE))
74 // clang-format on
75 state = CAN_STATE_PASSIVE;
76 }
77
78 if (frame->can_id & CAN_ERR_PROT) {
79 if (frame->data[2] & CAN_ERR_PROT_BIT)
80 error |= CAN_ERROR_BIT;
81 if (frame->data[2] & CAN_ERR_PROT_FORM)
82 error |= CAN_ERROR_FORM;
83 if (frame->data[2] & CAN_ERR_PROT_STUFF)
84 error |= CAN_ERROR_STUFF;
85 // clang-format off
86 if (frame->data[2] & (CAN_ERR_PROT_BIT0 | CAN_ERR_PROT_BIT1
87 | CAN_ERR_PROT_OVERLOAD))
88 // clang-format on
89 error |= CAN_ERROR_OTHER;
90 if (frame->data[2] & CAN_ERR_PROT_ACTIVE)
91 state = CAN_STATE_ACTIVE;
92 if (frame->data[3] & CAN_ERR_PROT_LOC_CRC_SEQ)
93 error |= CAN_ERROR_CRC;
94 }
95
96 if ((frame->can_id & CAN_ERR_TRX) && frame->data[4])
97 error |= CAN_ERROR_OTHER;
98
99 if (frame->can_id & CAN_ERR_ACK)
100 error |= CAN_ERROR_ACK;
101
102 if (frame->can_id & CAN_ERR_BUSOFF)
103 state = CAN_STATE_BUSOFF;
104#endif // HAVE_LINUX_CAN_ERROR_H
105
106 if (pstate)
107 *pstate = state;
108
109 if (perror)
110 *perror = error;
111
112 return 1;
113}
114
115int
116can_frame2can_msg(const struct can_frame *src, struct can_msg *dst)
117{
118 assert(src);
119 assert(dst);
120
121 if (src->can_id & CAN_ERR_FLAG) {
123 return -1;
124 }
125
126 memset(dst, 0, sizeof(*dst));
127 dst->flags = 0;
128 if (src->can_id & CAN_EFF_FLAG) {
129 dst->id = src->can_id & CAN_EFF_MASK;
130 dst->flags |= CAN_FLAG_IDE;
131 } else {
132 dst->id = src->can_id & CAN_SFF_MASK;
133 }
134 if (src->can_id & CAN_RTR_FLAG)
135 dst->flags |= CAN_FLAG_RTR;
136 dst->len = MIN(src->can_dlc, CAN_MAX_LEN);
137 // cppcheck-suppress knownConditionTrueFalse
138 if (!(dst->flags & CAN_FLAG_RTR))
139 memcpy(dst->data, src->data, dst->len);
140
141 return 0;
142}
143
144int
145can_msg2can_frame(const struct can_msg *src, struct can_frame *dst)
146{
147 assert(src);
148 assert(dst);
149
150#if !LELY_NO_CANFD
151 if (src->flags & CAN_FLAG_EDL) {
153 return -1;
154 }
155#endif
156
157 memset(dst, 0, sizeof(*dst));
158 dst->can_id = src->id;
159 if (src->flags & CAN_FLAG_IDE) {
160 dst->can_id &= CAN_EFF_MASK;
161 dst->can_id |= CAN_EFF_FLAG;
162 } else {
163 dst->can_id &= CAN_SFF_MASK;
164 }
165 dst->can_dlc = MIN(src->len, CAN_MAX_LEN);
166 if (src->flags & CAN_FLAG_RTR)
167 dst->can_id |= CAN_RTR_FLAG;
168 else
169 memcpy(dst->data, src->data, dst->can_dlc);
170
171 return 0;
172}
173
174#if !LELY_NO_CANFD && defined(CANFD_MTU)
175
176int
177canfd_frame2can_msg(const struct canfd_frame *src, struct can_msg *dst)
178{
179 assert(src);
180 assert(dst);
181
182 if (src->can_id & CAN_ERR_FLAG) {
184 return -1;
185 }
186
187 memset(dst, 0, sizeof(*dst));
188 dst->flags = CAN_FLAG_EDL;
189 if (src->can_id & CAN_EFF_FLAG) {
190 dst->id = src->can_id & CAN_EFF_MASK;
191 dst->flags |= CAN_FLAG_IDE;
192 } else {
193 dst->id = src->can_id & CAN_SFF_MASK;
194 }
195 if (src->flags & CANFD_BRS)
196 dst->flags |= CAN_FLAG_BRS;
197 if (src->flags & CANFD_ESI)
198 dst->flags |= CAN_FLAG_ESI;
199 dst->len = MIN(src->len, CANFD_MAX_LEN);
200 memcpy(dst->data, src->data, dst->len);
201
202 return 0;
203}
204
205int
206can_msg2canfd_frame(const struct can_msg *src, struct canfd_frame *dst)
207{
208 assert(src);
209 assert(dst);
210
211 if (!(src->flags & CAN_FLAG_EDL)) {
213 return -1;
214 }
215
216 memset(dst, 0, sizeof(*dst));
217 dst->can_id = src->id;
218 if (src->flags & CAN_FLAG_IDE) {
219 dst->can_id &= CAN_EFF_MASK;
220 dst->can_id |= CAN_EFF_FLAG;
221 } else {
222 dst->can_id &= CAN_SFF_MASK;
223 }
224 dst->flags = 0;
225 if (src->flags & CAN_FLAG_BRS)
226 dst->flags |= CANFD_BRS;
227 if (src->flags & CAN_FLAG_ESI)
228 dst->flags |= CANFD_ESI;
229 dst->len = MIN(src->len, CANFD_MAX_LEN);
230 memcpy(dst->data, src->data, dst->len);
231
232 return 0;
233}
234
235#endif // !LELY_NO_CANFD && CANFD_MTU
236
237#endif // !LELY_NO_STDIO && LELY_HAVE_SOCKET_CAN
can_error
The error flags of a CAN bus, which are not mutually exclusive.
Definition: err.h:42
@ CAN_ERROR_FORM
A form error.
Definition: err.h:50
@ CAN_ERROR_BIT
A single bit error.
Definition: err.h:44
@ CAN_ERROR_STUFF
A bit stuffing error.
Definition: err.h:46
@ CAN_ERROR_ACK
An acknowledgment error.
Definition: err.h:52
@ CAN_ERROR_CRC
A CRC sequence error.
Definition: err.h:48
@ CAN_ERROR_OTHER
One or more other errors.
Definition: err.h:54
can_state
The states of a CAN node, depending on the TX/RX error count.
Definition: err.h:28
@ CAN_STATE_BUSOFF
The bus off state (TX/RX error count >= 256).
Definition: err.h:34
@ CAN_STATE_PASSIVE
The error passive state (TX/RX error count < 256).
Definition: err.h:32
@ CAN_STATE_ACTIVE
The error active state (TX/RX error count < 128).
Definition: err.h:30
@ CAN_FLAG_IDE
The Identifier Extension (IDE) flag.
Definition: msg.h:43
@ CAN_FLAG_RTR
The Remote Transmission Request (RTR) flag (unavailable in CAN FD format frames).
Definition: msg.h:48
@ CAN_FLAG_BRS
The Bit Rate Switch (BRS) flag (only available in CAN FD format frames).
Definition: msg.h:62
@ CAN_FLAG_ESI
The Error State Indicator (ESI) flag (only available in CAN FD format frames).
Definition: msg.h:67
#define CAN_MAX_LEN
The maximum number of bytes in the payload of a CAN format frame.
Definition: msg.h:72
#define CANFD_MAX_LEN
The maximum number of bytes in the payload of a CAN FD format frame.
Definition: msg.h:76
This header file is part of the utilities library; it contains the native and platform-independent er...
@ ERRNUM_INVAL
Invalid argument.
Definition: errnum.h:132
void set_errnum(errnum_t errnum)
Sets the current (thread-specific) platform-independent error number to errnum.
Definition: errnum.h:424
This header file is part of the I/O library; it contains the CAN bus declarations for Linux.
This is the public header file of the utilities library.
#define MIN(a, b)
Returns the minimum of a and b.
Definition: util.h:57
int can_frame2can_msg(const struct can_frame *src, struct can_msg *dst)
Converts a SocketCAN CAN frame to a can_msg frame.
Definition: socket.c:116
int can_frame_is_error(const struct can_frame *frame, enum can_state *pstate, enum can_error *perror)
Checks if a SocketCAN CAN frame is an error frame and parses the bus state and error flags if it is.
Definition: socket.c:43
int can_msg2can_frame(const struct can_msg *src, struct can_frame *dst)
Converts a can_msg frame to a SocketCAN CAN frame.
Definition: socket.c:145
This header file is part of the CAN library; it contains the SocketCAN interface declarations.
int can_msg2canfd_frame(const struct can_msg *src, struct canfd_frame *dst)
Converts a can_msg frame to a SocketCAN CAN FD frame.
Definition: can_msg.h:141
int canfd_frame2can_msg(const struct canfd_frame *src, struct can_msg *dst)
Converts a SocketCAN CAN FD frame to a can_msg frame.
Definition: can_msg.h:112
This is the internal header file of the CAN bus operation queue functions.
This header file is part of the C11 and POSIX compatibility library; it includes <string....
A CAN or CAN FD format frame.
Definition: msg.h:87
uint_least8_t data[CAN_MSG_MAX_LEN]
The frame payload (in case of a data frame).
Definition: msg.h:102
uint_least32_t id
The identifier (11 or 29 bits, depending on the CAN_FLAG_IDE flag).
Definition: msg.h:89
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
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