-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
391 lines (345 loc) · 14.4 KB
/
Copy pathdemo.cpp
File metadata and controls
391 lines (345 loc) · 14.4 KB
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
389
390
391
#include "asc_parser/asc_parser.h"
#include <stdio.h>
#include <string.h>
// Color output macros (optional, for better visualization)
#ifdef _WIN32
#define COLOR_RESET ""
#define COLOR_RED ""
#define COLOR_GREEN ""
#define COLOR_YELLOW ""
#define COLOR_BLUE ""
#define COLOR_CYAN ""
#else
#define COLOR_RESET "\033[0m"
#define COLOR_RED "\033[31m"
#define COLOR_GREEN "\033[32m"
#define COLOR_YELLOW "\033[33m"
#define COLOR_BLUE "\033[34m"
#define COLOR_CYAN "\033[36m"
#endif
// Print frame in detailed format
static void print_frame_detailed(const asc_can_frame_t* frame, int frame_number) {
printf("\n%s=======================================================================%s\n",
COLOR_CYAN, COLOR_RESET);
printf("%sFrame #%d%s\n", COLOR_YELLOW, frame_number, COLOR_RESET);
printf("%s=======================================================================%s\n",
COLOR_CYAN, COLOR_RESET);
// Timestamp
printf(" %sTimestamp:%s %.6f s (relative)\n", COLOR_GREEN, COLOR_RESET, frame->timestamp);
printf(" %sAbsolute Time:%s %.6f s (epoch-based)\n", COLOR_GREEN, COLOR_RESET, frame->absolute_timestamp);
// Bus and ID
printf(" %sBus:%s %d\n", COLOR_GREEN, COLOR_RESET, frame->bus);
printf(" %sCAN ID:%s 0x%X%s%s (",
COLOR_GREEN, COLOR_RESET,
frame->frame_id,
frame->extended_format ? "x" : "",
frame->extended_format ? COLOR_YELLOW " [Extended]" COLOR_RESET : "");
if (frame->extended_format) {
printf("29-bit)\n");
} else {
printf("11-bit)\n");
}
// Frame type
printf(" %sFrame Type:%s ", COLOR_GREEN, COLOR_RESET);
if (frame->is_canfd) {
printf("%sCAN-FD%s ", COLOR_BLUE, COLOR_RESET);
if (frame->bitrate_switch) {
printf("[BRS] ");
}
if (frame->error_state_indicator) {
printf("[ESI] ");
}
} else {
printf("Classic CAN ");
}
switch (frame->frame_type) {
case ASC_FRAME_DATA:
printf("- Data Frame\n");
break;
case ASC_FRAME_REMOTE:
printf("- Remote Frame\n");
break;
case ASC_FRAME_ERROR:
printf("%s- Error Frame%s\n", COLOR_RED, COLOR_RESET);
break;
}
// Direction
printf(" %sDirection:%s %s\n",
COLOR_GREEN, COLOR_RESET,
frame->direction == ASC_FRAME_RX ? "Rx (Receive)" : "Tx (Transmit)");
// Data length
printf(" %sData Length:%s %d bytes\n", COLOR_GREEN, COLOR_RESET, frame->payload_length);
// Data payload
if (frame->payload_length > 0 && frame->frame_type == ASC_FRAME_DATA) {
printf(" %sPayload Data:%s\n", COLOR_GREEN, COLOR_RESET);
printf(" Hex: ");
for (int i = 0; i < frame->payload_length; i++) {
printf("%02X ", frame->payload[i]);
if ((i + 1) % 8 == 0 && i < frame->payload_length - 1) {
printf("\n ");
}
}
printf("\n");
printf(" Dec: ");
for (int i = 0; i < frame->payload_length; i++) {
printf("%3d ", frame->payload[i]);
if ((i + 1) % 8 == 0 && i < frame->payload_length - 1) {
printf("\n ");
}
}
printf("\n");
printf(" ASCII: ");
for (int i = 0; i < frame->payload_length; i++) {
if (frame->payload[i] >= 32 && frame->payload[i] <= 126) {
printf("%c", frame->payload[i]);
} else {
printf(".");
}
}
printf("\n");
}
// Signal name (if available)
if (strlen(frame->signal_name) > 0) {
printf(" %sSignal Name:%s %s\n", COLOR_GREEN, COLOR_RESET, frame->signal_name);
}
}
// Print frame in compact table format
static void print_frame_compact(const asc_can_frame_t* frame) {
// Timestamp
printf("%12.6f ", frame->timestamp);
// Bus
printf("%2d ", frame->bus);
// CAN ID
if (frame->extended_format) {
printf("0x%08X%s ", frame->frame_id, "x");
} else {
printf("0x%03X ", frame->frame_id);
}
// Direction
printf("%-2s ", frame->direction == ASC_FRAME_RX ? "Rx" : "Tx");
// Type
if (frame->is_canfd) {
printf("FD ");
} else {
printf("CL ");
}
switch (frame->frame_type) {
case ASC_FRAME_DATA:
printf("D ");
break;
case ASC_FRAME_REMOTE:
printf("R ");
break;
case ASC_FRAME_ERROR:
printf("E ");
break;
}
// Length
printf("%2d ", frame->payload_length);
// Data
for (int i = 0; i < frame->payload_length && i < 8; i++) {
printf("%02X ", frame->payload[i]);
}
if (frame->payload_length > 8) {
printf("... ");
}
// Signal name
if (strlen(frame->signal_name) > 0) {
printf(" [%s]", frame->signal_name);
}
printf("\n");
}
// Print statistics
static void print_statistics(int total_frames, int rx_frames, int tx_frames,
int data_frames, int remote_frames, int error_frames,
int canfd_frames, int extended_frames) {
printf("\n%s+================================================================+%s\n",
COLOR_CYAN, COLOR_RESET);
printf("%s| STATISTICS |%s\n",
COLOR_CYAN, COLOR_RESET);
printf("%s+================================================================+%s\n",
COLOR_CYAN, COLOR_RESET);
printf("%s|%s Total Frames: %-8d %s|%s\n",
COLOR_CYAN, COLOR_RESET, total_frames, COLOR_CYAN, COLOR_RESET);
printf("%s|%s %s|%s\n",
COLOR_CYAN, COLOR_RESET, COLOR_CYAN, COLOR_RESET);
printf("%s|%s Direction: %s|%s\n",
COLOR_CYAN, COLOR_RESET, COLOR_CYAN, COLOR_RESET);
printf("%s|%s - Rx (Receive): %-8d (%.1f%%) %s|%s\n",
COLOR_CYAN, COLOR_RESET, rx_frames,
total_frames > 0 ? (rx_frames * 100.0 / total_frames) : 0.0,
COLOR_CYAN, COLOR_RESET);
printf("%s|%s - Tx (Transmit): %-8d (%.1f%%) %s|%s\n",
COLOR_CYAN, COLOR_RESET, tx_frames,
total_frames > 0 ? (tx_frames * 100.0 / total_frames) : 0.0,
COLOR_CYAN, COLOR_RESET);
printf("%s|%s %s|%s\n",
COLOR_CYAN, COLOR_RESET, COLOR_CYAN, COLOR_RESET);
printf("%s|%s Frame Type: %s|%s\n",
COLOR_CYAN, COLOR_RESET, COLOR_CYAN, COLOR_RESET);
printf("%s|%s - Data Frames: %-8d (%.1f%%) %s|%s\n",
COLOR_CYAN, COLOR_RESET, data_frames,
total_frames > 0 ? (data_frames * 100.0 / total_frames) : 0.0,
COLOR_CYAN, COLOR_RESET);
printf("%s|%s - Remote Frames: %-8d (%.1f%%) %s|%s\n",
COLOR_CYAN, COLOR_RESET, remote_frames,
total_frames > 0 ? (remote_frames * 100.0 / total_frames) : 0.0,
COLOR_CYAN, COLOR_RESET);
printf("%s|%s - Error Frames: %-8d (%.1f%%) %s|%s\n",
COLOR_CYAN, COLOR_RESET, error_frames,
total_frames > 0 ? (error_frames * 100.0 / total_frames) : 0.0,
COLOR_CYAN, COLOR_RESET);
printf("%s|%s %s|%s\n",
COLOR_CYAN, COLOR_RESET, COLOR_CYAN, COLOR_RESET);
printf("%s|%s Format: %s|%s\n",
COLOR_CYAN, COLOR_RESET, COLOR_CYAN, COLOR_RESET);
printf("%s|%s - CAN-FD Frames: %-8d (%.1f%%) %s|%s\n",
COLOR_CYAN, COLOR_RESET, canfd_frames,
total_frames > 0 ? (canfd_frames * 100.0 / total_frames) : 0.0,
COLOR_CYAN, COLOR_RESET);
printf("%s|%s - Extended ID: %-8d (%.1f%%) %s|%s\n",
COLOR_CYAN, COLOR_RESET, extended_frames,
total_frames > 0 ? (extended_frames * 100.0 / total_frames) : 0.0,
COLOR_CYAN, COLOR_RESET);
printf("%s+================================================================+%s\n",
COLOR_CYAN, COLOR_RESET);
}
static void print_usage(const char* program_name) {
printf("Usage: %s [options] <asc_file>\n\n", program_name);
printf("Options:\n");
printf(" -d, --detailed Print detailed information for each frame\n");
printf(" -c, --compact Print compact table format (default)\n");
printf(" -l, --limit N Limit output to first N frames\n");
printf(" -h, --help Show this help message\n\n");
printf("Examples:\n");
printf(" %s input.asc # Print all frames in compact format\n", program_name);
printf(" %s -d input.asc # Print all frames in detailed format\n", program_name);
printf(" %s -l 10 input.asc # Print first 10 frames only\n", program_name);
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("%sError: No input file specified%s\n\n", COLOR_RED, COLOR_RESET);
print_usage(argv[0]);
return 1;
}
// Parse command line arguments
bool detailed_mode = false;
int frame_limit = -1; // -1 means no limit
const char* input_file = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--detailed") == 0) {
detailed_mode = true;
} else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--compact") == 0) {
detailed_mode = false;
} else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--limit") == 0) {
if (i + 1 < argc) {
frame_limit = (int)strtol(argv[++i], NULL, 10);
} else {
printf("%sError: Frame limit value required after -l/--limit%s\n",
COLOR_RED, COLOR_RESET);
return 1;
}
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
print_usage(argv[0]);
return 0;
} else {
input_file = argv[i];
}
}
if (!input_file) {
printf("%sError: No input file specified%s\n\n", COLOR_RED, COLOR_RESET);
print_usage(argv[0]);
return 1;
}
// Print header
printf("\n%s", COLOR_CYAN);
printf("+==========================================================================+\n");
printf("| ASC FILE PARSER DEMO |\n");
printf("+==========================================================================+\n");
printf("%s", COLOR_RESET);
printf("\n%sFile:%s %s\n", COLOR_GREEN, COLOR_RESET, input_file);
printf("%sMode:%s %s\n", COLOR_GREEN, COLOR_RESET,
detailed_mode ? "Detailed" : "Compact");
if (frame_limit > 0) {
printf("%sLimit:%s First %d frames\n", COLOR_GREEN, COLOR_RESET, frame_limit);
}
printf("\n");
// Create parser
asc_parser_t* parser = asc_parser_create(input_file);
if (!parser) {
printf("%sError: Failed to create parser%s\n", COLOR_RED, COLOR_RESET);
return 1;
}
if (parser->has_error) {
printf("%sError: %s%s\n", COLOR_RED, asc_parser_get_error(parser), COLOR_RESET);
asc_parser_destroy(parser);
return 1;
}
// Print table header for compact mode
if (!detailed_mode) {
printf("%s", COLOR_BLUE);
printf("----------------------------------------------------------------------------\n");
printf(" Timestamp Bus CAN ID Dir Type Len Data\n");
printf("----------------------------------------------------------------------------\n");
printf("%s", COLOR_RESET);
}
// Statistics counters
int total_frames = 0;
int rx_frames = 0;
int tx_frames = 0;
int data_frames = 0;
int remote_frames = 0;
int error_frames = 0;
int canfd_frames = 0;
int extended_frames = 0;
// Parse and print frames
asc_can_frame_t frame;
while (asc_parser_read_frame(parser, &frame)) {
total_frames++;
// Update statistics
if (frame.direction == ASC_FRAME_RX) rx_frames++;
else tx_frames++;
if (frame.frame_type == ASC_FRAME_DATA) data_frames++;
else if (frame.frame_type == ASC_FRAME_REMOTE) remote_frames++;
else if (frame.frame_type == ASC_FRAME_ERROR) error_frames++;
if (frame.is_canfd) canfd_frames++;
if (frame.extended_format) extended_frames++;
// Print frame if within limit
if (frame_limit < 0 || total_frames <= frame_limit) {
if (detailed_mode) {
print_frame_detailed(&frame, total_frames);
} else {
print_frame_compact(&frame);
}
}
// Show progress for large files
if (!detailed_mode && total_frames % 1000 == 0) {
printf("%s... Processed %d frames ...%s\n",
COLOR_YELLOW, total_frames, COLOR_RESET);
}
}
// Print footer for compact mode
if (!detailed_mode) {
printf("%s", COLOR_BLUE);
printf("----------------------------------------------------------------------------\n");
printf("%s", COLOR_RESET);
}
// Print statistics
print_statistics(total_frames, rx_frames, tx_frames,
data_frames, remote_frames, error_frames,
canfd_frames, extended_frames);
const long skipped = asc_parser_get_lines_skipped(parser);
if (skipped > 0) {
printf("\n%sWarning: %ld malformed frame line(s) skipped%s\n",
COLOR_YELLOW, skipped, COLOR_RESET);
}
// Check for errors
if (parser->has_error) {
printf("\n%sParser Warning: %s%s\n",
COLOR_YELLOW, asc_parser_get_error(parser), COLOR_RESET);
}
// Cleanup
asc_parser_destroy(parser);
printf("\n%sDone.%s\n\n", COLOR_GREEN, COLOR_RESET);
return 0;
}