unify buffer size for ASCII CAN frame string representations

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
pull/504/head
Oliver Hartkopp 2024-02-29 07:07:20 +01:00
parent 34a1cfad29
commit 1da219922d
5 changed files with 43 additions and 43 deletions

View File

@ -327,7 +327,7 @@ int main(int argc, char **argv)
FILE *logfile = NULL; FILE *logfile = NULL;
char fname[83]; /* suggested by -Wformat-overflow= */ char fname[83]; /* suggested by -Wformat-overflow= */
const char *logname = NULL; const char *logname = NULL;
static char abuf[10000]; /* ASCII buf FIXME - use calculated value */ static char afrbuf[AFRSZ]; /* ASCII CAN frame buffer size */
static int alen; static int alen;
signal(SIGTERM, sigterm); signal(SIGTERM, sigterm);
@ -831,22 +831,22 @@ int main(int argc, char **argv)
/* build common log format output */ /* build common log format output */
if ((log) || ((logfrmt) && (silent == SILENT_OFF))) { if ((log) || ((logfrmt) && (silent == SILENT_OFF))) {
alen = sprint_timestamp(abuf, logtimestamp, alen = sprint_timestamp(afrbuf, logtimestamp,
&tv, &last_tv); &tv, &last_tv);
alen += sprintf(abuf + alen, "%*s ", alen += sprintf(afrbuf + alen, "%*s ",
max_devname_len, devname[idx]); max_devname_len, devname[idx]);
alen += sprint_canframe(abuf + alen, &cu, 0); alen += sprint_canframe(afrbuf + alen, &cu, 0);
} }
/* write CAN frame in log file style to logfile */ /* write CAN frame in log file style to logfile */
if (log) if (log)
fprintf(logfile, "%s%s\n", abuf, extra_info); fprintf(logfile, "%s%s\n", afrbuf, extra_info);
/* print CAN frame in log file style to stdout */ /* print CAN frame in log file style to stdout */
if ((logfrmt) && (silent == SILENT_OFF)) { if ((logfrmt) && (silent == SILENT_OFF)) {
printf("%s%s\n", abuf, extra_info); printf("%s%s\n", afrbuf, extra_info);
goto out_fflush; /* no other output to stdout */ goto out_fflush; /* no other output to stdout */
} }
@ -860,32 +860,32 @@ int main(int argc, char **argv)
} }
/* print (colored) long CAN frame style to stdout */ /* print (colored) long CAN frame style to stdout */
alen = sprintf(abuf, " %s", (color > 2) ? col_on[idx % MAXCOL] : ""); alen = sprintf(afrbuf, " %s", (color > 2) ? col_on[idx % MAXCOL] : "");
alen += sprint_timestamp(abuf + alen, timestamp, &tv, &last_tv); alen += sprint_timestamp(afrbuf + alen, timestamp, &tv, &last_tv);
alen += sprintf(abuf + alen, " %s%*s", alen += sprintf(afrbuf + alen, " %s%*s",
(color && (color < 3)) ? col_on[idx % MAXCOL] : "", (color && (color < 3)) ? col_on[idx % MAXCOL] : "",
max_devname_len, devname[idx]); max_devname_len, devname[idx]);
if (extra_msg_info) { if (extra_msg_info) {
if (msg.msg_flags & MSG_DONTROUTE) if (msg.msg_flags & MSG_DONTROUTE)
alen += sprintf(abuf + alen, " TX %s", alen += sprintf(afrbuf + alen, " TX %s",
extra_m_info[cu.fd.flags & 3]); extra_m_info[cu.fd.flags & 3]);
else else
alen += sprintf(abuf + alen, " RX %s", alen += sprintf(afrbuf + alen, " RX %s",
extra_m_info[cu.fd.flags & 3]); extra_m_info[cu.fd.flags & 3]);
} }
alen += sprintf(abuf + alen, "%s ", (color == 1) ? col_off : ""); alen += sprintf(afrbuf + alen, "%s ", (color == 1) ? col_off : "");
alen += sprint_long_canframe(abuf + alen, &cu, view); alen += sprint_long_canframe(afrbuf + alen, &cu, view);
if ((view & CANLIB_VIEW_ERROR) && (cu.fd.can_id & CAN_ERR_FLAG)) { if ((view & CANLIB_VIEW_ERROR) && (cu.fd.can_id & CAN_ERR_FLAG)) {
alen += sprintf(abuf + alen, "\n\t"); alen += sprintf(afrbuf + alen, "\n\t");
alen += snprintf_can_error_frame(abuf + alen, alen += snprintf_can_error_frame(afrbuf + alen,
sizeof(abuf) - alen, sizeof(afrbuf) - alen,
&cu.fd, "\n\t"); &cu.fd, "\n\t");
} }
printf("%s%s\n", abuf, (color > 1) ? col_off : ""); printf("%s%s\n", afrbuf, (color > 1) ? col_off : "");
out_fflush: out_fflush:
fflush(stdout); fflush(stdout);
} }

