Pau Espin Pedrol | a18f1d7 | 2023-07-14 16:24:27 +0200 | [diff] [blame] | 1 | /*! \defgroup gsm0502 GSM 05.02 / 3GPP TS 45.002 |
| 2 | * @{ |
| 3 | * \file gsm0502.h |
| 4 | */ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 6 | #pragma once |
Harald Welte | 94df39e | 2011-06-26 14:33:57 +0200 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 11 | #include <osmocom/gsm/protocol/gsm_08_58.h> |
| 12 | |
Vadim Yanitskiy | ed0445b | 2020-06-17 03:55:46 +0700 | [diff] [blame] | 13 | /* 4.3.3 TDMA frame number : constants and modular arithmetic */ |
| 14 | #define GSM_TDMA_FN_DURATION_nS 4615384 /* in 1e−9 seconds (approx) */ |
| 15 | #define GSM_TDMA_FN_DURATION_uS 4615 /* in 1e-6 seconds (approx) */ |
| 16 | |
| 17 | #define GSM_TDMA_SUPERFRAME (26 * 51) |
| 18 | #define GSM_TDMA_HYPERFRAME (2048 * GSM_TDMA_SUPERFRAME) |
| 19 | |
| 20 | /*! Return the sum of two specified TDMA frame numbers (summation) */ |
| 21 | #define GSM_TDMA_FN_SUM(a, b) \ |
Vadim Yanitskiy | 8deedfa | 2022-10-04 02:41:13 +0700 | [diff] [blame] | 22 | (((a) + (b)) % GSM_TDMA_HYPERFRAME) |
Vadim Yanitskiy | ed0445b | 2020-06-17 03:55:46 +0700 | [diff] [blame] | 23 | /*! Return the difference of two specified TDMA frame numbers (subtraction) */ |
| 24 | #define GSM_TDMA_FN_SUB(a, b) \ |
Vadim Yanitskiy | 8deedfa | 2022-10-04 02:41:13 +0700 | [diff] [blame] | 25 | (((a) + GSM_TDMA_HYPERFRAME - (b)) % GSM_TDMA_HYPERFRAME) |
Vadim Yanitskiy | ed0445b | 2020-06-17 03:55:46 +0700 | [diff] [blame] | 26 | /*! Return the *minimum* difference of two specified TDMA frame numbers (distance) */ |
| 27 | #define GSM_TDMA_FN_DIFF(a, b) \ |
| 28 | OSMO_MIN(GSM_TDMA_FN_SUB(a, b), GSM_TDMA_FN_SUB(b, a)) |
| 29 | |
| 30 | /*! Increment the given TDMA frame number by 1 and return the result (like ++fn) */ |
| 31 | #define GSM_TDMA_FN_INC(fn) \ |
| 32 | ((fn) = GSM_TDMA_FN_SUM((fn), 1)) |
| 33 | /*! Decrement the given TDMA frame number by 1 and return the result (like --fn) */ |
| 34 | #define GSM_TDMA_FN_DEC(fn) \ |
| 35 | ((fn) = GSM_TDMA_FN_SUB((fn), 1)) |
| 36 | |
Vadim Yanitskiy | c657a50 | 2023-03-06 04:42:14 +0700 | [diff] [blame] | 37 | /* 5.2.3.1 Normal burst for GMSK (1 bit per symbol) */ |
| 38 | #define GSM_NBITS_NB_GMSK_TAIL 3 |
| 39 | #define GSM_NBITS_NB_GMSK_PAYLOAD (2 * 58) |
| 40 | #define GSM_NBITS_NB_GMSK_TRAIN_SEQ 26 |
| 41 | #define GSM_NBITS_NB_GMSK_BURST 148 /* without guard period */ |
| 42 | |
| 43 | /* 5.2.3.3 Normal burst for 8-PSK (3 bits per symbol) */ |
| 44 | #define GSM_NBITS_NB_8PSK_TAIL (GSM_NBITS_NB_GMSK_TAIL * 3) |
| 45 | #define GSM_NBITS_NB_8PSK_PAYLOAD (GSM_NBITS_NB_GMSK_PAYLOAD * 3) |
| 46 | #define GSM_NBITS_NB_8PSK_TRAIN_SEQ (GSM_NBITS_NB_GMSK_TRAIN_SEQ * 3) |
| 47 | #define GSM_NBITS_NB_8PSK_BURST (GSM_NBITS_NB_GMSK_BURST * 3) |
| 48 | |
| 49 | /* 5.2.5 Synchronization burst (also GMSK) */ |
| 50 | #define GSM_NBITS_SB_GMSK_TAIL GSM_NBITS_NB_GMSK_TAIL |
| 51 | #define GSM_NBITS_SB_GMSK_PAYLOAD (2 * 39) |
| 52 | #define GSM_NBITS_SB_GMSK_ETRAIN_SEQ 64 |
| 53 | #define GSM_NBITS_SB_GMSK_BURST GSM_NBITS_NB_GMSK_BURST |
| 54 | |
| 55 | /* 5.2.6 Dummy burst (also GMSK) */ |
| 56 | #define GSM_NBITS_DB_GMSK_TAIL GSM_NBITS_NB_GMSK_TAIL |
| 57 | #define GSM_NBITS_DB_GMSK_MIXED 142 |
| 58 | #define GSM_NBITS_DB_GMSK_BURST GSM_NBITS_NB_GMSK_BURST |
| 59 | |
| 60 | /* 5.2.7 Access burst (also GMSK) */ |
| 61 | #define GSM_NBITS_AB_GMSK_ETAIL 8 |
| 62 | #define GSM_NBITS_AB_GMSK_SYNCH_SEQ 41 |
| 63 | #define GSM_NBITS_AB_GMSK_PAYLOAD 36 |
| 64 | #define GSM_NBITS_AB_GMSK_TAIL GSM_NBITS_NB_GMSK_TAIL |
| 65 | #define GSM_NBITS_AB_GMSK_BURST GSM_NBITS_NB_GMSK_BURST |
| 66 | |
Vadim Yanitskiy | 5285d47 | 2023-09-25 23:30:38 +0700 | [diff] [blame] | 67 | /*! Compare the given TDMA FNs, taking the wrapping into account. |
| 68 | * \param[in] fn1 First TDMA Fn value to compare. |
| 69 | * \param[in] fn2 Second TDMA Fn value to compare. |
| 70 | * \returns similarly to memcmp(), -1 if fn1 goes before fn2; |
| 71 | * 0 if fn1 equals fn2; |
| 72 | * 1 if fn1 goes after fn2. */ |
Vadim Yanitskiy | 15b76f0 | 2023-09-26 16:36:21 +0700 | [diff] [blame] | 73 | static inline int gsm0502_fncmp(uint32_t fn1, uint32_t fn2) |
Vadim Yanitskiy | 5285d47 | 2023-09-25 23:30:38 +0700 | [diff] [blame] | 74 | { |
| 75 | const uint32_t thresh = GSM_TDMA_HYPERFRAME / 2; |
| 76 | |
| 77 | if (fn1 == fn2) |
| 78 | return 0; |
| 79 | if ((fn1 < fn2 && (fn2 - fn1) < thresh) || |
| 80 | (fn1 > fn2 && (fn1 - fn2) > thresh)) |
| 81 | return -1; |
| 82 | |
| 83 | return 1; |
| 84 | } |
| 85 | |
Harald Welte | 94df39e | 2011-06-26 14:33:57 +0200 | [diff] [blame] | 86 | /* Table 5 Clause 7 TS 05.02 */ |
| 87 | static inline unsigned int |
Pau Espin Pedrol | 6ca0a43 | 2022-11-24 17:09:03 +0100 | [diff] [blame] | 88 | gsm0502_get_n_pag_blocks(const struct gsm48_control_channel_descr *chan_desc) |
Harald Welte | 94df39e | 2011-06-26 14:33:57 +0200 | [diff] [blame] | 89 | { |
| 90 | if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C) |
| 91 | return 3 - chan_desc->bs_ag_blks_res; |
| 92 | else |
| 93 | return 9 - chan_desc->bs_ag_blks_res; |
| 94 | } |
| 95 | |
| 96 | /* Chapter 6.5.2 of TS 05.02 */ |
| 97 | static inline unsigned int |
| 98 | gsm0502_get_ccch_group(uint64_t imsi, unsigned int bs_cc_chans, |
| 99 | unsigned int n_pag_blocks) |
| 100 | { |
| 101 | return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) / n_pag_blocks; |
| 102 | } |
| 103 | |
| 104 | /* Chapter 6.5.2 of TS 05.02 */ |
| 105 | static inline unsigned int |
| 106 | gsm0502_get_paging_group(uint64_t imsi, unsigned int bs_cc_chans, |
| 107 | int n_pag_blocks) |
| 108 | { |
| 109 | return (imsi % 1000) % (bs_cc_chans * n_pag_blocks) % n_pag_blocks; |
| 110 | } |
| 111 | |
Harald Welte | ea19c97 | 2011-06-26 14:47:16 +0200 | [diff] [blame] | 112 | unsigned int |
Pau Espin Pedrol | 6ca0a43 | 2022-11-24 17:09:03 +0100 | [diff] [blame] | 113 | gsm0502_calc_paging_group(const struct gsm48_control_channel_descr *chan_desc, uint64_t imsi); |
Philipp Maier | 69e00cc | 2019-10-09 13:38:38 +0200 | [diff] [blame] | 114 | |
| 115 | enum gsm0502_fn_remap_channel { |
| 116 | FN_REMAP_TCH_F, |
| 117 | FN_REMAP_TCH_H0, |
| 118 | FN_REMAP_TCH_H1, |
| 119 | FN_REMAP_FACCH_F, |
| 120 | FN_REMAP_FACCH_H0, |
| 121 | FN_REMAP_FACCH_H1, |
| 122 | FN_REMAP_MAX, |
| 123 | }; |
| 124 | |
| 125 | uint32_t gsm0502_fn_remap(uint32_t fn, enum gsm0502_fn_remap_channel channel); |
Sylvain Munaut | 63bee45 | 2020-05-14 03:02:26 +0700 | [diff] [blame] | 126 | |
| 127 | uint16_t gsm0502_hop_seq_gen(const struct gsm_time *t, |
| 128 | uint8_t hsn, uint8_t maio, |
| 129 | size_t n, const uint16_t *ma); |
Vadim Yanitskiy | 418d76c | 2023-06-22 23:00:05 +0700 | [diff] [blame] | 130 | |
| 131 | int gsm0502_fn2ccch_block(uint32_t fn); |
Pau Espin Pedrol | a18f1d7 | 2023-07-14 16:24:27 +0200 | [diff] [blame] | 132 | |
| 133 | /*! @} */ |