isotpdump.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * isotpdump.c - dump and explain ISO15765-2 protocol CAN frames
  3. *
  4. * Copyright (c) 2008 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 <unistd.h>
  46. #include <string.h>
  47. #include <libgen.h>
  48. #include <time.h>
  49. #include <net/if.h>
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #include <sys/ioctl.h>
  53. #include <linux/can.h>
  54. #include <linux/can/raw.h>
  55. #include "terminal.h"
  56. #define NO_CAN_ID 0xFFFFFFFFU
  57. const char fc_info [4][9] = { "CTS", "WT", "OVFLW", "reserved" };
  58. const int canfd_on = 1;
  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. Use 'any' for all addresses)\n");
  65. fprintf(stderr, " -X <addr> (extended addressing mode (rx addr). Use 'any' for all)\n");
  66. fprintf(stderr, " -c (color mode)\n");
  67. fprintf(stderr, " -a (print data also in ASCII-chars)\n");
  68. fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
  69. fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
  70. fprintf(stderr, "\n");
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. int s;
  75. struct sockaddr_can addr;
  76. struct can_filter rfilter[2];
  77. struct canfd_frame frame;
  78. int nbytes, i;
  79. canid_t src = NO_CAN_ID;
  80. canid_t dst = NO_CAN_ID;
  81. int ext = 0;
  82. int extaddr = 0;
  83. int extany = 0;
  84. int rx_ext = 0;
  85. int rx_extaddr = 0;
  86. int rx_extany = 0;
  87. int asc = 0;
  88. int color = 0;
  89. int timestamp = 0;
  90. int datidx = 0;
  91. unsigned long fflen = 0;
  92. struct timeval tv, last_tv;
  93. unsigned int n_pci;
  94. int opt;
  95. last_tv.tv_sec = 0;
  96. last_tv.tv_usec = 0;
  97. while ((opt = getopt(argc, argv, "s:d:ax:X:ct:?")) != -1) {
  98. switch (opt) {
  99. case 's':
  100. src = strtoul(optarg, (char **)NULL, 16);
  101. if (strlen(optarg) > 7)
  102. src |= CAN_EFF_FLAG;
  103. break;
  104. case 'd':
  105. dst = strtoul(optarg, (char **)NULL, 16);
  106. if (strlen(optarg) > 7)
  107. dst |= CAN_EFF_FLAG;
  108. break;
  109. case 'c':
  110. color = 1;
  111. break;
  112. case 'a':
  113. asc = 1;
  114. break;
  115. case 'x':
  116. ext = 1;
  117. if (!strncmp(optarg, "any", 3))
  118. extany = 1;
  119. else
  120. extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  121. break;
  122. case 'X':
  123. rx_ext = 1;
  124. if (!strncmp(optarg, "any", 3))
  125. rx_extany = 1;
  126. else
  127. rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  128. break;
  129. case 't':
  130. timestamp = optarg[0];
  131. if ((timestamp != 'a') && (timestamp != 'A') &&
  132. (timestamp != 'd') && (timestamp != 'z')) {
  133. printf("%s: unknown timestamp mode '%c' - ignored\n",
  134. basename(argv[0]), optarg[0]);
  135. timestamp = 0;
  136. }
  137. break;
  138. case '?':
  139. print_usage(basename(argv[0]));
  140. exit(0);
  141. break;
  142. default:
  143. fprintf(stderr, "Unknown option %c\n", opt);
  144. print_usage(basename(argv[0]));
  145. exit(1);
  146. break;
  147. }
  148. }
  149. if (rx_ext && !ext) {
  150. print_usage(basename(argv[0]));
  151. exit(0);
  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. if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
  158. perror("socket");
  159. return 1;
  160. }
  161. /* try to switch the socket into CAN FD mode */
  162. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
  163. if (src & CAN_EFF_FLAG) {
  164. rfilter[0].can_id = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
  165. rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  166. } else {
  167. rfilter[0].can_id = src & CAN_SFF_MASK;
  168. rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  169. }
  170. if (dst & CAN_EFF_FLAG) {
  171. rfilter[1].can_id = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
  172. rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  173. } else {
  174. rfilter[1].can_id = dst & CAN_SFF_MASK;
  175. rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
  176. }
  177. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
  178. addr.can_family = AF_CAN;
  179. addr.can_ifindex = if_nametoindex(argv[optind]);
  180. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  181. perror("bind");
  182. return 1;
  183. }
  184. while (1) {
  185. nbytes = read(s, &frame, sizeof(frame));
  186. if (nbytes < 0) {
  187. perror("read");
  188. return 1;
  189. } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
  190. fprintf(stderr, "read: incomplete CAN frame %zu %d\n", sizeof(frame), nbytes);
  191. return 1;
  192. } else {
  193. if (frame.can_id == src && ext && !extany && extaddr != frame.data[0])
  194. continue;
  195. if (frame.can_id == dst && rx_ext && !rx_extany && rx_extaddr != frame.data[0])
  196. continue;
  197. if (color)
  198. printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
  199. if (timestamp) {
  200. ioctl(s, SIOCGSTAMP, &tv);
  201. switch (timestamp) {
  202. case 'a': /* absolute with timestamp */
  203. printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
  204. break;
  205. case 'A': /* absolute with date */
  206. {
  207. struct tm tm;
  208. char timestring[25];
  209. tm = *localtime(&tv.tv_sec);
  210. strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
  211. printf("(%s.%06ld) ", timestring, tv.tv_usec);
  212. }
  213. break;
  214. case 'd': /* delta */
  215. case 'z': /* starting with zero */
  216. {
  217. struct timeval diff;
  218. if (last_tv.tv_sec == 0) /* first init */
  219. last_tv = tv;
  220. diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
  221. diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
  222. if (diff.tv_usec < 0)
  223. diff.tv_sec--, diff.tv_usec += 1000000;
  224. if (diff.tv_sec < 0)
  225. diff.tv_sec = diff.tv_usec = 0;
  226. printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
  227. if (timestamp == 'd')
  228. last_tv = tv; /* update for delta calculation */
  229. }
  230. break;
  231. default: /* no timestamp output */
  232. break;
  233. }
  234. }
  235. if (frame.can_id & CAN_EFF_FLAG)
  236. printf(" %s %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
  237. else
  238. printf(" %s %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
  239. if (ext)
  240. printf("{%02X}", frame.data[0]);
  241. if (nbytes == CAN_MTU)
  242. printf(" [%d] ", frame.len);
  243. else
  244. printf(" [%02d] ", frame.len);
  245. datidx = 0;
  246. n_pci = frame.data[ext];
  247. switch (n_pci & 0xF0) {
  248. case 0x00:
  249. if (n_pci & 0xF) {
  250. printf("[SF] ln: %-4d data:", n_pci & 0xF);
  251. datidx = ext+1;
  252. } else {
  253. printf("[SF] ln: %-4d data:", frame.data[ext + 1]);
  254. datidx = ext+2;
  255. }
  256. break;
  257. case 0x10:
  258. fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
  259. if (fflen)
  260. datidx = ext+2;
  261. else {
  262. fflen = (frame.data[ext+2]<<24) +
  263. (frame.data[ext+3]<<16) +
  264. (frame.data[ext+4]<<8) +
  265. frame.data[ext+5];
  266. datidx = ext+6;
  267. }
  268. printf("[FF] ln: %-4lu data:", fflen);
  269. break;
  270. case 0x20:
  271. printf("[CF] sn: %X data:", n_pci & 0x0F);
  272. datidx = ext+1;
  273. break;
  274. case 0x30:
  275. n_pci &= 0x0F;
  276. printf("[FC] FC: %d ", n_pci);
  277. if (n_pci > 3)
  278. n_pci = 3;
  279. printf("= %s # ", fc_info[n_pci]);
  280. printf("BS: %d %s# ", frame.data[ext+1],
  281. (frame.data[ext+1])? "":"= off ");
  282. i = frame.data[ext+2];
  283. printf("STmin: 0x%02X = ", i);
  284. if (i < 0x80)
  285. printf("%d ms", i);
  286. else if (i > 0xF0 && i < 0xFA)
  287. printf("%d us", (i & 0x0F) * 100);
  288. else
  289. printf("reserved");
  290. break;
  291. default:
  292. printf("[??]");
  293. }
  294. if (datidx && frame.len > datidx) {
  295. printf(" ");
  296. for (i = datidx; i < frame.len; i++) {
  297. printf("%02X ", frame.data[i]);
  298. }
  299. if (asc) {
  300. printf("%*s", ((7-ext) - (frame.len-datidx))*3 + 5 ,
  301. "- '");
  302. for (i = datidx; i < frame.len; i++) {
  303. printf("%c",((frame.data[i] > 0x1F) &&
  304. (frame.data[i] < 0x7F))?
  305. frame.data[i] : '.');
  306. }
  307. printf("'");
  308. }
  309. }
  310. if (color)
  311. printf("%s", ATTRESET);
  312. printf("\n");
  313. fflush(stdout);
  314. }
  315. }
  316. close(s);
  317. return 0;
  318. }