From a0cbeb736750105b630ae54b599adac9b21f0e56 Mon Sep 17 00:00:00 2001 From: Richard Cochran Date: Sat, 8 Apr 2017 20:17:49 +0200 Subject: [PATCH] tsproc: Clarify the internal mode handling. The time stamp processor features four modes resulting from the combination of two independent Boolean options. Even though we have a proper enumerated type to represent the mode, still the code coverts the mode into two variables. The tests on the variables are currently simple enough, but soon we will want to add some more complexity. In the interest of clarity, this patch converts the paired Boolean tests into a switch/case pattern. No functional change. Signed-off-by: Richard Cochran --- tsproc.c | 64 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/tsproc.c b/tsproc.c index 1eb24a1..550f32c 100644 --- a/tsproc.c +++ b/tsproc.c @@ -26,8 +26,7 @@ struct tsproc { /* Processing options */ - int raw_mode; - int weighting; + enum tsproc_mode mode; /* Current ratio between remote and local clock frequency */ double clock_rate_ratio; @@ -48,6 +47,19 @@ struct tsproc { struct filter *delay_filter; }; +static int weighting(struct tsproc *tsp) +{ + switch (tsp->mode) { + case TSPROC_FILTER: + case TSPROC_RAW: + return 0; + case TSPROC_FILTER_WEIGHT: + case TSPROC_RAW_WEIGHT: + return 1; + } + return 0; +} + struct tsproc *tsproc_create(enum tsproc_mode mode, enum filter_type delay_filter, int filter_length) { @@ -59,20 +71,10 @@ struct tsproc *tsproc_create(enum tsproc_mode mode, switch (mode) { case TSPROC_FILTER: - tsp->raw_mode = 0; - tsp->weighting = 0; - break; case TSPROC_RAW: - tsp->raw_mode = 1; - tsp->weighting = 0; - break; case TSPROC_FILTER_WEIGHT: - tsp->raw_mode = 0; - tsp->weighting = 1; - break; case TSPROC_RAW_WEIGHT: - tsp->raw_mode = 1; - tsp->weighting = 1; + tsp->mode = mode; break; default: free(tsp); @@ -156,24 +158,46 @@ int tsproc_update_delay(struct tsproc *tsp, tmv_t *delay) pr_debug("delay filtered %10" PRId64 " raw %10" PRId64, tsp->filtered_delay, raw_delay); - if (delay) - *delay = tsp->raw_mode ? raw_delay : tsp->filtered_delay; + if (!delay) { + return 0; + } + + switch (tsp->mode) { + case TSPROC_FILTER: + case TSPROC_FILTER_WEIGHT: + *delay = tsp->filtered_delay; + break; + case TSPROC_RAW: + case TSPROC_RAW_WEIGHT: + *delay = raw_delay; + break; + } return 0; } int tsproc_update_offset(struct tsproc *tsp, tmv_t *offset, double *weight) { - tmv_t delay, raw_delay = 0; + tmv_t delay = 0, raw_delay = 0; if (tmv_is_zero(tsp->t1) || tmv_is_zero(tsp->t2) || tmv_is_zero(tsp->t3)) return -1; - if (tsp->raw_mode || tsp->weighting) + switch (tsp->mode) { + case TSPROC_FILTER: + delay = tsp->filtered_delay; + break; + case TSPROC_RAW: + case TSPROC_RAW_WEIGHT: raw_delay = get_raw_delay(tsp); - - delay = tsp->raw_mode ? raw_delay : tsp->filtered_delay; + delay = raw_delay; + break; + case TSPROC_FILTER_WEIGHT: + raw_delay = get_raw_delay(tsp); + delay = tsp->filtered_delay; + break; + } /* offset = t2 - t1 - delay */ *offset = tmv_sub(tmv_sub(tsp->t2, tsp->t1), delay); @@ -181,7 +205,7 @@ int tsproc_update_offset(struct tsproc *tsp, tmv_t *offset, double *weight) if (!weight) return 0; - if (tsp->weighting && tsp->filtered_delay > 0 && raw_delay > 0) { + if (weighting(tsp) && tsp->filtered_delay > 0 && raw_delay > 0) { *weight = (double)tsp->filtered_delay / raw_delay; if (*weight > 1.0) *weight = 1.0;