Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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> |
Harald Welte | aebe08c | 2010-03-04 10:39:17 +0100 | [diff] [blame] | 5 | * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 25 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | |
Jacob Erlbeck | 1d7f3b5 | 2013-08-12 17:07:53 +0200 | [diff] [blame] | 27 | #include <stddef.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | |
Jacob Erlbeck | 6b26218 | 2013-08-14 12:26:27 +0200 | [diff] [blame] | 30 | #include <osmocom/core/defs.h> |
Max | fb348ee | 2016-03-30 21:14:53 +0200 | [diff] [blame] | 31 | #include <osmocom/core/utils.h> |
Jacob Erlbeck | 3f69ceb | 2013-08-12 18:31:59 +0200 | [diff] [blame] | 32 | |
Sylvain Munaut | 221030f | 2010-04-27 21:48:40 +0200 | [diff] [blame] | 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 | |
Harald Welte | 622b718 | 2010-03-07 17:50:21 +0100 | [diff] [blame] | 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 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 | |
Harald Welte | cbc8062 | 2010-03-22 08:28:44 +0800 | [diff] [blame] | 59 | const char *gsm_band_name(enum gsm_band band); |
Harald Welte | aebe08c | 2010-03-04 10:39:17 +0100 | [diff] [blame] | 60 | enum gsm_band gsm_band_parse(const char *mhz); |
| 61 | |
Jacob Erlbeck | 1d7f3b5 | 2013-08-12 17:07:53 +0200 | [diff] [blame] | 62 | /*! |
| 63 | * \brief Decode a sequence of GSM 03.38 encoded 7 bit characters. |
| 64 | * |
| 65 | * \param decoded The destination buffer for the decoded characters. |
| 66 | * \param n A maximum of n chars is written (incl. terminating \0). |
| 67 | * Requires n >= 1. |
| 68 | * \param user_data A pointer to the start of the packed 7bit character |
| 69 | * sequence. |
Neels Hofmeyr | cda9885 | 2016-02-29 09:56:26 +0100 | [diff] [blame] | 70 | * \param length The length of the input sequence in septets, for |
| 71 | * example pass octet_length*8/7. |
Jacob Erlbeck | 1d7f3b5 | 2013-08-12 17:07:53 +0200 | [diff] [blame] | 72 | * |
| 73 | * \returns the number of (8 bit) chars written excluding the terminating \0. |
| 74 | * This is the same like strlen(decoded). |
| 75 | */ |
| 76 | int gsm_7bit_decode_n(char *decoded, size_t n, const uint8_t *user_data, uint8_t length); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 77 | |
Jacob Erlbeck | 1d7f3b5 | 2013-08-12 17:07:53 +0200 | [diff] [blame] | 78 | /*! |
| 79 | * \brief Decode a sequence of 7 bit characters (USSD encoding). |
| 80 | * |
| 81 | * \see gsm_7bit_encode_n() |
| 82 | */ |
| 83 | int gsm_7bit_decode_n_ussd(char *decoded, size_t n, const uint8_t *user_data, uint8_t length); |
| 84 | |
Jacob Erlbeck | 3f69ceb | 2013-08-12 18:31:59 +0200 | [diff] [blame] | 85 | /*! |
Jacob Erlbeck | 1d7f3b5 | 2013-08-12 17:07:53 +0200 | [diff] [blame] | 86 | * \brief Encode a text string into GSM 03.38 encoded 7 bit characters. |
| 87 | * |
| 88 | * \param result The destination buffer for the packed 7 bit sequence. |
| 89 | * \param n A maximum of n octets is written. |
| 90 | * \param data A pointer to the start of the \0 terminated 8 bit character |
| 91 | * string. |
| 92 | * \param octets_written Iff not NULL, *octets_written will be set to the |
| 93 | * number of octets written to the result buffer. |
| 94 | * |
| 95 | * \returns the number of septets that have been created. |
| 96 | */ |
| 97 | int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets_written); |
| 98 | |
| 99 | /*! |
| 100 | * \brief Encode a text string into GSM 03.38 encoded 7 bit characters (USSD encoding). |
| 101 | * |
| 102 | * \see gsm_7bit_decode_n() |
| 103 | */ |
| 104 | int gsm_7bit_encode_n_ussd(uint8_t *result, size_t n, const char *data, int *octets_written); |
| 105 | |
| 106 | /* the four functions below are helper functions and here for the unit test */ |
Harald Welte | ca69388 | 2013-03-13 15:10:55 +0100 | [diff] [blame] | 107 | int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding); |
Dennis Wehrle | 291e613 | 2011-07-24 20:14:13 +0200 | [diff] [blame] | 108 | int gsm_septet_encode(uint8_t *result, const char *data); |
| 109 | uint8_t gsm_get_octet_len(const uint8_t sept_len); |
Jacob Erlbeck | 1d7f3b5 | 2013-08-12 17:07:53 +0200 | [diff] [blame] | 110 | int gsm_7bit_decode_n_hdr(char *decoded, size_t n, const uint8_t *user_data, uint8_t length, uint8_t ud_hdr_ind); |
Dennis Wehrle | 291e613 | 2011-07-24 20:14:13 +0200 | [diff] [blame] | 111 | |
Harald Welte | b7b6123 | 2011-07-23 10:49:51 +0200 | [diff] [blame] | 112 | unsigned int ms_class_gmsk_dbm(enum gsm_band band, int ms_class); |
Andreas Eversberg | 2a68c7c | 2011-06-26 11:41:48 +0200 | [diff] [blame] | 113 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 114 | int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm); |
| 115 | int ms_pwr_dbm(enum gsm_band band, uint8_t lvl); |
| 116 | |
Bhaskar | 6b30f92 | 2013-05-16 17:35:49 +0530 | [diff] [blame] | 117 | /* According to TS 05.08 Chapter 8.1.4 */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 118 | int rxlev2dbm(uint8_t rxlev); |
| 119 | uint8_t dbm2rxlev(int dbm); |
| 120 | |
Sylvain Munaut | 4f58549 | 2010-06-09 13:38:56 +0200 | [diff] [blame] | 121 | /* According to GSM 04.08 Chapter 10.5.1.6 */ |
Max | 402b9b0 | 2016-04-21 14:58:30 +0200 | [diff] [blame] | 122 | static inline int ms_cm2_a5n_support(uint8_t *cm2, unsigned n) { |
Sylvain Munaut | 4f58549 | 2010-06-09 13:38:56 +0200 | [diff] [blame] | 123 | switch (n) { |
| 124 | case 0: return 1; |
| 125 | case 1: return (cm2[0] & (1<<3)) ? 0 : 1; |
| 126 | case 2: return (cm2[2] & (1<<0)) ? 1 : 0; |
| 127 | case 3: return (cm2[2] & (1<<1)) ? 1 : 0; |
| 128 | default: |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | |
Max | 402b9b0 | 2016-04-21 14:58:30 +0200 | [diff] [blame] | 133 | /* According to GSM 04.08 Chapter 10.5.1.7 */ |
| 134 | static inline int ms_cm3_a5n_support(uint8_t *cm3, unsigned n) { |
| 135 | switch (n) { |
| 136 | case 4: return (cm3[0] & (1<<0)) ? 1 : 0; |
| 137 | case 5: return (cm3[0] & (1<<1)) ? 1 : 0; |
| 138 | case 6: return (cm3[0] & (1<<2)) ? 1 : 0; |
| 139 | case 7: return (cm3[0] & (1<<3)) ? 1 : 0; |
| 140 | default: |
| 141 | return 0; |
| 142 | } |
| 143 | } |
| 144 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 145 | /* According to GSM 04.08 Chapter 10.5.2.29 */ |
| 146 | static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; } |
| 147 | static inline int rach_max_trans_raw2val(int raw) { |
| 148 | const int tbl[4] = { 1, 2, 4, 7 }; |
| 149 | return tbl[raw & 3]; |
| 150 | } |
| 151 | |
Harald Welte | 622b718 | 2010-03-07 17:50:21 +0100 | [diff] [blame] | 152 | #define ARFCN_PCS 0x8000 |
| 153 | #define ARFCN_UPLINK 0x4000 |
Sylvain Munaut | 2a471ee | 2010-11-13 17:51:37 +0100 | [diff] [blame] | 154 | #define ARFCN_FLAG_MASK 0xf000 /* Reserve the upper 5 bits for flags */ |
Harald Welte | 622b718 | 2010-03-07 17:50:21 +0100 | [diff] [blame] | 155 | |
| 156 | enum gsm_band gsm_arfcn2band(uint16_t arfcn); |
| 157 | |
| 158 | /* Convert an ARFCN to the frequency in MHz * 10 */ |
| 159 | uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink); |
| 160 | |
Sylvain Munaut | 5572031 | 2012-12-11 23:44:41 +0100 | [diff] [blame] | 161 | /* Convert a Frequency in MHz * 10 to ARFCN */ |
| 162 | uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink); |
| 163 | |
Harald Welte | 622b718 | 2010-03-07 17:50:21 +0100 | [diff] [blame] | 164 | /* Convert from frame number to GSM time */ |
| 165 | void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn); |
| 166 | |
| 167 | /* Convert from GSM time to frame number */ |
| 168 | uint32_t gsm_gsmtime2fn(struct gsm_time *time); |
| 169 | |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 170 | /* GSM TS 03.03 Chapter 2.6 */ |
Harald Welte | c226317 | 2010-06-01 10:47:07 +0200 | [diff] [blame] | 171 | enum gprs_tlli_type { |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 172 | TLLI_LOCAL, |
| 173 | TLLI_FOREIGN, |
| 174 | TLLI_RANDOM, |
| 175 | TLLI_AUXILIARY, |
| 176 | TLLI_RESERVED, |
Harald Welte | 1f6aad1 | 2015-08-15 19:51:45 +0200 | [diff] [blame] | 177 | TLLI_G_RNTI, |
| 178 | TLLI_RAND_G_RNTI, |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | /* TS 03.03 Chapter 2.6 */ |
| 182 | int gprs_tlli_type(uint32_t tlli); |
| 183 | |
Harald Welte | c226317 | 2010-06-01 10:47:07 +0200 | [diff] [blame] | 184 | uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type); |
| 185 | |
Harald Welte | 11c7193 | 2011-05-24 17:15:12 +0200 | [diff] [blame] | 186 | /* Osmocom internal, not part of any gsm spec */ |
| 187 | enum gsm_phys_chan_config { |
| 188 | GSM_PCHAN_NONE, |
| 189 | GSM_PCHAN_CCCH, |
| 190 | GSM_PCHAN_CCCH_SDCCH4, |
| 191 | GSM_PCHAN_TCH_F, |
| 192 | GSM_PCHAN_TCH_H, |
| 193 | GSM_PCHAN_SDCCH8_SACCH8C, |
| 194 | GSM_PCHAN_PDCH, /* GPRS PDCH */ |
| 195 | GSM_PCHAN_TCH_F_PDCH, /* TCH/F if used, PDCH otherwise */ |
| 196 | GSM_PCHAN_UNKNOWN, |
Harald Welte | 1b12d16 | 2014-12-28 13:37:01 +0100 | [diff] [blame] | 197 | GSM_PCHAN_CCCH_SDCCH4_CBCH, |
| 198 | GSM_PCHAN_SDCCH8_SACCH8C_CBCH, |
Neels Hofmeyr | fd80f5a | 2016-07-14 03:21:05 +0200 | [diff] [blame] | 199 | GSM_PCHAN_TCH_F_TCH_H_PDCH, |
Harald Welte | 11c7193 | 2011-05-24 17:15:12 +0200 | [diff] [blame] | 200 | _GSM_PCHAN_MAX |
| 201 | }; |
| 202 | |
| 203 | /* Osmocom internal, not part of any gsm spec */ |
| 204 | enum gsm_chan_t { |
| 205 | GSM_LCHAN_NONE, |
| 206 | GSM_LCHAN_SDCCH, |
| 207 | GSM_LCHAN_TCH_F, |
| 208 | GSM_LCHAN_TCH_H, |
| 209 | GSM_LCHAN_UNKNOWN, |
| 210 | GSM_LCHAN_CCCH, |
Harald Welte | 81cd54a | 2012-04-19 23:18:43 +0200 | [diff] [blame] | 211 | GSM_LCHAN_PDTCH, |
Harald Welte | 1b12d16 | 2014-12-28 13:37:01 +0100 | [diff] [blame] | 212 | GSM_LCHAN_CBCH, |
Harald Welte | 11c7193 | 2011-05-24 17:15:12 +0200 | [diff] [blame] | 213 | _GSM_LCHAN_MAX |
| 214 | }; |
| 215 | |
Max | fb348ee | 2016-03-30 21:14:53 +0200 | [diff] [blame] | 216 | extern const struct value_string gsm_chan_t_names[]; |
| 217 | |
Jacob Erlbeck | 1d7f3b5 | 2013-08-12 17:07:53 +0200 | [diff] [blame] | 218 | /* Deprectated functions */ |
Jacob Erlbeck | 26cbd45 | 2014-01-07 13:39:24 +0100 | [diff] [blame] | 219 | /* Limit encoding and decoding to use no more than this amount of buffer bytes */ |
| 220 | #define GSM_7BIT_LEGACY_MAX_BUFFER_SIZE 0x10000 |
| 221 | |
Jacob Erlbeck | 3f69ceb | 2013-08-12 18:31:59 +0200 | [diff] [blame] | 222 | int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n() instead"); |
| 223 | int gsm_7bit_decode_ussd(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n_ussd() instead"); |
| 224 | int gsm_7bit_encode(uint8_t *result, const char *data) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead"); |
| 225 | int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n_ussd() instead"); |
| 226 | int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead"); |