isotpperf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <stdint.h>
  46. #include <unistd.h>
  47. #include <string.h>
  48. #include <libgen.h>
  49. #include <time.h>
  50. #include <net/if.h>
  51. #include <sys/types.h>
  52. #include <sys/socket.h>
  53. #include <sys/ioctl.h>
  54. #include <linux/can.h>
  55. #include <linux/can/raw.h>
  56. #define NO_CAN_ID 0xFFFFFFFFU
  57. #define PERCENTRES 2 /* resolution in percent for bargraph */
  58. #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
  59. void print_usage(char *prg)
  60. {
  61. fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
  62. fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
  63. fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
  64. fprintf(stderr, " -x <addr> (extended addressing mode)\n");
  65. fprintf(stderr, " -X <addr> (extended addressing mode (rx addr))\n");
  66. fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
  67. fprintf(stderr, "\n");
  68. }
  69. /* substitute math.h function log10(value)+1 */
  70. unsigned int getdigits(unsigned int value)
  71. {
  72. int digits = 1;
  73. while (value > 9) {
  74. digits++;
  75. value /= 10;
  76. }
  77. return digits;
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. fd_set rdfs;
  82. int s;
  83. int running = 1;
  84. struct sockaddr_can addr;
  85. struct can_filter rfilter[2];
  86. struct canfd_frame frame;
  87. int canfd_on = 1;
  88. int nbytes, i, ret;
  89. canid_t src = NO_CAN_ID;
  90. canid_t dst = NO_CAN_ID;
  91. int ext = 0;
  92. int extaddr = 0;
  93. int rx_ext = 0;
  94. int rx_extaddr = 0;
  95. int datidx = 0;
  96. unsigned char bs = 0;
  97. unsigned char stmin = 0;
  98. unsigned char brs = 0;
  99. unsigned char ll_dl = 0;
  100. unsigned long fflen = 0;
  101. unsigned fflen_digits = 0;
  102. unsigned long rcvlen = 0;
  103. unsigned long percent = 0;
  104. struct timeval start_tv, end_tv, diff_tv, timeo;
  105. unsigned int n_pci;
  106. unsigned int sn, last_sn = 0;
  107. int opt;
  108. while ((opt = getopt(argc, argv, "s:d:x:X:?")) != -1) {
  109. switch (opt) {
  110. case 's':
  111. src = strtoul(optarg, (char **)NULL, 16);
  112. if (strlen(optarg) > 7)
  113. src |= CAN_EFF_FLAG;
  114. break;
  115. case 'd':
  116. dst = strtoul(optarg, (char **)NULL, 16);
  117. if (strlen(optarg) > 7)
  118. dst |= CAN_EFF_FLAG;
  119. break;
  120. case 'x':
  121. ext = 1;
  122. extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  123. break;
  124. case 'X':
  125. rx_ext = 1;
  126. rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  127. break;
  128. case '?':
  129. print_usage(basename(argv[0]));
  130. exit(0);
  131. break;
  132. default:
  133. fprintf(stderr, "Unknown option %c\n", opt);
  134. print_usage(basename(argv[0]));
  135. exit(1);
  136. break;
  137. }
  138. }
  139. if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
  140. print_usage(basename(argv[0]));
  141. exit(0);
  142. }
  143. if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
  144. perror("socket");
  145. return 1;
  146. }
  147. /* try to switch the socket into CAN FD mode */
  148. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
  149. /* set single CAN ID raw filters for src and dst frames */
  150. if (src & CAN_EFF_FLAG) {
  151. rfilter[0].can_id = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
  152. rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  153. } else {
  154. rfilter[0].can_id = src & CAN_SFF_MASK;
  155. rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  156. }
  157. if (dst & CAN_EFF_FLAG) {
  158. rfilter[1].can_id = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
  159. rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  160. } else {
  161. rfilter[1].can_id = dst & CAN_SFF_MASK;
  162. rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  163. }
  164. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
  165. addr.can_family = AF_CAN;
  166. addr.can_ifindex = if_nametoindex(argv[optind]);
  167. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  168. perror("bind");
  169. return 1;
  170. }
  171. while (running) {
  172. FD_ZERO(&rdfs);
  173. FD_SET(s, &rdfs);
  174. /* timeout for ISO TP transmissions */
  175. timeo.tv_sec = 1;
  176. timeo.tv_usec = 0;
  177. if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
  178. running = 0;
  179. continue;
  180. }
  181. /* detected timeout of already started transmission */
  182. if (rcvlen && !(FD_ISSET(s, &rdfs))) {
  183. printf("\r%-*s",78, " (transmission timed out)");
  184. fflush(stdout);
  185. fflen = rcvlen = 0;
  186. continue;
  187. }
  188. nbytes = read(s, &frame, sizeof(frame));
  189. if (nbytes < 0) {
  190. perror("read");
  191. ret = nbytes;
  192. running = 0;
  193. continue;
  194. } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
  195. fprintf(stderr, "read: incomplete CAN frame %zu %d\n", sizeof(frame), nbytes);
  196. ret = nbytes;
  197. running = 0;
  198. continue;
  199. } else {
  200. if (rcvlen) {
  201. /* make sure to process only the detected PDU CAN frame type */
  202. if (canfd_on && (nbytes != CANFD_MTU))
  203. continue;
  204. if (!canfd_on && (nbytes != CAN_MTU))
  205. continue;
  206. }
  207. /* check extended address if provided */
  208. if (ext && extaddr != frame.data[0])
  209. continue;
  210. /* only get flow control information from dst CAN ID */
  211. if (frame.can_id == dst) {
  212. /* check extended address if provided */
  213. if (rx_ext && frame.data[0] != rx_extaddr)
  214. continue;
  215. n_pci = frame.data[rx_ext];
  216. /* check flow control PCI only */
  217. if ((n_pci & 0xF0) == 0x30) {
  218. bs = frame.data[rx_ext+1];
  219. stmin = frame.data[rx_ext+2];
  220. } else
  221. continue;
  222. }
  223. /* data content starts and index datidx */
  224. datidx = 0;
  225. n_pci = frame.data[ext];
  226. switch (n_pci & 0xF0) {
  227. case 0x00:
  228. /* SF */
  229. if (n_pci & 0xF) {
  230. fflen = rcvlen = n_pci & 0xF;
  231. datidx = ext+1;
  232. } else {
  233. fflen = rcvlen = frame.data[ext + 1];
  234. datidx = ext+2;
  235. }
  236. /* ignore incorrect SF PDUs */
  237. if (frame.len < rcvlen + datidx)
  238. fflen = rcvlen = 0;
  239. /* get number of digits for printing */
  240. fflen_digits = getdigits(fflen);
  241. /* get CAN FD bitrate & LL_DL setting information */
  242. brs = frame.flags & CANFD_BRS;
  243. ll_dl = frame.len;
  244. if (ll_dl < 8)
  245. ll_dl = 8;
  246. ioctl(s, SIOCGSTAMP, &start_tv);
  247. /* determine CAN frame mode for this PDU */
  248. if (nbytes == CAN_MTU)
  249. canfd_on = 0;
  250. else
  251. canfd_on = 1;
  252. break;
  253. case 0x10:
  254. /* FF */
  255. fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
  256. if (fflen)
  257. datidx = ext+2;
  258. else {
  259. fflen = (frame.data[ext+2]<<24) +
  260. (frame.data[ext+3]<<16) +
  261. (frame.data[ext+4]<<8) +
  262. frame.data[ext+5];
  263. datidx = ext+6;
  264. }
  265. /* to increase the time resolution we multiply fflen with 1000 later */
  266. if (fflen >= (UINT32_MAX / 1000)) {
  267. printf("fflen %lu is more than ~4.2 MB - ignoring PDU\n", fflen);
  268. fflush(stdout);
  269. fflen = rcvlen = 0;
  270. continue;
  271. }
  272. rcvlen = frame.len - datidx;
  273. last_sn = 0;
  274. /* get number of digits for printing */
  275. fflen_digits = getdigits(fflen);
  276. /* get CAN FD bitrate & LL_DL setting information */
  277. brs = frame.flags & CANFD_BRS;
  278. ll_dl = frame.len;
  279. ioctl(s, SIOCGSTAMP, &start_tv);
  280. /* determine CAN frame mode for this PDU */
  281. if (nbytes == CAN_MTU)
  282. canfd_on = 0;
  283. else
  284. canfd_on = 1;
  285. break;
  286. case 0x20:
  287. /* CF */
  288. if (rcvlen) {
  289. sn = n_pci & 0x0F;
  290. if (sn == ((last_sn + 1) & 0xF)) {
  291. last_sn = sn;
  292. datidx = ext+1;
  293. rcvlen += frame.len - datidx;
  294. }
  295. }
  296. break;
  297. default:
  298. break;
  299. }
  300. /* PDU reception in process */
  301. if (rcvlen) {
  302. if (rcvlen > fflen)
  303. rcvlen = fflen;
  304. percent = (rcvlen * 100 / fflen);
  305. printf("\r %3lu%% ", percent);
  306. printf("|");
  307. if (percent > 100)
  308. percent = 100;
  309. for (i=0; i < NUMBAR; i++){
  310. if (i < percent/PERCENTRES)
  311. printf("X");
  312. else
  313. printf(".");
  314. }
  315. printf("| %*lu/%lu ", fflen_digits, rcvlen, fflen);
  316. }
  317. /* PDU complete */
  318. if (rcvlen && rcvlen >= fflen) {
  319. printf("\r%s %02d%c (BS:%2hhu # ", canfd_on?"CAN-FD":"CAN2.0", ll_dl, brs?'*':' ', bs);
  320. if (stmin < 0x80)
  321. printf("STmin:%3hhu msec)", stmin);
  322. else if (stmin > 0xF0 && stmin < 0xFA)
  323. printf("STmin:%3u usec)", (stmin & 0xF) * 100);
  324. else
  325. printf("STmin: invalid )");
  326. printf(" : %lu byte in ", fflen);
  327. /* calculate time */
  328. ioctl(s, SIOCGSTAMP, &end_tv);
  329. diff_tv.tv_sec = end_tv.tv_sec - start_tv.tv_sec;
  330. diff_tv.tv_usec = end_tv.tv_usec - start_tv.tv_usec;
  331. if (diff_tv.tv_usec < 0)
  332. diff_tv.tv_sec--, diff_tv.tv_usec += 1000000;
  333. if (diff_tv.tv_sec < 0)
  334. diff_tv.tv_sec = diff_tv.tv_usec = 0;
  335. /* check devisor to be not zero */
  336. if (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000){
  337. printf("%ld.%06lds ", diff_tv.tv_sec, diff_tv.tv_usec);
  338. printf("=> %lu byte/s", (fflen * 1000) /
  339. (diff_tv.tv_sec * 1000 + diff_tv.tv_usec / 1000));
  340. } else
  341. printf("(no time available) ");
  342. printf("\n");
  343. /* wait for next PDU */
  344. fflen = rcvlen = 0;
  345. }
  346. fflush(stdout);
  347. }
  348. }
  349. close(s);
  350. return ret;
  351. }