2 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
3 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
24 //#include <openbsc/gsm_data.h>
25 #include <osmocore/utils.h>
26 #include <osmocore/gsm_utils.h>
35 #include "../config.h"
37 /* GSM 03.38 6.2.1 Charachter packing */
38 int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length)
43 /* FIXME: We need to account for user data headers here */
45 for (; i < length; i ++)
47 ((user_data[(i * 7 + 7) >> 3] <<
48 (7 - ((i * 7 + 7) & 7))) |
49 (user_data[(i * 7) >> 3] >>
50 ((i * 7) & 7))) & 0x7f;
57 /* GSM 03.38 6.2.1 Charachter packing */
58 int gsm_7bit_encode(uint8_t *result, const char *data)
61 unsigned char ch1, ch2;
64 for ( i=0; i<strlen(data); i++ ) {
68 ch2 = data[(i+1)] & 0x7F;
69 ch2 = ch2 << (7-shift);
77 if ((shift == 7) && (i+1<strlen(data))) {
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 */
88 int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
102 /* we are guaranteed to have (5 <= dbm < 39) */
103 return 2 + ((39 - dbm) / 2);
116 /* we are guaranteed to have (0 <= dbm < 31) */
117 return (30 - dbm) / 2;
128 /* we are guaranteed to have (0 <= dbm < 31) */
129 return (30 - dbm) / 2;
136 int ms_pwr_dbm(enum gsm_band band, uint8_t lvl)
150 return 39 - ((lvl - 2) * 2) ;
156 return 30 - (lvl * 2);
160 return 36 - ((lvl - 29) * 2);
164 return 30 - (lvl * 2);
168 return 33 - (lvl - 30);
174 /* According to TS 08.05 Chapter 8.1.4 */
175 int rxlev2dbm(uint8_t rxlev)
183 /* According to TS 08.05 Chapter 8.1.4 */
184 uint8_t dbm2rxlev(int dbm)
186 int rxlev = dbm + 110;
196 const char *gsm_band_name(enum gsm_band band)
219 enum gsm_band gsm_band_parse(const char* mhz)
221 while (*mhz && !isdigit(*mhz))
227 switch (strtol(mhz, NULL, 10)) {
241 return GSM_BAND_1800;
243 return GSM_BAND_1900;
250 #ifdef HAVE_EXECINFO_H
251 #include <execinfo.h>
252 void generate_backtrace()
258 nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
259 printf("backtrace() returned %d addresses\n", nptrs);
261 strings = backtrace_symbols(buffer, nptrs);
265 for (i = 1; i < nptrs; i++)
266 printf("%s\n", strings[i]);
272 enum gsm_band gsm_arfcn2band(uint16_t arfcn)
274 if (arfcn & ARFCN_PCS)
275 return GSM_BAND_1900;
276 else if (arfcn <= 124)
278 else if (arfcn >= 955 && arfcn <= 1023)
280 else if (arfcn >= 128 && arfcn <= 251)
282 else if (arfcn >= 512 && arfcn <= 885)
283 return GSM_BAND_1800;
284 else if (arfcn >= 259 && arfcn <= 293)
286 else if (arfcn >= 306 && arfcn <= 340)
288 else if (arfcn >= 350 && arfcn <= 425)
290 else if (arfcn >= 438 && arfcn <= 511)
293 return GSM_BAND_1800;
296 /* Convert an ARFCN to the frequency in MHz * 10 */
297 uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink)
302 if (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) {
317 freq10_ul = 8242 + 2 * (arfcn - 128);
318 freq10_dl = freq10_ul + 450;
319 } else if (arfcn >= 512 && arfcn <= 885) {
321 freq10_ul = 17102 + 2 * (arfcn - 512);
322 freq10_dl = freq10_ul + 950;
323 } else if (arfcn >= 259 && arfcn <= 293) {
325 freq10_ul = 4506 + 2 * (arfcn - 259);
326 freq10_dl = freq10_ul + 100;
327 } else if (arfcn >= 306 && arfcn <= 340) {
329 freq10_ul = 4790 + 2 * (arfcn - 306);
330 freq10_dl = freq10_ul + 100;
331 } else if (arfcn >= 350 && arfcn <= 425) {
333 freq10_ul = 8060 + 2 * (arfcn - 350);
334 freq10_dl = freq10_ul + 450;
335 } else if (arfcn >= 438 && arfcn <= 511) {
337 freq10_ul = 7472 + 2 * (arfcn - 438);
338 freq10_dl = freq10_ul + 300;
348 void gsm_fn2gsmtime(struct gsm_time *time, uint32_t 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;
357 uint32_t gsm_gsmtime2fn(struct gsm_time *time)
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));
363 /* TS 03.03 Chapter 2.6 */
364 int gprs_tlli_type(uint32_t tlli)
366 if ((tlli & 0xc0000000) == 0xc0000000)
368 else if ((tlli & 0xc0000000) == 0x80000000)
370 else if ((tlli & 0xf8000000) == 0x78000000)
372 else if ((tlli & 0xf8000000) == 0x70000000)
373 return TLLI_AUXILIARY;
375 return TLLI_RESERVED;