Compare commits

...

6 Commits

Author SHA1 Message Date
GGZ8 8ac69dc4c7
Merge 7815be8741 into 1f038b3cd8 2026-01-08 10:49:34 +01:00
Marc Kleine-Budde 1f038b3cd8
Merge pull request #614 from hartkopp/master
canxl extension: use plain union instead of typedef
2025-12-18 19:26:31 +01:00
Oliver Hartkopp 392ccc970b canxl extension: use plain union instead of typedef
"In general, a pointer, or a struct that has elements that can reasonably
be directly accessed should never be a typedef."

https://www.kernel.org/doc/html/v5.0/process/coding-style.html#typedefs

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2025-12-18 18:23:38 +01: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
10 changed files with 80 additions and 28 deletions

View File

@ -84,7 +84,7 @@ static void print_usage(char *prg)
}
static void prframe(FILE *file, struct timeval *tv, int dev,
cu_t *cf, char dir)
union cfu *cf, char dir)
{
static char abuf[BUFLEN];
@ -177,7 +177,7 @@ static void eval_can(char* buf, struct timeval *date_tvp, char timestamps,
cf.len = CAN_ERR_DLC;
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
prframe(outfile, &tv, interface, (cu_t *)&cf, NO_DIR);
prframe(outfile, &tv, interface, (union cfu *)&cf, NO_DIR);
fflush(outfile);
return;
}
@ -249,7 +249,7 @@ static void eval_can(char* buf, struct timeval *date_tvp, char timestamps,
cf.data[i] = data[i] & 0xFFU;
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
prframe(outfile, &tv, interface, (union cfu *)&cf, dir[0]);
fflush(outfile);
}
}
@ -374,7 +374,7 @@ static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps,
}
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
prframe(outfile, &tv, interface, (union cfu *)&cf, dir[0]);
fflush(outfile);
/* No support for really strange CANFD ErrorFrames format m( */
@ -487,7 +487,7 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps,
cf.len8_dlc = dlc;
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
prframe(outfile, &tv, interface, (union cfu *)&cf, dir[0]);
fflush(outfile);
}
@ -594,7 +594,7 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps,
cf.flags |= CANFD_ESI;
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
prframe(outfile, &tv, interface, (union cfu *)&cf, dir[0]);
fflush(outfile);
}
@ -723,7 +723,7 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps,
cf.flags |= CANXL_RRS;
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
prframe(outfile, &tv, interface, (union cfu *)&cf, dir[0]);
fflush(outfile);
/* No support for CAN XL ErrorFrames */

View File

@ -342,7 +342,7 @@ int main(int argc, char **argv)
struct cmsghdr *cmsg;
struct can_filter *rfilter;
can_err_mask_t err_mask;
static cu_t cu; /* union for CAN CC/FD/XL frames */
static union cfu cu; /* union for CAN CC/FD/XL frames */
int nbytes, i;
struct ifreq ifr;
struct timespec ts, last_ts;

View File

