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