aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2018-05-25 18:20:06 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2018-05-31 15:45:59 +0200
commit8ca991b64c889f6d6db7071874cd944c6246d2ba (patch)
tree45c8f6e018d52bb28cf6d3241324c09054c300f7
parent64e807c4bc81624651b970290db1c6a0b03d8eef (diff)
add osmo_fsm_inst_state_chg_keep_timer()
-rw-r--r--include/osmocom/core/fsm.h15
-rw-r--r--src/fsm.c94
-rw-r--r--tests/fsm/fsm_test.c84
-rw-r--r--tests/fsm/fsm_test.err22
4 files changed, 184 insertions, 31 deletions
diff --git a/include/osmocom/core/fsm.h b/include/osmocom/core/fsm.h
index 174396a7..67e00ad7 100644
--- a/include/osmocom/core/fsm.h
+++ b/include/osmocom/core/fsm.h
@@ -182,6 +182,21 @@ int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
unsigned long timeout_secs, int T,
const char *file, int line);
+/*! perform a state change while keeping the current timer running.
+ *
+ * This is useful to keep a timeout across several states (without having to round the
+ * remaining time to seconds).
+ *
+ * This is a macro that calls _osmo_fsm_inst_state_chg_keep_timer() with the given
+ * parameters as well as the caller's source file and line number for logging
+ * purposes. See there for documentation.
+ */
+#define osmo_fsm_inst_state_chg_keep_timer(fi, new_state) \
+ _osmo_fsm_inst_state_chg_keep_timer(fi, new_state, \
+ __BASE_FILE__, __LINE__)
+int _osmo_fsm_inst_state_chg_keep_timer(struct osmo_fsm_inst *fi, uint32_t new_state,
+ const char *file, int line);
+
/*! dispatch an event to an osmocom finite state machine instance
*
* This is a macro that calls _osmo_fsm_inst_dispatch() with the given
diff --git a/src/fsm.c b/src/fsm.c
index 0370f65e..b5af2e7a 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -429,33 +429,9 @@ const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state)
return fsm->states[state].name;
}
-/*! perform a state change of the given FSM instance
- *
- * Best invoke via the osmo_fsm_inst_state_chg() macro which logs the source
- * file where the state change was effected. Alternatively, you may pass \a
- * file as NULL to use the normal file/line indication instead.
- *
- * All changes to the FSM instance state must be made via this
- * function. It verifies that the existing state actually permits a
- * transiiton to new_state.
- *
- * timeout_secs and T are optional parameters, and only have any effect
- * if timeout_secs is not 0. If the timeout function is used, then the
- * new_state is entered, and the FSM instances timer is set to expire
- * in timeout_secs functions. At that time, the FSM's timer_cb
- * function will be called for handling of the timeout by the user.
- *
- * \param[in] fi FSM instance whose state is to change
- * \param[in] new_state The new state into which we should change
- * \param[in] timeout_secs Timeout in seconds (if !=0)
- * \param[in] T Timer number (if \ref timeout_secs != 0)
- * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro)
- * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro)
- * \returns 0 on success; negative on error
- */
-int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
- unsigned long timeout_secs, int T,
- const char *file, int line)
+static int state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
+ bool keep_timer, unsigned long timeout_secs, int T,
+ const char *file, int line)
{
struct osmo_fsm *fsm = fi->fsm;
uint32_t old_state = fi->state;
@@ -469,8 +445,10 @@ int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
return -EPERM;
}
- /* delete the old timer */
- osmo_timer_del(&fi->timer);
+ if (!keep_timer) {
+ /* delete the old timer */
+ osmo_timer_del(&fi->timer);
+ }
if (st->onleave)
st->onleave(fi, new_state);
@@ -480,7 +458,7 @@ int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
fi->state = new_state;
st = &fsm->states[new_state];
- if (timeout_secs) {
+ if (!keep_timer && timeout_secs) {
fi->T = T;
osmo_timer_schedule(&fi->timer, timeout_secs, 0);
}
@@ -492,6 +470,62 @@ int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
return 0;
}
+/*! perform a state change of the given FSM instance
+ *
+ * Best invoke via the osmo_fsm_inst_state_chg() macro which logs the source
+ * file where the state change was effected. Alternatively, you may pass \a
+ * file as NULL to use the normal file/line indication instead.
+ *
+ * All changes to the FSM instance state must be made via an osmo_fsm_inst_state_chg_*
+ * function. It verifies that the existing state actually permits a
+ * transition to new_state.
+ *
+ * timeout_secs and T are optional parameters, and only have any effect
+ * if timeout_secs is not 0. If the timeout function is used, then the
+ * new_state is entered, and the FSM instances timer is set to expire
+ * in timeout_secs functions. At that time, the FSM's timer_cb
+ * function will be called for handling of the timeout by the user.
+ *
+ * \param[in] fi FSM instance whose state is to change
+ * \param[in] new_state The new state into which we should change
+ * \param[in] timeout_secs Timeout in seconds (if !=0)
+ * \param[in] T Timer number (if \ref timeout_secs != 0)
+ * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro)
+ * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro)
+ * \returns 0 on success; negative on error
+ */
+int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
+ unsigned long timeout_secs, int T,
+ const char *file, int line)
+{
+ return state_chg(fi, new_state, false, timeout_secs, T, file, line);
+}
+
+/*! perform a state change while keeping the current timer running.
+ *
+ * This is useful to keep a timeout across several states (without having to round the
+ * remaining time to seconds).
+ *
+ * Best invoke via the osmo_fsm_inst_state_chg_keep_timer() macro which logs the source
+ * file where the state change was effected. Alternatively, you may pass \a
+ * file as NULL to use the normal file/line indication instead.
+ *
+ * All changes to the FSM instance state must be made via an osmo_fsm_inst_state_chg_*
+ * function. It verifies that the existing state actually permits a
+ * transition to new_state.
+ *
+ * \param[in] fi FSM instance whose state is to change
+ * \param[in] new_state The new state into which we should change
+ * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro)
+ * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro)
+ * \returns 0 on success; negative on error
+ */
+int _osmo_fsm_inst_state_chg_keep_timer(struct osmo_fsm_inst *fi, uint32_t new_state,
+ const char *file, int line)
+{
+ return state_chg(fi, new_state, true, 0, 0, file, line);
+}
+
/*! dispatch an event to an osmocom finite state machine instance
*
* Best invoke via the osmo_fsm_inst_dispatch() macro which logs the source
diff --git a/tests/fsm/fsm_test.c b/tests/fsm/fsm_test.c
index e34164cf..cac575c2 100644
--- a/tests/fsm/fsm_test.c
+++ b/tests/fsm/fsm_test.c
@@ -266,6 +266,89 @@ do { \
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
}
+const struct timeval fake_time_start_time = { 123, 456 };
+
+#define fake_time_passes(secs, usecs) do \
+{ \
+ struct timeval diff; \
+ osmo_gettimeofday_override_add(secs, usecs); \
+ osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \
+ timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \
+ fprintf(stderr, "Elapsing %d.%06d, total time passed: %ld.%06ld s\n", \
+ secs, usecs, diff.tv_sec, diff.tv_usec); \
+ osmo_timers_prepare(); \
+ osmo_timers_update(); \
+} while (0)
+
+void fake_time_start()
+{
+ struct timespec *clock_override;
+
+ osmo_gettimeofday_override_time = fake_time_start_time;
+ osmo_gettimeofday_override = true;
+ clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
+ OSMO_ASSERT(clock_override);
+ clock_override->tv_sec = fake_time_start_time.tv_sec;
+ clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
+ osmo_clock_override_enable(CLOCK_MONOTONIC, true);
+ fake_time_passes(0, 0);
+}
+
+static int timeout_fired = 0;
+static int timer_cb(struct osmo_fsm_inst *fi)
+{
+ timeout_fired = fi->T;
+ return 0;
+}
+
+static void test_state_chg_keep_timer()
+{
+ struct osmo_fsm_inst *fi;
+
+ fprintf(stderr, "\n--- %s()\n", __func__);
+
+ fsm.timer_cb = timer_cb;
+
+ /* Test that no timer remains no timer */
+ fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
+ OSMO_ASSERT(fi);
+
+ osmo_fsm_inst_state_chg(fi, ST_ONE, 0, 0);
+ timeout_fired = -1;
+
+ osmo_fsm_inst_state_chg_keep_timer(fi, ST_TWO);
+
+ OSMO_ASSERT(timeout_fired == -1);
+ OSMO_ASSERT(fi->T == 0);
+
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
+
+ /* Test that a set time continues with exact precision */
+ fake_time_start();
+ fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
+ OSMO_ASSERT(fi);
+
+ osmo_fsm_inst_state_chg(fi, ST_ONE, 10, 10);
+
+ timeout_fired = -1;
+
+ fake_time_passes(2, 342);
+ osmo_fsm_inst_state_chg_keep_timer(fi, ST_TWO);
+
+ fake_time_passes(0, 0);
+ OSMO_ASSERT(timeout_fired == -1);
+
+ fake_time_passes(7, 1000000 - 342 - 1);
+ OSMO_ASSERT(timeout_fired == -1);
+
+ fake_time_passes(0, 1);
+ OSMO_ASSERT(timeout_fired == 10);
+
+ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
+
+ fprintf(stderr, "--- %s() done\n", __func__);
+}
+
static const struct log_info_cat default_categories[] = {
[DMAIN] = {
.name = "DMAIN",
@@ -306,6 +389,7 @@ int main(int argc, char **argv)
osmo_fsm_inst_free(finst);
test_id_api();
+ test_state_chg_keep_timer();
osmo_fsm_unregister(&fsm);
exit(0);
diff --git a/tests/fsm/fsm_test.err b/tests/fsm/fsm_test.err
index 3237def5..85606e2a 100644
--- a/tests/fsm/fsm_test.err
+++ b/tests/fsm/fsm_test.err
@@ -80,4 +80,24 @@ osmo_fsm_inst_update_id_f("%s%c%s", "arbitrary", '_', "id")
Test_FSM(arbitrary_id){NULL}: Terminating (cause = OSMO_FSM_TERM_REQUEST)
Test_FSM(arbitrary_id){NULL}: Freeing instance
Test_FSM(arbitrary_id){NULL}: Deallocated
- \ No newline at end of file
+
+--- test_state_chg_keep_timer()
+Test_FSM{NULL}: Allocated
+Test_FSM{NULL}: state_chg to ONE
+Test_FSM{ONE}: state_chg to TWO
+Test_FSM{TWO}: Terminating (cause = OSMO_FSM_TERM_REQUEST)
+Test_FSM{TWO}: Freeing instance
+Test_FSM{TWO}: Deallocated
+Total time passed: 0.000000 s
+Test_FSM{NULL}: Allocated
+Test_FSM{NULL}: state_chg to ONE
+Total time passed: 2.000342 s
+Test_FSM{ONE}: state_chg to TWO
+Total time passed: 2.000342 s
+Total time passed: 9.999999 s
+Total time passed: 10.000000 s
+Test_FSM{TWO}: Timeout of T10
+Test_FSM{TWO}: Terminating (cause = OSMO_FSM_TERM_REQUEST)
+Test_FSM{TWO}: Freeing instance
+Test_FSM{TWO}: Deallocated
+--- test_state_chg_keep_timer() done