Rework timers
authornorly <ny-git@enpas.org>
Sat, 18 Mar 2017 22:26:56 +0000 (23:26 +0100)
committernorly <ny-git@enpas.org>
Sat, 18 Mar 2017 22:27:37 +0000 (23:27 +0100)
vw-nm.c
vw-nm.h

diff --git a/vw-nm.c b/vw-nm.c
index 9ca640044b493c0f010be7c412b4a4d7e483763d..6a3ca05bb311b1aeaff6f02159f055cced754a8b 100644 (file)
--- a/vw-nm.c
+++ b/vw-nm.c
@@ -45,6 +45,32 @@ static int nm_is_rx_frame_valid(struct NM_Main *nm, struct can_frame *frame)
 
 
 
+static void nm_set_timer_now(struct NM_Main *nm) {
+       nm->tv.tv_sec = 0;
+       nm->tv.tv_usec = 0;
+       nm->timer_reason = NM_TIMER_NOW;
+}
+
+static void nm_set_timer_normal(struct NM_Main *nm) {
+       nm->tv.tv_sec = 0;
+       nm->tv.tv_usec = NM_USECS_NORMAL_TURN;
+       nm->timer_reason = NM_TIMER_NORMAL;
+}
+
+static void nm_set_timer_awol(struct NM_Main *nm) {
+       nm->tv.tv_sec = 0;
+       nm->tv.tv_usec = NM_USECS_NODE_AWOL;
+       nm->timer_reason = NM_TIMER_AWOL;
+}
+
+/*
+static void nm_set_timer_limphome(struct NM_Main *nm) {
+       nm->tv.tv_sec = 0;
+       nm->tv.tv_usec = NM_USECS_LIMPHOME;
+       nm->timer_reason = NM_TIMER_LIMPHOME;
+}
+*/
+
 
 static void nm_update_my_next_id(struct NM_Main *nm) {
        unsigned id = nm->my_id;
@@ -114,8 +140,8 @@ static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
 
                                nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
 
-                               nm->tv.tv_sec = 0;
-                               nm->tv.tv_usec = 0;
+                               nm_set_timer_now(nm);
+
                                /* IMPORTANT: The caller needs to check for
                                 * timeouts first, i.e. no other NM frames
                                 * are received until our correcting login
@@ -143,15 +169,13 @@ static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
                                 * Reset the timeout so anyone we missed
                                 * can send its login frame to correct us.
                                 */
-                               nm->tv.tv_sec = 0;
-                               nm->tv.tv_usec = NM_USECS_NORMAL_TURN;
+                               nm_set_timer_normal(nm);
                        } else {
                                /* We just got some random ON message.
                                 * Reset the timer looking out for broken
                                 * connectivity.
                                 */
-                               nm->tv.tv_sec = 0;
-                               nm->tv.tv_usec = NM_USECS_NODE_AWOL;
+                               nm_set_timer_awol(nm);
                        }
                        break;
                case NM_MAIN_LOGIN:
@@ -171,8 +195,7 @@ static void nm_handle_can_frame(struct NM_Main *nm, struct can_frame *frame)
                         * we do reset the timeout.
                         */
                        if (next != sender) {
-                               nm->tv.tv_sec = 0;
-                               nm->tv.tv_usec = NM_USECS_NODE_AWOL;
+                               nm_set_timer_awol(nm);
                        }
                        break;
                case NM_MAIN_LIMPHOME:
@@ -200,8 +223,7 @@ static void nm_buildframe(struct NM_Main *nm, struct can_frame *frame)
 
 static void nm_timeout_callback(struct NM_Main *nm, struct can_frame *frame)
 {
-       nm->tv.tv_sec = 0;
-       nm->tv.tv_usec = NM_USECS_NODE_AWOL;
+       nm_set_timer_awol(nm);
 
        nm_buildframe(nm, frame);
 }
diff --git a/vw-nm.h b/vw-nm.h
index 4a85f2a26067355006b19b63d98ecc80c2dd3646..2c38e72af82ff3101d7a1ccc5f0e62f28057bb9c 100644 (file)
--- a/vw-nm.h
+++ b/vw-nm.h
@@ -27,12 +27,22 @@ struct NM_Node {
        NM_State state;
 };
 
+
+enum timer_reason {
+       NM_TIMER_NOW,
+       NM_TIMER_NORMAL,
+       NM_TIMER_AWOL,
+       NM_TIMER_LIMPHOME,
+};
+
 struct NM_Main {
        unsigned max_nodes;
        struct NM_Node *nodes;
        NM_ID my_id;
        canid_t can_base;
+
        struct timeval tv;
+       enum timer_reason timer_reason;
 };
 
 
@@ -61,7 +71,7 @@ struct NM_Main {
 /* This timeout is 500 ms in:
  *  - 0x19 (RCD 310, Bosch)
  */
-#define NM_USECS_LIMP_HOME 500000
+#define NM_USECS_LIMPHOME 500000
 
 
 #endif /* __VW_NM_H__ */