aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPhilipp Maier <pmaier@sysmocom.de>2023-01-12 12:58:11 +0100
committerPhilipp Maier <pmaier@sysmocom.de>2023-01-12 15:30:24 +0100
commita13d5662b7c37b9219e2dd11fda40f24c0046cbf (patch)
treee9683e8535282b9da43e25598183206ee8e789df /src
parentb6a3836ad2b7c3a0b09577ce87cda02d333c22d5 (diff)
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 modulo functions, so lets use OSMO_MOD_FLR() instead. Also arrange 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
Diffstat (limited to 'src')
-rw-r--r--src/gsm/gsm_utils.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c
index 3b0ec6a8..bbc7dd64 100644
--- a/src/gsm/gsm_utils.c
+++ b/src/gsm/gsm_utils.c
@@ -887,8 +887,19 @@ char *gsm_fn_as_gsmtime_str(uint32_t fn)
* \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 44.018, section 10.5.2.38, 3GPP TS 45.002 section 4.3.3, and
+ * 3GPP TS 48.058, 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)