Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 1 | /*! \addtogroup gsm0808 |
| 2 | * @{ |
| 3 | * \file gsm0808_utils.h */ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 4 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 11 | * 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 Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 13 | * (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 Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 18 | * GNU General Public License for more details. |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 19 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | #pragma once |
| 25 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 26 | struct sockaddr_storage; |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 27 | |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 28 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm23003.h> |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 31 | #include <osmocom/gsm/gsm_utils.h> |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 32 | |
| 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 Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 36 | /*! 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 | */ |
| 39 | union 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) */ |
| 48 | struct 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 Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 54 | struct gsm0808_cell_id_list2 { |
| 55 | enum CELL_IDENT id_discr; |
Neels Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 56 | union gsm0808_cell_id_u id_list[GSM0808_CELL_ID_LIST2_MAXLEN]; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 57 | unsigned int id_list_len; |
| 58 | }; |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 59 | |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 60 | extern const struct value_string gsm0808_cell_id_discr_names[]; |
| 61 | static 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 | |
| 64 | const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid); |
| 65 | const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid); |
| 66 | const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil); |
| 67 | int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil); |
| 68 | int 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 Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 71 | uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg, |
| 72 | const struct sockaddr_storage *ss); |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 73 | int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss, |
| 74 | const uint8_t *elem, uint8_t len); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 75 | uint8_t gsm0808_enc_speech_codec(struct msgb *msg, |
| 76 | const struct gsm0808_speech_codec *sc); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 77 | int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc, |
| 78 | const uint8_t *elem, uint8_t len); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 79 | uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg, |
| 80 | const struct gsm0808_speech_codec_list |
| 81 | *scl); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 82 | int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl, |
| 83 | const uint8_t *elem, uint8_t len); |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 84 | uint8_t gsm0808_enc_channel_type(struct msgb *msg, |
| 85 | const struct gsm0808_channel_type *ct); |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 86 | int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct, |
| 87 | const uint8_t *elem, uint8_t len); |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 88 | uint8_t gsm0808_enc_encrypt_info(struct msgb *msg, |
| 89 | const struct gsm0808_encrypt_info *ei); |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 90 | int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei, |
| 91 | const uint8_t *elem, uint8_t len); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 92 | uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg, const struct gsm0808_cell_id_list2 *cil); |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 93 | uint8_t gsm0808_enc_cell_id_list(struct msgb *msg, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 94 | const struct gsm0808_cell_id_list *cil) |
| 95 | OSMO_DEPRECATED("use gsm0808_enc_cell_id_list2 instead"); |
| 96 | int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil, const uint8_t *elem, uint8_t len); |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 97 | int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 98 | const uint8_t *elem, uint8_t len) |
| 99 | OSMO_DEPRECATED("use gsm0808_dec_cell_id_list2 instead"); |
Neels Hofmeyr | 74663d9 | 2018-03-23 01:46:42 +0100 | [diff] [blame] | 100 | int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src); |
Neels Hofmeyr | 38e5841 | 2018-05-25 16:56:35 +0200 | [diff] [blame] | 101 | void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src); |
Neels Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 102 | uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci); |
| 103 | int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len); |
Neels Hofmeyr | 9a391e2 | 2017-06-23 02:34:08 +0200 | [diff] [blame] | 104 | int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch); |
Philipp Maier | 884ba0f | 2017-06-02 13:49:16 +0200 | [diff] [blame] | 105 | int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc, |
| 106 | uint8_t perm_spch); |
Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 107 | |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 108 | /*! Return 3GPP TS 48.008 3.2.2.49 Current Channel Type 1 from enum gsm_chan_t. */ |
| 109 | static inline uint8_t gsm0808_current_channel_type_1(enum gsm_chan_t type) |
| 110 | { |
| 111 | switch (type) { |
| 112 | default: |
| 113 | return 0; |
| 114 | case GSM_LCHAN_SDCCH: |
| 115 | return 0x01; |
| 116 | case GSM_LCHAN_TCH_F: |
| 117 | return 0x18; |
| 118 | case GSM_LCHAN_TCH_H: |
| 119 | return 0x19; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /*! Return 3GPP TS 48.008 3.2.2.51 Speech Version aka permitted speech version indication in 3.2.2.11 |
| 124 | * Channel Type. */ |
| 125 | static inline enum gsm0808_permitted_speech gsm0808_permitted_speech(enum gsm_chan_t type, |
| 126 | enum gsm48_chan_mode mode) |
| 127 | { |
| 128 | switch (mode) { |
| 129 | case GSM48_CMODE_SPEECH_V1: |
| 130 | switch (type) { |
| 131 | case GSM_LCHAN_TCH_F: |
| 132 | return GSM0808_PERM_FR1; |
| 133 | case GSM_LCHAN_TCH_H: |
| 134 | return GSM0808_PERM_HR1; |
| 135 | default: |
| 136 | return 0; |
| 137 | } |
| 138 | case GSM48_CMODE_SPEECH_EFR: |
| 139 | switch (type) { |
| 140 | case GSM_LCHAN_TCH_F: |
| 141 | return GSM0808_PERM_FR2; |
| 142 | case GSM_LCHAN_TCH_H: |
| 143 | return GSM0808_PERM_HR2; |
| 144 | default: |
| 145 | return 0; |
| 146 | } |
| 147 | case GSM48_CMODE_SPEECH_AMR: |
| 148 | switch (type) { |
| 149 | case GSM_LCHAN_TCH_F: |
| 150 | return GSM0808_PERM_HR3; |
| 151 | case GSM_LCHAN_TCH_H: |
| 152 | return GSM0808_PERM_HR3; |
| 153 | default: |
| 154 | return 0; |
| 155 | } |
| 156 | default: |
| 157 | return 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /*! Return 3GPP TS 48.008 3.2.2.33 Chosen Channel. */ |
| 162 | static inline uint8_t gsm0808_chosen_channel(enum gsm_chan_t type, enum gsm48_chan_mode mode) |
| 163 | { |
| 164 | uint8_t channel_mode = 0, channel = 0; |
| 165 | |
| 166 | switch (mode) { |
| 167 | case GSM48_CMODE_SPEECH_V1: |
| 168 | case GSM48_CMODE_SPEECH_EFR: |
| 169 | case GSM48_CMODE_SPEECH_AMR: |
| 170 | channel_mode = 0x9; |
| 171 | break; |
| 172 | case GSM48_CMODE_SIGN: |
| 173 | channel_mode = 0x8; |
| 174 | break; |
| 175 | case GSM48_CMODE_DATA_14k5: |
| 176 | channel_mode = 0xe; |
| 177 | break; |
| 178 | case GSM48_CMODE_DATA_12k0: |
| 179 | channel_mode = 0xb; |
| 180 | break; |
| 181 | case GSM48_CMODE_DATA_6k0: |
| 182 | channel_mode = 0xc; |
| 183 | break; |
| 184 | case GSM48_CMODE_DATA_3k6: |
| 185 | channel_mode = 0xd; |
| 186 | break; |
| 187 | default: |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | switch (type) { |
| 192 | case GSM_LCHAN_NONE: |
| 193 | channel = 0x0; |
| 194 | break; |
| 195 | case GSM_LCHAN_SDCCH: |
| 196 | channel = 0x1; |
| 197 | break; |
| 198 | case GSM_LCHAN_TCH_F: |
| 199 | channel = 0x8; |
| 200 | break; |
| 201 | case GSM_LCHAN_TCH_H: |
| 202 | channel = 0x9; |
| 203 | break; |
| 204 | default: |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | return channel_mode << 4 | channel; |
| 209 | } |
| 210 | |
| 211 | const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct); |
| 212 | |
Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 213 | /*! @} */ |