blob: c5bf2803f1b8f756a42bce0d9bcf48c4881bfa52 [file] [log] [blame]
Harald Welte37b61652017-10-16 18:46:03 +02001/*! \addtogroup gsm0808
2 * @{
3 * \file gsm0808_utils.h */
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02004/*
Harald Weltee08da972017-11-13 01:00:26 +09005 * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
Philipp Maier22401432017-03-24 17:59:26 +01006 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
Philipp Maier22401432017-03-24 17:59:26 +01009 *
10 * This program is free software; you can redistribute it and/or modify
Harald Weltee08da972017-11-13 01:00:26 +090011 * 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
Philipp Maier22401432017-03-24 17:59:26 +010013 * (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
Harald Weltee08da972017-11-13 01:00:26 +090018 * GNU General Public License for more details.
Philipp Maier22401432017-03-24 17:59:26 +010019 *
Harald Weltee08da972017-11-13 01:00:26 +090020 * You should have received a copy of the GNU General Public License
Philipp Maier22401432017-03-24 17:59:26 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24#pragma once
25
Harald Welte20725b92017-05-15 12:50:04 +020026struct sockaddr_storage;
Philipp Maier22401432017-03-24 17:59:26 +010027
Philipp Maier6f725d62017-03-24 18:03:17 +010028#include <osmocom/gsm/protocol/gsm_08_08.h>
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +020029#include <osmocom/gsm/protocol/gsm_04_08.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010030#include <osmocom/gsm/gsm23003.h>
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +020031#include <osmocom/gsm/gsm_utils.h>
Maxadc41722018-11-19 10:44:55 +010032#include <osmocom/gsm/tlv.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010033
34 /*! (225-1)/2 is the maximum number of elements in a cell identifier list. */
35#define GSM0808_CELL_ID_LIST2_MAXLEN 127
36
Neels Hofmeyr250e7f72018-04-13 03:30:14 +020037/*! Instead of this, use either struct gsm0808_cell_id or gsm0808_cell_id_list2.
38 * All elements contain parsed representations of the data in the corresponding IE, in host-byte order.
39 */
40union gsm0808_cell_id_u {
41 struct osmo_cell_global_id global;
42 struct osmo_lac_and_ci_id lac_and_ci;
43 uint16_t ci;
44 struct osmo_location_area_id lai_and_lac;
45 uint16_t lac;
46};
47
48/*! Parsed representation of Cell Identifier IE (3GPP TS 48.008 3.2.2.17) */
49struct gsm0808_cell_id {
50 enum CELL_IDENT id_discr;
51 union gsm0808_cell_id_u id;
52};
53
54/*! Parsed representation of a Cell Identifier List IE (3GPP TS 48.008 3.2.2.27). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010055struct gsm0808_cell_id_list2 {
56 enum CELL_IDENT id_discr;
Neels Hofmeyr250e7f72018-04-13 03:30:14 +020057 union gsm0808_cell_id_u id_list[GSM0808_CELL_ID_LIST2_MAXLEN];
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010058 unsigned int id_list_len;
59};
Philipp Maier6f725d62017-03-24 18:03:17 +010060
Max73878242018-11-09 12:35:03 +010061/*! LCLS-related parameters from 3GPP TS 48.008 */
62struct osmo_lcls {
63 enum gsm0808_lcls_config config; /* §3.2.2.116 Configuration */
64 enum gsm0808_lcls_control control; /* §3.2.2.117 Connection Status Control */
65 struct gsm0808_gcr *gcr; /* §3.2.2.115 Global Call Reference */
66 bool corr_needed; /* §3.2.2.118 Correlation-Not-Needed */
67};
68
Neels Hofmeyra4399c82018-04-17 02:26:10 +020069extern const struct value_string gsm0808_cell_id_discr_names[];
70static inline const char *gsm0808_cell_id_discr_name(enum CELL_IDENT id_discr)
71{ return get_value_string(gsm0808_cell_id_discr_names, id_discr); }
72
73const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid);
74const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid);
75const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil);
76int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil);
77int gsm0808_cell_id_u_name(char *buf, size_t buflen,
78 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u);
79
Philipp Maier22401432017-03-24 17:59:26 +010080uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
81 const struct sockaddr_storage *ss);
Philipp Maier22401432017-03-24 17:59:26 +010082int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
83 const uint8_t *elem, uint8_t len);
Philipp Maier6f725d62017-03-24 18:03:17 +010084uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
85 const struct gsm0808_speech_codec *sc);
Philipp Maier6f725d62017-03-24 18:03:17 +010086int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
87 const uint8_t *elem, uint8_t len);
Philipp Maier6f725d62017-03-24 18:03:17 +010088uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
89 const struct gsm0808_speech_codec_list
90 *scl);
Philipp Maier6f725d62017-03-24 18:03:17 +010091int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
92 const uint8_t *elem, uint8_t len);
Philipp Maiere0c65302017-03-28 17:05:40 +020093uint8_t gsm0808_enc_channel_type(struct msgb *msg,
94 const struct gsm0808_channel_type *ct);
Philipp Maiere0c65302017-03-28 17:05:40 +020095int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
96 const uint8_t *elem, uint8_t len);
Philipp Maier14e76b92017-03-28 18:36:52 +020097uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
98 const struct gsm0808_encrypt_info *ei);
Philipp Maier14e76b92017-03-28 18:36:52 +020099int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
100 const uint8_t *elem, uint8_t len);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100101uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg, const struct gsm0808_cell_id_list2 *cil);
Philipp Maier783047e2017-03-29 11:35:50 +0200102uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100103 const struct gsm0808_cell_id_list *cil)
104 OSMO_DEPRECATED("use gsm0808_enc_cell_id_list2 instead");
105int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil, const uint8_t *elem, uint8_t len);
Philipp Maier783047e2017-03-29 11:35:50 +0200106int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100107 const uint8_t *elem, uint8_t len)
108 OSMO_DEPRECATED("use gsm0808_dec_cell_id_list2 instead");
Neels Hofmeyr74663d92018-03-23 01:46:42 +0100109int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src);
Neels Hofmeyr38e58412018-05-25 16:56:35 +0200110void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src);
Neels Hofmeyr250e7f72018-04-13 03:30:14 +0200111uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci);
112int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len);
Neels Hofmeyr9a391e22017-06-23 02:34:08 +0200113int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch);
Philipp Maier884ba0f2017-06-02 13:49:16 +0200114int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
115 uint8_t perm_spch);
Philipp Maier369015c2018-09-21 09:07:20 +0200116uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg, bool fr);
Philipp Maier8515d032018-09-25 15:57:49 +0200117void gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg, uint16_t s15_s0);
Harald Welte37b61652017-10-16 18:46:03 +0200118
Maxaa934632018-11-07 13:16:54 +0100119/*! \returns 3GPP TS 08.08 §3.2.2.5 Class of a given Cause */
120static inline enum gsm0808_cause_class gsm0808_cause_class(enum gsm0808_cause cause)
121{
122 return (cause << 1) >> 4;
123}
124
125/*! \returns true if 3GPP TS 08.08 §3.2.2.5 Class has extended bit set */
126static inline bool gsm0808_cause_ext(enum gsm0808_cause cause)
127{
128 /* check that cause looks like 1XXX0000 where XXX represent class */
129 return (cause & 0x80) && !(cause & 0x0F);
130}
131
Maxed651d22018-11-07 15:25:05 +0100132int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp);
133
Maxaa934632018-11-07 13:16:54 +0100134/*! \returns 3GPP TS 48.008 3.2.2.49 Current Channel Type 1 from enum gsm_chan_t. */
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +0200135static inline uint8_t gsm0808_current_channel_type_1(enum gsm_chan_t type)
136{
137 switch (type) {
138 default:
139 return 0;
140 case GSM_LCHAN_SDCCH:
141 return 0x01;
142 case GSM_LCHAN_TCH_F:
143 return 0x18;
144 case GSM_LCHAN_TCH_H:
145 return 0x19;
146 }
147}
148
149/*! Return 3GPP TS 48.008 3.2.2.51 Speech Version aka permitted speech version indication in 3.2.2.11
150 * Channel Type. */
151static inline enum gsm0808_permitted_speech gsm0808_permitted_speech(enum gsm_chan_t type,
152 enum gsm48_chan_mode mode)
153{
154 switch (mode) {
155 case GSM48_CMODE_SPEECH_V1:
156 switch (type) {
157 case GSM_LCHAN_TCH_F:
158 return GSM0808_PERM_FR1;
159 case GSM_LCHAN_TCH_H:
160 return GSM0808_PERM_HR1;
161 default:
162 return 0;
163 }
164 case GSM48_CMODE_SPEECH_EFR:
165 switch (type) {
166 case GSM_LCHAN_TCH_F:
167 return GSM0808_PERM_FR2;
168 case GSM_LCHAN_TCH_H:
169 return GSM0808_PERM_HR2;
170 default:
171 return 0;
172 }
173 case GSM48_CMODE_SPEECH_AMR:
174 switch (type) {
175 case GSM_LCHAN_TCH_F:
Neels Hofmeyr94e42652018-06-07 16:54:11 +0200176 return GSM0808_PERM_FR3;
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +0200177 case GSM_LCHAN_TCH_H:
178 return GSM0808_PERM_HR3;
179 default:
180 return 0;
181 }
182 default:
183 return 0;
184 }
185}
186
187/*! Return 3GPP TS 48.008 3.2.2.33 Chosen Channel. */
188static inline uint8_t gsm0808_chosen_channel(enum gsm_chan_t type, enum gsm48_chan_mode mode)
189{
190 uint8_t channel_mode = 0, channel = 0;
191
192 switch (mode) {
193 case GSM48_CMODE_SPEECH_V1:
194 case GSM48_CMODE_SPEECH_EFR:
195 case GSM48_CMODE_SPEECH_AMR:
196 channel_mode = 0x9;
197 break;
198 case GSM48_CMODE_SIGN:
199 channel_mode = 0x8;
200 break;
201 case GSM48_CMODE_DATA_14k5:
202 channel_mode = 0xe;
203 break;
204 case GSM48_CMODE_DATA_12k0:
205 channel_mode = 0xb;
206 break;
207 case GSM48_CMODE_DATA_6k0:
208 channel_mode = 0xc;
209 break;
210 case GSM48_CMODE_DATA_3k6:
211 channel_mode = 0xd;
212 break;
213 default:
214 return 0;
215 }
216
217 switch (type) {
218 case GSM_LCHAN_NONE:
219 channel = 0x0;
220 break;
221 case GSM_LCHAN_SDCCH:
222 channel = 0x1;
223 break;
224 case GSM_LCHAN_TCH_F:
225 channel = 0x8;
226 break;
227 case GSM_LCHAN_TCH_H:
228 channel = 0x9;
229 break;
230 default:
231 return 0;
232 }
233
234 return channel_mode << 4 | channel;
235}
236
237const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct);
238
Harald Welte37b61652017-10-16 18:46:03 +0200239/*! @} */