blob: 94d11cfc6af2c66d49e1d662afc2fc434ae5f7b1 [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>
Harald Welte41e16682009-02-18 03:34:55 +000032#include <openbsc/debug.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000033
34struct map_entry {
35 struct llist_head list;
36 struct gsm_e1_subslot src, dst;
37};
38
39static LLIST_HEAD(ss_map);
40
41/* map one particular subslot to another subslot */
42int trau_mux_map(const struct gsm_e1_subslot *src,
43 const struct gsm_e1_subslot *dst)
44{
45 struct map_entry *me = malloc(sizeof(*me));
46 if (!me)
47 return -ENOMEM;
48
Harald Welte26aa6a12009-02-19 15:14:23 +000049 /* make sure to get rid of any stale old mappings */
50 trau_mux_unmap(src);
51 trau_mux_unmap(dst);
52
Harald Welte1fa60c82009-02-09 18:13:26 +000053 memcpy(&me->src, src, sizeof(me->src));
54 memcpy(&me->dst, dst, sizeof(me->dst));
55 llist_add(&me->list, &ss_map);
56
57 return 0;
58}
59
Harald Welte26aa6a12009-02-19 15:14:23 +000060int trau_mux_map_lchan(const struct gsm_lchan *src,
61 const struct gsm_lchan *dst)
62{
63 struct gsm_e1_subslot *src_ss, *dst_ss;
64
65 src_ss = &src->ts->e1_link;
66 dst_ss = &dst->ts->e1_link;
67
68 return trau_mux_map(src_ss, dst_ss);
69}
70
71
Harald Welte1fa60c82009-02-09 18:13:26 +000072/* unmap one particular subslot from another subslot */
73int trau_mux_unmap(const struct gsm_e1_subslot *ss)
74{
75 struct map_entry *me, *me2;
76
77 llist_for_each_entry_safe(me, me2, &ss_map, list) {
78 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
79 !memcmp(&me->dst, ss, sizeof(*ss))) {
80 llist_del(&me->list);
81 return 0;
82 }
83 }
84 return -ENOENT;
85}
86
87/* look-up an enty in the TRAU mux map */
88static struct gsm_e1_subslot *
89lookup_trau_mux_map(const struct gsm_e1_subslot *src)
90{
91 struct map_entry *me;
92
93 llist_for_each_entry(me, &ss_map, list) {
94 if (!memcmp(&me->src, src, sizeof(*src)))
95 return &me->dst;
96 if (!memcmp(&me->dst, src, sizeof(*src)))
97 return &me->src;
98 }
99 return NULL;
100}
101
102/* we get called by subchan_demux */
103int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
104 const u_int8_t *trau_bits, int num_bits)
105{
106 struct decoded_trau_frame tf;
107 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
108 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
109 struct subch_mux *mx;
Harald Welte41e16682009-02-18 03:34:55 +0000110 int rc;
Harald Welte1fa60c82009-02-09 18:13:26 +0000111
112 if (!dst_e1_ss)
113 return -EINVAL;
114
115 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
116 if (!mx)
117 return -EINVAL;
118
119 /* decode TRAU, change it to downlink, re-encode */
Harald Welte41e16682009-02-18 03:34:55 +0000120 rc = decode_trau_frame(&tf, trau_bits);
121 if (rc)
122 return rc;
123
Harald Welte1fa60c82009-02-09 18:13:26 +0000124 trau_frame_up2down(&tf);
125 encode_trau_frame(trau_bits_out, &tf);
126
127 /* and send it to the muxer */
128 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
129 TRAU_FRAME_BITS);
130}