isotprecv.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * isotprecv.c
  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 <unistd.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <libgen.h>
  48. #include <net/if.h>
  49. #include <sys/types.h>
  50. #include <sys/socket.h>
  51. #include <sys/ioctl.h>
  52. #include <linux/can.h>
  53. #include <linux/can/isotp.h>
  54. #define NO_CAN_ID 0xFFFFFFFFU
  55. #define BUFSIZE 5000 /* size > 4095 to check socket API internal checks */
  56. void print_usage(char *prg)
  57. {
  58. fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
  59. fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
  60. fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
  61. fprintf(stderr, " -x <addr>[:<rxaddr>] (extended addressing / opt. separate rxaddr)\n");
  62. fprintf(stderr, " -p [tx]:[rx] (set and enable tx/rx padding bytes)\n");
  63. fprintf(stderr, " -P <mode> (check rx padding for (l)ength (c)ontent (a)ll)\n");
  64. fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
  65. fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
  66. fprintf(stderr, " -f <time ns> (force rx stmin value in nanosecs)\n");
  67. fprintf(stderr, " -w <num> (max. wait frame transmissions.)\n");
  68. fprintf(stderr, " -l (loop: do not exit after pdu receiption.)\n");
  69. fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
  70. fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
  71. fprintf(stderr, "The pdu data is written on STDOUT in space separated ASCII hex values.\n");
  72. fprintf(stderr, "\n");
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. int s;
  77. struct sockaddr_can addr;
  78. static struct can_isotp_options opts;
  79. static struct can_isotp_fc_options fcopts;
  80. static struct can_isotp_ll_options llopts;
  81. int opt, i;
  82. extern int optind, opterr, optopt;
  83. __u32 force_rx_stmin = 0;
  84. int loop = 0;
  85. unsigned char msg[BUFSIZE];
  86. int nbytes;
  87. addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
  88. while ((opt = getopt(argc, argv, "s:d:x:p:P:b:m:w:f:lL:?")) != -1) {
  89. switch (opt) {
  90. case 's':
  91. addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
  92. if (strlen(optarg) > 7)
  93. addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
  94. break;
  95. case 'd':
  96. addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
  97. if (strlen(optarg) > 7)
  98. addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
  99. break;
  100. case 'x':
  101. {
  102. int elements = sscanf(optarg, "%hhx:%hhx",
  103. &opts.ext_address,
  104. &opts.rx_ext_address);
  105. if (elements == 1)
  106. opts.flags |= CAN_ISOTP_EXTEND_ADDR;
  107. else if (elements == 2)
  108. opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR);
  109. else {
  110. printf("incorrect extended addr values '%s'.\n", optarg);
  111. print_usage(basename(argv[0]));
  112. exit(0);
  113. }
  114. break;
  115. }
  116. case 'p':
  117. {
  118. int elements = sscanf(optarg, "%hhx:%hhx",
  119. &opts.txpad_content,
  120. &opts.rxpad_content);
  121. if (elements == 1)
  122. opts.flags |= CAN_ISOTP_TX_PADDING;
  123. else if (elements == 2)
  124. opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING);
  125. else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1)
  126. opts.flags |= CAN_ISOTP_RX_PADDING;
  127. else {
  128. printf("incorrect padding values '%s'.\n", optarg);
  129. print_usage(basename(argv[0]));
  130. exit(0);
  131. }
  132. break;
  133. }
  134. case 'P':
  135. if (optarg[0] == 'l')
  136. opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
  137. else if (optarg[0] == 'c')
  138. opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
  139. else if (optarg[0] == 'a')
  140. opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA);
  141. else {
  142. printf("unknown padding check option '%c'.\n", optarg[0]);
  143. print_usage(basename(argv[0]));
  144. exit(0);
  145. }
  146. break;
  147. case 'b':
  148. fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  149. break;
  150. case 'm':
  151. fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  152. break;
  153. case 'w':
  154. fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  155. break;
  156. case 'f':
  157. opts.flags |= CAN_ISOTP_FORCE_RXSTMIN;
  158. force_rx_stmin = strtoul(optarg, (char **)NULL, 10);
  159. break;
  160. case 'l':
  161. loop = 1;
  162. break;
  163. case 'L':
  164. if (sscanf(optarg, "%hhu:%hhu:%hhu",
  165. &llopts.mtu,
  166. &llopts.tx_dl,
  167. &llopts.tx_flags) != 3) {
  168. printf("unknown link layer options '%s'.\n", optarg);
  169. print_usage(basename(argv[0]));
  170. exit(0);
  171. }
  172. break;
  173. case '?':
  174. print_usage(basename(argv[0]));
  175. exit(0);
  176. break;
  177. default:
  178. fprintf(stderr, "Unknown option %c\n", opt);
  179. print_usage(basename(argv[0]));
  180. exit(1);
  181. break;
  182. }
  183. }
  184. if ((argc - optind != 1) ||
  185. (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
  186. (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
  187. print_usage(basename(argv[0]));
  188. exit(1);
  189. }
  190. if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
  191. perror("socket");
  192. exit(1);
  193. }
  194. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
  195. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
  196. if (llopts.tx_dl) {
  197. if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
  198. perror("link layer sockopt");
  199. exit(1);
  200. }
  201. }
  202. if (opts.flags & CAN_ISOTP_FORCE_RXSTMIN)
  203. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RX_STMIN, &force_rx_stmin, sizeof(force_rx_stmin));
  204. addr.can_family = AF_CAN;
  205. addr.can_ifindex = if_nametoindex(argv[optind]);
  206. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  207. perror("bind");
  208. close(s);
  209. exit(1);
  210. }
  211. do {
  212. nbytes = read(s, msg, BUFSIZE);
  213. if (nbytes > 0 && nbytes < BUFSIZE)
  214. for (i=0; i < nbytes; i++)
  215. printf("%02X ", msg[i]);
  216. printf("\n");
  217. } while (loop);
  218. close(s);
  219. return 0;
  220. }