add (untested) TRAU frame decoder with support for FR/EFR and AMR
diff --git a/include/openbsc/trau_frame.h b/include/openbsc/trau_frame.h
new file mode 100644
index 0000000..d307c52
--- /dev/null
+++ b/include/openbsc/trau_frame.h
@@ -0,0 +1,59 @@
+#ifndef _TRAU_FRAME_H
+#define _TRAU_FRAME_H
+/* TRAU frame handling according to GSM TS 08.60 */
+
+/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <sys/types.h>
+
+/* 21 for FR/EFR, 25 for AMR, 15 for OM, 15 for data, 13 for E-data, 21 idle */
+#define MAX_C_BITS 25
+/* 260 for FR/EFR, 256 for AMR, 264 for OM, 288 for E-data */
+#define MAX_D_BITS 288
+/* for all speech frames */
+#define MAX_T_BITS 4
+/* for OM */
+#define MAX_S_BITS 6
+/* for E-data */
+#define MAX_M_BITS 2
+
+struct decoded_trau_frame {
+ u_int8_t c_bits[MAX_C_BITS];
+ u_int8_t d_bits[MAX_D_BITS];
+ u_int8_t t_bits[MAX_T_BITS];
+ u_int8_t s_bits[MAX_S_BITS];
+ u_int8_t m_bits[MAX_M_BITS];
+};
+
+#define TRAU_FT_FR_UP 0x02 /* 0 0 0 1 0 - 3.5.1.1.1 */
+#define TRAU_FT_FR_DOWN 0x1c /* 1 1 1 0 0 - 3.5.1.1.1 */
+#define TRAU_FT_EFR 0x1a /* 1 1 0 1 0 - 3.5.1.1.1 */
+#define TRAU_FT_AMR 0x06 /* 0 0 1 1 0 - 3.5.1.2 */
+#define TRAU_FT_OM_UP 0x07 /* 0 0 1 0 1 - 3.5.2 */
+#define TRAU_FT_OM_DOWN 0x1b /* 1 1 0 1 1 - 3.5.2 */
+#define TRAU_FT_DATA_UP 0x08 /* 0 1 0 0 0 - 3.5.3 */
+#define TRAU_FT_DATA_DOWN 0x16 /* 1 0 1 1 0 - 3.5.3 */
+#define TRAU_FT_D145_SYNC 0x14 /* 1 0 1 0 0 - 3.5.3 */
+#define TRAU_FT_EDATA 0x1f /* 1 1 1 1 1 - 3.5.4 */
+
+
+int decode_trau_frame(struct decoded_trau_frame *fr, u_int8_t *trau_bits);
+
+#endif /* _TRAU_FRAME_H */
diff --git a/src/trau_frame.c b/src/trau_frame.c
new file mode 100644
index 0000000..e0238ce
--- /dev/null
+++ b/src/trau_frame.c
@@ -0,0 +1,115 @@
+/* TRAU frame handling according to GSM TS 08.60 */
+
+/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <openbsc/trau_frame.h>
+
+static u_int32_t get_bits(u_int8_t *bitbuf, int offset, int num)
+{
+ int i;
+ u_int32_t ret = 0;
+
+ for (i = offset; i < offset + num; i++) {
+ ret = ret << 1;
+ if (bitbuf[i])
+ ret |= 1;
+ }
+ return ret;
+}
+
+/* Decode according to 3.1.1 */
+static void decode_fr(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
+{
+ int i;
+ int d_idx = 0;
+
+ /* C1 .. C15 */
+ memcpy(fr->c_bits+0, trau_bits+17, 15);
+ /* C16 .. C21 */
+ memcpy(fr->c_bits+15, trau_bits+310, 6);
+ /* T1 .. T4 */
+ memcpy(fr->t_bits+0, trau_bits+316, 4);
+ /* D1 .. D255 */
+ for (i = 32; i < 304; i+= 16) {
+ memcpy(fr->d_bits + d_idx, trau_bits+i+1, 15);
+ d_idx += 15;
+ }
+ /* D256 .. D260 */
+ memcpy(fr->d_bits + d_idx, trau_bits + 305, 5);
+}
+
+static void decode_amr(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
+{
+ int i;
+ int d_idx = 0;
+
+ /* C1 .. C15 */
+ memcpy(fr->c_bits+0, trau_bits+17, 15);
+ /* C16 .. C25 */
+ memcpy(fr->c_bits+15, trau_bits+33, 10);
+ /* T1 .. T4 */
+ memcpy(fr->t_bits+0, trau_bits+316, 4);
+ /* D1 .. D5 */
+ memcpy(fr->d_bits, trau_bits+43, 5);
+ /* D6 .. D245 */
+ for (i = 48; i < 304; i += 16) {
+ memcpy(fr->d_bits + d_idx, trau_bits+i+1, 15);
+ d_idx += 15;
+ }
+ /* D246 .. D256 */
+ memcpy(fr->d_bits + d_idx, trau_bits + 305, 11);
+}
+
+int decode_trau_frame(struct decoded_trau_frame *fr, u_int8_t *trau_bits)
+{
+ u_int8_t cbits5 = get_bits(trau_bits, 17, 5);
+
+ switch (cbits5) {
+ case TRAU_FT_FR_UP:
+ case TRAU_FT_FR_DOWN:
+ case TRAU_FT_EFR:
+ decode_fr(fr, trau_bits);
+ break;
+ case TRAU_FT_AMR:
+ decode_amr(fr, trau_bits);
+ break;
+ case TRAU_FT_OM_UP:
+ case TRAU_FT_OM_DOWN:
+ case TRAU_FT_DATA_UP:
+ case TRAU_FT_DATA_DOWN:
+ case TRAU_FT_D145_SYNC:
+ case TRAU_FT_EDATA:
+ fprintf(stderr, "unimplemented TRAU Frame Type 0x%02x\n", cbits5);
+ return -1;
+ break;
+ default:
+ fprintf(stderr, "unknown TRAU Frame Type 0x%02x\n", cbits5);
+ return -1;
+ break;
+ }
+
+ return 0;
+}