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);
53 if (nm->nodes[nm->my_id].next == nm->my_id) {
54 /* Uh oh, we're the only one left. */
57 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
59 /* TODO: Timeout 140ms (RCD 310) */
65 static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
70 /* Is this a valid frame within our logical network? */
71 if (!nm_is_rx_frame_valid(nm, frame)) {
75 printf("Received NM frame from CAN ID %03x\n", frame->can_id);
78 /* Parse sender, its perceived successor, and its state */
79 sender = frame->can_id & (nm->max_nodes - 1);
80 next = frame->data[0];
81 state = frame->data[1];
83 /* TODO: Validate state, it needs to be within the enum */
85 /* Skip our own frames */
86 if (sender == nm->my_id) {
90 nm->nodes[sender].next = next;
91 nm->nodes[sender].state = state;
93 /* Update our view of the world */
94 nm_update_my_next_id(nm);
96 switch (state & NM_MAIN_MASK) {
98 /* We're not alone, so let's transition to ON for now.
100 nm->nodes[nm->my_id].state = NM_MAIN_ON;
102 /* The AWOL timeout is ONLY reset on
103 * NM_MAIN_ON messages.
105 nm_set_timer_awol(nm);
107 if (next == nm->nodes[nm->my_id].next
108 && nm->nodes[nm->my_id].next != nm->my_id) {
109 /* Sender doesn't know we exist */
111 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
113 nm_set_timer_now(nm);
115 /* IMPORTANT: The caller needs to check for
116 * timeouts first, i.e. no other NM frames
117 * are received until our correcting login
120 } else if (next == nm->nodes[nm->my_id].next) {
121 /* where (nm->nodes[nm->my_id].next == nm->my_id) */
123 /* It can happen when:
124 * - our sent frames don't go anywhere
125 * - we just logged in and immediately
126 * afterwards another ECU sent a regular
131 } else if (next == nm->my_id) {
132 /* It's our turn, do a normal timeout.
133 * This is a period in which anyone we missed
134 * can send its re-login frame to correct us.
137 nm_set_timer_normal(nm);
139 /* We just received a random ON message. */
145 /* Note: sender != nm->my_id */
147 /* We're not alone anymore, so let's change state. */
148 nm->nodes[nm->my_id].state = NM_MAIN_ON;
150 /* We don't reset the timeout when somebody logs in.
151 * Instead, we'll simply include them in the next
155 /* Nothing else to do. */
157 case NM_MAIN_LIMPHOME:
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 switch(nm->timer_reason) {
184 case NM_TIMER_NORMAL:
185 /* We're due to send our own ring message */
186 switch(nm->nodes[nm->my_id].state & NM_MAIN_MASK) {
190 /* We're going to be ready, let's
191 * change state (RCD 310 behavior)
193 nm->nodes[nm->my_id].state = NM_MAIN_ON;
196 printf("BUG: TIMER_NORMAL expired in unknown NM_MAIN state\n");
199 nm_set_timer_awol(nm);
200 nm_buildframe(nm, frame);
203 /* The network is silent because a node disappeared
204 * or something bad happened.
205 * Reset everything and start over.
209 case NM_TIMER_LIMPHOME:
214 nm_buildframe(nm, frame);
220 static int net_init(char *ifname)
224 struct sockaddr_can addr;
226 struct can_filter fi;
228 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
234 /* Convert interface name to index */
235 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
236 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
237 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
238 perror("SIOCGIFINDEX");
242 /* Open the CAN interface */
243 memset(&addr, 0, sizeof(addr));
244 addr.can_family = AF_CAN;
245 addr.can_ifindex = ifr.ifr_ifindex;
246 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
251 recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
252 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
253 &recv_own_msgs, sizeof(recv_own_msgs));
255 /* Handle only 32 NM IDs at CAN base ID 0x420 */
259 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &fi, sizeof(struct can_filter));
265 int main(int argc, char **argv)
273 printf("syntax: %s IFNAME MY_ID\n", argv[0]);
277 my_id = strtoul(argv[2], NULL, 0);
279 nm = nm_alloc(5, my_id, 0x420);
281 printf("Out of memory allocating NM struct.\n");
285 s = net_init(argv[1]);
287 /* Stir up the hornet's nest */
289 struct can_frame frame;
291 nm_buildframe(nm, &frame);
301 retval = select(s+1, &rdfs, NULL, NULL, &nm->tv);
302 /* We currently rely on Linux timeout behavior here,
303 * i.e. the timeout now reflects the remaining time */
307 } else if (!retval) {
308 /* Timeout, we NEED to check this first */
309 struct can_frame frame;
311 nm_timeout_callback(nm, &frame);
313 } else if (FD_ISSET(s, &rdfs)) {
314 struct can_frame frame;
317 ret = read(s, &frame, sizeof(frame));
319 perror("recvfrom CAN socket");
323 nm_handle_can_frame(nm, &frame);