blob: 24c139a87f0df51e2c5044197f56bb749bf46927 [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>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010032
33 /*! (225-1)/2 is the maximum number of elements in a cell identifier list. */
34#define GSM0808_CELL_ID_LIST2_MAXLEN 127
35
Neels Hofmeyr250e7f72018-04-13 03:30:14 +020036/*! Instead of this, use either struct gsm0808_cell_id or gsm0808_cell_id_list2.
37 * All elements contain parsed representations of the data in the corresponding IE, in host-byte order.
38 */
39union gsm0808_cell_id_u {
40 struct osmo_cell_global_id global;
41 struct osmo_lac_and_ci_id lac_and_ci;
42 uint16_t ci;
43 struct osmo_location_area_id lai_and_lac;
44 uint16_t lac;
45};
46
47/*! Parsed representation of Cell Identifier IE (3GPP TS 48.008 3.2.2.17) */
48struct gsm0808_cell_id {
49 enum CELL_IDENT id_discr;
50 union gsm0808_cell_id_u id;
51};
52
53/*! Parsed representation of a Cell Identifier List IE (3GPP TS 48.008 3.2.2.27). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010054struct gsm0808_cell_id_list2 {
55 enum CELL_IDENT id_discr;
Neels Hofmeyr250e7f72018-04-13 03:30:14 +020056 union gsm0808_cell_id_u id_list[GSM0808_CELL_ID_LIST2_MAXLEN];
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010057 unsigned int id_list_len;
58};
Philipp Maier6f725d62017-03-24 18:03:17 +010059
Neels Hofmeyra4399c82018-04-17 02:26:10 +020060extern const struct value_string gsm0808_cell_id_discr_names[];
61static inline const char *gsm0808_cell_id_discr_name(enum CELL_IDENT id_discr)
62{ return get_value_string(gsm0808_cell_id_discr_names, id_discr); }
63
64const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid);
65const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid);
66const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil);
67int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil);
68int gsm0808_cell_id_u_name(char *buf, size_t buflen,
69 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u);
70
Philipp Maier22401432017-03-24 17:59:26 +010071uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
72 const struct sockaddr_storage *ss);
Philipp Maier22401432017-03-24 17:59:26 +010073int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
74 const uint8_t *elem, uint8_t len);
Philipp Maier6f725d62017-03-24 18:03:17 +010075uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
76 const struct gsm0808_speech_codec *sc);
Philipp Maier6f725d62017-03-24 18:03:17 +010077int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
78 const uint8_t *elem, uint8_t len);
Philipp Maier6f725d62017-03-24 18:03:17 +010079uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
80 const struct gsm0808_speech_codec_list
81 *scl);
Philipp Maier6f725d62017-03-24 18:03:17 +010082int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
83 const uint8_t *elem, uint8_t len);
Philipp Maiere0c65302017-03-28 17:05:40 +020084uint8_t gsm0808_enc_channel_type(struct msgb *msg,
85 const struct gsm0808_channel_type *ct);
Philipp Maiere0c65302017-03-28 17:05:40 +020086int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
87 const uint8_t *elem, uint8_t len);
Philipp Maier14e76b92017-03-28 18:36:52 +020088uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
89 const struct gsm0808_encrypt_info *ei);
Philipp Maier14e76b92017-03-28 18:36:52 +020090int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
91 const uint8_t *elem, uint8_t len);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010092uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg, const struct gsm0808_cell_id_list2 *cil);
Philipp Maier783047e2017-03-29 11:35:50 +020093uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010094 const struct gsm0808_cell_id_list *cil)
95 OSMO_DEPRECATED("use gsm0808_enc_cell_id_list2 instead");
96int 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 +020097int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010098 const uint8_t *elem, uint8_t len)
99 OSMO_DEPRECATED("use gsm0808_dec_cell_id_list2 instead");
Neels Hofmeyr74663d92018-03-23 01:46:42 +0100100int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src);
Neels Hofmeyr250e7f72018-04-13 03:30:14 +0200101uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci);
102int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len);
Neels Hofmeyr9a391e22017-06-23 02:34:08 +0200103int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch);
Philipp Maier884ba0f2017-06-02 13:49:16 +0200104int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
105 uint8_t perm_spch);
Harald Welte37b61652017-10-16 18:46:03 +0200106
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +0200107/*! Return 3GPP TS 48.008 3.2.2.49 Current Channel Type 1 from enum gsm_chan_t. */
108static inline uint8_t gsm0808_current_channel_type_1(enum gsm_chan_t type)
109{
110 switch (type) {
111 default:
112 return 0;
113 case GSM_LCHAN_SDCCH:
114 return 0x01;
115 case GSM_LCHAN_TCH_F:
116 return 0x18;
117 case GSM_LCHAN_TCH_H:
118 return 0x19;
119 }
120}
121
122/*! Return 3GPP TS 48.008 3.2.2.51 Speech Version aka permitted speech version indication in 3.2.2.11
123 * Channel Type. */
124static inline enum gsm0808_permitted_speech gsm0808_permitted_speech(enum gsm_chan_t type,
125 enum gsm48_chan_mode mode)
126{
127 switch (mode) {
128 case GSM48_CMODE_SPEECH_V1:
129 switch (type) {
130 case GSM_LCHAN_TCH_F:
131 return GSM0808_PERM_FR1;
132 case GSM_LCHAN_TCH_H:
133 return GSM0808_PERM_HR1;
134 default:
135 return 0;
136 }
137 case GSM48_CMODE_SPEECH_EFR:
138 switch (type) {
139 case GSM_LCHAN_TCH_F:
140 return GSM0808_PERM_FR2;
141 case GSM_LCHAN_TCH_H:
142 return GSM0808_PERM_HR2;
143 default:
144 return 0;
145 }
146 case GSM48_CMODE_SPEECH_AMR:
147 switch (type) {
148 case GSM_LCHAN_TCH_F:
149 return GSM0808_PERM_HR3;
150 case GSM_LCHAN_TCH_H:
151 return GSM0808_PERM_HR3;
152 default:
153 return 0;
154 }
155 default:
156 return 0;
157 }
158}
159
160/*! Return 3GPP TS 48.008 3.2.2.33 Chosen Channel. */
161static inline uint8_t gsm0808_chosen_channel(enum gsm_chan_t type, enum gsm48_chan_mode mode)
162{
163 uint8_t channel_mode = 0, channel = 0;
164
165 switch (mode) {
166 case GSM48_CMODE_SPEECH_V1:
167 case GSM48_CMODE_SPEECH_EFR:
168 case GSM48_CMODE_SPEECH_AMR:
169 channel_mode = 0x9;
170 break;
171 case GSM48_CMODE_SIGN:
172 channel_mode = 0x8;
173 break;
174 case GSM48_CMODE_DATA_14k5:
175 channel_mode = 0xe;
176 break;
177 case GSM48_CMODE_DATA_12k0:
178 channel_mode = 0xb;
179 break;
180 case GSM48_CMODE_DATA_6k0:
181 channel_mode = 0xc;
182 break;
183 case GSM48_CMODE_DATA_3k6:
184 channel_mode = 0xd;
185 break;
186 default:
187 return 0;
188 }
189
190 switch (type) {
191 case GSM_LCHAN_NONE:
192 channel = 0x0;
193 break;
194 case GSM_LCHAN_SDCCH:
195 channel = 0x1;
196 break;
197 case GSM_LCHAN_TCH_F:
198 channel = 0x8;
199 break;
200 case GSM_LCHAN_TCH_H:
201 channel = 0x9;
202 break;
203 default:
204 return 0;
205 }
206
207 return channel_mode << 4 | channel;
208}
209
210const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct);
211
Harald Welte37b61652017-10-16 18:46:03 +0200212/*! @} */