Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gsm0502.c |
| 2 | * Paging helper code */ |
| 3 | /* |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | ea19c97 | 2011-06-26 14:47:16 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
Harald Welte | ea19c97 | 2011-06-26 14:47:16 +0200 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 27 | #include <osmocom/gsm/gsm0502.h> |
| 28 | #include <osmocom/gsm/gsm48.h> |
| 29 | #include <osmocom/gsm/rsl.h> |
Philipp Maier | 69e00cc | 2019-10-09 13:38:38 +0200 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm_utils.h> |
| 31 | #include <osmocom/core/logging.h> |
| 32 | #include <inttypes.h> |
Harald Welte | ea19c97 | 2011-06-26 14:47:16 +0200 | [diff] [blame] | 33 | |
| 34 | unsigned int |
| 35 | gsm0502_calc_paging_group(struct gsm48_control_channel_descr *chan_desc, uint64_t imsi) |
| 36 | { |
| 37 | int ccch_conf; |
| 38 | int bs_cc_chans; |
| 39 | int blocks; |
| 40 | unsigned int group; |
| 41 | |
| 42 | ccch_conf = chan_desc->ccch_conf; |
| 43 | bs_cc_chans = rsl_ccch_conf_to_bs_cc_chans(ccch_conf); |
| 44 | /* code word + 2, as 2 channels equals 0x0 */ |
| 45 | blocks = gsm48_number_of_paging_subchannels(chan_desc); |
| 46 | group = gsm0502_get_paging_group(imsi, bs_cc_chans, blocks); |
| 47 | |
| 48 | return group; |
| 49 | } |
Philipp Maier | 69e00cc | 2019-10-09 13:38:38 +0200 | [diff] [blame] | 50 | |
| 51 | /* Clause 7 Table 1 of 5 Mapping of logical channels onto physical channels */ |
| 52 | #define TCH_REPEAT_LENGTH 13 |
| 53 | #define FACCH_F_REPEAT_LENGTH 13 |
| 54 | #define FACCH_H_REPEAT_LENGTH 26 |
| 55 | |
| 56 | static const uint8_t gsm0502_tch_f_traffic_block_map[3][8] = { |
| 57 | {0, 1, 2, 3, 4, 5, 6, 7}, |
| 58 | {4, 5, 6, 7, 8, 9, 10, 11}, |
| 59 | {8, 9, 10, 11, 0, 1, 2, 3} |
| 60 | }; |
| 61 | |
| 62 | static const uint8_t gsm0502_tch_h0_traffic_block_map[3][4] = { |
| 63 | {0, 2, 4, 6}, |
| 64 | {4, 6, 8, 10}, |
| 65 | {8, 10, 0, 2} |
| 66 | }; |
| 67 | |
| 68 | static const uint8_t gsm0502_tch_h1_traffic_block_map[3][4] = { |
| 69 | {1, 3, 5, 7}, |
| 70 | {5, 7, 9, 11}, |
| 71 | {9, 11, 1, 3} |
| 72 | }; |
| 73 | |
| 74 | static const uint8_t gsm0502_tch_f_facch_block_map[3][8] = { |
| 75 | {0, 1, 2, 3, 4, 5, 6, 7}, |
| 76 | {4, 5, 6, 7, 8, 9, 10, 11}, |
| 77 | {8, 9, 10, 11, 0, 1, 2, 3} |
| 78 | }; |
| 79 | |
| 80 | static const uint8_t gsm0502_tch_h0_facch_block_map[3][6] = { |
| 81 | {0, 2, 4, 6, 8, 10}, |
| 82 | {8, 10, 13, 15, 17, 19}, |
| 83 | {17, 19, 21, 23, 0, 2} |
| 84 | }; |
| 85 | |
| 86 | static const uint8_t gsm0502_tch_h1_facch_block_map[3][6] = { |
| 87 | {1, 3, 5, 7, 9, 11}, |
| 88 | {9, 11, 14, 16, 18, 20}, |
| 89 | {18, 20, 22, 24, 1, 3} |
| 90 | }; |
| 91 | |
| 92 | /* Struct to describe a remapping function for block frame nbumbers. The member |
| 93 | * blockend describes the ending of a block for which we want to determine the |
| 94 | * beginning frame number. The member distance describes the value we need to |
| 95 | * subtract from the blockend frame number in order to get the beginning of the |
| 96 | * the block. The member cycle describes the Repeat length in TDMA frames we |
| 97 | * are dealing with. For traffic channels this is always 13, for control |
| 98 | * channels it is different. The member len simply defines amount of |
| 99 | * blockendings and distances we store in the remap table */ |
| 100 | struct fn_remap_table { |
| 101 | unsigned int cycle; |
| 102 | unsigned int len; |
| 103 | uint8_t blockend[8]; |
| 104 | uint8_t distance[8]; |
| 105 | }; |
| 106 | |
| 107 | /* Memory to hold the remap tables we will automatically generate on startup */ |
| 108 | static struct fn_remap_table tch_f_remap_table; |
| 109 | static struct fn_remap_table tch_h0_remap_table; |
| 110 | static struct fn_remap_table tch_h1_remap_table; |
| 111 | static struct fn_remap_table facch_f_remap_table; |
| 112 | static struct fn_remap_table facch_h0_remap_table; |
| 113 | static struct fn_remap_table facch_h1_remap_table; |
| 114 | static struct fn_remap_table *fn_remap_table_ptr[FN_REMAP_MAX]; |
| 115 | |
| 116 | /* Generate a remap table from a given block map. A block map lists the block |
| 117 | * layout as defined in GSM 05.02, Clause 7 Table 1 of 5, one block per row. |
| 118 | * Parameters: |
| 119 | * table: name of the remap table to output |
| 120 | * map: traffic block map input |
| 121 | * rows: length of the traffic block map |
| 122 | * cols: witdh of the traffic block map |
| 123 | * repeat: repeat length in TDMA frames (cycle) */ |
| 124 | #define fn_remap_table_from_traffic_block_map(table, map, rows, cols, repeat) \ |
| 125 | for(i=0;i<rows;i++) { \ |
| 126 | table.blockend[i] = map[i][cols-1]; \ |
| 127 | if(map[i][0] <= map[i][cols-1]) \ |
| 128 | table.distance[i] = map[i][cols-1] - map[i][0]; \ |
| 129 | else \ |
| 130 | table.distance[i] = repeat - map[i][0] + map[i][cols-1]; \ |
| 131 | } \ |
| 132 | table.cycle = repeat; \ |
| 133 | table.len = rows; |
| 134 | |
| 135 | /* Automatically generate fn remap tables on startupmake */ |
| 136 | static __attribute__ ((constructor)) |
| 137 | void fn_remap_tables_build(void) |
| 138 | { |
| 139 | /* Required by macro */ |
| 140 | unsigned int i; |
| 141 | |
| 142 | /* Generate tables */ |
| 143 | fn_remap_table_from_traffic_block_map(tch_f_remap_table, |
| 144 | gsm0502_tch_f_traffic_block_map, 3, 8, |
| 145 | TCH_REPEAT_LENGTH); |
| 146 | fn_remap_table_from_traffic_block_map(tch_h0_remap_table, |
| 147 | gsm0502_tch_h0_traffic_block_map, 3, 4, |
| 148 | TCH_REPEAT_LENGTH); |
| 149 | fn_remap_table_from_traffic_block_map(tch_h1_remap_table, |
| 150 | gsm0502_tch_h1_traffic_block_map, 3, 4, |
| 151 | TCH_REPEAT_LENGTH); |
| 152 | fn_remap_table_from_traffic_block_map(facch_f_remap_table, |
| 153 | gsm0502_tch_f_facch_block_map, 3, 8, |
| 154 | FACCH_F_REPEAT_LENGTH); |
| 155 | fn_remap_table_from_traffic_block_map(facch_h0_remap_table, |
| 156 | gsm0502_tch_h0_facch_block_map, 3, 6, |
| 157 | FACCH_H_REPEAT_LENGTH); |
| 158 | fn_remap_table_from_traffic_block_map(facch_h1_remap_table, |
| 159 | gsm0502_tch_h1_facch_block_map, 3, 6, |
| 160 | FACCH_H_REPEAT_LENGTH); |
| 161 | |
| 162 | fn_remap_table_ptr[FN_REMAP_TCH_F] = &tch_f_remap_table; |
| 163 | fn_remap_table_ptr[FN_REMAP_TCH_H0] = &tch_h0_remap_table; |
| 164 | fn_remap_table_ptr[FN_REMAP_TCH_H1] = &tch_h1_remap_table; |
| 165 | fn_remap_table_ptr[FN_REMAP_FACCH_F] = &facch_f_remap_table; |
| 166 | fn_remap_table_ptr[FN_REMAP_FACCH_H0] = &facch_h0_remap_table; |
| 167 | fn_remap_table_ptr[FN_REMAP_FACCH_H1] = &facch_h1_remap_table; |
| 168 | } |
| 169 | |
| 170 | /*! Calculate the frame number of the beginning of a block. |
| 171 | * \param[in] fn frame number of the block ending. |
| 172 | * \param[in] channel channel type (see also enum fn_remap_channel). |
| 173 | * \returns frame number of the beginning of the block or input frame number if |
| 174 | * remapping was not possible. */ |
| 175 | uint32_t gsm0502_fn_remap(uint32_t fn, enum gsm0502_fn_remap_channel channel) |
| 176 | { |
| 177 | uint8_t fn_cycle; |
| 178 | uint8_t i; |
| 179 | int sub = -1; |
| 180 | uint32_t fn_map; |
| 181 | struct fn_remap_table *table; |
| 182 | |
| 183 | OSMO_ASSERT(channel < ARRAY_SIZE(fn_remap_table_ptr)); |
| 184 | table = fn_remap_table_ptr[(uint8_t)channel]; |
| 185 | |
| 186 | fn_cycle = fn % table->cycle; |
| 187 | |
| 188 | for (i = 0; i < table->len; i++) { |
| 189 | if (table->blockend[i] == fn_cycle) { |
| 190 | sub = table->distance[i]; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (sub == -1) { |
| 196 | LOGP(DLGLOBAL, LOGL_ERROR, "could not remap frame number!, fn=%"PRIu32"\n", fn); |
| 197 | return fn; |
| 198 | } |
| 199 | |
| 200 | fn_map = (fn + GSM_MAX_FN - sub) % GSM_MAX_FN; |
| 201 | |
| 202 | return fn_map; |
| 203 | } |