blob: 02a75a53cc5c7459f1b7cf402bdf8e332ddd0daa [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
30int 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 range_enc_find_index(const int 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/**
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 */
82int range_enc_arfcns(const int range,
83 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 */
130 range_enc_arfcns(range / 2, arfcns_left, l_size,
131 out, index + greatest_power_of_2_lesser_or_equal_to(index + 1));
132 range_enc_arfcns((range -1 ) / 2, arfcns_right, r_size,
133 out, index + (2 * greatest_power_of_2_lesser_or_equal_to(index + 1)));
134 return 0;
135}
136
137/*
138 * The easiest is to use f0 == arfcns[0]. This means that under certain
139 * circumstances we can encode less ARFCNs than possible with an optimal f0.
140 *
141 * TODO: Solve the optimisation problem and pick f0 so that the max distance
142 * is the smallest. Taking into account the modulo operation. I think picking
143 * size/2 will be the optimal arfcn.
144 */
145/**
146 * This implements the range determination as described in GSM 04.08 J4. The
147 * result will be a base frequency f0 and the range to use.
148 *
149 * \param[in] arfcns The input frequencies, they must be sorted, lowest number first
150 * \param[in] size The length of the array
151 * \param[out] f0 The selected F0 base frequency. It might not be inside the list
152 */
153int range_enc_determine_range(const int *arfcns, const int size, int *f0)
154{
155 int max = 0;
156
157 /*
158 * Go for the easiest. And pick arfcns[0] == f0.
159 */
160 max = arfcns[size - 1] - arfcns[0];
161 *f0 = arfcns[0];
162
163 if (max < 128 && size <= 29)
164 return ARFCN_RANGE_128;
165 if (max < 256 && size <= 22)
166 return ARFCN_RANGE_256;
167 if (max < 512 && size <= 18)
168 return ARFCN_RANGE_512;
169 if (max < 1024 && size <= 17)
170 return ARFCN_RANGE_1024;
171
172 return ARFCN_RANGE_INVALID;
173}
174
175/*
176 * The below is easier is to write in four methods than
177 * to use the max_bits. The encoding is so screwed.. as
178 * the bits need to be put in place in the wrong order..
179 */
180#define HIGH_BITS(w, index, bits, offset) \
181 (w[index - 1] >> (bits - offset))
182#define LOW_BITS(w, index, bits, offset) \
183 (w[index - 1])
184
185static 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
192int range_enc_range128(uint8_t *chan_list, int f0, int *w)
193{
194 chan_list[0] = 0x8C;
195 write_orig_arfcn(chan_list, f0);
196
197 LOGP(DRR, LOGL_ERROR, "Range128 encoding is not implemented.\n");
198 return -1;
199}
200
201int range_enc_range256(uint8_t *chan_list, int f0, int *w)
202{
203 chan_list[0] = 0x8A;
204 write_orig_arfcn(chan_list, f0);
205
206 LOGP(DRR, LOGL_ERROR, "Range256 encoding is not implemented.\n");
207 return -1;
208}
209
210int range_enc_range512(uint8_t *chan_list, int f0, int *w)
211{
212 struct gsm48_range_512 *range512;
213 write_orig_arfcn(chan_list, f0);
214
215 range512 = (struct gsm48_range_512 *) &chan_list[0];
216 range512->form_id = chan_list[0] = 0x44;
217
218 /* W(1) */
219 range512->w1_hi = HIGH_BITS(w, 1, 9, 7);
220 range512->w1_lo = LOW_BITS (w, 1, 9, 2);
221 /* W(2) */
222 range512->w2_hi = HIGH_BITS(w, 2, 8, 6);
223 range512->w2_lo = LOW_BITS (w, 2, 8, 2);
224 /* W(3) */
225 range512->w3_hi = HIGH_BITS(w, 3, 8, 6);
226 range512->w3_lo = LOW_BITS (w, 3, 8, 2);
227 /* W(4) */
228 range512->w4_hi = HIGH_BITS(w, 4, 7, 6);
229 range512->w4_lo = LOW_BITS (w, 4, 7, 1);
230 /* W(5) */
231 range512->w5 = HIGH_BITS(w, 5, 7, 7);
232 /* W(6) */
233 range512->w6 = HIGH_BITS(w, 6, 7, 7);
234 /* W(7) */
235 range512->w7_hi = HIGH_BITS(w, 7, 7, 1);
236 range512->w7_lo = LOW_BITS (w, 7, 7, 6);
237 /* W(8) */
238 range512->w8_hi = HIGH_BITS(w, 8, 6, 2);
239 range512->w8_lo = LOW_BITS (w, 8, 6, 4);
240 /* W(9) */
241 range512->w9_hi = HIGH_BITS(w, 9, 6, 4);
242 range512->w9_lo = LOW_BITS(w, 9, 6, 2);
243 /* W(10) */
244 range512->w10 = HIGH_BITS(w, 10, 6, 6);
245 /* W(11) */
246 range512->w11 = HIGH_BITS(w, 11, 6, 6);
247 /* W(12) */
248 range512->w12_hi = HIGH_BITS(w, 12, 6, 2);
249 range512->w12_lo = LOW_BITS (w, 12, 6, 4);
250 /* W(13) */
251 range512->w13_hi = HIGH_BITS(w, 13, 6, 4);
252 range512->w13_lo = LOW_BITS(w, 13, 6, 2);
253 /* W(14) */
254 range512->w14 = HIGH_BITS(w, 14, 6, 6);
255 /* W(15) */
256 range512->w15 = HIGH_BITS(w, 15, 6, 6);
257 /* W(16) */
258 range512->w16_hi = HIGH_BITS(w, 16, 5, 2);
259 range512->w16_lo = HIGH_BITS(w, 16, 5, 3);
260 /* W(17) */
261 range512->w17 = HIGH_BITS(w, 17, 5, 5);
262
263 return 0;
264}
265
266int range_enc_range1024(uint8_t *chan_list, int f0, int f0_included, int *w)
267{
268 chan_list[0] = 0x80 | (f0_included << 2);
269
270 LOGP(DRR, LOGL_ERROR, "Range1024 encoding is not implemented.\n");
271 return -1;
272}
273
274int range_enc_filter_arfcns(const int range, int *arfcns,
275 const int size, const int f0, int *f0_included)
276{
277 int i, j = 0;
278 *f0_included = 0;
279
280 if (range == ARFCN_RANGE_1024) {
281 for (i = 0; i < size; ++i) {
282 if (arfcns[i] == f0) {
283 *f0_included = 1;
284 continue;
285 }
286
287 /* copy and subtract */
288 arfcns[j++] = mod(arfcns[i] - 1, 1024);
289 }
290 } else {
291 for (i = 0; i < size; ++i) {
292 /*
293 * Appendix J.4 says the following:
294 * All frequencies except F(0), minus F(0) + 1.
295 * I assume we need to exclude it here.
296 */
297 if (arfcns[i] == f0)
298 continue;
299
300 arfcns[j++] = mod(arfcns[i] - (f0 + 1), 1024);
301 }
302 }
303
304 return j;
305}