summaryrefslogtreecommitdiffstats
path: root/src/cansequence.c
blob: f86127b8709cc01d72f0c828bea668417e9c42a0 (plain)
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
#include <can_config.h>

#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <net/if.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>

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

extern int optind, opterr, optopt;

static int s = -1;
static int running = 1;

enum {
	VERSION_OPTION = CHAR_MAX + 1,
};

#define CAN_ID_DEFAULT	(2)

void print_usage(char *prg)
{
	fprintf(stderr, "Usage: %s [<can-interface>] [Options]\n"
		"\n"
		"cansequence sends CAN messages with a rising sequence number as payload.\n"
		"When the -r option is given, cansequence expects to receive these messages\n"
		"and prints an error message if a wrong sequence number is encountered.\n"
		"The main purpose of this program is to test the reliability of CAN links.\n"
		"\n"
		"Options:\n"
		" -e  --extended		send extended frame\n"
		" -i, --identifier=ID	CAN Identifier (default = %u)\n"
		" -r, --receive		work as receiver\n"
		"     --loop=COUNT	send message COUNT times\n"
		" -p  --poll		use poll(2) to wait for buffer space while sending\n"
		" -q  --quit		quit if a wrong sequence is encountered\n"
		" -v, --verbose		be verbose (twice to be even more verbose\n"
		" -h  --help		this help\n"
		"     --version		print version information and exit\n",
		prg, CAN_ID_DEFAULT);
}

void sigterm(int signo)
{
	running = 0;
}

int main(int argc, char **argv)
{
	struct ifreq ifr;
	struct sockaddr_can addr;
	struct can_frame frame = {
		.can_dlc = 1,
	};
	struct can_filter filter[] = {
		{
			.can_id = CAN_ID_DEFAULT,
		},
	};
	char *interface = "can0";
	unsigned char sequence = 0;
	int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
	int loopcount = 1, infinite = 1;
	int use_poll = 0;
	int extended = 0;
	int nbytes;
	int opt;
	int receive = 0;
	int sequence_init = 1;
	int verbose = 0, quit = 0;
	int exit_value = EXIT_SUCCESS;

	signal(SIGTERM, sigterm);
	signal(SIGHUP, sigterm);

	struct option long_options[] = {
		{ "extended",	no_argument,		0, 'e' },
		{ "help",	no_argument,		0, 'h' },
		{ "poll",	no_argument,		0, 'p' },
		{ "quit",	no_argument,		0, 'q' },
		{ "receive",	no_argument,		0, 'r' },
		{ "verbose",	no_argument,		0, 'v' },
		{ "version",	no_argument,		0, VERSION_OPTION},
		{ "identifier",	required_argument,	0, 'i' },
		{ "loop",	required_argument,	0, 'l' },
		{ 0,		0,			0, 0},
	};

	while ((opt = getopt_long(argc, argv, "ehpqrvi:l:", long_options, NULL)) != -1) {
		switch (opt) {
		case 'e':
			extended = 1;
			break;

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

		case 'p':
			use_poll = 1;
			break;

		case 'q':
			quit = 1;
			break;

		case 'r':
			receive = 1;
			break;

		case 'v':
			verbose++;
			break;

		case VERSION_OPTION:
			printf("cansequence %s\n", VERSION);
			exit(EXIT_SUCCESS);
			break;

		case 'l':
			if (optarg) {
				loopcount = strtoul(optarg, NULL, 0);
				infinite = 0;
			} else
				infinite = 1;
			break;

		case 'i':
			filter->can_id = strtoul(optarg, NULL, 0);
			break;


		default:
			fprintf(stderr, "Unknown option %c\n", opt);
			break;
		}
	}

	if (argv[optind] != NULL)
		interface = argv[optind];

	if (extended) {
		filter->can_mask = CAN_EFF_MASK;
		filter->can_id  &= CAN_EFF_MASK;
		filter->can_id  |= CAN_EFF_FLAG;
	} else {
		filter->can_mask = CAN_SFF_MASK;
		filter->can_id  &= CAN_SFF_MASK;
	}
	frame.can_id = filter->can_id;

	printf("interface = %s, family = %d, type = %d, proto = %d\n",
	       interface, family, type, proto);

	s = socket(family, type, proto);
	if (s < 0) {
		perror("socket");
		return 1;
	}

	addr.can_family = family;
	strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
	if (ioctl(s, SIOCGIFINDEX, &ifr)) {
		perror("ioctl");
		return 1;
	}
	addr.can_ifindex = ifr.ifr_ifindex;

	/* first don't recv. any msgs */
	if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0)) {
		perror("setsockopt");
		exit(EXIT_FAILURE);
	}

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

	if (receive) {
		/* enable recv. now */
		if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter, sizeof(filter))) {
			perror("setsockopt");
			exit(EXIT_FAILURE);
		}

		while ((infinite || loopcount--) && running) {
			nbytes = read(s, &frame, sizeof(struct can_frame));
			if (nbytes < 0) {
				perror("read");
				return 1;
			}

			if (sequence_init) {
				sequence_init = 0;
				sequence = frame.data[0];
			}

			if (verbose > 1)
				printf("received frame. sequence number: %d\n", frame.data[0]);

			if (frame.data[0] != sequence) {
				printf("received wrong sequence count. expected: %d, got: %d\n",
				       sequence, frame.data[0]);
				if (quit) {
					exit_value = EXIT_FAILURE;
					break;
				}
				sequence = frame.data[0];
			}

			sequence++;
			if (verbose && !sequence)
				printf("sequence wrap around\n");

		}
	} else {
		while ((infinite || loopcount--) && running) {
			ssize_t len;

			if (verbose > 1)
				printf("sending frame. sequence number: %d\n", sequence);

		again:
			len = write(s, &frame, sizeof(frame));
			if (len == -1) {
				switch (errno) {
				case ENOBUFS: {
					int err;
					struct pollfd fds[] = {
						{
							.fd	= s,
							.events	= POLLOUT,
						},
					};

					if (!use_poll) {
						perror("write");
						exit(EXIT_FAILURE);
					}

					err = poll(fds, 1, 1000);
					if (err == -1 && errno != -EINTR) {
						perror("poll()");
						exit(EXIT_FAILURE);
					}
				}
				case EINTR:	/* fallthrough */
					goto again;
				default:
					perror("write");
					exit(EXIT_FAILURE);
				}
			}

			(unsigned char)frame.data[0]++;
			sequence++;

			if (verbose && !sequence)
				printf("sequence wrap around\n");
		}
	}

	exit(exit_value);
}