-
Notifications
You must be signed in to change notification settings - Fork 1
/
CAN_common.cpp
388 lines (319 loc) · 13.5 KB
/
CAN_common.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <string.h>
#include "CAN_common.h"
#include "CANObject.h"
/// @brief Clears all attributes of CAN frame
/// @param can_frame CAN frame to clear
void clear_can_frame_struct(can_frame_t &can_frame)
{
can_frame.object_id = 0;
memset(can_frame.raw_data, 0, sizeof(can_frame.raw_data));
can_frame.raw_data_length = 0;
can_frame.initialized = false;
}
/// @brief Copies data from one CAN frame to another
/// @param dest_can_frame Destination CAN frame
/// @param src_can_frame Source CAN frame
void copy_can_frame_struct(can_frame_t &dest_can_frame, can_frame_t src_can_frame)
{
memcpy(dest_can_frame.raw_data, src_can_frame.raw_data, sizeof(dest_can_frame.raw_data));
dest_can_frame.initialized = src_can_frame.initialized;
dest_can_frame.object_id = src_can_frame.object_id;
dest_can_frame.raw_data_length = src_can_frame.raw_data_length;
}
/// @brief Clears all attributes of CAN error structure
/// @param error CAN error to clear
void clear_can_error_struct(can_error_t &error)
{
error.function_id = CAN_FUNC_NONE;
error.error_section = ERROR_SECTION_NONE;
error.error_code = 0;
}
/// @brief Common BlockInfo parameters will be applied to the specified CANObject.
/// All BlockInfo objects has:
/// - enabled timers (15000 ms period)
/// - enabled flood mode
/// - disabled events
/// - no external handlers for events, timers, set & request commends
/// @param block_sys_object Target CANObject
void set_block_info_params(CANObjectInterface &block_sys_object)
{
block_sys_object.SetTimerFloodMode(true);
block_sys_object.SetTimerPeriod(15000);
block_sys_object.SetErrorEventDelay(CAN_ERROR_DISABLED);
block_sys_object.SetObjectType(CAN_OBJECT_TYPE_SYSTEM_BLOCK_INFO);
block_sys_object.RegisterFunctionEvent(nullptr);
block_sys_object.RegisterFunctionRequest(nullptr);
block_sys_object.RegisterFunctionSet(nullptr);
block_sys_object.RegisterFunctionTimer(nullptr);
}
/// @brief Common BlockHealth parameters will be applied to the specified CANObject.
/// All BlockHealth objects has:
/// - disabled timers and flood mode
/// - enabled events (300 ms period)
/// - no external handlers for events, timers, set & request commends
/// @param block_sys_object Target CANObject
void set_block_health_params(CANObjectInterface &block_sys_object)
{
block_sys_object.SetTimerFloodMode(false);
block_sys_object.SetTimerPeriod(CAN_TIMER_DISABLED);
block_sys_object.SetErrorEventDelay(300);
block_sys_object.SetObjectType(CAN_OBJECT_TYPE_SYSTEM_BLOCK_HEALTH);
block_sys_object.RegisterFunctionEvent(nullptr);
block_sys_object.RegisterFunctionRequest(nullptr);
block_sys_object.RegisterFunctionSet(nullptr);
block_sys_object.RegisterFunctionTimer(nullptr);
};
/// @brief Common BlockFeatures parameters will be applied to the specified CANObject.
/// All BlockFeatures objects has:
/// - enabled timers (15000 ms period)
/// - enabled flood mode
/// - disabled events
/// - no external handlers for events, timers, set & request commends
/// @param block_sys_object Target CANObject
void set_block_features_params(CANObjectInterface &block_sys_object)
{
block_sys_object.SetTimerFloodMode(true);
block_sys_object.SetTimerPeriod(15000);
block_sys_object.SetErrorEventDelay(CAN_ERROR_DISABLED);
block_sys_object.SetObjectType(CAN_OBJECT_TYPE_SYSTEM_BLOCK_FEATURES);
block_sys_object.RegisterFunctionEvent(nullptr);
block_sys_object.RegisterFunctionRequest(nullptr);
block_sys_object.RegisterFunctionSet(nullptr);
block_sys_object.RegisterFunctionTimer(nullptr);
};
/// @brief Common BlockError parameters will be applied to the specified CANObject.
/// All BlockError objects has:
/// - disabled timers and flood mode
/// - enabled events (300 ms period)
/// - no external handlers for events, timers, set & request commends
/// @param block_sys_object Target CANObject
void set_block_error_params(CANObjectInterface &block_sys_object)
{
block_sys_object.SetTimerFloodMode(false);
block_sys_object.SetTimerPeriod(CAN_TIMER_DISABLED);
block_sys_object.SetErrorEventDelay(300);
block_sys_object.SetObjectType(CAN_OBJECT_TYPE_SYSTEM_BLOCK_ERROR);
block_sys_object.RegisterFunctionEvent(nullptr);
block_sys_object.RegisterFunctionRequest(nullptr);
block_sys_object.RegisterFunctionSet(nullptr);
block_sys_object.RegisterFunctionTimer(nullptr);
};
/// @brief Debug logger function: decodes function ID to to human-readable string.
/// @param function_id ID of the function.
/// @return Null-terminated string with name of the function
const char *get_function_name(can_function_id_t function_id)
{
#if defined(DEBUG) || defined(DETAILED_DEBUG)
switch (function_id)
{
case CAN_FUNC_NONE:
return "none";
case CAN_FUNC_SET_IN:
return "set: in";
case CAN_FUNC_TOGGLE_IN:
return "toggle: in";
case CAN_FUNC_ACTION_IN:
return "action: in";
case CAN_FUNC_LOCK_IN:
return "lock: in";
case CAN_FUNC_LOCK_OUT_OK:
return "lock: ok answer";
case CAN_FUNC_LOCK_OUT_ERR:
return "lock: error answer";
case CAN_FUNC_REQUEST_IN:
return "request: in";
case CAN_FUNC_TIMER_NORMAL:
return "timer: normal";
case CAN_FUNC_TIMER_WARNING:
return "timer: warning";
case CAN_FUNC_TIMER_CRITICAL:
return "timer: critical";
case CAN_FUNC_EVENT_OK:
return "event: ok";
case CAN_FUNC_EVENT_ERROR:
return "event: error";
case CAN_FUNC_SYSTEM_REQUEST_IN:
return "system request: in";
case CAN_FUNC_SYSTEM_REQUEST_OUT_OK:
return "system request: ok answer";
case CAN_FUNC_FIRST_OUT_OK:
case CAN_FUNC_FIRST_OUT_UNUSED:
case CAN_FUNC_FIRST_OUT_ERR:
return "correct other: some first value";
case CAN_FUNC_SEND_RAW_INIT_IN:
case CAN_FUNC_SEND_RAW_INIT_OUT_OK:
case CAN_FUNC_SEND_RAW_INIT_OUT_ERR:
case CAN_FUNC_SEND_RAW_CHUNK_START_IN:
case CAN_FUNC_SEND_RAW_CHUNK_START_OUT_OK:
case CAN_FUNC_SEND_RAW_CHUNK_START_OUT_ERR:
case CAN_FUNC_SEND_RAW_CHUNK_DATA_IN:
case CAN_FUNC_SEND_RAW_CHUNK_DATA_OUT_ERR:
case CAN_FUNC_SEND_RAW_CHUNK_END_IN:
case CAN_FUNC_SEND_RAW_CHUNK_END_OUT_OK:
case CAN_FUNC_SEND_RAW_CHUNK_END_OUT_ERR:
case CAN_FUNC_SEND_RAW_FINISH_IN:
case CAN_FUNC_SEND_RAW_FINISH_OUT_OK:
case CAN_FUNC_SEND_RAW_FINISH_OUT_ERR:
return "correct other: some send raw function";
default:
return "unknown";
}
#else // DEBUG
return "detailed names are disabled";
#endif // DEBUG
}
/// @brief Debug logger function: decodes timer type to human-readable string.
/// @param timer_type Type of the timer.
/// @return Null-terminated string with name of the timer type.
const char *get_timer_type_name(timer_type_t timer_type)
{
#if defined(DEBUG) || defined(DETAILED_DEBUG)
switch (timer_type)
{
case CAN_TIMER_TYPE_NONE:
return "timer type: none";
case CAN_TIMER_TYPE_NORMAL:
return "timer type: normal";
case CAN_TIMER_TYPE_WARNING:
return "timer type: warning";
case CAN_TIMER_TYPE_CRITICAL:
return "timer type: critical";
case CAN_TIMER_TYPE_MASK:
return "timer type: mask";
default:
return "timer type: unknown";
}
#else // DEBUG
return "detailed names are disabled";
#endif // DEBUG
}
/// @brief Debug logger function: decodes event type to human-readable string.
/// @param event_type Type of the event.
/// @return Null-terminated string with name of the event type.
const char *get_event_type_name(event_type_t event_type)
{
#if defined(DEBUG) || defined(DETAILED_DEBUG)
switch (event_type)
{
case CAN_EVENT_TYPE_NONE:
return "event type: none";
case CAN_EVENT_TYPE_NORMAL:
return "event type: normal";
case CAN_EVENT_TYPE_ERROR:
return "event type: error";
case CAN_EVENT_TYPE_MASK:
return "event type: mask";
default:
return "event type: unknown";
}
#else // DEBUG
return "detailed names are disabled";
#endif // DEBUG
}
/// @brief Debug logger function: decodes object type code to human-readable string.
/// @param object_type Type of the object.
/// @return Null-terminated string with name of the object type.
const char *get_object_type_name(object_type_t object_type)
{
#if defined(DEBUG) || defined(DETAILED_DEBUG)
switch (object_type)
{
case CAN_OBJECT_TYPE_UNKNOWN:
return "object type: unknown";
case CAN_OBJECT_TYPE_ORDINARY:
return "object type: ordinary object";
case CAN_OBJECT_TYPE_SYSTEM_BLOCK_INFO:
return "object type: system object - BlockInfo";
case CAN_OBJECT_TYPE_SYSTEM_BLOCK_HEALTH:
return "object type: system object - BlockHealth";
case CAN_OBJECT_TYPE_SYSTEM_BLOCK_FEATURES:
return "object type: system object - BlockFeatures";
case CAN_OBJECT_TYPE_SYSTEM_BLOCK_ERROR:
return "object type: system object - BlockError";
default:
return "event type: unknown";
}
#else // DEBUG
return "detailed names are disabled";
#endif // DEBUG
}
/// @brief Debug logger function: decodes error section & code to human-readable string.
/// @param error_section Error section to decode.
/// @param error_code Error code to decode.
/// @return Null-terminated string with name of the event type.
const char *get_error_code_name_for_section(error_section_t error_section, uint8_t error_code)
{
#if defined(DEBUG) || defined(DETAILED_DEBUG)
switch (error_section)
{
case ERROR_SECTION_NONE:
return "error: section [none], code [-]";
case ERROR_SECTION_CAN_MANAGER:
switch ((error_code_manager_t)error_code)
{
case ERROR_CODE_MANAGER_NONE:
return "error: section [CANManager], code [none]";
case ERROR_CODE_MANAGER_CAN_FRAME_AND_ERROR_STRUCT_ARE_BOTH_BLANK:
return "error: section [CANManager], code [CAN frame and error structure are both blank after handlers]";
case ERROR_CODE_MANAGER_SOMETHING_WRONG:
return "error: section [CANManager], code [something went wrong]";
default:
return "error: section [CANManager], code [unknown]";
}
break;
case ERROR_SECTION_CAN_OBJECT:
switch ((error_code_object_t)error_code)
{
case ERROR_CODE_OBJECT_NONE:
return "error: section [CANObject], code [none]";
case ERROR_CODE_OBJECT_UNSUPPORTED_EVENT_TYPE:
return "error: section [CANObject], code [unsupported event type]";
case ERROR_CODE_OBJECT_UNSUPPORTED_TIMER_TYPE:
return "error: section [CANObject], code [unsupported timer type]";
case ERROR_CODE_OBJECT_SET_FUNCTION_IS_MISSING:
return "error: section [CANObject], code [set function is missing]";
case ERROR_CODE_OBJECT_UNSUPPORTED_FUNCTION:
return "error: section [CANObject], code [unsupported function]";
case ERROR_CODE_OBJECT_INCORRECT_REQUEST:
return "error: section [CANObject], code [incorrect request]";
case ERROR_CODE_OBJECT_INCORRECT_FUNCTION_WORKFLOW:
return "error: section [CANObject], code [incorrect function workflow]";
case ERROR_CODE_OBJECT_HAVE_NO_DATA:
return "error: section [CANObject], code [have no data]";
case ERROR_CODE_OBJECT_INCORRECT_DATA_LENGTH:
return "error: section [CANObject], code [incorrect data length]";
case ERROR_CODE_OBJECT_SYSTEM_REQUEST_SHOULD_NOT_HAVE_DATA:
return "error: section [CANObject], code [system request should not have any frame data]";
case ERROR_CODE_OBJECT_TOGGLE_FUNCTION_IS_MISSING:
return "error: section [CANObject], code [external toggle handler is missing]";
case ERROR_CODE_OBJECT_TOGGLE_COMMAND_FRAME_SHOULD_NOT_HAVE_DATA:
return "error: section [CANObject], code [toggle command frame should not have any frame data]";
case ERROR_CODE_OBJECT_ACTION_FUNCTION_IS_MISSING:
return "error: section [CANObject], code [external action handler is missing]";
case ERROR_CODE_OBJECT_ACTION_COMMAND_FRAME_SHOULD_NOT_HAVE_DATA:
return "error: section [CANObject], code [action command frame should not have any frame data]";
case ERROR_CODE_OBJECT_LOCK_COMMAND_FRAME_DATA_LENGTH_ERROR:
return "error: section [CANObject], code [lock command frame data length error]";
case ERROR_CODE_OBJECT_LOCK_LEVEL_IS_UNKNOWN:
return "error: section [CANObject], code [unknown lock level]";
case ERROR_CODE_OBJECT_LOCKED:
return "error: section [CANObject], code [object locked for this function]";
case ERROR_CODE_OBJECT_BAD_INCOMING_CAN_FRAME:
return "error: section [CANObject], code [incoming CAN frame not initialized]";
case ERROR_CODE_OBJECT_HARDWARE_ERROR_CODE_IS_MISSING:
return "error: section [CANObject], code [hardware error code is missing]";
case ERROR_CODE_OBJECT_SOMETHING_WRONG:
return "error: section [CANObject], code [something went wrong]";
default:
return "error: section [CANObject], code [unknown]";
}
break;
case ERROR_SECTION_HARDWARE:
return "error: section [hardware], code [description is hardware related]";
default:
return "error: section [unknown], code [-]";
}
#else // DEBUG
return "detailed names are disabled";
#endif // DEBUG
}