blob: 753d01f95ee5e47e274a6d764494954f4282d52c [file] [log] [blame]
Harald Welte1fa60c82009-02-09 18:13:26 +00001/* Simple TRAU frame reflector to route voice calls */
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 <errno.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26
27#include <openbsc/gsm_data.h>
28#include <openbsc/trau_frame.h>
29#include <openbsc/trau_mux.h>
30#include <openbsc/subchan_demux.h>
31#include <openbsc/e1_input.h>
32
33struct map_entry {
34 struct llist_head list;
35 struct gsm_e1_subslot src, dst;
36};
37
38static LLIST_HEAD(ss_map);
39
40/* map one particular subslot to another subslot */
41int trau_mux_map(const struct gsm_e1_subslot *src,
42 const struct gsm_e1_subslot *dst)
43{
44 struct map_entry *me = malloc(sizeof(*me));
45 if (!me)
46 return -ENOMEM;
47
48 memcpy(&me->src, src, sizeof(me->src));
49 memcpy(&me->dst, dst, sizeof(me->dst));
50 llist_add(&me->list, &ss_map);
51
52 return 0;
53}
54
55/* unmap one particular subslot from another subslot */
56int trau_mux_unmap(const struct gsm_e1_subslot *ss)
57{
58 struct map_entry *me, *me2;
59
60 llist_for_each_entry_safe(me, me2, &ss_map, list) {
61 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
62 !memcmp(&me->dst, ss, sizeof(*ss))) {
63 llist_del(&me->list);
64 return 0;
65 }
66 }
67 return -ENOENT;
68}
69
70/* look-up an enty in the TRAU mux map */
71static struct gsm_e1_subslot *
72lookup_trau_mux_map(const struct gsm_e1_subslot *src)
73{
74 struct map_entry *me;
75
76 llist_for_each_entry(me, &ss_map, list) {
77 if (!memcmp(&me->src, src, sizeof(*src)))
78 return &me->dst;
79 if (!memcmp(&me->dst, src, sizeof(*src)))
80 return &me->src;
81 }
82 return NULL;
83}
84
85/* we get called by subchan_demux */
86int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
87 const u_int8_t *trau_bits, int num_bits)
88{
89 struct decoded_trau_frame tf;
90 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
91 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
92 struct subch_mux *mx;
93
94 if (!dst_e1_ss)
95 return -EINVAL;
96
97 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
98 if (!mx)
99 return -EINVAL;
100
101 /* decode TRAU, change it to downlink, re-encode */
102 decode_trau_frame(&tf, trau_bits);
103 trau_frame_up2down(&tf);
104 encode_trau_frame(trau_bits_out, &tf);
105
106 /* and send it to the muxer */
107 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
108 TRAU_FRAME_BITS);
109}