blob: e0238ce02c63e5db0f00fcaf78dcae5e69142b40 [file] [log] [blame]
Harald Welte2a8ee522009-01-05 19:01:01 +00001/* TRAU frame handling according to GSM TS 08.60 */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <unistd.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <errno.h>
27
28#include <openbsc/trau_frame.h>
29
30static u_int32_t get_bits(u_int8_t *bitbuf, int offset, int num)
31{
32 int i;
33 u_int32_t ret = 0;
34
35 for (i = offset; i < offset + num; i++) {
36 ret = ret << 1;
37 if (bitbuf[i])
38 ret |= 1;
39 }
40 return ret;
41}
42
43/* Decode according to 3.1.1 */
44static void decode_fr(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
45{
46 int i;
47 int d_idx = 0;
48
49 /* C1 .. C15 */
50 memcpy(fr->c_bits+0, trau_bits+17, 15);
51 /* C16 .. C21 */
52 memcpy(fr->c_bits+15, trau_bits+310, 6);
53 /* T1 .. T4 */
54 memcpy(fr->t_bits+0, trau_bits+316, 4);
55 /* D1 .. D255 */
56 for (i = 32; i < 304; i+= 16) {
57 memcpy(fr->d_bits + d_idx, trau_bits+i+1, 15);
58 d_idx += 15;
59 }
60 /* D256 .. D260 */
61 memcpy(fr->d_bits + d_idx, trau_bits + 305, 5);
62}
63
64static void decode_amr(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
65{
66 int i;
67 int d_idx = 0;
68
69 /* C1 .. C15 */
70 memcpy(fr->c_bits+0, trau_bits+17, 15);
71 /* C16 .. C25 */
72 memcpy(fr->c_bits+15, trau_bits+33, 10);
73 /* T1 .. T4 */
74 memcpy(fr->t_bits+0, trau_bits+316, 4);
75 /* D1 .. D5 */
76 memcpy(fr->d_bits, trau_bits+43, 5);
77 /* D6 .. D245 */
78 for (i = 48; i < 304; i += 16) {
79 memcpy(fr->d_bits + d_idx, trau_bits+i+1, 15);
80 d_idx += 15;
81 }
82 /* D246 .. D256 */
83 memcpy(fr->d_bits + d_idx, trau_bits + 305, 11);
84}
85
86int decode_trau_frame(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
87{
88 u_int8_t cbits5 = get_bits(trau_bits, 17, 5);
89
90 switch (cbits5) {
91 case TRAU_FT_FR_UP:
92 case TRAU_FT_FR_DOWN:
93 case TRAU_FT_EFR:
94 decode_fr(fr, trau_bits);
95 break;
96 case TRAU_FT_AMR:
97 decode_amr(fr, trau_bits);
98 break;
99 case TRAU_FT_OM_UP:
100 case TRAU_FT_OM_DOWN:
101 case TRAU_FT_DATA_UP:
102 case TRAU_FT_DATA_DOWN:
103 case TRAU_FT_D145_SYNC:
104 case TRAU_FT_EDATA:
105 fprintf(stderr, "unimplemented TRAU Frame Type 0x%02x\n", cbits5);
106 return -1;
107 break;
108 default:
109 fprintf(stderr, "unknown TRAU Frame Type 0x%02x\n", cbits5);
110 return -1;
111 break;
112 }
113
114 return 0;
115}