blob: 2cd88327a6d6d238ca5ffec69042a88c9a53a41e [file] [log] [blame]
Piotr Krysik4e4a92e2018-05-11 11:39:23 +02001/*! \file gsm_utils.h
2 * GSM utility functions, e.g. coding and decoding. */
3/*
4 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
5 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
7 *
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#pragma once
27
28#include <stddef.h>
29#include <stdint.h>
30
31#include <osmocom/core/defs.h>
32#include <osmocom/core/utils.h>
33
34#define ADD_MODULO(sum, delta, modulo) do { \
35 if ((sum += delta) >= modulo) \
36 sum -= modulo; \
37 } while (0)
38
39#define GSM_MAX_FN (26*51*2048)
40
41/* Max length of random identifier which can be requested via osmo_get_rand_id() */
42#define OSMO_MAX_RAND_ID_LEN 16
43
44//struct gsm_time {
45// uint32_t fn; /* FN count */
46// uint16_t t1; /* FN div (26*51) */
47// uint8_t t2; /* FN modulo 26 */
48// uint8_t t3; /* FN modulo 51 */
49// uint8_t tc;
50//};
51
52enum gsm_band {
53 GSM_BAND_850 = 1,
54 GSM_BAND_900 = 2,
55 GSM_BAND_1800 = 4,
56 GSM_BAND_1900 = 8,
57 GSM_BAND_450 = 0x10,
58 GSM_BAND_480 = 0x20,
59 GSM_BAND_750 = 0x40,
60 GSM_BAND_810 = 0x80,
61};
62
63const char *gsm_band_name(enum gsm_band band);
64enum gsm_band gsm_band_parse(const char *mhz);
65
66//int osmo_get_rand_id(uint8_t *out, size_t len);
67
68/*!
69 * Decode a sequence of GSM 03.38 encoded 7 bit characters.
70 *
71 * \param decoded The destination buffer for the decoded characters.
72 * \param n A maximum of n chars is written (incl. terminating \0).
73 * Requires n >= 1.
74 * \param user_data A pointer to the start of the packed 7bit character
75 * sequence.
76 * \param length The length of the input sequence in septets, for
77 * example pass octet_length*8/7.
78 *
79 * \returns the number of (8 bit) chars written excluding the terminating \0.
80 * This is the same like strlen(decoded).
81 */
82//int gsm_7bit_decode_n(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
83
84///*!
85// * Decode a sequence of 7 bit characters (USSD encoding).
86// *
87// * \see gsm_7bit_encode_n()
88// */
89//int gsm_7bit_decode_n_ussd(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
90
91///*!
92// * Encode a text string into GSM 03.38 encoded 7 bit characters.
93// *
94// * \param result The destination buffer for the packed 7 bit sequence.
95// * \param n A maximum of n octets is written.
96// * \param data A pointer to the start of the \0 terminated 8 bit character
97// * string.
98// * \param octets_written Iff not NULL, *octets_written will be set to the
99// * number of octets written to the result buffer.
100// *
101// * \returns the number of septets that have been created.
102// */
103//int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets_written);
104
105///*!
106// * Encode a text string into GSM 03.38 encoded 7 bit characters (USSD encoding).
107// *
108// * \see gsm_7bit_decode_n()
109// */
110//int gsm_7bit_encode_n_ussd(uint8_t *result, size_t n, const char *data, int *octets_written);
111
112///* the four functions below are helper functions and here for the unit test */
113//int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding);
114//int gsm_septet_encode(uint8_t *result, const char *data);
115//uint8_t gsm_get_octet_len(const uint8_t sept_len);
116//int gsm_7bit_decode_n_hdr(char *decoded, size_t n, const uint8_t *user_data, uint8_t length, uint8_t ud_hdr_ind);
117
118//unsigned int ms_class_gmsk_dbm(enum gsm_band band, int ms_class);
119
120//int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
121//int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
122
123///* According to TS 05.08 Chapter 8.1.4 */
124//int rxlev2dbm(uint8_t rxlev);
125//uint8_t dbm2rxlev(int dbm);
126
127///* According to GSM 04.08 Chapter 10.5.1.6 */
128//static inline int ms_cm2_a5n_support(uint8_t *cm2, unsigned n) {
129// switch (n) {
130// case 0: return 1;
131// case 1: return (cm2[0] & (1<<3)) ? 0 : 1;
132// case 2: return (cm2[2] & (1<<0)) ? 1 : 0;
133// case 3: return (cm2[2] & (1<<1)) ? 1 : 0;
134// default:
135// return 0;
136// }
137//}
138
139///* According to GSM 04.08 Chapter 10.5.1.7 */
140//static inline int ms_cm3_a5n_support(uint8_t *cm3, unsigned n) {
141// switch (n) {
142// case 4: return (cm3[0] & (1<<0)) ? 1 : 0;
143// case 5: return (cm3[0] & (1<<1)) ? 1 : 0;
144// case 6: return (cm3[0] & (1<<2)) ? 1 : 0;
145// case 7: return (cm3[0] & (1<<3)) ? 1 : 0;
146// default:
147// return 0;
148// }
149//}
150
151///* According to GSM 04.08 Chapter 10.5.2.29 */
152//static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
153//static inline int rach_max_trans_raw2val(int raw) {
154// const int tbl[4] = { 1, 2, 4, 7 };
155// return tbl[raw & 3];
156//}
157
158#define ARFCN_PCS 0x8000
159#define ARFCN_UPLINK 0x4000
160#define ARFCN_FLAG_MASK 0xf000 /* Reserve the upper 5 bits for flags */
161
162enum gsm_band gsm_arfcn2band(uint16_t arfcn);
163
164/* Convert an ARFCN to the frequency in MHz * 10 */
165uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
166
167/* Convert a Frequency in MHz * 10 to ARFCN */
168uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink);
169
170/* Convert from frame number to GSM time */
171//void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
172
173/* Parse GSM Frame Number into printable string */
174//char *gsm_fn_as_gsmtime_str(uint32_t fn);
175
176/* Convert from GSM time to frame number */
177//uint32_t gsm_gsmtime2fn(struct gsm_time *time);
178
179/* Returns static buffer with string representation of a GSM Time */
180//char *osmo_dump_gsmtime(const struct gsm_time *tm);
181
182/* GSM TS 03.03 Chapter 2.6 */
183//enum gprs_tlli_type {
184// TLLI_LOCAL,
185// TLLI_FOREIGN,
186// TLLI_RANDOM,
187// TLLI_AUXILIARY,
188// TLLI_RESERVED,
189// TLLI_G_RNTI,
190// TLLI_RAND_G_RNTI,
191//};
192
193/* TS 03.03 Chapter 2.6 */
194//int gprs_tlli_type(uint32_t tlli);
195
196//uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type);
197
198///* Osmocom internal, not part of any gsm spec */
199//enum gsm_phys_chan_config {
200// GSM_PCHAN_NONE,
201// GSM_PCHAN_CCCH,
202// GSM_PCHAN_CCCH_SDCCH4,
203// GSM_PCHAN_TCH_F,
204// GSM_PCHAN_TCH_H,
205// GSM_PCHAN_SDCCH8_SACCH8C,
206// GSM_PCHAN_PDCH, /* GPRS PDCH */
207// GSM_PCHAN_TCH_F_PDCH, /* TCH/F if used, PDCH otherwise */
208// GSM_PCHAN_UNKNOWN,
209// GSM_PCHAN_CCCH_SDCCH4_CBCH,
210// GSM_PCHAN_SDCCH8_SACCH8C_CBCH,
211// GSM_PCHAN_TCH_F_TCH_H_PDCH,
212// _GSM_PCHAN_MAX
213//};
214
215///* Osmocom internal, not part of any gsm spec */
216//enum gsm_chan_t {
217// GSM_LCHAN_NONE,
218// GSM_LCHAN_SDCCH,
219// GSM_LCHAN_TCH_F,
220// GSM_LCHAN_TCH_H,
221// GSM_LCHAN_UNKNOWN,
222// GSM_LCHAN_CCCH,
223// GSM_LCHAN_PDTCH,
224// GSM_LCHAN_CBCH,
225// _GSM_LCHAN_MAX
226//};
227
228//extern const struct value_string gsm_chan_t_names[];
229
230///* Deprectated functions */
231///* Limit encoding and decoding to use no more than this amount of buffer bytes */
232//#define GSM_7BIT_LEGACY_MAX_BUFFER_SIZE 0x10000
233
234//int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n() instead");
235//int gsm_7bit_decode_ussd(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n_ussd() instead");
236//int gsm_7bit_encode(uint8_t *result, const char *data) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");
237//int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n_ussd() instead");
238//int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");