cangen.c: added -c option for bursty output.

specify -c 100 to have 100 packets sent in burst before a gap.
use for example -p 1000 to avoid TX buffer full errors
pull/201/head
Rasmus Pedersen 2020-05-01 12:58:44 +02:00
parent 7575d43fd2
commit e03fd06abe
1 changed files with 14 additions and 5 deletions

View File

@ -66,6 +66,7 @@
#include "lib.h" #include "lib.h"
#define DEFAULT_GAP 200 /* ms */ #define DEFAULT_GAP 200 /* ms */
#define DEFAULT_BURST_COUNT 1
#define MODE_RANDOM 0 #define MODE_RANDOM 0
#define MODE_INCREMENT 1 #define MODE_INCREMENT 1
@ -104,6 +105,8 @@ void print_usage(char *prg)
" write() syscalls)\n"); " write() syscalls)\n");
fprintf(stderr, " -x (disable local loopback of " fprintf(stderr, " -x (disable local loopback of "
"generated CAN frames)\n"); "generated CAN frames)\n");
fprintf(stderr, " -c (number of messages to send in burst, "
"default 1)\n");
fprintf(stderr, " -v (increment verbose level for " fprintf(stderr, " -v (increment verbose level for "
"printing sent CAN frames)\n\n"); "printing sent CAN frames)\n\n");
fprintf(stderr, "Generation modes:\n"); fprintf(stderr, "Generation modes:\n");
@ -136,6 +139,7 @@ void sigterm(int signo)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
double gap = DEFAULT_GAP; double gap = DEFAULT_GAP;
unsigned long burst_count = DEFAULT_BURST_COUNT;
unsigned long polltimeout = 0; unsigned long polltimeout = 0;
unsigned char ignore_enobufs = 0; unsigned char ignore_enobufs = 0;
unsigned char extended = 0; unsigned char extended = 0;
@ -149,6 +153,7 @@ int main(int argc, char **argv)
unsigned char verbose = 0; unsigned char verbose = 0;
unsigned char rtr_frame = 0; unsigned char rtr_frame = 0;
int count = 0; int count = 0;
unsigned long burst_sent_count = 0;
int mtu, maxdlen; int mtu, maxdlen;
uint64_t incdata = 0; uint64_t incdata = 0;
int incdlc = 0; int incdlc = 0;
@ -176,7 +181,7 @@ int main(int argc, char **argv)
signal(SIGHUP, sigterm); signal(SIGHUP, sigterm);
signal(SIGINT, sigterm); signal(SIGINT, sigterm);
while ((opt = getopt(argc, argv, "ig:ebfmI:L:D:xp:n:vRh?")) != -1) { while ((opt = getopt(argc, argv, "ig:ebfmI:L:D:xp:n:c:vRh?")) != -1) {
switch (opt) { switch (opt) {
case 'i': case 'i':
@ -241,6 +246,10 @@ int main(int argc, char **argv)
} }
break; break;
case 'c':
burst_count = strtoul(optarg, NULL, 10);
break;
case 'v': case 'v':
verbose++; verbose++;
break; break;
@ -432,7 +441,6 @@ int main(int argc, char **argv)
else else
fprint_canframe(stdout, &frame, "\n", 1, maxdlen); fprint_canframe(stdout, &frame, "\n", 1, maxdlen);
} }
resend: resend:
nbytes = write(s, &frame, mtu); nbytes = write(s, &frame, mtu);
if (nbytes < 0) { if (nbytes < 0) {
@ -458,10 +466,11 @@ resend:
fprintf(stderr, "write: incomplete CAN frame\n"); fprintf(stderr, "write: incomplete CAN frame\n");
return 1; return 1;
} }
burst_sent_count++;
if (gap && burst_sent_count >= burst_count) /* gap == 0 => performance test :-] */
if (nanosleep(&ts, NULL)) return 1;
if (gap) /* gap == 0 => performance test :-] */ if(burst_sent_count >= burst_count) burst_sent_count = 0;
if (nanosleep(&ts, NULL))
return 1;
if (id_mode == MODE_INCREMENT) if (id_mode == MODE_INCREMENT)
frame.can_id++; frame.can_id++;