2 * Copyright 2015-2016 Max Staudt
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.
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <linux/can.h>
19 #include <linux/can/raw.h>
21 #include <sys/ioctl.h>
27 #include "vw-nm-tools.h"
33 static void nm_update_my_next_id(struct NM_Main *nm) {
34 unsigned id = nm->my_id;
40 if (id >= nm->max_nodes) {
44 state = nm->nodes[id].state & NM_MAIN_MASK;
46 if (state == NM_MAIN_ON || state == NM_MAIN_LOGIN) {
47 /* We skip limp home nodes */
48 nm->nodes[nm->my_id].next = id;
51 } while (id != nm->my_id);
56 static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
61 /* Is this a valid frame within our logical network? */
62 if (!nm_is_rx_frame_valid(nm, frame)) {
66 printf("Received NM frame from CAN ID %03x\n", frame->can_id);
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];
74 /* TODO: Validate state, it needs to be within the enum */
76 /* Skip our own frames */
77 if (sender == nm->my_id) {
81 /* If we're currently stuck in Limp Home mode, and we can see
82 * someone else's messages, reset and re-login.
84 if (nm->nodes[nm->my_id].state == NM_MAIN_LIMPHOME) {
89 nm->nodes[sender].next = next;
90 nm->nodes[sender].state = state;
92 /* Update our view of the world */
93 nm_update_my_next_id(nm);
95 switch (state & NM_MAIN_MASK) {
97 /* We're not alone, so let's transition to ON for now.
99 nm->nodes[nm->my_id].state = NM_MAIN_ON;
101 /* The AWOL timeout is ONLY reset on
102 * NM_MAIN_ON messages.
104 nm_set_timer_awol(nm);
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 */
110 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
112 nm_set_timer_now(nm);
114 /* IMPORTANT: The caller needs to check for
115 * timeouts first, i.e. no other NM frames
116 * are received until our correcting login
119 } else if (next == nm->nodes[nm->my_id].next) {
120 /* where (nm->nodes[nm->my_id].next == nm->my_id) */
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
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.
136 nm_set_timer_normal(nm);
138 /* We just received a random ON message. */
144 /* Note: sender != nm->my_id */
146 /* We're not alone anymore, so let's change state. */
147 nm->nodes[nm->my_id].state = NM_MAIN_ON;
149 /* We don't reset the timeout when somebody logs in.
150 * Instead, we'll simply include them in the next
154 /* Nothing else to do. */
156 case NM_MAIN_LIMPHOME:
157 /* Nothing we can do. Poor guy. */
169 static void nm_buildframe(struct NM_Main *nm, struct can_frame *frame)
171 frame->can_id = nm->can_base + nm->my_id;
173 frame->data[0] = nm->nodes[nm->my_id].next;
174 frame->data[1] = nm->nodes[nm->my_id].state;
180 static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
182 nm_buildframe(nm, frame);
184 switch(nm->timer_reason) {
186 /* We're due to log in */
188 /* We're going to be ready, let's
189 * change state (RCD 310 behavior)
191 nm->nodes[nm->my_id].state = NM_MAIN_ON;
192 nm_set_timer_normal(nm);
194 case NM_TIMER_NORMAL:
195 /* We're due to send our own ring message */
196 switch(nm->nodes[nm->my_id].state & NM_MAIN_MASK) {
200 printf("BUG: TIMER_NORMAL expired in non-ON state\n");
203 nm_set_timer_awol(nm);
206 /* The network is silent because a node disappeared
207 * or something bad happened.
208 * Reset everything and start over.
212 case NM_TIMER_LIMPHOME:
213 nm_set_timer_limphome(nm);
221 static int net_init(char *ifname)
225 struct sockaddr_can addr;
227 struct can_filter fi;
229 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
235 /* Convert interface name to index */
236 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
237 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
238 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
239 perror("SIOCGIFINDEX");
243 /* Open the CAN interface */
244 memset(&addr, 0, sizeof(addr));
245 addr.can_family = AF_CAN;
246 addr.can_ifindex = ifr.ifr_ifindex;
247 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
252 recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
253 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
254 &recv_own_msgs, sizeof(recv_own_msgs));
256 /* Handle only 32 NM IDs at CAN base ID 0x420 */
260 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &fi, sizeof(struct can_filter));
266 int main(int argc, char **argv)
274 printf("syntax: %s IFNAME MY_ID\n", argv[0]);
278 my_id = strtoul(argv[2], NULL, 0);
280 nm = nm_alloc(5, my_id, 0x420);
282 printf("Out of memory allocating NM struct.\n");
286 s = net_init(argv[1]);
294 retval = select(s+1, &rdfs, NULL, NULL, &nm->tv);
295 /* We currently rely on Linux timeout behavior here,
296 * i.e. the timeout now reflects the remaining time */
300 } else if (!retval) {
301 /* Timeout, we NEED to check this first */
302 struct can_frame frame;
304 nm_timeout_callback(nm, &frame);
306 } else if (FD_ISSET(s, &rdfs)) {
307 struct can_frame frame;
310 ret = read(s, &frame, sizeof(frame));
312 perror("recvfrom CAN socket");
316 nm_handle_can_frame(nm, &frame);