Merge commit '01fd5cb3f0da802ca82d3b6aef46d2a3d8e6c15c'
[osmocom-bb.git] / src / shared / libosmocore / include / osmocore / gsm_utils.h
1 /* 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>
5  * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
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 #ifndef GSM_UTILS_H
26 #define GSM_UTILS_H
27
28 #include <stdint.h>
29
30 struct gsm_time {
31         uint32_t        fn;     /* FN count */
32         uint16_t        t1;     /* FN div (26*51) */
33         uint8_t         t2;     /* FN modulo 26 */
34         uint8_t         t3;     /* FN modulo 51 */
35         uint8_t         tc;
36 };
37
38 enum gsm_band {
39         GSM_BAND_850    = 1,
40         GSM_BAND_900    = 2,
41         GSM_BAND_1800   = 4,
42         GSM_BAND_1900   = 8,
43         GSM_BAND_450    = 0x10,
44         GSM_BAND_480    = 0x20,
45         GSM_BAND_750    = 0x40,
46         GSM_BAND_810    = 0x80,
47 };
48
49 const char *gsm_band_name(enum gsm_band band);
50 enum gsm_band gsm_band_parse(const char *mhz);
51
52 int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length);
53 int gsm_7bit_encode(uint8_t *result, const char *data);
54
55 int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
56 int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
57
58 /* According to TS 08.05 Chapter 8.1.4 */
59 int rxlev2dbm(uint8_t rxlev);
60 uint8_t dbm2rxlev(int dbm);
61
62 /* According to GSM 04.08 Chapter 10.5.2.29 */
63 static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
64 static inline int rach_max_trans_raw2val(int raw) {
65         const int tbl[4] = { 1, 2, 4, 7 };
66         return tbl[raw & 3];
67 }
68
69 #define ARFCN_PCS       0x8000
70 #define ARFCN_UPLINK    0x4000
71
72 enum gsm_band gsm_arfcn2band(uint16_t arfcn);
73
74 /* Convert an ARFCN to the frequency in MHz * 10 */
75 uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
76
77 /* Convert from frame number to GSM time */
78 void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
79
80 /* Convert from GSM time to frame number */
81 uint32_t gsm_gsmtime2fn(struct gsm_time *time);
82
83 void generate_backtrace();
84 #endif