fixeria has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/34520?usp=email )
Change subject: gsm: add gsm0502_fn_is_greater() for comparing TDMA FNs ......................................................................
gsm: add gsm0502_fn_is_greater() for comparing TDMA FNs
We need this function in:
* osmocom-bb.git for trxcon and l1gprs, * osmo-pcu.git replacing fn_cmp().
Change-Id: I9590f2e836fc48650decf1564b6ab46306c4fe2d Related: OS#5500 --- M include/osmocom/gsm/gsm0502.h M tests/gsm0502/gsm0502_test.c 2 files changed, 49 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/20/34520/1
diff --git a/include/osmocom/gsm/gsm0502.h b/include/osmocom/gsm/gsm0502.h index ffd6e20..0d70aaf 100644 --- a/include/osmocom/gsm/gsm0502.h +++ b/include/osmocom/gsm/gsm0502.h @@ -6,6 +6,7 @@ #pragma once
#include <stdint.h> +#include <stdbool.h>
#include <osmocom/gsm/protocol/gsm_04_08.h> #include <osmocom/gsm/protocol/gsm_08_58.h> @@ -64,6 +65,20 @@ #define GSM_NBITS_AB_GMSK_TAIL GSM_NBITS_NB_GMSK_TAIL #define GSM_NBITS_AB_GMSK_BURST GSM_NBITS_NB_GMSK_BURST
+/* Check if the given TDMA Fn 'fn1' is greater than TDMA Fn 'fn2', + * taking the wrapping into account (i.e. 0 is greater than 2715647). + * Based on fn_cmp() implementation from osmo-pcu.git, simplified. */ +static inline bool gsm0502_fn_is_greater(uint32_t fn1, uint32_t fn2) +{ + const uint32_t thresh = GSM_TDMA_HYPERFRAME / 2; + + if ((fn1 < fn2 && (fn2 - fn1) < thresh) || + (fn1 > fn2 && (fn1 - fn2) > thresh)) + return false; + + return true; +} + /* Table 5 Clause 7 TS 05.02 */ static inline unsigned int gsm0502_get_n_pag_blocks(const struct gsm48_control_channel_descr *chan_desc) diff --git a/tests/gsm0502/gsm0502_test.c b/tests/gsm0502/gsm0502_test.c index e9deaa9..6390d8b 100644 --- a/tests/gsm0502/gsm0502_test.c +++ b/tests/gsm0502/gsm0502_test.c @@ -148,8 +148,27 @@ printf("\n"); }
+static void test_gsm0502_fn_is_greater(void) +{ + OSMO_ASSERT(gsm0502_fn_is_greater(42, 1337) == false); + OSMO_ASSERT(gsm0502_fn_is_greater(1337, 42) == true); + OSMO_ASSERT(gsm0502_fn_is_greater(42, 0) == true); + + /* 2715642 is very close to the Fn period (GSM_TDMA_HYPERFRAME) */ + OSMO_ASSERT(gsm0502_fn_is_greater(2715642, 42) == false); + OSMO_ASSERT(gsm0502_fn_is_greater(42, 2715642) == true); + OSMO_ASSERT(gsm0502_fn_is_greater(0, 2715642) == true); + + /* 1357824 is half of the Fn period (GSM_TDMA_HYPERFRAME) */ + OSMO_ASSERT(gsm0502_fn_is_greater(1357820, 1357824) == false); + OSMO_ASSERT(gsm0502_fn_is_greater(1357820, 1357825) == false); + OSMO_ASSERT(gsm0502_fn_is_greater(1357824, 1357820) == true); + OSMO_ASSERT(gsm0502_fn_is_greater(1357825, 1357820) == true); +} + int main(int argc, char **argv) { test_gsm0502_fn_remap(); + test_gsm0502_fn_is_greater(); return EXIT_SUCCESS; }