@ -299,7 +299,7 @@ static int setsockopt_txtime(int fd)
return 0;
}
static int do_send_one(int fd, cu_t *cu, size_t len, int timeout)
static int do_send_one(int fd, union cfu *cu, size_t len, int timeout)
{
uint8_t control[CMSG_SPACE(sizeof(uint64_t))] = { 0 };
struct iovec iov = {
@ -494,7 +494,7 @@ int main(int argc, char **argv)
struct can_raw_vcid_options vcid_opts = {
.flags = CAN_RAW_XL_VCID_TX_PASS,
};
static cu_t cu;
static union cfu cu;
int i;
struct ifreq ifr = { 0 };
const int enable_canfx = 1;
@ -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

@ -191,7 +191,7 @@ int main(int argc, char **argv)
.rx_vcid_mask = 0,
};
struct can_filter rfilter;
static cu_t cu; /* union for CAN CC/FD/XL frames */
static union cfu cu; /* union for CAN CC/FD/XL frames */
const int canfx_on = 1;
int nbytes, i, j;
struct ifreq ifr;

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");
@ -261,7 +270,7 @@ int main(int argc, char **argv)
struct can_raw_vcid_options vcid_opts = {
.flags = CAN_RAW_XL_VCID_TX_PASS,
};
static cu_t cu;
static union cfu cu;
static struct timeval today_tv, log_tv, last_log_tv, diff_tv;
struct timespec sleep_ts;
int s; /* CAN_RAW socket */
@ -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 */

View File

@ -117,7 +117,7 @@ int main(int argc, char **argv)
struct can_raw_vcid_options vcid_opts = {
.flags = CAN_RAW_XL_VCID_TX_PASS,
};
static cu_t cu;
static union cfu cu;
struct ifreq ifr;
/* check command line options */

6
lib.c
View File

@ -155,7 +155,7 @@ int hexstring2data(char *arg, unsigned char *data, int maxdlen)
return 0;
}
int parse_canframe(char *cs, cu_t *cu)
int parse_canframe(char *cs, union cfu *cu)
{
/* documentation see lib.h */
@ -309,7 +309,7 @@ int parse_canframe(char *cs, cu_t *cu)
return mtu;
}
int snprintf_canframe(char *buf, size_t size, cu_t *cu, int sep)
int snprintf_canframe(char *buf, size_t size, union cfu *cu, int sep)
{
/* documentation see lib.h */
@ -433,7 +433,7 @@ int snprintf_canframe(char *buf, size_t size, cu_t *cu, int sep)
return offset;
}
int snprintf_long_canframe(char *buf, size_t size, cu_t *cu, int view)
int snprintf_long_canframe(char *buf, size_t size, union cfu *cu, int view)
{
/* documentation see lib.h */

10
lib.h
View File

@ -59,11 +59,11 @@ static inline int pr_debug(const char* fmt, ...) {return 0;}
#endif
/* CAN CC/FD/XL frame union */
typedef union {
union cfu {
struct can_frame cc;
struct canfd_frame fd;
struct canxl_frame xl;
} cu_t;
};
/*
* The buffer size for ASCII CAN frame string representations
@ -110,7 +110,7 @@ int hexstring2data(char *arg, unsigned char *data, int maxdlen);
*
*/
int parse_canframe(char *cs, cu_t *cu);
int parse_canframe(char *cs, union cfu *cu);
/*
* Transfers a valid ASCII string describing a CAN frame into the CAN union
* containing CAN CC/FD/XL structs.
@ -180,7 +180,7 @@ int parse_canframe(char *cs, cu_t *cu);
* - CAN FD frames do not have a RTR bit
*/
int snprintf_canframe(char *buf, size_t size, cu_t *cu, int sep);
int snprintf_canframe(char *buf, size_t size, union cfu *cu, int sep);
/*
* Creates a CAN frame hexadecimal output in compact format.
* The CAN data[] is separated by '.' when sep != 0.
@ -213,7 +213,7 @@ int snprintf_canframe(char *buf, size_t size, cu_t *cu, int sep);
#define SWAP_DELIMITER '`'
int snprintf_long_canframe(char *buf, size_t size, cu_t *cu, int view);
int snprintf_long_canframe(char *buf, size_t size, union cfu *cu, int view);
/*
* Creates a CAN frame hexadecimal output in user readable format.
*

View File

@ -183,7 +183,7 @@ static void canfd_asc(struct canfd_frame *cf, int devno, int mtu,
fprintf(outfile, " %8d %4d %8X 0 0 0 0 0", 130000, 130, flags);
}
static void canxl_asc(cu_t *cu, int devno, int mtu,
static void canxl_asc(union cfu *cu, int devno, int mtu,
char *extra_info, FILE *outfile)
{
char id[10];
@ -314,7 +314,7 @@ int main(int argc, char **argv)
{
static char buf[BUFSZ], device[DEVSZ], afrbuf[AFRSZ], extra_info[EXTRASZ];
static cu_t cu;
static union cfu cu;
static struct timeval tv, start_tv;
FILE *infile = stdin;
FILE *outfile = stdout;

View File

@ -68,7 +68,7 @@
int main(void)
{
static char buf[BUFSZ], timestamp[TIMESZ], device[DEVSZ], afrbuf[AFRSZ];
static cu_t cu;
static union cfu cu;
int mtu;
while (fgets(buf, BUFSZ-1, stdin)) {