Skip to content

Commit e478abc

Browse files
committed
RealCAN implementation
Replay pre-recorded CAN Bus dumps respecting the original relative timestamps. Fix code style Conform to codebase style as per maintainer's suggestions. Refactor code logic according to suggestions for PR #521
1 parent 185c14f commit e478abc

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

canplayer.c

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <string.h>
4949
#include <time.h>
5050
#include <unistd.h>
51+
#include <stdbool.h>
5152

5253
#include <linux/can.h>
5354
#include <linux/can/raw.h>
@@ -89,6 +90,12 @@ const int canfx_on = 1;
8990

9091
extern int optind, opterr, optopt;
9192

93+
struct sleep {
94+
struct timeval *sleep_vector;
95+
size_t idx;
96+
size_t size;
97+
};
98+
9299
static void print_usage(char *prg)
93100
{
94101
fprintf(stderr, "%s - replay a compact CAN frame logfile to CAN devices.\n", prg);
@@ -116,7 +123,9 @@ static void print_usage(char *prg)
116123
fprintf(stderr, " -x (disable local "
117124
"loopback of sent CAN frames)\n");
118125
fprintf(stderr, " -v (verbose: print "
119-
"sent CAN frames)\n\n");
126+
"sent CAN frames)\n");
127+
fprintf(stderr, " -r (real-time: send "
128+
"CAN frames in real-time)\n\n");
120129
fprintf(stderr, "Interface assignment:\n");
121130
fprintf(stderr, " 0..n assignments like <write-if>=<log-if>\n\n");
122131
fprintf(stderr, " e.g. vcan2=can0 (send frames received from can0 on "
@@ -252,6 +261,38 @@ static int add_assignment(char *mode, int socket, char *txname,
252261
return 0;
253262
}
254263

264+
void load_gaps_from_file(FILE *fp, struct sleep *timestamps)
265+
{
266+
char *line = NULL;
267+
size_t len = 0;
268+
ssize_t read;
269+
270+
while ((read = getline(&line, &len, fp)) != -1) {
271+
if (timestamps->idx == timestamps->size) {
272+
timestamps->size++;
273+
timestamps->sleep_vector = realloc(timestamps->sleep_vector, timestamps->size * sizeof(struct timeval));
274+
if (!timestamps->sleep_vector) {
275+
fprintf(stderr, "Failed to create the timestamps vector!\n");
276+
exit(1);
277+
}
278+
}
279+
sscanf(line, "(%lu.%lu)", &timestamps->sleep_vector[timestamps->idx].tv_sec, &timestamps->sleep_vector[timestamps->idx].tv_usec);
280+
timestamps->idx++;
281+
}
282+
free(line);
283+
}
284+
285+
void init_timestamps(struct sleep *timestamps, size_t init_size)
286+
{
287+
timestamps->size = init_size;
288+
timestamps->sleep_vector = calloc(init_size, sizeof(*(timestamps->sleep_vector)));
289+
if (!timestamps->sleep_vector) {
290+
fprintf(stderr, "Failed to create the timestamps vector!\n");
291+
exit(1);
292+
}
293+
timestamps->idx = 0;
294+
}
295+
255296
int main(int argc, char **argv)
256297
{
257298
static char buf[BUFSZ], device[DEVSZ], afrbuf[AFRSZ];
@@ -278,8 +319,11 @@ int main(int argc, char **argv)
278319
int eof, txmtu, i, j;
279320
char *fret;
280321
unsigned long long sec, usec;
322+
bool gap_from_file = false;
323+
struct sleep timestamps;
324+
struct timeval send_time, act_time, init_time;
281325

282-
while ((opt = getopt(argc, argv, "I:l:tin:g:s:xv?")) != -1) {
326+
while ((opt = getopt(argc, argv, "I:l:tin:g:s:xvr?")) != -1) {
283327
switch (opt) {
284328
case 'I':
285329
infile = fopen(optarg, "r");
@@ -334,6 +378,17 @@ int main(int argc, char **argv)
334378
verbose++;
335379
break;
336380

381+
case 'r':
382+
if (!isatty(fileno(infile))) {
383+
fprintf(stderr, "Specify an input file for option -r !\n");
384+
exit(EXIT_FAILURE);
385+
}
386+
gap_from_file = true; /* using time delta from file */
387+
init_timestamps(&timestamps, 1);
388+
load_gaps_from_file(infile, &timestamps);
389+
timestamps.idx = 0;
390+
break;
391+
337392
case '?':
338393
default:
339394
print_usage(basename(argv[0]));
@@ -362,8 +417,10 @@ int main(int argc, char **argv)
362417
printf("interactive mode: press ENTER to process next CAN frame ...\n");
363418
}
364419

365-
sleep_ts.tv_sec = gap / 1000;
366-
sleep_ts.tv_nsec = (gap % 1000) * 1000000;
420+
if (!gap_from_file) {
421+
sleep_ts.tv_sec = gap / 1000;
422+
sleep_ts.tv_nsec = (gap % 1000) * 1000000;
423+
}
367424

368425
/* open socket */
369426
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
@@ -507,6 +564,23 @@ int main(int argc, char **argv)
507564
addr.can_family = AF_CAN;
508565
addr.can_ifindex = txidx; /* send via this interface */
509566

567+
if (gap_from_file && timestamps.idx > 0) {
568+
send_time = timestamps.sleep_vector[timestamps.idx];
569+
gettimeofday(&act_time, NULL);
570+
timersub(&act_time, &init_time, &act_time);
571+
572+
while (timercmp(&act_time, &send_time, <)){
573+
gettimeofday(&act_time, NULL);
574+
timersub(&act_time, &init_time, &act_time);
575+
}
576+
}
577+
if (gap_from_file && timestamps.idx == 0) {
578+
gettimeofday(&init_time, NULL);
579+
gettimeofday(&act_time, NULL);
580+
timersub(&act_time, &init_time, &act_time);
581+
}
582+
timestamps.idx++;
583+
510584
if (sendto(s, &cu, txmtu, 0, (struct sockaddr *)&addr, sizeof(addr)) != txmtu) {
511585
perror("sendto");
512586
return 1;
@@ -564,7 +638,7 @@ int main(int argc, char **argv)
564638

565639
} /* while frames_to_send ... */
566640

567-
if (nanosleep(&sleep_ts, NULL))
641+
if (!gap_from_file && nanosleep(&sleep_ts, NULL))
568642
return 1;
569643

570644
delay_loops++; /* private statistics */

0 commit comments

Comments
 (0)