]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotpperf.c
e00bb9c0eb627166a245125da9b12e246a1a0bb0
[can-utils.git] / isotpperf.c
1 /*
2  * isotpperf.c - ISO15765-2 protocol performance visualisation
3  *
4  * Copyright (c) 2014 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
23  *
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.
26  *
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
38  * DAMAGE.
39  *
40  * Send feedback to <linux-can@vger.kernel.org>
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stdint.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <libgen.h>
50 #include <time.h>
51
52 #include <net/if.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56
57 #include <linux/can.h>
58 #include <linux/can/raw.h>
59
60 #define NO_CAN_ID 0xFFFFFFFFU
61 #define PERCENTRES 2 /* resolution in percent for bargraph */
62 #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
63
64 void print_usage(char *prg)
65 {
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");
74 }
75
76 /* substitute math.h function log10(value)+1 */
77 unsigned int getdigits(unsigned int value)
78 {
79         int  digits = 1;
80
81         while (value > 9) {
82                 digits++;
83                 value /= 10;
84         }
85         return digits;
86 }
87
88 int main(int argc, char **argv)
89 {
90         fd_set rdfs;
91         int s;
92         int running = 1;
93         struct sockaddr_can addr;
94         struct can_filter rfilter[2];
95         struct canfd_frame frame;
96         int canfd_on = 1;
97         int nbytes, i, ret;
98         canid_t src = NO_CAN_ID;
99         canid_t dst = NO_CAN_ID;
100         int ext = 0;
101         int extaddr = 0;
102         int rx_ext = 0;
103         int rx_extaddr = 0;
104         int datidx = 0;
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;
112         unsigned int n_pci;
113         unsigned int sn, last_sn = 0;
114         int opt;
115
116         while ((opt = getopt(argc, argv, "s:d:x:X:?")) != -1) {
117                 switch (opt) {
118                 case 's':
119                         src = strtoul(optarg, (char **)NULL, 16);
120                         if (strlen(optarg) > 7)
121                                 src |= CAN_EFF_FLAG;
122                         break;
123
124                 case 'd':
125                         dst = strtoul(optarg, (char **)NULL, 16);
126                         if (strlen(optarg) > 7)
127                                 dst |= CAN_EFF_FLAG;
128                         break;
129
130                 case 'x':
131                         ext = 1;
132                         extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
133                         break;
134
135                 case 'X':
136                         rx_ext = 1;
137                         rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
138                         break;
139
140                 case '?':
141                         print_usage(basename(argv[0]));
142                         exit(0);
143                         break;
144
145                 default:
146                         fprintf(stderr, "Unknown option %c\n", opt);
147                         print_usage(basename(argv[0]));
148                         exit(1);
149                         break;
150                 }
151         }
152
153         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
154                 print_usage(basename(argv[0]));
155                 exit(0);
156         }
157
158         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
159                 perror("socket");
160                 return 1;
161         }
162
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));
165
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);
170         } else {
171                 rfilter[0].can_id   = src & CAN_SFF_MASK;
172                 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
173         }
174
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);
178         } else {
179                 rfilter[1].can_id   = dst & CAN_SFF_MASK;
180                 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
181         }
182
183         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
184
185         addr.can_family = AF_CAN;
186         addr.can_ifindex = if_nametoindex(argv[optind]);
187
188         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
189                 perror("bind");
190                 return 1;
191         }
192
193         while (running) {
194
195                 FD_ZERO(&rdfs);
196                 FD_SET(s, &rdfs);
197
198                 /* timeout for ISO TP transmissions */
199                 timeo.tv_sec  = 1;
200                 timeo.tv_usec = 0;
201
202                 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
203                         running = 0;
204                         continue;
205                 }
206
207                 /* detected timeout of already started transmission */
208                 if (rcvlen && !(FD_ISSET(s, &rdfs))) {
209                         printf("\r%-*s",78, " (transmission timed out)");
210                         fflush(stdout);
211                         fflen = rcvlen = 0;
212                         continue;
213                 }
214
215                 nbytes = read(s, &frame, sizeof(frame));
216                 if (nbytes < 0) {
217                         perror("read");
218                         ret = nbytes;
219                         running = 0;
220                         continue;
221                 } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
222                         fprintf(stderr, "read: incomplete CAN frame %lu %d\n", sizeof(frame), nbytes);
223                         ret = nbytes;
224                         running = 0;
225                         continue;
226                 } else {
227                         if (rcvlen) {
228                                 /* make sure to process only the detected PDU CAN frame type */
229                                 if (canfd_on && (nbytes != CANFD_MTU))
230                                         continue;
231                                 if (!canfd_on && (nbytes != CAN_MTU))
232                                         continue;
233                         }
234
235                         /* check extended address if provided */
236                         if (ext && extaddr != frame.data[0])
237                                 continue;
238
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)
243                                         continue;
244
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];
250                                 } else
251                                         continue;
252                         }
253                         
254                         /* data content starts and index datidx */
255                         datidx = 0;
256
257                         n_pci = frame.data[ext];
258                         switch (n_pci & 0xF0) {
259
260                         case 0x00:
261                                 /* SF */
262                                 if (n_pci & 0xF) {
263                                         fflen = rcvlen = n_pci & 0xF;
264                                         datidx = ext+1;
265                                 } else {
266                                         fflen = rcvlen = frame.data[ext + 1];
267                                         datidx = ext+2;
268                                 }
269
270                                 /* ignore incorrect SF PDUs */
271                                 if (frame.len < rcvlen + datidx)
272                                         fflen = rcvlen = 0;
273
274                                 /* get number of digits for printing */
275                                 fflen_digits = getdigits(fflen);
276
277                                 ioctl(s, SIOCGSTAMP, &start_tv);
278
279                                 /* determine CAN frame mode for this PDU */
280                                 if (nbytes == CAN_MTU)
281                                         canfd_on = 0;
282                                 else
283                                         canfd_on = 1;
284
285                                 break;
286
287                         case 0x10:
288                                 /* FF */
289                                 fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
290                                 if (fflen)
291                                         datidx = ext+2;
292                                 else {
293                                         fflen = (frame.data[ext+2]<<24) +
294                                                 (frame.data[ext+3]<<16) +
295                                                 (frame.data[ext+4]<<8) +
296                                                 frame.data[ext+5];
297                                         datidx = ext+6;
298                                 }
299
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);
303                                         fflush(stdout);
304                                         fflen = rcvlen = 0;
305                                         continue;
306                                 }
307                                 rcvlen = frame.len - datidx;
308                                 last_sn = 0;
309
310                                 /* get number of digits for printing */
311                                 fflen_digits = getdigits(fflen);
312
313                                 ioctl(s, SIOCGSTAMP, &start_tv);
314
315                                 /* determine CAN frame mode for this PDU */
316                                 if (nbytes == CAN_MTU)
317                                         canfd_on = 0;
318                                 else
319                                         canfd_on = 1;
320
321                                 break;
322
323                         case 0x20:
324                                 /* CF */
325                                 if (rcvlen) {
326                                         sn = n_pci & 0x0F;
327                                         if (sn == ((last_sn + 1) & 0xF)) {
328                                                 last_sn = sn;
329                                                 datidx = ext+1;
330                                                 rcvlen += frame.len - datidx;
331                                         }
332                                 }
333                                 break;
334
335                         default:
336                                 break;
337                         }
338
339                         /* PDU reception in process */
340                         if (rcvlen) {
341                                 if (rcvlen > fflen)
342                                         rcvlen = fflen;
343                                 
344                                 percent = (rcvlen * 100 / fflen);
345                                 printf("\r %3lu%% ", percent);
346
347                                 printf("|");
348
349                                 if (percent > 100)
350                                         percent = 100;
351
352                                 for (i=0; i < NUMBAR; i++){
353                                         if (i < percent/PERCENTRES)
354                                                 printf("X");
355                                         else
356                                                 printf(".");
357                                 }
358                                 printf("| %*lu/%lu ", fflen_digits, rcvlen, fflen);
359                         }
360
361                         /* PDU complete */
362                         if (rcvlen && rcvlen >= fflen) {
363
364                                 printf("\r%s (BS:%2hhu # ", canfd_on?"CAN-FD":"CAN2.0", bs);
365                                 if (stmin < 0x80)
366                                         printf("STmin:%3hhu msec)", stmin);
367                                 else if (stmin > 0xF0 && stmin < 0xFA)
368                                         printf("STmin:3%u usec)", (stmin & 0xF) * 100);
369                                 else
370                                         printf("STmin: invalid   )");
371
372                                 printf(" : %lu byte in ", fflen);
373
374                                 /* calculate time */
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;
382
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));
388                                 } else
389                                         printf("(no time available)     ");
390
391                                 printf("\n");
392                                 /* wait for next PDU */
393                                 fflen = rcvlen = 0;
394                         }
395                         fflush(stdout);
396                 }
397         }
398
399         close(s);
400
401         return ret;
402 }