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