Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2013 by Andreas Eversberg <jolly@eversberg.eu> |
| 3 | * (C) 2015 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co> |
| 4 | * (C) 2016 by Tom Tsou <tom.tsou@ettus.com> |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 5 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 26 | #include <errno.h> |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 27 | |
| 28 | #include <osmocom/core/bits.h> |
| 29 | #include <osmocom/core/conv.h> |
| 30 | #include <osmocom/core/utils.h> |
| 31 | #include <osmocom/core/crcgen.h> |
| 32 | #include <osmocom/core/endian.h> |
| 33 | |
| 34 | #include <osmocom/gprs/protocol/gsm_04_60.h> |
| 35 | #include <osmocom/gprs/gprs_rlc.h> |
| 36 | |
| 37 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 38 | #include <osmocom/gsm/gsm0503.h> |
| 39 | #include <osmocom/codec/codec.h> |
| 40 | |
| 41 | #include <osmocom/coding/gsm0503_interleaving.h> |
| 42 | #include <osmocom/coding/gsm0503_mapping.h> |
| 43 | #include <osmocom/coding/gsm0503_tables.h> |
| 44 | #include <osmocom/coding/gsm0503_coding.h> |
| 45 | #include <osmocom/coding/gsm0503_parity.h> |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 46 | #include <osmocom/coding/gsm0503_amr_dtx.h> |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 47 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 48 | /*! \mainpage libosmocoding Documentation |
| 49 | * |
| 50 | * \section sec_intro Introduction |
| 51 | * This library is a collection of definitions, tables and functions |
| 52 | * implementing the GSM/GPRS/EGPRS channel coding (and decoding) as |
| 53 | * specified in 3GPP TS 05.03 / 45.003. |
| 54 | * |
Vadim Yanitskiy | 9a232fd | 2018-01-19 03:05:32 +0600 | [diff] [blame] | 55 | * libosmocoding is developed as part of the Osmocom (Open Source Mobile |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 56 | * Communications) project, a community-based, collaborative development |
| 57 | * project to create Free and Open Source implementations of mobile |
| 58 | * communications systems. For more information about Osmocom, please |
| 59 | * see https://osmocom.org/ |
| 60 | * |
| 61 | * \section sec_copyright Copyright and License |
| 62 | * Copyright © 2013 by Andreas Eversberg\n |
| 63 | * Copyright © 2015 by Alexander Chemeris\n |
| 64 | * Copyright © 2016 by Tom Tsou\n |
| 65 | * Documentation Copyright © 2017 by Harald Welte\n |
| 66 | * All rights reserved. \n\n |
| 67 | * The source code of libosmocoding is licensed under the terms of the GNU |
| 68 | * General Public License as published by the Free Software Foundation; |
| 69 | * either version 2 of the License, or (at your option) any later |
| 70 | * version.\n |
| 71 | * See <http://www.gnu.org/licenses/> or COPYING included in the source |
| 72 | * code package istelf.\n |
| 73 | * The information detailed here is provided AS IS with NO WARRANTY OF |
| 74 | * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND |
| 75 | * FITNESS FOR A PARTICULAR PURPOSE. |
| 76 | * \n\n |
| 77 | * |
| 78 | * \section sec_tracker Homepage + Issue Tracker |
| 79 | * libosmocoding is distributed as part of libosmocore and shares its |
| 80 | * project page at http://osmocom.org/projects/libosmocore |
| 81 | * |
| 82 | * An Issue Tracker can be found at |
| 83 | * https://osmocom.org/projects/libosmocore/issues |
| 84 | * |
| 85 | * \section sec_contact Contact and Support |
| 86 | * Community-based support is available at the OpenBSC mailing list |
| 87 | * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n |
| 88 | * Commercial support options available upon request from |
| 89 | * <http://sysmocom.de/> |
| 90 | */ |
| 91 | |
| 92 | |
| 93 | /*! \addtogroup coding |
| 94 | * @{ |
| 95 | * |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 96 | * GSM TS 05.03 coding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 97 | * |
| 98 | * This module is the "master module" of libosmocoding. It uses the |
| 99 | * various other modules (mapping, parity, interleaving) in order to |
| 100 | * implement the complete channel coding (and decoding) chain for the |
| 101 | * various channel types as defined in TS 05.03 / 45.003. |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 102 | * |
| 103 | * \file gsm0503_coding.c */ |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 104 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 105 | /* |
| 106 | * EGPRS coding limits |
| 107 | */ |
| 108 | |
| 109 | /* Max header size with parity bits */ |
| 110 | #define EGPRS_HDR_UPP_MAX 54 |
| 111 | |
| 112 | /* Max encoded header size */ |
| 113 | #define EGPRS_HDR_C_MAX 162 |
| 114 | |
| 115 | /* Max punctured header size */ |
| 116 | #define EGPRS_HDR_HC_MAX 160 |
| 117 | |
| 118 | /* Max data block size with parity bits */ |
| 119 | #define EGPRS_DATA_U_MAX 612 |
| 120 | |
| 121 | /* Max encoded data block size */ |
| 122 | #define EGPRS_DATA_C_MAX 1836 |
| 123 | |
| 124 | /* Max single block punctured data size */ |
| 125 | #define EGPRS_DATA_DC_MAX 1248 |
| 126 | |
| 127 | /* Dual block punctured data size */ |
| 128 | #define EGPRS_DATA_C1 612 |
| 129 | #define EGPRS_DATA_C2 EGPRS_DATA_C1 |
| 130 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 131 | /*! union across the three different EGPRS Uplink header types */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 132 | union gprs_rlc_ul_hdr_egprs { |
| 133 | struct gprs_rlc_ul_header_egprs_1 type1; |
| 134 | struct gprs_rlc_ul_header_egprs_2 type2; |
| 135 | struct gprs_rlc_ul_header_egprs_3 type3; |
| 136 | }; |
| 137 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 138 | /*! union across the three different EGPRS Downlink header types */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 139 | union gprs_rlc_dl_hdr_egprs { |
| 140 | struct gprs_rlc_dl_header_egprs_1 type1; |
| 141 | struct gprs_rlc_dl_header_egprs_2 type2; |
| 142 | struct gprs_rlc_dl_header_egprs_3 type3; |
| 143 | }; |
| 144 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 145 | /*! Structure describing a Modulation and Coding Scheme */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 146 | struct gsm0503_mcs_code { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 147 | /*! Modulation and Coding Scheme (MSC) number */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 148 | uint8_t mcs; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 149 | /*! Length of Uplink Stealing Flag (USF) in bits */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 150 | uint8_t usf_len; |
| 151 | |
| 152 | /* Header coding */ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 153 | /*! Length of header (bits) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 154 | uint8_t hdr_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 155 | /*! Length of header convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 156 | uint8_t hdr_code_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 157 | /*! Length of header code puncturing sequence */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 158 | uint8_t hdr_punc_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 159 | /*! header convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 160 | const struct osmo_conv_code *hdr_conv; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 161 | /*! header puncturing sequence */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 162 | const uint8_t *hdr_punc; |
| 163 | |
| 164 | /* Data coding */ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 165 | /*! length of data (bits) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 166 | uint16_t data_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 167 | /*! length of data convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 168 | uint16_t data_code_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 169 | /*! length of data code puncturing sequence */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 170 | uint16_t data_punc_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 171 | /*! data convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 172 | const struct osmo_conv_code *data_conv; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 173 | /*! data puncturing sequences */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 174 | const uint8_t *data_punc[3]; |
| 175 | }; |
| 176 | |
| 177 | /* |
| 178 | * EGPRS UL coding parameters |
| 179 | */ |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 180 | const struct gsm0503_mcs_code gsm0503_mcs_ul_codes[EGPRS_NUM_MCS] = { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 181 | { |
| 182 | .mcs = EGPRS_MCS0, |
| 183 | }, |
| 184 | { |
| 185 | .mcs = EGPRS_MCS1, |
| 186 | .hdr_len = 31, |
| 187 | .hdr_code_len = 117, |
| 188 | .hdr_punc_len = 80, |
| 189 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 190 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 191 | |
| 192 | .data_len = 178, |
| 193 | .data_code_len = 588, |
| 194 | .data_punc_len = 372, |
| 195 | .data_conv = &gsm0503_mcs1, |
| 196 | .data_punc = { |
| 197 | gsm0503_puncture_mcs1_p1, |
| 198 | gsm0503_puncture_mcs1_p2, |
| 199 | NULL, |
| 200 | }, |
| 201 | }, |
| 202 | { |
| 203 | .mcs = EGPRS_MCS2, |
| 204 | .hdr_len = 31, |
| 205 | .hdr_code_len = 117, |
| 206 | .hdr_punc_len = 80, |
| 207 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 208 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 209 | |
| 210 | .data_len = 226, |
| 211 | .data_code_len = 732, |
| 212 | .data_punc_len = 372, |
| 213 | .data_conv = &gsm0503_mcs2, |
| 214 | .data_punc = { |
| 215 | gsm0503_puncture_mcs2_p1, |
| 216 | gsm0503_puncture_mcs2_p2, |
| 217 | NULL, |
| 218 | }, |
| 219 | }, |
| 220 | { |
| 221 | .mcs = EGPRS_MCS3, |
| 222 | .hdr_len = 31, |
| 223 | .hdr_code_len = 117, |
| 224 | .hdr_punc_len = 80, |
| 225 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 226 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 227 | |
| 228 | .data_len = 298, |
| 229 | .data_code_len = 948, |
| 230 | .data_punc_len = 372, |
| 231 | .data_conv = &gsm0503_mcs3, |
| 232 | .data_punc = { |
| 233 | gsm0503_puncture_mcs3_p1, |
| 234 | gsm0503_puncture_mcs3_p2, |
| 235 | gsm0503_puncture_mcs3_p3, |
| 236 | }, |
| 237 | }, |
| 238 | { |
| 239 | .mcs = EGPRS_MCS4, |
| 240 | .hdr_len = 31, |
| 241 | .hdr_code_len = 117, |
| 242 | .hdr_punc_len = 80, |
| 243 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 244 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 245 | |
| 246 | .data_len = 354, |
| 247 | .data_code_len = 1116, |
| 248 | .data_punc_len = 372, |
| 249 | .data_conv = &gsm0503_mcs4, |
| 250 | .data_punc = { |
| 251 | gsm0503_puncture_mcs4_p1, |
| 252 | gsm0503_puncture_mcs4_p2, |
| 253 | gsm0503_puncture_mcs4_p3, |
| 254 | }, |
| 255 | }, |
| 256 | { |
| 257 | .mcs = EGPRS_MCS5, |
| 258 | .hdr_len = 37, |
| 259 | .hdr_code_len = 135, |
| 260 | .hdr_punc_len = 136, |
| 261 | .hdr_conv = &gsm0503_mcs5_ul_hdr, |
| 262 | .hdr_punc = NULL, |
| 263 | |
| 264 | .data_len = 450, |
| 265 | .data_code_len = 1404, |
| 266 | .data_punc_len = 1248, |
| 267 | .data_conv = &gsm0503_mcs5, |
| 268 | .data_punc = { |
| 269 | gsm0503_puncture_mcs5_p1, |
| 270 | gsm0503_puncture_mcs5_p2, |
| 271 | NULL, |
| 272 | }, |
| 273 | }, |
| 274 | { |
| 275 | .mcs = EGPRS_MCS6, |
| 276 | .hdr_len = 37, |
| 277 | .hdr_code_len = 135, |
| 278 | .hdr_punc_len = 136, |
| 279 | .hdr_conv = &gsm0503_mcs5_ul_hdr, |
| 280 | .hdr_punc = NULL, |
| 281 | |
| 282 | .data_len = 594, |
| 283 | .data_code_len = 1836, |
| 284 | .data_punc_len = 1248, |
| 285 | .data_conv = &gsm0503_mcs6, |
| 286 | .data_punc = { |
| 287 | gsm0503_puncture_mcs6_p1, |
| 288 | gsm0503_puncture_mcs6_p2, |
| 289 | NULL, |
| 290 | }, |
| 291 | }, |
| 292 | { |
| 293 | .mcs = EGPRS_MCS7, |
| 294 | .hdr_len = 46, |
| 295 | .hdr_code_len = 162, |
| 296 | .hdr_punc_len = 160, |
| 297 | .hdr_conv = &gsm0503_mcs7_ul_hdr, |
| 298 | .hdr_punc = gsm0503_puncture_mcs7_ul_hdr, |
| 299 | |
| 300 | .data_len = 900, |
| 301 | .data_code_len = 1404, |
| 302 | .data_punc_len = 612, |
| 303 | .data_conv = &gsm0503_mcs7, |
| 304 | .data_punc = { |
| 305 | gsm0503_puncture_mcs7_p1, |
| 306 | gsm0503_puncture_mcs7_p2, |
| 307 | gsm0503_puncture_mcs7_p3, |
| 308 | } |
| 309 | }, |
| 310 | { |
| 311 | .mcs = EGPRS_MCS8, |
| 312 | .hdr_len = 46, |
| 313 | .hdr_code_len = 162, |
| 314 | .hdr_punc_len = 160, |
| 315 | .hdr_conv = &gsm0503_mcs7_ul_hdr, |
| 316 | .hdr_punc = gsm0503_puncture_mcs7_ul_hdr, |
| 317 | |
| 318 | .data_len = 1092, |
| 319 | .data_code_len = 1692, |
| 320 | .data_punc_len = 612, |
| 321 | .data_conv = &gsm0503_mcs8, |
| 322 | .data_punc = { |
| 323 | gsm0503_puncture_mcs8_p1, |
| 324 | gsm0503_puncture_mcs8_p2, |
| 325 | gsm0503_puncture_mcs8_p3, |
| 326 | } |
| 327 | }, |
| 328 | { |
| 329 | .mcs = EGPRS_MCS9, |
| 330 | .hdr_len = 46, |
| 331 | .hdr_code_len = 162, |
| 332 | .hdr_punc_len = 160, |
| 333 | .hdr_conv = &gsm0503_mcs7_ul_hdr, |
| 334 | .hdr_punc = gsm0503_puncture_mcs7_ul_hdr, |
| 335 | |
| 336 | .data_len = 1188, |
| 337 | .data_code_len = 1836, |
| 338 | .data_punc_len = 612, |
| 339 | .data_conv = &gsm0503_mcs9, |
| 340 | .data_punc = { |
| 341 | gsm0503_puncture_mcs9_p1, |
| 342 | gsm0503_puncture_mcs9_p2, |
| 343 | gsm0503_puncture_mcs9_p3, |
| 344 | } |
| 345 | }, |
| 346 | }; |
| 347 | |
| 348 | /* |
| 349 | * EGPRS DL coding parameters |
| 350 | */ |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 351 | const struct gsm0503_mcs_code gsm0503_mcs_dl_codes[EGPRS_NUM_MCS] = { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 352 | { |
| 353 | .mcs = EGPRS_MCS0, |
| 354 | }, |
| 355 | { |
| 356 | .mcs = EGPRS_MCS1, |
| 357 | .usf_len = 3, |
| 358 | .hdr_len = 28, |
| 359 | .hdr_code_len = 108, |
| 360 | .hdr_punc_len = 68, |
| 361 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 362 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 363 | |
| 364 | .data_len = 178, |
| 365 | .data_code_len = 588, |
| 366 | .data_punc_len = 372, |
| 367 | .data_conv = &gsm0503_mcs1, |
| 368 | .data_punc = { |
| 369 | gsm0503_puncture_mcs1_p1, |
| 370 | gsm0503_puncture_mcs1_p2, |
| 371 | NULL, |
| 372 | }, |
| 373 | }, |
| 374 | { |
| 375 | .mcs = EGPRS_MCS2, |
| 376 | .usf_len = 3, |
| 377 | .hdr_len = 28, |
| 378 | .hdr_code_len = 108, |
| 379 | .hdr_punc_len = 68, |
| 380 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 381 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 382 | |
| 383 | .data_len = 226, |
| 384 | .data_code_len = 732, |
| 385 | .data_punc_len = 372, |
| 386 | .data_conv = &gsm0503_mcs2, |
| 387 | .data_punc = { |
| 388 | gsm0503_puncture_mcs2_p1, |
| 389 | gsm0503_puncture_mcs2_p2, |
| 390 | NULL, |
| 391 | }, |
| 392 | }, |
| 393 | { |
| 394 | .mcs = EGPRS_MCS3, |
| 395 | .usf_len = 3, |
| 396 | .hdr_len = 28, |
| 397 | .hdr_code_len = 108, |
| 398 | .hdr_punc_len = 68, |
| 399 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 400 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 401 | |
| 402 | .data_len = 298, |
| 403 | .data_code_len = 948, |
| 404 | .data_punc_len = 372, |
| 405 | .data_conv = &gsm0503_mcs3, |
| 406 | .data_punc = { |
| 407 | gsm0503_puncture_mcs3_p1, |
| 408 | gsm0503_puncture_mcs3_p2, |
| 409 | gsm0503_puncture_mcs3_p3, |
| 410 | }, |
| 411 | }, |
| 412 | { |
| 413 | .mcs = EGPRS_MCS4, |
| 414 | .usf_len = 3, |
| 415 | .hdr_len = 28, |
| 416 | .hdr_code_len = 108, |
| 417 | .hdr_punc_len = 68, |
| 418 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 419 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 420 | |
| 421 | .data_len = 354, |
| 422 | .data_code_len = 1116, |
| 423 | .data_punc_len = 372, |
| 424 | .data_conv = &gsm0503_mcs4, |
| 425 | .data_punc = { |
| 426 | gsm0503_puncture_mcs4_p1, |
| 427 | gsm0503_puncture_mcs4_p2, |
| 428 | gsm0503_puncture_mcs4_p3, |
| 429 | }, |
| 430 | }, |
| 431 | { |
| 432 | .mcs = EGPRS_MCS5, |
| 433 | .usf_len = 3, |
| 434 | .hdr_len = 25, |
| 435 | .hdr_code_len = 99, |
| 436 | .hdr_punc_len = 100, |
| 437 | .hdr_conv = &gsm0503_mcs5_dl_hdr, |
| 438 | .hdr_punc = NULL, |
| 439 | |
| 440 | .data_len = 450, |
| 441 | .data_code_len = 1404, |
| 442 | .data_punc_len = 1248, |
| 443 | .data_conv = &gsm0503_mcs5, |
| 444 | .data_punc = { |
| 445 | gsm0503_puncture_mcs5_p1, |
| 446 | gsm0503_puncture_mcs5_p2, |
| 447 | NULL, |
| 448 | }, |
| 449 | }, |
| 450 | { |
| 451 | .mcs = EGPRS_MCS6, |
| 452 | .usf_len = 3, |
| 453 | .hdr_len = 25, |
| 454 | .hdr_code_len = 99, |
| 455 | .hdr_punc_len = 100, |
| 456 | .hdr_conv = &gsm0503_mcs5_dl_hdr, |
| 457 | .hdr_punc = NULL, |
| 458 | |
| 459 | .data_len = 594, |
| 460 | .data_code_len = 1836, |
| 461 | .data_punc_len = 1248, |
| 462 | .data_conv = &gsm0503_mcs6, |
| 463 | .data_punc = { |
| 464 | gsm0503_puncture_mcs6_p1, |
| 465 | gsm0503_puncture_mcs6_p2, |
| 466 | NULL, |
| 467 | }, |
| 468 | }, |
| 469 | { |
| 470 | .mcs = EGPRS_MCS7, |
| 471 | .usf_len = 3, |
| 472 | .hdr_len = 37, |
| 473 | .hdr_code_len = 135, |
| 474 | .hdr_punc_len = 124, |
| 475 | .hdr_conv = &gsm0503_mcs7_dl_hdr, |
| 476 | .hdr_punc = gsm0503_puncture_mcs7_dl_hdr, |
| 477 | |
| 478 | .data_len = 900, |
| 479 | .data_code_len = 1404, |
| 480 | .data_punc_len = 612, |
| 481 | .data_conv = &gsm0503_mcs7, |
| 482 | .data_punc = { |
| 483 | gsm0503_puncture_mcs7_p1, |
| 484 | gsm0503_puncture_mcs7_p2, |
| 485 | gsm0503_puncture_mcs7_p3, |
| 486 | } |
| 487 | }, |
| 488 | { |
| 489 | .mcs = EGPRS_MCS8, |
| 490 | .usf_len = 3, |
| 491 | .hdr_len = 37, |
| 492 | .hdr_code_len = 135, |
| 493 | .hdr_punc_len = 124, |
| 494 | .hdr_conv = &gsm0503_mcs7_dl_hdr, |
| 495 | .hdr_punc = gsm0503_puncture_mcs7_dl_hdr, |
| 496 | |
| 497 | .data_len = 1092, |
| 498 | .data_code_len = 1692, |
| 499 | .data_punc_len = 612, |
| 500 | .data_conv = &gsm0503_mcs8, |
| 501 | .data_punc = { |
| 502 | gsm0503_puncture_mcs8_p1, |
| 503 | gsm0503_puncture_mcs8_p2, |
| 504 | gsm0503_puncture_mcs8_p3, |
| 505 | } |
| 506 | }, |
| 507 | { |
| 508 | .mcs = EGPRS_MCS9, |
| 509 | .usf_len = 3, |
| 510 | .hdr_len = 37, |
| 511 | .hdr_code_len = 135, |
| 512 | .hdr_punc_len = 124, |
| 513 | .hdr_conv = &gsm0503_mcs7_dl_hdr, |
| 514 | .hdr_punc = gsm0503_puncture_mcs7_dl_hdr, |
| 515 | |
| 516 | .data_len = 1188, |
| 517 | .data_code_len = 1836, |
| 518 | .data_punc_len = 612, |
| 519 | .data_conv = &gsm0503_mcs9, |
| 520 | .data_punc = { |
| 521 | gsm0503_puncture_mcs9_p1, |
| 522 | gsm0503_puncture_mcs9_p2, |
| 523 | gsm0503_puncture_mcs9_p3, |
| 524 | } |
| 525 | }, |
| 526 | }; |
| 527 | |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 528 | /*! Convolutional Decode + compute BER for punctured codes |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 529 | * \param[in] code Description of Convolutional Code |
| 530 | * \param[in] input Input soft-bits (-127...127) |
| 531 | * \param[out] output bits |
| 532 | * \param[out] n_errors Number of bit-errors |
| 533 | * \param[out] n_bits_total Number of bits |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 534 | * \param[in] data_punc Puncturing mask array. Can be NULL. |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 535 | */ |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 536 | static int osmo_conv_decode_ber_punctured(const struct osmo_conv_code *code, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 537 | const sbit_t *input, ubit_t *output, |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 538 | int *n_errors, int *n_bits_total, |
| 539 | const uint8_t *data_punc) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 540 | { |
| 541 | int res, i, coded_len; |
| 542 | ubit_t recoded[EGPRS_DATA_C_MAX]; |
| 543 | |
| 544 | res = osmo_conv_decode(code, input, output); |
| 545 | |
Pau Espin Pedrol | d2737e6 | 2022-05-13 13:07:21 +0200 | [diff] [blame] | 546 | if (!n_bits_total && !n_errors) |
| 547 | return res; |
| 548 | |
| 549 | coded_len = osmo_conv_encode(code, output, recoded); |
Pau Espin Pedrol | 6c58d15 | 2022-05-13 14:03:27 +0200 | [diff] [blame] | 550 | OSMO_ASSERT(ARRAY_SIZE(recoded) >= coded_len); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 551 | |
| 552 | /* Count bit errors */ |
| 553 | if (n_errors) { |
| 554 | *n_errors = 0; |
| 555 | for (i = 0; i < coded_len; i++) { |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 556 | if (((!data_punc) || (data_punc && !data_punc[i])) && |
| 557 | !((recoded[i] && input[i] < 0) || |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 558 | (!recoded[i] && input[i] > 0)) ) |
| 559 | *n_errors += 1; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (n_bits_total) |
| 564 | *n_bits_total = coded_len; |
| 565 | |
| 566 | return res; |
| 567 | } |
| 568 | |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 569 | /*! Convolutional Decode + compute BER for non-punctured codes |
| 570 | * \param[in] code Description of Convolutional Code |
| 571 | * \param[in] input Input soft-bits (-127...127) |
| 572 | * \param[out] output bits |
| 573 | * \param[out] n_errors Number of bit-errors |
| 574 | * \param[out] n_bits_total Number of bits |
| 575 | */ |
| 576 | static int osmo_conv_decode_ber(const struct osmo_conv_code *code, |
| 577 | const sbit_t *input, ubit_t *output, |
| 578 | int *n_errors, int *n_bits_total) |
| 579 | { |
| 580 | return osmo_conv_decode_ber_punctured(code, input, output, |
| 581 | n_errors, n_bits_total, NULL); |
| 582 | } |
| 583 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 584 | /*! convenience wrapper for decoding coded bits |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 585 | * \param[out] l2_data caller-allocated buffer for L2 Frame |
| 586 | * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3 |
| 587 | * \param[out] n_errors Number of detected errors |
| 588 | * \param[out] n_bits_total Number of total coded bits |
| 589 | * \returns 0 on success; -1 on CRC error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 590 | static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 591 | int *n_errors, int *n_bits_total) |
| 592 | { |
| 593 | ubit_t conv[224]; |
| 594 | int rv; |
| 595 | |
| 596 | osmo_conv_decode_ber(&gsm0503_xcch, cB, |
| 597 | conv, n_errors, n_bits_total); |
| 598 | |
| 599 | rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40, |
| 600 | conv, 184, conv + 184); |
| 601 | if (rv) |
| 602 | return -1; |
| 603 | |
| 604 | osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 609 | /*! convenience wrapper for encoding to coded bits |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 610 | * \param[out] cB caller-allocated buffer for 456 coded bits as per TS 05.03 4.1.3 |
| 611 | * \param[out] l2_data to-be-encoded L2 Frame |
| 612 | * \returns 0 */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 613 | static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 614 | { |
| 615 | ubit_t conv[224]; |
| 616 | |
| 617 | osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1); |
| 618 | |
| 619 | osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184); |
| 620 | |
| 621 | osmo_conv_encode(&gsm0503_xcch, conv, cB); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * GSM xCCH block transcoding |
| 628 | */ |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 629 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 630 | /*! Decoding of xCCH data from bursts to L2 frame |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 631 | * \param[out] l2_data caller-allocated output data buffer |
| 632 | * \param[in] bursts four GSM bursts in soft-bits |
| 633 | * \param[out] n_errors Number of detected errors |
| 634 | * \param[out] n_bits_total Number of total coded bits |
| 635 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 636 | int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 637 | int *n_errors, int *n_bits_total) |
| 638 | { |
| 639 | sbit_t iB[456], cB[456]; |
| 640 | int i; |
| 641 | |
| 642 | for (i = 0; i < 4; i++) |
| 643 | gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL); |
| 644 | |
| 645 | gsm0503_xcch_deinterleave(cB, iB); |
| 646 | |
| 647 | return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total); |
| 648 | } |
| 649 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 650 | /*! Encoding of xCCH data from L2 frame to bursts |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 651 | * \param[out] bursts caller-allocated burst data (unpacked bits) |
| 652 | * \param[in] l2_data L2 input data (MAC block) |
| 653 | * \returns 0 |
| 654 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 655 | int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 656 | { |
| 657 | ubit_t iB[456], cB[456], hl = 1, hn = 1; |
| 658 | int i; |
| 659 | |
| 660 | _xcch_encode_cB(cB, l2_data); |
| 661 | |
| 662 | gsm0503_xcch_interleave(cB, iB); |
| 663 | |
| 664 | for (i = 0; i < 4; i++) |
| 665 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * EGPRS PDTCH UL block decoding |
| 672 | */ |
| 673 | |
| 674 | /* |
| 675 | * Type 3 - MCS-1,2,3,4 |
| 676 | * Unmapping and deinterleaving |
| 677 | */ |
| 678 | static int egprs_type3_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc) |
| 679 | { |
| 680 | int i; |
| 681 | sbit_t iB[456], q[8]; |
| 682 | |
| 683 | for (i = 0; i < 4; i++) { |
| 684 | gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], |
| 685 | q + i * 2, q + i * 2 + 1); |
| 686 | } |
| 687 | |
| 688 | gsm0503_mcs1_ul_deinterleave(hc, dc, iB); |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Type 2 - MCS-5,6 |
| 695 | * Unmapping and deinterleaving |
| 696 | */ |
| 697 | static int egprs_type2_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc) |
| 698 | { |
| 699 | int i; |
| 700 | sbit_t burst[348]; |
| 701 | sbit_t hi[EGPRS_HDR_HC_MAX]; |
| 702 | sbit_t di[EGPRS_DATA_DC_MAX]; |
| 703 | |
| 704 | for (i = 0; i < 4; i++) { |
| 705 | memcpy(burst, &bursts[i * 348], 348); |
| 706 | |
| 707 | gsm0503_mcs5_burst_swap(burst); |
| 708 | gsm0503_mcs5_ul_burst_unmap(di, burst, hi, i); |
| 709 | } |
| 710 | |
| 711 | gsm0503_mcs5_ul_deinterleave(hc, dc, hi, di); |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Type 1 - MCS-7,8,9 |
| 718 | * Unmapping and deinterleaving - Note that MCS-7 interleaver is unique |
| 719 | */ |
| 720 | static int egprs_type1_unmap(const sbit_t *bursts, sbit_t *hc, |
| 721 | sbit_t *c1, sbit_t *c2, int msc) |
| 722 | { |
| 723 | int i; |
| 724 | sbit_t burst[348]; |
| 725 | sbit_t hi[EGPRS_HDR_HC_MAX]; |
| 726 | sbit_t di[EGPRS_DATA_C1 * 2]; |
| 727 | |
| 728 | for (i = 0; i < 4; i++) { |
| 729 | memcpy(burst, &bursts[i * 348], 348); |
| 730 | |
| 731 | gsm0503_mcs5_burst_swap(burst); |
| 732 | gsm0503_mcs7_ul_burst_unmap(di, burst, hi, i); |
| 733 | } |
| 734 | |
| 735 | if (msc == EGPRS_MCS7) |
| 736 | gsm0503_mcs7_ul_deinterleave(hc, c1, c2, hi, di); |
| 737 | else |
| 738 | gsm0503_mcs8_ul_deinterleave(hc, c1, c2, hi, di); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Decode EGPRS UL header section |
| 745 | * |
| 746 | * 1. Depuncture |
| 747 | * 2. Convolutional decoding |
| 748 | * 3. CRC check |
| 749 | */ |
| 750 | static int _egprs_decode_hdr(const sbit_t *hc, int mcs, |
| 751 | union gprs_rlc_ul_hdr_egprs *hdr) |
| 752 | { |
| 753 | sbit_t C[EGPRS_HDR_C_MAX]; |
| 754 | ubit_t upp[EGPRS_HDR_UPP_MAX]; |
| 755 | int i, j, rc; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 756 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 757 | |
| 758 | code = &gsm0503_mcs_ul_codes[mcs]; |
| 759 | |
| 760 | /* Skip depuncturing on MCS-5,6 header */ |
| 761 | if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) { |
| 762 | memcpy(C, hc, code->hdr_code_len); |
| 763 | goto hdr_conv_decode; |
| 764 | } |
| 765 | |
| 766 | if (!code->hdr_punc) { |
| 767 | /* Invalid MCS-X header puncture matrix */ |
| 768 | return -1; |
| 769 | } |
| 770 | |
| 771 | i = code->hdr_code_len - 1; |
| 772 | j = code->hdr_punc_len - 1; |
| 773 | |
| 774 | for (; i >= 0; i--) { |
| 775 | if (!code->hdr_punc[i]) |
| 776 | C[i] = hc[j--]; |
| 777 | else |
| 778 | C[i] = 0; |
| 779 | } |
| 780 | |
| 781 | hdr_conv_decode: |
| 782 | osmo_conv_decode_ber(code->hdr_conv, C, upp, NULL, NULL); |
| 783 | rc = osmo_crc8gen_check_bits(&gsm0503_mcs_crc8_hdr, upp, |
| 784 | code->hdr_len, upp + code->hdr_len); |
| 785 | if (rc) |
| 786 | return -1; |
| 787 | |
| 788 | osmo_ubit2pbit_ext((pbit_t *) hdr, 0, upp, 0, code->hdr_len, 1); |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * Blind MCS header decoding based on burst length and CRC validation. |
| 795 | * Ignore 'q' value coding identification. This approach provides |
| 796 | * the strongest chance of header recovery. |
| 797 | */ |
| 798 | static int egprs_decode_hdr(union gprs_rlc_ul_hdr_egprs *hdr, |
| 799 | const sbit_t *bursts, uint16_t nbits) |
| 800 | { |
| 801 | int rc; |
| 802 | sbit_t hc[EGPRS_HDR_HC_MAX]; |
| 803 | |
| 804 | if (nbits == GSM0503_GPRS_BURSTS_NBITS) { |
| 805 | /* MCS-1,2,3,4 */ |
| 806 | egprs_type3_unmap(bursts, hc, NULL); |
| 807 | rc = _egprs_decode_hdr(hc, EGPRS_MCS1, hdr); |
| 808 | if (!rc) |
| 809 | return EGPRS_HDR_TYPE3; |
| 810 | } else if (nbits == GSM0503_EGPRS_BURSTS_NBITS) { |
| 811 | /* MCS-5,6 */ |
| 812 | egprs_type2_unmap(bursts, hc, NULL); |
| 813 | rc = _egprs_decode_hdr(hc, EGPRS_MCS5, hdr); |
| 814 | if (!rc) |
| 815 | return EGPRS_HDR_TYPE2; |
| 816 | |
| 817 | /* MCS-7,8,9 */ |
| 818 | egprs_type1_unmap(bursts, hc, NULL, NULL, EGPRS_MCS7); |
| 819 | rc = _egprs_decode_hdr(hc, EGPRS_MCS7, hdr); |
| 820 | if (!rc) |
| 821 | return EGPRS_HDR_TYPE1; |
| 822 | } |
| 823 | |
| 824 | return -1; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Parse EGPRS UL header for coding and puncturing scheme (CPS) |
| 829 | * |
| 830 | * Type 1 - MCS-7,8,9 |
| 831 | * Type 2 - MCS-5,6 |
| 832 | * Type 3 - MCS-1,2,3,4 |
| 833 | */ |
| 834 | static int egprs_parse_ul_cps(struct egprs_cps *cps, |
| 835 | union gprs_rlc_ul_hdr_egprs *hdr, int type) |
| 836 | { |
| 837 | uint8_t bits; |
| 838 | |
| 839 | switch (type) { |
| 840 | case EGPRS_HDR_TYPE1: |
| 841 | bits = hdr->type1.cps; |
| 842 | break; |
| 843 | case EGPRS_HDR_TYPE2: |
| 844 | bits = (hdr->type2.cps_lo << 2) | hdr->type2.cps_hi; |
| 845 | break; |
| 846 | case EGPRS_HDR_TYPE3: |
| 847 | bits = (hdr->type3.cps_lo << 2) | hdr->type3.cps_hi; |
| 848 | break; |
| 849 | default: |
| 850 | return -1; |
| 851 | } |
| 852 | |
| 853 | return egprs_get_cps(cps, type, bits); |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * Decode EGPRS UL data section |
| 858 | * |
| 859 | * 1. Depuncture |
| 860 | * 2. Convolutional decoding |
| 861 | * 3. CRC check |
| 862 | * 4. Block combining (MCS-7,8,9 only) |
| 863 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 864 | static int egprs_decode_data(uint8_t *l2_data, const sbit_t *c, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 865 | int mcs, int p, int blk, int *n_errors, int *n_bits_total) |
| 866 | { |
| 867 | ubit_t u[EGPRS_DATA_U_MAX]; |
| 868 | sbit_t C[EGPRS_DATA_C_MAX]; |
| 869 | |
| 870 | int i, j, rc, data_len; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 871 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 872 | |
| 873 | if (blk && mcs < EGPRS_MCS7) { |
| 874 | /* Invalid MCS-X block state */ |
| 875 | return -1; |
| 876 | } |
| 877 | |
| 878 | code = &gsm0503_mcs_ul_codes[mcs]; |
| 879 | if (!code->data_punc[p]) { |
| 880 | /* Invalid MCS-X data puncture matrix */ |
| 881 | return -1; |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * MCS-1,6 - single block processing |
| 886 | * MCS-7,9 - dual block processing |
| 887 | */ |
| 888 | if (mcs >= EGPRS_MCS7) |
| 889 | data_len = code->data_len / 2; |
| 890 | else |
| 891 | data_len = code->data_len; |
| 892 | |
| 893 | i = code->data_code_len - 1; |
| 894 | j = code->data_punc_len - 1; |
| 895 | |
| 896 | for (; i >= 0; i--) { |
| 897 | if (!code->data_punc[p][i]) |
| 898 | C[i] = c[j--]; |
| 899 | else |
| 900 | C[i] = 0; |
| 901 | } |
| 902 | |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 903 | osmo_conv_decode_ber_punctured(code->data_conv, C, u, |
| 904 | n_errors, n_bits_total, code->data_punc[p]); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 905 | rc = osmo_crc16gen_check_bits(&gsm0503_mcs_crc12, u, |
| 906 | data_len, u + data_len); |
| 907 | if (rc) |
| 908 | return -1; |
| 909 | |
| 910 | /* Offsets output pointer on the second block of Type 1 MCS */ |
| 911 | osmo_ubit2pbit_ext(l2_data, code->hdr_len + blk * data_len, |
| 912 | u, 0, data_len, 1); |
| 913 | |
| 914 | /* Return the number of bytes required for the bit message */ |
Max | dd75bac | 2017-06-13 15:07:01 +0200 | [diff] [blame] | 915 | return OSMO_BYTES_FOR_BITS(code->hdr_len + code->data_len); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 916 | } |
| 917 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 918 | /*! Decode EGPRS UL message |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 919 | * 1. Header section decoding |
| 920 | * 2. Extract CPS settings |
| 921 | * 3. Burst unmapping and deinterleaving |
| 922 | * 4. Data section decoding |
| 923 | * \param[out] l2_data caller-allocated buffer for L2 Frame |
| 924 | * \param[in] bursts burst input data as soft unpacked bits |
| 925 | * \param[in] nbits number of bits in \a bursts |
| 926 | * \param usf_p unused argument ?!? |
| 927 | * \param[out] n_errors number of detected bit-errors |
Alexander Chemeris | ed7d2dd | 2018-07-14 21:06:27 +0200 | [diff] [blame] | 928 | * \param[out] n_bits_total total number of decoded bits |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 929 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 930 | int gsm0503_pdtch_egprs_decode(uint8_t *l2_data, const sbit_t *bursts, uint16_t nbits, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 931 | uint8_t *usf_p, int *n_errors, int *n_bits_total) |
| 932 | { |
| 933 | sbit_t dc[EGPRS_DATA_DC_MAX]; |
| 934 | sbit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2]; |
| 935 | int type, rc; |
| 936 | struct egprs_cps cps; |
| 937 | union gprs_rlc_ul_hdr_egprs *hdr; |
| 938 | |
Alexander Chemeris | 50f7d74 | 2018-07-14 21:07:27 +0200 | [diff] [blame] | 939 | if (n_errors) |
| 940 | *n_errors = 0; |
| 941 | if (n_bits_total) |
| 942 | *n_bits_total = 0; |
| 943 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 944 | if ((nbits != GSM0503_GPRS_BURSTS_NBITS) && |
| 945 | (nbits != GSM0503_EGPRS_BURSTS_NBITS)) { |
| 946 | /* Invalid EGPRS bit length */ |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 947 | return -EOVERFLOW; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | hdr = (union gprs_rlc_ul_hdr_egprs *) l2_data; |
| 951 | type = egprs_decode_hdr(hdr, bursts, nbits); |
| 952 | if (egprs_parse_ul_cps(&cps, hdr, type) < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 953 | return -EIO; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 954 | |
| 955 | switch (cps.mcs) { |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 956 | case EGPRS_MCS0: |
| 957 | return -ENOTSUP; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 958 | case EGPRS_MCS1: |
| 959 | case EGPRS_MCS2: |
| 960 | case EGPRS_MCS3: |
| 961 | case EGPRS_MCS4: |
| 962 | egprs_type3_unmap(bursts, NULL, dc); |
| 963 | break; |
| 964 | case EGPRS_MCS5: |
| 965 | case EGPRS_MCS6: |
| 966 | egprs_type2_unmap(bursts, NULL, dc); |
| 967 | break; |
| 968 | case EGPRS_MCS7: |
| 969 | case EGPRS_MCS8: |
| 970 | case EGPRS_MCS9: |
| 971 | egprs_type1_unmap(bursts, NULL, c1, c2, cps.mcs); |
| 972 | break; |
| 973 | default: |
| 974 | /* Invalid MCS-X */ |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 975 | return -EINVAL; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* Decode MCS-X block, where X = cps.mcs */ |
| 979 | if (cps.mcs < EGPRS_MCS7) { |
| 980 | rc = egprs_decode_data(l2_data, dc, cps.mcs, cps.p[0], |
| 981 | 0, n_errors, n_bits_total); |
| 982 | if (rc < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 983 | return -EFAULT; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 984 | } else { |
Alexander Chemeris | 9444326 | 2018-07-14 21:09:54 +0200 | [diff] [blame] | 985 | /* Bit counters for the second block */ |
| 986 | int n_errors2, n_bits_total2; |
| 987 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 988 | /* MCS-7,8,9 block 1 */ |
| 989 | rc = egprs_decode_data(l2_data, c1, cps.mcs, cps.p[0], |
| 990 | 0, n_errors, n_bits_total); |
| 991 | if (rc < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 992 | return -EFAULT; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 993 | |
| 994 | /* MCS-7,8,9 block 2 */ |
| 995 | rc = egprs_decode_data(l2_data, c2, cps.mcs, cps.p[1], |
Alexander Chemeris | 9444326 | 2018-07-14 21:09:54 +0200 | [diff] [blame] | 996 | 1, &n_errors2, &n_bits_total2); |
| 997 | if (n_errors) |
| 998 | *n_errors += n_errors2; |
| 999 | if (n_bits_total) |
| 1000 | *n_bits_total += n_bits_total2; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1001 | if (rc < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 1002 | return -EFAULT; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | return rc; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * GSM PDTCH block transcoding |
| 1010 | */ |
| 1011 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1012 | /*! Decode GPRS PDTCH |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1013 | * \param[out] l2_data caller-allocated buffer for L2 Frame |
| 1014 | * \param[in] bursts burst input data as soft unpacked bits |
| 1015 | * \param[out] usf_p uplink stealing flag |
| 1016 | * \param[out] n_errors number of detected bit-errors |
| 1017 | * \param[out] n_bits_total total number of dcoded bits |
| 1018 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1019 | int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1020 | int *n_errors, int *n_bits_total) |
| 1021 | { |
| 1022 | sbit_t iB[456], cB[676], hl_hn[8]; |
| 1023 | ubit_t conv[456]; |
| 1024 | int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy */ |
| 1025 | |
| 1026 | for (i = 0; i < 4; i++) |
| 1027 | gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], |
| 1028 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1029 | |
| 1030 | for (i = 0; i < 4; i++) { |
| 1031 | for (j = 0, k = 0; j < 8; j++) |
| 1032 | k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j])); |
| 1033 | |
| 1034 | if (i == 0 || k < best) { |
| 1035 | best = k; |
| 1036 | cs = i + 1; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | gsm0503_xcch_deinterleave(cB, iB); |
| 1041 | |
| 1042 | switch (cs) { |
| 1043 | case 1: |
| 1044 | osmo_conv_decode_ber(&gsm0503_xcch, cB, |
| 1045 | conv, n_errors, n_bits_total); |
| 1046 | |
| 1047 | rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40, |
| 1048 | conv, 184, conv + 184); |
| 1049 | if (rv) |
| 1050 | return -1; |
| 1051 | |
| 1052 | osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1); |
| 1053 | |
| 1054 | return 23; |
| 1055 | case 2: |
| 1056 | for (i = 587, j = 455; i >= 0; i--) { |
| 1057 | if (!gsm0503_puncture_cs2[i]) |
| 1058 | cB[i] = cB[j--]; |
| 1059 | else |
| 1060 | cB[i] = 0; |
| 1061 | } |
| 1062 | |
| 1063 | osmo_conv_decode_ber(&gsm0503_cs2_np, cB, |
| 1064 | conv, n_errors, n_bits_total); |
| 1065 | |
| 1066 | for (i = 0; i < 8; i++) { |
| 1067 | for (j = 0, k = 0; j < 6; j++) |
| 1068 | k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j])); |
| 1069 | |
| 1070 | if (i == 0 || k < best) { |
| 1071 | best = k; |
| 1072 | usf = i; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | conv[3] = usf & 1; |
| 1077 | conv[4] = (usf >> 1) & 1; |
| 1078 | conv[5] = (usf >> 2) & 1; |
| 1079 | if (usf_p) |
| 1080 | *usf_p = usf; |
| 1081 | |
| 1082 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1083 | conv + 3, 271, conv + 3 + 271); |
| 1084 | if (rv) |
| 1085 | return -1; |
| 1086 | |
| 1087 | osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1); |
| 1088 | |
| 1089 | return 34; |
| 1090 | case 3: |
| 1091 | for (i = 675, j = 455; i >= 0; i--) { |
| 1092 | if (!gsm0503_puncture_cs3[i]) |
| 1093 | cB[i] = cB[j--]; |
| 1094 | else |
| 1095 | cB[i] = 0; |
| 1096 | } |
| 1097 | |
| 1098 | osmo_conv_decode_ber(&gsm0503_cs3_np, cB, |
| 1099 | conv, n_errors, n_bits_total); |
| 1100 | |
| 1101 | for (i = 0; i < 8; i++) { |
| 1102 | for (j = 0, k = 0; j < 6; j++) |
| 1103 | k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j])); |
| 1104 | |
| 1105 | if (i == 0 || k < best) { |
| 1106 | best = k; |
| 1107 | usf = i; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | conv[3] = usf & 1; |
| 1112 | conv[4] = (usf >> 1) & 1; |
| 1113 | conv[5] = (usf >> 2) & 1; |
| 1114 | if (usf_p) |
| 1115 | *usf_p = usf; |
| 1116 | |
| 1117 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1118 | conv + 3, 315, conv + 3 + 315); |
| 1119 | if (rv) |
| 1120 | return -1; |
| 1121 | |
| 1122 | osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1); |
| 1123 | |
| 1124 | return 40; |
| 1125 | case 4: |
| 1126 | for (i = 12; i < 456; i++) |
| 1127 | conv[i] = (cB[i] < 0) ? 1 : 0; |
| 1128 | |
| 1129 | for (i = 0; i < 8; i++) { |
| 1130 | for (j = 0, k = 0; j < 12; j++) |
| 1131 | k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j])); |
| 1132 | |
| 1133 | if (i == 0 || k < best) { |
| 1134 | best = k; |
| 1135 | usf = i; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | conv[9] = usf & 1; |
| 1140 | conv[10] = (usf >> 1) & 1; |
| 1141 | conv[11] = (usf >> 2) & 1; |
| 1142 | if (usf_p) |
| 1143 | *usf_p = usf; |
| 1144 | |
| 1145 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1146 | conv + 9, 431, conv + 9 + 431); |
| 1147 | if (rv) { |
| 1148 | *n_bits_total = 456 - 12; |
| 1149 | *n_errors = *n_bits_total; |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
| 1153 | *n_bits_total = 456 - 12; |
| 1154 | *n_errors = 0; |
| 1155 | |
| 1156 | osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1); |
| 1157 | |
| 1158 | return 54; |
| 1159 | default: |
| 1160 | *n_bits_total = 0; |
| 1161 | *n_errors = 0; |
| 1162 | break; |
| 1163 | } |
| 1164 | |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
| 1168 | /* |
Pau Espin Pedrol | f62f073 | 2020-04-07 13:12:11 +0200 | [diff] [blame] | 1169 | * EGPRS PDTCH DL block encoding |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1170 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1171 | static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1172 | { |
| 1173 | int i; |
| 1174 | ubit_t iB[456]; |
| 1175 | const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3]; |
| 1176 | |
Pau Espin Pedrol | 63ebc36 | 2020-04-07 13:15:36 +0200 | [diff] [blame] | 1177 | gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1178 | |
| 1179 | for (i = 0; i < 4; i++) { |
| 1180 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 1181 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1182 | } |
| 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1187 | static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1188 | { |
| 1189 | int i; |
| 1190 | const ubit_t *up; |
| 1191 | ubit_t hi[EGPRS_HDR_HC_MAX]; |
| 1192 | ubit_t di[EGPRS_DATA_DC_MAX]; |
| 1193 | |
| 1194 | gsm0503_mcs5_dl_interleave(hc, dc, hi, di); |
| 1195 | up = gsm0503_mcs5_usf_precode_table[usf]; |
| 1196 | |
| 1197 | for (i = 0; i < 4; i++) { |
| 1198 | gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i); |
| 1199 | gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]); |
| 1200 | } |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1205 | static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc, |
| 1206 | const ubit_t *c1, const ubit_t *c2, int usf, int mcs) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1207 | { |
| 1208 | int i; |
| 1209 | const ubit_t *up; |
| 1210 | ubit_t hi[EGPRS_HDR_HC_MAX]; |
| 1211 | ubit_t di[EGPRS_DATA_C1 * 2]; |
| 1212 | |
| 1213 | if (mcs == EGPRS_MCS7) |
| 1214 | gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di); |
| 1215 | else |
| 1216 | gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di); |
| 1217 | |
| 1218 | up = gsm0503_mcs5_usf_precode_table[usf]; |
| 1219 | |
| 1220 | for (i = 0; i < 4; i++) { |
| 1221 | gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i); |
| 1222 | gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]); |
| 1223 | } |
| 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1228 | static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1229 | { |
| 1230 | int i, j; |
| 1231 | ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX]; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 1232 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1233 | |
| 1234 | code = &gsm0503_mcs_dl_codes[mcs]; |
| 1235 | |
| 1236 | osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1); |
| 1237 | osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp, |
| 1238 | code->hdr_len, upp + code->hdr_len); |
| 1239 | |
| 1240 | osmo_conv_encode(code->hdr_conv, upp, C); |
| 1241 | |
| 1242 | /* MCS-5,6 header direct puncture instead of table */ |
| 1243 | if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) { |
| 1244 | memcpy(hc, C, code->hdr_code_len); |
| 1245 | hc[99] = hc[98]; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | if (!code->hdr_punc) { |
| 1250 | /* Invalid MCS-X header puncture matrix */ |
| 1251 | return -1; |
| 1252 | } |
| 1253 | |
| 1254 | for (i = 0, j = 0; i < code->hdr_code_len; i++) { |
| 1255 | if (!code->hdr_punc[i]) |
| 1256 | hc[j++] = C[i]; |
| 1257 | } |
| 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1262 | static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1263 | int mcs, int p, int blk) |
| 1264 | { |
| 1265 | int i, j, data_len; |
| 1266 | ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX]; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 1267 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1268 | |
| 1269 | code = &gsm0503_mcs_dl_codes[mcs]; |
| 1270 | |
| 1271 | /* |
| 1272 | * Dual block - MCS-7,8,9 |
| 1273 | * Single block - MCS-1,2,3,4,5,6 |
| 1274 | */ |
| 1275 | if (mcs >= EGPRS_MCS7) |
| 1276 | data_len = code->data_len / 2; |
| 1277 | else |
| 1278 | data_len = code->data_len; |
| 1279 | |
| 1280 | osmo_pbit2ubit_ext(u, 0, l2_data, |
| 1281 | code->usf_len + code->hdr_len + blk * data_len, data_len, 1); |
| 1282 | |
| 1283 | osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len); |
| 1284 | |
| 1285 | osmo_conv_encode(code->data_conv, u, C); |
| 1286 | |
| 1287 | if (!code->data_punc[p]) { |
| 1288 | /* Invalid MCS-X data puncture matrix */ |
| 1289 | return -1; |
| 1290 | } |
| 1291 | |
| 1292 | for (i = 0, j = 0; i < code->data_code_len; i++) { |
| 1293 | if (!code->data_punc[p][i]) |
| 1294 | c[j++] = C[i]; |
| 1295 | } |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Parse EGPRS DL header for coding and puncturing scheme (CPS) |
| 1302 | * |
| 1303 | * Type 1 - MCS-7,8,9 |
| 1304 | * Type 2 - MCS-5,6 |
| 1305 | * Type 3 - MCS-1,2,3,4 |
| 1306 | */ |
| 1307 | static int egprs_parse_dl_cps(struct egprs_cps *cps, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1308 | const union gprs_rlc_dl_hdr_egprs *hdr, int type) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1309 | { |
| 1310 | uint8_t bits; |
| 1311 | |
| 1312 | switch (type) { |
| 1313 | case EGPRS_HDR_TYPE1: |
| 1314 | bits = hdr->type1.cps; |
| 1315 | break; |
| 1316 | case EGPRS_HDR_TYPE2: |
| 1317 | bits = hdr->type2.cps; |
| 1318 | break; |
| 1319 | case EGPRS_HDR_TYPE3: |
| 1320 | bits = hdr->type3.cps; |
| 1321 | break; |
| 1322 | default: |
| 1323 | return -1; |
| 1324 | } |
| 1325 | |
| 1326 | return egprs_get_cps(cps, type, bits); |
| 1327 | } |
| 1328 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1329 | /*! EGPRS DL message encoding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1330 | * \param[out] bursts caller-allocated buffer for unpacked burst bits |
| 1331 | * \param[in] l2_data L2 (MAC) block to be encoded |
| 1332 | * \param[in] l2_len length of l2_data in bytes, used to determine MCS |
Vadim Yanitskiy | 8055cdd | 2020-03-30 18:16:38 +0700 | [diff] [blame] | 1333 | * \returns number of bits encoded; negative on error */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1334 | int gsm0503_pdtch_egprs_encode(ubit_t *bursts, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1335 | const uint8_t *l2_data, uint8_t l2_len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1336 | { |
| 1337 | ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX]; |
| 1338 | ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2]; |
| 1339 | uint8_t mcs; |
| 1340 | struct egprs_cps cps; |
| 1341 | union gprs_rlc_dl_hdr_egprs *hdr; |
| 1342 | |
| 1343 | switch (l2_len) { |
| 1344 | case 27: |
| 1345 | mcs = EGPRS_MCS1; |
| 1346 | break; |
| 1347 | case 33: |
| 1348 | mcs = EGPRS_MCS2; |
| 1349 | break; |
| 1350 | case 42: |
| 1351 | mcs = EGPRS_MCS3; |
| 1352 | break; |
| 1353 | case 49: |
| 1354 | mcs = EGPRS_MCS4; |
| 1355 | break; |
| 1356 | case 60: |
| 1357 | mcs = EGPRS_MCS5; |
| 1358 | break; |
| 1359 | case 78: |
| 1360 | mcs = EGPRS_MCS6; |
| 1361 | break; |
| 1362 | case 118: |
| 1363 | mcs = EGPRS_MCS7; |
| 1364 | break; |
| 1365 | case 142: |
| 1366 | mcs = EGPRS_MCS8; |
| 1367 | break; |
| 1368 | case 154: |
| 1369 | mcs = EGPRS_MCS9; |
| 1370 | break; |
| 1371 | default: |
| 1372 | return -1; |
| 1373 | } |
| 1374 | |
| 1375 | /* Read header for USF and puncturing matrix selection. */ |
| 1376 | hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data; |
| 1377 | |
| 1378 | switch (mcs) { |
| 1379 | case EGPRS_MCS1: |
| 1380 | case EGPRS_MCS2: |
| 1381 | case EGPRS_MCS3: |
| 1382 | case EGPRS_MCS4: |
| 1383 | /* Check for valid CPS and matching MCS to message size */ |
| 1384 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) || |
| 1385 | (cps.mcs != mcs)) |
| 1386 | goto bad_header; |
| 1387 | |
| 1388 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1389 | egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0); |
| 1390 | egprs_type3_map(bursts, hc, dc, hdr->type3.usf); |
| 1391 | break; |
| 1392 | case EGPRS_MCS5: |
| 1393 | case EGPRS_MCS6: |
| 1394 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) || |
| 1395 | (cps.mcs != mcs)) |
| 1396 | goto bad_header; |
| 1397 | |
| 1398 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1399 | egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0); |
| 1400 | egprs_type2_map(bursts, hc, dc, hdr->type2.usf); |
| 1401 | break; |
| 1402 | case EGPRS_MCS7: |
| 1403 | case EGPRS_MCS8: |
| 1404 | case EGPRS_MCS9: |
| 1405 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) || |
| 1406 | (cps.mcs != mcs)) |
| 1407 | goto bad_header; |
| 1408 | |
| 1409 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1410 | egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0); |
| 1411 | egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1); |
| 1412 | egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs); |
| 1413 | break; |
| 1414 | } |
| 1415 | |
| 1416 | return mcs >= EGPRS_MCS5 ? |
| 1417 | GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS; |
| 1418 | |
| 1419 | bad_header: |
| 1420 | /* Invalid EGPRS MCS-X header */ |
| 1421 | return -1; |
| 1422 | } |
| 1423 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1424 | /*! GPRS DL message encoding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1425 | * \param[out] bursts caller-allocated buffer for unpacked burst bits |
| 1426 | * \param[in] l2_data L2 (MAC) block to be encoded |
| 1427 | * \param[in] l2_len length of l2_data in bytes, used to determine CS |
Vadim Yanitskiy | 8055cdd | 2020-03-30 18:16:38 +0700 | [diff] [blame] | 1428 | * \returns number of bits encoded; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1429 | int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1430 | { |
| 1431 | ubit_t iB[456], cB[676]; |
| 1432 | const ubit_t *hl_hn; |
| 1433 | ubit_t conv[334]; |
| 1434 | int i, j, usf; |
| 1435 | |
| 1436 | switch (l2_len) { |
| 1437 | case 23: |
| 1438 | osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1); |
| 1439 | |
| 1440 | osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184); |
| 1441 | |
| 1442 | osmo_conv_encode(&gsm0503_xcch, conv, cB); |
| 1443 | |
| 1444 | hl_hn = gsm0503_pdtch_hl_hn_ubit[0]; |
| 1445 | |
| 1446 | break; |
| 1447 | case 34: |
| 1448 | osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1); |
| 1449 | usf = l2_data[0] & 0x7; |
| 1450 | |
| 1451 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3, |
| 1452 | 271, conv + 3 + 271); |
| 1453 | |
| 1454 | memcpy(conv, gsm0503_usf2six[usf], 6); |
| 1455 | |
| 1456 | osmo_conv_encode(&gsm0503_cs2_np, conv, cB); |
| 1457 | |
| 1458 | for (i = 0, j = 0; i < 588; i++) |
| 1459 | if (!gsm0503_puncture_cs2[i]) |
| 1460 | cB[j++] = cB[i]; |
| 1461 | |
| 1462 | hl_hn = gsm0503_pdtch_hl_hn_ubit[1]; |
| 1463 | |
| 1464 | break; |
| 1465 | case 40: |
| 1466 | osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1); |
| 1467 | usf = l2_data[0] & 0x7; |
| 1468 | |
| 1469 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3, |
| 1470 | 315, conv + 3 + 315); |
| 1471 | |
| 1472 | memcpy(conv, gsm0503_usf2six[usf], 6); |
| 1473 | |
| 1474 | osmo_conv_encode(&gsm0503_cs3_np, conv, cB); |
| 1475 | |
| 1476 | for (i = 0, j = 0; i < 676; i++) |
| 1477 | if (!gsm0503_puncture_cs3[i]) |
| 1478 | cB[j++] = cB[i]; |
| 1479 | |
| 1480 | hl_hn = gsm0503_pdtch_hl_hn_ubit[2]; |
| 1481 | |
| 1482 | break; |
| 1483 | case 54: |
| 1484 | osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1); |
| 1485 | usf = l2_data[0] & 0x7; |
| 1486 | |
| 1487 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9, |
| 1488 | 431, cB + 9 + 431); |
| 1489 | |
| 1490 | memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12); |
| 1491 | |
| 1492 | hl_hn = gsm0503_pdtch_hl_hn_ubit[3]; |
| 1493 | |
| 1494 | break; |
| 1495 | default: |
| 1496 | return -1; |
| 1497 | } |
| 1498 | |
| 1499 | gsm0503_xcch_interleave(cB, iB); |
| 1500 | |
| 1501 | for (i = 0; i < 4; i++) { |
| 1502 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 1503 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1504 | } |
| 1505 | |
| 1506 | return GSM0503_GPRS_BURSTS_NBITS; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * GSM TCH/F FR/EFR transcoding |
| 1511 | */ |
| 1512 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1513 | /*! assemble a FR codec frame in format as used inside RTP |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1514 | * \param[out] tch_data Codec frame in RTP format |
| 1515 | * \param[in] b_bits Codec frame in 'native' format |
| 1516 | * \param[in] net_order FIXME */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1517 | static void tch_fr_reassemble(uint8_t *tch_data, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1518 | const ubit_t *b_bits, int net_order) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1519 | { |
| 1520 | int i, j, k, l, o; |
| 1521 | |
| 1522 | tch_data[0] = 0xd << 4; |
| 1523 | memset(tch_data + 1, 0, 32); |
| 1524 | |
| 1525 | if (net_order) { |
| 1526 | for (i = 0, j = 4; i < 260; i++, j++) |
| 1527 | tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7))); |
| 1528 | |
| 1529 | return; |
| 1530 | } |
| 1531 | |
| 1532 | /* reassemble d-bits */ |
| 1533 | i = 0; /* counts bits */ |
| 1534 | j = 4; /* counts output bits */ |
| 1535 | k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */ |
| 1536 | l = 0; /* counts element bits */ |
| 1537 | o = 0; /* offset input bits */ |
| 1538 | while (i < 260) { |
| 1539 | tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7))); |
| 1540 | if (--k < 0) { |
| 1541 | o += gsm0503_gsm_fr_map[l]; |
| 1542 | k = gsm0503_gsm_fr_map[++l]-1; |
| 1543 | } |
| 1544 | i++; |
| 1545 | j++; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | static void tch_fr_disassemble(ubit_t *b_bits, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1550 | const uint8_t *tch_data, int net_order) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1551 | { |
| 1552 | int i, j, k, l, o; |
| 1553 | |
| 1554 | if (net_order) { |
| 1555 | for (i = 0, j = 4; i < 260; i++, j++) |
| 1556 | b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1557 | |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | i = 0; /* counts bits */ |
| 1562 | j = 4; /* counts input bits */ |
| 1563 | k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */ |
| 1564 | l = 0; /* counts element bits */ |
| 1565 | o = 0; /* offset output bits */ |
| 1566 | while (i < 260) { |
| 1567 | b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1568 | if (--k < 0) { |
| 1569 | o += gsm0503_gsm_fr_map[l]; |
| 1570 | k = gsm0503_gsm_fr_map[++l] - 1; |
| 1571 | } |
| 1572 | i++; |
| 1573 | j++; |
| 1574 | } |
| 1575 | } |
| 1576 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1577 | /* assemble a HR codec frame in format as used inside RTP */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1578 | static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1579 | { |
| 1580 | int i, j; |
| 1581 | |
| 1582 | tch_data[0] = 0x00; /* F = 0, FT = 000 */ |
| 1583 | memset(tch_data + 1, 0, 14); |
| 1584 | |
| 1585 | for (i = 0, j = 8; i < 112; i++, j++) |
| 1586 | tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7))); |
| 1587 | } |
| 1588 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1589 | static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1590 | { |
| 1591 | int i, j; |
| 1592 | |
| 1593 | for (i = 0, j = 8; i < 112; i++, j++) |
| 1594 | b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1595 | } |
| 1596 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1597 | /* assemble a EFR codec frame in format as used inside RTP */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1598 | static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1599 | { |
| 1600 | int i, j; |
| 1601 | |
| 1602 | tch_data[0] = 0xc << 4; |
| 1603 | memset(tch_data + 1, 0, 30); |
| 1604 | |
| 1605 | for (i = 0, j = 4; i < 244; i++, j++) |
| 1606 | tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7))); |
| 1607 | } |
| 1608 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1609 | static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1610 | { |
| 1611 | int i, j; |
| 1612 | |
| 1613 | for (i = 0, j = 4; i < 244; i++, j++) |
| 1614 | b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1615 | } |
| 1616 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1617 | /* assemble a AMR codec frame in format as used inside RTP */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1618 | static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1619 | { |
| 1620 | int i, j; |
| 1621 | |
| 1622 | memset(tch_data, 0, (len + 7) >> 3); |
| 1623 | |
| 1624 | for (i = 0, j = 0; i < len; i++, j++) |
| 1625 | tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7))); |
| 1626 | } |
| 1627 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1628 | static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1629 | { |
| 1630 | int i, j; |
| 1631 | |
| 1632 | for (i = 0, j = 0; i < len; i++, j++) |
| 1633 | d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1634 | } |
| 1635 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 1636 | /* Append STI and MI bits to the SID_UPDATE frame, see also |
| 1637 | * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */ |
| 1638 | static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi) |
| 1639 | { |
| 1640 | /* Zero out the space that had been used by the CRC14 */ |
| 1641 | memset(sid_update + 35, 0, 14); |
| 1642 | |
| 1643 | /* Append STI and MI parameters */ |
| 1644 | sid_update[35] = sti & 1; |
| 1645 | sid_update[36] = mi & 1; |
| 1646 | sid_update[37] = mi >> 1 & 1; |
| 1647 | sid_update[38] = mi >> 2 & 1; |
| 1648 | } |
| 1649 | |
| 1650 | /* Extract a SID UPDATE fram the sbits of an FR AMR frame */ |
| 1651 | static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits) |
| 1652 | { |
| 1653 | |
| 1654 | unsigned int i; |
| 1655 | |
| 1656 | sbits += 32; |
| 1657 | |
| 1658 | for (i = 0; i < 53; i++) { |
| 1659 | sid_update[0] = sbits[0]; |
| 1660 | sid_update[1] = sbits[1]; |
| 1661 | sid_update[2] = sbits[2]; |
| 1662 | sid_update[3] = sbits[3]; |
| 1663 | sid_update += 4; |
| 1664 | sbits += 8; |
| 1665 | } |
| 1666 | |
| 1667 | } |
| 1668 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1669 | /* re-arrange according to TS 05.03 Table 2 (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1670 | static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1671 | { |
| 1672 | int i; |
| 1673 | |
| 1674 | for (i = 0; i < 260; i++) |
| 1675 | b_bits[gsm610_bitorder[i]] = d_bits[i]; |
| 1676 | } |
| 1677 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1678 | /* re-arrange according to TS 05.03 Table 2 (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1679 | static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1680 | { |
| 1681 | int i; |
| 1682 | |
| 1683 | for (i = 0; i < 260; i++) |
| 1684 | d_bits[i] = b_bits[gsm610_bitorder[i]]; |
| 1685 | } |
| 1686 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1687 | /* re-arrange according to TS 05.03 Table 3a (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1688 | static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1689 | { |
| 1690 | int i; |
| 1691 | |
| 1692 | const uint16_t *map; |
| 1693 | |
| 1694 | if (!d_bits[93] && !d_bits[94]) |
| 1695 | map = gsm620_unvoiced_bitorder; |
| 1696 | else |
| 1697 | map = gsm620_voiced_bitorder; |
| 1698 | |
| 1699 | for (i = 0; i < 112; i++) |
| 1700 | b_bits[map[i]] = d_bits[i]; |
| 1701 | } |
| 1702 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1703 | /* re-arrange according to TS 05.03 Table 3a (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1704 | static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1705 | { |
| 1706 | int i; |
| 1707 | const uint16_t *map; |
| 1708 | |
| 1709 | if (!b_bits[34] && !b_bits[35]) |
| 1710 | map = gsm620_unvoiced_bitorder; |
| 1711 | else |
| 1712 | map = gsm620_voiced_bitorder; |
| 1713 | |
| 1714 | for (i = 0; i < 112; i++) |
| 1715 | d_bits[i] = b_bits[map[i]]; |
| 1716 | } |
| 1717 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1718 | /* re-arrange according to TS 05.03 Table 6 (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1719 | static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1720 | { |
| 1721 | int i; |
| 1722 | |
| 1723 | for (i = 0; i < 260; i++) |
| 1724 | b_bits[gsm660_bitorder[i]] = d_bits[i]; |
| 1725 | } |
| 1726 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1727 | /* re-arrange according to TS 05.03 Table 6 (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1728 | static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1729 | { |
| 1730 | int i; |
| 1731 | |
| 1732 | for (i = 0; i < 260; i++) |
| 1733 | d_bits[i] = b_bits[gsm660_bitorder[i]]; |
| 1734 | } |
| 1735 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1736 | /* extract the 65 protected class1a+1b bits */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1737 | static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1738 | { |
| 1739 | int i; |
| 1740 | |
| 1741 | for (i = 0; i < 65; i++) |
| 1742 | b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1]; |
| 1743 | } |
| 1744 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1745 | static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1746 | { |
| 1747 | int i; |
| 1748 | |
| 1749 | for (i = 0; i < 91; i++) { |
| 1750 | d[i << 1] = u[i]; |
| 1751 | d[(i << 1) + 1] = u[184 - i]; |
| 1752 | } |
| 1753 | |
| 1754 | for (i = 0; i < 3; i++) |
| 1755 | p[i] = u[91 + i]; |
| 1756 | } |
| 1757 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1758 | static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1759 | { |
| 1760 | int i; |
| 1761 | |
| 1762 | for (i = 0; i < 91; i++) { |
| 1763 | u[i] = d[i << 1]; |
| 1764 | u[184 - i] = d[(i << 1) + 1]; |
| 1765 | } |
| 1766 | |
| 1767 | for (i = 0; i < 3; i++) |
| 1768 | u[91 + i] = p[i]; |
| 1769 | } |
| 1770 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1771 | static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1772 | { |
| 1773 | memcpy(d, u, 95); |
| 1774 | memcpy(p, u + 95, 3); |
| 1775 | } |
| 1776 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1777 | static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1778 | { |
| 1779 | memcpy(u, d, 95); |
| 1780 | memcpy(u + 95, p, 3); |
| 1781 | } |
| 1782 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1783 | static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1784 | { |
| 1785 | memcpy(w, s, 71); |
| 1786 | w[71] = w[72] = s[69]; |
| 1787 | memcpy(w + 73, s + 71, 50); |
| 1788 | w[123] = w[124] = s[119]; |
| 1789 | memcpy(w + 125, s + 121, 53); |
| 1790 | w[178] = w[179] = s[172]; |
| 1791 | memcpy(w + 180, s + 174, 50); |
| 1792 | w[230] = w[231] = s[222]; |
| 1793 | memcpy(w + 232, s + 224, 20); |
| 1794 | memcpy(w + 252, p, 8); |
| 1795 | } |
| 1796 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1797 | static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1798 | { |
| 1799 | int sum; |
| 1800 | |
| 1801 | memcpy(s, w, 71); |
| 1802 | sum = s[69] + w[71] + w[72]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1803 | s[69] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1804 | memcpy(s + 71, w + 73, 50); |
| 1805 | sum = s[119] + w[123] + w[124]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1806 | s[119] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1807 | memcpy(s + 121, w + 125, 53); |
| 1808 | sum = s[172] + w[178] + w[179]; |
| 1809 | s[172] = (sum > 2); |
| 1810 | memcpy(s + 174, w + 180, 50); |
Niro Mahasinghe | 834e2ac | 2017-11-03 12:22:34 +0100 | [diff] [blame] | 1811 | sum = s[222] + w[230] + w[231]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1812 | s[222] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1813 | memcpy(s + 224, w + 232, 20); |
| 1814 | memcpy(p, w + 252, 8); |
| 1815 | } |
| 1816 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1817 | static void tch_amr_merge(ubit_t *u, const ubit_t *d, const ubit_t *p, int len, int prot) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1818 | { |
| 1819 | memcpy(u, d, prot); |
| 1820 | memcpy(u + prot, p, 6); |
| 1821 | memcpy(u + prot + 6, d + prot, len - prot); |
| 1822 | } |
| 1823 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1824 | static void tch_amr_unmerge(ubit_t *d, ubit_t *p, const ubit_t *u, int len, int prot) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1825 | { |
| 1826 | memcpy(d, u, prot); |
| 1827 | memcpy(p, u + prot, 6); |
| 1828 | memcpy(d + prot, u + prot + 6, len - prot); |
| 1829 | } |
| 1830 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1831 | /*! Perform channel decoding of a FR/EFR channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1832 | * \param[out] tch_data Codec frame in RTP payload format |
| 1833 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 1834 | * \param[in] net_order FIXME |
| 1835 | * \param[in] efr Is this channel using EFR (1) or FR (0) |
| 1836 | * \param[out] n_errors Number of detected bit errors |
| 1837 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 1838 | * \returns length of bytes used in \a tch_data output buffer; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1839 | int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1840 | int net_order, int efr, int *n_errors, int *n_bits_total) |
| 1841 | { |
| 1842 | sbit_t iB[912], cB[456], h; |
| 1843 | ubit_t conv[185], s[244], w[260], b[65], d[260], p[8]; |
| 1844 | int i, rv, len, steal = 0; |
| 1845 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1846 | /* map from 8 bursts to interleaved data bits (iB) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1847 | for (i = 0; i < 8; i++) { |
| 1848 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 1849 | &bursts[i * 116], &h, i >> 2); |
| 1850 | steal -= h; |
| 1851 | } |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1852 | /* we now have the bits of the four bursts (interface 4 in |
| 1853 | * Figure 1a of TS 05.03 */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1854 | |
| 1855 | gsm0503_tch_fr_deinterleave(cB, iB); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1856 | /* we now have the coded bits c(B): interface 3 in Fig. 1a */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1857 | |
| 1858 | if (steal > 0) { |
| 1859 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 1860 | if (rv) { |
| 1861 | /* Error decoding FACCH frame */ |
| 1862 | return -1; |
| 1863 | } |
| 1864 | |
| 1865 | return 23; |
| 1866 | } |
| 1867 | |
| 1868 | osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1869 | /* we now have the data bits 'u': interface 2 in Fig. 1a */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1870 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1871 | /* input: 'conv', output: d[ata] + p[arity] */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1872 | tch_fr_unreorder(d, p, conv); |
| 1873 | |
| 1874 | for (i = 0; i < 78; i++) |
| 1875 | d[i + 182] = (cB[i + 378] < 0) ? 1 : 0; |
| 1876 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1877 | /* check if parity of first 50 (class 1) 'd'-bits match 'p' */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1878 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p); |
| 1879 | if (rv) { |
| 1880 | /* Error checking CRC8 for the FR part of an EFR/FR frame */ |
| 1881 | return -1; |
| 1882 | } |
| 1883 | |
| 1884 | if (efr) { |
| 1885 | tch_efr_d_to_w(w, d); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1886 | /* we now have the preliminary-coded bits w(k) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1887 | |
| 1888 | tch_efr_unreorder(s, p, w); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1889 | /* we now have the data delivered to the preliminary |
| 1890 | * channel encoding unit s(k) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1891 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1892 | /* extract the 65 most important bits according TS 05.03 3.1.1.1 */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1893 | tch_efr_protected(s, b); |
| 1894 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1895 | /* perform CRC-8 on 65 most important bits (50 bits of |
| 1896 | * class 1a + 15 bits of class 1b) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1897 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p); |
| 1898 | if (rv) { |
| 1899 | /* Error checking CRC8 for the EFR part of an EFR frame */ |
| 1900 | return -1; |
| 1901 | } |
| 1902 | |
| 1903 | tch_efr_reassemble(tch_data, s); |
| 1904 | |
| 1905 | len = GSM_EFR_BYTES; |
| 1906 | } else { |
| 1907 | tch_fr_d_to_b(w, d); |
| 1908 | |
| 1909 | tch_fr_reassemble(tch_data, w, net_order); |
| 1910 | |
| 1911 | len = GSM_FR_BYTES; |
| 1912 | } |
| 1913 | |
| 1914 | return len; |
| 1915 | } |
| 1916 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1917 | /*! Perform channel encoding on a TCH/FS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1918 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 1919 | * \param[in] tch_data Codec input data in RTP payload format |
| 1920 | * \param[in] len Length of \a tch_data in bytes |
| 1921 | * \param[in] net_order FIXME |
| 1922 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1923 | int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1924 | int len, int net_order) |
| 1925 | { |
| 1926 | ubit_t iB[912], cB[456], h; |
| 1927 | ubit_t conv[185], w[260], b[65], s[244], d[260], p[8]; |
| 1928 | int i; |
| 1929 | |
| 1930 | switch (len) { |
| 1931 | case GSM_EFR_BYTES: /* TCH EFR */ |
| 1932 | |
| 1933 | tch_efr_disassemble(s, tch_data); |
| 1934 | |
| 1935 | tch_efr_protected(s, b); |
| 1936 | |
| 1937 | osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p); |
| 1938 | |
| 1939 | tch_efr_reorder(w, s, p); |
| 1940 | |
| 1941 | tch_efr_w_to_d(d, w); |
| 1942 | |
| 1943 | goto coding_efr_fr; |
| 1944 | case GSM_FR_BYTES: /* TCH FR */ |
| 1945 | tch_fr_disassemble(w, tch_data, net_order); |
| 1946 | |
| 1947 | tch_fr_b_to_d(d, w); |
| 1948 | |
| 1949 | coding_efr_fr: |
| 1950 | osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p); |
| 1951 | |
| 1952 | tch_fr_reorder(conv, d, p); |
| 1953 | |
| 1954 | memcpy(cB + 378, d + 182, 78); |
| 1955 | |
| 1956 | osmo_conv_encode(&gsm0503_tch_fr, conv, cB); |
| 1957 | |
| 1958 | h = 0; |
| 1959 | |
| 1960 | break; |
| 1961 | case GSM_MACBLOCK_LEN: /* FACCH */ |
| 1962 | _xcch_encode_cB(cB, tch_data); |
| 1963 | |
| 1964 | h = 1; |
| 1965 | |
| 1966 | break; |
| 1967 | default: |
| 1968 | return -1; |
| 1969 | } |
| 1970 | |
| 1971 | gsm0503_tch_fr_interleave(cB, iB); |
| 1972 | |
| 1973 | for (i = 0; i < 8; i++) { |
| 1974 | gsm0503_tch_burst_map(&iB[i * 114], |
| 1975 | &bursts[i * 116], &h, i >> 2); |
| 1976 | } |
| 1977 | |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1981 | /*! Perform channel decoding of a HR(v1) channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1982 | * \param[out] tch_data Codec frame in RTP payload format |
| 1983 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 1984 | * \param[in] odd Odd (1) or even (0) frame number |
| 1985 | * \param[out] n_errors Number of detected bit errors |
| 1986 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 1987 | * \returns length of bytes used in \a tch_data output buffer; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1988 | int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1989 | int *n_errors, int *n_bits_total) |
| 1990 | { |
| 1991 | sbit_t iB[912], cB[456], h; |
| 1992 | ubit_t conv[98], b[112], d[112], p[3]; |
| 1993 | int i, rv, steal = 0; |
| 1994 | |
| 1995 | /* Only unmap the stealing bits */ |
| 1996 | if (!odd) { |
| 1997 | for (i = 0; i < 4; i++) { |
| 1998 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0); |
| 1999 | steal -= h; |
| 2000 | } |
| 2001 | |
| 2002 | for (i = 2; i < 5; i++) { |
| 2003 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1); |
| 2004 | steal -= h; |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | /* If we found a stole FACCH, but only at correct alignment */ |
| 2009 | if (steal > 0) { |
| 2010 | for (i = 0; i < 6; i++) { |
| 2011 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2012 | &bursts[i * 116], NULL, i >> 2); |
| 2013 | } |
| 2014 | |
| 2015 | for (i = 2; i < 4; i++) { |
| 2016 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], |
| 2017 | &bursts[i * 116], NULL, 1); |
| 2018 | } |
| 2019 | |
| 2020 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2021 | |
| 2022 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2023 | if (rv) { |
| 2024 | /* Error decoding FACCH frame */ |
| 2025 | return -1; |
| 2026 | } |
| 2027 | |
| 2028 | return GSM_MACBLOCK_LEN; |
| 2029 | } |
| 2030 | |
| 2031 | for (i = 0; i < 4; i++) { |
| 2032 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2033 | &bursts[i * 116], NULL, i >> 1); |
| 2034 | } |
| 2035 | |
| 2036 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2037 | |
| 2038 | osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total); |
| 2039 | |
| 2040 | tch_hr_unreorder(d, p, conv); |
| 2041 | |
| 2042 | for (i = 0; i < 17; i++) |
| 2043 | d[i + 95] = (cB[i + 211] < 0) ? 1 : 0; |
| 2044 | |
| 2045 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p); |
| 2046 | if (rv) { |
| 2047 | /* Error checking CRC8 for an HR frame */ |
| 2048 | return -1; |
| 2049 | } |
| 2050 | |
| 2051 | tch_hr_d_to_b(b, d); |
| 2052 | |
| 2053 | tch_hr_reassemble(tch_data, b); |
| 2054 | |
| 2055 | return 15; |
| 2056 | } |
| 2057 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2058 | /*! Perform channel encoding on a TCH/HS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2059 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2060 | * \param[in] tch_data Codec input data in RTP payload format |
| 2061 | * \param[in] len Length of \a tch_data in bytes |
| 2062 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2063 | int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2064 | { |
| 2065 | ubit_t iB[912], cB[456], h; |
| 2066 | ubit_t conv[98], b[112], d[112], p[3]; |
| 2067 | int i; |
| 2068 | |
| 2069 | switch (len) { |
| 2070 | case 15: /* TCH HR */ |
| 2071 | tch_hr_disassemble(b, tch_data); |
| 2072 | |
| 2073 | tch_hr_b_to_d(d, b); |
| 2074 | |
| 2075 | osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p); |
| 2076 | |
| 2077 | tch_hr_reorder(conv, d, p); |
| 2078 | |
| 2079 | osmo_conv_encode(&gsm0503_tch_hr, conv, cB); |
| 2080 | |
| 2081 | memcpy(cB + 211, d + 95, 17); |
| 2082 | |
| 2083 | h = 0; |
| 2084 | |
| 2085 | gsm0503_tch_hr_interleave(cB, iB); |
| 2086 | |
| 2087 | for (i = 0; i < 4; i++) { |
| 2088 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2089 | &bursts[i * 116], &h, i >> 1); |
| 2090 | } |
| 2091 | |
| 2092 | break; |
| 2093 | case GSM_MACBLOCK_LEN: /* FACCH */ |
| 2094 | _xcch_encode_cB(cB, tch_data); |
| 2095 | |
| 2096 | h = 1; |
| 2097 | |
| 2098 | gsm0503_tch_fr_interleave(cB, iB); |
| 2099 | |
| 2100 | for (i = 0; i < 6; i++) { |
| 2101 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2102 | &bursts[i * 116], &h, i >> 2); |
| 2103 | } |
| 2104 | |
| 2105 | for (i = 2; i < 4; i++) { |
| 2106 | gsm0503_tch_burst_map(&iB[i * 114 + 456], |
| 2107 | &bursts[i * 116], &h, 1); |
| 2108 | } |
| 2109 | |
| 2110 | break; |
| 2111 | default: |
| 2112 | return -1; |
| 2113 | } |
| 2114 | |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2118 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2119 | * \param[out] tch_data Codec frame in RTP payload format |
| 2120 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2121 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2122 | * \param[in] codec array of active codecs (active codec set) |
| 2123 | * \param[in] codecs number of codecs in \a codec |
| 2124 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2125 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2126 | * \param[out] n_errors Number of detected bit errors |
| 2127 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 2128 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2129 | * codec out of range; negative on error |
| 2130 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2131 | int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2132 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2133 | uint8_t *cmr, int *n_errors, int *n_bits_total) |
| 2134 | { |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2135 | return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req, |
| 2136 | codec, codecs, ft, cmr, n_errors, |
| 2137 | n_bits_total, NULL); |
| 2138 | } |
| 2139 | |
| 2140 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
| 2141 | * \param[out] tch_data Codec frame in RTP payload format |
| 2142 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2143 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2144 | * \param[in] codec array of active codecs (active codec set) |
| 2145 | * \param[in] codecs number of codecs in \a codec |
| 2146 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2147 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2148 | * \param[out] n_errors Number of detected bit errors |
| 2149 | * \param[out] n_bits_total Total number of bits |
| 2150 | * \param[inout] dtx DTX frame type output, previous DTX frame type input |
| 2151 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2152 | * codec out of range; negative on error |
| 2153 | */ |
| 2154 | int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, |
| 2155 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2156 | uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) |
| 2157 | { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2158 | sbit_t iB[912], cB[456], h; |
| 2159 | ubit_t d[244], p[6], conv[250]; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2160 | int i, j, k, best = 0, rv, len, steal = 0, id = -1; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2161 | ubit_t cBd[456]; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2162 | *n_errors = 0; *n_bits_total = 0; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2163 | static ubit_t sid_first_dummy[64] = { 0 }; |
| 2164 | sbit_t sid_update_enc[256]; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2165 | |
| 2166 | for (i=0; i<8; i++) { |
| 2167 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 2168 | steal -= h; |
| 2169 | } |
| 2170 | |
| 2171 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2172 | |
| 2173 | if (steal > 0) { |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2174 | /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame. |
| 2175 | * It's normally updated by gsm0503_detect_afs_dtx_frame(), which is not |
| 2176 | * reached in case of FACCH. Reset it here to avoid FACCH/F frames being |
| 2177 | * misinterpreted as AMR's special DTX frames. */ |
| 2178 | if (dtx != NULL) |
| 2179 | *dtx = AMR_OTHER; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2180 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2181 | if (rv) { |
| 2182 | /* Error decoding FACCH frame */ |
| 2183 | return -1; |
| 2184 | } |
| 2185 | |
| 2186 | return GSM_MACBLOCK_LEN; |
| 2187 | } |
| 2188 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2189 | /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ |
| 2190 | if (dtx) { |
Vadim Yanitskiy | bf2d5e9 | 2022-05-15 15:58:10 +0300 | [diff] [blame] | 2191 | const enum gsm0503_amr_dtx_frames dtx_prev = *dtx; |
| 2192 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2193 | osmo_sbit2ubit(cBd, cB, 456); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2194 | *dtx = gsm0503_detect_afs_dtx_frame(n_errors, n_bits_total, cBd); |
| 2195 | |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2196 | switch (*dtx) { |
| 2197 | case AMR_OTHER: |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2198 | /* NOTE: The AFS_SID_UPDATE frame is splitted into |
| 2199 | * two half rate frames. If the id marker frame |
| 2200 | * (AFS_SID_UPDATE) is detected the following frame |
| 2201 | * contains the actual comfort noised data part of |
| 2202 | * (AFS_SID_UPDATE_CN). */ |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2203 | if (dtx_prev != AFS_SID_UPDATE) |
| 2204 | break; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2205 | /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2206 | *dtx = AFS_SID_UPDATE_CN; |
| 2207 | |
| 2208 | extract_afs_sid_update(sid_update_enc, cB); |
| 2209 | osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, |
| 2210 | sid_update_enc, conv, n_errors, |
| 2211 | n_bits_total); |
| 2212 | rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, |
| 2213 | 35, conv + 35); |
| 2214 | if (rv != 0) { |
| 2215 | /* Error checking CRC14 for an AMR SID_UPDATE frame */ |
| 2216 | return -1; |
| 2217 | } |
| 2218 | |
| 2219 | tch_amr_sid_update_append(conv, 1, |
| 2220 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2221 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2222 | tch_amr_reassemble(tch_data, conv, 39); |
| 2223 | len = 5; |
| 2224 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2225 | case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2226 | tch_amr_sid_update_append(sid_first_dummy, 0, |
| 2227 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2228 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2229 | tch_amr_reassemble(tch_data, conv, 39); |
| 2230 | len = 5; |
| 2231 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2232 | case AFS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */ |
| 2233 | case AFS_ONSET: /* TODO: parse CMI or CMC/CMR (N * 16 - M bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2234 | len = 0; |
| 2235 | goto out; |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2236 | default: |
| 2237 | break; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2241 | for (i = 0; i < 4; i++) { |
| 2242 | for (j = 0, k = 0; j < 8; j++) |
| 2243 | k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j])); |
| 2244 | |
| 2245 | if (i == 0 || k < best) { |
| 2246 | best = k; |
| 2247 | id = i; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | /* Check if indicated codec fits into range of codecs */ |
| 2252 | if (id >= codecs) { |
| 2253 | /* Codec mode out of range, return id */ |
| 2254 | return id; |
| 2255 | } |
| 2256 | |
| 2257 | switch ((codec_mode_req) ? codec[*ft] : codec[id]) { |
| 2258 | case 7: /* TCH/AFS12.2 */ |
| 2259 | osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8, |
| 2260 | conv, n_errors, n_bits_total); |
| 2261 | |
| 2262 | tch_amr_unmerge(d, p, conv, 244, 81); |
| 2263 | |
| 2264 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p); |
| 2265 | if (rv) { |
| 2266 | /* Error checking CRC8 for an AMR 12.2 frame */ |
| 2267 | return -1; |
| 2268 | } |
| 2269 | |
| 2270 | tch_amr_reassemble(tch_data, d, 244); |
| 2271 | |
| 2272 | len = 31; |
| 2273 | |
| 2274 | break; |
| 2275 | case 6: /* TCH/AFS10.2 */ |
| 2276 | osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8, |
| 2277 | conv, n_errors, n_bits_total); |
| 2278 | |
| 2279 | tch_amr_unmerge(d, p, conv, 204, 65); |
| 2280 | |
| 2281 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p); |
| 2282 | if (rv) { |
| 2283 | /* Error checking CRC8 for an AMR 10.2 frame */ |
| 2284 | return -1; |
| 2285 | } |
| 2286 | |
| 2287 | tch_amr_reassemble(tch_data, d, 204); |
| 2288 | |
| 2289 | len = 26; |
| 2290 | |
| 2291 | break; |
| 2292 | case 5: /* TCH/AFS7.95 */ |
| 2293 | osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8, |
| 2294 | conv, n_errors, n_bits_total); |
| 2295 | |
| 2296 | tch_amr_unmerge(d, p, conv, 159, 75); |
| 2297 | |
| 2298 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p); |
| 2299 | if (rv) { |
| 2300 | /* Error checking CRC8 for an AMR 7.95 frame */ |
| 2301 | return -1; |
| 2302 | } |
| 2303 | |
| 2304 | tch_amr_reassemble(tch_data, d, 159); |
| 2305 | |
| 2306 | len = 20; |
| 2307 | |
| 2308 | break; |
| 2309 | case 4: /* TCH/AFS7.4 */ |
| 2310 | osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8, |
| 2311 | conv, n_errors, n_bits_total); |
| 2312 | |
| 2313 | tch_amr_unmerge(d, p, conv, 148, 61); |
| 2314 | |
| 2315 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2316 | if (rv) { |
| 2317 | /* Error checking CRC8 for an AMR 7.4 frame */ |
| 2318 | return -1; |
| 2319 | } |
| 2320 | |
| 2321 | tch_amr_reassemble(tch_data, d, 148); |
| 2322 | |
| 2323 | len = 19; |
| 2324 | |
| 2325 | break; |
| 2326 | case 3: /* TCH/AFS6.7 */ |
| 2327 | osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8, |
| 2328 | conv, n_errors, n_bits_total); |
| 2329 | |
| 2330 | tch_amr_unmerge(d, p, conv, 134, 55); |
| 2331 | |
| 2332 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2333 | if (rv) { |
| 2334 | /* Error checking CRC8 for an AMR 6.7 frame */ |
| 2335 | return -1; |
| 2336 | } |
| 2337 | |
| 2338 | tch_amr_reassemble(tch_data, d, 134); |
| 2339 | |
| 2340 | len = 17; |
| 2341 | |
| 2342 | break; |
| 2343 | case 2: /* TCH/AFS5.9 */ |
| 2344 | osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8, |
| 2345 | conv, n_errors, n_bits_total); |
| 2346 | |
| 2347 | tch_amr_unmerge(d, p, conv, 118, 55); |
| 2348 | |
| 2349 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2350 | if (rv) { |
| 2351 | /* Error checking CRC8 for an AMR 5.9 frame */ |
| 2352 | return -1; |
| 2353 | } |
| 2354 | |
| 2355 | tch_amr_reassemble(tch_data, d, 118); |
| 2356 | |
| 2357 | len = 15; |
| 2358 | |
| 2359 | break; |
| 2360 | case 1: /* TCH/AFS5.15 */ |
| 2361 | osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8, |
| 2362 | conv, n_errors, n_bits_total); |
| 2363 | |
| 2364 | tch_amr_unmerge(d, p, conv, 103, 49); |
| 2365 | |
| 2366 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2367 | if (rv) { |
| 2368 | /* Error checking CRC8 for an AMR 5.15 frame */ |
| 2369 | return -1; |
| 2370 | } |
| 2371 | |
| 2372 | tch_amr_reassemble(tch_data, d, 103); |
| 2373 | |
| 2374 | len = 13; |
| 2375 | |
| 2376 | break; |
| 2377 | case 0: /* TCH/AFS4.75 */ |
| 2378 | osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8, |
| 2379 | conv, n_errors, n_bits_total); |
| 2380 | |
| 2381 | tch_amr_unmerge(d, p, conv, 95, 39); |
| 2382 | |
| 2383 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2384 | if (rv) { |
| 2385 | /* Error checking CRC8 for an AMR 4.75 frame */ |
| 2386 | return -1; |
| 2387 | } |
| 2388 | |
| 2389 | tch_amr_reassemble(tch_data, d, 95); |
| 2390 | |
| 2391 | len = 12; |
| 2392 | |
| 2393 | break; |
| 2394 | default: |
| 2395 | /* Unknown frame type */ |
| 2396 | *n_bits_total = 448; |
| 2397 | *n_errors = *n_bits_total; |
| 2398 | return -1; |
| 2399 | } |
| 2400 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2401 | out: |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2402 | /* Change codec request / indication, if frame is valid */ |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2403 | if (id != -1) { |
| 2404 | if (codec_mode_req) |
| 2405 | *cmr = id; |
| 2406 | else |
| 2407 | *ft = id; |
| 2408 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2409 | |
| 2410 | return len; |
| 2411 | } |
| 2412 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2413 | /*! Perform channel encoding on a TCH/AFS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2414 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2415 | * \param[in] tch_data Codec input data in RTP payload format |
| 2416 | * \param[in] len Length of \a tch_data in bytes |
| 2417 | * \param[in] codec_mode_req Use CMR (1) or FT (0) |
| 2418 | * \param[in] codec Array of codecs (active codec set) |
| 2419 | * \param[in] codecs Number of entries in \a codec |
| 2420 | * \param[in] ft Frame Type to be used for encoding (index to \a codec) |
| 2421 | * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only) |
| 2422 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2423 | int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2424 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft, |
| 2425 | uint8_t cmr) |
| 2426 | { |
| 2427 | ubit_t iB[912], cB[456], h; |
| 2428 | ubit_t d[244], p[6], conv[250]; |
| 2429 | int i; |
| 2430 | uint8_t id; |
| 2431 | |
| 2432 | if (len == GSM_MACBLOCK_LEN) { /* FACCH */ |
| 2433 | _xcch_encode_cB(cB, tch_data); |
| 2434 | |
| 2435 | h = 1; |
| 2436 | |
| 2437 | goto facch; |
| 2438 | } |
| 2439 | |
| 2440 | h = 0; |
| 2441 | |
| 2442 | if (codec_mode_req) { |
| 2443 | if (cmr >= codecs) { |
| 2444 | /* FIXME: CMR ID is not in codec list! */ |
| 2445 | return -1; |
| 2446 | } |
| 2447 | id = cmr; |
| 2448 | } else { |
| 2449 | if (ft >= codecs) { |
| 2450 | /* FIXME: FT ID is not in codec list! */ |
| 2451 | return -1; |
| 2452 | } |
| 2453 | id = ft; |
| 2454 | } |
| 2455 | |
| 2456 | switch (codec[ft]) { |
| 2457 | case 7: /* TCH/AFS12.2 */ |
| 2458 | if (len != 31) |
| 2459 | goto invalid_length; |
| 2460 | |
| 2461 | tch_amr_disassemble(d, tch_data, 244); |
| 2462 | |
| 2463 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p); |
| 2464 | |
| 2465 | tch_amr_merge(conv, d, p, 244, 81); |
| 2466 | |
| 2467 | osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8); |
| 2468 | |
| 2469 | break; |
| 2470 | case 6: /* TCH/AFS10.2 */ |
| 2471 | if (len != 26) |
| 2472 | goto invalid_length; |
| 2473 | |
| 2474 | tch_amr_disassemble(d, tch_data, 204); |
| 2475 | |
| 2476 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p); |
| 2477 | |
| 2478 | tch_amr_merge(conv, d, p, 204, 65); |
| 2479 | |
| 2480 | osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8); |
| 2481 | |
| 2482 | break; |
| 2483 | case 5: /* TCH/AFS7.95 */ |
| 2484 | if (len != 20) |
| 2485 | goto invalid_length; |
| 2486 | |
| 2487 | tch_amr_disassemble(d, tch_data, 159); |
| 2488 | |
| 2489 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p); |
| 2490 | |
| 2491 | tch_amr_merge(conv, d, p, 159, 75); |
| 2492 | |
| 2493 | osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8); |
| 2494 | |
| 2495 | break; |
| 2496 | case 4: /* TCH/AFS7.4 */ |
| 2497 | if (len != 19) |
| 2498 | goto invalid_length; |
| 2499 | |
| 2500 | tch_amr_disassemble(d, tch_data, 148); |
| 2501 | |
| 2502 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2503 | |
| 2504 | tch_amr_merge(conv, d, p, 148, 61); |
| 2505 | |
| 2506 | osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8); |
| 2507 | |
| 2508 | break; |
| 2509 | case 3: /* TCH/AFS6.7 */ |
| 2510 | if (len != 17) |
| 2511 | goto invalid_length; |
| 2512 | |
| 2513 | tch_amr_disassemble(d, tch_data, 134); |
| 2514 | |
| 2515 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2516 | |
| 2517 | tch_amr_merge(conv, d, p, 134, 55); |
| 2518 | |
| 2519 | osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8); |
| 2520 | |
| 2521 | break; |
| 2522 | case 2: /* TCH/AFS5.9 */ |
| 2523 | if (len != 15) |
| 2524 | goto invalid_length; |
| 2525 | |
| 2526 | tch_amr_disassemble(d, tch_data, 118); |
| 2527 | |
| 2528 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2529 | |
| 2530 | tch_amr_merge(conv, d, p, 118, 55); |
| 2531 | |
| 2532 | osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8); |
| 2533 | |
| 2534 | break; |
| 2535 | case 1: /* TCH/AFS5.15 */ |
| 2536 | if (len != 13) |
| 2537 | goto invalid_length; |
| 2538 | |
| 2539 | tch_amr_disassemble(d, tch_data, 103); |
| 2540 | |
| 2541 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2542 | |
| 2543 | tch_amr_merge(conv, d, p, 103, 49); |
| 2544 | |
| 2545 | osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8); |
| 2546 | |
| 2547 | break; |
| 2548 | case 0: /* TCH/AFS4.75 */ |
| 2549 | if (len != 12) |
| 2550 | goto invalid_length; |
| 2551 | |
| 2552 | tch_amr_disassemble(d, tch_data, 95); |
| 2553 | |
| 2554 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2555 | |
| 2556 | tch_amr_merge(conv, d, p, 95, 39); |
| 2557 | |
| 2558 | osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8); |
| 2559 | |
| 2560 | break; |
| 2561 | default: |
| 2562 | /* FIXME: FT %ft is not supported */ |
| 2563 | return -1; |
| 2564 | } |
| 2565 | |
| 2566 | memcpy(cB, gsm0503_afs_ic_ubit[id], 8); |
| 2567 | |
| 2568 | facch: |
| 2569 | gsm0503_tch_fr_interleave(cB, iB); |
| 2570 | |
| 2571 | for (i = 0; i < 8; i++) { |
| 2572 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2573 | &bursts[i * 116], &h, i >> 2); |
| 2574 | } |
| 2575 | |
| 2576 | return 0; |
| 2577 | |
| 2578 | invalid_length: |
| 2579 | /* FIXME: payload length %len does not comply with codec type %ft */ |
| 2580 | return -1; |
| 2581 | } |
| 2582 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2583 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2584 | * \param[out] tch_data Codec frame in RTP payload format |
| 2585 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2586 | * \param[in] odd Is this an odd (1) or even (0) frame number? |
| 2587 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2588 | * \param[in] codec array of active codecs (active codec set) |
| 2589 | * \param[in] codecs number of codecs in \a codec |
| 2590 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2591 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2592 | * \param[out] n_errors Number of detected bit errors |
| 2593 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 2594 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2595 | * codec out of range; negative on error |
| 2596 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2597 | int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2598 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2599 | uint8_t *cmr, int *n_errors, int *n_bits_total) |
| 2600 | { |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2601 | return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req, |
| 2602 | codec, codecs, ft, cmr, n_errors, |
| 2603 | n_bits_total, NULL); |
| 2604 | } |
| 2605 | |
| 2606 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
| 2607 | * \param[out] tch_data Codec frame in RTP payload format |
| 2608 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2609 | * \param[in] odd Is this an odd (1) or even (0) frame number? |
| 2610 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2611 | * \param[in] codec array of active codecs (active codec set) |
| 2612 | * \param[in] codecs number of codecs in \a codec |
| 2613 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2614 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2615 | * \param[out] n_errors Number of detected bit errors |
| 2616 | * \param[out] n_bits_total Total number of bits |
| 2617 | * \param[inout] dtx DTX frame type output, previous DTX frame type input |
| 2618 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2619 | * codec out of range; negative on error |
| 2620 | */ |
| 2621 | int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd, |
| 2622 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2623 | uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) |
| 2624 | { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2625 | sbit_t iB[912], cB[456], h; |
| 2626 | ubit_t d[244], p[6], conv[135]; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2627 | int i, j, k, best = 0, rv, len, steal = 0, id = -1; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2628 | ubit_t cBd[456]; |
| 2629 | static ubit_t sid_first_dummy[64] = { 0 }; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2630 | |
| 2631 | /* only unmap the stealing bits */ |
| 2632 | if (!odd) { |
| 2633 | for (i = 0; i < 4; i++) { |
| 2634 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0); |
| 2635 | steal -= h; |
| 2636 | } |
| 2637 | for (i = 2; i < 5; i++) { |
| 2638 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1); |
| 2639 | steal -= h; |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | /* if we found a stole FACCH, but only at correct alignment */ |
| 2644 | if (steal > 0) { |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2645 | /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame. |
| 2646 | * It's normally updated by gsm0503_detect_ahs_dtx_frame(), which is not |
| 2647 | * reached in case of FACCH. Reset it here to avoid FACCH/H frames being |
| 2648 | * misinterpreted as AMR's special DTX frames. */ |
| 2649 | if (dtx != NULL) |
| 2650 | *dtx = AMR_OTHER; |
| 2651 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2652 | for (i = 0; i < 6; i++) { |
| 2653 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2654 | &bursts[i * 116], NULL, i >> 2); |
| 2655 | } |
| 2656 | |
| 2657 | for (i = 2; i < 4; i++) { |
| 2658 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], |
| 2659 | &bursts[i * 116], NULL, 1); |
| 2660 | } |
| 2661 | |
| 2662 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2663 | |
| 2664 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2665 | if (rv) { |
| 2666 | /* Error decoding FACCH frame */ |
| 2667 | return -1; |
| 2668 | } |
| 2669 | |
| 2670 | return GSM_MACBLOCK_LEN; |
| 2671 | } |
| 2672 | |
| 2673 | for (i = 0; i < 4; i++) { |
| 2674 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2675 | &bursts[i * 116], NULL, i >> 1); |
| 2676 | } |
| 2677 | |
| 2678 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2679 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2680 | /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ |
| 2681 | if (dtx) { |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2682 | int n_bits_total_sid; |
| 2683 | int n_errors_sid; |
Vadim Yanitskiy | bf2d5e9 | 2022-05-15 15:58:10 +0300 | [diff] [blame] | 2684 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2685 | osmo_sbit2ubit(cBd, cB, 456); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2686 | *dtx = gsm0503_detect_ahs_dtx_frame(n_errors, n_bits_total, cBd); |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2687 | /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2688 | |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2689 | switch (*dtx) { |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2690 | case AHS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */ |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2691 | /* cB[] contains 16 bits of coded in-band data and 212 bits containing |
| 2692 | * the identification marker. We need to unmap/deinterleave 114 odd |
| 2693 | * bits from the last two blocks, 114 even bits from the first two |
| 2694 | * blocks and combine them together. */ |
| 2695 | gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0); |
| 2696 | gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0); |
| 2697 | gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1); |
| 2698 | gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1); |
| 2699 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2700 | |
| 2701 | /* cB[] is expected to contain 16 bits of coded in-band data and |
| 2702 | * 212 bits containing the coded data (53 bits coded at 1/4 rate). */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2703 | *dtx = AHS_SID_UPDATE_CN; |
| 2704 | |
| 2705 | osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2706 | cB + 16, conv, &n_errors_sid, |
| 2707 | &n_bits_total_sid); |
| 2708 | /* gsm0503_detect_ahs_dtx_frame() calculates BER for the marker, |
| 2709 | * osmo_conv_decode_ber() calculates BER for the coded data. */ |
| 2710 | if (n_errors != NULL) |
| 2711 | *n_errors += n_errors_sid; |
| 2712 | if (n_bits_total != NULL) |
| 2713 | *n_bits_total += n_bits_total_sid; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2714 | rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, |
| 2715 | 35, conv + 35); |
| 2716 | if (rv != 0) { |
| 2717 | /* Error checking CRC14 for an AMR SID_UPDATE frame */ |
| 2718 | return -1; |
| 2719 | } |
| 2720 | |
| 2721 | tch_amr_sid_update_append(conv, 1, |
| 2722 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2723 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2724 | tch_amr_reassemble(tch_data, conv, 39); |
| 2725 | len = 5; |
| 2726 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2727 | case AHS_SID_FIRST_P2: /* TODO: parse CMI or CMC/CMR (N * 16 - M bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2728 | tch_amr_sid_update_append(sid_first_dummy, 0, |
| 2729 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2730 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2731 | tch_amr_reassemble(tch_data, sid_first_dummy, 39); |
| 2732 | len = 5; |
| 2733 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2734 | case AHS_ONSET: /* TODO: parse CMI or CMC/CMR (N * 16 - M bit) */ |
| 2735 | case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
| 2736 | case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
| 2737 | case AHS_SID_FIRST_P1: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2738 | len = 0; |
| 2739 | goto out; |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2740 | default: |
| 2741 | break; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2745 | for (i = 0; i < 4; i++) { |
| 2746 | for (j = 0, k = 0; j < 4; j++) |
| 2747 | k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j])); |
| 2748 | |
| 2749 | if (i == 0 || k < best) { |
| 2750 | best = k; |
| 2751 | id = i; |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | /* Check if indicated codec fits into range of codecs */ |
| 2756 | if (id >= codecs) { |
| 2757 | /* Codec mode out of range, return id */ |
| 2758 | return id; |
| 2759 | } |
| 2760 | |
| 2761 | switch ((codec_mode_req) ? codec[*ft] : codec[id]) { |
| 2762 | case 5: /* TCH/AHS7.95 */ |
| 2763 | osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4, |
| 2764 | conv, n_errors, n_bits_total); |
| 2765 | |
| 2766 | tch_amr_unmerge(d, p, conv, 123, 67); |
| 2767 | |
| 2768 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p); |
| 2769 | if (rv) { |
| 2770 | /* Error checking CRC8 for an AMR 7.95 frame */ |
| 2771 | return -1; |
| 2772 | } |
| 2773 | |
| 2774 | for (i = 0; i < 36; i++) |
| 2775 | d[i + 123] = (cB[i + 192] < 0) ? 1 : 0; |
| 2776 | |
| 2777 | tch_amr_reassemble(tch_data, d, 159); |
| 2778 | |
| 2779 | len = 20; |
| 2780 | |
| 2781 | break; |
| 2782 | case 4: /* TCH/AHS7.4 */ |
| 2783 | osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4, |
| 2784 | conv, n_errors, n_bits_total); |
| 2785 | |
| 2786 | tch_amr_unmerge(d, p, conv, 120, 61); |
| 2787 | |
| 2788 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2789 | if (rv) { |
| 2790 | /* Error checking CRC8 for an AMR 7.4 frame */ |
| 2791 | return -1; |
| 2792 | } |
| 2793 | |
| 2794 | for (i = 0; i < 28; i++) |
| 2795 | d[i + 120] = (cB[i + 200] < 0) ? 1 : 0; |
| 2796 | |
| 2797 | tch_amr_reassemble(tch_data, d, 148); |
| 2798 | |
| 2799 | len = 19; |
| 2800 | |
| 2801 | break; |
| 2802 | case 3: /* TCH/AHS6.7 */ |
| 2803 | osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4, |
| 2804 | conv, n_errors, n_bits_total); |
| 2805 | |
| 2806 | tch_amr_unmerge(d, p, conv, 110, 55); |
| 2807 | |
| 2808 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2809 | if (rv) { |
| 2810 | /* Error checking CRC8 for an AMR 6.7 frame */ |
| 2811 | return -1; |
| 2812 | } |
| 2813 | |
| 2814 | for (i = 0; i < 24; i++) |
| 2815 | d[i + 110] = (cB[i + 204] < 0) ? 1 : 0; |
| 2816 | |
| 2817 | tch_amr_reassemble(tch_data, d, 134); |
| 2818 | |
| 2819 | len = 17; |
| 2820 | |
| 2821 | break; |
| 2822 | case 2: /* TCH/AHS5.9 */ |
| 2823 | osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4, |
| 2824 | conv, n_errors, n_bits_total); |
| 2825 | |
| 2826 | tch_amr_unmerge(d, p, conv, 102, 55); |
| 2827 | |
| 2828 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2829 | if (rv) { |
| 2830 | /* Error checking CRC8 for an AMR 5.9 frame */ |
| 2831 | return -1; |
| 2832 | } |
| 2833 | |
| 2834 | for (i = 0; i < 16; i++) |
| 2835 | d[i + 102] = (cB[i + 212] < 0) ? 1 : 0; |
| 2836 | |
| 2837 | tch_amr_reassemble(tch_data, d, 118); |
| 2838 | |
| 2839 | len = 15; |
| 2840 | |
| 2841 | break; |
| 2842 | case 1: /* TCH/AHS5.15 */ |
| 2843 | osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4, |
| 2844 | conv, n_errors, n_bits_total); |
| 2845 | |
| 2846 | tch_amr_unmerge(d, p, conv, 91, 49); |
| 2847 | |
| 2848 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2849 | if (rv) { |
| 2850 | /* Error checking CRC8 for an AMR 5.15 frame */ |
| 2851 | return -1; |
| 2852 | } |
| 2853 | |
| 2854 | for (i = 0; i < 12; i++) |
| 2855 | d[i + 91] = (cB[i + 216] < 0) ? 1 : 0; |
| 2856 | |
| 2857 | tch_amr_reassemble(tch_data, d, 103); |
| 2858 | |
| 2859 | len = 13; |
| 2860 | |
| 2861 | break; |
| 2862 | case 0: /* TCH/AHS4.75 */ |
| 2863 | osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4, |
| 2864 | conv, n_errors, n_bits_total); |
| 2865 | |
| 2866 | tch_amr_unmerge(d, p, conv, 83, 39); |
| 2867 | |
| 2868 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2869 | if (rv) { |
| 2870 | /* Error checking CRC8 for an AMR 4.75 frame */ |
| 2871 | return -1; |
| 2872 | } |
| 2873 | |
| 2874 | for (i = 0; i < 12; i++) |
| 2875 | d[i + 83] = (cB[i + 216] < 0) ? 1 : 0; |
| 2876 | |
| 2877 | tch_amr_reassemble(tch_data, d, 95); |
| 2878 | |
| 2879 | len = 12; |
| 2880 | |
| 2881 | break; |
| 2882 | default: |
| 2883 | /* Unknown frame type */ |
| 2884 | *n_bits_total = 159; |
| 2885 | *n_errors = *n_bits_total; |
| 2886 | return -1; |
| 2887 | } |
| 2888 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2889 | out: |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2890 | /* Change codec request / indication, if frame is valid */ |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2891 | if (id != -1) { |
| 2892 | if (codec_mode_req) |
| 2893 | *cmr = id; |
| 2894 | else |
| 2895 | *ft = id; |
| 2896 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2897 | |
| 2898 | return len; |
| 2899 | } |
| 2900 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2901 | /*! Perform channel encoding on a TCH/AHS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2902 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2903 | * \param[in] tch_data Codec input data in RTP payload format |
| 2904 | * \param[in] len Length of \a tch_data in bytes |
| 2905 | * \param[in] codec_mode_req Use CMR (1) or FT (0) |
| 2906 | * \param[in] codec Array of codecs (active codec set) |
| 2907 | * \param[in] codecs Number of entries in \a codec |
| 2908 | * \param[in] ft Frame Type to be used for encoding (index to \a codec) |
| 2909 | * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only) |
| 2910 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2911 | int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2912 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft, |
| 2913 | uint8_t cmr) |
| 2914 | { |
| 2915 | ubit_t iB[912], cB[456], h; |
| 2916 | ubit_t d[244], p[6], conv[135]; |
| 2917 | int i; |
| 2918 | uint8_t id; |
| 2919 | |
| 2920 | if (len == GSM_MACBLOCK_LEN) { /* FACCH */ |
| 2921 | _xcch_encode_cB(cB, tch_data); |
| 2922 | |
| 2923 | h = 1; |
| 2924 | |
| 2925 | gsm0503_tch_fr_interleave(cB, iB); |
| 2926 | |
| 2927 | for (i = 0; i < 6; i++) |
| 2928 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 2929 | &h, i >> 2); |
| 2930 | for (i = 2; i < 4; i++) |
| 2931 | gsm0503_tch_burst_map(&iB[i * 114 + 456], |
| 2932 | &bursts[i * 116], &h, 1); |
| 2933 | |
| 2934 | return 0; |
| 2935 | } |
| 2936 | |
| 2937 | h = 0; |
| 2938 | |
| 2939 | if (codec_mode_req) { |
| 2940 | if (cmr >= codecs) { |
| 2941 | /* FIXME: CMR ID %d not in codec list */ |
| 2942 | return -1; |
| 2943 | } |
| 2944 | id = cmr; |
| 2945 | } else { |
| 2946 | if (ft >= codecs) { |
| 2947 | /* FIXME: FT ID %d not in codec list */ |
| 2948 | return -1; |
| 2949 | } |
| 2950 | id = ft; |
| 2951 | } |
| 2952 | |
| 2953 | switch (codec[ft]) { |
| 2954 | case 5: /* TCH/AHS7.95 */ |
| 2955 | if (len != 20) |
| 2956 | goto invalid_length; |
| 2957 | |
| 2958 | tch_amr_disassemble(d, tch_data, 159); |
| 2959 | |
| 2960 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p); |
| 2961 | |
| 2962 | tch_amr_merge(conv, d, p, 123, 67); |
| 2963 | |
| 2964 | osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4); |
| 2965 | |
| 2966 | memcpy(cB + 192, d + 123, 36); |
| 2967 | |
| 2968 | break; |
| 2969 | case 4: /* TCH/AHS7.4 */ |
| 2970 | if (len != 19) |
| 2971 | goto invalid_length; |
| 2972 | |
| 2973 | tch_amr_disassemble(d, tch_data, 148); |
| 2974 | |
| 2975 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2976 | |
| 2977 | tch_amr_merge(conv, d, p, 120, 61); |
| 2978 | |
| 2979 | osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4); |
| 2980 | |
| 2981 | memcpy(cB + 200, d + 120, 28); |
| 2982 | |
| 2983 | break; |
| 2984 | case 3: /* TCH/AHS6.7 */ |
| 2985 | if (len != 17) |
| 2986 | goto invalid_length; |
| 2987 | |
| 2988 | tch_amr_disassemble(d, tch_data, 134); |
| 2989 | |
| 2990 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2991 | |
| 2992 | tch_amr_merge(conv, d, p, 110, 55); |
| 2993 | |
| 2994 | osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4); |
| 2995 | |
| 2996 | memcpy(cB + 204, d + 110, 24); |
| 2997 | |
| 2998 | break; |
| 2999 | case 2: /* TCH/AHS5.9 */ |
| 3000 | if (len != 15) |
| 3001 | goto invalid_length; |
| 3002 | |
| 3003 | tch_amr_disassemble(d, tch_data, 118); |
| 3004 | |
| 3005 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 3006 | |
| 3007 | tch_amr_merge(conv, d, p, 102, 55); |
| 3008 | |
| 3009 | osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4); |
| 3010 | |
| 3011 | memcpy(cB + 212, d + 102, 16); |
| 3012 | |
| 3013 | break; |
| 3014 | case 1: /* TCH/AHS5.15 */ |
| 3015 | if (len != 13) |
| 3016 | goto invalid_length; |
| 3017 | |
| 3018 | tch_amr_disassemble(d, tch_data, 103); |
| 3019 | |
| 3020 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p); |
| 3021 | |
| 3022 | tch_amr_merge(conv, d, p, 91, 49); |
| 3023 | |
| 3024 | osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4); |
| 3025 | |
| 3026 | memcpy(cB + 216, d + 91, 12); |
| 3027 | |
| 3028 | break; |
| 3029 | case 0: /* TCH/AHS4.75 */ |
| 3030 | if (len != 12) |
| 3031 | goto invalid_length; |
| 3032 | |
| 3033 | tch_amr_disassemble(d, tch_data, 95); |
| 3034 | |
| 3035 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p); |
| 3036 | |
| 3037 | tch_amr_merge(conv, d, p, 83, 39); |
| 3038 | |
| 3039 | osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4); |
| 3040 | |
| 3041 | memcpy(cB + 216, d + 83, 12); |
| 3042 | |
| 3043 | break; |
| 3044 | default: |
| 3045 | /* FIXME: FT %ft is not supported */ |
| 3046 | return -1; |
| 3047 | } |
| 3048 | |
Philipp Maier | cfea39b | 2021-08-31 16:02:31 +0200 | [diff] [blame] | 3049 | memcpy(cB, gsm0503_ahs_ic_ubit[id], 4); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3050 | |
| 3051 | gsm0503_tch_hr_interleave(cB, iB); |
| 3052 | |
| 3053 | for (i = 0; i < 4; i++) |
| 3054 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1); |
| 3055 | |
| 3056 | return 0; |
| 3057 | |
| 3058 | invalid_length: |
| 3059 | /* FIXME: payload length %len does not comply with codec type %ft */ |
| 3060 | return -1; |
| 3061 | } |
| 3062 | |
| 3063 | /* |
| 3064 | * GSM RACH transcoding |
| 3065 | */ |
| 3066 | |
| 3067 | /* |
| 3068 | * GSM RACH apply BSIC to parity |
| 3069 | * |
| 3070 | * p(j) = p(j) xor b(j) j = 0, ..., 5 |
| 3071 | * b(0) = MSB of PLMN colour code |
| 3072 | * b(5) = LSB of BS colour code |
| 3073 | */ |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3074 | static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3075 | { |
| 3076 | int i; |
| 3077 | |
| 3078 | /* Apply it */ |
| 3079 | for (i = 0; i < 6; i++) |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3080 | d[start + i] ^= ((bsic >> (5 - i)) & 1); |
| 3081 | } |
| 3082 | |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3083 | static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit, |
| 3084 | int *n_errors, int *n_bits_total) |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3085 | { |
| 3086 | ubit_t conv[17]; |
| 3087 | uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8; |
| 3088 | int rv; |
| 3089 | |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3090 | osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv, |
| 3091 | n_errors, n_bits_total); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3092 | |
| 3093 | rach_apply_bsic(conv, bsic, nbits); |
| 3094 | |
| 3095 | rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits); |
| 3096 | if (rv) |
| 3097 | return -1; |
| 3098 | |
| 3099 | osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1); |
| 3100 | |
Vadim Yanitskiy | 9e713f3 | 2020-03-31 19:40:09 +0700 | [diff] [blame] | 3101 | return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0]; |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | /*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003 |
| 3105 | * \param[out] ra output buffer for RACH data |
| 3106 | * \param[in] burst Input burst data |
| 3107 | * \param[in] bsic BSIC used in this cell |
| 3108 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3109 | int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic) |
| 3110 | { |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3111 | int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3112 | |
| 3113 | if (r < 0) |
| 3114 | return r; |
| 3115 | |
| 3116 | *ra = r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3117 | |
| 3118 | return 0; |
| 3119 | } |
| 3120 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3121 | /*! Decode the (8-bit) RACH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3122 | * \param[out] ra output buffer for RACH data |
| 3123 | * \param[in] burst Input burst data |
| 3124 | * \param[in] bsic BSIC used in this cell |
| 3125 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3126 | int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3127 | { |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3128 | int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL); |
| 3129 | if (r < 0) |
| 3130 | return r; |
| 3131 | |
| 3132 | *ra = r; |
| 3133 | return 0; |
| 3134 | } |
| 3135 | |
| 3136 | /*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003 |
| 3137 | * \param[out] ra output buffer for RACH data |
| 3138 | * \param[in] burst Input burst data |
| 3139 | * \param[in] bsic BSIC used in this cell |
| 3140 | * \param[out] n_errors Number of detected bit errors |
| 3141 | * \param[out] n_bits_total Total number of bits |
| 3142 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3143 | int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic, |
| 3144 | int *n_errors, int *n_bits_total) |
| 3145 | { |
| 3146 | int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total); |
| 3147 | if (r < 0) |
| 3148 | return r; |
| 3149 | |
| 3150 | *ra = r; |
| 3151 | return 0; |
| 3152 | } |
| 3153 | |
| 3154 | /*! Decode the (8-bit) RACH according to TS 05.03 |
| 3155 | * \param[out] ra output buffer for RACH data |
| 3156 | * \param[in] burst Input burst data |
| 3157 | * \param[in] bsic BSIC used in this cell |
| 3158 | * \param[out] n_errors Number of detected bit errors |
| 3159 | * \param[out] n_bits_total Total number of bits |
| 3160 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3161 | int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic, |
| 3162 | int *n_errors, int *n_bits_total) |
| 3163 | { |
| 3164 | int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3165 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3166 | if (r < 0) |
| 3167 | return r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3168 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3169 | *ra = r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3170 | |
| 3171 | return 0; |
| 3172 | } |
| 3173 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3174 | /*! Encode the (8-bit) RACH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3175 | * \param[out] burst Caller-allocated output burst buffer |
| 3176 | * \param[in] ra Input RACH data |
| 3177 | * \param[in] bsic BSIC used in this cell |
| 3178 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3179 | int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3180 | { |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3181 | return gsm0503_rach_ext_encode(burst, *ra, bsic, false); |
| 3182 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3183 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3184 | /*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003 |
| 3185 | * \param[out] burst Caller-allocated output burst buffer |
| 3186 | * \param[in] ra11 Input RACH data |
| 3187 | * \param[in] bsic BSIC used in this cell |
| 3188 | * \param[in] is_11bit whether given RA is 11 bit or not |
| 3189 | * \returns 0 on success; negative on error */ |
| 3190 | int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit) |
| 3191 | { |
| 3192 | ubit_t conv[17]; |
| 3193 | uint8_t ra[2] = { 0 }, nbits = 8; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3194 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3195 | if (is_11bit) { |
Vadim Yanitskiy | 9e713f3 | 2020-03-31 19:40:09 +0700 | [diff] [blame] | 3196 | ra[0] = (uint8_t) (ra11 >> 3); |
| 3197 | ra[1] = (uint8_t) (ra11 & 0x07); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3198 | nbits = 11; |
| 3199 | } else |
| 3200 | ra[0] = (uint8_t)ra11; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3201 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3202 | osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3203 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3204 | osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits); |
| 3205 | |
| 3206 | rach_apply_bsic(conv, bsic, nbits); |
| 3207 | |
| 3208 | osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3209 | |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | /* |
| 3214 | * GSM SCH transcoding |
| 3215 | */ |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3216 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3217 | /*! Decode the SCH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3218 | * \param[out] sb_info output buffer for SCH data |
| 3219 | * \param[in] burst Input burst data |
| 3220 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3221 | int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3222 | { |
| 3223 | ubit_t conv[35]; |
| 3224 | int rv; |
| 3225 | |
| 3226 | osmo_conv_decode(&gsm0503_sch, burst, conv); |
| 3227 | |
| 3228 | rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25); |
| 3229 | if (rv) |
| 3230 | return -1; |
| 3231 | |
| 3232 | osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1); |
| 3233 | |
| 3234 | return 0; |
| 3235 | } |
| 3236 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3237 | /*! Encode the SCH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3238 | * \param[out] burst Caller-allocated output burst buffer |
| 3239 | * \param[in] sb_info Input SCH data |
| 3240 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3241 | int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3242 | { |
| 3243 | ubit_t conv[35]; |
| 3244 | |
| 3245 | osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1); |
| 3246 | |
| 3247 | osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25); |
| 3248 | |
| 3249 | osmo_conv_encode(&gsm0503_sch, conv, burst); |
| 3250 | |
| 3251 | return 0; |
| 3252 | } |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3253 | |
| 3254 | /*! @} */ |