blob: c94bca7ecfa8aa9e0c2d81ae7209dedab6ac1e56 [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
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200531/*! Convolutional Decode + compute BER
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
537 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700538static int osmo_conv_decode_ber(const struct osmo_conv_code *code,
539 const sbit_t *input, ubit_t *output,
540 int *n_errors, int *n_bits_total)
541{
542 int res, i, coded_len;
543 ubit_t recoded[EGPRS_DATA_C_MAX];
544
545 res = osmo_conv_decode(code, input, output);
546
547 if (n_bits_total || n_errors) {
548 coded_len = osmo_conv_encode(code, output, recoded);
549 OSMO_ASSERT(sizeof(recoded) / sizeof(recoded[0]) >= coded_len);
550 }
551
552 /* Count bit errors */
553 if (n_errors) {
554 *n_errors = 0;
555 for (i = 0; i < coded_len; i++) {
556 if (!((recoded[i] && input[i] < 0) ||
557 (!recoded[i] && input[i] > 0)) )
558 *n_errors += 1;
559 }
560 }
561
562 if (n_bits_total)
563 *n_bits_total = coded_len;
564
565 return res;
566}
567
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200568/*! convenience wrapper for decoding coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200569 * \param[out] l2_data caller-allocated buffer for L2 Frame
570 * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3
571 * \param[out] n_errors Number of detected errors
572 * \param[out] n_bits_total Number of total coded bits
573 * \returns 0 on success; -1 on CRC error */
Harald Welteb9946d32017-06-12 09:40:16 +0200574static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700575 int *n_errors, int *n_bits_total)
576{
577 ubit_t conv[224];
578 int rv;
579
580 osmo_conv_decode_ber(&gsm0503_xcch, cB,
581 conv, n_errors, n_bits_total);
582
583 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
584 conv, 184, conv + 184);
585 if (rv)
586 return -1;
587
588 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
589
590 return 0;
591}
592
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200593/*! convenience wrapper for encoding to coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200594 * \param[out] cB caller-allocated buffer for 456 coded bits as per TS 05.03 4.1.3
595 * \param[out] l2_data to-be-encoded L2 Frame
596 * \returns 0 */
Harald Welteb9946d32017-06-12 09:40:16 +0200597static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700598{
599 ubit_t conv[224];
600
601 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
602
603 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
604
605 osmo_conv_encode(&gsm0503_xcch, conv, cB);
606
607 return 0;
608}
609
610/*
611 * GSM xCCH block transcoding
612 */
Harald Weltec6636782017-06-12 14:59:37 +0200613
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200614/*! Decoding of xCCH data from bursts to L2 frame
Harald Weltec6636782017-06-12 14:59:37 +0200615 * \param[out] l2_data caller-allocated output data buffer
616 * \param[in] bursts four GSM bursts in soft-bits
617 * \param[out] n_errors Number of detected errors
618 * \param[out] n_bits_total Number of total coded bits
619 */
Harald Welteb9946d32017-06-12 09:40:16 +0200620int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700621 int *n_errors, int *n_bits_total)
622{
623 sbit_t iB[456], cB[456];
624 int i;
625
626 for (i = 0; i < 4; i++)
627 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL);
628
629 gsm0503_xcch_deinterleave(cB, iB);
630
631 return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total);
632}
633
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200634/*! Encoding of xCCH data from L2 frame to bursts
Harald Weltec6636782017-06-12 14:59:37 +0200635 * \param[out] bursts caller-allocated burst data (unpacked bits)
636 * \param[in] l2_data L2 input data (MAC block)
637 * \returns 0
638 */
Harald Welteb9946d32017-06-12 09:40:16 +0200639int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700640{
641 ubit_t iB[456], cB[456], hl = 1, hn = 1;
642 int i;
643
644 _xcch_encode_cB(cB, l2_data);
645
646 gsm0503_xcch_interleave(cB, iB);
647
648 for (i = 0; i < 4; i++)
649 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn);
650
651 return 0;
652}
653
654/*
655 * EGPRS PDTCH UL block decoding
656 */
657
658/*
659 * Type 3 - MCS-1,2,3,4
660 * Unmapping and deinterleaving
661 */
662static int egprs_type3_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
663{
664 int i;
665 sbit_t iB[456], q[8];
666
667 for (i = 0; i < 4; i++) {
668 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
669 q + i * 2, q + i * 2 + 1);
670 }
671
672 gsm0503_mcs1_ul_deinterleave(hc, dc, iB);
673
674 return 0;
675}
676
677/*
678 * Type 2 - MCS-5,6
679 * Unmapping and deinterleaving
680 */
681static int egprs_type2_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
682{
683 int i;
684 sbit_t burst[348];
685 sbit_t hi[EGPRS_HDR_HC_MAX];
686 sbit_t di[EGPRS_DATA_DC_MAX];
687
688 for (i = 0; i < 4; i++) {
689 memcpy(burst, &bursts[i * 348], 348);
690
691 gsm0503_mcs5_burst_swap(burst);
692 gsm0503_mcs5_ul_burst_unmap(di, burst, hi, i);
693 }
694
695 gsm0503_mcs5_ul_deinterleave(hc, dc, hi, di);
696
697 return 0;
698}
699
700/*
701 * Type 1 - MCS-7,8,9
702 * Unmapping and deinterleaving - Note that MCS-7 interleaver is unique
703 */
704static int egprs_type1_unmap(const sbit_t *bursts, sbit_t *hc,
705 sbit_t *c1, sbit_t *c2, int msc)
706{
707 int i;
708 sbit_t burst[348];
709 sbit_t hi[EGPRS_HDR_HC_MAX];
710 sbit_t di[EGPRS_DATA_C1 * 2];
711
712 for (i = 0; i < 4; i++) {
713 memcpy(burst, &bursts[i * 348], 348);
714
715 gsm0503_mcs5_burst_swap(burst);
716 gsm0503_mcs7_ul_burst_unmap(di, burst, hi, i);
717 }
718
719 if (msc == EGPRS_MCS7)
720 gsm0503_mcs7_ul_deinterleave(hc, c1, c2, hi, di);
721 else
722 gsm0503_mcs8_ul_deinterleave(hc, c1, c2, hi, di);
723
724 return 0;
725}
726
727/*
728 * Decode EGPRS UL header section
729 *
730 * 1. Depuncture
731 * 2. Convolutional decoding
732 * 3. CRC check
733 */
734static int _egprs_decode_hdr(const sbit_t *hc, int mcs,
735 union gprs_rlc_ul_hdr_egprs *hdr)
736{
737 sbit_t C[EGPRS_HDR_C_MAX];
738 ubit_t upp[EGPRS_HDR_UPP_MAX];
739 int i, j, rc;
Harald Welte2f984ea2017-06-12 15:05:21 +0200740 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700741
742 code = &gsm0503_mcs_ul_codes[mcs];
743
744 /* Skip depuncturing on MCS-5,6 header */
745 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
746 memcpy(C, hc, code->hdr_code_len);
747 goto hdr_conv_decode;
748 }
749
750 if (!code->hdr_punc) {
751 /* Invalid MCS-X header puncture matrix */
752 return -1;
753 }
754
755 i = code->hdr_code_len - 1;
756 j = code->hdr_punc_len - 1;
757
758 for (; i >= 0; i--) {
759 if (!code->hdr_punc[i])
760 C[i] = hc[j--];
761 else
762 C[i] = 0;
763 }
764
765hdr_conv_decode:
766 osmo_conv_decode_ber(code->hdr_conv, C, upp, NULL, NULL);
767 rc = osmo_crc8gen_check_bits(&gsm0503_mcs_crc8_hdr, upp,
768 code->hdr_len, upp + code->hdr_len);
769 if (rc)
770 return -1;
771
772 osmo_ubit2pbit_ext((pbit_t *) hdr, 0, upp, 0, code->hdr_len, 1);
773
774 return 0;
775}
776
777/*
778 * Blind MCS header decoding based on burst length and CRC validation.
779 * Ignore 'q' value coding identification. This approach provides
780 * the strongest chance of header recovery.
781 */
782static int egprs_decode_hdr(union gprs_rlc_ul_hdr_egprs *hdr,
783 const sbit_t *bursts, uint16_t nbits)
784{
785 int rc;
786 sbit_t hc[EGPRS_HDR_HC_MAX];
787
788 if (nbits == GSM0503_GPRS_BURSTS_NBITS) {
789 /* MCS-1,2,3,4 */
790 egprs_type3_unmap(bursts, hc, NULL);
791 rc = _egprs_decode_hdr(hc, EGPRS_MCS1, hdr);
792 if (!rc)
793 return EGPRS_HDR_TYPE3;
794 } else if (nbits == GSM0503_EGPRS_BURSTS_NBITS) {
795 /* MCS-5,6 */
796 egprs_type2_unmap(bursts, hc, NULL);
797 rc = _egprs_decode_hdr(hc, EGPRS_MCS5, hdr);
798 if (!rc)
799 return EGPRS_HDR_TYPE2;
800
801 /* MCS-7,8,9 */
802 egprs_type1_unmap(bursts, hc, NULL, NULL, EGPRS_MCS7);
803 rc = _egprs_decode_hdr(hc, EGPRS_MCS7, hdr);
804 if (!rc)
805 return EGPRS_HDR_TYPE1;
806 }
807
808 return -1;
809}
810
811/*
812 * Parse EGPRS UL header for coding and puncturing scheme (CPS)
813 *
814 * Type 1 - MCS-7,8,9
815 * Type 2 - MCS-5,6
816 * Type 3 - MCS-1,2,3,4
817 */
818static int egprs_parse_ul_cps(struct egprs_cps *cps,
819 union gprs_rlc_ul_hdr_egprs *hdr, int type)
820{
821 uint8_t bits;
822
823 switch (type) {
824 case EGPRS_HDR_TYPE1:
825 bits = hdr->type1.cps;
826 break;
827 case EGPRS_HDR_TYPE2:
828 bits = (hdr->type2.cps_lo << 2) | hdr->type2.cps_hi;
829 break;
830 case EGPRS_HDR_TYPE3:
831 bits = (hdr->type3.cps_lo << 2) | hdr->type3.cps_hi;
832 break;
833 default:
834 return -1;
835 }
836
837 return egprs_get_cps(cps, type, bits);
838}
839
840/*
841 * Decode EGPRS UL data section
842 *
843 * 1. Depuncture
844 * 2. Convolutional decoding
845 * 3. CRC check
846 * 4. Block combining (MCS-7,8,9 only)
847 */
Harald Welteb9946d32017-06-12 09:40:16 +0200848static int egprs_decode_data(uint8_t *l2_data, const sbit_t *c,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700849 int mcs, int p, int blk, int *n_errors, int *n_bits_total)
850{
851 ubit_t u[EGPRS_DATA_U_MAX];
852 sbit_t C[EGPRS_DATA_C_MAX];
853
854 int i, j, rc, data_len;
Harald Welte2f984ea2017-06-12 15:05:21 +0200855 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700856
857 if (blk && mcs < EGPRS_MCS7) {
858 /* Invalid MCS-X block state */
859 return -1;
860 }
861
862 code = &gsm0503_mcs_ul_codes[mcs];
863 if (!code->data_punc[p]) {
864 /* Invalid MCS-X data puncture matrix */
865 return -1;
866 }
867
868 /*
869 * MCS-1,6 - single block processing
870 * MCS-7,9 - dual block processing
871 */
872 if (mcs >= EGPRS_MCS7)
873 data_len = code->data_len / 2;
874 else
875 data_len = code->data_len;
876
877 i = code->data_code_len - 1;
878 j = code->data_punc_len - 1;
879
880 for (; i >= 0; i--) {
881 if (!code->data_punc[p][i])
882 C[i] = c[j--];
883 else
884 C[i] = 0;
885 }
886
887 osmo_conv_decode_ber(code->data_conv, C, u, n_errors, n_bits_total);
888 rc = osmo_crc16gen_check_bits(&gsm0503_mcs_crc12, u,
889 data_len, u + data_len);
890 if (rc)
891 return -1;
892
893 /* Offsets output pointer on the second block of Type 1 MCS */
894 osmo_ubit2pbit_ext(l2_data, code->hdr_len + blk * data_len,
895 u, 0, data_len, 1);
896
897 /* Return the number of bytes required for the bit message */
Maxdd75bac2017-06-13 15:07:01 +0200898 return OSMO_BYTES_FOR_BITS(code->hdr_len + code->data_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700899}
900
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200901/*! Decode EGPRS UL message
Harald Weltec6636782017-06-12 14:59:37 +0200902 * 1. Header section decoding
903 * 2. Extract CPS settings
904 * 3. Burst unmapping and deinterleaving
905 * 4. Data section decoding
906 * \param[out] l2_data caller-allocated buffer for L2 Frame
907 * \param[in] bursts burst input data as soft unpacked bits
908 * \param[in] nbits number of bits in \a bursts
909 * \param usf_p unused argument ?!?
910 * \param[out] n_errors number of detected bit-errors
911 * \param[out] n_bits_total total number of dcoded bits
912 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +0200913int gsm0503_pdtch_egprs_decode(uint8_t *l2_data, const sbit_t *bursts, uint16_t nbits,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700914 uint8_t *usf_p, int *n_errors, int *n_bits_total)
915{
916 sbit_t dc[EGPRS_DATA_DC_MAX];
917 sbit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
918 int type, rc;
919 struct egprs_cps cps;
920 union gprs_rlc_ul_hdr_egprs *hdr;
921
922 if ((nbits != GSM0503_GPRS_BURSTS_NBITS) &&
923 (nbits != GSM0503_EGPRS_BURSTS_NBITS)) {
924 /* Invalid EGPRS bit length */
Maxc8cf8202017-05-22 16:07:04 +0200925 return -EOVERFLOW;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700926 }
927
928 hdr = (union gprs_rlc_ul_hdr_egprs *) l2_data;
929 type = egprs_decode_hdr(hdr, bursts, nbits);
930 if (egprs_parse_ul_cps(&cps, hdr, type) < 0)
Maxc8cf8202017-05-22 16:07:04 +0200931 return -EIO;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700932
933 switch (cps.mcs) {
Maxc8cf8202017-05-22 16:07:04 +0200934 case EGPRS_MCS0:
935 return -ENOTSUP;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700936 case EGPRS_MCS1:
937 case EGPRS_MCS2:
938 case EGPRS_MCS3:
939 case EGPRS_MCS4:
940 egprs_type3_unmap(bursts, NULL, dc);
941 break;
942 case EGPRS_MCS5:
943 case EGPRS_MCS6:
944 egprs_type2_unmap(bursts, NULL, dc);
945 break;
946 case EGPRS_MCS7:
947 case EGPRS_MCS8:
948 case EGPRS_MCS9:
949 egprs_type1_unmap(bursts, NULL, c1, c2, cps.mcs);
950 break;
951 default:
952 /* Invalid MCS-X */
Maxc8cf8202017-05-22 16:07:04 +0200953 return -EINVAL;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700954 }
955
956 /* Decode MCS-X block, where X = cps.mcs */
957 if (cps.mcs < EGPRS_MCS7) {
958 rc = egprs_decode_data(l2_data, dc, cps.mcs, cps.p[0],
959 0, n_errors, n_bits_total);
960 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200961 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700962 } else {
963 /* MCS-7,8,9 block 1 */
964 rc = egprs_decode_data(l2_data, c1, cps.mcs, cps.p[0],
965 0, n_errors, n_bits_total);
966 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200967 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700968
969 /* MCS-7,8,9 block 2 */
970 rc = egprs_decode_data(l2_data, c2, cps.mcs, cps.p[1],
971 1, n_errors, n_bits_total);
972 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200973 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700974 }
975
976 return rc;
977}
978
979/*
980 * GSM PDTCH block transcoding
981 */
982
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200983/*! Decode GPRS PDTCH
Harald Weltec6636782017-06-12 14:59:37 +0200984 * \param[out] l2_data caller-allocated buffer for L2 Frame
985 * \param[in] bursts burst input data as soft unpacked bits
986 * \param[out] usf_p uplink stealing flag
987 * \param[out] n_errors number of detected bit-errors
988 * \param[out] n_bits_total total number of dcoded bits
989 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +0200990int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700991 int *n_errors, int *n_bits_total)
992{
993 sbit_t iB[456], cB[676], hl_hn[8];
994 ubit_t conv[456];
995 int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy */
996
997 for (i = 0; i < 4; i++)
998 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
999 hl_hn + i * 2, hl_hn + i * 2 + 1);
1000
1001 for (i = 0; i < 4; i++) {
1002 for (j = 0, k = 0; j < 8; j++)
1003 k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j]));
1004
1005 if (i == 0 || k < best) {
1006 best = k;
1007 cs = i + 1;
1008 }
1009 }
1010
1011 gsm0503_xcch_deinterleave(cB, iB);
1012
1013 switch (cs) {
1014 case 1:
1015 osmo_conv_decode_ber(&gsm0503_xcch, cB,
1016 conv, n_errors, n_bits_total);
1017
1018 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
1019 conv, 184, conv + 184);
1020 if (rv)
1021 return -1;
1022
1023 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
1024
1025 return 23;
1026 case 2:
1027 for (i = 587, j = 455; i >= 0; i--) {
1028 if (!gsm0503_puncture_cs2[i])
1029 cB[i] = cB[j--];
1030 else
1031 cB[i] = 0;
1032 }
1033
1034 osmo_conv_decode_ber(&gsm0503_cs2_np, cB,
1035 conv, n_errors, n_bits_total);
1036
1037 for (i = 0; i < 8; i++) {
1038 for (j = 0, k = 0; j < 6; j++)
1039 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1040
1041 if (i == 0 || k < best) {
1042 best = k;
1043 usf = i;
1044 }
1045 }
1046
1047 conv[3] = usf & 1;
1048 conv[4] = (usf >> 1) & 1;
1049 conv[5] = (usf >> 2) & 1;
1050 if (usf_p)
1051 *usf_p = usf;
1052
1053 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1054 conv + 3, 271, conv + 3 + 271);
1055 if (rv)
1056 return -1;
1057
1058 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
1059
1060 return 34;
1061 case 3:
1062 for (i = 675, j = 455; i >= 0; i--) {
1063 if (!gsm0503_puncture_cs3[i])
1064 cB[i] = cB[j--];
1065 else
1066 cB[i] = 0;
1067 }
1068
1069 osmo_conv_decode_ber(&gsm0503_cs3_np, cB,
1070 conv, n_errors, n_bits_total);
1071
1072 for (i = 0; i < 8; i++) {
1073 for (j = 0, k = 0; j < 6; j++)
1074 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1075
1076 if (i == 0 || k < best) {
1077 best = k;
1078 usf = i;
1079 }
1080 }
1081
1082 conv[3] = usf & 1;
1083 conv[4] = (usf >> 1) & 1;
1084 conv[5] = (usf >> 2) & 1;
1085 if (usf_p)
1086 *usf_p = usf;
1087
1088 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1089 conv + 3, 315, conv + 3 + 315);
1090 if (rv)
1091 return -1;
1092
1093 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1);
1094
1095 return 40;
1096 case 4:
1097 for (i = 12; i < 456; i++)
1098 conv[i] = (cB[i] < 0) ? 1 : 0;
1099
1100 for (i = 0; i < 8; i++) {
1101 for (j = 0, k = 0; j < 12; j++)
1102 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
1103
1104 if (i == 0 || k < best) {
1105 best = k;
1106 usf = i;
1107 }
1108 }
1109
1110 conv[9] = usf & 1;
1111 conv[10] = (usf >> 1) & 1;
1112 conv[11] = (usf >> 2) & 1;
1113 if (usf_p)
1114 *usf_p = usf;
1115
1116 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1117 conv + 9, 431, conv + 9 + 431);
1118 if (rv) {
1119 *n_bits_total = 456 - 12;
1120 *n_errors = *n_bits_total;
1121 return -1;
1122 }
1123
1124 *n_bits_total = 456 - 12;
1125 *n_errors = 0;
1126
1127 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
1128
1129 return 54;
1130 default:
1131 *n_bits_total = 0;
1132 *n_errors = 0;
1133 break;
1134 }
1135
1136 return -1;
1137}
1138
1139/*
1140 * EGPRS PDTCH UL block encoding
1141 */
Harald Welteb9946d32017-06-12 09:40:16 +02001142static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001143{
1144 int i;
1145 ubit_t iB[456];
1146 const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1147
1148 gsm0503_mcs1_dl_interleave(gsm0503_usf2six[usf], hc, dc, iB);
1149
1150 for (i = 0; i < 4; i++) {
1151 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1152 hl_hn + i * 2, hl_hn + i * 2 + 1);
1153 }
1154
1155 return 0;
1156}
1157
Harald Welteb9946d32017-06-12 09:40:16 +02001158static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001159{
1160 int i;
1161 const ubit_t *up;
1162 ubit_t hi[EGPRS_HDR_HC_MAX];
1163 ubit_t di[EGPRS_DATA_DC_MAX];
1164
1165 gsm0503_mcs5_dl_interleave(hc, dc, hi, di);
1166 up = gsm0503_mcs5_usf_precode_table[usf];
1167
1168 for (i = 0; i < 4; i++) {
1169 gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1170 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1171 }
1172
1173 return 0;
1174}
1175
Harald Welteb9946d32017-06-12 09:40:16 +02001176static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc,
1177 const ubit_t *c1, const ubit_t *c2, int usf, int mcs)
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_C1 * 2];
1183
1184 if (mcs == EGPRS_MCS7)
1185 gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di);
1186 else
1187 gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di);
1188
1189 up = gsm0503_mcs5_usf_precode_table[usf];
1190
1191 for (i = 0; i < 4; i++) {
1192 gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1193 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1194 }
1195
1196 return 0;
1197}
1198
Harald Welteb9946d32017-06-12 09:40:16 +02001199static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001200{
1201 int i, j;
1202 ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001203 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001204
1205 code = &gsm0503_mcs_dl_codes[mcs];
1206
1207 osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1);
1208 osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp,
1209 code->hdr_len, upp + code->hdr_len);
1210
1211 osmo_conv_encode(code->hdr_conv, upp, C);
1212
1213 /* MCS-5,6 header direct puncture instead of table */
1214 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
1215 memcpy(hc, C, code->hdr_code_len);
1216 hc[99] = hc[98];
1217 return 0;
1218 }
1219
1220 if (!code->hdr_punc) {
1221 /* Invalid MCS-X header puncture matrix */
1222 return -1;
1223 }
1224
1225 for (i = 0, j = 0; i < code->hdr_code_len; i++) {
1226 if (!code->hdr_punc[i])
1227 hc[j++] = C[i];
1228 }
1229
1230 return 0;
1231}
1232
Harald Welteb9946d32017-06-12 09:40:16 +02001233static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001234 int mcs, int p, int blk)
1235{
1236 int i, j, data_len;
1237 ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001238 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001239
1240 code = &gsm0503_mcs_dl_codes[mcs];
1241
1242 /*
1243 * Dual block - MCS-7,8,9
1244 * Single block - MCS-1,2,3,4,5,6
1245 */
1246 if (mcs >= EGPRS_MCS7)
1247 data_len = code->data_len / 2;
1248 else
1249 data_len = code->data_len;
1250
1251 osmo_pbit2ubit_ext(u, 0, l2_data,
1252 code->usf_len + code->hdr_len + blk * data_len, data_len, 1);
1253
1254 osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len);
1255
1256 osmo_conv_encode(code->data_conv, u, C);
1257
1258 if (!code->data_punc[p]) {
1259 /* Invalid MCS-X data puncture matrix */
1260 return -1;
1261 }
1262
1263 for (i = 0, j = 0; i < code->data_code_len; i++) {
1264 if (!code->data_punc[p][i])
1265 c[j++] = C[i];
1266 }
1267
1268 return 0;
1269}
1270
1271/*
1272 * Parse EGPRS DL header for coding and puncturing scheme (CPS)
1273 *
1274 * Type 1 - MCS-7,8,9
1275 * Type 2 - MCS-5,6
1276 * Type 3 - MCS-1,2,3,4
1277 */
1278static int egprs_parse_dl_cps(struct egprs_cps *cps,
Harald Welteb9946d32017-06-12 09:40:16 +02001279 const union gprs_rlc_dl_hdr_egprs *hdr, int type)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001280{
1281 uint8_t bits;
1282
1283 switch (type) {
1284 case EGPRS_HDR_TYPE1:
1285 bits = hdr->type1.cps;
1286 break;
1287 case EGPRS_HDR_TYPE2:
1288 bits = hdr->type2.cps;
1289 break;
1290 case EGPRS_HDR_TYPE3:
1291 bits = hdr->type3.cps;
1292 break;
1293 default:
1294 return -1;
1295 }
1296
1297 return egprs_get_cps(cps, type, bits);
1298}
1299
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001300/*! EGPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001301 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1302 * \param[in] l2_data L2 (MAC) block to be encoded
1303 * \param[in] l2_len length of l2_data in bytes, used to determine MCS
1304 * \returns 0 on success; negative on error */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001305int gsm0503_pdtch_egprs_encode(ubit_t *bursts,
Harald Welteb9946d32017-06-12 09:40:16 +02001306 const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001307{
1308 ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX];
1309 ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
1310 uint8_t mcs;
1311 struct egprs_cps cps;
1312 union gprs_rlc_dl_hdr_egprs *hdr;
1313
1314 switch (l2_len) {
1315 case 27:
1316 mcs = EGPRS_MCS1;
1317 break;
1318 case 33:
1319 mcs = EGPRS_MCS2;
1320 break;
1321 case 42:
1322 mcs = EGPRS_MCS3;
1323 break;
1324 case 49:
1325 mcs = EGPRS_MCS4;
1326 break;
1327 case 60:
1328 mcs = EGPRS_MCS5;
1329 break;
1330 case 78:
1331 mcs = EGPRS_MCS6;
1332 break;
1333 case 118:
1334 mcs = EGPRS_MCS7;
1335 break;
1336 case 142:
1337 mcs = EGPRS_MCS8;
1338 break;
1339 case 154:
1340 mcs = EGPRS_MCS9;
1341 break;
1342 default:
1343 return -1;
1344 }
1345
1346 /* Read header for USF and puncturing matrix selection. */
1347 hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data;
1348
1349 switch (mcs) {
1350 case EGPRS_MCS1:
1351 case EGPRS_MCS2:
1352 case EGPRS_MCS3:
1353 case EGPRS_MCS4:
1354 /* Check for valid CPS and matching MCS to message size */
1355 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) ||
1356 (cps.mcs != mcs))
1357 goto bad_header;
1358
1359 egprs_encode_hdr(hc, l2_data, mcs);
1360 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1361 egprs_type3_map(bursts, hc, dc, hdr->type3.usf);
1362 break;
1363 case EGPRS_MCS5:
1364 case EGPRS_MCS6:
1365 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) ||
1366 (cps.mcs != mcs))
1367 goto bad_header;
1368
1369 egprs_encode_hdr(hc, l2_data, mcs);
1370 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1371 egprs_type2_map(bursts, hc, dc, hdr->type2.usf);
1372 break;
1373 case EGPRS_MCS7:
1374 case EGPRS_MCS8:
1375 case EGPRS_MCS9:
1376 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) ||
1377 (cps.mcs != mcs))
1378 goto bad_header;
1379
1380 egprs_encode_hdr(hc, l2_data, mcs);
1381 egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0);
1382 egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1);
1383 egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs);
1384 break;
1385 }
1386
1387 return mcs >= EGPRS_MCS5 ?
1388 GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS;
1389
1390bad_header:
1391 /* Invalid EGPRS MCS-X header */
1392 return -1;
1393}
1394
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001395/*! GPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001396 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1397 * \param[in] l2_data L2 (MAC) block to be encoded
1398 * \param[in] l2_len length of l2_data in bytes, used to determine CS
1399 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001400int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001401{
1402 ubit_t iB[456], cB[676];
1403 const ubit_t *hl_hn;
1404 ubit_t conv[334];
1405 int i, j, usf;
1406
1407 switch (l2_len) {
1408 case 23:
1409 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
1410
1411 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
1412
1413 osmo_conv_encode(&gsm0503_xcch, conv, cB);
1414
1415 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
1416
1417 break;
1418 case 34:
1419 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
1420 usf = l2_data[0] & 0x7;
1421
1422 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1423 271, conv + 3 + 271);
1424
1425 memcpy(conv, gsm0503_usf2six[usf], 6);
1426
1427 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
1428
1429 for (i = 0, j = 0; i < 588; i++)
1430 if (!gsm0503_puncture_cs2[i])
1431 cB[j++] = cB[i];
1432
1433 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
1434
1435 break;
1436 case 40:
1437 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
1438 usf = l2_data[0] & 0x7;
1439
1440 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1441 315, conv + 3 + 315);
1442
1443 memcpy(conv, gsm0503_usf2six[usf], 6);
1444
1445 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
1446
1447 for (i = 0, j = 0; i < 676; i++)
1448 if (!gsm0503_puncture_cs3[i])
1449 cB[j++] = cB[i];
1450
1451 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
1452
1453 break;
1454 case 54:
1455 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
1456 usf = l2_data[0] & 0x7;
1457
1458 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
1459 431, cB + 9 + 431);
1460
1461 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
1462
1463 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1464
1465 break;
1466 default:
1467 return -1;
1468 }
1469
1470 gsm0503_xcch_interleave(cB, iB);
1471
1472 for (i = 0; i < 4; i++) {
1473 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1474 hl_hn + i * 2, hl_hn + i * 2 + 1);
1475 }
1476
1477 return GSM0503_GPRS_BURSTS_NBITS;
1478}
1479
1480/*
1481 * GSM TCH/F FR/EFR transcoding
1482 */
1483
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001484/*! assemble a FR codec frame in format as used inside RTP
Harald Weltec6636782017-06-12 14:59:37 +02001485 * \param[out] tch_data Codec frame in RTP format
1486 * \param[in] b_bits Codec frame in 'native' format
1487 * \param[in] net_order FIXME */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001488static void tch_fr_reassemble(uint8_t *tch_data,
Harald Welteb9946d32017-06-12 09:40:16 +02001489 const ubit_t *b_bits, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001490{
1491 int i, j, k, l, o;
1492
1493 tch_data[0] = 0xd << 4;
1494 memset(tch_data + 1, 0, 32);
1495
1496 if (net_order) {
1497 for (i = 0, j = 4; i < 260; i++, j++)
1498 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1499
1500 return;
1501 }
1502
1503 /* reassemble d-bits */
1504 i = 0; /* counts bits */
1505 j = 4; /* counts output bits */
1506 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
1507 l = 0; /* counts element bits */
1508 o = 0; /* offset input bits */
1509 while (i < 260) {
1510 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
1511 if (--k < 0) {
1512 o += gsm0503_gsm_fr_map[l];
1513 k = gsm0503_gsm_fr_map[++l]-1;
1514 }
1515 i++;
1516 j++;
1517 }
1518}
1519
1520static void tch_fr_disassemble(ubit_t *b_bits,
Harald Welteb9946d32017-06-12 09:40:16 +02001521 const uint8_t *tch_data, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001522{
1523 int i, j, k, l, o;
1524
1525 if (net_order) {
1526 for (i = 0, j = 4; i < 260; i++, j++)
1527 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1528
1529 return;
1530 }
1531
1532 i = 0; /* counts bits */
1533 j = 4; /* counts input bits */
1534 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
1535 l = 0; /* counts element bits */
1536 o = 0; /* offset output bits */
1537 while (i < 260) {
1538 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1539 if (--k < 0) {
1540 o += gsm0503_gsm_fr_map[l];
1541 k = gsm0503_gsm_fr_map[++l] - 1;
1542 }
1543 i++;
1544 j++;
1545 }
1546}
1547
Harald Weltec6636782017-06-12 14:59:37 +02001548/* assemble a HR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001549static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001550{
1551 int i, j;
1552
1553 tch_data[0] = 0x00; /* F = 0, FT = 000 */
1554 memset(tch_data + 1, 0, 14);
1555
1556 for (i = 0, j = 8; i < 112; i++, j++)
1557 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1558}
1559
Harald Welteb9946d32017-06-12 09:40:16 +02001560static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001561{
1562 int i, j;
1563
1564 for (i = 0, j = 8; i < 112; i++, j++)
1565 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1566}
1567
Harald Weltec6636782017-06-12 14:59:37 +02001568/* assemble a EFR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001569static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001570{
1571 int i, j;
1572
1573 tch_data[0] = 0xc << 4;
1574 memset(tch_data + 1, 0, 30);
1575
1576 for (i = 0, j = 4; i < 244; i++, j++)
1577 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1578}
1579
Harald Welteb9946d32017-06-12 09:40:16 +02001580static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001581{
1582 int i, j;
1583
1584 for (i = 0, j = 4; i < 244; i++, j++)
1585 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1586}
1587
Harald Weltec6636782017-06-12 14:59:37 +02001588/* assemble a AMR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001589static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001590{
1591 int i, j;
1592
1593 memset(tch_data, 0, (len + 7) >> 3);
1594
1595 for (i = 0, j = 0; i < len; i++, j++)
1596 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
1597}
1598
Harald Welteb9946d32017-06-12 09:40:16 +02001599static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001600{
1601 int i, j;
1602
1603 for (i = 0, j = 0; i < len; i++, j++)
1604 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1605}
1606
Harald Weltec6636782017-06-12 14:59:37 +02001607/* re-arrange according to TS 05.03 Table 2 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001608static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001609{
1610 int i;
1611
1612 for (i = 0; i < 260; i++)
1613 b_bits[gsm610_bitorder[i]] = d_bits[i];
1614}
1615
Harald Weltec6636782017-06-12 14:59:37 +02001616/* re-arrange according to TS 05.03 Table 2 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001617static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001618{
1619 int i;
1620
1621 for (i = 0; i < 260; i++)
1622 d_bits[i] = b_bits[gsm610_bitorder[i]];
1623}
1624
Harald Weltec6636782017-06-12 14:59:37 +02001625/* re-arrange according to TS 05.03 Table 3a (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001626static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001627{
1628 int i;
1629
1630 const uint16_t *map;
1631
1632 if (!d_bits[93] && !d_bits[94])
1633 map = gsm620_unvoiced_bitorder;
1634 else
1635 map = gsm620_voiced_bitorder;
1636
1637 for (i = 0; i < 112; i++)
1638 b_bits[map[i]] = d_bits[i];
1639}
1640
Harald Weltec6636782017-06-12 14:59:37 +02001641/* re-arrange according to TS 05.03 Table 3a (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001642static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001643{
1644 int i;
1645 const uint16_t *map;
1646
1647 if (!b_bits[34] && !b_bits[35])
1648 map = gsm620_unvoiced_bitorder;
1649 else
1650 map = gsm620_voiced_bitorder;
1651
1652 for (i = 0; i < 112; i++)
1653 d_bits[i] = b_bits[map[i]];
1654}
1655
Harald Weltec6636782017-06-12 14:59:37 +02001656/* re-arrange according to TS 05.03 Table 6 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001657static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001658{
1659 int i;
1660
1661 for (i = 0; i < 260; i++)
1662 b_bits[gsm660_bitorder[i]] = d_bits[i];
1663}
1664
Harald Weltec6636782017-06-12 14:59:37 +02001665/* re-arrange according to TS 05.03 Table 6 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001666static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001667{
1668 int i;
1669
1670 for (i = 0; i < 260; i++)
1671 d_bits[i] = b_bits[gsm660_bitorder[i]];
1672}
1673
Harald Weltec6636782017-06-12 14:59:37 +02001674/* extract the 65 protected class1a+1b bits */
Harald Welteb9946d32017-06-12 09:40:16 +02001675static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001676{
1677 int i;
1678
1679 for (i = 0; i < 65; i++)
1680 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
1681}
1682
Harald Welteb9946d32017-06-12 09:40:16 +02001683static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001684{
1685 int i;
1686
1687 for (i = 0; i < 91; i++) {
1688 d[i << 1] = u[i];
1689 d[(i << 1) + 1] = u[184 - i];
1690 }
1691
1692 for (i = 0; i < 3; i++)
1693 p[i] = u[91 + i];
1694}
1695
Harald Welteb9946d32017-06-12 09:40:16 +02001696static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001697{
1698 int i;
1699
1700 for (i = 0; i < 91; i++) {
1701 u[i] = d[i << 1];
1702 u[184 - i] = d[(i << 1) + 1];
1703 }
1704
1705 for (i = 0; i < 3; i++)
1706 u[91 + i] = p[i];
1707}
1708
Harald Welteb9946d32017-06-12 09:40:16 +02001709static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001710{
1711 memcpy(d, u, 95);
1712 memcpy(p, u + 95, 3);
1713}
1714
Harald Welteb9946d32017-06-12 09:40:16 +02001715static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001716{
1717 memcpy(u, d, 95);
1718 memcpy(u + 95, p, 3);
1719}
1720
Harald Welteb9946d32017-06-12 09:40:16 +02001721static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001722{
1723 memcpy(w, s, 71);
1724 w[71] = w[72] = s[69];
1725 memcpy(w + 73, s + 71, 50);
1726 w[123] = w[124] = s[119];
1727 memcpy(w + 125, s + 121, 53);
1728 w[178] = w[179] = s[172];
1729 memcpy(w + 180, s + 174, 50);
1730 w[230] = w[231] = s[222];
1731 memcpy(w + 232, s + 224, 20);
1732 memcpy(w + 252, p, 8);
1733}
1734
Harald Welteb9946d32017-06-12 09:40:16 +02001735static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001736{
1737 int sum;
1738
1739 memcpy(s, w, 71);
1740 sum = s[69] + w[71] + w[72];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001741 s[69] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001742 memcpy(s + 71, w + 73, 50);
1743 sum = s[119] + w[123] + w[124];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001744 s[119] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001745 memcpy(s + 121, w + 125, 53);
1746 sum = s[172] + w[178] + w[179];
1747 s[172] = (sum > 2);
1748 memcpy(s + 174, w + 180, 50);
Niro Mahasinghe834e2ac2017-11-03 12:22:34 +01001749 sum = s[222] + w[230] + w[231];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001750 s[222] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001751 memcpy(s + 224, w + 232, 20);
1752 memcpy(p, w + 252, 8);
1753}
1754
Harald Welteb9946d32017-06-12 09:40:16 +02001755static 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 +07001756{
1757 memcpy(u, d, prot);
1758 memcpy(u + prot, p, 6);
1759 memcpy(u + prot + 6, d + prot, len - prot);
1760}
1761
Harald Welteb9946d32017-06-12 09:40:16 +02001762static 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 +07001763{
1764 memcpy(d, u, prot);
1765 memcpy(p, u + prot, 6);
1766 memcpy(d + prot, u + prot + 6, len - prot);
1767}
1768
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001769/*! Perform channel decoding of a FR/EFR channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001770 * \param[out] tch_data Codec frame in RTP payload format
1771 * \param[in] bursts buffer containing the symbols of 8 bursts
1772 * \param[in] net_order FIXME
1773 * \param[in] efr Is this channel using EFR (1) or FR (0)
1774 * \param[out] n_errors Number of detected bit errors
1775 * \param[out] n_bits_total Total number of bits
1776 * \returns length of bytes used in \a tch_data output buffer */
Harald Welteb9946d32017-06-12 09:40:16 +02001777int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001778 int net_order, int efr, int *n_errors, int *n_bits_total)
1779{
1780 sbit_t iB[912], cB[456], h;
1781 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
1782 int i, rv, len, steal = 0;
1783
Harald Weltec6636782017-06-12 14:59:37 +02001784 /* map from 8 bursts to interleaved data bits (iB) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001785 for (i = 0; i < 8; i++) {
1786 gsm0503_tch_burst_unmap(&iB[i * 114],
1787 &bursts[i * 116], &h, i >> 2);
1788 steal -= h;
1789 }
Harald Weltec6636782017-06-12 14:59:37 +02001790 /* we now have the bits of the four bursts (interface 4 in
1791 * Figure 1a of TS 05.03 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001792
1793 gsm0503_tch_fr_deinterleave(cB, iB);
Harald Weltec6636782017-06-12 14:59:37 +02001794 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001795
1796 if (steal > 0) {
1797 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1798 if (rv) {
1799 /* Error decoding FACCH frame */
1800 return -1;
1801 }
1802
1803 return 23;
1804 }
1805
1806 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Harald Weltec6636782017-06-12 14:59:37 +02001807 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001808
Harald Weltec6636782017-06-12 14:59:37 +02001809 /* input: 'conv', output: d[ata] + p[arity] */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001810 tch_fr_unreorder(d, p, conv);
1811
1812 for (i = 0; i < 78; i++)
1813 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
1814
Harald Weltec6636782017-06-12 14:59:37 +02001815 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001816 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1817 if (rv) {
1818 /* Error checking CRC8 for the FR part of an EFR/FR frame */
1819 return -1;
1820 }
1821
1822 if (efr) {
1823 tch_efr_d_to_w(w, d);
Harald Weltec6636782017-06-12 14:59:37 +02001824 /* we now have the preliminary-coded bits w(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001825
1826 tch_efr_unreorder(s, p, w);
Harald Weltec6636782017-06-12 14:59:37 +02001827 /* we now have the data delivered to the preliminary
1828 * channel encoding unit s(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001829
Harald Weltec6636782017-06-12 14:59:37 +02001830 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001831 tch_efr_protected(s, b);
1832
Harald Weltec6636782017-06-12 14:59:37 +02001833 /* perform CRC-8 on 65 most important bits (50 bits of
1834 * class 1a + 15 bits of class 1b) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001835 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1836 if (rv) {
1837 /* Error checking CRC8 for the EFR part of an EFR frame */
1838 return -1;
1839 }
1840
1841 tch_efr_reassemble(tch_data, s);
1842
1843 len = GSM_EFR_BYTES;
1844 } else {
1845 tch_fr_d_to_b(w, d);
1846
1847 tch_fr_reassemble(tch_data, w, net_order);
1848
1849 len = GSM_FR_BYTES;
1850 }
1851
1852 return len;
1853}
1854
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001855/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001856 * \param[out] bursts caller-allocated output buffer for bursts bits
1857 * \param[in] tch_data Codec input data in RTP payload format
1858 * \param[in] len Length of \a tch_data in bytes
1859 * \param[in] net_order FIXME
1860 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001861int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001862 int len, int net_order)
1863{
1864 ubit_t iB[912], cB[456], h;
1865 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
1866 int i;
1867
1868 switch (len) {
1869 case GSM_EFR_BYTES: /* TCH EFR */
1870
1871 tch_efr_disassemble(s, tch_data);
1872
1873 tch_efr_protected(s, b);
1874
1875 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1876
1877 tch_efr_reorder(w, s, p);
1878
1879 tch_efr_w_to_d(d, w);
1880
1881 goto coding_efr_fr;
1882 case GSM_FR_BYTES: /* TCH FR */
1883 tch_fr_disassemble(w, tch_data, net_order);
1884
1885 tch_fr_b_to_d(d, w);
1886
1887coding_efr_fr:
1888 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1889
1890 tch_fr_reorder(conv, d, p);
1891
1892 memcpy(cB + 378, d + 182, 78);
1893
1894 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
1895
1896 h = 0;
1897
1898 break;
1899 case GSM_MACBLOCK_LEN: /* FACCH */
1900 _xcch_encode_cB(cB, tch_data);
1901
1902 h = 1;
1903
1904 break;
1905 default:
1906 return -1;
1907 }
1908
1909 gsm0503_tch_fr_interleave(cB, iB);
1910
1911 for (i = 0; i < 8; i++) {
1912 gsm0503_tch_burst_map(&iB[i * 114],
1913 &bursts[i * 116], &h, i >> 2);
1914 }
1915
1916 return 0;
1917}
1918
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001919/*! Perform channel decoding of a HR(v1) channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001920 * \param[out] tch_data Codec frame in RTP payload format
1921 * \param[in] bursts buffer containing the symbols of 8 bursts
1922 * \param[in] odd Odd (1) or even (0) frame number
1923 * \param[out] n_errors Number of detected bit errors
1924 * \param[out] n_bits_total Total number of bits
1925 * \returns length of bytes used in \a tch_data output buffer */
Harald Welteb9946d32017-06-12 09:40:16 +02001926int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001927 int *n_errors, int *n_bits_total)
1928{
1929 sbit_t iB[912], cB[456], h;
1930 ubit_t conv[98], b[112], d[112], p[3];
1931 int i, rv, steal = 0;
1932
1933 /* Only unmap the stealing bits */
1934 if (!odd) {
1935 for (i = 0; i < 4; i++) {
1936 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
1937 steal -= h;
1938 }
1939
1940 for (i = 2; i < 5; i++) {
1941 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
1942 steal -= h;
1943 }
1944 }
1945
1946 /* If we found a stole FACCH, but only at correct alignment */
1947 if (steal > 0) {
1948 for (i = 0; i < 6; i++) {
1949 gsm0503_tch_burst_unmap(&iB[i * 114],
1950 &bursts[i * 116], NULL, i >> 2);
1951 }
1952
1953 for (i = 2; i < 4; i++) {
1954 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
1955 &bursts[i * 116], NULL, 1);
1956 }
1957
1958 gsm0503_tch_fr_deinterleave(cB, iB);
1959
1960 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1961 if (rv) {
1962 /* Error decoding FACCH frame */
1963 return -1;
1964 }
1965
1966 return GSM_MACBLOCK_LEN;
1967 }
1968
1969 for (i = 0; i < 4; i++) {
1970 gsm0503_tch_burst_unmap(&iB[i * 114],
1971 &bursts[i * 116], NULL, i >> 1);
1972 }
1973
1974 gsm0503_tch_hr_deinterleave(cB, iB);
1975
1976 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
1977
1978 tch_hr_unreorder(d, p, conv);
1979
1980 for (i = 0; i < 17; i++)
1981 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
1982
1983 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
1984 if (rv) {
1985 /* Error checking CRC8 for an HR frame */
1986 return -1;
1987 }
1988
1989 tch_hr_d_to_b(b, d);
1990
1991 tch_hr_reassemble(tch_data, b);
1992
1993 return 15;
1994}
1995
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001996/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001997 * \param[out] bursts caller-allocated output buffer for bursts bits
1998 * \param[in] tch_data Codec input data in RTP payload format
1999 * \param[in] len Length of \a tch_data in bytes
2000 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002001int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002002{
2003 ubit_t iB[912], cB[456], h;
2004 ubit_t conv[98], b[112], d[112], p[3];
2005 int i;
2006
2007 switch (len) {
2008 case 15: /* TCH HR */
2009 tch_hr_disassemble(b, tch_data);
2010
2011 tch_hr_b_to_d(d, b);
2012
2013 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2014
2015 tch_hr_reorder(conv, d, p);
2016
2017 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
2018
2019 memcpy(cB + 211, d + 95, 17);
2020
2021 h = 0;
2022
2023 gsm0503_tch_hr_interleave(cB, iB);
2024
2025 for (i = 0; i < 4; i++) {
2026 gsm0503_tch_burst_map(&iB[i * 114],
2027 &bursts[i * 116], &h, i >> 1);
2028 }
2029
2030 break;
2031 case GSM_MACBLOCK_LEN: /* FACCH */
2032 _xcch_encode_cB(cB, tch_data);
2033
2034 h = 1;
2035
2036 gsm0503_tch_fr_interleave(cB, iB);
2037
2038 for (i = 0; i < 6; i++) {
2039 gsm0503_tch_burst_map(&iB[i * 114],
2040 &bursts[i * 116], &h, i >> 2);
2041 }
2042
2043 for (i = 2; i < 4; i++) {
2044 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2045 &bursts[i * 116], &h, 1);
2046 }
2047
2048 break;
2049 default:
2050 return -1;
2051 }
2052
2053 return 0;
2054}
2055
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002056/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002057 * \param[out] tch_data Codec frame in RTP payload format
2058 * \param[in] bursts buffer containing the symbols of 8 bursts
2059 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2060 * \param[in] codec array of active codecs (active codec set)
2061 * \param[in] codecs number of codecs in \a codec
2062 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2063 * \param[out] cmr Output in \a codec_mode_req = 1
2064 * \param[out] n_errors Number of detected bit errors
2065 * \param[out] n_bits_total Total number of bits
2066 * \returns length of bytes used in \a tch_data output buffer */
Harald Welteb9946d32017-06-12 09:40:16 +02002067int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002068 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2069 uint8_t *cmr, int *n_errors, int *n_bits_total)
2070{
2071 sbit_t iB[912], cB[456], h;
2072 ubit_t d[244], p[6], conv[250];
2073 int i, j, k, best = 0, rv, len, steal = 0, id = 0;
2074 *n_errors = 0; *n_bits_total = 0;
2075
2076 for (i=0; i<8; i++) {
2077 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
2078 steal -= h;
2079 }
2080
2081 gsm0503_tch_fr_deinterleave(cB, iB);
2082
2083 if (steal > 0) {
2084 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2085 if (rv) {
2086 /* Error decoding FACCH frame */
2087 return -1;
2088 }
2089
2090 return GSM_MACBLOCK_LEN;
2091 }
2092
2093 for (i = 0; i < 4; i++) {
2094 for (j = 0, k = 0; j < 8; j++)
2095 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
2096
2097 if (i == 0 || k < best) {
2098 best = k;
2099 id = i;
2100 }
2101 }
2102
2103 /* Check if indicated codec fits into range of codecs */
2104 if (id >= codecs) {
2105 /* Codec mode out of range, return id */
2106 return id;
2107 }
2108
2109 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2110 case 7: /* TCH/AFS12.2 */
2111 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
2112 conv, n_errors, n_bits_total);
2113
2114 tch_amr_unmerge(d, p, conv, 244, 81);
2115
2116 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
2117 if (rv) {
2118 /* Error checking CRC8 for an AMR 12.2 frame */
2119 return -1;
2120 }
2121
2122 tch_amr_reassemble(tch_data, d, 244);
2123
2124 len = 31;
2125
2126 break;
2127 case 6: /* TCH/AFS10.2 */
2128 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
2129 conv, n_errors, n_bits_total);
2130
2131 tch_amr_unmerge(d, p, conv, 204, 65);
2132
2133 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
2134 if (rv) {
2135 /* Error checking CRC8 for an AMR 10.2 frame */
2136 return -1;
2137 }
2138
2139 tch_amr_reassemble(tch_data, d, 204);
2140
2141 len = 26;
2142
2143 break;
2144 case 5: /* TCH/AFS7.95 */
2145 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
2146 conv, n_errors, n_bits_total);
2147
2148 tch_amr_unmerge(d, p, conv, 159, 75);
2149
2150 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
2151 if (rv) {
2152 /* Error checking CRC8 for an AMR 7.95 frame */
2153 return -1;
2154 }
2155
2156 tch_amr_reassemble(tch_data, d, 159);
2157
2158 len = 20;
2159
2160 break;
2161 case 4: /* TCH/AFS7.4 */
2162 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
2163 conv, n_errors, n_bits_total);
2164
2165 tch_amr_unmerge(d, p, conv, 148, 61);
2166
2167 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2168 if (rv) {
2169 /* Error checking CRC8 for an AMR 7.4 frame */
2170 return -1;
2171 }
2172
2173 tch_amr_reassemble(tch_data, d, 148);
2174
2175 len = 19;
2176
2177 break;
2178 case 3: /* TCH/AFS6.7 */
2179 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
2180 conv, n_errors, n_bits_total);
2181
2182 tch_amr_unmerge(d, p, conv, 134, 55);
2183
2184 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2185 if (rv) {
2186 /* Error checking CRC8 for an AMR 6.7 frame */
2187 return -1;
2188 }
2189
2190 tch_amr_reassemble(tch_data, d, 134);
2191
2192 len = 17;
2193
2194 break;
2195 case 2: /* TCH/AFS5.9 */
2196 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
2197 conv, n_errors, n_bits_total);
2198
2199 tch_amr_unmerge(d, p, conv, 118, 55);
2200
2201 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2202 if (rv) {
2203 /* Error checking CRC8 for an AMR 5.9 frame */
2204 return -1;
2205 }
2206
2207 tch_amr_reassemble(tch_data, d, 118);
2208
2209 len = 15;
2210
2211 break;
2212 case 1: /* TCH/AFS5.15 */
2213 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
2214 conv, n_errors, n_bits_total);
2215
2216 tch_amr_unmerge(d, p, conv, 103, 49);
2217
2218 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2219 if (rv) {
2220 /* Error checking CRC8 for an AMR 5.15 frame */
2221 return -1;
2222 }
2223
2224 tch_amr_reassemble(tch_data, d, 103);
2225
2226 len = 13;
2227
2228 break;
2229 case 0: /* TCH/AFS4.75 */
2230 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
2231 conv, n_errors, n_bits_total);
2232
2233 tch_amr_unmerge(d, p, conv, 95, 39);
2234
2235 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2236 if (rv) {
2237 /* Error checking CRC8 for an AMR 4.75 frame */
2238 return -1;
2239 }
2240
2241 tch_amr_reassemble(tch_data, d, 95);
2242
2243 len = 12;
2244
2245 break;
2246 default:
2247 /* Unknown frame type */
2248 *n_bits_total = 448;
2249 *n_errors = *n_bits_total;
2250 return -1;
2251 }
2252
2253 /* Change codec request / indication, if frame is valid */
2254 if (codec_mode_req)
2255 *cmr = id;
2256 else
2257 *ft = id;
2258
2259 return len;
2260}
2261
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002262/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002263 * \param[out] bursts caller-allocated output buffer for bursts bits
2264 * \param[in] tch_data Codec input data in RTP payload format
2265 * \param[in] len Length of \a tch_data in bytes
2266 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2267 * \param[in] codec Array of codecs (active codec set)
2268 * \param[in] codecs Number of entries in \a codec
2269 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2270 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2271 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002272int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002273 int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft,
2274 uint8_t cmr)
2275{
2276 ubit_t iB[912], cB[456], h;
2277 ubit_t d[244], p[6], conv[250];
2278 int i;
2279 uint8_t id;
2280
2281 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2282 _xcch_encode_cB(cB, tch_data);
2283
2284 h = 1;
2285
2286 goto facch;
2287 }
2288
2289 h = 0;
2290
2291 if (codec_mode_req) {
2292 if (cmr >= codecs) {
2293 /* FIXME: CMR ID is not in codec list! */
2294 return -1;
2295 }
2296 id = cmr;
2297 } else {
2298 if (ft >= codecs) {
2299 /* FIXME: FT ID is not in codec list! */
2300 return -1;
2301 }
2302 id = ft;
2303 }
2304
2305 switch (codec[ft]) {
2306 case 7: /* TCH/AFS12.2 */
2307 if (len != 31)
2308 goto invalid_length;
2309
2310 tch_amr_disassemble(d, tch_data, 244);
2311
2312 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
2313
2314 tch_amr_merge(conv, d, p, 244, 81);
2315
2316 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
2317
2318 break;
2319 case 6: /* TCH/AFS10.2 */
2320 if (len != 26)
2321 goto invalid_length;
2322
2323 tch_amr_disassemble(d, tch_data, 204);
2324
2325 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
2326
2327 tch_amr_merge(conv, d, p, 204, 65);
2328
2329 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
2330
2331 break;
2332 case 5: /* TCH/AFS7.95 */
2333 if (len != 20)
2334 goto invalid_length;
2335
2336 tch_amr_disassemble(d, tch_data, 159);
2337
2338 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
2339
2340 tch_amr_merge(conv, d, p, 159, 75);
2341
2342 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
2343
2344 break;
2345 case 4: /* TCH/AFS7.4 */
2346 if (len != 19)
2347 goto invalid_length;
2348
2349 tch_amr_disassemble(d, tch_data, 148);
2350
2351 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2352
2353 tch_amr_merge(conv, d, p, 148, 61);
2354
2355 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
2356
2357 break;
2358 case 3: /* TCH/AFS6.7 */
2359 if (len != 17)
2360 goto invalid_length;
2361
2362 tch_amr_disassemble(d, tch_data, 134);
2363
2364 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2365
2366 tch_amr_merge(conv, d, p, 134, 55);
2367
2368 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
2369
2370 break;
2371 case 2: /* TCH/AFS5.9 */
2372 if (len != 15)
2373 goto invalid_length;
2374
2375 tch_amr_disassemble(d, tch_data, 118);
2376
2377 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2378
2379 tch_amr_merge(conv, d, p, 118, 55);
2380
2381 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
2382
2383 break;
2384 case 1: /* TCH/AFS5.15 */
2385 if (len != 13)
2386 goto invalid_length;
2387
2388 tch_amr_disassemble(d, tch_data, 103);
2389
2390 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2391
2392 tch_amr_merge(conv, d, p, 103, 49);
2393
2394 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
2395
2396 break;
2397 case 0: /* TCH/AFS4.75 */
2398 if (len != 12)
2399 goto invalid_length;
2400
2401 tch_amr_disassemble(d, tch_data, 95);
2402
2403 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2404
2405 tch_amr_merge(conv, d, p, 95, 39);
2406
2407 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
2408
2409 break;
2410 default:
2411 /* FIXME: FT %ft is not supported */
2412 return -1;
2413 }
2414
2415 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
2416
2417facch:
2418 gsm0503_tch_fr_interleave(cB, iB);
2419
2420 for (i = 0; i < 8; i++) {
2421 gsm0503_tch_burst_map(&iB[i * 114],
2422 &bursts[i * 116], &h, i >> 2);
2423 }
2424
2425 return 0;
2426
2427invalid_length:
2428 /* FIXME: payload length %len does not comply with codec type %ft */
2429 return -1;
2430}
2431
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002432/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002433 * \param[out] tch_data Codec frame in RTP payload format
2434 * \param[in] bursts buffer containing the symbols of 8 bursts
2435 * \param[in] odd Is this an odd (1) or even (0) frame number?
2436 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2437 * \param[in] codec array of active codecs (active codec set)
2438 * \param[in] codecs number of codecs in \a codec
2439 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2440 * \param[out] cmr Output in \a codec_mode_req = 1
2441 * \param[out] n_errors Number of detected bit errors
2442 * \param[out] n_bits_total Total number of bits
2443 * \returns length of bytes used in \a tch_data output buffer */
Harald Welteb9946d32017-06-12 09:40:16 +02002444int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002445 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2446 uint8_t *cmr, int *n_errors, int *n_bits_total)
2447{
2448 sbit_t iB[912], cB[456], h;
2449 ubit_t d[244], p[6], conv[135];
2450 int i, j, k, best = 0, rv, len, steal = 0, id = 0;
2451
2452 /* only unmap the stealing bits */
2453 if (!odd) {
2454 for (i = 0; i < 4; i++) {
2455 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2456 steal -= h;
2457 }
2458 for (i = 2; i < 5; i++) {
2459 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2460 steal -= h;
2461 }
2462 }
2463
2464 /* if we found a stole FACCH, but only at correct alignment */
2465 if (steal > 0) {
2466 for (i = 0; i < 6; i++) {
2467 gsm0503_tch_burst_unmap(&iB[i * 114],
2468 &bursts[i * 116], NULL, i >> 2);
2469 }
2470
2471 for (i = 2; i < 4; i++) {
2472 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2473 &bursts[i * 116], NULL, 1);
2474 }
2475
2476 gsm0503_tch_fr_deinterleave(cB, iB);
2477
2478 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2479 if (rv) {
2480 /* Error decoding FACCH frame */
2481 return -1;
2482 }
2483
2484 return GSM_MACBLOCK_LEN;
2485 }
2486
2487 for (i = 0; i < 4; i++) {
2488 gsm0503_tch_burst_unmap(&iB[i * 114],
2489 &bursts[i * 116], NULL, i >> 1);
2490 }
2491
2492 gsm0503_tch_hr_deinterleave(cB, iB);
2493
2494 for (i = 0; i < 4; i++) {
2495 for (j = 0, k = 0; j < 4; j++)
2496 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
2497
2498 if (i == 0 || k < best) {
2499 best = k;
2500 id = i;
2501 }
2502 }
2503
2504 /* Check if indicated codec fits into range of codecs */
2505 if (id >= codecs) {
2506 /* Codec mode out of range, return id */
2507 return id;
2508 }
2509
2510 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2511 case 5: /* TCH/AHS7.95 */
2512 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
2513 conv, n_errors, n_bits_total);
2514
2515 tch_amr_unmerge(d, p, conv, 123, 67);
2516
2517 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
2518 if (rv) {
2519 /* Error checking CRC8 for an AMR 7.95 frame */
2520 return -1;
2521 }
2522
2523 for (i = 0; i < 36; i++)
2524 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
2525
2526 tch_amr_reassemble(tch_data, d, 159);
2527
2528 len = 20;
2529
2530 break;
2531 case 4: /* TCH/AHS7.4 */
2532 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
2533 conv, n_errors, n_bits_total);
2534
2535 tch_amr_unmerge(d, p, conv, 120, 61);
2536
2537 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2538 if (rv) {
2539 /* Error checking CRC8 for an AMR 7.4 frame */
2540 return -1;
2541 }
2542
2543 for (i = 0; i < 28; i++)
2544 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
2545
2546 tch_amr_reassemble(tch_data, d, 148);
2547
2548 len = 19;
2549
2550 break;
2551 case 3: /* TCH/AHS6.7 */
2552 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
2553 conv, n_errors, n_bits_total);
2554
2555 tch_amr_unmerge(d, p, conv, 110, 55);
2556
2557 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2558 if (rv) {
2559 /* Error checking CRC8 for an AMR 6.7 frame */
2560 return -1;
2561 }
2562
2563 for (i = 0; i < 24; i++)
2564 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
2565
2566 tch_amr_reassemble(tch_data, d, 134);
2567
2568 len = 17;
2569
2570 break;
2571 case 2: /* TCH/AHS5.9 */
2572 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
2573 conv, n_errors, n_bits_total);
2574
2575 tch_amr_unmerge(d, p, conv, 102, 55);
2576
2577 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2578 if (rv) {
2579 /* Error checking CRC8 for an AMR 5.9 frame */
2580 return -1;
2581 }
2582
2583 for (i = 0; i < 16; i++)
2584 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
2585
2586 tch_amr_reassemble(tch_data, d, 118);
2587
2588 len = 15;
2589
2590 break;
2591 case 1: /* TCH/AHS5.15 */
2592 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
2593 conv, n_errors, n_bits_total);
2594
2595 tch_amr_unmerge(d, p, conv, 91, 49);
2596
2597 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2598 if (rv) {
2599 /* Error checking CRC8 for an AMR 5.15 frame */
2600 return -1;
2601 }
2602
2603 for (i = 0; i < 12; i++)
2604 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
2605
2606 tch_amr_reassemble(tch_data, d, 103);
2607
2608 len = 13;
2609
2610 break;
2611 case 0: /* TCH/AHS4.75 */
2612 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
2613 conv, n_errors, n_bits_total);
2614
2615 tch_amr_unmerge(d, p, conv, 83, 39);
2616
2617 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2618 if (rv) {
2619 /* Error checking CRC8 for an AMR 4.75 frame */
2620 return -1;
2621 }
2622
2623 for (i = 0; i < 12; i++)
2624 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
2625
2626 tch_amr_reassemble(tch_data, d, 95);
2627
2628 len = 12;
2629
2630 break;
2631 default:
2632 /* Unknown frame type */
2633 *n_bits_total = 159;
2634 *n_errors = *n_bits_total;
2635 return -1;
2636 }
2637
2638 /* Change codec request / indication, if frame is valid */
2639 if (codec_mode_req)
2640 *cmr = id;
2641 else
2642 *ft = id;
2643
2644 return len;
2645}
2646
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002647/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002648 * \param[out] bursts caller-allocated output buffer for bursts bits
2649 * \param[in] tch_data Codec input data in RTP payload format
2650 * \param[in] len Length of \a tch_data in bytes
2651 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2652 * \param[in] codec Array of codecs (active codec set)
2653 * \param[in] codecs Number of entries in \a codec
2654 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2655 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2656 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002657int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002658 int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft,
2659 uint8_t cmr)
2660{
2661 ubit_t iB[912], cB[456], h;
2662 ubit_t d[244], p[6], conv[135];
2663 int i;
2664 uint8_t id;
2665
2666 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2667 _xcch_encode_cB(cB, tch_data);
2668
2669 h = 1;
2670
2671 gsm0503_tch_fr_interleave(cB, iB);
2672
2673 for (i = 0; i < 6; i++)
2674 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
2675 &h, i >> 2);
2676 for (i = 2; i < 4; i++)
2677 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2678 &bursts[i * 116], &h, 1);
2679
2680 return 0;
2681 }
2682
2683 h = 0;
2684
2685 if (codec_mode_req) {
2686 if (cmr >= codecs) {
2687 /* FIXME: CMR ID %d not in codec list */
2688 return -1;
2689 }
2690 id = cmr;
2691 } else {
2692 if (ft >= codecs) {
2693 /* FIXME: FT ID %d not in codec list */
2694 return -1;
2695 }
2696 id = ft;
2697 }
2698
2699 switch (codec[ft]) {
2700 case 5: /* TCH/AHS7.95 */
2701 if (len != 20)
2702 goto invalid_length;
2703
2704 tch_amr_disassemble(d, tch_data, 159);
2705
2706 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
2707
2708 tch_amr_merge(conv, d, p, 123, 67);
2709
2710 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
2711
2712 memcpy(cB + 192, d + 123, 36);
2713
2714 break;
2715 case 4: /* TCH/AHS7.4 */
2716 if (len != 19)
2717 goto invalid_length;
2718
2719 tch_amr_disassemble(d, tch_data, 148);
2720
2721 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2722
2723 tch_amr_merge(conv, d, p, 120, 61);
2724
2725 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
2726
2727 memcpy(cB + 200, d + 120, 28);
2728
2729 break;
2730 case 3: /* TCH/AHS6.7 */
2731 if (len != 17)
2732 goto invalid_length;
2733
2734 tch_amr_disassemble(d, tch_data, 134);
2735
2736 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2737
2738 tch_amr_merge(conv, d, p, 110, 55);
2739
2740 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
2741
2742 memcpy(cB + 204, d + 110, 24);
2743
2744 break;
2745 case 2: /* TCH/AHS5.9 */
2746 if (len != 15)
2747 goto invalid_length;
2748
2749 tch_amr_disassemble(d, tch_data, 118);
2750
2751 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2752
2753 tch_amr_merge(conv, d, p, 102, 55);
2754
2755 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
2756
2757 memcpy(cB + 212, d + 102, 16);
2758
2759 break;
2760 case 1: /* TCH/AHS5.15 */
2761 if (len != 13)
2762 goto invalid_length;
2763
2764 tch_amr_disassemble(d, tch_data, 103);
2765
2766 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2767
2768 tch_amr_merge(conv, d, p, 91, 49);
2769
2770 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
2771
2772 memcpy(cB + 216, d + 91, 12);
2773
2774 break;
2775 case 0: /* TCH/AHS4.75 */
2776 if (len != 12)
2777 goto invalid_length;
2778
2779 tch_amr_disassemble(d, tch_data, 95);
2780
2781 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2782
2783 tch_amr_merge(conv, d, p, 83, 39);
2784
2785 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
2786
2787 memcpy(cB + 216, d + 83, 12);
2788
2789 break;
2790 default:
2791 /* FIXME: FT %ft is not supported */
2792 return -1;
2793 }
2794
2795 memcpy(cB, gsm0503_afs_ic_ubit[id], 4);
2796
2797 gsm0503_tch_hr_interleave(cB, iB);
2798
2799 for (i = 0; i < 4; i++)
2800 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
2801
2802 return 0;
2803
2804invalid_length:
2805 /* FIXME: payload length %len does not comply with codec type %ft */
2806 return -1;
2807}
2808
2809/*
2810 * GSM RACH transcoding
2811 */
2812
2813/*
2814 * GSM RACH apply BSIC to parity
2815 *
2816 * p(j) = p(j) xor b(j) j = 0, ..., 5
2817 * b(0) = MSB of PLMN colour code
2818 * b(5) = LSB of BS colour code
2819 */
Max32e56412017-10-16 14:58:00 +02002820static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002821{
2822 int i;
2823
2824 /* Apply it */
2825 for (i = 0; i < 6; i++)
Max32e56412017-10-16 14:58:00 +02002826 d[start + i] ^= ((bsic >> (5 - i)) & 1);
2827}
2828
2829static inline int16_t rach_decode(const sbit_t *burst, uint8_t bsic, bool is_11bit)
2830{
2831 ubit_t conv[17];
2832 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
2833 int rv;
2834
2835 osmo_conv_decode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv);
2836
2837 rach_apply_bsic(conv, bsic, nbits);
2838
2839 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
2840 if (rv)
2841 return -1;
2842
2843 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
2844
2845 return is_11bit ? osmo_load16le(ra) : ra[0];
2846}
2847
2848/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
2849 * \param[out] ra output buffer for RACH data
2850 * \param[in] burst Input burst data
2851 * \param[in] bsic BSIC used in this cell
2852 * \returns 0 on success; negative on error (e.g. CRC error) */
2853int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
2854{
2855 int16_t r = rach_decode(burst, bsic, true);
2856
2857 if (r < 0)
2858 return r;
2859
2860 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002861
2862 return 0;
2863}
2864
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002865/*! Decode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002866 * \param[out] ra output buffer for RACH data
2867 * \param[in] burst Input burst data
2868 * \param[in] bsic BSIC used in this cell
2869 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02002870int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002871{
Max32e56412017-10-16 14:58:00 +02002872 int16_t r = rach_decode(burst, bsic, false);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002873
Max32e56412017-10-16 14:58:00 +02002874 if (r < 0)
2875 return r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002876
Max32e56412017-10-16 14:58:00 +02002877 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002878
2879 return 0;
2880}
2881
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002882/*! Encode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002883 * \param[out] burst Caller-allocated output burst buffer
2884 * \param[in] ra Input RACH data
2885 * \param[in] bsic BSIC used in this cell
2886 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002887int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002888{
Max32e56412017-10-16 14:58:00 +02002889 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
2890}
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002891
Max32e56412017-10-16 14:58:00 +02002892/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
2893 * \param[out] burst Caller-allocated output burst buffer
2894 * \param[in] ra11 Input RACH data
2895 * \param[in] bsic BSIC used in this cell
2896 * \param[in] is_11bit whether given RA is 11 bit or not
2897 * \returns 0 on success; negative on error */
2898int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
2899{
2900 ubit_t conv[17];
2901 uint8_t ra[2] = { 0 }, nbits = 8;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002902
Max32e56412017-10-16 14:58:00 +02002903 if (is_11bit) {
2904 osmo_store16le(ra11, ra);
2905 nbits = 11;
2906 } else
2907 ra[0] = (uint8_t)ra11;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002908
Max32e56412017-10-16 14:58:00 +02002909 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002910
Max32e56412017-10-16 14:58:00 +02002911 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
2912
2913 rach_apply_bsic(conv, bsic, nbits);
2914
2915 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002916
2917 return 0;
2918}
2919
2920/*
2921 * GSM SCH transcoding
2922 */
Harald Weltec6636782017-06-12 14:59:37 +02002923
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002924/*! Decode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002925 * \param[out] sb_info output buffer for SCH data
2926 * \param[in] burst Input burst data
2927 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02002928int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002929{
2930 ubit_t conv[35];
2931 int rv;
2932
2933 osmo_conv_decode(&gsm0503_sch, burst, conv);
2934
2935 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
2936 if (rv)
2937 return -1;
2938
2939 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
2940
2941 return 0;
2942}
2943
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002944/*! Encode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002945 * \param[out] burst Caller-allocated output burst buffer
2946 * \param[in] sb_info Input SCH data
2947 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002948int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002949{
2950 ubit_t conv[35];
2951
2952 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
2953
2954 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
2955
2956 osmo_conv_encode(&gsm0503_sch, conv, burst);
2957
2958 return 0;
2959}
Harald Weltec6636782017-06-12 14:59:37 +02002960
2961/*! @} */