isotpsend.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * isotpsend.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, " -t <time ns> (frame transmit time (N_As) in nanosecs)\n");
  65. fprintf(stderr, " -f <time ns> (ignore FC and force local tx stmin value in nanosecs)\n");
  66. fprintf(stderr, " -D <len> (send a fixed PDU with len bytes - no STDIN data)\n");
  67. fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
  68. fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
  69. fprintf(stderr, "The pdu data is expected on STDIN in space separated ASCII hex values.\n");
  70. fprintf(stderr, "\n");
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. int s;
  75. struct sockaddr_can addr;
  76. static struct can_isotp_options opts;
  77. static struct can_isotp_ll_options llopts;
  78. int opt;
  79. extern int optind, opterr, optopt;
  80. __u32 force_tx_stmin = 0;
  81. unsigned char buf[BUFSIZE];
  82. int buflen = 0;
  83. int datalen = 0;
  84. int retval = 0;
  85. addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
  86. while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:D:L:?")) != -1) {
  87. switch (opt) {
  88. case 's':
  89. addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
  90. if (strlen(optarg) > 7)
  91. addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
  92. break;
  93. case 'd':
  94. addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
  95. if (strlen(optarg) > 7)
  96. addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
  97. break;
  98. case 'x':
  99. {
  100. int elements = sscanf(optarg, "%hhx:%hhx",
  101. &opts.ext_address,
  102. &opts.rx_ext_address);
  103. if (elements == 1)
  104. opts.flags |= CAN_ISOTP_EXTEND_ADDR;
  105. else if (elements == 2)
  106. opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR);
  107. else {
  108. printf("incorrect extended addr values '%s'.\n", optarg);
  109. print_usage(basename(argv[0]));
  110. exit(0);
  111. }
  112. break;
  113. }
  114. case 'p':
  115. {
  116. int elements = sscanf(optarg, "%hhx:%hhx",
  117. &opts.txpad_content,
  118. &opts.rxpad_content);
  119. if (elements == 1)
  120. opts.flags |= CAN_ISOTP_TX_PADDING;
  121. else if (elements == 2)
  122. opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING);
  123. else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1)
  124. opts.flags |= CAN_ISOTP_RX_PADDING;
  125. else {
  126. printf("incorrect padding values '%s'.\n", optarg);
  127. print_usage(basename(argv[0]));
  128. exit(0);
  129. }
  130. break;
  131. }
  132. case 'P':
  133. if (optarg[0] == 'l')
  134. opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
  135. else if (optarg[0] == 'c')
  136. opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
  137. else if (optarg[0] == 'a')
  138. opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA);
  139. else {
  140. printf("unknown padding check option '%c'.\n", optarg[0]);
  141. print_usage(basename(argv[0]));
  142. exit(0);
  143. }
  144. break;
  145. case 't':
  146. opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
  147. break;
  148. case 'f':
  149. opts.flags |= CAN_ISOTP_FORCE_TXSTMIN;
  150. force_tx_stmin = strtoul(optarg, (char **)NULL, 10);
  151. break;
  152. case 'D':
  153. datalen = strtoul(optarg, (char **)NULL, 10);
  154. if (!datalen || datalen >= BUFSIZE) {
  155. print_usage(basename(argv[0]));
  156. exit(0);
  157. }
  158. break;
  159. case 'L':
  160. if (sscanf(optarg, "%hhu:%hhu:%hhu",
  161. &llopts.mtu,
  162. &llopts.tx_dl,
  163. &llopts.tx_flags) != 3) {
  164. printf("unknown link layer options '%s'.\n", optarg);
  165. print_usage(basename(argv[0]));
  166. exit(0);
  167. }
  168. break;
  169. case '?':
  170. print_usage(basename(argv[0]));
  171. exit(0);
  172. break;
  173. default:
  174. fprintf(stderr, "Unknown option %c\n", opt);
  175. print_usage(basename(argv[0]));
  176. exit(1);
  177. break;
  178. }
  179. }
  180. if ((argc - optind != 1) ||
  181. (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
  182. (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
  183. print_usage(basename(argv[0]));
  184. exit(1);
  185. }
  186. if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
  187. perror("socket");
  188. exit(1);
  189. }
  190. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
  191. if (llopts.tx_dl) {
  192. if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
  193. perror("link layer sockopt");
  194. exit(1);
  195. }
  196. }
  197. if (opts.flags & CAN_ISOTP_FORCE_TXSTMIN)
  198. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_TX_STMIN, &force_tx_stmin, sizeof(force_tx_stmin));
  199. addr.can_family = AF_CAN;
  200. addr.can_ifindex = if_nametoindex(argv[optind]);
  201. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  202. perror("bind");
  203. close(s);
  204. exit(1);
  205. }
  206. if (!datalen) {
  207. while (buflen < BUFSIZE && scanf("%hhx", &buf[buflen]) == 1)
  208. buflen++;
  209. } else {
  210. for (buflen = 0; buflen < datalen; buflen++)
  211. buf[buflen] = ((buflen % 0xFF) + 1) & 0xFF;
  212. }
  213. retval = write(s, buf, buflen);
  214. if (retval < 0) {
  215. perror("write");
  216. return retval;
  217. }
  218. if (retval != buflen)
  219. fprintf(stderr, "wrote only %d from %d byte\n", retval, buflen);
  220. /*
  221. * due to a Kernel internal wait queue the PDU is sent completely
  222. * before close() returns.
  223. */
  224. close(s);
  225. return 0;
  226. }