blob: 8eb2b453c88f5a38a125f1831871d31861179a3c [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
Harald Welte38e04372009-01-05 19:42:46 +000064/* Decode according to 3.1.2 */
Harald Welte2a8ee522009-01-05 19:01:01 +000065static void decode_amr(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
66{
67 int i;
68 int d_idx = 0;
69
70 /* C1 .. C15 */
71 memcpy(fr->c_bits+0, trau_bits+17, 15);
72 /* C16 .. C25 */
73 memcpy(fr->c_bits+15, trau_bits+33, 10);
74 /* T1 .. T4 */
75 memcpy(fr->t_bits+0, trau_bits+316, 4);
76 /* D1 .. D5 */
77 memcpy(fr->d_bits, trau_bits+43, 5);
78 /* D6 .. D245 */
79 for (i = 48; i < 304; i += 16) {
80 memcpy(fr->d_bits + d_idx, trau_bits+i+1, 15);
81 d_idx += 15;
82 }
83 /* D246 .. D256 */
84 memcpy(fr->d_bits + d_idx, trau_bits + 305, 11);
85}
86
87int decode_trau_frame(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
88{
89 u_int8_t cbits5 = get_bits(trau_bits, 17, 5);
90
91 switch (cbits5) {
92 case TRAU_FT_FR_UP:
93 case TRAU_FT_FR_DOWN:
Harald Welte38e04372009-01-05 19:42:46 +000094 case TRAU_FT_IDLE_UP:
95 case TRAU_FT_IDLE_DOWN:
Harald Welte2a8ee522009-01-05 19:01:01 +000096 case TRAU_FT_EFR:
97 decode_fr(fr, trau_bits);
98 break;
99 case TRAU_FT_AMR:
100 decode_amr(fr, trau_bits);
101 break;
102 case TRAU_FT_OM_UP:
103 case TRAU_FT_OM_DOWN:
104 case TRAU_FT_DATA_UP:
105 case TRAU_FT_DATA_DOWN:
106 case TRAU_FT_D145_SYNC:
107 case TRAU_FT_EDATA:
108 fprintf(stderr, "unimplemented TRAU Frame Type 0x%02x\n", cbits5);
109 return -1;
110 break;
111 default:
112 fprintf(stderr, "unknown TRAU Frame Type 0x%02x\n", cbits5);
113 return -1;
114 break;
115 }
116
117 return 0;
118}