2 * isotpperf.c - ISO15765-2 protocol performance visualisation
4 * Copyright (c) 2014 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
57 #include <linux/can.h>
58 #include <linux/can/raw.h>
60 #define NO_CAN_ID 0xFFFFFFFFU
61 #define PERCENTRES 2 /* resolution in percent for bargraph */
62 #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
64 void print_usage(char *prg)
66 fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
67 fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
68 fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
69 fprintf(stderr, " -x <addr> (extended addressing mode)\n");
70 fprintf(stderr, " -X <addr> (extended addressing mode (rx addr))\n");
71 fprintf(stderr, " -f (CAN FD mode - only process CAN FD frames)\n");
72 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
73 fprintf(stderr, "\n");
76 /* substitute math.h function log10(value)+1 */
77 unsigned int getdigits(unsigned int value)
88 int main(int argc, char **argv)
93 struct sockaddr_can addr;
94 struct can_filter rfilter[2];
95 struct canfd_frame frame;
98 canid_t src = NO_CAN_ID;
99 canid_t dst = NO_CAN_ID;
105 unsigned char bs = 0;
106 unsigned char stmin = 0;
107 unsigned long fflen = 0;
108 unsigned fflen_digits = 0;
109 unsigned long rcvlen = 0;
110 unsigned long percent = 0;
111 struct timeval start_tv, end_tv, diff_tv, timeo;
113 unsigned int sn, last_sn = 0;
116 while ((opt = getopt(argc, argv, "s:d:x:X:?")) != -1) {
119 src = strtoul(optarg, (char **)NULL, 16);
120 if (strlen(optarg) > 7)
125 dst = strtoul(optarg, (char **)NULL, 16);
126 if (strlen(optarg) > 7)
132 extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
137 rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
141 print_usage(basename(argv[0]));
146 fprintf(stderr, "Unknown option %c\n", opt);
147 print_usage(basename(argv[0]));
153 if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
154 print_usage(basename(argv[0]));
158 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
163 /* try to switch the socket into CAN FD mode */
164 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
166 /* set single CAN ID raw filters for src and dst frames */
167 if (src & CAN_EFF_FLAG) {
168 rfilter[0].can_id = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
169 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
171 rfilter[0].can_id = src & CAN_SFF_MASK;
172 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
175 if (dst & CAN_EFF_FLAG) {
176 rfilter[1].can_id = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
177 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
179 rfilter[1].can_id = dst & CAN_SFF_MASK;
180 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
183 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
185 addr.can_family = AF_CAN;
186 addr.can_ifindex = if_nametoindex(argv[optind]);
188 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
198 /* timeout for ISO TP transmissions */
202 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
207 /* detected timeout of already started transmission */
208 if (rcvlen && !(FD_ISSET(s, &rdfs))) {
209 printf("\r%-*s",78, " (transmission timed out)");
215 nbytes = read(s, &frame, sizeof(frame));
221 } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
222 fprintf(stderr, "read: incomplete CAN frame %lu %d\n", sizeof(frame), nbytes);
228 /* make sure to process only the detected PDU CAN frame type */
229 if (canfd_on && (nbytes != CANFD_MTU))
231 if (!canfd_on && (nbytes != CAN_MTU))
235 /* check extended address if provided */
236 if (ext && extaddr != frame.data[0])
239 /* only get flow control information from dst CAN ID */
240 if (frame.can_id == dst) {
241 /* check extended address if provided */
242 if (rx_ext && frame.data[0] != rx_extaddr)
245 n_pci = frame.data[rx_ext];
246 /* check flow control PCI only */
247 if ((n_pci & 0xF0) == 0x30) {
248 bs = frame.data[rx_ext+1];
249 stmin = frame.data[rx_ext+2];
254 /* data content starts and index datidx */
257 n_pci = frame.data[ext];
258 switch (n_pci & 0xF0) {
263 fflen = rcvlen = n_pci & 0xF;
266 fflen = rcvlen = frame.data[ext + 1];
270 /* ignore incorrect SF PDUs */
271 if (frame.len < rcvlen + datidx)
274 /* get number of digits for printing */
275 fflen_digits = getdigits(fflen);
277 ioctl(s, SIOCGSTAMP, &start_tv);
279 /* determine CAN frame mode for this PDU */
280 if (nbytes == CAN_MTU)
289 fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
293 fflen = (frame.data[ext+2]<<24) +
294 (frame.data[ext+3]<<16) +
295 (frame.data[ext+4]<<8) +
300 /* to increase the time resolution we multiply fflen with 1000 later */
301 if (fflen >= (UINT32_MAX / 1000)) {
302 printf("fflen %lu is more than ~4.2 MB - ignoring PDU\n", fflen);
307 rcvlen = frame.len - datidx;
310 /* get number of digits for printing */
311 fflen_digits = getdigits(fflen);
313 ioctl(s, SIOCGSTAMP, &start_tv);
315 /* determine CAN frame mode for this PDU */
316 if (nbytes == CAN_MTU)
327 if (sn == ((last_sn + 1) & 0xF)) {
330 rcvlen += frame.len - datidx;
339 /* PDU reception in process */
344 percent = (rcvlen * 100 / fflen);
345 printf("\r %3lu%% ", percent);
352 for (i=0; i < NUMBAR; i++){
353 if (i < percent/PERCENTRES)
358 printf("| %*lu/%lu ", fflen_digits, rcvlen, fflen);
362 if (rcvlen && rcvlen >= fflen) {
364 printf("\r%s (BS:%2hhu # ", canfd_on?"CAN-FD":"CAN2.0", bs);
366 printf("STmin:%3hhu msec)", stmin);
367 else if (stmin > 0xF0 && stmin < 0xFA)
368 printf("STmin:3%u usec)", (stmin & 0xF) * 100);
370 printf("STmin: invalid )");
372 printf(" : %lu byte in ", fflen);
375 ioctl(s, SIOCGSTAMP, &end_tv);
376 diff_tv.tv_sec = end_tv.tv_sec - start_tv.tv_sec;
377 diff_tv.tv_usec = end_tv.tv_usec - start_tv.tv_usec;
378 if (diff_tv.tv_usec < 0)
379 diff_tv.tv_sec--, diff_tv.tv_usec += 1000000;
380 if (diff_tv.tv_sec < 0)
381 diff_tv.tv_sec = diff_tv.tv_usec = 0;
383 /* check devisor to be not zero */
384 if (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000){
385 printf("%ld.%06lds ", diff_tv.tv_sec, diff_tv.tv_usec);
386 printf("=> %lu byte/s", (fflen * 1000) /
387 (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000));
389 printf("(no time available) ");
392 /* wait for next PDU */