View File

@ -828,16 +828,17 @@ int main(int argc, char **argv)
} }
if (verbose) { if (verbose) {
static char abuf[10000]; /* ASCII buf FIXME - use calculated value */ static char afrbuf[AFRSZ]; /* ASCII CAN frame buffer size */
printf(" %s ", argv[optind]); printf(" %s ", argv[optind]);
if (verbose > 1) if (verbose > 1)
sprint_long_canframe(abuf, (cu_t *)&frame, (verbose > 2) ? CANLIB_VIEW_ASCII : 0); sprint_long_canframe(afrbuf, (cu_t *)&frame, (verbose > 2) ? CANLIB_VIEW_ASCII : 0);
else else
sprint_canframe(abuf, (cu_t *)&frame, 1); sprint_canframe(afrbuf, (cu_t *)&frame, 1);
printf("%s\n", abuf); printf("%s\n", afrbuf);
} }
ret = do_send_one(s, &frame, mtu, polltimeout); ret = do_send_one(s, &frame, mtu, polltimeout);

View File

@ -239,7 +239,7 @@ int add_assignment(char *mode, int socket, char *txname, char *rxname, int verbo
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
static char buf[BUFSZ], device[BUFSZ], ascframe[10000]; static char buf[BUFSZ], device[BUFSZ], afrbuf[AFRSZ];
struct sockaddr_can addr; struct sockaddr_can addr;
struct can_raw_vcid_options vcid_opts = { struct can_raw_vcid_options vcid_opts = {
.flags = CAN_RAW_XL_VCID_TX_PASS, .flags = CAN_RAW_XL_VCID_TX_PASS,
@ -426,7 +426,7 @@ int main(int argc, char **argv)
eof = 0; eof = 0;
if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, ascframe) != 4) { if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, afrbuf) != 4) {
fprintf(stderr, "incorrect line format in logfile\n"); fprintf(stderr, "incorrect line format in logfile\n");
return 1; return 1;
} }
@ -455,7 +455,7 @@ int main(int argc, char **argv)
if (interactive) if (interactive)
getchar(); getchar();
/* log_tv/device/ascframe are valid here */ /* log_tv/device/afrbuf are valid here */
if (strlen(device) >= IFNAMSIZ) { if (strlen(device) >= IFNAMSIZ) {
fprintf(stderr, "log interface name '%s' too long!", device); fprintf(stderr, "log interface name '%s' too long!", device);
@ -479,9 +479,9 @@ int main(int argc, char **argv)
} else if (txidx > 0) { /* only send to valid CAN devices */ } else if (txidx > 0) { /* only send to valid CAN devices */
txmtu = parse_canframe(ascframe, &cu); /* dual-use frame */ txmtu = parse_canframe(afrbuf, &cu); /* dual-use frame */
if (!txmtu) { if (!txmtu) {
fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe); fprintf(stderr, "wrong CAN frame format: '%s'!", afrbuf);
return 1; return 1;
} }
@ -499,8 +499,8 @@ int main(int argc, char **argv)
if (verbose) { if (verbose) {
printf("%s (%s) ", get_txname(device), device); printf("%s (%s) ", get_txname(device), device);
sprint_long_canframe(ascframe, &cu, CANLIB_VIEW_INDENT_SFF); sprint_long_canframe(afrbuf, &cu, CANLIB_VIEW_INDENT_SFF);
printf("%s\n", ascframe); printf("%s\n", afrbuf);
} }
if (count && (--count == 0)) if (count && (--count == 0))
@ -520,7 +520,7 @@ int main(int argc, char **argv)
break; break;
} }
if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, ascframe) != 4) { if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, afrbuf) != 4) {
fprintf(stderr, "incorrect line format in logfile\n"); fprintf(stderr, "incorrect line format in logfile\n");
return 1; return 1;
} }

