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