blob: 6423a9a8c1085f24b8ce85751dea828bdb4b7a93 [file] [log] [blame]
Stefan Sperlingfdf8b7b2018-07-27 12:19:15 +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 <osmocom/gsm/protocol/gsm_04_08.h>
24#include <osmocom/gsm/gsm48_arfcn_range_encode.h>
25
26#include <osmocom/core/utils.h>
27
28#include <errno.h>
29
30static inline 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
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 */
55int osmo_gsm48_range_enc_find_index(enum osmo_gsm48_range 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/* Worker for range_enc_arfcns(), do not call directly. */
76static int _range_enc_arfcns(enum osmo_gsm48_range range,
77 const int *arfcns, int size, int *out,
78 const int index)
79{
80 int split_at;
81 int i;
82
83 /*
84 * The below is a GNU extension and we can remove it when
85 * we move to a quicksort like in-situ swap with the pivot.
86 */
87 int arfcns_left[size / 2];
88 int arfcns_right[size / 2];
89 int l_size;
90 int r_size;
91 int l_origin;
92 int r_origin;
93
94 /* Now do the processing */
95 split_at = osmo_gsm48_range_enc_find_index(range, arfcns, size);
96 if (split_at < 0)
97 return -EINVAL;
98
99 /* we now know where to split */
100 out[index] = 1 + arfcns[split_at];
101
102 /* calculate the work that needs to be done for the leafs */
103 l_origin = mod(arfcns[split_at] + ((range - 1) / 2) + 1, range);
104 r_origin = mod(arfcns[split_at] + 1, range);
105 for (i = 0, l_size = 0, r_size = 0; i < size; ++i) {
106 if (mod(arfcns[i] - l_origin, range) < range / 2)
107 arfcns_left[l_size++] = mod(arfcns[i] - l_origin, range);
108 if (mod(arfcns[i] - r_origin, range) < range / 2)
109 arfcns_right[r_size++] = mod(arfcns[i] - r_origin, range);
110 }
111
112 /*
113 * Now recurse and we need to make this iterative... but as the
114 * tree is balanced the stack will not be too deep.
115 */
116 if (l_size)
117 osmo_gsm48_range_enc_arfcns(range / 2, arfcns_left, l_size,
118 out, index + greatest_power_of_2_lesser_or_equal_to(index + 1));
119 if (r_size)
120 osmo_gsm48_range_enc_arfcns((range - 1) / 2, arfcns_right, r_size,
121 out, index + (2 * greatest_power_of_2_lesser_or_equal_to(index + 1)));
122 return 0;
123}
124
125/**
126 * Range encode the ARFCN list.
127 * \param range The range to use.
128 * \param arfcns The list of ARFCNs
129 * \param size The size of the list of ARFCNs
130 * \param out Place to store the W(i) output.
131 */
132int osmo_gsm48_range_enc_arfcns(enum osmo_gsm48_range range,
133 const int *arfcns, int size, int *out,
134 const int index)
135{
136 if (size <= 0)
137 return 0;
138
139 if (size == 1) {
140 out[index] = 1 + arfcns[0];
141 return 0;
142 }
143
144 return _range_enc_arfcns(range, arfcns, size, out, index);
145}
146
147/*
148 * The easiest is to use f0 == arfcns[0]. This means that under certain
149 * circumstances we can encode less ARFCNs than possible with an optimal f0.
150 *
151 * TODO: Solve the optimisation problem and pick f0 so that the max distance
152 * is the smallest. Taking into account the modulo operation. I think picking
153 * size/2 will be the optimal arfcn.
154 */
155/**
156 * This implements the range determination as described in GSM 04.08 J4. The
157 * result will be a base frequency f0 and the range to use. Note that for range
158 * 1024 encoding f0 always refers to ARFCN 0 even if it is not an element of
159 * the arfcns list.
160 *
161 * \param[in] arfcns The input frequencies, they must be sorted, lowest number first
162 * \param[in] size The length of the array
163 * \param[out] f0 The selected F0 base frequency. It might not be inside the list
164 */
165int osmo_gsm48_range_enc_determine_range(const int *arfcns, const int size, int *f0)
166{
167 int max = 0;
168
169 /*
170 * Go for the easiest. And pick arfcns[0] == f0.
171 */
172 max = arfcns[size - 1] - arfcns[0];
173 *f0 = arfcns[0];
174
175 if (max < 128 && size <= 29)
176 return OSMO_GSM48_ARFCN_RANGE_128;
177 if (max < 256 && size <= 22)
178 return OSMO_GSM48_ARFCN_RANGE_256;
179 if (max < 512 && size <= 18)
180 return OSMO_GSM48_ARFCN_RANGE_512;
181 if (max < 1024 && size <= 17) {
182 *f0 = 0;
183 return OSMO_GSM48_ARFCN_RANGE_1024;
184 }
185
186 return OSMO_GSM48_ARFCN_RANGE_INVALID;
187}
188
189static void write_orig_arfcn(uint8_t *chan_list, int f0)
190{
191 chan_list[0] |= (f0 >> 9) & 1;
192 chan_list[1] = (f0 >> 1);
193 chan_list[2] = (f0 & 1) << 7;
194}
195
196static void write_all_wn(uint8_t *chan_list, int bit_offs,
197 int *w, int w_size, int w1_len)
198{
199 int octet_offs = 0; /* offset into chan_list */
200 int wk_len = w1_len; /* encoding size in bits of w[k] */
201 int k; /* 1 based */
202 int level = 0; /* tree level, top level = 0 */
203 int lvl_left = 1; /* nodes per tree level */
204
205 /* W(2^i) to W(2^(i+1)-1) are on w1_len-i bits when present */
206
207 for (k = 1; k <= w_size; k++) {
208 int wk_left = wk_len;
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 /* 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 chan_list[octet_offs] &= ~(cur_mask << (8 - bit_offs));
246 chan_list[octet_offs] |= wk_slice << (8 - bit_offs);
247
248 /* adjust output */
249 if (bit_offs == 8) {
250 bit_offs = 0;
251 octet_offs += 1;
252 }
253 }
254
255 /* adjust bit sizes */
256 lvl_left -= 1;
257 if (!lvl_left) {
258 /* completed tree level, advance to next */
259 level += 1;
260 lvl_left = 1 << level;
261 wk_len -= 1;
262 }
263 }
264}
265
266int osmo_gsm48_range_enc_128(uint8_t *chan_list, int f0, int *w)
267{
268 chan_list[0] = 0x8C;
269 write_orig_arfcn(chan_list, f0);
270
271 write_all_wn(&chan_list[2], 1, w, 28, 7);
272 return 0;
273}
274
275int osmo_gsm48_range_enc_256(uint8_t *chan_list, int f0, int *w)
276{
277 chan_list[0] = 0x8A;
278 write_orig_arfcn(chan_list, f0);
279
280 write_all_wn(&chan_list[2], 1, w, 21, 8);
281 return 0;
282}
283
284int osmo_gsm48_range_enc_512(uint8_t *chan_list, int f0, int *w)
285{
286 chan_list[0] = 0x88;
287 write_orig_arfcn(chan_list, f0);
288
289 write_all_wn(&chan_list[2], 1, w, 17, 9);
290 return 0;
291}
292
293int osmo_gsm48_range_enc_1024(uint8_t *chan_list, int f0, int f0_included, int *w)
294{
295 chan_list[0] = 0x80 | (f0_included << 2);
296
297 write_all_wn(&chan_list[0], 6, w, 16, 10);
298 return 0;
299}
300
301int osmo_gsm48_range_enc_filter_arfcns(int *arfcns, const int size, const int f0, int *f0_included)
302{
303 int i, j = 0;
304 *f0_included = 0;
305
306 for (i = 0; i < size; ++i) {
307 /*
308 * Appendix J.4 says the following:
309 * All frequencies except F(0), minus F(0) + 1.
310 * I assume we need to exclude it here.
311 */
312 if (arfcns[i] == f0) {
313 *f0_included = 1;
314 continue;
315 }
316
317 arfcns[j++] = mod(arfcns[i] - (f0 + 1), 1024);
318 }
319
320 return j;
321}