blob: 5da713c259a23974ae2d1347ffc66822797c2311 [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>
Nico Golde28de0532010-07-09 17:19:12 +02005 * (C) 2010 by Nico Golde <nico@ngolde.de>
Harald Welteec8b4502010-02-20 20:34:29 +01006 *
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 General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/utils.h>
27#include <osmocom/gsm/gsm_utils.h>
Harald Welteec8b4502010-02-20 20:34:29 +010028
29#include <stdlib.h>
30#include <stdint.h>
31#include <string.h>
32#include <stdio.h>
33#include <errno.h>
Harald Welteaebe08c2010-03-04 10:39:17 +010034#include <ctype.h>
Harald Welteec8b4502010-02-20 20:34:29 +010035
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010036#include "../../config.h"
Harald Welteec8b4502010-02-20 20:34:29 +010037
Holger Hans Peter Freytherdd02a472010-07-23 16:35:00 +080038/* ETSI GSM 03.38 6.2.1 and 6.2.1.1 default alphabet
39 * Greek symbols at hex positions 0x10 and 0x12-0x1a
40 * left out as they can't be handled with a char and
41 * since most phones don't display or write these
42 * characters this would only needlessly make the code
43 * more complex
44*/
45static unsigned char gsm_7bit_alphabet[] = {
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0xff, 0xff, 0x0d, 0xff,
47 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
48 0xff, 0xff, 0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
49 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
50 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
51 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
52 0x5a, 0x3c, 0x2f, 0x3e, 0x14, 0x11, 0xff, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
53 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
54 0x78, 0x79, 0x7a, 0x28, 0x40, 0x29, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55 0xff, 0xff, 0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x40, 0xff, 0x01, 0xff,
57 0x03, 0xff, 0x7b, 0x7d, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0xff, 0xff, 0xff, 0xff,
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5b, 0x7e, 0x5d, 0xff, 0x7c, 0xff, 0xff, 0xff,
59 0xff, 0x5b, 0x0e, 0x1c, 0x09, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5d,
60 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0x0b, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff, 0x1e, 0x7f,
61 0xff, 0xff, 0xff, 0x7b, 0x0f, 0x1d, 0xff, 0x04, 0x05, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
62 0xff, 0x7d, 0x08, 0xff, 0xff, 0xff, 0x7c, 0xff, 0x0c, 0x06, 0xff, 0xff, 0x7e, 0xff, 0xff
63};
64
Nico Golde28de0532010-07-09 17:19:12 +020065/* GSM 03.38 6.2.1 Character lookup for decoding */
66static int gsm_septet_lookup(uint8_t ch)
67{
68 int i = 0;
69 for(; i < sizeof(gsm_7bit_alphabet); i++){
70 if(gsm_7bit_alphabet[i] == ch)
71 return i;
72 }
73 return -1;
74}
75
76/* GSM 03.38 6.2.1 Character unpacking */
Harald Welteec8b4502010-02-20 20:34:29 +010077int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length)
78{
79 int i = 0;
80 int l = 0;
Nico Goldec0ce9aa2010-07-20 15:43:58 +020081 int septet_l = (length * 8) / 7;
Holger Hans Peter Freytherf8a342c2010-07-21 03:14:01 +080082 uint8_t *rtext = calloc(septet_l, sizeof(uint8_t));
Nico Golde28de0532010-07-09 17:19:12 +020083 uint8_t tmp;
Harald Welteec8b4502010-02-20 20:34:29 +010084
Nico Golde28de0532010-07-09 17:19:12 +020085 /* FIXME: We need to account for user data headers here */
Harald Welteec8b4502010-02-20 20:34:29 +010086 i += l;
Nico Goldec0ce9aa2010-07-20 15:43:58 +020087 for (; i < septet_l; i++){
Nico Golde28de0532010-07-09 17:19:12 +020088 rtext[i] =
Harald Welteec8b4502010-02-20 20:34:29 +010089 ((user_data[(i * 7 + 7) >> 3] <<
90 (7 - ((i * 7 + 7) & 7))) |
91 (user_data[(i * 7) >> 3] >>
92 ((i * 7) & 7))) & 0x7f;
Nico Golde28de0532010-07-09 17:19:12 +020093 }
Nico Goldec0ce9aa2010-07-20 15:43:58 +020094
95 for(i = 0; i < septet_l; i++){
Nico Golde28de0532010-07-09 17:19:12 +020096 /* this is an extension character */
Holger Hans Peter Freyther446bf372010-07-20 02:54:54 +080097 if(rtext[i] == 0x1b && i + 1 < length){
Nico Golde28de0532010-07-09 17:19:12 +020098 tmp = rtext[i+1];
99 *(text++) = gsm_7bit_alphabet[0x7f + tmp];
Harald Welteec8b4502010-02-20 20:34:29 +0100100 i++;
Nico Golde28de0532010-07-09 17:19:12 +0200101 continue;
Harald Welteec8b4502010-02-20 20:34:29 +0100102 }
Nico Golde28de0532010-07-09 17:19:12 +0200103
104 *(text++) = gsm_septet_lookup(rtext[i]);
Harald Welteec8b4502010-02-20 20:34:29 +0100105 }
106
Nico Golde28de0532010-07-09 17:19:12 +0200107 *text = '\0';
108 free(rtext);
109
110 return i;
111}
112
113/* GSM 03.38 6.2.1 Prepare character packing */
114static int gsm_septet_encode(uint8_t *result, const char *data)
115{
116 int i, y = 0;
117 uint8_t ch;
118 for(i = 0; i < strlen(data); i++){
119 ch = data[i];
120 switch(ch){
121 /* fall-through for extension characters */
122 case 0x0c:
123 case 0x5e:
124 case 0x7b:
125 case 0x7d:
126 case 0x5c:
127 case 0x5b:
128 case 0x7e:
129 case 0x5d:
130 case 0x7c:
131 result[y++] = 0x1b;
132 default:
133 result[y] = gsm_7bit_alphabet[ch];
134 break;
135 }
136 y++;
137 }
138
139 return y;
140}
141
142/* GSM 03.38 6.2.1 Character packing */
143int gsm_7bit_encode(uint8_t *result, const char *data)
144{
145 int i,y,z = 0;
146 /* prepare for the worst case, every character expanding to two bytes */
147 uint8_t *rdata = calloc(strlen(data) * 2, sizeof(uint8_t));
148 uint8_t cb, nb;
149 int shift = 0;
150
151 y = gsm_septet_encode(rdata, data);
152
153 for(i = 0; i < y; i++) {
154 if(shift == 7 && i + 1 < y){
155 shift = 0;
156 continue;
157 }
158
159 cb = (rdata[i] & 0x7f) >> shift;
160 if(i + 1 < y){
161 nb = (rdata[i + 1] & 0x7f) << (7 - shift);
162 cb = cb | nb;
163 }
164
165 result[z++] = cb;
166
167 shift++;
168 }
169
170 free(rdata);
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200171 return z;
Harald Welteec8b4502010-02-20 20:34:29 +0100172}
173
Andreas Eversberg2a68c7c2011-06-26 11:41:48 +0200174/* convert power class to dBm according to GSM TS 05.05 */
175unsigned int ms_class_gmsk_dbm(enum gsm_band band, int class)
176{
177 switch (band) {
178 case GSM_BAND_450:
179 case GSM_BAND_480:
180 case GSM_BAND_750:
181 case GSM_BAND_900:
182 case GSM_BAND_810:
183 case GSM_BAND_850:
184 if (class == 1)
185 return 43; /* 20W */
186 if (class == 2)
187 return 39; /* 8W */
188 if (class == 3)
189 return 37; /* 5W */
190 if (class == 4)
191 return 33; /* 2W */
192 if (class == 5)
193 return 29; /* 0.8W */
194 break;
195 case GSM_BAND_1800:
196 if (class == 1)
197 return 30; /* 1W */
198 if (class == 2)
199 return 24; /* 0.25W */
200 if (class == 3)
201 return 36; /* 4W */
202 break;
203 case GSM_BAND_1900:
204 if (class == 1)
205 return 30; /* 1W */
206 if (class == 2)
207 return 24; /* 0.25W */
208 if (class == 3)
209 return 33; /* 2W */
210 break;
211 }
212 return -EINVAL;
213}
214
Harald Welteec8b4502010-02-20 20:34:29 +0100215/* determine power control level for given dBm value, as indicated
216 * by the tables in chapter 4.1.1 of GSM TS 05.05 */
217int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
218{
219 switch (band) {
220 case GSM_BAND_450:
221 case GSM_BAND_480:
222 case GSM_BAND_750:
223 case GSM_BAND_900:
224 case GSM_BAND_810:
225 case GSM_BAND_850:
226 if (dbm >= 39)
227 return 0;
228 else if (dbm < 5)
229 return 19;
230 else {
231 /* we are guaranteed to have (5 <= dbm < 39) */
232 return 2 + ((39 - dbm) / 2);
233 }
234 break;
235 case GSM_BAND_1800:
236 if (dbm >= 36)
237 return 29;
238 else if (dbm >= 34)
239 return 30;
240 else if (dbm >= 32)
241 return 31;
242 else if (dbm == 31)
243 return 0;
244 else {
245 /* we are guaranteed to have (0 <= dbm < 31) */
246 return (30 - dbm) / 2;
247 }
248 break;
249 case GSM_BAND_1900:
250 if (dbm >= 33)
251 return 30;
252 else if (dbm >= 32)
253 return 31;
254 else if (dbm == 31)
255 return 0;
256 else {
257 /* we are guaranteed to have (0 <= dbm < 31) */
258 return (30 - dbm) / 2;
259 }
260 break;
261 }
262 return -EINVAL;
263}
264
265int ms_pwr_dbm(enum gsm_band band, uint8_t lvl)
266{
267 lvl &= 0x1f;
268
269 switch (band) {
270 case GSM_BAND_450:
271 case GSM_BAND_480:
272 case GSM_BAND_750:
273 case GSM_BAND_900:
274 case GSM_BAND_810:
275 case GSM_BAND_850:
276 if (lvl < 2)
277 return 39;
278 else if (lvl < 20)
279 return 39 - ((lvl - 2) * 2) ;
280 else
281 return 5;
282 break;
283 case GSM_BAND_1800:
284 if (lvl < 16)
285 return 30 - (lvl * 2);
286 else if (lvl < 29)
287 return 0;
288 else
289 return 36 - ((lvl - 29) * 2);
290 break;
291 case GSM_BAND_1900:
292 if (lvl < 16)
293 return 30 - (lvl * 2);
294 else if (lvl < 30)
295 return -EINVAL;
296 else
297 return 33 - (lvl - 30);
298 break;
299 }
300 return -EINVAL;
301}
302
303/* According to TS 08.05 Chapter 8.1.4 */
304int rxlev2dbm(uint8_t rxlev)
305{
306 if (rxlev > 63)
307 rxlev = 63;
308
309 return -110 + rxlev;
310}
311
312/* According to TS 08.05 Chapter 8.1.4 */
313uint8_t dbm2rxlev(int dbm)
314{
315 int rxlev = dbm + 110;
316
317 if (rxlev > 63)
318 rxlev = 63;
319 else if (rxlev < 0)
320 rxlev = 0;
321
322 return rxlev;
323}
324
Harald Weltecbc80622010-03-22 08:28:44 +0800325const char *gsm_band_name(enum gsm_band band)
Harald Welteaebe08c2010-03-04 10:39:17 +0100326{
327 switch (band) {
328 case GSM_BAND_450:
329 return "GSM450";
330 case GSM_BAND_480:
Sylvain Munaute10ae5b2010-07-04 11:41:36 +0200331 return "GSM480";
Harald Welteaebe08c2010-03-04 10:39:17 +0100332 case GSM_BAND_750:
333 return "GSM750";
334 case GSM_BAND_810:
335 return "GSM810";
336 case GSM_BAND_850:
337 return "GSM850";
338 case GSM_BAND_900:
339 return "GSM900";
340 case GSM_BAND_1800:
341 return "DCS1800";
342 case GSM_BAND_1900:
343 return "PCS1900";
344 }
345 return "invalid";
346}
347
348enum gsm_band gsm_band_parse(const char* mhz)
349{
350 while (*mhz && !isdigit(*mhz))
351 mhz++;
352
353 if (*mhz == '\0')
354 return -EINVAL;
355
Harald Welted3ff15f2010-03-07 18:23:47 +0100356 switch (strtol(mhz, NULL, 10)) {
Harald Welteaebe08c2010-03-04 10:39:17 +0100357 case 450:
358 return GSM_BAND_450;
359 case 480:
360 return GSM_BAND_480;
361 case 750:
362 return GSM_BAND_750;
363 case 810:
364 return GSM_BAND_810;
365 case 850:
366 return GSM_BAND_850;
367 case 900:
368 return GSM_BAND_900;
369 case 1800:
370 return GSM_BAND_1800;
371 case 1900:
372 return GSM_BAND_1900;
373 default:
374 return -EINVAL;
375 }
376}
377
Harald Welte622b7182010-03-07 17:50:21 +0100378enum gsm_band gsm_arfcn2band(uint16_t arfcn)
379{
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100380 int is_pcs = arfcn & ARFCN_PCS;
381
382 arfcn &= ~ARFCN_FLAG_MASK;
383
384 if (is_pcs)
Harald Welte622b7182010-03-07 17:50:21 +0100385 return GSM_BAND_1900;
386 else if (arfcn <= 124)
387 return GSM_BAND_900;
388 else if (arfcn >= 955 && arfcn <= 1023)
389 return GSM_BAND_900;
390 else if (arfcn >= 128 && arfcn <= 251)
391 return GSM_BAND_850;
392 else if (arfcn >= 512 && arfcn <= 885)
393 return GSM_BAND_1800;
394 else if (arfcn >= 259 && arfcn <= 293)
395 return GSM_BAND_450;
396 else if (arfcn >= 306 && arfcn <= 340)
397 return GSM_BAND_480;
398 else if (arfcn >= 350 && arfcn <= 425)
399 return GSM_BAND_810;
400 else if (arfcn >= 438 && arfcn <= 511)
401 return GSM_BAND_750;
402 else
403 return GSM_BAND_1800;
404}
405
406/* Convert an ARFCN to the frequency in MHz * 10 */
407uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink)
408{
409 uint16_t freq10_ul;
410 uint16_t freq10_dl;
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100411 int is_pcs = arfcn & ARFCN_PCS;
Harald Welte622b7182010-03-07 17:50:21 +0100412
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100413 arfcn &= ~ARFCN_FLAG_MASK;
414
415 if (is_pcs) {
Harald Welte622b7182010-03-07 17:50:21 +0100416 /* DCS 1900 */
417 arfcn &= ~ARFCN_PCS;
418 freq10_ul = 18502 + 2 * (arfcn-512);
419 freq10_dl = freq10_ul + 800;
420 } else if (arfcn <= 124) {
421 /* Primary GSM + ARFCN 0 of E-GSM */
422 freq10_ul = 8900 + 2 * arfcn;
423 freq10_dl = freq10_ul + 450;
424 } else if (arfcn >= 955 && arfcn <= 1023) {
425 /* E-GSM and R-GSM */
426 freq10_ul = 8900 + 2 * (arfcn - 1024);
427 freq10_dl = freq10_ul + 450;
428 } else if (arfcn >= 128 && arfcn <= 251) {
429 /* GSM 850 */
430 freq10_ul = 8242 + 2 * (arfcn - 128);
431 freq10_dl = freq10_ul + 450;
432 } else if (arfcn >= 512 && arfcn <= 885) {
433 /* DCS 1800 */
434 freq10_ul = 17102 + 2 * (arfcn - 512);
435 freq10_dl = freq10_ul + 950;
436 } else if (arfcn >= 259 && arfcn <= 293) {
437 /* GSM 450 */
438 freq10_ul = 4506 + 2 * (arfcn - 259);
439 freq10_dl = freq10_ul + 100;
440 } else if (arfcn >= 306 && arfcn <= 340) {
441 /* GSM 480 */
442 freq10_ul = 4790 + 2 * (arfcn - 306);
443 freq10_dl = freq10_ul + 100;
444 } else if (arfcn >= 350 && arfcn <= 425) {
445 /* GSM 810 */
446 freq10_ul = 8060 + 2 * (arfcn - 350);
447 freq10_dl = freq10_ul + 450;
448 } else if (arfcn >= 438 && arfcn <= 511) {
449 /* GSM 750 */
450 freq10_ul = 7472 + 2 * (arfcn - 438);
451 freq10_dl = freq10_ul + 300;
452 } else
453 return 0xffff;
454
455 if (uplink)
456 return freq10_ul;
457 else
458 return freq10_dl;
459}
460
461void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn)
462{
463 time->fn = fn;
464 time->t1 = time->fn / (26*51);
465 time->t2 = time->fn % 26;
466 time->t3 = time->fn % 51;
467 time->tc = (time->fn / 51) % 8;
468}
469
470uint32_t gsm_gsmtime2fn(struct gsm_time *time)
471{
472 /* TS 05.02 Chapter 4.3.3 TDMA frame number */
473 return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1));
474}
Harald Weltea1c4f762010-05-01 11:59:42 +0200475
476/* TS 03.03 Chapter 2.6 */
477int gprs_tlli_type(uint32_t tlli)
478{
479 if ((tlli & 0xc0000000) == 0xc0000000)
480 return TLLI_LOCAL;
481 else if ((tlli & 0xc0000000) == 0x80000000)
482 return TLLI_FOREIGN;
483 else if ((tlli & 0xf8000000) == 0x78000000)
484 return TLLI_RANDOM;
485 else if ((tlli & 0xf8000000) == 0x70000000)
486 return TLLI_AUXILIARY;
487
488 return TLLI_RESERVED;
489}
Harald Weltec2263172010-06-01 10:47:07 +0200490
491uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type)
492{
493 uint32_t tlli;
494 switch (type) {
495 case TLLI_LOCAL:
496 tlli = p_tmsi | 0xc0000000;
497 break;
498 case TLLI_FOREIGN:
499 tlli = (p_tmsi & 0x3fffffff) | 0x80000000;
500 break;
501 default:
502 tlli = 0;
503 break;
504 }
505 return tlli;
506}