blob: 97497c8e76b4f1662d5c2e857c7de88f16fd1c38 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/*
2 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
3 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte622b7182010-03-07 17:50:21 +01004 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welteec8b4502010-02-20 20:34:29 +01005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24//#include <openbsc/gsm_data.h>
25#include <osmocore/utils.h>
26#include <osmocore/gsm_utils.h>
27
28#include <stdlib.h>
29#include <stdint.h>
30#include <string.h>
31#include <stdio.h>
32#include <errno.h>
Harald Welteaebe08c2010-03-04 10:39:17 +010033#include <ctype.h>
Harald Welteec8b4502010-02-20 20:34:29 +010034
35#include "../config.h"
36
37/* GSM 03.38 6.2.1 Charachter packing */
38int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length)
39{
40 int i = 0;
41 int l = 0;
42
43 /* FIXME: We need to account for user data headers here */
44 i += l;
45 for (; i < length; i ++)
46 *(text ++) =
47 ((user_data[(i * 7 + 7) >> 3] <<
48 (7 - ((i * 7 + 7) & 7))) |
49 (user_data[(i * 7) >> 3] >>
50 ((i * 7) & 7))) & 0x7f;
51 *text = '\0';
52
53 return i - l;
54}
55
56
57/* GSM 03.38 6.2.1 Charachter packing */
58int gsm_7bit_encode(uint8_t *result, const char *data)
59{
60 int i,j = 0;
61 unsigned char ch1, ch2;
62 int shift = 0;
63
64 for ( i=0; i<strlen(data); i++ ) {
65
66 ch1 = data[i] & 0x7F;
67 ch1 = ch1 >> shift;
68 ch2 = data[(i+1)] & 0x7F;
69 ch2 = ch2 << (7-shift);
70
71 ch1 = ch1 | ch2;
72
73 result[j++] = ch1;
74
75 shift++;
76
77 if ((shift == 7) && (i+1<strlen(data))) {
78 shift = 0;
79 i++;
80 }
81 }
82
83 return i;
84}
85
86/* determine power control level for given dBm value, as indicated
87 * by the tables in chapter 4.1.1 of GSM TS 05.05 */
88int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
89{
90 switch (band) {
91 case GSM_BAND_450:
92 case GSM_BAND_480:
93 case GSM_BAND_750:
94 case GSM_BAND_900:
95 case GSM_BAND_810:
96 case GSM_BAND_850:
97 if (dbm >= 39)
98 return 0;
99 else if (dbm < 5)
100 return 19;
101 else {
102 /* we are guaranteed to have (5 <= dbm < 39) */
103 return 2 + ((39 - dbm) / 2);
104 }
105 break;
106 case GSM_BAND_1800:
107 if (dbm >= 36)
108 return 29;
109 else if (dbm >= 34)
110 return 30;
111 else if (dbm >= 32)
112 return 31;
113 else if (dbm == 31)
114 return 0;
115 else {
116 /* we are guaranteed to have (0 <= dbm < 31) */
117 return (30 - dbm) / 2;
118 }
119 break;
120 case GSM_BAND_1900:
121 if (dbm >= 33)
122 return 30;
123 else if (dbm >= 32)
124 return 31;
125 else if (dbm == 31)
126 return 0;
127 else {
128 /* we are guaranteed to have (0 <= dbm < 31) */
129 return (30 - dbm) / 2;
130 }
131 break;
132 }
133 return -EINVAL;
134}
135
136int ms_pwr_dbm(enum gsm_band band, uint8_t lvl)
137{
138 lvl &= 0x1f;
139
140 switch (band) {
141 case GSM_BAND_450:
142 case GSM_BAND_480:
143 case GSM_BAND_750:
144 case GSM_BAND_900:
145 case GSM_BAND_810:
146 case GSM_BAND_850:
147 if (lvl < 2)
148 return 39;
149 else if (lvl < 20)
150 return 39 - ((lvl - 2) * 2) ;
151 else
152 return 5;
153 break;
154 case GSM_BAND_1800:
155 if (lvl < 16)
156 return 30 - (lvl * 2);
157 else if (lvl < 29)
158 return 0;
159 else
160 return 36 - ((lvl - 29) * 2);
161 break;
162 case GSM_BAND_1900:
163 if (lvl < 16)
164 return 30 - (lvl * 2);
165 else if (lvl < 30)
166 return -EINVAL;
167 else
168 return 33 - (lvl - 30);
169 break;
170 }
171 return -EINVAL;
172}
173
174/* According to TS 08.05 Chapter 8.1.4 */
175int rxlev2dbm(uint8_t rxlev)
176{
177 if (rxlev > 63)
178 rxlev = 63;
179
180 return -110 + rxlev;
181}
182
183/* According to TS 08.05 Chapter 8.1.4 */
184uint8_t dbm2rxlev(int dbm)
185{
186 int rxlev = dbm + 110;
187
188 if (rxlev > 63)
189 rxlev = 63;
190 else if (rxlev < 0)
191 rxlev = 0;
192
193 return rxlev;
194}
195
Harald Welteaebe08c2010-03-04 10:39:17 +0100196char *gsm_band_name(enum gsm_band band)
197{
198 switch (band) {
199 case GSM_BAND_450:
200 return "GSM450";
201 case GSM_BAND_480:
202 return "GSM450";
203 case GSM_BAND_750:
204 return "GSM750";
205 case GSM_BAND_810:
206 return "GSM810";
207 case GSM_BAND_850:
208 return "GSM850";
209 case GSM_BAND_900:
210 return "GSM900";
211 case GSM_BAND_1800:
212 return "DCS1800";
213 case GSM_BAND_1900:
214 return "PCS1900";
215 }
216 return "invalid";
217}
218
219enum gsm_band gsm_band_parse(const char* mhz)
220{
221 while (*mhz && !isdigit(*mhz))
222 mhz++;
223
224 if (*mhz == '\0')
225 return -EINVAL;
226
Harald Welted3ff15f2010-03-07 18:23:47 +0100227 switch (strtol(mhz, NULL, 10)) {
Harald Welteaebe08c2010-03-04 10:39:17 +0100228 case 450:
229 return GSM_BAND_450;
230 case 480:
231 return GSM_BAND_480;
232 case 750:
233 return GSM_BAND_750;
234 case 810:
235 return GSM_BAND_810;
236 case 850:
237 return GSM_BAND_850;
238 case 900:
239 return GSM_BAND_900;
240 case 1800:
241 return GSM_BAND_1800;
242 case 1900:
243 return GSM_BAND_1900;
244 default:
245 return -EINVAL;
246 }
247}
248
249
Harald Welteec8b4502010-02-20 20:34:29 +0100250#ifdef HAVE_EXECINFO_H
251#include <execinfo.h>
252void generate_backtrace()
253{
254 int i, nptrs;
255 void *buffer[100];
256 char **strings;
257
258 nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
259 printf("backtrace() returned %d addresses\n", nptrs);
260
261 strings = backtrace_symbols(buffer, nptrs);
262 if (!strings)
263 return;
264
265 for (i = 1; i < nptrs; i++)
266 printf("%s\n", strings[i]);
267
268 free(strings);
269}
270#endif
Harald Welte622b7182010-03-07 17:50:21 +0100271
272enum gsm_band gsm_arfcn2band(uint16_t arfcn)
273{
274 if (arfcn & ARFCN_PCS)
275 return GSM_BAND_1900;
276 else if (arfcn <= 124)
277 return GSM_BAND_900;
278 else if (arfcn >= 955 && arfcn <= 1023)
279 return GSM_BAND_900;
280 else if (arfcn >= 128 && arfcn <= 251)
281 return GSM_BAND_850;
282 else if (arfcn >= 512 && arfcn <= 885)
283 return GSM_BAND_1800;
284 else if (arfcn >= 259 && arfcn <= 293)
285 return GSM_BAND_450;
286 else if (arfcn >= 306 && arfcn <= 340)
287 return GSM_BAND_480;
288 else if (arfcn >= 350 && arfcn <= 425)
289 return GSM_BAND_810;
290 else if (arfcn >= 438 && arfcn <= 511)
291 return GSM_BAND_750;
292 else
293 return GSM_BAND_1800;
294}
295
296/* Convert an ARFCN to the frequency in MHz * 10 */
297uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink)
298{
299 uint16_t freq10_ul;
300 uint16_t freq10_dl;
301
302 if (arfcn & ARFCN_PCS) {
303 /* DCS 1900 */
304 arfcn &= ~ARFCN_PCS;
305 freq10_ul = 18502 + 2 * (arfcn-512);
306 freq10_dl = freq10_ul + 800;
307 } else if (arfcn <= 124) {
308 /* Primary GSM + ARFCN 0 of E-GSM */
309 freq10_ul = 8900 + 2 * arfcn;
310 freq10_dl = freq10_ul + 450;
311 } else if (arfcn >= 955 && arfcn <= 1023) {
312 /* E-GSM and R-GSM */
313 freq10_ul = 8900 + 2 * (arfcn - 1024);
314 freq10_dl = freq10_ul + 450;
315 } else if (arfcn >= 128 && arfcn <= 251) {
316 /* GSM 850 */
317 freq10_ul = 8242 + 2 * (arfcn - 128);
318 freq10_dl = freq10_ul + 450;
319 } else if (arfcn >= 512 && arfcn <= 885) {
320 /* DCS 1800 */
321 freq10_ul = 17102 + 2 * (arfcn - 512);
322 freq10_dl = freq10_ul + 950;
323 } else if (arfcn >= 259 && arfcn <= 293) {
324 /* GSM 450 */
325 freq10_ul = 4506 + 2 * (arfcn - 259);
326 freq10_dl = freq10_ul + 100;
327 } else if (arfcn >= 306 && arfcn <= 340) {
328 /* GSM 480 */
329 freq10_ul = 4790 + 2 * (arfcn - 306);
330 freq10_dl = freq10_ul + 100;
331 } else if (arfcn >= 350 && arfcn <= 425) {
332 /* GSM 810 */
333 freq10_ul = 8060 + 2 * (arfcn - 350);
334 freq10_dl = freq10_ul + 450;
335 } else if (arfcn >= 438 && arfcn <= 511) {
336 /* GSM 750 */
337 freq10_ul = 7472 + 2 * (arfcn - 438);
338 freq10_dl = freq10_ul + 300;
339 } else
340 return 0xffff;
341
342 if (uplink)
343 return freq10_ul;
344 else
345 return freq10_dl;
346}
347
348void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn)
349{
350 time->fn = fn;
351 time->t1 = time->fn / (26*51);
352 time->t2 = time->fn % 26;
353 time->t3 = time->fn % 51;
354 time->tc = (time->fn / 51) % 8;
355}
356
357uint32_t gsm_gsmtime2fn(struct gsm_time *time)
358{
359 /* TS 05.02 Chapter 4.3.3 TDMA frame number */
360 return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1));
361}