Skip to content

Commit 5b8145f

Browse files
Merge pull request #99 from olerem/canfdtest-2018.09.18
Extend canfdtest to test proper RX and TX-ECHO order
2 parents 3e4d589 + ba603c5 commit 5b8145f

1 file changed

Lines changed: 54 additions & 25 deletions

File tree

canfdtest.c

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static int running = 1;
4747
static int verbose;
4848
static int sockfd;
4949
static int test_loops;
50+
static int exit_sig;
5051

5152
static void print_usage(char *prg)
5253
{
@@ -71,47 +72,47 @@ static void print_usage(char *prg)
7172
exit(1);
7273
}
7374

74-
static void print_frame(struct can_frame *frame)
75+
static void print_frame(struct can_frame *frame, int inc)
7576
{
7677
int i;
7778

78-
printf("%04x: ", frame->can_id);
79+
printf("%04x: ", frame->can_id + inc);
7980
if (frame->can_id & CAN_RTR_FLAG) {
8081
printf("remote request");
8182
} else {
8283
printf("[%d]", frame->can_dlc);
8384
for (i = 0; i < frame->can_dlc; i++)
84-
printf(" %02x", frame->data[i]);
85+
printf(" %02x", frame->data[i] + inc);
8586
}
8687
printf("\n");
8788
}
8889

89-
static void print_compare(struct can_frame *exp, struct can_frame *rec)
90+
static void print_compare(struct can_frame *exp, struct can_frame *rec, int inc)
9091
{
9192
printf("expected: ");
92-
print_frame(exp);
93+
print_frame(exp, inc);
9394
printf("received: ");
94-
print_frame(rec);
95+
print_frame(rec, 0);
9596
}
9697

97-
static void compare_frame(struct can_frame *exp, struct can_frame *rec)
98+
static void compare_frame(struct can_frame *exp, struct can_frame *rec, int inc)
9899
{
99100
int i;
100101

101-
if (rec->can_id != exp->can_id) {
102+
if (rec->can_id != exp->can_id + inc) {
102103
printf("Message ID mismatch!\n");
103-
print_compare(exp, rec);
104+
print_compare(exp, rec, inc);
104105
running = 0;
105106
} else if (rec->can_dlc != exp->can_dlc) {
106107
printf("Message length mismatch!\n");
107-
print_compare(exp, rec);
108+
print_compare(exp, rec, inc);
108109
running = 0;
109110
} else {
110111
for (i = 0; i < rec->can_dlc; i++) {
111-
if (rec->data[i] != exp->data[i]) {
112+
if (rec->data[i] != ((exp->data[i] + inc) & 0xff)) {
112113
printf("Databyte %x mismatch !\n", i);
113114
print_compare(exp,
114-
rec);
115+
rec, inc);
115116
running = 0;
116117
}
117118
}
@@ -149,6 +150,7 @@ static void signal_handler(int signo)
149150
{
150151
close(sockfd);
151152
running = 0;
153+
exit_sig = signo;
152154
}
153155

154156
static int recv_frame(struct can_frame *frame)
@@ -235,26 +237,24 @@ static int can_echo_dut(void)
235237
static int can_echo_gen(void)
236238
{
237239
struct can_frame tx_frames[CAN_MSG_COUNT];
240+
int recv_tx[CAN_MSG_COUNT];
238241
struct can_frame rx_frame;
239242
unsigned char counter = 0;
240-
int send_pos = 0, recv_pos = 0, unprocessed = 0, loops = 0;
243+
int send_pos = 0, recv_rx_pos = 0, recv_tx_pos = 0, unprocessed = 0, loops = 0;
241244
int i;
242245

243246
while (running) {
244247
if (unprocessed < CAN_MSG_COUNT) {
245248
/* still send messages */
246249
tx_frames[send_pos].can_dlc = CAN_MSG_LEN;
247250
tx_frames[send_pos].can_id = CAN_MSG_ID;
251+
recv_tx[send_pos] = 0;
252+
248253
for (i = 0; i < CAN_MSG_LEN; i++)
249254
tx_frames[send_pos].data[i] = counter + i;
250255
if (send_frame(&tx_frames[send_pos]))
251256
return -1;
252257

253-
/* increment to be equal to expected */
254-
tx_frames[send_pos].can_id++;
255-
for (i = 0; i < CAN_MSG_LEN; i++)
256-
tx_frames[send_pos].data[i]++;
257-
258258
send_pos++;
259259
if (send_pos == CAN_MSG_COUNT)
260260
send_pos = 0;
@@ -272,18 +272,32 @@ static int can_echo_gen(void)
272272
return -1;
273273

274274
if (verbose > 1)
275-
print_frame(&rx_frame);
276-
277-
/* compare with expected */
278-
compare_frame(&tx_frames[recv_pos], &rx_frame);
275+
print_frame(&rx_frame, 0);
276+
277+
/* own frame */
278+
if (rx_frame.can_id == CAN_MSG_ID) {
279+
compare_frame(&tx_frames[recv_tx_pos], &rx_frame, 0);
280+
recv_tx[recv_tx_pos] = 1;
281+
recv_tx_pos++;
282+
if (recv_tx_pos == CAN_MSG_COUNT)
283+
recv_tx_pos = 0;
284+
continue;
285+
} else {
286+
if (!recv_tx[recv_rx_pos]) {
287+
printf("RX before TX!\n");
288+
running = 0;
289+
}
290+
/* compare with expected */
291+
compare_frame(&tx_frames[recv_rx_pos], &rx_frame, 1);
292+
recv_rx_pos++;
293+
if (recv_rx_pos == CAN_MSG_COUNT)
294+
recv_rx_pos = 0;
295+
}
279296

280297
loops++;
281298
if (test_loops && loops >= test_loops)
282299
break;
283300

284-
recv_pos++;
285-
if (recv_pos == CAN_MSG_COUNT)
286-
recv_pos = 0;
287301
unprocessed--;
288302
}
289303
}
@@ -300,6 +314,7 @@ int main(int argc, char *argv[])
300314
int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
301315
int echo_gen = 0;
302316
int opt, err;
317+
int recv_own_msgs = 1;
303318

304319
signal(SIGTERM, signal_handler);
305320
signal(SIGHUP, signal_handler);
@@ -337,6 +352,14 @@ int main(int argc, char *argv[])
337352
return 1;
338353
}
339354

355+
if (echo_gen) {
356+
if (setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
357+
&recv_own_msgs, sizeof(recv_own_msgs)) == -1) {
358+
perror("setsockopt");
359+
return 1;
360+
}
361+
}
362+
340363
addr.can_family = family;
341364
addr.can_ifindex = if_nametoindex(intf_name);
342365

@@ -355,5 +378,11 @@ int main(int argc, char *argv[])
355378
printf("Exiting...\n");
356379

357380
close(sockfd);
381+
382+
if (exit_sig) {
383+
signal(exit_sig, SIG_DFL);
384+
kill(getpid(), exit_sig);
385+
}
386+
358387
return err;
359388
}

0 commit comments

Comments
 (0)