19
lib.h
View File

@ -63,17 +63,16 @@ typedef union {
struct canxl_frame xl; struct canxl_frame xl;
} cu_t; } cu_t;
/* buffer sizes for CAN frame string representations */ #define CL_CFSZ 400 /* to be removed */
#define CL_ID (sizeof("12345678##1")) /*
#define CL_DATA sizeof(".AA") * The buffer size for ASCII CAN frame string representations
#define CL_BINDATA sizeof(".10101010") * covers also the 'long' CAN frame output from sprint_long_canframe()
* including (swapped) binary represetations, timestamps, netdevice names,
/* CAN FD ASCII hex short representation with DATA_SEPERATORs */ * lengths and error message details as the CAN XL data is cropped to 64
#define CL_CFSZ (2*CL_ID + 64*CL_DATA) * byte (the 'long' CAN frame output is only for display on terminals).
*/
/* CAN FD ASCII hex long representation with binary output */ #define AFRSZ 6300 /* 3*2048 (data) + 22 (timestamp) + 18 (netdev) + ID/HDR */
#define CL_LONGCFSZ (2*CL_ID + sizeof(" [255] ") + (64*CL_BINDATA))
/* CAN DLC to real data length conversion helpers especially for CAN FD */ /* CAN DLC to real data length conversion helpers especially for CAN FD */

View File

@ -183,7 +183,7 @@ void canfd_asc(struct canfd_frame *cf, int devno, int mtu, char *extra_info, FIL
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
static char buf[BUFSZ], device[BUFSZ], ascframe[10000], extra_info[BUFSZ]; static char buf[BUFSZ], device[BUFSZ], afrbuf[AFRSZ], extra_info[BUFSZ];
static cu_t cu; static cu_t cu;
static struct timeval tv, start_tv; static struct timeval tv, start_tv;
@ -262,13 +262,13 @@ int main(int argc, char **argv)
continue; continue;
if (sscanf(buf, "(%llu.%llu) %s %s %s", &sec, &usec, if (sscanf(buf, "(%llu.%llu) %s %s %s", &sec, &usec,
device, ascframe, extra_info) != 5) { device, afrbuf, extra_info) != 5) {
/* do not evaluate the extra info */ /* do not evaluate the extra info */
extra_info[0] = 0; extra_info[0] = 0;
if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec,
device, ascframe) != 4) { device, afrbuf) != 4) {
fprintf(stderr, "incorrect line format in logfile\n"); fprintf(stderr, "incorrect line format in logfile\n");
return 1; return 1;
} }
@ -295,7 +295,7 @@ int main(int argc, char **argv)
if (devno) { /* only convert for selected CAN devices */ if (devno) { /* only convert for selected CAN devices */
mtu = parse_canframe(ascframe, &cu); mtu = parse_canframe(afrbuf, &cu);
/* convert only CAN CC and CAN FD frames */ /* convert only CAN CC and CAN FD frames */
if ((mtu != CAN_MTU) && (mtu != CANFD_MTU)) if ((mtu != CAN_MTU) && (mtu != CANFD_MTU))