-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
CO_error.c
265 lines (230 loc) · 9.42 KB
/
CO_error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* CAN module object for Linux socketCAN Error handling.
*
* @file CO_error.c
* @author Martin Wagner
* @copyright 2018 - 2020 Neuberger Gebaeudeautomation GmbH
*
*
* This file is part of <https://github.com/CANopenNode/CANopenNode>, a CANopen Stack.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <syslog.h>
#include <time.h>
#include <linux/can/error.h>
#include "CO_error.h"
#include "301/CO_driver.h"
/*
* Reset CAN interface and set to listen only mode
*/
static CO_CANinterfaceState_t
CO_CANerrorSetListenOnly(CO_CANinterfaceErrorhandler_t* CANerrorhandler, bool_t resetIf) {
log_printf(LOG_DEBUG, DBG_CAN_SET_LISTEN_ONLY, CANerrorhandler->ifName);
clock_gettime(CLOCK_MONOTONIC, &CANerrorhandler->timestamp);
CANerrorhandler->listenOnly = true;
if (resetIf) {
int ret;
char command[100];
snprintf(command, sizeof(command),
"ip link set %s down && "
"ip link set %s up "
"&",
CANerrorhandler->ifName, CANerrorhandler->ifName);
ret = system(command);
if (ret < 0) {
log_printf(LOG_DEBUG, DBG_ERRNO, "system()");
}
}
return CO_INTERFACE_LISTEN_ONLY;
}
/*
* Clear listen only
*/
static void
CO_CANerrorClearListenOnly(CO_CANinterfaceErrorhandler_t* CANerrorhandler) {
log_printf(LOG_DEBUG, DBG_CAN_CLR_LISTEN_ONLY, CANerrorhandler->ifName);
CANerrorhandler->listenOnly = false;
CANerrorhandler->timestamp.tv_sec = 0;
CANerrorhandler->timestamp.tv_nsec = 0;
}
/*
* Check and handle "bus off" state
*/
static CO_CANinterfaceState_t
CO_CANerrorBusoff(CO_CANinterfaceErrorhandler_t* CANerrorhandler, const struct can_frame* msg) {
CO_CANinterfaceState_t result = CO_INTERFACE_ACTIVE;
if ((msg->can_id & CAN_ERR_BUSOFF) != 0) {
log_printf(LOG_NOTICE, CAN_BUSOFF, CANerrorhandler->ifName);
/* The can interface changed it's state to "bus off" (e.g. because of
* a short on the can wires). We re-start the interface and mark it
* "listen only".
* Restarting the interface is the only way to clear kernel and hardware
* tx queues */
result = CO_CANerrorSetListenOnly(CANerrorhandler, true);
CANerrorhandler->CANerrorStatus |= CO_CAN_ERRTX_BUS_OFF;
}
return result;
}
/*
* Check and handle controller problems
*/
static CO_CANinterfaceState_t
CO_CANerrorCrtl(CO_CANinterfaceErrorhandler_t* CANerrorhandler, const struct can_frame* msg) {
CO_CANinterfaceState_t result = CO_INTERFACE_ACTIVE;
/* Control
* - error counters (rec/tec) are handled inside CAN hardware, nothing
* to do in here
* - we can't really do anything about buffer overflows here. Confirmed
* CANopen protocols will detect the error, non-confirmed protocols
* need to be error tolerant.
* - There is no information, when CAN controller leaves warning level,
* so we can't clear it. So we also don't set it. */
if ((msg->can_id & CAN_ERR_CRTL) != 0) {
/* clear bus off here */
CANerrorhandler->CANerrorStatus &= 0xFFFF ^ CO_CAN_ERRTX_BUS_OFF;
if ((msg->data[1] & CAN_ERR_CRTL_RX_PASSIVE) != 0) {
log_printf(LOG_NOTICE, CAN_RX_PASSIVE, CANerrorhandler->ifName);
CANerrorhandler->CANerrorStatus |= CO_CAN_ERRRX_PASSIVE;
/* CANerrorhandler->CANerrorStatus |= CO_CAN_ERRRX_WARNING; */
} else if ((msg->data[1] & CAN_ERR_CRTL_TX_PASSIVE) != 0) {
log_printf(LOG_NOTICE, CAN_TX_PASSIVE, CANerrorhandler->ifName);
CANerrorhandler->CANerrorStatus |= CO_CAN_ERRTX_PASSIVE;
/* CANerrorhandler->CANerrorStatus |= CO_CAN_ERRTX_WARNING; */
} else if ((msg->data[1] & CAN_ERR_CRTL_RX_OVERFLOW) != 0) {
log_printf(LOG_NOTICE, CAN_RX_BUF_OVERFLOW, CANerrorhandler->ifName);
CANerrorhandler->CANerrorStatus |= CO_CAN_ERRRX_OVERFLOW;
} else if ((msg->data[1] & CAN_ERR_CRTL_TX_OVERFLOW) != 0) {
log_printf(LOG_NOTICE, CAN_TX_BUF_OVERFLOW, CANerrorhandler->ifName);
CANerrorhandler->CANerrorStatus |= CO_CAN_ERRTX_OVERFLOW;
} else if ((msg->data[1] & CAN_ERR_CRTL_RX_WARNING) != 0) {
log_printf(LOG_INFO, CAN_RX_LEVEL_WARNING, CANerrorhandler->ifName);
/* clear passive flag, set warning */
CANerrorhandler->CANerrorStatus &= 0x7FFF ^ CO_CAN_ERRRX_PASSIVE;
/* CANerrorhandler->CANerrorStatus |= CO_CAN_ERRRX_WARNING; */
} else if ((msg->data[1] & CAN_ERR_CRTL_TX_WARNING) != 0) {
log_printf(LOG_INFO, CAN_TX_LEVEL_WARNING, CANerrorhandler->ifName);
/* clear passive flag, set warning */
CANerrorhandler->CANerrorStatus &= 0x7FFF ^ CO_CAN_ERRTX_PASSIVE;
/* CANerrorhandler->CANerrorStatus |= CO_CAN_ERRTX_WARNING; */
}
#ifdef CAN_ERR_CRTL_ACTIVE
else if ((msg->data[1] & CAN_ERR_CRTL_ACTIVE) != 0) {
log_printf(LOG_NOTICE, CAN_TX_LEVEL_ACTIVE, CANerrorhandler->ifName);
}
#endif
}
return result;
}
/*
* Check and handle controller problems
*/
static CO_CANinterfaceState_t
CO_CANerrorNoack(CO_CANinterfaceErrorhandler_t* CANerrorhandler, const struct can_frame* msg) {
CO_CANinterfaceState_t result = CO_INTERFACE_ACTIVE;
if (CANerrorhandler->listenOnly) {
return CO_INTERFACE_LISTEN_ONLY;
}
/* received no ACK on transmission */
if ((msg->can_id & CAN_ERR_ACK) != 0) {
CANerrorhandler->noackCounter++;
if (CANerrorhandler->noackCounter > CO_CANerror_NOACK_MAX) {
log_printf(LOG_INFO, CAN_NOACK, CANerrorhandler->ifName);
/* We get the NO-ACK error continuously when no other CAN node
* is active on the bus (Error Counting exception 1 in CAN spec).
* todo - you need to pull the message causing no-ack from the CAN
* hardware buffer. This can be done by either resetting interface
* in here or deleting it within Linux Kernel can driver (set "false"). */
result = CO_CANerrorSetListenOnly(CANerrorhandler, true);
}
} else {
CANerrorhandler->noackCounter = 0;
}
return result;
}
void
CO_CANerror_init(CO_CANinterfaceErrorhandler_t* CANerrorhandler, int fd, const char* ifName) {
if (CANerrorhandler == NULL) {
return;
}
CANerrorhandler->fd = fd;
memcpy(CANerrorhandler->ifName, ifName, sizeof(CANerrorhandler->ifName));
CANerrorhandler->noackCounter = 0;
CANerrorhandler->listenOnly = false;
CANerrorhandler->timestamp.tv_sec = 0;
CANerrorhandler->timestamp.tv_nsec = 0;
CANerrorhandler->CANerrorStatus = 0;
}
void
CO_CANerror_disable(CO_CANinterfaceErrorhandler_t* CANerrorhandler) {
if (CANerrorhandler == NULL) {
return;
}
memset(CANerrorhandler, 0, sizeof(*CANerrorhandler));
CANerrorhandler->fd = -1;
}
void
CO_CANerror_rxMsg(CO_CANinterfaceErrorhandler_t* CANerrorhandler) {
if (CANerrorhandler == NULL) {
return;
}
/* someone is active, we can leave listen only immediately */
if (CANerrorhandler->listenOnly) {
CO_CANerrorClearListenOnly(CANerrorhandler);
}
CANerrorhandler->noackCounter = 0;
}
CO_CANinterfaceState_t
CO_CANerror_txMsg(CO_CANinterfaceErrorhandler_t* CANerrorhandler) {
struct timespec now;
if (CANerrorhandler == NULL) {
return CO_INTERFACE_BUS_OFF;
}
if (CANerrorhandler->listenOnly) {
clock_gettime(CLOCK_MONOTONIC, &now);
if (CANerrorhandler->timestamp.tv_sec + CO_CANerror_LISTEN_ONLY < now.tv_sec) {
/* let's try that again. Maybe someone is waiting for LSS now. It
* doesn't matter which message is sent, as all messages are ACKed. */
CO_CANerrorClearListenOnly(CANerrorhandler);
return CO_INTERFACE_ACTIVE;
}
return CO_INTERFACE_LISTEN_ONLY;
}
return CO_INTERFACE_ACTIVE;
}
CO_CANinterfaceState_t
CO_CANerror_rxMsgError(CO_CANinterfaceErrorhandler_t* CANerrorhandler, const struct can_frame* msg) {
if (CANerrorhandler == NULL) {
return CO_INTERFACE_BUS_OFF;
}
CO_CANinterfaceState_t result;
/* Log all error messages in full to debug log, even if analysis is done
* further on. */
log_printf(LOG_DEBUG, DBG_CAN_ERROR_GENERAL, (int)msg->can_id, msg->data[0], msg->data[1], msg->data[2],
msg->data[3], msg->data[4], msg->data[5], msg->data[6], msg->data[7], CANerrorhandler->ifName);
/* Process errors - start with the most unambiguous one */
result = CO_CANerrorBusoff(CANerrorhandler, msg);
if (result != CO_INTERFACE_ACTIVE) {
return result;
}
result = CO_CANerrorCrtl(CANerrorhandler, msg);
if (result != CO_INTERFACE_ACTIVE) {
return result;
}
result = CO_CANerrorNoack(CANerrorhandler, msg);
if (result != CO_INTERFACE_ACTIVE) {
return result;
}
return CO_INTERFACE_ACTIVE;
}