]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotpperf.c
candump: Enable HW timestamping before using it
[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, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
72         fprintf(stderr, "\n");
73 }
74
75 /* substitute math.h function log10(value)+1 */
76 unsigned int getdigits(unsigned int value)
77 {
78         int  digits = 1;
79
80         while (value > 9) {
81                 digits++;
82                 value /= 10;
83         }
84         return digits;
85 }
86
87 int main(int argc, char **argv)
88 {
89         fd_set rdfs;
90         int s;
91         int running = 1;
92         struct sockaddr_can addr;
93         struct can_filter rfilter[2];
94         struct canfd_frame frame;
95         int canfd_on = 1;
96         int nbytes, i, ret;
97         canid_t src = NO_CAN_ID;
98         canid_t dst = NO_CAN_ID;
99         int ext = 0;
100         int extaddr = 0;
101         int rx_ext = 0;
102         int rx_extaddr = 0;
103         int datidx = 0;
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;
113         unsigned int n_pci;
114         unsigned int sn, last_sn = 0;
115         int opt;
116
117         while ((opt = getopt(argc, argv, "s:d:x:X:?")) != -1) {
118                 switch (opt) {
119                 case 's':
120                         src = strtoul(optarg, (char **)NULL, 16);
121                         if (strlen(optarg) > 7)
122                                 src |= CAN_EFF_FLAG;
123                         break;
124
125                 case 'd':
126                         dst = strtoul(optarg, (char **)NULL, 16);
127                         if (strlen(optarg) > 7)
128                                 dst |= CAN_EFF_FLAG;
129                         break;
130
131                 case 'x':
132                         ext = 1;
133                         extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
134                         break;
135
136                 case 'X':
137                         rx_ext = 1;
138                         rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
139                         break;
140
141                 case '?':
142                         print_usage(basename(argv[0]));
143                         exit(0);
144                         break;
145
146                 default:
147                         fprintf(stderr, "Unknown option %c\n", opt);
148                         print_usage(basename(argv[0]));
149                         exit(1);
150                         break;
151                 }
152         }
153
154         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
155                 print_usage(basename(argv[0]));
156                 exit(0);
157         }
158
159         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
160                 perror("socket");
161                 return 1;
162         }
163
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));
166
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);
171         } else {
172                 rfilter[0].can_id   = src & CAN_SFF_MASK;
173                 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
174         }
175
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);
179         } else {
180                 rfilter[1].can_id   = dst & CAN_SFF_MASK;
181                 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
182         }
183
184         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
185
186         addr.can_family = AF_CAN;
187         addr.can_ifindex = if_nametoindex(argv[optind]);
188
189         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
190                 perror("bind");
191                 return 1;
192         }
193
194         while (running) {
195
196                 FD_ZERO(&rdfs);
197                 FD_SET(s, &rdfs);
198
199                 /* timeout for ISO TP transmissions */
200                 timeo.tv_sec  = 1;
201                 timeo.tv_usec = 0;
202
203                 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
204                         running = 0;
205                         continue;
206                 }
207
208                 /* detected timeout of already started transmission */
209                 if (rcvlen && !(FD_ISSET(s, &rdfs))) {
210                         printf("\r%-*s",78, " (transmission timed out)");
211                         fflush(stdout);
212                         fflen = rcvlen = 0;
213                         continue;
214                 }
215
216                 nbytes = read(s, &frame, sizeof(frame));
217                 if (nbytes < 0) {
218                         perror("read");
219                         ret = nbytes;
220                         running = 0;
221                         continue;
222                 } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
223                         fprintf(stderr, "read: incomplete CAN frame %zu %d\n", sizeof(frame), nbytes);
224                         ret = nbytes;
225                         running = 0;
226                         continue;
227                 } else {
228                         if (rcvlen) {
229                                 /* make sure to process only the detected PDU CAN frame type */
230                                 if (canfd_on && (nbytes != CANFD_MTU))
231                                         continue;
232                                 if (!canfd_on && (nbytes != CAN_MTU))
233                                         continue;
234                         }
235
236                         /* check extended address if provided */
237                         if (ext && extaddr != frame.data[0])
238                                 continue;
239
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)
244                                         continue;
245
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];
251                                 } else
252                                         continue;
253                         }
254                         
255                         /* data content starts and index datidx */
256                         datidx = 0;
257
258                         n_pci = frame.data[ext];
259                         switch (n_pci & 0xF0) {
260
261                         case 0x00:
262                                 /* SF */
263                                 if (n_pci & 0xF) {
264                                         fflen = rcvlen = n_pci & 0xF;
265                                         datidx = ext+1;
266                                 } else {
267                                         fflen = rcvlen = frame.data[ext + 1];
268                                         datidx = ext+2;
269                                 }
270
271                                 /* ignore incorrect SF PDUs */
272                                 if (frame.len < rcvlen + datidx)
273                                         fflen = rcvlen = 0;
274
275                                 /* get number of digits for printing */
276                                 fflen_digits = getdigits(fflen);
277
278                                 /* get CAN FD bitrate & LL_DL setting information */
279                                 brs = frame.flags & CANFD_BRS;
280                                 ll_dl = frame.len;
281                                 if (ll_dl < 8)
282                                         ll_dl = 8;
283
284                                 ioctl(s, SIOCGSTAMP, &start_tv);
285
286                                 /* determine CAN frame mode for this PDU */
287                                 if (nbytes == CAN_MTU)
288                                         canfd_on = 0;
289                                 else
290                                         canfd_on = 1;
291
292                                 break;
293
294                         case 0x10:
295                                 /* FF */
296                                 fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
297                                 if (fflen)
298                                         datidx = ext+2;
299                                 else {
300                                         fflen = (frame.data[ext+2]<<24) +
301                                                 (frame.data[ext+3]<<16) +
302                                                 (frame.data[ext+4]<<8) +
303                                                 frame.data[ext+5];
304                                         datidx = ext+6;
305                                 }
306
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);
310                                         fflush(stdout);
311                                         fflen = rcvlen = 0;
312                                         continue;
313                                 }
314                                 rcvlen = frame.len - datidx;
315                                 last_sn = 0;
316
317                                 /* get number of digits for printing */
318                                 fflen_digits = getdigits(fflen);
319
320                                 /* get CAN FD bitrate & LL_DL setting information */
321                                 brs = frame.flags & CANFD_BRS;
322                                 ll_dl = frame.len;
323
324                                 ioctl(s, SIOCGSTAMP, &start_tv);
325
326                                 /* determine CAN frame mode for this PDU */
327                                 if (nbytes == CAN_MTU)
328                                         canfd_on = 0;
329                                 else
330                                         canfd_on = 1;
331
332                                 break;
333
334                         case 0x20:
335                                 /* CF */
336                                 if (rcvlen) {
337                                         sn = n_pci & 0x0F;
338                                         if (sn == ((last_sn + 1) & 0xF)) {
339                                                 last_sn = sn;
340                                                 datidx = ext+1;
341                                                 rcvlen += frame.len - datidx;
342                                         }
343                                 }
344                                 break;
345
346                         default:
347                                 break;
348                         }
349
350                         /* PDU reception in process */
351                         if (rcvlen) {
352                                 if (rcvlen > fflen)
353                                         rcvlen = fflen;
354                                 
355                                 percent = (rcvlen * 100 / fflen);
356                                 printf("\r %3lu%% ", percent);
357
358                                 printf("|");
359
360                                 if (percent > 100)
361                                         percent = 100;
362
363                                 for (i=0; i < NUMBAR; i++){
364                                         if (i < percent/PERCENTRES)
365                                                 printf("X");
366                                         else
367                                                 printf(".");
368                                 }
369                                 printf("| %*lu/%lu ", fflen_digits, rcvlen, fflen);
370                         }
371
372                         /* PDU complete */
373                         if (rcvlen && rcvlen >= fflen) {
374
375                                 printf("\r%s %02d%c (BS:%2hhu # ", canfd_on?"CAN-FD":"CAN2.0", ll_dl, brs?'*':' ', bs);
376                                 if (stmin < 0x80)
377                                         printf("STmin:%3hhu msec)", stmin);
378                                 else if (stmin > 0xF0 && stmin < 0xFA)
379                                         printf("STmin:%3u usec)", (stmin & 0xF) * 100);
380                                 else
381                                         printf("STmin: invalid   )");
382
383                                 printf(" : %lu byte in ", fflen);
384
385                                 /* calculate time */
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;
393
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));
399                                 } else
400                                         printf("(no time available)     ");
401
402                                 printf("\n");
403                                 /* wait for next PDU */
404                                 fflen = rcvlen = 0;
405                         }
406                         fflush(stdout);
407                 }
408         }
409
410         close(s);
411
412         return ret;
413 }