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"
31 static int nm_is_rx_frame_valid(struct NM_Main *nm, struct can_frame *frame)
33 if (frame->can_dlc < 2) {
34 printf("Skipping short frame from CAN ID %03x\n", frame->can_id);
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);
49 static void nm_update_my_next_id(struct NM_Main *nm) {
50 unsigned id = nm->my_id;
56 if (id >= nm->max_nodes) {
60 state = nm->nodes[id].state & NM_MAIN_MASK;
62 if (state == NM_MAIN_ON || state == NM_MAIN_LOGIN) {
63 /* TODO: Check for limp home nodes? */
64 nm->nodes[nm->my_id].next = id;
67 } while (id != nm->my_id);
72 static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
77 /* Is this a valid frame within our logical network? */
78 if (!nm_is_rx_frame_valid(nm, frame)) {
82 printf("Received NM frame from CAN ID %03x\n", frame->can_id);
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];
90 /* TODO: Validate state, it needs to be within the enum */
92 /* Skip our own frames */
93 if (sender == nm->my_id) {
97 nm->nodes[sender].next = next;
98 nm->nodes[sender].state = state;
100 switch (state & NM_MAIN_MASK) {
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 */
106 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
110 /* IMPORTANT: The caller needs to check for
111 * timeouts first, so no other NM frames are
112 * received until our correcting login has
115 } else if (next == nm->nodes[nm->my_id].next) {
116 /* where nm->nodes[nm->my_id].next == nm->my_id */
118 /* TODO: Is this a case we need to handle? */
120 /* It can happen when:
121 * - our sent frames don't go anywhere
122 * - we just logged in and immediately
123 * afterwards another ECU sent a regular
124 * NM frame without knowing that we exist.
126 } else if (next == nm->my_id) {
128 * Reset the timeout so anyone we missed
129 * can send its login frame to correct us.
132 nm->tv.tv_usec = NM_USECS_MY_TURN;
134 /* We just got some random ON message.
135 * Reset the timer looking out for broken
139 nm->tv.tv_usec = NM_USECS_LIMP_HOME;
143 /* Note: sender != nm->my_id */
145 printf("Handling LOGIN\n");
147 nm_update_my_next_id(nm);
149 /* We're not alone anymore, so let's change state. */
150 nm->nodes[nm->my_id].state = NM_MAIN_ON;
152 /* We don't reset the timeout when somebody logs in.
153 * Instead, we'll simply include them in the next
156 /* Actually, when a login is done as a correction,
157 * we do reset the timeout.
172 static void nm_buildframe(struct NM_Main *nm, struct can_frame *frame)
174 frame->can_id = nm->can_base + nm->my_id;
176 frame->data[0] = nm->nodes[nm->my_id].next;
177 frame->data[1] = nm->nodes[nm->my_id].state;
183 static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
186 nm->tv.tv_usec = NM_USECS_LIMP_HOME;
188 nm_buildframe(nm, frame);
194 static void nm_start(struct NM_Main *nm, struct can_frame *frame)
197 nm->tv.tv_usec = 50000;
201 nm->nodes[nm->my_id].next = nm->my_id;
202 nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
204 nm_buildframe(nm, frame);
210 static int net_init(char *ifname)
214 struct sockaddr_can addr;
216 struct can_filter fi;
218 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
224 /* Convert interface name to index */
225 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
226 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
227 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
228 perror("SIOCGIFINDEX");
232 /* Open the CAN interface */
233 memset(&addr, 0, sizeof(addr));
234 addr.can_family = AF_CAN;
235 addr.can_ifindex = ifr.ifr_ifindex;
236 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
241 recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
242 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
243 &recv_own_msgs, sizeof(recv_own_msgs));
245 /* Handle only 32 NM IDs at CAN base ID 0x420 */
249 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &fi, sizeof(struct can_filter));
255 int main(int argc, char **argv)
262 printf("syntax: %s IFNAME\n", argv[0]);
266 nm = nm_alloc(5, 0x0b, 0x420);
268 printf("Out of memory allocating NM struct.\n");
272 s = net_init(argv[1]);
274 /* Stir up the hornet's nest */
276 struct can_frame frame;
278 nm_start(nm, &frame);
288 retval = select(s+1, &rdfs, NULL, NULL, &nm->tv);
289 /* We currently rely on Linux timeout behavior here,
290 * i.e. the timeout now reflects the remaining time */
294 } else if (!retval) {
295 /* Timeout, we NEED to check this first */
296 struct can_frame frame;
298 nm_timeout_callback(nm, &frame);
300 } else if (FD_ISSET(s, &rdfs)) {
301 struct can_frame frame;
304 ret = read(s, &frame, sizeof(frame));
306 perror("recvfrom CAN socket");
310 nm_handle_can_frame(nm, &frame);