]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotpperf.c
isotpperf: display bitrate setting status in summary
[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 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                                 /* get CAN FD bitrate setting information */
278                                 brs = frame.flags & CANFD_BRS;
279
280                                 ioctl(s, SIOCGSTAMP, &start_tv);
281
282                                 /* determine CAN frame mode for this PDU */
283                                 if (nbytes == CAN_MTU)
284                                         canfd_on = 0;
285                                 else
286                                         canfd_on = 1;
287
288                                 break;
289
290                         case 0x10:
291                                 /* FF */
292                                 fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
293                                 if (fflen)
294                                         datidx = ext+2;
295                                 else {
296                                         fflen = (frame.data[ext+2]<<24) +
297                                                 (frame.data[ext+3]<<16) +
298                                                 (frame.data[ext+4]<<8) +
299                                                 frame.data[ext+5];
300                                         datidx = ext+6;
301                                 }
302
303                                 /* to increase the time resolution we multiply fflen with 1000 later */
304                                 if (fflen >= (UINT32_MAX / 1000)) {
305                                         printf("fflen %lu is more than ~4.2 MB - ignoring PDU\n", fflen);
306                                         fflush(stdout);
307                                         fflen = rcvlen = 0;
308                                         continue;
309                                 }
310                                 rcvlen = frame.len - datidx;
311                                 last_sn = 0;
312
313                                 /* get number of digits for printing */
314                                 fflen_digits = getdigits(fflen);
315
316                                 /* get CAN FD bitrate setting information */
317                                 brs = frame.flags & CANFD_BRS;
318
319                                 ioctl(s, SIOCGSTAMP, &start_tv);
320
321                                 /* determine CAN frame mode for this PDU */
322                                 if (nbytes == CAN_MTU)
323                                         canfd_on = 0;
324                                 else
325                                         canfd_on = 1;
326
327                                 break;
328
329                         case 0x20:
330                                 /* CF */
331                                 if (rcvlen) {
332                                         sn = n_pci & 0x0F;
333                                         if (sn == ((last_sn + 1) & 0xF)) {
334                                                 last_sn = sn;
335                                                 datidx = ext+1;
336                                                 rcvlen += frame.len - datidx;
337                                         }
338                                 }
339                                 break;
340
341                         default:
342                                 break;
343                         }
344
345                         /* PDU reception in process */
346                         if (rcvlen) {
347                                 if (rcvlen > fflen)
348                                         rcvlen = fflen;
349                                 
350                                 percent = (rcvlen * 100 / fflen);
351                                 printf("\r %3lu%% ", percent);
352
353                                 printf("|");
354
355                                 if (percent > 100)
356                                         percent = 100;
357
358                                 for (i=0; i < NUMBAR; i++){
359                                         if (i < percent/PERCENTRES)
360                                                 printf("X");
361                                         else
362                                                 printf(".");
363                                 }
364                                 printf("| %*lu/%lu ", fflen_digits, rcvlen, fflen);
365                         }
366
367                         /* PDU complete */
368                         if (rcvlen && rcvlen >= fflen) {
369
370                                 printf("\r%s%c (BS:%2hhu # ", canfd_on?"CAN-FD":"CAN2.0", brs?'*':' ', bs);
371                                 if (stmin < 0x80)
372                                         printf("STmin:%3hhu msec)", stmin);
373                                 else if (stmin > 0xF0 && stmin < 0xFA)
374                                         printf("STmin:%3u usec)", (stmin & 0xF) * 100);
375                                 else
376                                         printf("STmin: invalid   )");
377
378                                 printf(" : %lu byte in ", fflen);
379
380                                 /* calculate time */
381                                 ioctl(s, SIOCGSTAMP, &end_tv);
382                                 diff_tv.tv_sec  = end_tv.tv_sec  - start_tv.tv_sec;
383                                 diff_tv.tv_usec = end_tv.tv_usec - start_tv.tv_usec;
384                                 if (diff_tv.tv_usec < 0)
385                                         diff_tv.tv_sec--, diff_tv.tv_usec += 1000000;
386                                 if (diff_tv.tv_sec < 0)
387                                         diff_tv.tv_sec = diff_tv.tv_usec = 0;
388
389                                 /* check devisor to be not zero */
390                                 if (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000){
391                                         printf("%ld.%06lds ", diff_tv.tv_sec, diff_tv.tv_usec);
392                                         printf("=> %lu byte/s", (fflen * 1000) /
393                                                (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000));
394                                 } else
395                                         printf("(no time available)     ");
396
397                                 printf("\n");
398                                 /* wait for next PDU */
399                                 fflen = rcvlen = 0;
400                         }
401                         fflush(stdout);
402                 }
403         }
404
405         close(s);
406
407         return ret;
408 }