libosmogsm  0.11.0-dirty
Osmocom GSM library
include/osmocom/gsm/gsm_utils.h
Go to the documentation of this file.
00001 
00003 /*
00004  * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
00005  * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
00006  * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
00007  *
00008  * All Rights Reserved
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License along
00021  * with this program; if not, write to the Free Software Foundation, Inc.,
00022  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00023  *
00024  */
00025 
00026 #pragma once
00027 
00028 #include <stddef.h>
00029 #include <stdint.h>
00030 
00031 #include <osmocom/core/defs.h>
00032 #include <osmocom/core/utils.h>
00033 
00034 #define ADD_MODULO(sum, delta, modulo) do {     \
00035         if ((sum += delta) >= modulo)           \
00036                 sum -= modulo;                  \
00037         } while (0)
00038 
00039 #define GSM_MAX_FN      (26*51*2048)
00040 
00041 /* Max length of random identifier which can be requested via osmo_get_rand_id() */
00042 #define OSMO_MAX_RAND_ID_LEN 16
00043 
00044 struct gsm_time {
00045         uint32_t        fn;     /* FN count */
00046         uint16_t        t1;     /* FN div (26*51) */
00047         uint8_t         t2;     /* FN modulo 26 */
00048         uint8_t         t3;     /* FN modulo 51 */
00049         uint8_t         tc;
00050 };
00051 
00052 enum gsm_band {
00053         GSM_BAND_850    = 1,
00054         GSM_BAND_900    = 2,
00055         GSM_BAND_1800   = 4,
00056         GSM_BAND_1900   = 8,
00057         GSM_BAND_450    = 0x10,
00058         GSM_BAND_480    = 0x20,
00059         GSM_BAND_750    = 0x40,
00060         GSM_BAND_810    = 0x80,
00061 };
00062 
00063 const char *gsm_band_name(enum gsm_band band);
00064 enum gsm_band gsm_band_parse(const char *mhz);
00065 
00066 int osmo_get_rand_id(uint8_t *out, size_t len);
00067 
00082 int gsm_7bit_decode_n(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
00083 
00089 int gsm_7bit_decode_n_ussd(char *decoded, size_t n, const uint8_t *user_data, uint8_t length);
00090 
00103 int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets_written);
00104 
00110 int gsm_7bit_encode_n_ussd(uint8_t *result, size_t n, const char *data, int *octets_written);
00111 
00112 /* the four functions below are helper functions and here for the unit test */
00113 int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding);
00114 int gsm_septet_encode(uint8_t *result, const char *data);
00115 uint8_t gsm_get_octet_len(const uint8_t sept_len);
00116 int gsm_7bit_decode_n_hdr(char *decoded, size_t n, const uint8_t *user_data, uint8_t length, uint8_t ud_hdr_ind);
00117 
00118 unsigned int ms_class_gmsk_dbm(enum gsm_band band, int ms_class);
00119 
00120 int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
00121 int ms_pwr_dbm(enum gsm_band band, uint8_t lvl);
00122 
00123 /* According to TS 05.08 Chapter 8.1.4 */
00124 int rxlev2dbm(uint8_t rxlev);
00125 uint8_t dbm2rxlev(int dbm);
00126 
00127 /* According to GSM 04.08 Chapter 10.5.1.6 */
00128 static inline int ms_cm2_a5n_support(uint8_t *cm2, unsigned n) {
00129         switch (n) {
00130                 case 0: return 1;
00131                 case 1: return (cm2[0] & (1<<3)) ? 0 : 1;
00132                 case 2: return (cm2[2] & (1<<0)) ? 1 : 0;
00133                 case 3: return (cm2[2] & (1<<1)) ? 1 : 0;
00134                 default:
00135                         return 0;
00136         }
00137 }
00138 
00139 /* According to GSM 04.08 Chapter 10.5.1.7 */
00140 static inline int ms_cm3_a5n_support(uint8_t *cm3, unsigned n) {
00141         switch (n) {
00142                 case 4: return (cm3[0] & (1<<0)) ? 1 : 0;
00143                 case 5: return (cm3[0] & (1<<1)) ? 1 : 0;
00144                 case 6: return (cm3[0] & (1<<2)) ? 1 : 0;
00145                 case 7: return (cm3[0] & (1<<3)) ? 1 : 0;
00146                 default:
00147                         return 0;
00148         }
00149 }
00150 
00151 /* According to GSM 04.08 Chapter 10.5.2.29 */
00152 static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
00153 static inline int rach_max_trans_raw2val(int raw) {
00154         const int tbl[4] = { 1, 2, 4, 7 };
00155         return tbl[raw & 3];
00156 }
00157 
00158 #define ARFCN_PCS       0x8000
00159 #define ARFCN_UPLINK    0x4000
00160 #define ARFCN_FLAG_MASK 0xf000  /* Reserve the upper 5 bits for flags */
00161 
00162 enum gsm_band gsm_arfcn2band(uint16_t arfcn);
00163 
00164 /* Convert an ARFCN to the frequency in MHz * 10 */
00165 uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink);
00166 
00167 /* Convert a Frequency in MHz * 10 to ARFCN */
00168 uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink);
00169 
00170 /* Convert from frame number to GSM time */
00171 void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn);
00172 
00173 /* Parse GSM Frame Number into printable string */
00174 char *gsm_fn_as_gsmtime_str(uint32_t fn);
00175 
00176 /* Convert from GSM time to frame number */
00177 uint32_t gsm_gsmtime2fn(struct gsm_time *time);
00178 
00179 /* Returns static buffer with string representation of a GSM Time */
00180 char *osmo_dump_gsmtime(const struct gsm_time *tm);
00181 
00182 /* GSM TS 03.03 Chapter 2.6 */
00183 enum gprs_tlli_type {
00184         TLLI_LOCAL,
00185         TLLI_FOREIGN,
00186         TLLI_RANDOM,
00187         TLLI_AUXILIARY,
00188         TLLI_RESERVED,
00189         TLLI_G_RNTI,
00190         TLLI_RAND_G_RNTI,
00191 };
00192 
00193 /* TS 03.03 Chapter 2.6 */
00194 int gprs_tlli_type(uint32_t tlli);
00195 
00196 uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type);
00197 
00198 /* Osmocom internal, not part of any gsm spec */
00199 enum gsm_phys_chan_config {
00200         GSM_PCHAN_NONE,
00201         GSM_PCHAN_CCCH,
00202         GSM_PCHAN_CCCH_SDCCH4,
00203         GSM_PCHAN_TCH_F,
00204         GSM_PCHAN_TCH_H,
00205         GSM_PCHAN_SDCCH8_SACCH8C,
00206         GSM_PCHAN_PDCH,         /* GPRS PDCH */
00207         GSM_PCHAN_TCH_F_PDCH,   /* TCH/F if used, PDCH otherwise */
00208         GSM_PCHAN_UNKNOWN,
00209         GSM_PCHAN_CCCH_SDCCH4_CBCH,
00210         GSM_PCHAN_SDCCH8_SACCH8C_CBCH,
00211         GSM_PCHAN_TCH_F_TCH_H_PDCH,
00212         _GSM_PCHAN_MAX
00213 };
00214 
00215 /* Osmocom internal, not part of any gsm spec */
00216 enum gsm_chan_t {
00217         GSM_LCHAN_NONE,
00218         GSM_LCHAN_SDCCH,
00219         GSM_LCHAN_TCH_F,
00220         GSM_LCHAN_TCH_H,
00221         GSM_LCHAN_UNKNOWN,
00222         GSM_LCHAN_CCCH,
00223         GSM_LCHAN_PDTCH,
00224         GSM_LCHAN_CBCH,
00225         _GSM_LCHAN_MAX
00226 };
00227 
00228 extern const struct value_string gsm_chan_t_names[];
00229 
00230 /* Deprectated functions */
00231 /* Limit encoding and decoding to use no more than this amount of buffer bytes */
00232 #define GSM_7BIT_LEGACY_MAX_BUFFER_SIZE  0x10000
00233 
00234 int gsm_7bit_decode(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n() instead");
00235 int gsm_7bit_decode_ussd(char *decoded, const uint8_t *user_data, uint8_t length) OSMO_DEPRECATED("Use gsm_7bit_decode_n_ussd() instead");
00236 int gsm_7bit_encode(uint8_t *result, const char *data) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");
00237 int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n_ussd() instead");
00238 int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets_written) OSMO_DEPRECATED("Use gsm_7bit_encode_n() instead");
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines