blob: afe552d89b69a0c5cd7bd766c8040773ebe9fb19 [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
Pau Espin Pedrol6c7ac6a2021-06-01 13:59:52 +0200169 /* don't dereference arfcns[] array if size is 0 */
170 if (size == 0)
171 return OSMO_GSM48_ARFCN_RANGE_128;
172
Stefan Sperlingfdf8b7b2018-07-27 12:19:15 +0200173 /*
174 * Go for the easiest. And pick arfcns[0] == f0.
175 */
176 max = arfcns[size - 1] - arfcns[0];
177 *f0 = arfcns[0];
178
179 if (max < 128 && size <= 29)
180 return OSMO_GSM48_ARFCN_RANGE_128;
181 if (max < 256 && size <= 22)
182 return OSMO_GSM48_ARFCN_RANGE_256;
183 if (max < 512 && size <= 18)
184 return OSMO_GSM48_ARFCN_RANGE_512;
185 if (max < 1024 && size <= 17) {
186 *f0 = 0;
187 return OSMO_GSM48_ARFCN_RANGE_1024;
188 }
189
190 return OSMO_GSM48_ARFCN_RANGE_INVALID;
191}
192
193static void write_orig_arfcn(uint8_t *chan_list, int f0)
194{
195 chan_list[0] |= (f0 >> 9) & 1;
196 chan_list[1] = (f0 >> 1);
197 chan_list[2] = (f0 & 1) << 7;
198}
199
200static void write_all_wn(uint8_t *chan_list, int bit_offs,
201 int *w, int w_size, int w1_len)
202{
203 int octet_offs = 0; /* offset into chan_list */
204 int wk_len = w1_len; /* encoding size in bits of w[k] */
205 int k; /* 1 based */
206 int level = 0; /* tree level, top level = 0 */
207 int lvl_left = 1; /* nodes per tree level */
208
209 /* W(2^i) to W(2^(i+1)-1) are on w1_len-i bits when present */
210
211 for (k = 1; k <= w_size; k++) {
212 int wk_left = wk_len;
213
214 while (wk_left > 0) {
215 int cur_bits = 8 - bit_offs;
216 int cur_mask;
217 int wk_slice;
218
219 if (cur_bits > wk_left)
220 cur_bits = wk_left;
221
222 cur_mask = ((1 << cur_bits) - 1);
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 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
270int osmo_gsm48_range_enc_128(uint8_t *chan_list, int f0, int *w)
271{
272 chan_list[0] = 0x8C;
273 write_orig_arfcn(chan_list, f0);
274
275 write_all_wn(&chan_list[2], 1, w, 28, 7);
276 return 0;
277}
278
279int osmo_gsm48_range_enc_256(uint8_t *chan_list, int f0, int *w)
280{
281 chan_list[0] = 0x8A;
282 write_orig_arfcn(chan_list, f0);
283
284 write_all_wn(&chan_list[2], 1, w, 21, 8);
285 return 0;
286}
287
288int osmo_gsm48_range_enc_512(uint8_t *chan_list, int f0, int *w)
289{
290 chan_list[0] = 0x88;
291 write_orig_arfcn(chan_list, f0);
292
293 write_all_wn(&chan_list[2], 1, w, 17, 9);
294 return 0;
295}
296
297int osmo_gsm48_range_enc_1024(uint8_t *chan_list, int f0, int f0_included, int *w)
298{
299 chan_list[0] = 0x80 | (f0_included << 2);
300
301 write_all_wn(&chan_list[0], 6, w, 16, 10);
302 return 0;
303}
304
305int osmo_gsm48_range_enc_filter_arfcns(int *arfcns, const int size, const int f0, int *f0_included)
306{
307 int i, j = 0;
308 *f0_included = 0;
309
310 for (i = 0; i < size; ++i) {
311 /*
312 * Appendix J.4 says the following:
313 * All frequencies except F(0), minus F(0) + 1.
314 * I assume we need to exclude it here.
315 */
316 if (arfcns[i] == f0) {
317 *f0_included = 1;
318 continue;
319 }
320
321 arfcns[j++] = mod(arfcns[i] - (f0 + 1), 1024);
322 }
323
324 return j;
325}