Detect limp home state
[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         /* If we're currently stuck in Limp Home mode, and we can see
82          * someone else's messages: reset counters, reset NM, re-login.
83          */
84         if (nm->nodes[nm->my_id].state == NM_MAIN_LIMPHOME) {
85                 nm_initreset(nm);
86                 return;
87         }
88
89         nm->nodes[sender].next = next;
90         nm->nodes[sender].state = state;
91
92         /* Update our view of the world */
93         nm_update_my_next_id(nm);
94
95         switch (state & NM_MAIN_MASK) {
96                 case NM_MAIN_ON:
97                         /* We're not alone, so let's transition to ON for now.
98                          */
99                         nm->nodes[nm->my_id].state = NM_MAIN_ON;
100
101                         /* The AWOL timeout is ONLY reset on
102                          * NM_MAIN_ON messages.
103                          */
104                         nm_set_timer_awol(nm);
105
106                         if (next == nm->nodes[nm->my_id].next
107                                 && nm->nodes[nm->my_id].next != nm->my_id) {
108                                 /* Sender doesn't know we exist */
109
110                                 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
111
112                                 nm_set_timer_now(nm);
113
114                                 /* IMPORTANT: The caller needs to check for
115                                  * timeouts first, i.e. no other NM frames
116                                  * are received until our correcting login
117                                  * has been sent.
118                                  */
119                         } else if (next == nm->nodes[nm->my_id].next) {
120                                 /* where (nm->nodes[nm->my_id].next == nm->my_id) */
121
122                                 /* It can happen when:
123                                  *  - our sent frames don't go anywhere
124                                  *  - we just logged in and immediately
125                                  *    afterwards another ECU sent a regular
126                                  *    NM frame.
127                                  */
128
129                                 /* Nothing to do. */
130                         } else if (next == nm->my_id) {
131                                 /* It's our turn, do a normal timeout.
132                                  * This is a period in which anyone we missed
133                                  * can send its re-login frame to correct us.
134                                  */
135
136                                 nm_set_timer_normal(nm);
137                         } else {
138                                 /* We just received a random ON message. */
139
140                                 /* Nothing to do. */
141                         }
142                         break;
143                 case NM_MAIN_LOGIN:
144                         /* Note: sender != nm->my_id */
145
146                         /* We're not alone anymore, so let's change state. */
147                         nm->nodes[nm->my_id].state = NM_MAIN_ON;
148
149                         /* We don't reset the timeout when somebody logs in.
150                          * Instead, we'll simply include them in the next
151                          * round.
152                          */
153
154                         /* Nothing else to do. */
155                         break;
156                 case NM_MAIN_LIMPHOME:
157                         /* Nothing we can do. Poor guy. */
158                         break;
159         }
160
161         nm_dump_all(nm);
162 }
163
164
165
166
167
168
169 static void nm_buildframe(struct NM_Main *nm, struct can_frame *frame)
170 {
171         frame->can_id = nm->can_base + nm->my_id;
172         frame->can_dlc = 2;
173         frame->data[0] = nm->nodes[nm->my_id].next;
174         frame->data[1] = nm->nodes[nm->my_id].state;
175 }
176
177
178
179
180 static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
181 {
182         switch(nm->timer_reason) {
183                 case NM_TIMER_NOW:
184                         /* We're due to log in */
185                         nm_buildframe(nm, frame);
186
187                         if ((nm->nodes[nm->my_id].state & NM_MAIN_MASK)
188                                 != NM_MAIN_LOGIN) {
189
190                                 printf("BUG: TIMER_NOW expired in non-ON state %u\n",
191                                         nm->nodes[nm->my_id].state & NM_MAIN_MASK);
192                         }
193
194                         /* We're going to be ready, let's
195                          * change state (RCD 310 behavior)
196                          */
197                         nm->nodes[nm->my_id].state = NM_MAIN_ON;
198
199                         nm_set_timer_normal(nm);
200                         break;
201                 case NM_TIMER_NORMAL:
202                         /* We're due to send our own ring message */
203                         nm_buildframe(nm, frame);
204
205                         if ((nm->nodes[nm->my_id].state & NM_MAIN_MASK)
206                                 != NM_MAIN_ON) {
207
208                                 printf("BUG: TIMER_NORMAL expired in non-ON state %u\n",
209                                         nm->nodes[nm->my_id].state & NM_MAIN_MASK);
210                         }
211
212                         nm_set_timer_awol(nm);
213                         break;
214                 case NM_TIMER_AWOL:
215                         /* The network is silent because a node disappeared
216                          * or something bad happened.
217                          * Reset everything and start over.
218                          */
219                         nm_reset(nm);
220                         nm_buildframe(nm, frame);
221                         break;
222                 case NM_TIMER_LIMPHOME:
223                         printf("Limp home timer expired again :(\n");
224
225                         nm_buildframe(nm, frame);
226                         nm_set_timer_limphome(nm);
227                         break;
228         }
229 }
230
231
232
233
234 static int net_init(char *ifname)
235 {
236         int s;
237         int recv_own_msgs;
238         struct sockaddr_can addr;
239         struct ifreq ifr;
240         struct can_filter fi;
241
242         s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
243         if (s < 0) {
244                 perror("socket");
245                 exit(1);
246         }
247
248         /* Convert interface name to index */
249         memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
250         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
251         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
252                 perror("SIOCGIFINDEX");
253                 exit(1);
254         }
255
256         /* Open the CAN interface */
257         memset(&addr, 0, sizeof(addr));
258         addr.can_family = AF_CAN;
259         addr.can_ifindex = ifr.ifr_ifindex;
260         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
261                 perror("bind");
262                 return 0;
263         }
264
265         recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
266         setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
267                         &recv_own_msgs, sizeof(recv_own_msgs));
268
269         /* Handle only 32 NM IDs at CAN base ID 0x420 */
270         fi.can_id   = 0x420;
271         fi.can_mask = 0x7E0;
272
273         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &fi, sizeof(struct can_filter));
274
275         return s;
276 }
277
278
279 int main(int argc, char **argv)
280 {
281         struct NM_Main *nm;
282         fd_set rdfs;
283         int s;
284         NM_ID my_id;
285
286         if (argc != 3) {
287                 printf("syntax: %s IFNAME MY_ID\n", argv[0]);
288                 return 1;
289         }
290
291         my_id = strtoul(argv[2], NULL, 0);
292
293         nm = nm_alloc(5, my_id, 0x420);
294         if (!nm) {
295                 printf("Out of memory allocating NM struct.\n");
296                 return 1;
297         }
298
299         s = net_init(argv[1]);
300
301         while (1) {
302                 int retval;
303
304                 FD_ZERO(&rdfs);
305                 FD_SET(s, &rdfs);
306
307                 retval = select(s+1, &rdfs, NULL, NULL, &nm->tv);
308                 /* We currently rely on Linux timeout behavior here,
309                  * i.e. the timeout now reflects the remaining time */
310                 if (retval < 0) {
311                         perror("select");
312                         return 1;
313                 } else if (!retval) {
314                         /* Timeout, we NEED to check this first */
315                         struct can_frame frame;
316
317                         nm_timeout_callback(nm, &frame);
318                         can_tx(s, &frame);
319                 } else if (FD_ISSET(s, &rdfs)) {
320                         struct can_frame frame;
321                         ssize_t ret;
322
323                         ret = read(s, &frame, sizeof(frame));
324                         if (ret < 0) {
325                                 perror("recvfrom CAN socket");
326                                 return 1;
327                         }
328
329                         nm_handle_can_frame(nm, &frame);
330                         continue;
331                 }
332         }
333
334         nm_free(nm);
335         close(s);
336
337         return 0;
338 }