blob: 45af4f24358c6c455e694e65f3b75134eabc40a1 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm_utils.h
2 * GSM utility functions, e.g. coding and decoding. */
Harald Welteec8b4502010-02-20 20:34:29 +01003/*
4 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
5 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welteaebe08c2010-03-04 10:39:17 +01006 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
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
Sylvain Munaut12ba7782014-06-16 10:13:40 +020026#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +010027
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020028#include <stddef.h>
Harald Welteec8b4502010-02-20 20:34:29 +010029#include <stdint.h>
30
Jacob Erlbeck6b262182013-08-14 12:26:27 +020031#include <osmocom/core/defs.h>
Maxfb348ee2016-03-30 21:14:53 +020032#include <osmocom/core/utils.h>
Jacob Erlbeck3f69ceb2013-08-12 18:31:59 +020033
Sylvain Munaut221030f2010-04-27 21:48:40 +020034#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
Max4b2b0cc2017-07-10 14:32:48 +020041/* Max length of random identifier which can be requested via osmo_get_rand_id() */
42#define OSMO_MAX_RAND_ID_LEN 16
43
Harald Welte622b7182010-03-07 17:50:21 +010044struct 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
Harald Welteec8b4502010-02-20 20:34:29 +010052enum 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
Harald Weltecbc80622010-03-22 08:28:44 +080063const char *gsm_band_name(enum gsm_band band);
Harald Welteaebe08c2010-03-04 10:39:17 +010064enum gsm_band gsm_band_parse(const char *mhz);
65
Max4b2b0cc2017-07-10 14:32:48 +020066int osmo_get_rand_id(uint8_t *out, size_t len);
67
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020068/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +020069 * Decode a sequence of GSM 03.38 encoded 7 bit characters.
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020070 *
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.
Neels Hofmeyrcda98852016-02-29 09:56:26 +010076 * \param length The length of the input sequence in septets, for
77 * example pass octet_length*8/7.
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020078 *
79 * \returns the number of (8 bit) chars written excluding the terminating \0.
80 * This is the same like strlen(decoded).
81 */
82int gsm_7bit_decode_n(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
Harald Welteec8b4502010-02-20 20:34:29 +010083
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020084/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +020085 * Decode a sequence of 7 bit characters (USSD encoding).
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020086 *
87 * \see gsm_7bit_encode_n()
88 */
89int gsm_7bit_decode_n_ussd(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
90
Jacob Erlbeck3f69ceb2013-08-12 18:31:59 +020091/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092 * Encode a text string into GSM 03.38 encoded 7 bit characters.
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020093 *
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 */
103int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets_written);
104
105/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106 * Encode a text string into GSM 03.38 encoded 7 bit characters (USSD encoding).
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200107 *
108 * \see gsm_7bit_decode_n()
109 */
110int 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 */
Vadim Yanitskiy943133c2021-01-30 01:31:32 +0100113int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding)
114 OSMO_DEPRECATED("This function is unable to handle more than 255 septets, "
115 "use gsm_septet_pack() instead.");
116int gsm_septet_pack(uint8_t *result, const uint8_t *rdata, size_t septet_len, uint8_t padding);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200117int gsm_septet_encode(uint8_t *result, const char *data);
118uint8_t gsm_get_octet_len(const uint8_t sept_len);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200119int gsm_7bit_decode_n_hdr(char *decoded, size_t n, const uint8_t *user_data, uint8_t length, uint8_t ud_hdr_ind);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200120
Pau Espin Pedrole40b9632019-10-31 15:38:30 +0100121int ms_class_gmsk_dbm(enum gsm_band band, int ms_class);
Harald Welteec8b4502010-02-20 20:34:29 +0100122int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
123int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
124
Bhaskar6b30f922013-05-16 17:35:49 +0530125/* According to TS 05.08 Chapter 8.1.4 */
Harald Welteec8b4502010-02-20 20:34:29 +0100126int rxlev2dbm(uint8_t rxlev);
127uint8_t dbm2rxlev(int dbm);
128
Sylvain Munaut4f585492010-06-09 13:38:56 +0200129/* According to GSM 04.08 Chapter 10.5.1.6 */
Max402b9b02016-04-21 14:58:30 +0200130static inline int ms_cm2_a5n_support(uint8_t *cm2, unsigned n) {
Sylvain Munaut4f585492010-06-09 13:38:56 +0200131 switch (n) {
132 case 0: return 1;
133 case 1: return (cm2[0] & (1<<3)) ? 0 : 1;
134 case 2: return (cm2[2] & (1<<0)) ? 1 : 0;
135 case 3: return (cm2[2] & (1<<1)) ? 1 : 0;
136 default:
137 return 0;
138 }
139}
140
Max402b9b02016-04-21 14:58:30 +0200141/* According to GSM 04.08 Chapter 10.5.1.7 */
142static inline int ms_cm3_a5n_support(uint8_t *cm3, unsigned n) {
143 switch (n) {
144 case 4: return (cm3[0] & (1<<0)) ? 1 : 0;
145 case 5: return (cm3[0] & (1<<1)) ? 1 : 0;
146 case 6: return (cm3[0] & (1<<2)) ? 1 : 0;
147 case 7: return (cm3[0] & (1<<3)) ? 1 : 0;
148 default:
149 return 0;
150 }
151}
152
Harald Welteec8b4502010-02-20 20:34:29 +0100153/* According to GSM 04.08 Chapter 10.5.2.29 */
154static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
155static inline int rach_max_trans_raw2val(int raw) {
156 const int tbl[4] = { 1, 2, 4, 7 };
157 return tbl[raw & 3];
158}
159
Harald Welte622b7182010-03-07 17:50:21 +0100160#define ARFCN_PCS 0x8000
161#define ARFCN_UPLINK 0x4000
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100162#define ARFCN_FLAG_MASK 0xf000 /* Reserve the upper 5 bits for flags */
Harald Welte622b7182010-03-07 17:50:21 +0100163
Pau Espin Pedrolc8772512018-11-16 12:59:46 +0100164int gsm_arfcn2band_rc(uint16_t arfcn, enum gsm_band *band);
165enum gsm_band gsm_arfcn2band(uint16_t arfcn) OSMO_DEPRECATED("Use gsm_arfcn2band_rc() instead");
Harald Welte622b7182010-03-07 17:50:21 +0100166
167/* Convert an ARFCN to the frequency in MHz * 10 */
168uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
169
Sylvain Munaut55720312012-12-11 23:44:41 +0100170/* Convert a Frequency in MHz * 10 to ARFCN */
171uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink);
172
Harald Welte622b7182010-03-07 17:50:21 +0100173/* Convert from frame number to GSM time */
174void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
175
Philipp Maierb808da42017-06-26 10:50:28 +0200176/* Parse GSM Frame Number into printable string */
177char *gsm_fn_as_gsmtime_str(uint32_t fn);
178
Harald Welte622b7182010-03-07 17:50:21 +0100179/* Convert from GSM time to frame number */
180uint32_t gsm_gsmtime2fn(struct gsm_time *time);
181
Pau Espin Pedrol363130f2017-07-03 10:42:42 +0200182/* Returns static buffer with string representation of a GSM Time */
183char *osmo_dump_gsmtime(const struct gsm_time *tm);
Harald Welte4a62eda2019-03-18 18:27:00 +0100184char *osmo_dump_gsmtime_buf(char *buf, size_t buf_len, const struct gsm_time *tm);
Harald Welte179f3572019-03-18 18:38:47 +0100185char *osmo_dump_gsmtime_c(const void *ctx, const struct gsm_time *tm);
Pau Espin Pedrol363130f2017-07-03 10:42:42 +0200186
Harald Weltea1c4f762010-05-01 11:59:42 +0200187/* GSM TS 03.03 Chapter 2.6 */
Harald Weltec2263172010-06-01 10:47:07 +0200188enum gprs_tlli_type {
Harald Weltea1c4f762010-05-01 11:59:42 +0200189 TLLI_LOCAL,
190 TLLI_FOREIGN,
191 TLLI_RANDOM,
192 TLLI_AUXILIARY,
193 TLLI_RESERVED,
Harald Welte1f6aad12015-08-15 19:51:45 +0200194 TLLI_G_RNTI,
195 TLLI_RAND_G_RNTI,
Harald Weltea1c4f762010-05-01 11:59:42 +0200196};
197
198/* TS 03.03 Chapter 2.6 */
199int gprs_tlli_type(uint32_t tlli);
200
Harald Weltec2263172010-06-01 10:47:07 +0200201uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type);
202
Harald Welte11c71932011-05-24 17:15:12 +0200203/* Osmocom internal, not part of any gsm spec */
204enum gsm_phys_chan_config {
205 GSM_PCHAN_NONE,
206 GSM_PCHAN_CCCH,
207 GSM_PCHAN_CCCH_SDCCH4,
208 GSM_PCHAN_TCH_F,
209 GSM_PCHAN_TCH_H,
210 GSM_PCHAN_SDCCH8_SACCH8C,
211 GSM_PCHAN_PDCH, /* GPRS PDCH */
212 GSM_PCHAN_TCH_F_PDCH, /* TCH/F if used, PDCH otherwise */
213 GSM_PCHAN_UNKNOWN,
Harald Welte1b12d162014-12-28 13:37:01 +0100214 GSM_PCHAN_CCCH_SDCCH4_CBCH,
215 GSM_PCHAN_SDCCH8_SACCH8C_CBCH,
Pau Espin Pedrol45dc9d02021-06-28 12:12:53 +0200216 GSM_PCHAN_OSMO_DYN,
Harald Welte11c71932011-05-24 17:15:12 +0200217 _GSM_PCHAN_MAX
218};
Pau Espin Pedrol45dc9d02021-06-28 12:12:53 +0200219/* Backward compatibility with older naming: */
220#define GSM_PCHAN_TCH_F_TCH_H_PDCH GSM_PCHAN_OSMO_DYN
Harald Welte11c71932011-05-24 17:15:12 +0200221
222/* Osmocom internal, not part of any gsm spec */
223enum gsm_chan_t {
224 GSM_LCHAN_NONE,
225 GSM_LCHAN_SDCCH,
226 GSM_LCHAN_TCH_F,
227 GSM_LCHAN_TCH_H,
228 GSM_LCHAN_UNKNOWN,
229 GSM_LCHAN_CCCH,
Harald Welte81cd54a2012-04-19 23:18:43 +0200230 GSM_LCHAN_PDTCH,
Harald Welte1b12d162014-12-28 13:37:01 +0100231 GSM_LCHAN_CBCH,
Harald Welte11c71932011-05-24 17:15:12 +0200232 _GSM_LCHAN_MAX
233};
234
Maxfb348ee2016-03-30 21:14:53 +0200235extern const struct value_string gsm_chan_t_names[];
Vadim Yanitskiy412a4bb2019-05-02 14:31:44 +0200236static inline const char *gsm_chan_t_name(enum gsm_chan_t val)
Philipp Maierb0663052018-09-25 13:05:40 +0200237{ return get_value_string(gsm_chan_t_names, val); }
Maxfb348ee2016-03-30 21:14:53 +0200238
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200239/* Deprectated functions */
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100240/* Limit encoding and decoding to use no more than this amount of buffer bytes */
241#define GSM_7BIT_LEGACY_MAX_BUFFER_SIZE 0x10000
242
Jacob Erlbeck3f69ceb2013-08-12 18:31:59 +0200243int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n() instead");
244int gsm_7bit_decode_ussd(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n_ussd() instead");
245int gsm_7bit_encode(uint8_t *result, const char *data) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");
246int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n_ussd() instead");
247int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");
Neels Hofmeyr5b5c3492018-12-26 18:03:17 +0100248
249enum osmo_rat_type {
250 OSMO_RAT_UNKNOWN = 0,
251 OSMO_RAT_GERAN_A,
252 OSMO_RAT_UTRAN_IU,
Neels Hofmeyr0d39a8d2019-01-04 04:37:00 +0100253 OSMO_RAT_EUTRAN_SGS,
Neels Hofmeyr5b5c3492018-12-26 18:03:17 +0100254
255 /* keep this last */
256 OSMO_RAT_COUNT
257};
258
259extern const struct value_string osmo_rat_type_names[];
260inline static const char *osmo_rat_type_name(enum osmo_rat_type val)
261{ return get_value_string(osmo_rat_type_names, val); }