blob: c111f94ad1c4ea5b4695f86b0f7131d59fc5a563 [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
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020028#include <stddef.h>
Harald Welteec8b4502010-02-20 20:34:29 +010029#include <stdint.h>
30
Sylvain Munaut221030f2010-04-27 21:48:40 +020031#define ADD_MODULO(sum, delta, modulo) do { \
32 if ((sum += delta) >= modulo) \
33 sum -= modulo; \
34 } while (0)
35
36#define GSM_MAX_FN (26*51*2048)
37
Harald Welte622b7182010-03-07 17:50:21 +010038struct gsm_time {
39 uint32_t fn; /* FN count */
40 uint16_t t1; /* FN div (26*51) */
41 uint8_t t2; /* FN modulo 26 */
42 uint8_t t3; /* FN modulo 51 */
43 uint8_t tc;
44};
45
Harald Welteec8b4502010-02-20 20:34:29 +010046enum gsm_band {
47 GSM_BAND_850 = 1,
48 GSM_BAND_900 = 2,
49 GSM_BAND_1800 = 4,
50 GSM_BAND_1900 = 8,
51 GSM_BAND_450 = 0x10,
52 GSM_BAND_480 = 0x20,
53 GSM_BAND_750 = 0x40,
54 GSM_BAND_810 = 0x80,
55};
56
Harald Weltecbc80622010-03-22 08:28:44 +080057const char *gsm_band_name(enum gsm_band band);
Harald Welteaebe08c2010-03-04 10:39:17 +010058enum gsm_band gsm_band_parse(const char *mhz);
59
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020060/*!
61 * \brief Decode a sequence of GSM 03.38 encoded 7 bit characters.
62 *
63 * \param decoded The destination buffer for the decoded characters.
64 * \param n A maximum of n chars is written (incl. terminating \0).
65 * Requires n >= 1.
66 * \param user_data A pointer to the start of the packed 7bit character
67 * sequence.
68 * \param length The length of the input sequence (in octets).
69 *
70 * \returns the number of (8 bit) chars written excluding the terminating \0.
71 * This is the same like strlen(decoded).
72 */
73int gsm_7bit_decode_n(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
Harald Welteec8b4502010-02-20 20:34:29 +010074
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020075/*!
76 * \brief Decode a sequence of 7 bit characters (USSD encoding).
77 *
78 * \see gsm_7bit_encode_n()
79 */
80int gsm_7bit_decode_n_ussd(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
81
82/**
83 * \brief Encode a text string into GSM 03.38 encoded 7 bit characters.
84 *
85 * \param result The destination buffer for the packed 7 bit sequence.
86 * \param n A maximum of n octets is written.
87 * \param data A pointer to the start of the \0 terminated 8 bit character
88 * string.
89 * \param octets_written Iff not NULL, *octets_written will be set to the
90 * number of octets written to the result buffer.
91 *
92 * \returns the number of septets that have been created.
93 */
94int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets_written);
95
96/*!
97 * \brief Encode a text string into GSM 03.38 encoded 7 bit characters (USSD encoding).
98 *
99 * \see gsm_7bit_decode_n()
100 */
101int gsm_7bit_encode_n_ussd(uint8_t *result, size_t n, const char *data, int *octets_written);
102
103/* the four functions below are helper functions and here for the unit test */
Harald Welteca693882013-03-13 15:10:55 +0100104int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200105int gsm_septet_encode(uint8_t *result, const char *data);
106uint8_t gsm_get_octet_len(const uint8_t sept_len);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200107int 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 +0200108
Harald Welteb7b61232011-07-23 10:49:51 +0200109unsigned int ms_class_gmsk_dbm(enum gsm_band band, int ms_class);
Andreas Eversberg2a68c7c2011-06-26 11:41:48 +0200110
Harald Welteec8b4502010-02-20 20:34:29 +0100111int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
112int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
113
Bhaskar6b30f922013-05-16 17:35:49 +0530114/* According to TS 05.08 Chapter 8.1.4 */
Harald Welteec8b4502010-02-20 20:34:29 +0100115int rxlev2dbm(uint8_t rxlev);
116uint8_t dbm2rxlev(int dbm);
117
Sylvain Munaut4f585492010-06-09 13:38:56 +0200118/* According to GSM 04.08 Chapter 10.5.1.6 */
119static inline int ms_cm2_a5n_support(uint8_t *cm2, int n) {
120 switch (n) {
121 case 0: return 1;
122 case 1: return (cm2[0] & (1<<3)) ? 0 : 1;
123 case 2: return (cm2[2] & (1<<0)) ? 1 : 0;
124 case 3: return (cm2[2] & (1<<1)) ? 1 : 0;
125 default:
126 return 0;
127 }
128}
129
Harald Welteec8b4502010-02-20 20:34:29 +0100130/* According to GSM 04.08 Chapter 10.5.2.29 */
131static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
132static inline int rach_max_trans_raw2val(int raw) {
133 const int tbl[4] = { 1, 2, 4, 7 };
134 return tbl[raw & 3];
135}
136
Harald Welte622b7182010-03-07 17:50:21 +0100137#define ARFCN_PCS 0x8000
138#define ARFCN_UPLINK 0x4000
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100139#define ARFCN_FLAG_MASK 0xf000 /* Reserve the upper 5 bits for flags */
Harald Welte622b7182010-03-07 17:50:21 +0100140
141enum gsm_band gsm_arfcn2band(uint16_t arfcn);
142
143/* Convert an ARFCN to the frequency in MHz * 10 */
144uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
145
Sylvain Munaut55720312012-12-11 23:44:41 +0100146/* Convert a Frequency in MHz * 10 to ARFCN */
147uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink);
148
Harald Welte622b7182010-03-07 17:50:21 +0100149/* Convert from frame number to GSM time */
150void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
151
152/* Convert from GSM time to frame number */
153uint32_t gsm_gsmtime2fn(struct gsm_time *time);
154
Harald Weltea1c4f762010-05-01 11:59:42 +0200155/* GSM TS 03.03 Chapter 2.6 */
Harald Weltec2263172010-06-01 10:47:07 +0200156enum gprs_tlli_type {
Harald Weltea1c4f762010-05-01 11:59:42 +0200157 TLLI_LOCAL,
158 TLLI_FOREIGN,
159 TLLI_RANDOM,
160 TLLI_AUXILIARY,
161 TLLI_RESERVED,
162};
163
164/* TS 03.03 Chapter 2.6 */
165int gprs_tlli_type(uint32_t tlli);
166
Harald Weltec2263172010-06-01 10:47:07 +0200167uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type);
168
Harald Welte11c71932011-05-24 17:15:12 +0200169/* Osmocom internal, not part of any gsm spec */
170enum gsm_phys_chan_config {
171 GSM_PCHAN_NONE,
172 GSM_PCHAN_CCCH,
173 GSM_PCHAN_CCCH_SDCCH4,
174 GSM_PCHAN_TCH_F,
175 GSM_PCHAN_TCH_H,
176 GSM_PCHAN_SDCCH8_SACCH8C,
177 GSM_PCHAN_PDCH, /* GPRS PDCH */
178 GSM_PCHAN_TCH_F_PDCH, /* TCH/F if used, PDCH otherwise */
179 GSM_PCHAN_UNKNOWN,
180 _GSM_PCHAN_MAX
181};
182
183/* Osmocom internal, not part of any gsm spec */
184enum gsm_chan_t {
185 GSM_LCHAN_NONE,
186 GSM_LCHAN_SDCCH,
187 GSM_LCHAN_TCH_F,
188 GSM_LCHAN_TCH_H,
189 GSM_LCHAN_UNKNOWN,
190 GSM_LCHAN_CCCH,
Harald Welte81cd54a2012-04-19 23:18:43 +0200191 GSM_LCHAN_PDTCH,
Harald Welte11c71932011-05-24 17:15:12 +0200192 _GSM_LCHAN_MAX
193};
194
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200195/* Deprectated functions */
196int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length) __attribute__((deprecated ("Use gsm_7bit_decode_n() instead")));
197int gsm_7bit_decode_ussd(char *decoded, const uint8_t *user_data, uint8_t length) __attribute__((deprecated ("Use gsm_7bit_decode_n_ussd() instead")));
198int gsm_7bit_encode(uint8_t *result, const char *data) __attribute__((deprecated ("Use gsm_7bit_encode_n() instead")));
199int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets_written) __attribute__((deprecated ("Use gsm_7bit_encode_n_ussd() instead")));
200int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets_written) __attribute__((deprecated ("Use gsm_7bit_encode_n() instead")));
201
Harald Welte11c71932011-05-24 17:15:12 +0200202
Harald Welteec8b4502010-02-20 20:34:29 +0100203#endif