blob: 9ca48407ebc2a1513e69e1afb2fe35f3c187deb7 [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
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
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
77/**
78 * Range encode the ARFCN list.
79 * \param range The range to use.
80 * \param arfcns The list of ARFCNs
81 * \param size The size of the list of ARFCNs
82 * \param out Place to store the W(i) output.
83 */
Max34be86b2016-12-16 18:45:51 +010084int range_enc_arfcns(enum gsm48_range range,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020085 const int *arfcns, int size, int *out,
86 const int index)
87{
88 int split_at;
89 int i;
90
91 /*
92 * The below is a GNU extension and we can remove it when
93 * we move to a quicksort like in-situ swap with the pivot.
94 */
95 int arfcns_left[size / 2];
96 int arfcns_right[size / 2];
97 int l_size;
98 int r_size;
99 int l_origin;
100 int r_origin;
101
102
103 /* Test the two recursion anchors and stop processing */
104 if (size == 0)
105 return 0;
106
107 if (size == 1) {
108 out[index] = 1 + arfcns[0];
109 return 0;
110 }
111
112 /* Now do the processing */
113 split_at = range_enc_find_index(range, arfcns, size);
Max881064e2016-12-14 14:51:40 +0100114 if (split_at < 0)
115 return -EINVAL;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200116
117 /* we now know where to split */
118 out[index] = 1 + arfcns[split_at];
119
120 /* calculate the work that needs to be done for the leafs */
121 l_origin = mod(arfcns[split_at] + ((range - 1) / 2) + 1, range);
122 r_origin = mod(arfcns[split_at] + 1, range);
123 for (i = 0, l_size = 0, r_size = 0; i < size; ++i) {
124 if (mod(arfcns[i] - l_origin, range) < range / 2)
125 arfcns_left[l_size++] = mod(arfcns[i] - l_origin, range);
126 if (mod(arfcns[i] - r_origin, range) < range / 2)
127 arfcns_right[r_size++] = mod(arfcns[i] - r_origin, range);
128 }
129
130 /*
131 * Now recurse and we need to make this iterative... but as the
132 * tree is balanced the stack will not be too deep.
133 */
Max34be86b2016-12-16 18:45:51 +0100134 if (l_size)
135 range_enc_arfcns(range / 2, arfcns_left, l_size,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200136 out, index + greatest_power_of_2_lesser_or_equal_to(index + 1));
Max34be86b2016-12-16 18:45:51 +0100137 if (r_size)
138 range_enc_arfcns((range - 1) / 2, arfcns_right, r_size,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200139 out, index + (2 * greatest_power_of_2_lesser_or_equal_to(index + 1)));
140 return 0;
141}
142
143/*
144 * The easiest is to use f0 == arfcns[0]. This means that under certain
145 * circumstances we can encode less ARFCNs than possible with an optimal f0.
146 *
147 * TODO: Solve the optimisation problem and pick f0 so that the max distance
148 * is the smallest. Taking into account the modulo operation. I think picking
149 * size/2 will be the optimal arfcn.
150 */
151/**
152 * This implements the range determination as described in GSM 04.08 J4. The
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100153 * result will be a base frequency f0 and the range to use. Note that for range
154 * 1024 encoding f0 always refers to ARFCN 0 even if it is not an element of
155 * the arfcns list.
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200156 *
157 * \param[in] arfcns The input frequencies, they must be sorted, lowest number first
158 * \param[in] size The length of the array
159 * \param[out] f0 The selected F0 base frequency. It might not be inside the list
160 */
161int range_enc_determine_range(const int *arfcns, const int size, int *f0)
162{
163 int max = 0;
164
165 /*
166 * Go for the easiest. And pick arfcns[0] == f0.
167 */
168 max = arfcns[size - 1] - arfcns[0];
169 *f0 = arfcns[0];
170
171 if (max < 128 && size <= 29)
172 return ARFCN_RANGE_128;
173 if (max < 256 && size <= 22)
174 return ARFCN_RANGE_256;
175 if (max < 512 && size <= 18)
176 return ARFCN_RANGE_512;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100177 if (max < 1024 && size <= 17) {
178 *f0 = 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200179 return ARFCN_RANGE_1024;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100180 }
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200181
182 return ARFCN_RANGE_INVALID;
183}
184
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200185static void write_orig_arfcn(uint8_t *chan_list, int f0)
186{
187 chan_list[0] |= (f0 >> 9) & 1;
188 chan_list[1] = (f0 >> 1);
189 chan_list[2] = (f0 & 1) << 7;
190}
191
Jacob Erlbeckf8f72e22014-01-10 17:46:51 +0100192static void write_all_wn(uint8_t *chan_list, int bit_offs,
193 int *w, int w_size, int w1_len)
194{
195 int octet_offs = 0; /* offset into chan_list */
196 int wk_len = w1_len; /* encoding size in bits of w[k] */
197 int k; /* 1 based */
198 int level = 0; /* tree level, top level = 0 */
199 int lvl_left = 1; /* nodes per tree level */
200
201 /* W(2^i) to W(2^(i+1)-1) are on w1_len-i bits when present */
202
203 for (k = 1; k <= w_size; k++) {
204 int wk_left = wk_len;
205 DEBUGP(DRR,
206 "k=%d, wk_len=%d, offs=%d:%d, level=%d, "
207 "lvl_left=%d\n",
208 k, wk_len, octet_offs, bit_offs, level, lvl_left);
209
210 while (wk_left > 0) {
211 int cur_bits = 8 - bit_offs;
212 int cur_mask;
213 int wk_slice;
214
215 if (cur_bits > wk_left)
216 cur_bits = wk_left;
217
218 cur_mask = ((1 << cur_bits) - 1);
219
220 DEBUGP(DRR,
221 " wk_left=%d, cur_bits=%d, offs=%d:%d\n",
222 wk_left, cur_bits, octet_offs, bit_offs);
223
224 /* advance */
225 wk_left -= cur_bits;
226 bit_offs += cur_bits;
227
228 /* right aligned wk data for current out octet */
229 wk_slice = (w[k-1] >> wk_left) & cur_mask;
230
231 /* cur_bits now contains the number of bits
232 * that are to be copied from wk to the chan_list.
233 * wk_left is set to the number of bits that must
234 * not yet be copied.
235 * bit_offs points after the bit area that is going to
236 * be overwritten:
237 *
238 * wk_left
239 * |
240 * v
241 * wk: WWWWWWWWWWW
242 * |||||<-- wk_slice, cur_bits=5
243 * --WWWWW-
244 * ^
245 * |
246 * bit_offs
247 */
248
249 DEBUGP(DRR,
250 " wk=%02x, slice=%02x/%02x, cl=%02x\n",
251 w[k-1], wk_slice, cur_mask, wk_slice << (8 - bit_offs));
252
253 chan_list[octet_offs] &= ~(cur_mask << (8 - bit_offs));
254 chan_list[octet_offs] |= wk_slice << (8 - bit_offs);
255
256 /* adjust output */
257 if (bit_offs == 8) {
258 bit_offs = 0;
259 octet_offs += 1;
260 }
261 }
262
263 /* adjust bit sizes */
264 lvl_left -= 1;
265 if (!lvl_left) {
266 /* completed tree level, advance to next */
267 level += 1;
268 lvl_left = 1 << level;
269 wk_len -= 1;
270 }
271 }
272}
273
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200274int range_enc_range128(uint8_t *chan_list, int f0, int *w)
275{
276 chan_list[0] = 0x8C;
277 write_orig_arfcn(chan_list, f0);
278
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100279 write_all_wn(&chan_list[2], 1, w, 28, 7);
280 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200281}
282
283int range_enc_range256(uint8_t *chan_list, int f0, int *w)
284{
285 chan_list[0] = 0x8A;
286 write_orig_arfcn(chan_list, f0);
287
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100288 write_all_wn(&chan_list[2], 1, w, 21, 8);
289 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200290}
291
292int range_enc_range512(uint8_t *chan_list, int f0, int *w)
293{
Holger Hans Peter Freyther27a788d2013-12-12 17:00:57 +0100294 chan_list[0] = 0x88;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200295 write_orig_arfcn(chan_list, f0);
296
Jacob Erlbeckf8f72e22014-01-10 17:46:51 +0100297 write_all_wn(&chan_list[2], 1, w, 17, 9);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200298 return 0;
299}
300
301int range_enc_range1024(uint8_t *chan_list, int f0, int f0_included, int *w)
302{
303 chan_list[0] = 0x80 | (f0_included << 2);
304
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100305 write_all_wn(&chan_list[0], 6, w, 16, 10);
306 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200307}
308
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100309int range_enc_filter_arfcns(int *arfcns,
310 const int size, const int f0, int *f0_included)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200311{
312 int i, j = 0;
313 *f0_included = 0;
314
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100315 for (i = 0; i < size; ++i) {
316 /*
317 * Appendix J.4 says the following:
318 * All frequencies except F(0), minus F(0) + 1.
319 * I assume we need to exclude it here.
320 */
321 if (arfcns[i] == f0) {
322 *f0_included = 1;
323 continue;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200324 }
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200325
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100326 arfcns[j++] = mod(arfcns[i] - (f0 + 1), 1024);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200327 }
328
329 return j;
330}