summaryrefslogtreecommitdiff
path: root/vw-nm.c
blob: 689cf694db56450e12bd639c60d77b4636bb1d0c (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
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
/*
 * Copyright 2015-2016 Max Staudt
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License 2 as published
 * by the Free Software Foundation.
 */

#include <assert.h>

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <endian.h>
#include <sys/time.h>


#include "vw-nm.h"
#include "vw-nm-tools.h"



static int nm_is_rx_frame_valid(struct NM_Main *nm, struct can_frame *frame)
{
	if (frame->can_dlc < 2) {
		printf("Skipping short frame from CAN ID %03x\n", frame->can_id);
		return 0;
	}

	if ((frame->can_id & ~(nm->max_nodes - 1)) != nm->can_base) {
		printf("Skipping non-NM from CAN ID %03x\n", frame->can_id);
		return 0;
	}

	return 1;
}




static void nm_update_my_next_id(struct NM_Main *nm) {
	unsigned id = nm->my_id;

	do {
		NM_State state;

		id++;
		if (id >= nm->max_nodes) {
			id = 0;
		}

		state = nm->nodes[id].state & NM_MAIN_MASK;

		if (state == NM_MAIN_ON || state == NM_MAIN_LOGIN) {
			/* TODO: Check for limp home nodes? */
			nm->nodes[nm->my_id].next = id;
			break;
		}
	} while (id != nm->my_id);
}



static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
{
	NM_ID sender, next;
	NM_State state;

	/* Is this a valid frame within our logical network? */
	if (!nm_is_rx_frame_valid(nm, frame)) {
		return;
	}

	printf("Received NM frame from CAN ID %03x\n", frame->can_id);


	/* Parse sender, its perceived successor, and its state */
	sender = frame->can_id & (nm->max_nodes - 1);
	next = frame->data[0];
	state = frame->data[1];

	/* TODO: Validate state, it needs to be within the enum */

	/* Skip our own frames */
	if (sender == nm->my_id) {
		return;
	}

	nm->nodes[sender].next = next;
	nm->nodes[sender].state = state;

	switch (state & NM_MAIN_MASK) {
		case NM_MAIN_ON:
			if (next == nm->nodes[nm->my_id].next
				&& nm->nodes[nm->my_id].next != nm->my_id) {
				/* sender doesn't know we exist */

				nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;

				nm->tv.tv_sec = 0;
				nm->tv.tv_usec = 0;
				/* IMPORTANT: The caller needs to check for
				 * timeouts first, so no other NM frames are
				 * received until our correcting login has
				 * been sent.
				 */
			} else if (next == nm->nodes[nm->my_id].next) {
				/* where nm->nodes[nm->my_id].next == nm->my_id */

				/* TODO: Is this a case we need to handle? */

				/* It can happen when:
				 *  - our sent frames don't go anywhere
				 *  - we just logged in and immediately
				 *    afterwards another ECU sent a regular
				 *    NM frame without knowing that we exist.
				 */
			} else if (next == nm->my_id) {
				/* It's our turn.
				 * Reset the timeout so anyone we missed
				 * can send its login frame to correct us.
				 */
				nm->tv.tv_sec = 0;
				nm->tv.tv_usec = NM_USECS_MY_TURN;
			} else {
				/* We just got some random ON message.
				 * Reset the timer looking out for broken
				 * connectivity.
				 */
				nm->tv.tv_sec = 0;
				nm->tv.tv_usec = NM_USECS_OTHER_TURN;
			}
			break;
		case NM_MAIN_LOGIN:
			/* Note: sender != nm->my_id */

			printf("Handling LOGIN\n");

			nm_update_my_next_id(nm);

			/* We're not alone anymore, so let's change state. */
			nm->nodes[nm->my_id].state = NM_MAIN_ON;

			/* We don't reset the timeout when somebody logs in.
			 * Instead, we'll simply include them in the next
			 * round. */

			/* Actually, when a login is done as a correction,
			 * we do reset the timeout.
			 *
			 * TODO.
			 */
			break;
	}

	nm_dump_all(nm);
}






static void nm_buildframe(struct NM_Main *nm, struct can_frame *frame)
{
	frame->can_id = nm->can_base + nm->my_id;
	frame->can_dlc = 2;
	frame->data[0] = nm->nodes[nm->my_id].next;
	frame->data[1] = nm->nodes[nm->my_id].state;
}




static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
{
	nm->tv.tv_sec = 0;
	nm->tv.tv_usec = NM_USECS_OTHER_TURN;

	nm_buildframe(nm, frame);
}




static void nm_start(struct NM_Main *nm, struct can_frame *frame)
{
	nm->tv.tv_sec = 0;
	nm->tv.tv_usec = 50000;



	nm->nodes[nm->my_id].next = nm->my_id;
	nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;

	nm_buildframe(nm, frame);
}




static int net_init(char *ifname)
{
        int s;
	int recv_own_msgs;
	struct sockaddr_can addr;
	struct ifreq ifr;
	struct can_filter fi;

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

	/* Convert interface name to index */
	memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
		perror("SIOCGIFINDEX");
		exit(1);
	}

	/* Open the CAN interface */
	memset(&addr, 0, sizeof(addr));
	addr.can_family = AF_CAN;
	addr.can_ifindex = ifr.ifr_ifindex;
	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("bind");
		return 0;
	}

	recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
	setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
			&recv_own_msgs, sizeof(recv_own_msgs));

	/* Handle only 32 NM IDs at CAN base ID 0x420 */
	fi.can_id   = 0x420;
	fi.can_mask = 0x7E0;

        setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &fi, sizeof(struct can_filter));

	return s;
}


int main(int argc, char **argv)
{
	struct NM_Main *nm;
  	fd_set rdfs;
	int s;

	if (argc != 2) {
		printf("syntax: %s IFNAME\n", argv[0]);
		return 1;
	}

	nm = nm_alloc(5, 0x0b, 0x420);
	if (!nm) {
		printf("Out of memory allocating NM struct.\n");
		return 1;
	}

	s = net_init(argv[1]);

	/* Stir up the hornet's nest */
	if (1) {
		struct can_frame frame;

		nm_start(nm, &frame);
		can_tx(s, &frame);
	}

	while (1) {
		int retval;

		FD_ZERO(&rdfs);
		FD_SET(s, &rdfs);

		retval = select(s+1, &rdfs, NULL, NULL, &nm->tv);
		/* We currently rely on Linux timeout behavior here,
		 * i.e. the timeout now reflects the remaining time */
		if (retval < 0) {
			perror("select");
			return 1;
		} else if (!retval) {
			/* Timeout, we NEED to check this first */
			struct can_frame frame;

			nm_timeout_callback(nm, &frame);
			can_tx(s, &frame);
		} else if (FD_ISSET(s, &rdfs)) {
			struct can_frame frame;
			ssize_t ret;

			ret = read(s, &frame, sizeof(frame));
			if (ret < 0) {
				perror("recvfrom CAN socket");
				return 1;
			}

			nm_handle_can_frame(nm, &frame);
			continue;
		}
	}

	nm_free(nm);

	return 0;
}