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, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
72 fprintf(stderr, "\n");
75 /* substitute math.h function log10(value)+1 */
76 unsigned int getdigits(unsigned int value)
87 int main(int argc, char **argv)
92 struct sockaddr_can addr;
93 struct can_filter rfilter[2];
94 struct canfd_frame frame;
97 canid_t src = NO_CAN_ID;
98 canid_t dst = NO_CAN_ID;
104 unsigned char bs = 0;
105 unsigned char stmin = 0;
106 unsigned char brs = 0;
107 unsigned char ll_dl = 0;
108 unsigned long fflen = 0;
109 unsigned fflen_digits = 0;
110 unsigned long rcvlen = 0;
111 unsigned long percent = 0;
112 struct timeval start_tv, end_tv, diff_tv, timeo;
114 unsigned int sn, last_sn = 0;
117 while ((opt = getopt(argc, argv, "s:d:x:X:?")) != -1) {
120 src = strtoul(optarg, (char **)NULL, 16);
121 if (strlen(optarg) > 7)
126 dst = strtoul(optarg, (char **)NULL, 16);
127 if (strlen(optarg) > 7)
133 extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
138 rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
142 print_usage(basename(argv[0]));
147 fprintf(stderr, "Unknown option %c\n", opt);
148 print_usage(basename(argv[0]));
154 if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
155 print_usage(basename(argv[0]));
159 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
164 /* try to switch the socket into CAN FD mode */
165 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
167 /* set single CAN ID raw filters for src and dst frames */
168 if (src & CAN_EFF_FLAG) {
169 rfilter[0].can_id = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
170 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
172 rfilter[0].can_id = src & CAN_SFF_MASK;
173 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
176 if (dst & CAN_EFF_FLAG) {
177 rfilter[1].can_id = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
178 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
180 rfilter[1].can_id = dst & CAN_SFF_MASK;
181 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
184 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
186 addr.can_family = AF_CAN;
187 addr.can_ifindex = if_nametoindex(argv[optind]);
189 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
199 /* timeout for ISO TP transmissions */
203 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
208 /* detected timeout of already started transmission */
209 if (rcvlen && !(FD_ISSET(s, &rdfs))) {
210 printf("\r%-*s",78, " (transmission timed out)");
216 nbytes = read(s, &frame, sizeof(frame));
222 } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
223 fprintf(stderr, "read: incomplete CAN frame %zu %d\n", sizeof(frame), nbytes);
229 /* make sure to process only the detected PDU CAN frame type */
230 if (canfd_on && (nbytes != CANFD_MTU))
232 if (!canfd_on && (nbytes != CAN_MTU))
236 /* check extended address if provided */
237 if (ext && extaddr != frame.data[0])
240 /* only get flow control information from dst CAN ID */
241 if (frame.can_id == dst) {
242 /* check extended address if provided */
243 if (rx_ext && frame.data[0] != rx_extaddr)
246 n_pci = frame.data[rx_ext];
247 /* check flow control PCI only */
248 if ((n_pci & 0xF0) == 0x30) {
249 bs = frame.data[rx_ext+1];
250 stmin = frame.data[rx_ext+2];
255 /* data content starts and index datidx */
258 n_pci = frame.data[ext];
259 switch (n_pci & 0xF0) {
264 fflen = rcvlen = n_pci & 0xF;
267 fflen = rcvlen = frame.data[ext + 1];
271 /* ignore incorrect SF PDUs */
272 if (frame.len < rcvlen + datidx)
275 /* get number of digits for printing */
276 fflen_digits = getdigits(fflen);
278 /* get CAN FD bitrate & LL_DL setting information */
279 brs = frame.flags & CANFD_BRS;
284 ioctl(s, SIOCGSTAMP, &start_tv);
286 /* determine CAN frame mode for this PDU */
287 if (nbytes == CAN_MTU)
296 fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
300 fflen = (frame.data[ext+2]<<24) +
301 (frame.data[ext+3]<<16) +
302 (frame.data[ext+4]<<8) +
307 /* to increase the time resolution we multiply fflen with 1000 later */
308 if (fflen >= (UINT32_MAX / 1000)) {
309 printf("fflen %lu is more than ~4.2 MB - ignoring PDU\n", fflen);
314 rcvlen = frame.len - datidx;
317 /* get number of digits for printing */
318 fflen_digits = getdigits(fflen);
320 /* get CAN FD bitrate & LL_DL setting information */
321 brs = frame.flags & CANFD_BRS;
324 ioctl(s, SIOCGSTAMP, &start_tv);
326 /* determine CAN frame mode for this PDU */
327 if (nbytes == CAN_MTU)
338 if (sn == ((last_sn + 1) & 0xF)) {
341 rcvlen += frame.len - datidx;
350 /* PDU reception in process */
355 percent = (rcvlen * 100 / fflen);
356 printf("\r %3lu%% ", percent);
363 for (i=0; i < NUMBAR; i++){
364 if (i < percent/PERCENTRES)
369 printf("| %*lu/%lu ", fflen_digits, rcvlen, fflen);
373 if (rcvlen && rcvlen >= fflen) {
375 printf("\r%s %02d%c (BS:%2hhu # ", canfd_on?"CAN-FD":"CAN2.0", ll_dl, brs?'*':' ', bs);
377 printf("STmin:%3hhu msec)", stmin);
378 else if (stmin > 0xF0 && stmin < 0xFA)
379 printf("STmin:%3u usec)", (stmin & 0xF) * 100);
381 printf("STmin: invalid )");
383 printf(" : %lu byte in ", fflen);
386 ioctl(s, SIOCGSTAMP, &end_tv);
387 diff_tv.tv_sec = end_tv.tv_sec - start_tv.tv_sec;
388 diff_tv.tv_usec = end_tv.tv_usec - start_tv.tv_usec;
389 if (diff_tv.tv_usec < 0)
390 diff_tv.tv_sec--, diff_tv.tv_usec += 1000000;
391 if (diff_tv.tv_sec < 0)
392 diff_tv.tv_sec = diff_tv.tv_usec = 0;
394 /* check devisor to be not zero */
395 if (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000){
396 printf("%ld.%06lds ", diff_tv.tv_sec, diff_tv.tv_usec);
397 printf("=> %lu byte/s", (fflen * 1000) /
398 (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000));
400 printf("(no time available) ");
403 /* wait for next PDU */