blob: 3823c11b6c0e385b611742e99d6c90844f525064 [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
49 memcpy(&me->src, src, sizeof(me->src));
50 memcpy(&me->dst, dst, sizeof(me->dst));
51 llist_add(&me->list, &ss_map);
52
53 return 0;
54}
55
56/* unmap one particular subslot from another subslot */
57int trau_mux_unmap(const struct gsm_e1_subslot *ss)
58{
59 struct map_entry *me, *me2;
60
61 llist_for_each_entry_safe(me, me2, &ss_map, list) {
62 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
63 !memcmp(&me->dst, ss, sizeof(*ss))) {
64 llist_del(&me->list);
65 return 0;
66 }
67 }
68 return -ENOENT;
69}
70
71/* look-up an enty in the TRAU mux map */
72static struct gsm_e1_subslot *
73lookup_trau_mux_map(const struct gsm_e1_subslot *src)
74{
75 struct map_entry *me;
76
77 llist_for_each_entry(me, &ss_map, list) {
78 if (!memcmp(&me->src, src, sizeof(*src)))
79 return &me->dst;
80 if (!memcmp(&me->dst, src, sizeof(*src)))
81 return &me->src;
82 }
83 return NULL;
84}
85
86/* we get called by subchan_demux */
87int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
88 const u_int8_t *trau_bits, int num_bits)
89{
90 struct decoded_trau_frame tf;
91 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
92 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
93 struct subch_mux *mx;
Harald Welte41e16682009-02-18 03:34:55 +000094 int rc;
Harald Welte1fa60c82009-02-09 18:13:26 +000095
96 if (!dst_e1_ss)
97 return -EINVAL;
98
99 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
100 if (!mx)
101 return -EINVAL;
102
103 /* decode TRAU, change it to downlink, re-encode */
Harald Welte41e16682009-02-18 03:34:55 +0000104 rc = decode_trau_frame(&tf, trau_bits);
105 if (rc)
106 return rc;
107
Harald Welte1fa60c82009-02-09 18:13:26 +0000108 trau_frame_up2down(&tf);
109 encode_trau_frame(trau_bits_out, &tf);
110
111 /* and send it to the muxer */
112 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
113 TRAU_FRAME_BITS);
114}