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