blob: 96f8589929741aea4fb5dc7f40e81ce0c156d76f [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
Harald Welte45b407a2009-05-23 15:51:12 +000039struct upqueue_entry {
40 struct llist_head list;
41 struct gsm_network *net;
42 struct gsm_e1_subslot src;
43 u_int32_t callref;
44};
45
Harald Welte1fa60c82009-02-09 18:13:26 +000046static LLIST_HEAD(ss_map);
Harald Welte45b407a2009-05-23 15:51:12 +000047static LLIST_HEAD(ss_upqueue);
Harald Welte1fa60c82009-02-09 18:13:26 +000048
49/* map one particular subslot to another subslot */
50int trau_mux_map(const struct gsm_e1_subslot *src,
51 const struct gsm_e1_subslot *dst)
52{
53 struct map_entry *me = malloc(sizeof(*me));
54 if (!me)
55 return -ENOMEM;
56
Harald Weltedbd2ea82009-02-19 17:07:01 +000057 DEBUGP(DCC, "Setting up TRAU mux map between (e1=%u,ts=%u,ss=%u) "
58 "and (e1=%u,ts=%u,ss=%u)\n",
59 src->e1_nr, src->e1_ts, src->e1_ts_ss,
60 dst->e1_nr, dst->e1_ts, dst->e1_ts_ss);
61
Harald Welte26aa6a12009-02-19 15:14:23 +000062 /* make sure to get rid of any stale old mappings */
Harald Welte45b407a2009-05-23 15:51:12 +000063 trau_mux_unmap(src, 0);
64 trau_mux_unmap(dst, 0);
Harald Welte26aa6a12009-02-19 15:14:23 +000065
Harald Welte1fa60c82009-02-09 18:13:26 +000066 memcpy(&me->src, src, sizeof(me->src));
67 memcpy(&me->dst, dst, sizeof(me->dst));
68 llist_add(&me->list, &ss_map);
69
70 return 0;
71}
72
Harald Welte26aa6a12009-02-19 15:14:23 +000073int trau_mux_map_lchan(const struct gsm_lchan *src,
74 const struct gsm_lchan *dst)
75{
76 struct gsm_e1_subslot *src_ss, *dst_ss;
77
78 src_ss = &src->ts->e1_link;
79 dst_ss = &dst->ts->e1_link;
80
81 return trau_mux_map(src_ss, dst_ss);
82}
83
84
Harald Welte1fa60c82009-02-09 18:13:26 +000085/* unmap one particular subslot from another subslot */
Harald Welte45b407a2009-05-23 15:51:12 +000086int trau_mux_unmap(const struct gsm_e1_subslot *ss, u_int32_t callref)
Harald Welte1fa60c82009-02-09 18:13:26 +000087{
88 struct map_entry *me, *me2;
Harald Welte45b407a2009-05-23 15:51:12 +000089 struct upqueue_entry *ue, *ue2;
Harald Welte1fa60c82009-02-09 18:13:26 +000090
Harald Welte45b407a2009-05-23 15:51:12 +000091 if (ss)
92 llist_for_each_entry_safe(me, me2, &ss_map, list) {
93 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
94 !memcmp(&me->dst, ss, sizeof(*ss))) {
95 llist_del(&me->list);
96 return 0;
97 }
98 }
99 llist_for_each_entry_safe(ue, ue2, &ss_upqueue, list) {
100 if (ue->callref == callref) {
101 llist_del(&ue->list);
102 return 0;
103 }
104 if (ss && !memcmp(&ue->src, ss, sizeof(*ss))) {
105 llist_del(&ue->list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000106 return 0;
107 }
108 }
109 return -ENOENT;
110}
111
112/* look-up an enty in the TRAU mux map */
113static struct gsm_e1_subslot *
114lookup_trau_mux_map(const struct gsm_e1_subslot *src)
115{
116 struct map_entry *me;
117
118 llist_for_each_entry(me, &ss_map, list) {
119 if (!memcmp(&me->src, src, sizeof(*src)))
120 return &me->dst;
121 if (!memcmp(&me->dst, src, sizeof(*src)))
122 return &me->src;
123 }
124 return NULL;
125}
126
Harald Welte45b407a2009-05-23 15:51:12 +0000127/* look-up an enty in the TRAU upqueue */
128struct upqueue_entry *
129lookup_trau_upqueue(const struct gsm_e1_subslot *src)
130{
131 struct upqueue_entry *ue;
132
133 llist_for_each_entry(ue, &ss_upqueue, list) {
134 if (!memcmp(&ue->src, src, sizeof(*src)))
135 return ue;
136 }
137 return NULL;
138}
139
Harald Welte1fa60c82009-02-09 18:13:26 +0000140/* we get called by subchan_demux */
141int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
142 const u_int8_t *trau_bits, int num_bits)
143{
144 struct decoded_trau_frame tf;
145 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
146 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
147 struct subch_mux *mx;
Harald Welte41e16682009-02-18 03:34:55 +0000148 int rc;
Harald Welte1fa60c82009-02-09 18:13:26 +0000149
Harald Welte45b407a2009-05-23 15:51:12 +0000150 /* decode TRAU, change it to downlink, re-encode */
151 rc = decode_trau_frame(&tf, trau_bits);
152 if (rc)
153 return rc;
154
Harald Welte1fa60c82009-02-09 18:13:26 +0000155 if (!dst_e1_ss)
156 return -EINVAL;
157
158 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
159 if (!mx)
160 return -EINVAL;
161
Harald Welte1fa60c82009-02-09 18:13:26 +0000162 trau_frame_up2down(&tf);
163 encode_trau_frame(trau_bits_out, &tf);
164
165 /* and send it to the muxer */
166 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
167 TRAU_FRAME_BITS);
168}
Harald Welte45b407a2009-05-23 15:51:12 +0000169
170/* add receiver instance for lchan and callref */
171int trau_recv_lchan(struct gsm_lchan *lchan, u_int32_t callref)
172{
173 struct gsm_e1_subslot *src_ss;
174 struct upqueue_entry *ue = malloc(sizeof(*ue));
175
176 if (!ue)
177 return -ENOMEM;
178
179 src_ss = &lchan->ts->e1_link;
180
181 DEBUGP(DCC, "Setting up TRAU receiver (e1=%u,ts=%u,ss=%u) "
182 "and (callref 0x%x)\n",
183 src_ss->e1_nr, src_ss->e1_ts, src_ss->e1_ts_ss,
184 callref);
185
186 /* make sure to get rid of any stale old mappings */
187 trau_mux_unmap(src_ss, callref);
188
189 memcpy(&ue->src, src_ss, sizeof(ue->src));
190 ue->net = lchan->ts->trx->bts->network;
191 ue->callref = callref;
192 llist_add(&ue->list, &ss_upqueue);
193
194 return 0;
195}
196
197int trau_send_lchan(struct gsm_lchan *lchan, struct decoded_trau_frame *tf)
198{
199 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
200 struct gsm_e1_subslot *dst_e1_ss = &lchan->ts->e1_link;
201 struct subch_mux *mx;
202
203 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
204 if (!mx)
205 return -EINVAL;
206
207 encode_trau_frame(trau_bits_out, tf);
208
209 /* and send it to the muxer */
210 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
211 TRAU_FRAME_BITS);
212}