4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
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
40 * Send feedback to <linux-can@vger.kernel.org>
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/ioctl.h>
60 #include <netinet/in.h>
62 #include <linux/can.h>
63 #include <linux/can/raw.h>
69 #define MAXDEV 6 /* change sscanf()'s manually if changed here */
71 #define ANL "\r\n" /* newline in ASC mode */
74 #define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */
78 static char devname[MAXDEV][IFNAMSIZ+1];
79 static int dindex[MAXDEV];
80 static int max_devname_len;
82 extern int optind, opterr, optopt;
84 static volatile int running = 1;
86 void print_usage(char *prg)
88 fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
89 fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
90 fprintf(stderr, "Options: -m <mask> (ID filter mask. Default 0x00000000) *\n");
91 fprintf(stderr, " -v <value> (ID filter value. Default 0x00000000) *\n");
92 fprintf(stderr, " -i <0|1> (invert the specified ID filter) *\n");
93 fprintf(stderr, " -e <emask> (mask for error frames)\n");
94 fprintf(stderr, " -p <port> (listen on port <port>. Default: %d)\n", DEFPORT);
95 fprintf(stderr, "\n");
96 fprintf(stderr, "* The CAN ID filter matches, when ...\n");
97 fprintf(stderr, " <received_can_id> & mask == value & mask\n");
98 fprintf(stderr, "\n");
99 fprintf(stderr, "When using more than one CAN interface the options\n");
100 fprintf(stderr, "m/v/i/e have comma seperated values e.g. '-m 0,7FF,0'\n");
101 fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n\n", ANYDEV);
104 int idx2dindex(int ifidx, int socket)
109 for (i=0; i<MAXDEV; i++) {
110 if (dindex[i] == ifidx)
114 /* create new interface index cache entry */
116 /* remove index cache zombies first */
117 for (i=0; i < MAXDEV; i++) {
119 ifr.ifr_ifindex = dindex[i];
120 if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
125 for (i=0; i < MAXDEV; i++)
126 if (!dindex[i]) /* free entry */
130 printf("Interface index cache only supports %d interfaces.\n", MAXDEV);
136 ifr.ifr_ifindex = ifidx;
137 if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
138 perror("SIOCGIFNAME");
140 if (max_devname_len < strlen(ifr.ifr_name))
141 max_devname_len = strlen(ifr.ifr_name);
143 strcpy(devname[i], ifr.ifr_name);
146 printf("new index %d (%s)\n", i, devname[i]);
153 * This is a Signalhandler. When we get a signal, that a child
154 * terminated, we wait for it, so the zombie will disappear.
156 void childdied(int i)
162 * This is a Signalhandler for a cought SIGTERM
164 void shutdown_gra(int i)
170 int main(int argc, char **argv)
172 struct sigaction signalaction;
176 int socki, accsocket;
177 canid_t mask[MAXDEV] = {0};
178 canid_t value[MAXDEV] = {0};
179 int inv_filter[MAXDEV] = {0};
180 can_err_mask_t err_mask[MAXDEV] = {0};
182 int currmax = 1; /* we assume at least one can bus ;-) */
183 struct sockaddr_can addr;
184 struct can_filter rfilter;
185 struct canfd_frame frame;
186 const int canfd_on = 1;
187 int nbytes, i, j, maxdlen;
191 struct sockaddr_in inaddr;
192 struct sockaddr_in clientaddr;
193 socklen_t sin_size = sizeof(clientaddr);
196 sigemptyset(&sigset);
197 signalaction.sa_handler = &childdied;
198 signalaction.sa_mask = sigset;
199 signalaction.sa_flags = 0;
200 sigaction(SIGCHLD, &signalaction, NULL); /* install signal for dying child */
201 signalaction.sa_handler = &shutdown_gra;
202 signalaction.sa_mask = sigset;
203 signalaction.sa_flags = 0;
204 sigaction(SIGTERM, &signalaction, NULL); /* install Signal for termination */
205 sigaction(SIGINT, &signalaction, NULL); /* install Signal for termination */
207 while ((opt = getopt(argc, argv, "m:v:i:e:p:?")) != -1) {
211 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
212 &mask[0], &mask[1], &mask[2],
213 &mask[3], &mask[4], &mask[5]);
219 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
220 &value[0], &value[1], &value[2],
221 &value[3], &value[4], &value[5]);
227 i = sscanf(optarg, "%d,%d,%d,%d,%d,%d",
228 &inv_filter[0], &inv_filter[1], &inv_filter[2],
229 &inv_filter[3], &inv_filter[4], &inv_filter[5]);
235 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
236 &err_mask[0], &err_mask[1], &err_mask[2],
237 &err_mask[3], &err_mask[4], &err_mask[5]);
245 print_usage(basename(argv[0]));
251 if (optind == argc) {
252 print_usage(basename(argv[0]));
256 /* count in options higher than device count ? */
257 if (optind + currmax > argc) {
258 printf("low count of CAN devices!\n");
262 currmax = argc - optind; /* find real number of CAN devices */
264 if (currmax > MAXDEV) {
265 printf("More than %d CAN devices!\n", MAXDEV);
270 socki = socket(PF_INET, SOCK_STREAM, 0);
276 inaddr.sin_family = AF_INET;
277 inaddr.sin_addr.s_addr = htonl(INADDR_ANY);
278 inaddr.sin_port = htons(port);
280 while(bind(socki, (struct sockaddr*)&inaddr, sizeof(inaddr)) < 0) {
281 printf(".");fflush(NULL);
285 if (listen(socki, 3) != 0) {
291 accsocket = accept(socki, (struct sockaddr*)&clientaddr, &sin_size);
293 //printf("accepted\n");
299 else if (errno != EINTR) {
305 for (i=0; i<currmax; i++) {
308 printf("open %d '%s' m%08X v%08X i%d e%d.\n",
309 i, argv[optind+i], mask[i], value[i],
310 inv_filter[i], err_mask[i]);
313 if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
318 if (mask[i] || value[i]) {
320 printf("CAN ID filter[%d] for %s set to "
321 "mask = %08X, value = %08X %s\n",
322 i, argv[optind+i], mask[i], value[i],
323 (inv_filter[i]) ? "(inv_filter)" : "");
325 rfilter.can_id = value[i];
326 rfilter.can_mask = mask[i];
328 rfilter.can_id |= CAN_INV_FILTER;
330 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
331 &rfilter, sizeof(rfilter));
335 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
336 &err_mask[i], sizeof(err_mask[i]));
338 /* try to switch the socket into CAN FD mode */
339 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
341 j = strlen(argv[optind+i]);
343 if (!(j < IFNAMSIZ)) {
344 printf("name of CAN device '%s' is too long!\n", argv[optind+i]);
348 if (j > max_devname_len)
349 max_devname_len = j; /* for nice printing */
351 addr.can_family = AF_CAN;
353 if (strcmp(ANYDEV, argv[optind+i])) {
354 strcpy(ifr.ifr_name, argv[optind+i]);
355 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
356 perror("SIOCGIFINDEX");
359 addr.can_ifindex = ifr.ifr_ifindex;
362 addr.can_ifindex = 0; /* any can interface */
364 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
373 for (i=0; i<currmax; i++)
376 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
382 for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
384 if (FD_ISSET(s[i], &rdfs)) {
386 socklen_t len = sizeof(addr);
389 if ((nbytes = recvfrom(s[i], &frame, CANFD_MTU, 0,
390 (struct sockaddr*)&addr, &len)) < 0) {
395 if ((size_t)nbytes == CAN_MTU)
396 maxdlen = CAN_MAX_DLEN;
397 else if ((size_t)nbytes == CANFD_MTU)
398 maxdlen = CANFD_MAX_DLEN;
400 fprintf(stderr, "read: incomplete CAN frame\n");
404 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
405 perror("SIOCGSTAMP");
408 idx = idx2dindex(addr.can_ifindex, s[i]);
410 sprintf(temp, "(%ld.%06ld) %*s ",
411 tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx]);
412 sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen);
415 if (write(accsocket, temp, strlen(temp)) < 0) {
416 perror("writeaccsock");
421 /* print CAN frame in log file style to stdout */
422 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
423 printf("%*s ", max_devname_len, devname[idx]);
424 fprint_canframe(stdout, &frame, "\n", 0, maxdlen);
431 for (i=0; i<currmax; i++)