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