state = nm->nodes[id].state & NM_MAIN_MASK;
if (state == NM_MAIN_ON || state == NM_MAIN_LOGIN) {
- /* TODO: Check for limp home nodes? */
+ /* We skip limp home nodes */
nm->nodes[nm->my_id].next = id;
break;
}
} while (id != nm->my_id);
+
+ if (nm->nodes[nm->my_id].next == nm->my_id) {
+ /* Uh oh, we're the only one left. */
+
+ /* TODO */
+ nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
+
+ /* TODO: Timeout 140ms (RCD 310) */
+ }
}
nm->tv.tv_sec = 0;
nm->tv.tv_usec = 0;
/* IMPORTANT: The caller needs to check for
- * timeouts first, so no other NM frames are
- * received until our correcting login has
- * been sent.
+ * timeouts first, i.e. no other NM frames
+ * are received until our correcting login
+ * has been sent.
*/
} else if (next == nm->nodes[nm->my_id].next) {
- /* where nm->nodes[nm->my_id].next == nm->my_id */
+ /* where (nm->nodes[nm->my_id].next == nm->my_id) */
/* It can happen when:
* - our sent frames don't go anywhere
* connectivity.
*/
nm->tv.tv_sec = 0;
- nm->tv.tv_usec = NM_USECS_OTHER_TURN;
+ nm->tv.tv_usec = NM_USECS_NODE_AWOL;
}
break;
case NM_MAIN_LOGIN:
/* We're not alone anymore, so let's change state. */
nm->nodes[nm->my_id].state = NM_MAIN_ON;
- /* We don't reset the timeout when somebody logs in.
+ /* We don't reset the timeout when somebody logs in,
+ * i.e. (next == sender).
* Instead, we'll simply include them in the next
* round. */
/* Actually, when a login is done as a correction,
* we do reset the timeout.
- *
- * TODO.
*/
+ if (next != sender) {
+ nm->tv.tv_sec = 0;
+ nm->tv.tv_usec = NM_USECS_NODE_AWOL;
+ }
break;
+ case NM_MAIN_LIMPHOME:
+ nm_update_my_next_id(nm);
}
nm_dump_all(nm);
static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
{
nm->tv.tv_sec = 0;
- nm->tv.tv_usec = NM_USECS_OTHER_TURN;
+ nm->tv.tv_usec = NM_USECS_NODE_AWOL;
nm_buildframe(nm, frame);
}
static void nm_start(struct NM_Main *nm, struct can_frame *frame)
{
nm->tv.tv_sec = 0;
- nm->tv.tv_usec = 50000;
+ nm->tv.tv_usec = NM_USECS_NODE_AWOL;
struct NM_Main *nm;
fd_set rdfs;
int s;
+ NM_ID my_id;
- if (argc != 2) {
- printf("syntax: %s IFNAME\n", argv[0]);
+ if (argc != 3) {
+ printf("syntax: %s IFNAME MY_ID\n", argv[0]);
return 1;
}
- nm = nm_alloc(5, 0x0b, 0x420);
+ my_id = strtoul(argv[2], NULL, 0);
+
+ nm = nm_alloc(5, my_id, 0x420);
if (!nm) {
printf("Out of memory allocating NM struct.\n");
return 1;
}
nm_free(nm);
+ close(s);
return 0;
}