dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/30946 )
Change subject: gsm_utils: improve gsm_gsmtime2fn() ......................................................................
gsm_utils: improve gsm_gsmtime2fn()
The function gsm_gsmtime2fn() uses a hack to account for the truncated modulo implementation of C/C++. libosmocore offers proven floored modulo functions, so lets use OSMO_MOD_FLR() instead. Also arange the formula so that it looks more like the one in the spec.
Also add better spec references and a final modulo GSM_MAX_FN to prevent frame number results that exceed the valid range.
Change-Id: Ibf94bca8223f1f7858a6dd67bf27de0ab6feab20 --- M src/gsm/gsm_utils.c 1 file changed, 13 insertions(+), 2 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/46/30946/1
diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c index 3b0ec6a..e0c3052 100644 --- a/src/gsm/gsm_utils.c +++ b/src/gsm/gsm_utils.c @@ -887,8 +887,19 @@ * \returns GSM Frame Number */ uint32_t gsm_gsmtime2fn(struct gsm_time *time) { - /* TS 05.02 Chapter 4.3.3 TDMA frame number */ - return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1)); + uint32_t fn; + + /* See also: + * 3GPP TS 04.08, section 10.5.2.38, GSM TS 05.02 section 4.3.3, and + * 3GPP TS 08.58, section 9.3.8 */ + fn = 51 * OSMO_MOD_FLR((time->t3-time->t2), 26) + time->t3 + 51 * 26 * time->t1; + + /* Note: Corrupted input values may cause a resulting frame number + * larger then the maximum permitted value of GSM_MAX_FN. Even though + * the caller is expected to check the input values beforehand we must + * make sure that the result cannot exceed the value range of a valid + * GSM frame number. */ + return fn % GSM_MAX_FN; }
char *osmo_dump_gsmtime_buf(char *buf, size_t buf_len, const struct gsm_time *tm)