~ubuntu-branches/ubuntu/wily/can-utils/wily-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * isotpdump.c - dump and explain ISO15765-2 protocol CAN frames
 *
 * Copyright (c) 2008 Volkswagen Group Electronic Research
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of Volkswagen nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * Alternatively, provided that this notice is retained in full, this
 * software may be distributed under the terms of the GNU General
 * Public License ("GPL") version 2, in which case the provisions of the
 * GPL apply INSTEAD OF those given above.
 *
 * The provided data structures and external interfaces from this code
 * are not restricted to be used by modules with a GPL compatible license.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * Send feedback to <linux-can@vger.kernel.org>
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
#include <time.h>

#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <linux/can.h>
#include <linux/can/raw.h>
#include "terminal.h"

#define NO_CAN_ID 0xFFFFFFFFU

const char fc_info [4][9] = { "CTS", "WT", "OVFLW", "reserved" };
const int canfd_on = 1;

void print_usage(char *prg)
{
	fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
	fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
	fprintf(stderr, "         -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
	fprintf(stderr, "         -x <addr>   (extended addressing mode. Use 'any' for all addresses)\n");
	fprintf(stderr, "         -X <addr>   (extended addressing mode (rx addr). Use 'any' for all)\n");
	fprintf(stderr, "         -c          (color mode)\n");
	fprintf(stderr, "         -a          (print data also in ASCII-chars)\n");
	fprintf(stderr, "         -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
	fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
	fprintf(stderr, "\n");
}

int main(int argc, char **argv)
{
	int s;
	struct sockaddr_can addr;
	struct can_filter rfilter[2];
	struct canfd_frame frame;
	int nbytes, i;
	canid_t src = NO_CAN_ID;
	canid_t dst = NO_CAN_ID;
	int ext = 0;
	int extaddr = 0;
	int extany = 0;
	int rx_ext = 0;
	int rx_extaddr = 0;
	int rx_extany = 0;
	int asc = 0;
	int color = 0;
	int timestamp = 0;
	int datidx = 0;
	unsigned long fflen = 0;
	struct ifreq ifr;
	int ifindex;
	struct timeval tv, last_tv;
	unsigned int n_pci;
	int opt;

	last_tv.tv_sec  = 0;
	last_tv.tv_usec = 0;

	while ((opt = getopt(argc, argv, "s:d:ax:X:ct:?")) != -1) {
		switch (opt) {
		case 's':
			src = strtoul(optarg, (char **)NULL, 16);
			if (strlen(optarg) > 7)
				src |= CAN_EFF_FLAG;
			break;

		case 'd':
			dst = strtoul(optarg, (char **)NULL, 16);
			if (strlen(optarg) > 7)
				dst |= CAN_EFF_FLAG;
			break;

		case 'c':
			color = 1;
			break;

		case 'a':
			asc = 1;
			break;

		case 'x':
			ext = 1;
			if (!strncmp(optarg, "any", 3))
				extany = 1;
			else
				extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
			break;

		case 'X':
			rx_ext = 1;
			if (!strncmp(optarg, "any", 3))
				rx_extany = 1;
			else
				rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
			break;

		case 't':
			timestamp = optarg[0];
			if ((timestamp != 'a') && (timestamp != 'A') &&
			    (timestamp != 'd') && (timestamp != 'z')) {
				printf("%s: unknown timestamp mode '%c' - ignored\n",
				       basename(argv[0]), optarg[0]);
				timestamp = 0;
			}
			break;

		case '?':
			print_usage(basename(argv[0]));
			exit(0);
			break;

		default:
			fprintf(stderr, "Unknown option %c\n", opt);
			print_usage(basename(argv[0]));
			exit(1);
			break;
		}
	}

	if (rx_ext && !ext) {
		print_usage(basename(argv[0]));
		exit(0);
	}

	if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
		print_usage(basename(argv[0]));
		exit(0);
	}

	if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
		perror("socket");
		return 1;
	}

	/* try to switch the socket into CAN FD mode */
	setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));

	if (src & CAN_EFF_FLAG) {
		rfilter[0].can_id   = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
		rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
	} else {
		rfilter[0].can_id   = src & CAN_SFF_MASK;
		rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
	}

	if (dst & CAN_EFF_FLAG) {
		rfilter[1].can_id   = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
		rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
	} else {
		rfilter[1].can_id   = dst & CAN_SFF_MASK;
		rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
	}

	setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

	strcpy(ifr.ifr_name, argv[optind]);
	ioctl(s, SIOCGIFINDEX, &ifr);
	ifindex = ifr.ifr_ifindex;

	addr.can_family = AF_CAN;
	addr.can_ifindex = ifindex;

	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("bind");
		return 1;
	}

	while (1) {
		nbytes = read(s, &frame, sizeof(frame));
		if (nbytes < 0) {
			perror("read");
			return 1;
		} else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
			fprintf(stderr, "read: incomplete CAN frame %zu %d\n", sizeof(frame), nbytes);
			return 1;
		} else {

			if (frame.can_id == src && ext && !extany && extaddr != frame.data[0])
				continue;

			if (frame.can_id == dst && rx_ext && !rx_extany && rx_extaddr != frame.data[0])
				continue;

			if (color)
				printf("%s", (frame.can_id == src)? FGRED:FGBLUE);

			if (timestamp) {
				ioctl(s, SIOCGSTAMP, &tv);


				switch (timestamp) {

				case 'a': /* absolute with timestamp */
					printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
					break;

				case 'A': /* absolute with date */
				{
					struct tm tm;
					char timestring[25];

					tm = *localtime(&tv.tv_sec);
					strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
					printf("(%s.%06ld) ", timestring, tv.tv_usec);
				}
				break;

				case 'd': /* delta */
				case 'z': /* starting with zero */
				{
					struct timeval diff;

					if (last_tv.tv_sec == 0)   /* first init */
						last_tv = tv;
					diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
					diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
					if (diff.tv_usec < 0)
						diff.tv_sec--, diff.tv_usec += 1000000;
					if (diff.tv_sec < 0)
						diff.tv_sec = diff.tv_usec = 0;
					printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);

					if (timestamp == 'd')
						last_tv = tv; /* update for delta calculation */
				}
				break;

				default: /* no timestamp output */
					break;
				}
			}

			if (frame.can_id & CAN_EFF_FLAG)
				printf(" %s  %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
			else
				printf(" %s  %3X", argv[optind], frame.can_id & CAN_SFF_MASK);

			if (ext)
				printf("{%02X}", frame.data[0]);

			if (nbytes == CAN_MTU)
				printf("  [%d]  ", frame.len);
			else
				printf(" [%02d]  ", frame.len);

			datidx = 0;
			n_pci = frame.data[ext];
	    
			switch (n_pci & 0xF0) {
			case 0x00:
				if (n_pci & 0xF) {
					printf("[SF] ln: %-4d data:", n_pci & 0xF);
					datidx = ext+1;
				} else {
					printf("[SF] ln: %-4d data:", frame.data[ext + 1]);
					datidx = ext+2;
				}
				break;

			case 0x10:
				fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
				if (fflen)
					datidx = ext+2;
				else {
					fflen = (frame.data[ext+2]<<24) +
						(frame.data[ext+3]<<16) +
						(frame.data[ext+4]<<8) +
						frame.data[ext+5];
					datidx = ext+6;
				}
				printf("[FF] ln: %-4lu data:", fflen);
				break;

			case 0x20:
				printf("[CF] sn: %X    data:", n_pci & 0x0F);
				datidx = ext+1;
				break;

			case 0x30:
				n_pci &= 0x0F;
				printf("[FC] FC: %d ", n_pci);

				if (n_pci > 3)
					n_pci = 3;

				printf("= %s # ", fc_info[n_pci]);

				printf("BS: %d %s# ", frame.data[ext+1],
				       (frame.data[ext+1])? "":"= off ");

				i = frame.data[ext+2];
				printf("STmin: 0x%02X = ", i);

				if (i < 0x80)
					printf("%d ms", i);
				else if (i > 0xF0 && i < 0xFA)
					printf("%d us", (i & 0x0F) * 100);
				else
					printf("reserved");
				break;

			default:
				printf("[??]");
			}

			if (datidx && frame.len > datidx) {
				printf(" ");
				for (i = datidx; i < frame.len; i++) {
					printf("%02X ", frame.data[i]);
				}

				if (asc) {
					printf("%*s", ((7-ext) - (frame.len-datidx))*3 + 5 ,
					       "-  '");
					for (i = datidx; i < frame.len; i++) {
						printf("%c",((frame.data[i] > 0x1F) &&
							     (frame.data[i] < 0x7F))?
						       frame.data[i] : '.');
					}
					printf("'");
				}
			}

			if (color)
				printf("%s", ATTRESET);
			printf("\n");
			fflush(stdout);
		}
	}

	close(s);

	return 0;
}