]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - isotpsend.c
can-utils: AOSP build clean up
[sojka/can-utils.git] / isotpsend.c
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
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <libgen.h>
49
50 #include <net/if.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54
55 #include <linux/can.h>
56 #include <linux/can/isotp.h>
57
58 #define NO_CAN_ID 0xFFFFFFFFU
59
60 void print_usage(char *prg)
61 {
62         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
63         fprintf(stderr, "Options: -s <can_id>  (source can_id. Use 8 digits for extended IDs)\n");
64         fprintf(stderr, "         -d <can_id>  (destination can_id. Use 8 digits for extended IDs)\n");
65         fprintf(stderr, "         -x <addr>    (extended addressing mode. Use 'any' for all addresses)\n");
66         fprintf(stderr, "         -p <byte>    (set and enable padding byte)\n");
67         fprintf(stderr, "         -P <mode>    (check padding in FC. (l)ength (c)ontent (a)ll)\n");
68         fprintf(stderr, "         -t <time ns> (frame transmit time (N_As) in nanosecs)\n");
69         fprintf(stderr, "         -f <time ns> (ignore FC and force local tx stmin value in nanosecs)\n");
70         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
71         fprintf(stderr, "The pdu data is expected on STDIN in space separated ASCII hex values.\n");
72         fprintf(stderr, "\n");
73 }
74
75 int main(int argc, char **argv)
76 {
77     int s;
78     struct sockaddr_can addr;
79     struct ifreq ifr;
80     static struct can_isotp_options opts;
81     int opt;
82     extern int optind, opterr, optopt;
83     __u32 force_tx_stmin = 0;
84     unsigned char buf[4096];
85     int buflen = 0;
86
87     addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
88
89     while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:?")) != -1) {
90             switch (opt) {
91             case 's':
92                     addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
93                     if (strlen(optarg) > 7)
94                             addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
95                     break;
96
97             case 'd':
98                     addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
99                     if (strlen(optarg) > 7)
100                             addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
101                     break;
102
103             case 'x':
104                     opts.flags |= CAN_ISOTP_EXTEND_ADDR;
105                     opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
106                     break;
107
108             case 'p':
109                     opts.flags |= CAN_ISOTP_TX_PADDING;
110                     opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
111                     break;
112
113             case 'P':
114                     if (optarg[0] == 'l')
115                             opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
116                     else if (optarg[0] == 'c')
117                             opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
118                     else if (optarg[0] == 'a')
119                             opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
120                     else {
121                             printf("unknown padding check option '%c'.\n", optarg[0]);
122                             print_usage(basename(argv[0]));
123                             exit(0);
124                     }
125                     break;
126
127             case 't':
128                     opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
129                     break;
130
131             case 'f':
132                     opts.flags |= CAN_ISOTP_FORCE_TXSTMIN;
133                     force_tx_stmin = strtoul(optarg, (char **)NULL, 10);
134                     break;
135
136             case '?':
137                     print_usage(basename(argv[0]));
138                     exit(0);
139                     break;
140
141             default:
142                     fprintf(stderr, "Unknown option %c\n", opt);
143                     print_usage(basename(argv[0]));
144                     exit(1);
145                     break;
146             }
147     }
148
149     if ((argc - optind != 1) ||
150         (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
151         (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
152             print_usage(basename(argv[0]));
153             exit(1);
154     }
155   
156     if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
157         perror("socket");
158         exit(1);
159     }
160
161     setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
162
163     if (opts.flags & CAN_ISOTP_FORCE_TXSTMIN)
164             setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_TX_STMIN, &force_tx_stmin, sizeof(force_tx_stmin));
165
166     addr.can_family = AF_CAN;
167     strcpy(ifr.ifr_name, argv[optind]);
168     ioctl(s, SIOCGIFINDEX, &ifr);
169     addr.can_ifindex = ifr.ifr_ifindex;
170
171     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
172         perror("bind");
173         close(s);
174         exit(1);
175     }
176
177     while (buflen < 4096 && scanf("%hhx", &buf[buflen]) == 1)
178             buflen++;
179
180     write(s, buf, buflen);
181
182     /* 
183      * due to a Kernel internal wait queue the PDU is sent completely
184      * before close() returns.
185      */
186     close(s);
187
188     return 0;
189 }