Lely core libraries 2.3.4
can_err.h
Go to the documentation of this file.
1
22#ifndef LELY_IO2_INTERN_LINUX_CAN_ERR_H_
23#define LELY_IO2_INTERN_LINUX_CAN_ERR_H_
24
25#include "io.h"
26
27#ifdef __linux__
28
29#include <lely/io2/can/err.h>
30#include <lely/util/util.h>
31
32#include <assert.h>
33#include <errno.h>
34
35#include <linux/can.h>
36#include <linux/can/error.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42static int can_frame2can_err(
43 const struct can_frame *frame, struct can_err *err);
44
45static inline int
46can_frame2can_err(const struct can_frame *frame, struct can_err *err)
47{
48 assert(frame);
49
50 if (!(frame->can_id & CAN_ERR_FLAG))
51 return 0;
52
53 if (frame->can_dlc != CAN_ERR_DLC) {
54 errno = EINVAL;
55 return -1;
56 }
57
58 enum can_state state = err ? err->state : CAN_STATE_ACTIVE;
59 enum can_error error = err ? err->error : 0;
60
61 if (frame->can_id & CAN_ERR_RESTARTED)
62 state = CAN_STATE_ACTIVE;
63
64 if (frame->can_id & CAN_ERR_TX_TIMEOUT)
65 error |= CAN_ERROR_OTHER;
66
67 if (frame->can_id & CAN_ERR_CRTL) {
68#ifdef CAN_ERR_CRTL_ACTIVE
69 if (frame->data[1] & CAN_ERR_CRTL_ACTIVE)
70 state = CAN_STATE_ACTIVE;
71#endif
72 // clang-format off
73 if (frame->data[1] & (CAN_ERR_CRTL_RX_PASSIVE
74 | CAN_ERR_CRTL_TX_PASSIVE))
75 // clang-format on
76 state = CAN_STATE_PASSIVE;
77 }
78
79 if (frame->can_id & CAN_ERR_PROT) {
80 if (frame->data[2] & CAN_ERR_PROT_BIT)
81 error |= CAN_ERROR_BIT;
82 if (frame->data[2] & CAN_ERR_PROT_FORM)
83 error |= CAN_ERROR_FORM;
84 if (frame->data[2] & CAN_ERR_PROT_STUFF)
85 error |= CAN_ERROR_STUFF;
86 // clang-format off
87 if (frame->data[2] & (CAN_ERR_PROT_BIT0 | CAN_ERR_PROT_BIT1
88 | CAN_ERR_PROT_OVERLOAD))
89 // clang-format on
90 error |= CAN_ERROR_OTHER;
91 if (frame->data[2] & CAN_ERR_PROT_ACTIVE)
92 state = CAN_STATE_ACTIVE;
93 if (frame->data[3] & CAN_ERR_PROT_LOC_CRC_SEQ)
94 error |= CAN_ERROR_CRC;
95 }
96
97 if ((frame->can_id & CAN_ERR_TRX) && frame->data[4])
98 error |= CAN_ERROR_OTHER;
99
100 if (frame->can_id & CAN_ERR_ACK)
101 error |= CAN_ERROR_ACK;
102
103 if (frame->can_id & CAN_ERR_BUSOFF)
104 state = CAN_STATE_BUSOFF;
105
106 if (err) {
107 err->state = state;
108 err->error = error;
109 }
110
111 return 1;
112}
113
114#ifdef __cplusplus
115}
116#endif
117
118#endif // __linux__
119
120#endif // !LELY_IO2_INTERN_LINUX_CAN_ERR_H_
can_error
The error flags of a CAN bus, which are not mutually exclusive.
Definition err.h:42
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
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.
This header file is part of the I/O library; it contains CAN bus error definitions.
This is the internal header file of the Windows-specific I/O declarations.
A CAN error frame.
Definition err.h:28
int state
The state of the CAN node (one of CAN_STATE_ACTIVE, CAN_STATE_PASSIVE or CAN_STATE_BUSOFF).
Definition err.h:33
int error
The error flags of the CAN bus (any combination of CAN_ERROR_BIT, CAN_ERROR_STUFF,...
Definition err.h:39