Compare commits

...

6 Commits

Author SHA1 Message Date
GGZ8 4be73985fc
Merge 7815be8741 into 88a0417a6b 2025-12-09 21:59:35 -05:00
Marc Kleine-Budde 88a0417a6b
Merge pull request #609 from dphipps-qnx/fix_cmsg_types
candump: Use SCM_* consts instead of SO_* for cmsg_type
2025-12-05 16:26:30 +01:00
Darcy Phipps 65e715d56d candump: Use SCM_* consts instead of SO_* for cmsg_type
There are explicit #defines for SCM_TIMESTAMP and SCM_TIMESTAMPING. They
currently happen to have the same value as SO_TIMESTAMP and
SO_TIMESTAMPING but the former should be used when checking cmsg_type in
case one day they do not.
2025-12-05 09:35:50 -05:00
GGZ8 7815be8741 WIP on pr-realcan: 277048e RealCAN implementation 2024-09-30 12:41:39 +02:00
GGZ8 dae324dbd5 index on pr-realcan: 277048e RealCAN implementation 2024-09-30 12:41:39 +02:00
GZZ8 277048eba6 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
2024-05-27 13:52:19 +02:00
3 changed files with 61 additions and 6 deletions

View File

@ -72,6 +72,9 @@
#ifndef SO_TIMESTAMPING
#define SO_TIMESTAMPING 37
#endif
#ifndef SCM_TIMESTAMPING
#define SCM_TIMESTAMPING SO_TIMESTAMPING
#endif
#define TIMESTAMPSZ 50 /* string 'absolute with date' requires max 49 bytes */
@ -807,13 +810,13 @@ int main(int argc, char **argv)
for (cmsg = CMSG_FIRSTHDR(&msg);
cmsg && (cmsg->cmsg_level == SOL_SOCKET);
cmsg = CMSG_NXTHDR(&msg,cmsg)) {
if (cmsg->cmsg_type == SO_TIMESTAMP) {
if (cmsg->cmsg_type == SCM_TIMESTAMP) {
struct timeval tv;
memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec;
ts.tv_nsec *= 1000;
} else if (cmsg->cmsg_type == SO_TIMESTAMPING) {
} else if (cmsg->cmsg_type == SCM_TIMESTAMPING) {
struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);
/*

View File

@ -867,7 +867,14 @@ int main(int argc, char **argv)
if (ret)
return 1;
int k[] = {1,2};
int j = 0;
while (running) {
ts_gap = double_to_timespec((k[j%2])/1000.0);
// printf("%lu, %lu\n", ts_gap.tv_sec, ts_gap.tv_nsec);
setsockopt_txtime(s);
setup_time();
j++;
/* clear values but preserve cu.fd.len */
cu.fd.flags = 0;
cu.fd.__res0 = 0;

View File

@ -48,6 +48,7 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
#include <linux/can.h>
#include <linux/can/raw.h>
@ -89,6 +90,12 @@ const int canfx_on = 1;
extern int optind, opterr, optopt;
struct sleep {
struct timeval *sleep_vector;
size_t idx;
size_t size;
};
static void print_usage(char *prg)
{
fprintf(stderr, "%s - replay a compact CAN frame logfile to CAN devices.\n", prg);
@ -117,6 +124,8 @@ static void print_usage(char *prg)
"loopback of sent CAN frames)\n");
fprintf(stderr, " -v (verbose: print "
"sent CAN frames)\n");
fprintf(stderr, " -r (real-time: send "
"CAN frames in real-time)\n");
fprintf(stderr, " -h (show "
"this help message)\n\n");
fprintf(stderr, "Interface assignment:\n");
@ -280,8 +289,11 @@ int main(int argc, char **argv)
int eof, txmtu, i, j;
char *fret;
unsigned long long sec, usec;
bool gap_from_file = false;
struct sleep timestamps;
struct timeval send_time, act_time, init_trace, init_time;
while ((opt = getopt(argc, argv, "I:l:tin:g:s:xvh")) != -1) {
while ((opt = getopt(argc, argv, "I:l:tin:g:s:xvrh")) != -1) {
switch (opt) {
case 'I':
infile = fopen(optarg, "r");
@ -336,6 +348,17 @@ int main(int argc, char **argv)
verbose++;
break;
case 'r':
if (isatty(fileno(infile))) {
fprintf(stderr, "Specify an input file for option -r !\n");
exit(EXIT_FAILURE);
}
gap_from_file = true; /* using time delta from file */
init_trace.tv_sec = 0;
init_trace.tv_usec = 0;
timestamps.idx = 0; /*to avoid warning accessing idx variable*/
break;
case 'h':
print_usage(basename(argv[0]));
exit(EXIT_SUCCESS);
@ -368,8 +391,10 @@ int main(int argc, char **argv)
printf("interactive mode: press ENTER to process next CAN frame ...\n");
}
sleep_ts.tv_sec = gap / 1000;
sleep_ts.tv_nsec = (gap % 1000) * 1000000;
if (!gap_from_file) {
sleep_ts.tv_sec = gap / 1000;
sleep_ts.tv_nsec = (gap % 1000) * 1000000;
}
/* open socket */
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
@ -553,6 +578,26 @@ int main(int argc, char **argv)
}
log_tv.tv_sec = sec;
if (gap_from_file){
if (timestamps.idx == 0){
gettimeofday(&init_time, NULL);
if (log_tv.tv_sec > 0 || log_tv.tv_usec > 0)
init_trace = log_tv;
}
timersub(&log_tv, &init_trace, &send_time);
if (timestamps.idx > 0){
gettimeofday(&act_time, NULL);
timersub(&act_time, &init_time, &act_time);
while (timercmp(&act_time, &send_time, <)){
gettimeofday(&act_time, NULL);
timersub(&act_time, &init_time, &act_time);
}
}
timestamps.idx++;
}
/*
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
* 3rd party or handcrafted logfiles that treat the timestamp as float
@ -582,7 +627,7 @@ int main(int argc, char **argv)
} /* while frames_to_send ... */
if (nanosleep(&sleep_ts, NULL))
if (!gap_from_file && nanosleep(&sleep_ts, NULL))
return 1;
delay_loops++; /* private statistics */