Reset timeout on correcting re-login. Comments.
[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 static int nm_is_rx_frame_valid(struct NM_Main *nm, struct can_frame *frame)
32 {
33         if (frame->can_dlc < 2) {
34                 printf("Skipping short frame from CAN ID %03x\n", frame->can_id);
35                 return 0;
36         }
37
38         if ((frame->can_id & ~(nm->max_nodes - 1)) != nm->can_base) {
39                 printf("Skipping non-NM from CAN ID %03x\n", frame->can_id);
40                 return 0;
41         }
42
43         return 1;
44 }
45
46
47
48
49 static void nm_update_my_next_id(struct NM_Main *nm) {
50         unsigned id = nm->my_id;
51
52         do {
53                 NM_State state;
54
55                 id++;
56                 if (id >= nm->max_nodes) {
57                         id = 0;
58                 }
59
60                 state = nm->nodes[id].state & NM_MAIN_MASK;
61
62                 if (state == NM_MAIN_ON || state == NM_MAIN_LOGIN) {
63                         /* We skip limp home nodes */
64                         nm->nodes[nm->my_id].next = id;
65                         break;
66                 }
67         } while (id != nm->my_id);
68 }
69
70
71
72 static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
73 {
74         NM_ID sender, next;
75         NM_State state;
76
77         /* Is this a valid frame within our logical network? */
78         if (!nm_is_rx_frame_valid(nm, frame)) {
79                 return;
80         }
81
82         printf("Received NM frame from CAN ID %03x\n", frame->can_id);
83
84
85         /* Parse sender, its perceived successor, and its state */
86         sender = frame->can_id & (nm->max_nodes - 1);
87         next = frame->data[0];
88         state = frame->data[1];
89
90         /* TODO: Validate state, it needs to be within the enum */
91
92         /* Skip our own frames */
93         if (sender == nm->my_id) {
94                 return;
95         }
96
97         nm->nodes[sender].next = next;
98         nm->nodes[sender].state = state;
99
100         switch (state & NM_MAIN_MASK) {
101                 case NM_MAIN_ON:
102                         if (next == nm->nodes[nm->my_id].next
103                                 && nm->nodes[nm->my_id].next != nm->my_id) {
104                                 /* sender doesn't know we exist */
105
106                                 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
107
108                                 nm->tv.tv_sec = 0;
109                                 nm->tv.tv_usec = 0;
110                                 /* IMPORTANT: The caller needs to check for
111                                  * timeouts first, i.e. no other NM frames
112                                  * are received until our correcting login
113                                  * has been sent.
114                                  */
115                         } else if (next == nm->nodes[nm->my_id].next) {
116                                 /* where (nm->nodes[nm->my_id].next == nm->my_id) */
117
118                                 /* It can happen when:
119                                  *  - our sent frames don't go anywhere
120                                  *  - we just logged in and immediately
121                                  *    afterwards another ECU sent a regular
122                                  *    NM frame.
123                                  */
124
125                                 /* Let's handle this just like a LOGIN, since
126                                  * we're learning about a new device.
127                                  * See case NM_MAIN_LOGIN below for details.
128                                  */
129
130                                 nm_update_my_next_id(nm);
131                                 nm->nodes[nm->my_id].state = NM_MAIN_ON;
132                         } else if (next == nm->my_id) {
133                                 /* It's our turn.
134                                  * Reset the timeout so anyone we missed
135                                  * can send its login frame to correct us.
136                                  */
137                                 nm->tv.tv_sec = 0;
138                                 nm->tv.tv_usec = NM_USECS_MY_TURN;
139                         } else {
140                                 /* We just got some random ON message.
141                                  * Reset the timer looking out for broken
142                                  * connectivity.
143                                  */
144                                 nm->tv.tv_sec = 0;
145                                 nm->tv.tv_usec = NM_USECS_OTHER_TURN;
146                         }
147                         break;
148                 case NM_MAIN_LOGIN:
149                         /* Note: sender != nm->my_id */
150
151                         nm_update_my_next_id(nm);
152
153                         /* We're not alone anymore, so let's change state. */
154                         nm->nodes[nm->my_id].state = NM_MAIN_ON;
155
156                         /* We don't reset the timeout when somebody logs in,
157                          * i.e. (next == sender).
158                          * Instead, we'll simply include them in the next
159                          * round. */
160
161                         /* Actually, when a login is done as a correction,
162                          * we do reset the timeout.
163                          */
164                         if (next != sender) {
165                                 nm->tv.tv_sec = 0;
166                                 nm->tv.tv_usec = NM_USECS_OTHER_TURN;
167                         }
168                         break;
169         }
170
171         nm_dump_all(nm);
172 }
173
174
175
176
177
178
179 static void nm_buildframe(struct NM_Main *nm, struct can_frame *frame)
180 {
181         frame->can_id = nm->can_base + nm->my_id;
182         frame->can_dlc = 2;
183         frame->data[0] = nm->nodes[nm->my_id].next;
184         frame->data[1] = nm->nodes[nm->my_id].state;
185 }
186
187
188
189
190 static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
191 {
192         nm->tv.tv_sec = 0;
193         nm->tv.tv_usec = NM_USECS_OTHER_TURN;
194
195         nm_buildframe(nm, frame);
196 }
197
198
199
200
201 static void nm_start(struct NM_Main *nm, struct can_frame *frame)
202 {
203         nm->tv.tv_sec = 0;
204         nm->tv.tv_usec = 50000;
205
206
207
208         nm->nodes[nm->my_id].next = nm->my_id;
209         nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
210
211         nm_buildframe(nm, frame);
212 }
213
214
215
216
217 static int net_init(char *ifname)
218 {
219         int s;
220         int recv_own_msgs;
221         struct sockaddr_can addr;
222         struct ifreq ifr;
223         struct can_filter fi;
224
225         s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
226         if (s < 0) {
227                 perror("socket");
228                 exit(1);
229         }
230
231         /* Convert interface name to index */
232         memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
233         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
234         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
235                 perror("SIOCGIFINDEX");
236                 exit(1);
237         }
238
239         /* Open the CAN interface */
240         memset(&addr, 0, sizeof(addr));
241         addr.can_family = AF_CAN;
242         addr.can_ifindex = ifr.ifr_ifindex;
243         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
244                 perror("bind");
245                 return 0;
246         }
247
248         recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
249         setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
250                         &recv_own_msgs, sizeof(recv_own_msgs));
251
252         /* Handle only 32 NM IDs at CAN base ID 0x420 */
253         fi.can_id   = 0x420;
254         fi.can_mask = 0x7E0;
255
256         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &fi, sizeof(struct can_filter));
257
258         return s;
259 }
260
261
262 int main(int argc, char **argv)
263 {
264         struct NM_Main *nm;
265         fd_set rdfs;
266         int s;
267
268         if (argc != 2) {
269                 printf("syntax: %s IFNAME\n", argv[0]);
270                 return 1;
271         }
272
273         nm = nm_alloc(5, 0x0b, 0x420);
274         if (!nm) {
275                 printf("Out of memory allocating NM struct.\n");
276                 return 1;
277         }
278
279         s = net_init(argv[1]);
280
281         /* Stir up the hornet's nest */
282         if (1) {
283                 struct can_frame frame;
284
285                 nm_start(nm, &frame);
286                 can_tx(s, &frame);
287         }
288
289         while (1) {
290                 int retval;
291
292                 FD_ZERO(&rdfs);
293                 FD_SET(s, &rdfs);
294
295                 retval = select(s+1, &rdfs, NULL, NULL, &nm->tv);
296                 /* We currently rely on Linux timeout behavior here,
297                  * i.e. the timeout now reflects the remaining time */
298                 if (retval < 0) {
299                         perror("select");
300                         return 1;
301                 } else if (!retval) {
302                         /* Timeout, we NEED to check this first */
303                         struct can_frame frame;
304
305                         nm_timeout_callback(nm, &frame);
306                         can_tx(s, &frame);
307                 } else if (FD_ISSET(s, &rdfs)) {
308                         struct can_frame frame;
309                         ssize_t ret;
310
311                         ret = read(s, &frame, sizeof(frame));
312                         if (ret < 0) {
313                                 perror("recvfrom CAN socket");
314                                 return 1;
315                         }
316
317                         nm_handle_can_frame(nm, &frame);
318                         continue;
319                 }
320         }
321
322         nm_free(nm);
323
324         return 0;
325 }