msgfile: Add a file parser for a simple file format
[osmocom-bb.git] / src / gsm_utils.c
index 97497c8..4b4e2c6 100644 (file)
@@ -2,6 +2,7 @@
  * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
  * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
  * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2010 by Nico Golde <nico@ngolde.de>
  *
  * All Rights Reserved
  *
 
 #include "../config.h"
 
-/* GSM 03.38 6.2.1 Charachter packing */
+/* ETSI GSM 03.38 6.2.1 and 6.2.1.1 default alphabet
+ * Greek symbols at hex positions 0x10 and 0x12-0x1a
+ * left out as they can't be handled with a char and
+ * since most phones don't display or write these
+ * characters this would only needlessly make the code
+ * more complex
+*/
+static unsigned char gsm_7bit_alphabet[] = {
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0xff, 0xff, 0x0d, 0xff,
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+       0xff, 0xff, 0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
+       0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
+       0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
+       0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
+       0x5a, 0x3c, 0x2f, 0x3e, 0x14, 0x11, 0xff, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
+       0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
+       0x78, 0x79, 0x7a, 0x28, 0x40, 0x29, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+       0xff, 0xff, 0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff,
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x40, 0xff, 0x01, 0xff,
+       0x03, 0xff, 0x7b, 0x7d, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0xff, 0xff, 0xff, 0xff,
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5b, 0x7e, 0x5d, 0xff, 0x7c, 0xff, 0xff, 0xff,
+       0xff, 0x5b, 0x0e, 0x1c, 0x09, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5d,
+       0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0x0b, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff, 0x1e, 0x7f,
+       0xff, 0xff, 0xff, 0x7b, 0x0f, 0x1d, 0xff, 0x04, 0x05, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
+       0xff, 0x7d, 0x08, 0xff, 0xff, 0xff, 0x7c, 0xff, 0x0c, 0x06, 0xff, 0xff, 0x7e, 0xff, 0xff
+};
+
+/* GSM 03.38 6.2.1 Character lookup for decoding */
+static int gsm_septet_lookup(uint8_t ch)
+{
+       int i = 0;
+       for(; i < sizeof(gsm_7bit_alphabet); i++){
+               if(gsm_7bit_alphabet[i] == ch)
+                       return i;
+       }
+       return -1;
+}
+
+/* GSM 03.38 6.2.1 Character unpacking */
 int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length)
 {
        int i = 0;
        int l = 0;
+       int septet_l = (length * 8) / 7;
+       uint8_t *rtext = calloc(septet_l, sizeof(uint8_t));
+       uint8_t tmp;
 
-        /* FIXME: We need to account for user data headers here */
+       /* FIXME: We need to account for user data headers here */
        i += l;
-       for (; i < length; i ++)
-               *(text ++) =
+       for (; i < septet_l; i++){
+               rtext[i] =
                        ((user_data[(i * 7 + 7) >> 3] <<
                          (7 - ((i * 7 + 7) & 7))) |
                         (user_data[(i * 7) >> 3] >>
                          ((i * 7) & 7))) & 0x7f;
+       }
+
+       for(i = 0; i < septet_l; i++){
+               /* this is an extension character */
+               if(rtext[i] == 0x1b && i + 1 < length){
+                       tmp = rtext[i+1];
+                       *(text++) = gsm_7bit_alphabet[0x7f + tmp];
+                       i++;
+                       continue;
+               }
+
+               *(text++) = gsm_septet_lookup(rtext[i]);
+       }
+
        *text = '\0';
+       free(rtext);
 
-       return i - l;
+       return i;
 }
 
+/* GSM 03.38 6.2.1 Prepare character packing */
+static int gsm_septet_encode(uint8_t *result, const char *data)
+{
+       int i, y = 0;
+       uint8_t ch;
+       for(i = 0; i < strlen(data); i++){
+               ch = data[i];
+               switch(ch){
+               /* fall-through for extension characters */
+               case 0x0c:
+               case 0x5e:
+               case 0x7b:
+               case 0x7d:
+               case 0x5c:
+               case 0x5b:
+               case 0x7e:
+               case 0x5d:
+               case 0x7c:
+                       result[y++] = 0x1b;
+               default:
+                       result[y] = gsm_7bit_alphabet[ch];
+                       break;
+               }
+               y++;
+       }
+
+       return y;
+}
 
-/* GSM 03.38 6.2.1 Charachter packing */
+/* GSM 03.38 6.2.1 Character packing */
 int gsm_7bit_encode(uint8_t *result, const char *data)
 {
-       int i,j = 0;
-       unsigned char ch1, ch2;
+       int i,y,z = 0;
+       /* prepare for the worst case, every character expanding to two bytes */
+       uint8_t *rdata = calloc(strlen(data) * 2, sizeof(uint8_t));
+       uint8_t cb, nb;
        int shift = 0;
 
-       for ( i=0; i<strlen(data); i++ ) {
+       y = gsm_septet_encode(rdata, data);
 
-               ch1 = data[i] & 0x7F;
-               ch1 = ch1 >> shift;
-               ch2 = data[(i+1)] & 0x7F;
-               ch2 = ch2 << (7-shift);
+       for(i = 0; i < y; i++) {
+               if(shift == 7 && i + 1 < y){
+                       shift = 0;
+                       continue;
+               }
 
-               ch1 = ch1 | ch2;
+               cb = (rdata[i] & 0x7f) >> shift;
+               if(i + 1 < y){
+                       nb = (rdata[i + 1] & 0x7f) << (7 - shift);
+                       cb = cb | nb;
+               }
 
-               result[j++] = ch1;
+               result[z++] = cb;
 
                shift++;
-
-               if ((shift == 7) && (i+1<strlen(data))) {
-                       shift = 0;
-                       i++;
-               }
        }
 
-       return i;
+       free(rdata);
+       return z;
 }
 
 /* determine power control level for given dBm value, as indicated
@@ -193,13 +281,13 @@ uint8_t dbm2rxlev(int dbm)
        return rxlev;
 }
 
-char *gsm_band_name(enum gsm_band band)
+const char *gsm_band_name(enum gsm_band band)
 {
        switch (band) {
        case GSM_BAND_450:
                return "GSM450";
        case GSM_BAND_480:
-               return "GSM450";
+               return "GSM480";
        case GSM_BAND_750:
                return "GSM750";
        case GSM_BAND_810:
@@ -359,3 +447,35 @@ uint32_t gsm_gsmtime2fn(struct gsm_time *time)
        /* TS 05.02 Chapter 4.3.3 TDMA frame number */
        return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1));
 }
+
+/* TS 03.03 Chapter 2.6 */
+int gprs_tlli_type(uint32_t tlli)
+{
+       if ((tlli & 0xc0000000) == 0xc0000000)
+               return TLLI_LOCAL;
+       else if ((tlli & 0xc0000000) == 0x80000000)
+               return TLLI_FOREIGN;
+       else if ((tlli & 0xf8000000) == 0x78000000)
+               return TLLI_RANDOM;
+       else if ((tlli & 0xf8000000) == 0x70000000)
+               return TLLI_AUXILIARY;
+
+       return TLLI_RESERVED;
+}
+
+uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type)
+{
+       uint32_t tlli;
+       switch (type) {
+       case TLLI_LOCAL:
+               tlli = p_tmsi | 0xc0000000;
+               break;
+       case TLLI_FOREIGN:
+               tlli = (p_tmsi & 0x3fffffff) | 0x80000000;
+               break;
+       default:
+               tlli = 0;
+               break;
+       }
+       return tlli;
+}