blob: 6cd46e457af623eef9788fcad26e6ea13a271e06 [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>
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#ifndef GSM_UTILS_H
26#define GSM_UTILS_H
27
28#include <stdint.h>
29
Sylvain Munaut221030f2010-04-27 21:48:40 +020030#define ADD_MODULO(sum, delta, modulo) do { \
31 if ((sum += delta) >= modulo) \
32 sum -= modulo; \
33 } while (0)
34
35#define GSM_MAX_FN (26*51*2048)
36
Harald Welte622b7182010-03-07 17:50:21 +010037struct gsm_time {
38 uint32_t fn; /* FN count */
39 uint16_t t1; /* FN div (26*51) */
40 uint8_t t2; /* FN modulo 26 */
41 uint8_t t3; /* FN modulo 51 */
42 uint8_t tc;
43};
44
Harald Welteec8b4502010-02-20 20:34:29 +010045enum gsm_band {
46 GSM_BAND_850 = 1,
47 GSM_BAND_900 = 2,
48 GSM_BAND_1800 = 4,
49 GSM_BAND_1900 = 8,
50 GSM_BAND_450 = 0x10,
51 GSM_BAND_480 = 0x20,
52 GSM_BAND_750 = 0x40,
53 GSM_BAND_810 = 0x80,
54};
55
Harald Weltecbc80622010-03-22 08:28:44 +080056const char *gsm_band_name(enum gsm_band band);
Harald Welteaebe08c2010-03-04 10:39:17 +010057enum gsm_band gsm_band_parse(const char *mhz);
58
Harald Welteec8b4502010-02-20 20:34:29 +010059int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length);
Andreas Eversberg95975552013-08-08 12:38:53 +020060int gsm_7bit_decode_ussd(char *decoded, const uint8_t *user_data, uint8_t length);
Dennis Wehrle291e6132011-07-24 20:14:13 +020061int gsm_7bit_decode_hdr(char *decoded, const uint8_t *user_data, uint8_t length, uint8_t ud_hdr_ind);
Harald Welteec8b4502010-02-20 20:34:29 +010062int gsm_7bit_encode(uint8_t *result, const char *data);
Andreas Eversberg95975552013-08-08 12:38:53 +020063int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets_written);
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +020064int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets_written);
Harald Welteec8b4502010-02-20 20:34:29 +010065
Holger Hans Peter Freyther94b2f642013-08-07 11:09:43 +020066/* the three functions below are helper functions and here for the unit test */
Harald Welteca693882013-03-13 15:10:55 +010067int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding);
Dennis Wehrle291e6132011-07-24 20:14:13 +020068int gsm_septet_encode(uint8_t *result, const char *data);
69uint8_t gsm_get_octet_len(const uint8_t sept_len);
70
Harald Welteb7b61232011-07-23 10:49:51 +020071unsigned int ms_class_gmsk_dbm(enum gsm_band band, int ms_class);
Andreas Eversberg2a68c7c2011-06-26 11:41:48 +020072
Harald Welteec8b4502010-02-20 20:34:29 +010073int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
74int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
75
Bhaskar6b30f922013-05-16 17:35:49 +053076/* According to TS 05.08 Chapter 8.1.4 */
Harald Welteec8b4502010-02-20 20:34:29 +010077int rxlev2dbm(uint8_t rxlev);
78uint8_t dbm2rxlev(int dbm);
79
Sylvain Munaut4f585492010-06-09 13:38:56 +020080/* According to GSM 04.08 Chapter 10.5.1.6 */
81static inline int ms_cm2_a5n_support(uint8_t *cm2, int n) {
82 switch (n) {
83 case 0: return 1;
84 case 1: return (cm2[0] & (1<<3)) ? 0 : 1;
85 case 2: return (cm2[2] & (1<<0)) ? 1 : 0;
86 case 3: return (cm2[2] & (1<<1)) ? 1 : 0;
87 default:
88 return 0;
89 }
90}
91
Harald Welteec8b4502010-02-20 20:34:29 +010092/* According to GSM 04.08 Chapter 10.5.2.29 */
93static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
94static inline int rach_max_trans_raw2val(int raw) {
95 const int tbl[4] = { 1, 2, 4, 7 };
96 return tbl[raw & 3];
97}
98
Harald Welte622b7182010-03-07 17:50:21 +010099#define ARFCN_PCS 0x8000
100#define ARFCN_UPLINK 0x4000
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100101#define ARFCN_FLAG_MASK 0xf000 /* Reserve the upper 5 bits for flags */
Harald Welte622b7182010-03-07 17:50:21 +0100102
103enum gsm_band gsm_arfcn2band(uint16_t arfcn);
104
105/* Convert an ARFCN to the frequency in MHz * 10 */
106uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
107
Sylvain Munaut55720312012-12-11 23:44:41 +0100108/* Convert a Frequency in MHz * 10 to ARFCN */
109uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink);
110
Harald Welte622b7182010-03-07 17:50:21 +0100111/* Convert from frame number to GSM time */
112void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
113
114/* Convert from GSM time to frame number */
115uint32_t gsm_gsmtime2fn(struct gsm_time *time);
116
Harald Weltea1c4f762010-05-01 11:59:42 +0200117/* GSM TS 03.03 Chapter 2.6 */
Harald Weltec2263172010-06-01 10:47:07 +0200118enum gprs_tlli_type {
Harald Weltea1c4f762010-05-01 11:59:42 +0200119 TLLI_LOCAL,
120 TLLI_FOREIGN,
121 TLLI_RANDOM,
122 TLLI_AUXILIARY,
123 TLLI_RESERVED,
124};
125
126/* TS 03.03 Chapter 2.6 */
127int gprs_tlli_type(uint32_t tlli);
128
Harald Weltec2263172010-06-01 10:47:07 +0200129uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type);
130
Harald Welte11c71932011-05-24 17:15:12 +0200131/* Osmocom internal, not part of any gsm spec */
132enum gsm_phys_chan_config {
133 GSM_PCHAN_NONE,
134 GSM_PCHAN_CCCH,
135 GSM_PCHAN_CCCH_SDCCH4,
136 GSM_PCHAN_TCH_F,
137 GSM_PCHAN_TCH_H,
138 GSM_PCHAN_SDCCH8_SACCH8C,
139 GSM_PCHAN_PDCH, /* GPRS PDCH */
140 GSM_PCHAN_TCH_F_PDCH, /* TCH/F if used, PDCH otherwise */
141 GSM_PCHAN_UNKNOWN,
142 _GSM_PCHAN_MAX
143};
144
145/* Osmocom internal, not part of any gsm spec */
146enum gsm_chan_t {
147 GSM_LCHAN_NONE,
148 GSM_LCHAN_SDCCH,
149 GSM_LCHAN_TCH_F,
150 GSM_LCHAN_TCH_H,
151 GSM_LCHAN_UNKNOWN,
152 GSM_LCHAN_CCCH,
Harald Welte81cd54a2012-04-19 23:18:43 +0200153 GSM_LCHAN_PDTCH,
Harald Welte11c71932011-05-24 17:15:12 +0200154 _GSM_LCHAN_MAX
155};
156
157
Harald Welteec8b4502010-02-20 20:34:29 +0100158#endif