blob: 99188384d55727938a0f25a0549e9d987d83b786 [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
Max34be86b2016-12-16 18:45:51 +010030static inline int greatest_power_of_2_lesser_or_equal_to(int index)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020031{
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
42static 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 */
Max34be86b2016-12-16 18:45:51 +010055int range_enc_find_index(enum gsm48_range range, const int *freqs, const int size)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020056{
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 */
Max34be86b2016-12-16 18:45:51 +010082int range_enc_arfcns(enum gsm48_range range,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020083 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 */
Max34be86b2016-12-16 18:45:51 +0100130 if (l_size)
131 range_enc_arfcns(range / 2, arfcns_left, l_size,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200132 out, index + greatest_power_of_2_lesser_or_equal_to(index + 1));
Max34be86b2016-12-16 18:45:51 +0100133 if (r_size)
134 range_enc_arfcns((range - 1) / 2, arfcns_right, r_size,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200135 out, index + (2 * greatest_power_of_2_lesser_or_equal_to(index + 1)));
136 return 0;
137}
138
139/*
140 * The easiest is to use f0 == arfcns[0]. This means that under certain
141 * circumstances we can encode less ARFCNs than possible with an optimal f0.
142 *
143 * TODO: Solve the optimisation problem and pick f0 so that the max distance
144 * is the smallest. Taking into account the modulo operation. I think picking
145 * size/2 will be the optimal arfcn.
146 */
147/**
148 * This implements the range determination as described in GSM 04.08 J4. The
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100149 * result will be a base frequency f0 and the range to use. Note that for range
150 * 1024 encoding f0 always refers to ARFCN 0 even if it is not an element of
151 * the arfcns list.
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200152 *
153 * \param[in] arfcns The input frequencies, they must be sorted, lowest number first
154 * \param[in] size The length of the array
155 * \param[out] f0 The selected F0 base frequency. It might not be inside the list
156 */
157int range_enc_determine_range(const int *arfcns, const int size, int *f0)
158{
159 int max = 0;
160
161 /*
162 * Go for the easiest. And pick arfcns[0] == f0.
163 */
164 max = arfcns[size - 1] - arfcns[0];
165 *f0 = arfcns[0];
166
167 if (max < 128 && size <= 29)
168 return ARFCN_RANGE_128;
169 if (max < 256 && size <= 22)
170 return ARFCN_RANGE_256;
171 if (max < 512 && size <= 18)
172 return ARFCN_RANGE_512;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100173 if (max < 1024 && size <= 17) {
174 *f0 = 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200175 return ARFCN_RANGE_1024;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100176 }
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200177
178 return ARFCN_RANGE_INVALID;
179}
180
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200181static void write_orig_arfcn(uint8_t *chan_list, int f0)
182{
183 chan_list[0] |= (f0 >> 9) & 1;
184 chan_list[1] = (f0 >> 1);
185 chan_list[2] = (f0 & 1) << 7;
186}
187
Jacob Erlbeckf8f72e22014-01-10 17:46:51 +0100188static void write_all_wn(uint8_t *chan_list, int bit_offs,
189 int *w, int w_size, int w1_len)
190{
191 int octet_offs = 0; /* offset into chan_list */
192 int wk_len = w1_len; /* encoding size in bits of w[k] */
193 int k; /* 1 based */
194 int level = 0; /* tree level, top level = 0 */
195 int lvl_left = 1; /* nodes per tree level */
196
197 /* W(2^i) to W(2^(i+1)-1) are on w1_len-i bits when present */
198
199 for (k = 1; k <= w_size; k++) {
200 int wk_left = wk_len;
201 DEBUGP(DRR,
202 "k=%d, wk_len=%d, offs=%d:%d, level=%d, "
203 "lvl_left=%d\n",
204 k, wk_len, octet_offs, bit_offs, level, lvl_left);
205
206 while (wk_left > 0) {
207 int cur_bits = 8 - bit_offs;
208 int cur_mask;
209 int wk_slice;
210
211 if (cur_bits > wk_left)
212 cur_bits = wk_left;
213
214 cur_mask = ((1 << cur_bits) - 1);
215
216 DEBUGP(DRR,
217 " wk_left=%d, cur_bits=%d, offs=%d:%d\n",
218 wk_left, cur_bits, octet_offs, bit_offs);
219
220 /* advance */
221 wk_left -= cur_bits;
222 bit_offs += cur_bits;
223
224 /* right aligned wk data for current out octet */
225 wk_slice = (w[k-1] >> wk_left) & cur_mask;
226
227 /* cur_bits now contains the number of bits
228 * that are to be copied from wk to the chan_list.
229 * wk_left is set to the number of bits that must
230 * not yet be copied.
231 * bit_offs points after the bit area that is going to
232 * be overwritten:
233 *
234 * wk_left
235 * |
236 * v
237 * wk: WWWWWWWWWWW
238 * |||||<-- wk_slice, cur_bits=5
239 * --WWWWW-
240 * ^
241 * |
242 * bit_offs
243 */
244
245 DEBUGP(DRR,
246 " wk=%02x, slice=%02x/%02x, cl=%02x\n",
247 w[k-1], wk_slice, cur_mask, wk_slice << (8 - bit_offs));
248
249 chan_list[octet_offs] &= ~(cur_mask << (8 - bit_offs));
250 chan_list[octet_offs] |= wk_slice << (8 - bit_offs);
251
252 /* adjust output */
253 if (bit_offs == 8) {
254 bit_offs = 0;
255 octet_offs += 1;
256 }
257 }
258
259 /* adjust bit sizes */
260 lvl_left -= 1;
261 if (!lvl_left) {
262 /* completed tree level, advance to next */
263 level += 1;
264 lvl_left = 1 << level;
265 wk_len -= 1;
266 }
267 }
268}
269
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200270int range_enc_range128(uint8_t *chan_list, int f0, int *w)
271{
272 chan_list[0] = 0x8C;
273 write_orig_arfcn(chan_list, f0);
274
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100275 write_all_wn(&chan_list[2], 1, w, 28, 7);
276 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200277}
278
279int range_enc_range256(uint8_t *chan_list, int f0, int *w)
280{
281 chan_list[0] = 0x8A;
282 write_orig_arfcn(chan_list, f0);
283
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100284 write_all_wn(&chan_list[2], 1, w, 21, 8);
285 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200286}
287
288int range_enc_range512(uint8_t *chan_list, int f0, int *w)
289{
Holger Hans Peter Freyther27a788d2013-12-12 17:00:57 +0100290 chan_list[0] = 0x88;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200291 write_orig_arfcn(chan_list, f0);
292
Jacob Erlbeckf8f72e22014-01-10 17:46:51 +0100293 write_all_wn(&chan_list[2], 1, w, 17, 9);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200294 return 0;
295}
296
297int range_enc_range1024(uint8_t *chan_list, int f0, int f0_included, int *w)
298{
299 chan_list[0] = 0x80 | (f0_included << 2);
300
Jacob Erlbeck64277e62014-01-10 17:47:43 +0100301 write_all_wn(&chan_list[0], 6, w, 16, 10);
302 return 0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200303}
304
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100305int range_enc_filter_arfcns(int *arfcns,
306 const int size, const int f0, int *f0_included)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200307{
308 int i, j = 0;
309 *f0_included = 0;
310
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100311 for (i = 0; i < size; ++i) {
312 /*
313 * Appendix J.4 says the following:
314 * All frequencies except F(0), minus F(0) + 1.
315 * I assume we need to exclude it here.
316 */
317 if (arfcns[i] == f0) {
318 *f0_included = 1;
319 continue;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200320 }
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200321
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100322 arfcns[j++] = mod(arfcns[i] - (f0 + 1), 1024);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200323 }
324
325 return j;
326}