blob: 84f9f635f8c77668543142fa611fc994a90d7a89 [file] [log] [blame]
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +02001/* 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
Neels Hofmeyrc0164792017-09-04 15:15:32 +020023#include <osmocom/bsc/arfcn_range_encode.h>
24#include <osmocom/bsc/debug.h>
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020025
26#include <osmocom/gsm/protocol/gsm_04_08.h>
27
28#include <osmocom/core/utils.h>
29
Max881064e2016-12-14 14:51:40 +010030#include <errno.h>
31
Max34be86b2016-12-16 18:45:51 +010032static inline int greatest_power_of_2_lesser_or_equal_to(int index)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020033{
34 int power_of_2 = 1;
35
36 do {
37 power_of_2 *= 2;
38 } while (power_of_2 <= index);
39
40 /* now go back one step */
41 return power_of_2 / 2;
42}
43
44static inline int mod(int data, int range)
45{
46 int res = data % range;
47 while (res < 0)
48 res += range;
49 return res;
50}
51
52/**
53 * Determine at which index to split the ARFCNs to create an
54 * equally size partition for the given range. Return -1 if
55 * no such partition exists.
56 */
Max34be86b2016-12-16 18:45:51 +010057int range_enc_find_index(enum gsm48_range range, const int *freqs, const int size)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020058{
59 int i, j, n;
60
61 const int RANGE_DELTA = (range - 1) / 2;
62
63 for (i = 0; i < size; ++i) {
64 n = 0;
65 for (j = 0; j < size; ++j) {
66 if (mod(freqs[j] - freqs[i], range) <= RANGE_DELTA)
67 n += 1;
68 }
69
70 if (n - 1 == (size - 1) / 2)
71 return i;
72 }
73
74 return -1;
75}
76
Neels Hofmeyrcf4a5732018-03-28 18:07:17 +020077/* Worker for range_enc_arfcns(), do not call directly. */
78int _range_enc_arfcns(enum gsm48_range range,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020079 const int *arfcns, int size, int *out,
80 const int index)
81{
82 int split_at;
83 int i;
84
85 /*
86 * The below is a GNU extension and we can remove it when
87 * we move to a quicksort like in-situ swap with the pivot.
88 */
89 int arfcns_left[size / 2];
90 int arfcns_right[size / 2];
91 int l_size;
92 int r_size;
93 int l_origin;
94 int r_origin;
95
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020096 /* Now do the processing */
97 split_at = range_enc_find_index(range, arfcns, size);
Max881064e2016-12-14 14:51:40 +010098 if (split_at < 0)
99 return -EINVAL;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200100
101 /* we now know where to split */
102 out[index] = 1 + arfcns[split_at];
103
104 /* calculate the work that needs to be done for the leafs */
105 l_origin = mod(arfcns[split_at] + ((range - 1) / 2) + 1, range);
106 r_origin = mod(arfcns[split_at] + 1, range);
107 for (i = 0, l_size = 0, r_size = 0; i < size; ++i) {
108 if (mod(arfcns[i] - l_origin, range) < range / 2)
109 arfcns_left[l_size++] = mod(arfcns[i] - l_origin, range);
110 if (mod(arfcns[i] - r_origin, range) < range / 2)
111 arfcns_right[r_size++] = mod(arfcns[i] - r_origin, range);
112 }
113
114 /*
115 * Now recurse and we need to make this iterative... but as the
116 * tree is balanced the stack will not be too deep.
117 */
Max34be86b2016-12-16 18:45:51 +0100118 if (l_size)
119 range_enc_arfcns(range / 2, arfcns_left, l_size,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200120 out, index + greatest_power_of_2_lesser_or_equal_to(index + 1));
Max34be86b2016-12-16 18:45:51 +0100121 if (r_size)
122 range_enc_arfcns((range - 1) / 2, arfcns_right, r_size,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200123 out, index + (2 * greatest_power_of_2_lesser_or_equal_to(index + 1)));
124 return 0;
125}
126
Neels Hofmeyrcf4a5732018-03-28 18:07:17 +0200127/**
128 * Range encode the ARFCN list.
129 * \param range The range to use.
130 * \param arfcns The list of ARFCNs
131 * \param size The size of the list of ARFCNs
132 * \param out Place to store the W(i) output.
133 */
134int range_enc_arfcns(enum gsm48_range range,
135 const int *arfcns, int size, int *out,
136 const int index)
137{
138 if (size <= 0)
139 return 0;
140
141 if (size == 1) {
142 out[index] = 1 + arfcns[0];
143 return 0;
144 }
145
146 return _range_enc_arfcns(range, arfcns, size, out, index);
147}
148
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200149/*
150 * The easiest is to use f0 == arfcns[0]. This means that under certain
151 * circumstances we can encode less ARFCNs than possible with an optimal f0.
152 *
153 * TODO: Solve the optimisation problem and pick f0 so that the max distance
154 * is the smallest. Taking into account the modulo operation. I think picking
155 * size/2 will be the optimal arfcn.
156 */
157/**
158 * This implements the range determination as described in GSM 04.08 J4. The
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100159 * result will be a base frequency f0 and the range to use. Note that for range
160 * 1024 encoding f0 always refers to ARFCN 0 even if it is not an element of
161 * the arfcns list.
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200162 *
163 * \param[in] arfcns The input frequencies, they must be sorted, lowest number first
164 * \param[in] size The length of the array
165 * \param[out] f0 The selected F0 base frequency. It might not be inside the list
166 */
167int range_enc_determine_range(const int *arfcns, const int size, int *f0)
168{
169 int max = 0;
170
171 /*
172 * Go for the easiest. And pick arfcns[0] == f0.
173 */
174 max = arfcns[size - 1] - arfcns[0];
175 *f0 = arfcns[0];
176
177 if (max < 128 && size <= 29)
178 return ARFCN_RANGE_128;
179 if (max < 256 && size <= 22)
180 return ARFCN_RANGE_256;
181 if (max < 512 && size <= 18)
182 return ARFCN_RANGE_512;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100183 if (max < 1024 && size <= 17) {
184 *f0 = 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200185 return ARFCN_RANGE_1024;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100186 }
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200187
188 return ARFCN_RANGE_INVALID;
189}
190
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200191static void write_orig_arfcn(uint8_t *chan_list, int f0)
192{
193 chan_list[0] |= (f0 >> 9) & 1;
194 chan_list[1] = (f0 >> 1);
195 chan_list[2] = (f0 & 1) << 7;
196}
197
Jacob Erlbeckf8f72e22014-01-10 17:46:51 +0100198static void write_all_wn(uint8_t *chan_list, int bit_offs,
199 int *w, int w_size, int w1_len)
200{
201 int octet_offs = 0; /* offset into chan_list */
202 int wk_len = w1_len; /* encoding size in bits of w[k] */
203 int k; /* 1 based */
204 int level = 0; /* tree level, top level = 0 */
205 int lvl_left = 1; /* nodes per tree level */
206
207 /* W(2^i) to W(2^(i+1)-1) are on w1_len-i bits when present */
208
209 for (k = 1; k <= w_size; k++) {
210 int wk_left = wk_len;
211 DEBUGP(DRR,
212 "k=%d, wk_len=%d, offs=%d:%d, level=%d, "
213 "lvl_left=%d\n",
214 k, wk_len, octet_offs, bit_offs, level, lvl_left);
215
216 while (wk_left > 0) {
217 int cur_bits = 8 - bit_offs;
218 int cur_mask;
219 int wk_slice;
220
221 if (cur_bits > wk_left)
222 cur_bits = wk_left;
223
224 cur_mask = ((1 << cur_bits) - 1);
225
226 DEBUGP(DRR,
227 " wk_left=%d, cur_bits=%d, offs=%d:%d\n",
228 wk_left, cur_bits, octet_offs, bit_offs);
229
230 /* advance */
231 wk_left -= cur_bits;
232 bit_offs += cur_bits;
233
234 /* right aligned wk data for current out octet */
235 wk_slice = (w[k-1] >> wk_left) & cur_mask;
236
237 /* cur_bits now contains the number of bits
238 * that are to be copied from wk to the chan_list.
239 * wk_left is set to the number of bits that must
240 * not yet be copied.
241 * bit_offs points after the bit area that is going to
242 * be overwritten:
243 *
244 * wk_left
245 * |
246 * v
247 * wk: WWWWWWWWWWW
248 * |||||<-- wk_slice, cur_bits=5
249 * --WWWWW-
250 * ^
251 * |
252 * bit_offs
253 */
254
255 DEBUGP(DRR,
256 " wk=%02x, slice=%02x/%02x, cl=%02x\n",
257 w[k-1], wk_slice, cur_mask, wk_slice << (8 - bit_offs));
258
259 chan_list[octet_offs] &= ~(cur_mask << (8 - bit_offs));
260 chan_list[octet_offs] |= wk_slice << (8 - bit_offs);
261
262 /* adjust output */
263 if (bit_offs == 8) {
264 bit_offs = 0;
265 octet_offs += 1;
266 }
267 }
268
269 /* adjust bit sizes */
270 lvl_left -= 1;
271 if (!lvl_left) {
272 /* completed tree level, advance to next */
273 level += 1;
274 lvl_left = 1 << level;
275 wk_len -= 1;
276 }
277 }
278}
279
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200280int range_enc_range128(uint8_t *chan_list, int f0, int *w)
281{
282 chan_list[0] = 0x8C;
283 write_orig_arfcn(chan_list, f0);
284
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100285 write_all_wn(&chan_list[2], 1, w, 28, 7);
286 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200287}
288
289int range_enc_range256(uint8_t *chan_list, int f0, int *w)
290{
291 chan_list[0] = 0x8A;
292 write_orig_arfcn(chan_list, f0);
293
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100294 write_all_wn(&chan_list[2], 1, w, 21, 8);
295 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200296}
297
298int range_enc_range512(uint8_t *chan_list, int f0, int *w)
299{
Holger Hans Peter Freyther27a788d2013-12-12 17:00:57 +0100300 chan_list[0] = 0x88;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200301 write_orig_arfcn(chan_list, f0);
302
Jacob Erlbeckf8f72e22014-01-10 17:46:51 +0100303 write_all_wn(&chan_list[2], 1, w, 17, 9);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200304 return 0;
305}
306
307int range_enc_range1024(uint8_t *chan_list, int f0, int f0_included, int *w)
308{
309 chan_list[0] = 0x80 | (f0_included << 2);
310
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100311 write_all_wn(&chan_list[0], 6, w, 16, 10);
312 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200313}
314
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100315int range_enc_filter_arfcns(int *arfcns,
316 const int size, const int f0, int *f0_included)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200317{
318 int i, j = 0;
319 *f0_included = 0;
320
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100321 for (i = 0; i < size; ++i) {
322 /*
323 * Appendix J.4 says the following:
324 * All frequencies except F(0), minus F(0) + 1.
325 * I assume we need to exclude it here.
326 */
327 if (arfcns[i] == f0) {
328 *f0_included = 1;
329 continue;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200330 }
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200331
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100332 arfcns[j++] = mod(arfcns[i] - (f0 + 1), 1024);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200333 }
334
335 return j;
336}