sms: SMS where cropped (from VTY), concatenation of SMS where not possible
[osmocom-bb.git] / src / gsm / gsm_utils.c
1 /*
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>
5  * (C) 2010 by Nico Golde <nico@ngolde.de>
6  *
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>
26 #include <osmocom/core/utils.h>
27 #include <osmocom/gsm/gsm_utils.h>
28
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <ctype.h>
35
36 #include "../../config.h"
37
38 /* 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 */
45 static 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
65 /* GSM 03.38 6.2.1 Character lookup for decoding */
66 static 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 /* Compute the number of octets from the number of septets, for instance: 47 septets needs 41,125 = 42 octets */
77 uint8_t gsm_get_octet_len(const uint8_t sept_len){
78         int octet_len = (sept_len * 7) / 8;
79         if ((sept_len * 7) % 8 != 0)
80                 octet_len++;
81
82         return octet_len;
83 }
84
85 /* GSM 03.38 6.2.1 Character unpacking */
86 int gsm_7bit_decode_hdr(char *text, const uint8_t *user_data, uint8_t septet_l, uint8_t ud_hdr_ind)
87 {
88         int i = 0;
89         int shift = 0;
90
91         uint8_t *rtext = calloc(septet_l, sizeof(uint8_t));
92         uint8_t tmp;
93
94         /* skip the user data header */
95         if (ud_hdr_ind) {
96                 /* get user data header length + 1 (for the 'user data header length'-field) */
97                 shift = ((user_data[0] + 1) * 8) / 7;
98                 if ((((user_data[0] + 1) * 8) % 7) != 0)
99                         shift++;
100                 septet_l = septet_l - shift;
101         }
102
103         for (i = 0; i < septet_l; i++) {
104                 rtext[i] =
105                         ((user_data[((i + shift) * 7 + 7) >> 3] <<
106                           (7 - (((i + shift) * 7 + 7) & 7))) |
107                          (user_data[((i + shift) * 7) >> 3] >>
108                           (((i + shift) * 7) & 7))) & 0x7f;
109         }
110
111         for(i = 0; i < septet_l; i++){
112                 /* this is an extension character */
113                 if(rtext[i] == 0x1b && i + 1 < septet_l){
114                         tmp = rtext[i+1];
115                         *(text++) = gsm_7bit_alphabet[0x7f + tmp];
116                         i++;
117                         continue;
118                 }
119
120                 *(text++) = gsm_septet_lookup(rtext[i]);
121         }
122
123         if (ud_hdr_ind)
124                 i += shift;
125         *text = '\0';
126         free(rtext);
127
128         return i;
129 }
130
131 int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t septet_l)
132 {
133         return gsm_7bit_decode_hdr(text, user_data, septet_l, 0);
134 }
135
136 /* GSM 03.38 6.2.1 Prepare character packing */
137 int gsm_septet_encode(uint8_t *result, const char *data)
138 {
139         int i, y = 0;
140         uint8_t ch;
141         for(i = 0; i < strlen(data); i++){
142                 ch = data[i];
143                 switch(ch){
144                 /* fall-through for extension characters */
145                 case 0x0c:
146                 case 0x5e:
147                 case 0x7b:
148                 case 0x7d:
149                 case 0x5c:
150                 case 0x5b:
151                 case 0x7e:
152                 case 0x5d:
153                 case 0x7c:
154                         result[y++] = 0x1b;
155                 default:
156                         result[y] = gsm_7bit_alphabet[ch];
157                         break;
158                 }
159                 y++;
160         }
161
162         return y;
163 }
164
165 /* 7bit to octet packing */
166 int gsm_septets2octets(uint8_t *result, uint8_t *rdata, uint8_t septet_len, uint8_t padding){
167         int i = 0, z = 0;
168         uint8_t cb, nb;
169         int shift = 0;
170         uint8_t *data = calloc(septet_len + 1, sizeof(uint8_t));
171
172         if (padding) {
173                 shift = 7 - padding;
174                 /* the first zero is needed for padding */
175                 memcpy(data + 1, rdata, septet_len);
176                 septet_len++;
177         } else
178                 memcpy(data, rdata, septet_len);
179
180         for(i = 0; i < septet_len; i++) {
181                 if (shift == 7) {
182                         /*
183                          * special end case with the. This is necessary if the
184                          * last septet fits into the previous octet. E.g. 48
185                          * non-extension characters:
186                          *   ....ag ( a = 1100001, g = 1100111)
187                          * result[40] = 100001 XX, result[41] = 1100111 1 */
188                         if (i + 1 < septet_len) {
189                                 shift = 0;
190                                 continue;
191                         } else if (i + 1 == septet_len)
192                                 break;
193                 }
194
195                 cb = (data[i] & 0x7f) >> shift;
196                 if (i + 1 < septet_len) {
197                         nb = (data[i + 1] & 0x7f) << (7 - shift);
198                         cb = cb | nb;
199                 }
200
201                 result[z++] = cb;
202                 shift++;
203         }
204
205         free(data);
206
207         return z;
208 }
209
210 /* GSM 03.38 6.2.1 Character packing */
211 int gsm_7bit_encode(uint8_t *result, const char *data)
212 {
213         int y = 0, z = 0;
214         /* prepare for the worst case, every character expanding to two bytes */
215         uint8_t *rdata = calloc(strlen(data) * 2, sizeof(uint8_t));
216         y = gsm_septet_encode(rdata, data);
217         z = gsm_septets2octets(result, rdata, y, 0);
218
219         free(rdata);
220
221         /*
222          * We don't care about the number of octets (z), because they are not
223          * unique. E.g.:
224          *  1.) 46 non-extension characters + 1 extension character
225          *         => (46 * 7 bit + (1 * (2 * 7 bit))) / 8 bit =  42 octets
226          *  2.) 47 non-extension characters
227          *         => (47 * 7 bit) / 8 bit = 41,125 = 42 octets
228          *  3.) 48 non-extension characters
229          *         => (48 * 7 bit) / 8 bit = 42 octects
230          */
231         return y;
232 }
233
234 /* convert power class to dBm according to GSM TS 05.05 */
235 unsigned int ms_class_gmsk_dbm(enum gsm_band band, int class)
236 {
237         switch (band) {
238         case GSM_BAND_450:
239         case GSM_BAND_480:
240         case GSM_BAND_750:
241         case GSM_BAND_900:
242         case GSM_BAND_810:
243         case GSM_BAND_850:
244                 if (class == 1)
245                         return 43; /* 20W */
246                 if (class == 2)
247                         return 39; /* 8W */
248                 if (class == 3)
249                         return 37; /* 5W */
250                 if (class == 4)
251                         return 33; /* 2W */
252                 if (class == 5)
253                         return 29; /* 0.8W */
254                 break;
255         case GSM_BAND_1800:
256                 if (class == 1)
257                         return 30; /* 1W */
258                 if (class == 2)
259                         return 24; /* 0.25W */
260                 if (class == 3)
261                         return 36; /* 4W */
262                 break;
263         case GSM_BAND_1900:
264                 if (class == 1)
265                         return 30; /* 1W */
266                 if (class == 2)
267                         return 24; /* 0.25W */
268                 if (class == 3)
269                         return 33; /* 2W */
270                 break;
271         }
272         return -EINVAL;
273 }
274
275 /* determine power control level for given dBm value, as indicated
276  * by the tables in chapter 4.1.1 of GSM TS 05.05 */
277 int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
278 {
279         switch (band) {
280         case GSM_BAND_450:
281         case GSM_BAND_480:
282         case GSM_BAND_750:
283         case GSM_BAND_900:
284         case GSM_BAND_810:
285         case GSM_BAND_850:
286                 if (dbm >= 39)
287                         return 0;
288                 else if (dbm < 5)
289                         return 19;
290                 else {
291                         /* we are guaranteed to have (5 <= dbm < 39) */
292                         return 2 + ((39 - dbm) / 2);
293                 }
294                 break;
295         case GSM_BAND_1800:
296                 if (dbm >= 36)
297                         return 29;
298                 else if (dbm >= 34)     
299                         return 30;
300                 else if (dbm >= 32)
301                         return 31;
302                 else if (dbm == 31)
303                         return 0;
304                 else {
305                         /* we are guaranteed to have (0 <= dbm < 31) */
306                         return (30 - dbm) / 2;
307                 }
308                 break;
309         case GSM_BAND_1900:
310                 if (dbm >= 33)
311                         return 30;
312                 else if (dbm >= 32)
313                         return 31;
314                 else if (dbm == 31)
315                         return 0;
316                 else {
317                         /* we are guaranteed to have (0 <= dbm < 31) */
318                         return (30 - dbm) / 2;
319                 }
320                 break;
321         }
322         return -EINVAL;
323 }
324
325 int ms_pwr_dbm(enum gsm_band band, uint8_t lvl)
326 {
327         lvl &= 0x1f;
328
329         switch (band) {
330         case GSM_BAND_450:
331         case GSM_BAND_480:
332         case GSM_BAND_750:
333         case GSM_BAND_900:
334         case GSM_BAND_810:
335         case GSM_BAND_850:
336                 if (lvl < 2)
337                         return 39;
338                 else if (lvl < 20)
339                         return 39 - ((lvl - 2) * 2) ;
340                 else
341                         return 5;
342                 break;
343         case GSM_BAND_1800:
344                 if (lvl < 16)
345                         return 30 - (lvl * 2);
346                 else if (lvl < 29)
347                         return 0;
348                 else
349                         return 36 - ((lvl - 29) * 2);
350                 break;
351         case GSM_BAND_1900:
352                 if (lvl < 16)
353                         return 30 - (lvl * 2);
354                 else if (lvl < 30)
355                         return -EINVAL;
356                 else
357                         return 33 - (lvl - 30);
358                 break;
359         }
360         return -EINVAL;
361 }
362
363 /* According to TS 08.05 Chapter 8.1.4 */
364 int rxlev2dbm(uint8_t rxlev)
365 {
366         if (rxlev > 63)
367                 rxlev = 63;
368
369         return -110 + rxlev;
370 }
371
372 /* According to TS 08.05 Chapter 8.1.4 */
373 uint8_t dbm2rxlev(int dbm)
374 {
375         int rxlev = dbm + 110;
376
377         if (rxlev > 63)
378                 rxlev = 63;
379         else if (rxlev < 0)
380                 rxlev = 0;
381
382         return rxlev;
383 }
384
385 const char *gsm_band_name(enum gsm_band band)
386 {
387         switch (band) {
388         case GSM_BAND_450:
389                 return "GSM450";
390         case GSM_BAND_480:
391                 return "GSM480";
392         case GSM_BAND_750:
393                 return "GSM750";
394         case GSM_BAND_810:
395                 return "GSM810";
396         case GSM_BAND_850:
397                 return "GSM850";
398         case GSM_BAND_900:
399                 return "GSM900";
400         case GSM_BAND_1800:
401                 return "DCS1800";
402         case GSM_BAND_1900:
403                 return "PCS1900";
404         }
405         return "invalid";
406 }
407
408 enum gsm_band gsm_band_parse(const char* mhz)
409 {
410         while (*mhz && !isdigit(*mhz))
411                 mhz++;
412
413         if (*mhz == '\0')
414                 return -EINVAL;
415
416         switch (strtol(mhz, NULL, 10)) {
417         case 450:
418                 return GSM_BAND_450;
419         case 480:
420                 return GSM_BAND_480;
421         case 750:
422                 return GSM_BAND_750;
423         case 810:
424                 return GSM_BAND_810;
425         case 850:
426                 return GSM_BAND_850;
427         case 900:
428                 return GSM_BAND_900;
429         case 1800:
430                 return GSM_BAND_1800;
431         case 1900:
432                 return GSM_BAND_1900;
433         default:
434                 return -EINVAL;
435         }
436 }
437
438 enum gsm_band gsm_arfcn2band(uint16_t arfcn)
439 {
440         int is_pcs = arfcn & ARFCN_PCS;
441
442         arfcn &= ~ARFCN_FLAG_MASK;
443
444         if (is_pcs)
445                 return GSM_BAND_1900;
446         else if (arfcn <= 124)
447                 return GSM_BAND_900;
448         else if (arfcn >= 955 && arfcn <= 1023)
449                 return GSM_BAND_900;
450         else if (arfcn >= 128 && arfcn <= 251)
451                 return GSM_BAND_850;
452         else if (arfcn >= 512 && arfcn <= 885)
453                 return GSM_BAND_1800;
454         else if (arfcn >= 259 && arfcn <= 293)
455                 return GSM_BAND_450;
456         else if (arfcn >= 306 && arfcn <= 340)
457                 return GSM_BAND_480;
458         else if (arfcn >= 350 && arfcn <= 425)
459                 return GSM_BAND_810;
460         else if (arfcn >= 438 && arfcn <= 511)
461                 return GSM_BAND_750;
462         else
463                 return GSM_BAND_1800;
464 }
465
466 /* Convert an ARFCN to the frequency in MHz * 10 */
467 uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink)
468 {
469         uint16_t freq10_ul;
470         uint16_t freq10_dl;
471         int is_pcs = arfcn & ARFCN_PCS;
472
473         arfcn &= ~ARFCN_FLAG_MASK;
474
475         if (is_pcs) {
476                 /* DCS 1900 */
477                 arfcn &= ~ARFCN_PCS;
478                 freq10_ul = 18502 + 2 * (arfcn-512);
479                 freq10_dl = freq10_ul + 800;
480         } else if (arfcn <= 124) {
481                 /* Primary GSM + ARFCN 0 of E-GSM */
482                 freq10_ul = 8900 + 2 * arfcn;
483                 freq10_dl = freq10_ul + 450;
484         } else if (arfcn >= 955 && arfcn <= 1023) {
485                 /* E-GSM and R-GSM */
486                 freq10_ul = 8900 + 2 * (arfcn - 1024);
487                 freq10_dl = freq10_ul + 450;
488         } else if (arfcn >= 128 && arfcn <= 251) {
489                 /* GSM 850 */
490                 freq10_ul = 8242 + 2 * (arfcn - 128);
491                 freq10_dl = freq10_ul + 450;
492         } else if (arfcn >= 512 && arfcn <= 885) {
493                 /* DCS 1800 */
494                 freq10_ul = 17102 + 2 * (arfcn - 512);
495                 freq10_dl = freq10_ul + 950;
496         } else if (arfcn >= 259 && arfcn <= 293) {
497                 /* GSM 450 */
498                 freq10_ul = 4506 + 2 * (arfcn - 259);
499                 freq10_dl = freq10_ul + 100;
500         } else if (arfcn >= 306 && arfcn <= 340) {
501                 /* GSM 480 */
502                 freq10_ul = 4790 + 2 * (arfcn - 306);
503                 freq10_dl = freq10_ul + 100;
504         } else if (arfcn >= 350 && arfcn <= 425) {
505                 /* GSM 810 */
506                 freq10_ul = 8060 + 2 * (arfcn - 350);
507                 freq10_dl = freq10_ul + 450;
508         } else if (arfcn >= 438 && arfcn <= 511) {
509                 /* GSM 750 */
510                 freq10_ul = 7472 + 2 * (arfcn - 438);
511                 freq10_dl = freq10_ul + 300;
512         } else
513                 return 0xffff;
514
515         if (uplink)
516                 return freq10_ul;
517         else
518                 return freq10_dl;
519 }
520
521 void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn)
522 {
523         time->fn = fn;
524         time->t1 = time->fn / (26*51);
525         time->t2 = time->fn % 26;
526         time->t3 = time->fn % 51;
527         time->tc = (time->fn / 51) % 8;
528 }
529
530 uint32_t gsm_gsmtime2fn(struct gsm_time *time)
531 {
532         /* TS 05.02 Chapter 4.3.3 TDMA frame number */
533         return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1));
534 }
535
536 /* TS 03.03 Chapter 2.6 */
537 int gprs_tlli_type(uint32_t tlli)
538 {
539         if ((tlli & 0xc0000000) == 0xc0000000)
540                 return TLLI_LOCAL;
541         else if ((tlli & 0xc0000000) == 0x80000000)
542                 return TLLI_FOREIGN;
543         else if ((tlli & 0xf8000000) == 0x78000000)
544                 return TLLI_RANDOM;
545         else if ((tlli & 0xf8000000) == 0x70000000)
546                 return TLLI_AUXILIARY;
547
548         return TLLI_RESERVED;
549 }
550
551 uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type)
552 {
553         uint32_t tlli;
554         switch (type) {
555         case TLLI_LOCAL:
556                 tlli = p_tmsi | 0xc0000000;
557                 break;
558         case TLLI_FOREIGN:
559                 tlli = (p_tmsi & 0x3fffffff) | 0x80000000;
560                 break;
561         default:
562                 tlli = 0;
563                 break;
564         }
565         return tlli;
566 }