Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 1 | /* gsm 04.08 system information (si) encoding and decoding |
| 2 | * 3gpp ts 04.08 version 7.21.0 release 1998 / etsi ts 100 940 v7.21.0 */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2012 Holger Hans Peter Freyther |
| 6 | * (C) 2012 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/arfcn_range_encode.h> |
| 24 | #include <openbsc/debug.h> |
| 25 | |
| 26 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 27 | |
| 28 | #include <osmocom/core/utils.h> |
| 29 | |
| 30 | int greatest_power_of_2_lesser_or_equal_to(int index) |
| 31 | { |
| 32 | int power_of_2 = 1; |
| 33 | |
| 34 | do { |
| 35 | power_of_2 *= 2; |
| 36 | } while (power_of_2 <= index); |
| 37 | |
| 38 | /* now go back one step */ |
| 39 | return power_of_2 / 2; |
| 40 | } |
| 41 | |
| 42 | static inline int mod(int data, int range) |
| 43 | { |
| 44 | int res = data % range; |
| 45 | while (res < 0) |
| 46 | res += range; |
| 47 | return res; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Determine at which index to split the ARFCNs to create an |
| 52 | * equally size partition for the given range. Return -1 if |
| 53 | * no such partition exists. |
| 54 | */ |
| 55 | int range_enc_find_index(const int range, const int *freqs, const int size) |
| 56 | { |
| 57 | int i, j, n; |
| 58 | |
| 59 | const int RANGE_DELTA = (range - 1) / 2; |
| 60 | |
| 61 | for (i = 0; i < size; ++i) { |
| 62 | n = 0; |
| 63 | for (j = 0; j < size; ++j) { |
| 64 | if (mod(freqs[j] - freqs[i], range) <= RANGE_DELTA) |
| 65 | n += 1; |
| 66 | } |
| 67 | |
| 68 | if (n - 1 == (size - 1) / 2) |
| 69 | return i; |
| 70 | } |
| 71 | |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Range encode the ARFCN list. |
| 77 | * \param range The range to use. |
| 78 | * \param arfcns The list of ARFCNs |
| 79 | * \param size The size of the list of ARFCNs |
| 80 | * \param out Place to store the W(i) output. |
| 81 | */ |
| 82 | int range_enc_arfcns(const int range, |
| 83 | const int *arfcns, int size, int *out, |
| 84 | const int index) |
| 85 | { |
| 86 | int split_at; |
| 87 | int i; |
| 88 | |
| 89 | /* |
| 90 | * The below is a GNU extension and we can remove it when |
| 91 | * we move to a quicksort like in-situ swap with the pivot. |
| 92 | */ |
| 93 | int arfcns_left[size / 2]; |
| 94 | int arfcns_right[size / 2]; |
| 95 | int l_size; |
| 96 | int r_size; |
| 97 | int l_origin; |
| 98 | int r_origin; |
| 99 | |
| 100 | |
| 101 | /* Test the two recursion anchors and stop processing */ |
| 102 | if (size == 0) |
| 103 | return 0; |
| 104 | |
| 105 | if (size == 1) { |
| 106 | out[index] = 1 + arfcns[0]; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* Now do the processing */ |
| 111 | split_at = range_enc_find_index(range, arfcns, size); |
| 112 | |
| 113 | /* we now know where to split */ |
| 114 | out[index] = 1 + arfcns[split_at]; |
| 115 | |
| 116 | /* calculate the work that needs to be done for the leafs */ |
| 117 | l_origin = mod(arfcns[split_at] + ((range - 1) / 2) + 1, range); |
| 118 | r_origin = mod(arfcns[split_at] + 1, range); |
| 119 | for (i = 0, l_size = 0, r_size = 0; i < size; ++i) { |
| 120 | if (mod(arfcns[i] - l_origin, range) < range / 2) |
| 121 | arfcns_left[l_size++] = mod(arfcns[i] - l_origin, range); |
| 122 | if (mod(arfcns[i] - r_origin, range) < range / 2) |
| 123 | arfcns_right[r_size++] = mod(arfcns[i] - r_origin, range); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Now recurse and we need to make this iterative... but as the |
| 128 | * tree is balanced the stack will not be too deep. |
| 129 | */ |
| 130 | range_enc_arfcns(range / 2, arfcns_left, l_size, |
| 131 | out, index + greatest_power_of_2_lesser_or_equal_to(index + 1)); |
| 132 | range_enc_arfcns((range -1 ) / 2, arfcns_right, r_size, |
| 133 | out, index + (2 * greatest_power_of_2_lesser_or_equal_to(index + 1))); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * The easiest is to use f0 == arfcns[0]. This means that under certain |
| 139 | * circumstances we can encode less ARFCNs than possible with an optimal f0. |
| 140 | * |
| 141 | * TODO: Solve the optimisation problem and pick f0 so that the max distance |
| 142 | * is the smallest. Taking into account the modulo operation. I think picking |
| 143 | * size/2 will be the optimal arfcn. |
| 144 | */ |
| 145 | /** |
| 146 | * This implements the range determination as described in GSM 04.08 J4. The |
Jacob Erlbeck | 7be3fea | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 147 | * result will be a base frequency f0 and the range to use. Note that for range |
| 148 | * 1024 encoding f0 always refers to ARFCN 0 even if it is not an element of |
| 149 | * the arfcns list. |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 150 | * |
| 151 | * \param[in] arfcns The input frequencies, they must be sorted, lowest number first |
| 152 | * \param[in] size The length of the array |
| 153 | * \param[out] f0 The selected F0 base frequency. It might not be inside the list |
| 154 | */ |
| 155 | int range_enc_determine_range(const int *arfcns, const int size, int *f0) |
| 156 | { |
| 157 | int max = 0; |
| 158 | |
| 159 | /* |
| 160 | * Go for the easiest. And pick arfcns[0] == f0. |
| 161 | */ |
| 162 | max = arfcns[size - 1] - arfcns[0]; |
| 163 | *f0 = arfcns[0]; |
| 164 | |
| 165 | if (max < 128 && size <= 29) |
| 166 | return ARFCN_RANGE_128; |
| 167 | if (max < 256 && size <= 22) |
| 168 | return ARFCN_RANGE_256; |
| 169 | if (max < 512 && size <= 18) |
| 170 | return ARFCN_RANGE_512; |
Jacob Erlbeck | 7be3fea | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 171 | if (max < 1024 && size <= 17) { |
| 172 | *f0 = 0; |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 173 | return ARFCN_RANGE_1024; |
Jacob Erlbeck | 7be3fea | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 174 | } |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 175 | |
| 176 | return ARFCN_RANGE_INVALID; |
| 177 | } |
| 178 | |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 179 | static void write_orig_arfcn(uint8_t *chan_list, int f0) |
| 180 | { |
| 181 | chan_list[0] |= (f0 >> 9) & 1; |
| 182 | chan_list[1] = (f0 >> 1); |
| 183 | chan_list[2] = (f0 & 1) << 7; |
| 184 | } |
| 185 | |
Jacob Erlbeck | d56e048 | 2014-01-10 17:46:51 +0100 | [diff] [blame] | 186 | static void write_all_wn(uint8_t *chan_list, int bit_offs, |
| 187 | int *w, int w_size, int w1_len) |
| 188 | { |
| 189 | int octet_offs = 0; /* offset into chan_list */ |
| 190 | int wk_len = w1_len; /* encoding size in bits of w[k] */ |
| 191 | int k; /* 1 based */ |
| 192 | int level = 0; /* tree level, top level = 0 */ |
| 193 | int lvl_left = 1; /* nodes per tree level */ |
| 194 | |
| 195 | /* W(2^i) to W(2^(i+1)-1) are on w1_len-i bits when present */ |
| 196 | |
| 197 | for (k = 1; k <= w_size; k++) { |
| 198 | int wk_left = wk_len; |
| 199 | DEBUGP(DRR, |
| 200 | "k=%d, wk_len=%d, offs=%d:%d, level=%d, " |
| 201 | "lvl_left=%d\n", |
| 202 | k, wk_len, octet_offs, bit_offs, level, lvl_left); |
| 203 | |
| 204 | while (wk_left > 0) { |
| 205 | int cur_bits = 8 - bit_offs; |
| 206 | int cur_mask; |
| 207 | int wk_slice; |
| 208 | |
| 209 | if (cur_bits > wk_left) |
| 210 | cur_bits = wk_left; |
| 211 | |
| 212 | cur_mask = ((1 << cur_bits) - 1); |
| 213 | |
| 214 | DEBUGP(DRR, |
| 215 | " wk_left=%d, cur_bits=%d, offs=%d:%d\n", |
| 216 | wk_left, cur_bits, octet_offs, bit_offs); |
| 217 | |
| 218 | /* advance */ |
| 219 | wk_left -= cur_bits; |
| 220 | bit_offs += cur_bits; |
| 221 | |
| 222 | /* right aligned wk data for current out octet */ |
| 223 | wk_slice = (w[k-1] >> wk_left) & cur_mask; |
| 224 | |
| 225 | /* cur_bits now contains the number of bits |
| 226 | * that are to be copied from wk to the chan_list. |
| 227 | * wk_left is set to the number of bits that must |
| 228 | * not yet be copied. |
| 229 | * bit_offs points after the bit area that is going to |
| 230 | * be overwritten: |
| 231 | * |
| 232 | * wk_left |
| 233 | * | |
| 234 | * v |
| 235 | * wk: WWWWWWWWWWW |
| 236 | * |||||<-- wk_slice, cur_bits=5 |
| 237 | * --WWWWW- |
| 238 | * ^ |
| 239 | * | |
| 240 | * bit_offs |
| 241 | */ |
| 242 | |
| 243 | DEBUGP(DRR, |
| 244 | " wk=%02x, slice=%02x/%02x, cl=%02x\n", |
| 245 | w[k-1], wk_slice, cur_mask, wk_slice << (8 - bit_offs)); |
| 246 | |
| 247 | chan_list[octet_offs] &= ~(cur_mask << (8 - bit_offs)); |
| 248 | chan_list[octet_offs] |= wk_slice << (8 - bit_offs); |
| 249 | |
| 250 | /* adjust output */ |
| 251 | if (bit_offs == 8) { |
| 252 | bit_offs = 0; |
| 253 | octet_offs += 1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* adjust bit sizes */ |
| 258 | lvl_left -= 1; |
| 259 | if (!lvl_left) { |
| 260 | /* completed tree level, advance to next */ |
| 261 | level += 1; |
| 262 | lvl_left = 1 << level; |
| 263 | wk_len -= 1; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 268 | int range_enc_range128(uint8_t *chan_list, int f0, int *w) |
| 269 | { |
| 270 | chan_list[0] = 0x8C; |
| 271 | write_orig_arfcn(chan_list, f0); |
| 272 | |
Jacob Erlbeck | b6fb63f | 2014-01-10 17:47:43 +0100 | [diff] [blame] | 273 | write_all_wn(&chan_list[2], 1, w, 28, 7); |
| 274 | return 0; |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | int range_enc_range256(uint8_t *chan_list, int f0, int *w) |
| 278 | { |
| 279 | chan_list[0] = 0x8A; |
| 280 | write_orig_arfcn(chan_list, f0); |
| 281 | |
Jacob Erlbeck | b6fb63f | 2014-01-10 17:47:43 +0100 | [diff] [blame] | 282 | write_all_wn(&chan_list[2], 1, w, 21, 8); |
| 283 | return 0; |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | int range_enc_range512(uint8_t *chan_list, int f0, int *w) |
| 287 | { |
Holger Hans Peter Freyther | 52d8f5a | 2013-12-12 17:00:57 +0100 | [diff] [blame] | 288 | chan_list[0] = 0x88; |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 289 | write_orig_arfcn(chan_list, f0); |
| 290 | |
Jacob Erlbeck | d56e048 | 2014-01-10 17:46:51 +0100 | [diff] [blame] | 291 | write_all_wn(&chan_list[2], 1, w, 17, 9); |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | int range_enc_range1024(uint8_t *chan_list, int f0, int f0_included, int *w) |
| 296 | { |
| 297 | chan_list[0] = 0x80 | (f0_included << 2); |
| 298 | |
Jacob Erlbeck | b6fb63f | 2014-01-10 17:47:43 +0100 | [diff] [blame] | 299 | write_all_wn(&chan_list[0], 6, w, 16, 10); |
| 300 | return 0; |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Jacob Erlbeck | 7be3fea | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 303 | int range_enc_filter_arfcns(int *arfcns, |
| 304 | const int size, const int f0, int *f0_included) |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 305 | { |
| 306 | int i, j = 0; |
| 307 | *f0_included = 0; |
| 308 | |
Jacob Erlbeck | 7be3fea | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 309 | for (i = 0; i < size; ++i) { |
| 310 | /* |
| 311 | * Appendix J.4 says the following: |
| 312 | * All frequencies except F(0), minus F(0) + 1. |
| 313 | * I assume we need to exclude it here. |
| 314 | */ |
| 315 | if (arfcns[i] == f0) { |
| 316 | *f0_included = 1; |
| 317 | continue; |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 318 | } |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 319 | |
Jacob Erlbeck | 7be3fea | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 320 | arfcns[j++] = mod(arfcns[i] - (f0 + 1), 1024); |
Holger Hans Peter Freyther | a9e7de6 | 2012-10-13 12:38:54 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | return j; |
| 324 | } |