blob: 64f9edc1d3c1792d9574428d44523ba8ee4b4e37 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/* GSM utility functions, e.g. coding and decoding */
2/*
3 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welteaebe08c2010-03-04 10:39:17 +01005 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Nico Golde28de0532010-07-09 17:19:12 +02006 * (C) 2010 by Nico Golde <nico@ngolde.de>
Harald Welteec8b4502010-02-20 20:34:29 +01007 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26#ifndef GSM_UTILS_H
27#define GSM_UTILS_H
28
29#include <stdint.h>
30
Sylvain Munaut221030f2010-04-27 21:48:40 +020031#define ADD_MODULO(sum, delta, modulo) do { \
32 if ((sum += delta) >= modulo) \
33 sum -= modulo; \
34 } while (0)
35
36#define GSM_MAX_FN (26*51*2048)
37
Harald Welte622b7182010-03-07 17:50:21 +010038struct gsm_time {
39 uint32_t fn; /* FN count */
40 uint16_t t1; /* FN div (26*51) */
41 uint8_t t2; /* FN modulo 26 */
42 uint8_t t3; /* FN modulo 51 */
43 uint8_t tc;
44};
45
Harald Welteec8b4502010-02-20 20:34:29 +010046enum gsm_band {
47 GSM_BAND_850 = 1,
48 GSM_BAND_900 = 2,
49 GSM_BAND_1800 = 4,
50 GSM_BAND_1900 = 8,
51 GSM_BAND_450 = 0x10,
52 GSM_BAND_480 = 0x20,
53 GSM_BAND_750 = 0x40,
54 GSM_BAND_810 = 0x80,
55};
56
Nico Golde28de0532010-07-09 17:19:12 +020057/* ETSI GSM 03.38 6.2.1 and 6.2.1.1 default alphabet
58 * Greek symbols at hex positions 0x10 and 0x12-0x1a
59 * left out as they can't be handled with a char and
60 * since most phones don't display or write these
61 * characters this would only needlessly make the code
62 * more complex
63*/
64unsigned char gsm_7bit_alphabet[] = {
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0xff, 0xff, 0x0d, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
68 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
69 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
70 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
71 0x5a, 0x3c, 0x2f, 0x3e, 0x14, 0x11, 0xff, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
72 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
73 0x78, 0x79, 0x7a, 0x28, 0x40, 0x29, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff,
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x40, 0xff, 0x01, 0xff,
76 0x03, 0xff, 0x7b, 0x7d, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5b, 0x7e, 0x5d, 0xff, 0x7c, 0xff, 0xff, 0xff,
78 0xff, 0x5b, 0x0e, 0x1c, 0x09, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5d,
79 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0x0b, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff, 0x1e, 0x7f,
80 0xff, 0xff, 0xff, 0x7b, 0x0f, 0x1d, 0xff, 0x04, 0x05, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
81 0xff, 0x7d, 0x08, 0xff, 0xff, 0xff, 0x7c, 0xff, 0x0c, 0x06, 0xff, 0xff, 0x7e, 0xff, 0xff
82};
83
Harald Weltecbc80622010-03-22 08:28:44 +080084const char *gsm_band_name(enum gsm_band band);
Harald Welteaebe08c2010-03-04 10:39:17 +010085enum gsm_band gsm_band_parse(const char *mhz);
86
Harald Welteec8b4502010-02-20 20:34:29 +010087int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length);
88int gsm_7bit_encode(uint8_t *result, const char *data);
89
90int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
91int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
92
93/* According to TS 08.05 Chapter 8.1.4 */
94int rxlev2dbm(uint8_t rxlev);
95uint8_t dbm2rxlev(int dbm);
96
Sylvain Munaut4f585492010-06-09 13:38:56 +020097/* According to GSM 04.08 Chapter 10.5.1.6 */
98static inline int ms_cm2_a5n_support(uint8_t *cm2, int n) {
99 switch (n) {
100 case 0: return 1;
101 case 1: return (cm2[0] & (1<<3)) ? 0 : 1;
102 case 2: return (cm2[2] & (1<<0)) ? 1 : 0;
103 case 3: return (cm2[2] & (1<<1)) ? 1 : 0;
104 default:
105 return 0;
106 }
107}
108
Harald Welteec8b4502010-02-20 20:34:29 +0100109/* According to GSM 04.08 Chapter 10.5.2.29 */
110static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
111static inline int rach_max_trans_raw2val(int raw) {
112 const int tbl[4] = { 1, 2, 4, 7 };
113 return tbl[raw & 3];
114}
115
Harald Welte622b7182010-03-07 17:50:21 +0100116#define ARFCN_PCS 0x8000
117#define ARFCN_UPLINK 0x4000
118
119enum gsm_band gsm_arfcn2band(uint16_t arfcn);
120
121/* Convert an ARFCN to the frequency in MHz * 10 */
122uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
123
124/* Convert from frame number to GSM time */
125void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
126
127/* Convert from GSM time to frame number */
128uint32_t gsm_gsmtime2fn(struct gsm_time *time);
129
Harald Weltea1c4f762010-05-01 11:59:42 +0200130/* GSM TS 03.03 Chapter 2.6 */
Harald Weltec2263172010-06-01 10:47:07 +0200131enum gprs_tlli_type {
Harald Weltea1c4f762010-05-01 11:59:42 +0200132 TLLI_LOCAL,
133 TLLI_FOREIGN,
134 TLLI_RANDOM,
135 TLLI_AUXILIARY,
136 TLLI_RESERVED,
137};
138
139/* TS 03.03 Chapter 2.6 */
140int gprs_tlli_type(uint32_t tlli);
141
Harald Weltec2263172010-06-01 10:47:07 +0200142uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type);
143
Harald Welteec8b4502010-02-20 20:34:29 +0100144void generate_backtrace();
145#endif