5c15071e3b7b47727bf988ce64d4bbed0b874d9b
[revag-nm.git] / vw-nm.c
1 /*
2  * Copyright 2015-2016 Max Staudt
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License 2 as published
6  * by the Free Software Foundation.
7  */
8
9 #include <assert.h>
10
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <linux/can.h>
19 #include <linux/can/raw.h>
20 #include <net/if.h>
21 #include <sys/ioctl.h>
22 #include <endian.h>
23 #include <sys/time.h>
24
25
26 #include "vw-nm.h"
27 #include "vw-nm-tools.h"
28
29
30
31
32
33 static void nm_update_my_next_id(struct NM_Main *nm) {
34         unsigned id = nm->my_id;
35
36         do {
37                 NM_State state;
38
39                 id++;
40                 if (id >= nm->max_nodes) {
41                         id = 0;
42                 }
43
44                 state = nm->nodes[id].state & NM_MAIN_MASK;
45
46                 if (state == NM_MAIN_ON || state == NM_MAIN_LOGIN) {
47                         /* We skip limp home nodes */
48                         nm->nodes[nm->my_id].next = id;
49                         break;
50                 }
51         } while (id != nm->my_id);
52 }
53
54
55
56 static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
57 {
58         NM_ID sender, next;
59         NM_State state;
60
61         /* Is this a valid frame within our logical network? */
62         if (!nm_is_rx_frame_valid(nm, frame)) {
63                 return;
64         }
65
66         printf("Received NM frame from CAN ID %03x\n", frame->can_id);
67
68
69         /* Parse sender, its perceived successor, and its state */
70         sender = frame->can_id & (nm->max_nodes - 1);
71         next = frame->data[0];
72         state = frame->data[1];
73
74         /* TODO: Validate state, it needs to be within the enum */
75
76         /* Skip our own frames */
77         if (sender == nm->my_id) {
78                 return;
79         }
80
81         nm->nodes[sender].next = next;
82         nm->nodes[sender].state = state;
83
84         /* Update our view of the world */
85         nm_update_my_next_id(nm);
86
87         switch (state & NM_MAIN_MASK) {
88                 case NM_MAIN_ON:
89                         /* We're not alone, so let's transition to ON for now.
90                          */
91                         nm->nodes[nm->my_id].state = NM_MAIN_ON;
92
93                         /* The AWOL timeout is ONLY reset on
94                          * NM_MAIN_ON messages.
95                          */
96                         nm_set_timer_awol(nm);
97
98                         if (next == nm->nodes[nm->my_id].next
99                                 && nm->nodes[nm->my_id].next != nm->my_id) {
100                                 /* Sender doesn't know we exist */
101
102                                 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
103
104                                 nm_set_timer_now(nm);
105
106                                 /* IMPORTANT: The caller needs to check for
107                                  * timeouts first, i.e. no other NM frames
108                                  * are received until our correcting login
109                                  * has been sent.
110                                  */
111                         } else if (next == nm->nodes[nm->my_id].next) {
112                                 /* where (nm->nodes[nm->my_id].next == nm->my_id) */
113
114                                 /* It can happen when:
115                                  *  - our sent frames don't go anywhere
116                                  *  - we just logged in and immediately
117                                  *    afterwards another ECU sent a regular
118                                  *    NM frame.
119                                  */
120
121                                 /* Nothing to do. */
122                         } else if (next == nm->my_id) {
123                                 /* It's our turn, do a normal timeout.
124                                  * This is a period in which anyone we missed
125                                  * can send its re-login frame to correct us.
126                                  */
127
128                                 nm_set_timer_normal(nm);
129                         } else {
130                                 /* We just received a random ON message. */
131
132                                 /* Nothing to do. */
133                         }
134                         break;
135                 case NM_MAIN_LOGIN:
136                         /* Note: sender != nm->my_id */
137
138                         /* We're not alone anymore, so let's change state. */
139                         nm->nodes[nm->my_id].state = NM_MAIN_ON;
140
141                         /* We don't reset the timeout when somebody logs in.
142                          * Instead, we'll simply include them in the next
143                          * round.
144                          */
145
146                         /* Nothing else to do. */
147                         break;
148                 case NM_MAIN_LIMPHOME:
149                         break;
150         }
151
152         nm_dump_all(nm);
153 }
154
155
156
157
158
159
160 static void nm_buildframe(struct NM_Main *nm, struct can_frame *frame)
161 {
162         frame->can_id = nm->can_base + nm->my_id;
163         frame->can_dlc = 2;
164         frame->data[0] = nm->nodes[nm->my_id].next;
165         frame->data[1] = nm->nodes[nm->my_id].state;
166 }
167
168
169
170
171 static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
172 {
173         switch(nm->timer_reason) {
174                 case NM_TIMER_NOW:
175                 case NM_TIMER_NORMAL:
176                         /* We're due to send our own ring message */
177                         switch(nm->nodes[nm->my_id].state & NM_MAIN_MASK) {
178                                 case NM_MAIN_ON:
179                                         break;
180                                 case NM_MAIN_LOGIN:
181                                         /* We're going to be ready, let's
182                                          * change state (RCD 310 behavior)
183                                          */
184                                         nm->nodes[nm->my_id].state = NM_MAIN_ON;
185                                         break;
186                                 default:
187                                         printf("BUG: TIMER_NORMAL expired in unknown NM_MAIN state\n");
188                                         break;
189                         }
190                         nm_set_timer_awol(nm);
191                         nm_buildframe(nm, frame);
192                         break;
193                 case NM_TIMER_AWOL:
194                         /* The network is silent because a node disappeared
195                          * or something bad happened.
196                          * Reset everything and start over.
197                          */
198                         nm_reset(nm);
199                         break;
200                 case NM_TIMER_LIMPHOME:
201                         /* TODO */
202                         break;
203         }
204
205         nm_buildframe(nm, frame);
206 }
207
208
209
210
211 static int net_init(char *ifname)
212 {
213         int s;
214         int recv_own_msgs;
215         struct sockaddr_can addr;
216         struct ifreq ifr;
217         struct can_filter fi;
218
219         s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
220         if (s < 0) {
221                 perror("socket");
222                 exit(1);
223         }
224
225         /* Convert interface name to index */
226         memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
227         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
228         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
229                 perror("SIOCGIFINDEX");
230                 exit(1);
231         }
232
233         /* Open the CAN interface */
234         memset(&addr, 0, sizeof(addr));
235         addr.can_family = AF_CAN;
236         addr.can_ifindex = ifr.ifr_ifindex;
237         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
238                 perror("bind");
239                 return 0;
240         }
241
242         recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
243         setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
244                         &recv_own_msgs, sizeof(recv_own_msgs));
245
246         /* Handle only 32 NM IDs at CAN base ID 0x420 */
247         fi.can_id   = 0x420;
248         fi.can_mask = 0x7E0;
249
250         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &fi, sizeof(struct can_filter));
251
252         return s;
253 }
254
255
256 int main(int argc, char **argv)
257 {
258         struct NM_Main *nm;
259         fd_set rdfs;
260         int s;
261         NM_ID my_id;
262
263         if (argc != 3) {
264                 printf("syntax: %s IFNAME MY_ID\n", argv[0]);
265                 return 1;
266         }
267
268         my_id = strtoul(argv[2], NULL, 0);
269
270         nm = nm_alloc(5, my_id, 0x420);
271         if (!nm) {
272                 printf("Out of memory allocating NM struct.\n");
273                 return 1;
274         }
275
276         s = net_init(argv[1]);
277
278         /* Stir up the hornet's nest */
279         if (1) {
280                 struct can_frame frame;
281
282                 nm_buildframe(nm, &frame);
283                 can_tx(s, &frame);
284         }
285
286         while (1) {
287                 int retval;
288
289                 FD_ZERO(&rdfs);
290                 FD_SET(s, &rdfs);
291
292                 retval = select(s+1, &rdfs, NULL, NULL, &nm->tv);
293                 /* We currently rely on Linux timeout behavior here,
294                  * i.e. the timeout now reflects the remaining time */
295                 if (retval < 0) {
296                         perror("select");
297                         return 1;
298                 } else if (!retval) {
299                         /* Timeout, we NEED to check this first */
300                         struct can_frame frame;
301
302                         nm_timeout_callback(nm, &frame);
303                         can_tx(s, &frame);
304                 } else if (FD_ISSET(s, &rdfs)) {
305                         struct can_frame frame;
306                         ssize_t ret;
307
308                         ret = read(s, &frame, sizeof(frame));
309                         if (ret < 0) {
310                                 perror("recvfrom CAN socket");
311                                 return 1;
312                         }
313
314                         nm_handle_can_frame(nm, &frame);
315                         continue;
316                 }
317         }
318
319         nm_free(nm);
320         close(s);
321
322         return 0;
323 }