blob: 2616785c00651dfaa01c01693d0b9c3d481bcb4a [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 Weltedbd2ea82009-02-19 17:07:01 +000049 DEBUGP(DCC, "Setting up TRAU mux map between (e1=%u,ts=%u,ss=%u) "
50 "and (e1=%u,ts=%u,ss=%u)\n",
51 src->e1_nr, src->e1_ts, src->e1_ts_ss,
52 dst->e1_nr, dst->e1_ts, dst->e1_ts_ss);
53
Harald Welte26aa6a12009-02-19 15:14:23 +000054 /* make sure to get rid of any stale old mappings */
55 trau_mux_unmap(src);
56 trau_mux_unmap(dst);
57
Harald Welte1fa60c82009-02-09 18:13:26 +000058 memcpy(&me->src, src, sizeof(me->src));
59 memcpy(&me->dst, dst, sizeof(me->dst));
60 llist_add(&me->list, &ss_map);
61
62 return 0;
63}
64
Harald Welte26aa6a12009-02-19 15:14:23 +000065int trau_mux_map_lchan(const struct gsm_lchan *src,
66 const struct gsm_lchan *dst)
67{
68 struct gsm_e1_subslot *src_ss, *dst_ss;
69
70 src_ss = &src->ts->e1_link;
71 dst_ss = &dst->ts->e1_link;
72
73 return trau_mux_map(src_ss, dst_ss);
74}
75
76
Harald Welte1fa60c82009-02-09 18:13:26 +000077/* unmap one particular subslot from another subslot */
78int trau_mux_unmap(const struct gsm_e1_subslot *ss)
79{
80 struct map_entry *me, *me2;
81
82 llist_for_each_entry_safe(me, me2, &ss_map, list) {
83 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
84 !memcmp(&me->dst, ss, sizeof(*ss))) {
85 llist_del(&me->list);
86 return 0;
87 }
88 }
89 return -ENOENT;
90}
91
92/* look-up an enty in the TRAU mux map */
93static struct gsm_e1_subslot *
94lookup_trau_mux_map(const struct gsm_e1_subslot *src)
95{
96 struct map_entry *me;
97
98 llist_for_each_entry(me, &ss_map, list) {
99 if (!memcmp(&me->src, src, sizeof(*src)))
100 return &me->dst;
101 if (!memcmp(&me->dst, src, sizeof(*src)))
102 return &me->src;
103 }
104 return NULL;
105}
106
107/* we get called by subchan_demux */
108int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
109 const u_int8_t *trau_bits, int num_bits)
110{
111 struct decoded_trau_frame tf;
112 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
113 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
114 struct subch_mux *mx;
Harald Welte41e16682009-02-18 03:34:55 +0000115 int rc;
Harald Welte1fa60c82009-02-09 18:13:26 +0000116
117 if (!dst_e1_ss)
118 return -EINVAL;
119
120 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
121 if (!mx)
122 return -EINVAL;
123
124 /* decode TRAU, change it to downlink, re-encode */
Harald Welte41e16682009-02-18 03:34:55 +0000125 rc = decode_trau_frame(&tf, trau_bits);
126 if (rc)
127 return rc;
128
Harald Welte1fa60c82009-02-09 18:13:26 +0000129 trau_frame_up2down(&tf);
130 encode_trau_frame(trau_bits_out, &tf);
131
132 /* and send it to the muxer */
133 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
134 TRAU_FRAME_BITS);
135}