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