libosmogsm  0.6.3
Osmocom GSM library
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 <stddef.h>
29 #include <stdint.h>
30 
31 #include <osmocom/core/defs.h>
32 
33 #define ADD_MODULO(sum, delta, modulo) do { \
34  if ((sum += delta) >= modulo) \
35  sum -= modulo; \
36  } while (0)
37 
38 #define GSM_MAX_FN (26*51*2048)
39 
40 struct gsm_time {
41  uint32_t fn; /* FN count */
42  uint16_t t1; /* FN div (26*51) */
43  uint8_t t2; /* FN modulo 26 */
44  uint8_t t3; /* FN modulo 51 */
45  uint8_t tc;
46 };
47 
48 enum gsm_band {
49  GSM_BAND_850 = 1,
50  GSM_BAND_900 = 2,
51  GSM_BAND_1800 = 4,
52  GSM_BAND_1900 = 8,
53  GSM_BAND_450 = 0x10,
54  GSM_BAND_480 = 0x20,
55  GSM_BAND_750 = 0x40,
56  GSM_BAND_810 = 0x80,
57 };
58 
59 const char *gsm_band_name(enum gsm_band band);
60 enum gsm_band gsm_band_parse(const char *mhz);
61 
75 int gsm_7bit_decode_n(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
76 
82 int gsm_7bit_decode_n_ussd(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
83 
96 int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets_written);
97 
103 int gsm_7bit_encode_n_ussd(uint8_t *result, size_t n, const char *data, int *octets_written);
104 
105 /* the four functions below are helper functions and here for the unit test */
106 int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding);
107 int gsm_septet_encode(uint8_t *result, const char *data);
108 uint8_t gsm_get_octet_len(const uint8_t sept_len);
109 int gsm_7bit_decode_n_hdr(char *decoded, size_t n, const uint8_t *user_data, uint8_t length, uint8_t ud_hdr_ind);
110 
111 unsigned int ms_class_gmsk_dbm(enum gsm_band band, int ms_class);
112 
113 int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
114 int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
115 
116 /* According to TS 05.08 Chapter 8.1.4 */
117 int rxlev2dbm(uint8_t rxlev);
118 uint8_t dbm2rxlev(int dbm);
119 
120 /* According to GSM 04.08 Chapter 10.5.1.6 */
121 static inline int ms_cm2_a5n_support(uint8_t *cm2, int n) {
122  switch (n) {
123  case 0: return 1;
124  case 1: return (cm2[0] & (1<<3)) ? 0 : 1;
125  case 2: return (cm2[2] & (1<<0)) ? 1 : 0;
126  case 3: return (cm2[2] & (1<<1)) ? 1 : 0;
127  default:
128  return 0;
129  }
130 }
131 
132 /* According to GSM 04.08 Chapter 10.5.2.29 */
133 static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
134 static inline int rach_max_trans_raw2val(int raw) {
135  const int tbl[4] = { 1, 2, 4, 7 };
136  return tbl[raw & 3];
137 }
138 
139 #define ARFCN_PCS 0x8000
140 #define ARFCN_UPLINK 0x4000
141 #define ARFCN_FLAG_MASK 0xf000 /* Reserve the upper 5 bits for flags */
142 
143 enum gsm_band gsm_arfcn2band(uint16_t arfcn);
144 
145 /* Convert an ARFCN to the frequency in MHz * 10 */
146 uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
147 
148 /* Convert a Frequency in MHz * 10 to ARFCN */
149 uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink);
150 
151 /* Convert from frame number to GSM time */
152 void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
153 
154 /* Convert from GSM time to frame number */
155 uint32_t gsm_gsmtime2fn(struct gsm_time *time);
156 
157 /* GSM TS 03.03 Chapter 2.6 */
158 enum gprs_tlli_type {
159  TLLI_LOCAL,
160  TLLI_FOREIGN,
161  TLLI_RANDOM,
162  TLLI_AUXILIARY,
163  TLLI_RESERVED,
164 };
165 
166 /* TS 03.03 Chapter 2.6 */
167 int gprs_tlli_type(uint32_t tlli);
168 
169 uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type);
170 
171 /* Osmocom internal, not part of any gsm spec */
172 enum gsm_phys_chan_config {
173  GSM_PCHAN_NONE,
174  GSM_PCHAN_CCCH,
175  GSM_PCHAN_CCCH_SDCCH4,
176  GSM_PCHAN_TCH_F,
177  GSM_PCHAN_TCH_H,
178  GSM_PCHAN_SDCCH8_SACCH8C,
179  GSM_PCHAN_PDCH, /* GPRS PDCH */
180  GSM_PCHAN_TCH_F_PDCH, /* TCH/F if used, PDCH otherwise */
181  GSM_PCHAN_UNKNOWN,
182  _GSM_PCHAN_MAX
183 };
184 
185 /* Osmocom internal, not part of any gsm spec */
186 enum gsm_chan_t {
187  GSM_LCHAN_NONE,
188  GSM_LCHAN_SDCCH,
189  GSM_LCHAN_TCH_F,
190  GSM_LCHAN_TCH_H,
191  GSM_LCHAN_UNKNOWN,
192  GSM_LCHAN_CCCH,
193  GSM_LCHAN_PDTCH,
194  _GSM_LCHAN_MAX
195 };
196 
197 /* Deprectated functions */
198 int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n() instead");
199 int gsm_7bit_decode_ussd(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n_ussd() instead");
200 int gsm_7bit_encode(uint8_t *result, const char *data) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");
201 int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n_ussd() instead");
202 int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");
203 
204 
205 #endif