Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de> |
| 3 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 622b718 | 2010-03-07 17:50:21 +0100 | [diff] [blame] | 4 | * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 5 | * (C) 2010 by Nico Golde <nico@ngolde.de> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 <osmocore/utils.h> |
| 27 | #include <osmocore/gsm_utils.h> |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <stdint.h> |
| 31 | #include <string.h> |
| 32 | #include <stdio.h> |
| 33 | #include <errno.h> |
Harald Welte | aebe08c | 2010-03-04 10:39:17 +0100 | [diff] [blame] | 34 | #include <ctype.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | |
| 36 | #include "../config.h" |
| 37 | |
Holger Hans Peter Freyther | dd02a47 | 2010-07-23 16:35:00 +0800 | [diff] [blame] | 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 | |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 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 | /* GSM 03.38 6.2.1 Character unpacking */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 77 | int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length) |
| 78 | { |
| 79 | int i = 0; |
| 80 | int l = 0; |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 81 | int septet_l = (length * 8) / 7; |
Holger Hans Peter Freyther | f8a342c | 2010-07-21 03:14:01 +0800 | [diff] [blame] | 82 | uint8_t *rtext = calloc(septet_l, sizeof(uint8_t)); |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 83 | uint8_t tmp; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 84 | |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 85 | /* FIXME: We need to account for user data headers here */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 86 | i += l; |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 87 | for (; i < septet_l; i++){ |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 88 | rtext[i] = |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 89 | ((user_data[(i * 7 + 7) >> 3] << |
| 90 | (7 - ((i * 7 + 7) & 7))) | |
| 91 | (user_data[(i * 7) >> 3] >> |
| 92 | ((i * 7) & 7))) & 0x7f; |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 93 | } |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 94 | |
| 95 | for(i = 0; i < septet_l; i++){ |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 96 | /* this is an extension character */ |
Holger Hans Peter Freyther | 446bf37 | 2010-07-20 02:54:54 +0800 | [diff] [blame] | 97 | if(rtext[i] == 0x1b && i + 1 < length){ |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 98 | tmp = rtext[i+1]; |
| 99 | *(text++) = gsm_7bit_alphabet[0x7f + tmp]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 100 | i++; |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 101 | continue; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 102 | } |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 103 | |
| 104 | *(text++) = gsm_septet_lookup(rtext[i]); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 107 | *text = '\0'; |
| 108 | free(rtext); |
| 109 | |
| 110 | return i; |
| 111 | } |
| 112 | |
| 113 | /* GSM 03.38 6.2.1 Prepare character packing */ |
| 114 | static 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 */ |
| 143 | int 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 Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 171 | return z; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* determine power control level for given dBm value, as indicated |
| 175 | * by the tables in chapter 4.1.1 of GSM TS 05.05 */ |
| 176 | int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm) |
| 177 | { |
| 178 | switch (band) { |
| 179 | case GSM_BAND_450: |
| 180 | case GSM_BAND_480: |
| 181 | case GSM_BAND_750: |
| 182 | case GSM_BAND_900: |
| 183 | case GSM_BAND_810: |
| 184 | case GSM_BAND_850: |
| 185 | if (dbm >= 39) |
| 186 | return 0; |
| 187 | else if (dbm < 5) |
| 188 | return 19; |
| 189 | else { |
| 190 | /* we are guaranteed to have (5 <= dbm < 39) */ |
| 191 | return 2 + ((39 - dbm) / 2); |
| 192 | } |
| 193 | break; |
| 194 | case GSM_BAND_1800: |
| 195 | if (dbm >= 36) |
| 196 | return 29; |
| 197 | else if (dbm >= 34) |
| 198 | return 30; |
| 199 | else if (dbm >= 32) |
| 200 | return 31; |
| 201 | else if (dbm == 31) |
| 202 | return 0; |
| 203 | else { |
| 204 | /* we are guaranteed to have (0 <= dbm < 31) */ |
| 205 | return (30 - dbm) / 2; |
| 206 | } |
| 207 | break; |
| 208 | case GSM_BAND_1900: |
| 209 | if (dbm >= 33) |
| 210 | return 30; |
| 211 | else if (dbm >= 32) |
| 212 | return 31; |
| 213 | else if (dbm == 31) |
| 214 | return 0; |
| 215 | else { |
| 216 | /* we are guaranteed to have (0 <= dbm < 31) */ |
| 217 | return (30 - dbm) / 2; |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | |
| 224 | int ms_pwr_dbm(enum gsm_band band, uint8_t lvl) |
| 225 | { |
| 226 | lvl &= 0x1f; |
| 227 | |
| 228 | switch (band) { |
| 229 | case GSM_BAND_450: |
| 230 | case GSM_BAND_480: |
| 231 | case GSM_BAND_750: |
| 232 | case GSM_BAND_900: |
| 233 | case GSM_BAND_810: |
| 234 | case GSM_BAND_850: |
| 235 | if (lvl < 2) |
| 236 | return 39; |
| 237 | else if (lvl < 20) |
| 238 | return 39 - ((lvl - 2) * 2) ; |
| 239 | else |
| 240 | return 5; |
| 241 | break; |
| 242 | case GSM_BAND_1800: |
| 243 | if (lvl < 16) |
| 244 | return 30 - (lvl * 2); |
| 245 | else if (lvl < 29) |
| 246 | return 0; |
| 247 | else |
| 248 | return 36 - ((lvl - 29) * 2); |
| 249 | break; |
| 250 | case GSM_BAND_1900: |
| 251 | if (lvl < 16) |
| 252 | return 30 - (lvl * 2); |
| 253 | else if (lvl < 30) |
| 254 | return -EINVAL; |
| 255 | else |
| 256 | return 33 - (lvl - 30); |
| 257 | break; |
| 258 | } |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | /* According to TS 08.05 Chapter 8.1.4 */ |
| 263 | int rxlev2dbm(uint8_t rxlev) |
| 264 | { |
| 265 | if (rxlev > 63) |
| 266 | rxlev = 63; |
| 267 | |
| 268 | return -110 + rxlev; |
| 269 | } |
| 270 | |
| 271 | /* According to TS 08.05 Chapter 8.1.4 */ |
| 272 | uint8_t dbm2rxlev(int dbm) |
| 273 | { |
| 274 | int rxlev = dbm + 110; |
| 275 | |
| 276 | if (rxlev > 63) |
| 277 | rxlev = 63; |
| 278 | else if (rxlev < 0) |
| 279 | rxlev = 0; |
| 280 | |
| 281 | return rxlev; |
| 282 | } |
| 283 | |
Harald Welte | cbc8062 | 2010-03-22 08:28:44 +0800 | [diff] [blame] | 284 | const char *gsm_band_name(enum gsm_band band) |
Harald Welte | aebe08c | 2010-03-04 10:39:17 +0100 | [diff] [blame] | 285 | { |
| 286 | switch (band) { |
| 287 | case GSM_BAND_450: |
| 288 | return "GSM450"; |
| 289 | case GSM_BAND_480: |
Sylvain Munaut | e10ae5b | 2010-07-04 11:41:36 +0200 | [diff] [blame] | 290 | return "GSM480"; |
Harald Welte | aebe08c | 2010-03-04 10:39:17 +0100 | [diff] [blame] | 291 | case GSM_BAND_750: |
| 292 | return "GSM750"; |
| 293 | case GSM_BAND_810: |
| 294 | return "GSM810"; |
| 295 | case GSM_BAND_850: |
| 296 | return "GSM850"; |
| 297 | case GSM_BAND_900: |
| 298 | return "GSM900"; |
| 299 | case GSM_BAND_1800: |
| 300 | return "DCS1800"; |
| 301 | case GSM_BAND_1900: |
| 302 | return "PCS1900"; |
| 303 | } |
| 304 | return "invalid"; |
| 305 | } |
| 306 | |
| 307 | enum gsm_band gsm_band_parse(const char* mhz) |
| 308 | { |
| 309 | while (*mhz && !isdigit(*mhz)) |
| 310 | mhz++; |
| 311 | |
| 312 | if (*mhz == '\0') |
| 313 | return -EINVAL; |
| 314 | |
Harald Welte | d3ff15f | 2010-03-07 18:23:47 +0100 | [diff] [blame] | 315 | switch (strtol(mhz, NULL, 10)) { |
Harald Welte | aebe08c | 2010-03-04 10:39:17 +0100 | [diff] [blame] | 316 | case 450: |
| 317 | return GSM_BAND_450; |
| 318 | case 480: |
| 319 | return GSM_BAND_480; |
| 320 | case 750: |
| 321 | return GSM_BAND_750; |
| 322 | case 810: |
| 323 | return GSM_BAND_810; |
| 324 | case 850: |
| 325 | return GSM_BAND_850; |
| 326 | case 900: |
| 327 | return GSM_BAND_900; |
| 328 | case 1800: |
| 329 | return GSM_BAND_1800; |
| 330 | case 1900: |
| 331 | return GSM_BAND_1900; |
| 332 | default: |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 338 | #ifdef HAVE_EXECINFO_H |
| 339 | #include <execinfo.h> |
| 340 | void generate_backtrace() |
| 341 | { |
| 342 | int i, nptrs; |
| 343 | void *buffer[100]; |
| 344 | char **strings; |
| 345 | |
| 346 | nptrs = backtrace(buffer, ARRAY_SIZE(buffer)); |
| 347 | printf("backtrace() returned %d addresses\n", nptrs); |
| 348 | |
| 349 | strings = backtrace_symbols(buffer, nptrs); |
| 350 | if (!strings) |
| 351 | return; |
| 352 | |
| 353 | for (i = 1; i < nptrs; i++) |
| 354 | printf("%s\n", strings[i]); |
| 355 | |
| 356 | free(strings); |
| 357 | } |
| 358 | #endif |
Harald Welte | 622b718 | 2010-03-07 17:50:21 +0100 | [diff] [blame] | 359 | |
| 360 | enum gsm_band gsm_arfcn2band(uint16_t arfcn) |
| 361 | { |
| 362 | if (arfcn & ARFCN_PCS) |
| 363 | return GSM_BAND_1900; |
| 364 | else if (arfcn <= 124) |
| 365 | return GSM_BAND_900; |
| 366 | else if (arfcn >= 955 && arfcn <= 1023) |
| 367 | return GSM_BAND_900; |
| 368 | else if (arfcn >= 128 && arfcn <= 251) |
| 369 | return GSM_BAND_850; |
| 370 | else if (arfcn >= 512 && arfcn <= 885) |
| 371 | return GSM_BAND_1800; |
| 372 | else if (arfcn >= 259 && arfcn <= 293) |
| 373 | return GSM_BAND_450; |
| 374 | else if (arfcn >= 306 && arfcn <= 340) |
| 375 | return GSM_BAND_480; |
| 376 | else if (arfcn >= 350 && arfcn <= 425) |
| 377 | return GSM_BAND_810; |
| 378 | else if (arfcn >= 438 && arfcn <= 511) |
| 379 | return GSM_BAND_750; |
| 380 | else |
| 381 | return GSM_BAND_1800; |
| 382 | } |
| 383 | |
| 384 | /* Convert an ARFCN to the frequency in MHz * 10 */ |
| 385 | uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink) |
| 386 | { |
| 387 | uint16_t freq10_ul; |
| 388 | uint16_t freq10_dl; |
| 389 | |
| 390 | if (arfcn & ARFCN_PCS) { |
| 391 | /* DCS 1900 */ |
| 392 | arfcn &= ~ARFCN_PCS; |
| 393 | freq10_ul = 18502 + 2 * (arfcn-512); |
| 394 | freq10_dl = freq10_ul + 800; |
| 395 | } else if (arfcn <= 124) { |
| 396 | /* Primary GSM + ARFCN 0 of E-GSM */ |
| 397 | freq10_ul = 8900 + 2 * arfcn; |
| 398 | freq10_dl = freq10_ul + 450; |
| 399 | } else if (arfcn >= 955 && arfcn <= 1023) { |
| 400 | /* E-GSM and R-GSM */ |
| 401 | freq10_ul = 8900 + 2 * (arfcn - 1024); |
| 402 | freq10_dl = freq10_ul + 450; |
| 403 | } else if (arfcn >= 128 && arfcn <= 251) { |
| 404 | /* GSM 850 */ |
| 405 | freq10_ul = 8242 + 2 * (arfcn - 128); |
| 406 | freq10_dl = freq10_ul + 450; |
| 407 | } else if (arfcn >= 512 && arfcn <= 885) { |
| 408 | /* DCS 1800 */ |
| 409 | freq10_ul = 17102 + 2 * (arfcn - 512); |
| 410 | freq10_dl = freq10_ul + 950; |
| 411 | } else if (arfcn >= 259 && arfcn <= 293) { |
| 412 | /* GSM 450 */ |
| 413 | freq10_ul = 4506 + 2 * (arfcn - 259); |
| 414 | freq10_dl = freq10_ul + 100; |
| 415 | } else if (arfcn >= 306 && arfcn <= 340) { |
| 416 | /* GSM 480 */ |
| 417 | freq10_ul = 4790 + 2 * (arfcn - 306); |
| 418 | freq10_dl = freq10_ul + 100; |
| 419 | } else if (arfcn >= 350 && arfcn <= 425) { |
| 420 | /* GSM 810 */ |
| 421 | freq10_ul = 8060 + 2 * (arfcn - 350); |
| 422 | freq10_dl = freq10_ul + 450; |
| 423 | } else if (arfcn >= 438 && arfcn <= 511) { |
| 424 | /* GSM 750 */ |
| 425 | freq10_ul = 7472 + 2 * (arfcn - 438); |
| 426 | freq10_dl = freq10_ul + 300; |
| 427 | } else |
| 428 | return 0xffff; |
| 429 | |
| 430 | if (uplink) |
| 431 | return freq10_ul; |
| 432 | else |
| 433 | return freq10_dl; |
| 434 | } |
| 435 | |
| 436 | void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn) |
| 437 | { |
| 438 | time->fn = fn; |
| 439 | time->t1 = time->fn / (26*51); |
| 440 | time->t2 = time->fn % 26; |
| 441 | time->t3 = time->fn % 51; |
| 442 | time->tc = (time->fn / 51) % 8; |
| 443 | } |
| 444 | |
| 445 | uint32_t gsm_gsmtime2fn(struct gsm_time *time) |
| 446 | { |
| 447 | /* TS 05.02 Chapter 4.3.3 TDMA frame number */ |
| 448 | return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1)); |
| 449 | } |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 450 | |
| 451 | /* TS 03.03 Chapter 2.6 */ |
| 452 | int gprs_tlli_type(uint32_t tlli) |
| 453 | { |
| 454 | if ((tlli & 0xc0000000) == 0xc0000000) |
| 455 | return TLLI_LOCAL; |
| 456 | else if ((tlli & 0xc0000000) == 0x80000000) |
| 457 | return TLLI_FOREIGN; |
| 458 | else if ((tlli & 0xf8000000) == 0x78000000) |
| 459 | return TLLI_RANDOM; |
| 460 | else if ((tlli & 0xf8000000) == 0x70000000) |
| 461 | return TLLI_AUXILIARY; |
| 462 | |
| 463 | return TLLI_RESERVED; |
| 464 | } |
Harald Welte | c226317 | 2010-06-01 10:47:07 +0200 | [diff] [blame] | 465 | |
| 466 | uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type) |
| 467 | { |
| 468 | uint32_t tlli; |
| 469 | switch (type) { |
| 470 | case TLLI_LOCAL: |
| 471 | tlli = p_tmsi | 0xc0000000; |
| 472 | break; |
| 473 | case TLLI_FOREIGN: |
| 474 | tlli = (p_tmsi & 0x3fffffff) | 0x80000000; |
| 475 | break; |
| 476 | default: |
| 477 | tlli = 0; |
| 478 | break; |
| 479 | } |
| 480 | return tlli; |
| 481 | } |