blob: 04febbd6311168eab4fb9268e585d23e5e039c32 [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 Welte2cf161b2009-06-20 22:36:41 +020033#include <openbsc/talloc.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000034
35struct map_entry {
36 struct llist_head list;
37 struct gsm_e1_subslot src, dst;
38};
39
Harald Welte45b407a2009-05-23 15:51:12 +000040struct upqueue_entry {
41 struct llist_head list;
42 struct gsm_network *net;
43 struct gsm_e1_subslot src;
44 u_int32_t callref;
45};
46
Harald Welte1fa60c82009-02-09 18:13:26 +000047static LLIST_HEAD(ss_map);
Harald Welte45b407a2009-05-23 15:51:12 +000048static LLIST_HEAD(ss_upqueue);
Harald Welte1fa60c82009-02-09 18:13:26 +000049
Harald Welte2cf161b2009-06-20 22:36:41 +020050static void *tall_map_ctx, *tall_upq_ctx;
51
Harald Welte1fa60c82009-02-09 18:13:26 +000052/* map one particular subslot to another subslot */
53int trau_mux_map(const struct gsm_e1_subslot *src,
54 const struct gsm_e1_subslot *dst)
55{
Harald Welte2cf161b2009-06-20 22:36:41 +020056 struct map_entry *me;
57
58 if (!tall_map_ctx)
59 tall_map_ctx = talloc_named_const(tall_bsc_ctx, 1,
60 "trau_map_entry");
61
62 me = talloc(tall_map_ctx, struct map_entry);
Harald Welte1fa60c82009-02-09 18:13:26 +000063 if (!me)
64 return -ENOMEM;
65
Harald Weltedbd2ea82009-02-19 17:07:01 +000066 DEBUGP(DCC, "Setting up TRAU mux map between (e1=%u,ts=%u,ss=%u) "
67 "and (e1=%u,ts=%u,ss=%u)\n",
68 src->e1_nr, src->e1_ts, src->e1_ts_ss,
69 dst->e1_nr, dst->e1_ts, dst->e1_ts_ss);
70
Harald Welte26aa6a12009-02-19 15:14:23 +000071 /* make sure to get rid of any stale old mappings */
Harald Welte45b407a2009-05-23 15:51:12 +000072 trau_mux_unmap(src, 0);
73 trau_mux_unmap(dst, 0);
Harald Welte26aa6a12009-02-19 15:14:23 +000074
Harald Welte1fa60c82009-02-09 18:13:26 +000075 memcpy(&me->src, src, sizeof(me->src));
76 memcpy(&me->dst, dst, sizeof(me->dst));
77 llist_add(&me->list, &ss_map);
78
79 return 0;
80}
81
Harald Welte26aa6a12009-02-19 15:14:23 +000082int trau_mux_map_lchan(const struct gsm_lchan *src,
83 const struct gsm_lchan *dst)
84{
85 struct gsm_e1_subslot *src_ss, *dst_ss;
86
87 src_ss = &src->ts->e1_link;
88 dst_ss = &dst->ts->e1_link;
89
90 return trau_mux_map(src_ss, dst_ss);
91}
92
93
Harald Welte1fa60c82009-02-09 18:13:26 +000094/* unmap one particular subslot from another subslot */
Harald Welte45b407a2009-05-23 15:51:12 +000095int trau_mux_unmap(const struct gsm_e1_subslot *ss, u_int32_t callref)
Harald Welte1fa60c82009-02-09 18:13:26 +000096{
97 struct map_entry *me, *me2;
Harald Welte45b407a2009-05-23 15:51:12 +000098 struct upqueue_entry *ue, *ue2;
Harald Welte1fa60c82009-02-09 18:13:26 +000099
Harald Welte45b407a2009-05-23 15:51:12 +0000100 if (ss)
101 llist_for_each_entry_safe(me, me2, &ss_map, list) {
102 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
103 !memcmp(&me->dst, ss, sizeof(*ss))) {
104 llist_del(&me->list);
105 return 0;
106 }
107 }
108 llist_for_each_entry_safe(ue, ue2, &ss_upqueue, list) {
109 if (ue->callref == callref) {
110 llist_del(&ue->list);
111 return 0;
112 }
113 if (ss && !memcmp(&ue->src, ss, sizeof(*ss))) {
114 llist_del(&ue->list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000115 return 0;
116 }
117 }
118 return -ENOENT;
119}
120
121/* look-up an enty in the TRAU mux map */
122static struct gsm_e1_subslot *
123lookup_trau_mux_map(const struct gsm_e1_subslot *src)
124{
125 struct map_entry *me;
126
127 llist_for_each_entry(me, &ss_map, list) {
128 if (!memcmp(&me->src, src, sizeof(*src)))
129 return &me->dst;
130 if (!memcmp(&me->dst, src, sizeof(*src)))
131 return &me->src;
132 }
133 return NULL;
134}
135
Harald Welte45b407a2009-05-23 15:51:12 +0000136/* look-up an enty in the TRAU upqueue */
137struct upqueue_entry *
138lookup_trau_upqueue(const struct gsm_e1_subslot *src)
139{
140 struct upqueue_entry *ue;
141
142 llist_for_each_entry(ue, &ss_upqueue, list) {
143 if (!memcmp(&ue->src, src, sizeof(*src)))
144 return ue;
145 }
146 return NULL;
147}
148
Harald Welte1fa60c82009-02-09 18:13:26 +0000149/* we get called by subchan_demux */
150int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
151 const u_int8_t *trau_bits, int num_bits)
152{
153 struct decoded_trau_frame tf;
154 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
155 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
156 struct subch_mux *mx;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800157 struct upqueue_entry *ue;
158 struct msgb *msg;
159 struct gsm_trau_frame *frame;
Harald Welte41e16682009-02-18 03:34:55 +0000160 int rc;
Harald Welte1fa60c82009-02-09 18:13:26 +0000161
Harald Welte45b407a2009-05-23 15:51:12 +0000162 /* decode TRAU, change it to downlink, re-encode */
163 rc = decode_trau_frame(&tf, trau_bits);
164 if (rc)
165 return rc;
166
Harald Welte4bfdfe72009-06-10 23:11:52 +0800167 if (!dst_e1_ss) {
168 /* frame shall be sent to upqueue */
169 if (!(ue = lookup_trau_upqueue(src_e1_ss)))
170 return -EINVAL;
171 if (!ue->callref)
172 return -EINVAL;
Harald Welte966636f2009-06-26 19:39:35 +0200173 msg = msgb_alloc(sizeof(struct gsm_trau_frame) + sizeof(tf),
174 "TRAU");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800175 if (!msg)
176 return -ENOMEM;
177 frame = (struct gsm_trau_frame *)msg->data;
178 frame->msg_type = GSM_TRAU_FRAME;
179 frame->callref = ue->callref;
180 memcpy(frame->data, &tf, sizeof(tf));
181 msgb_enqueue(&ue->net->upqueue, msg);
182
183 return 0;
184 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000185
186 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
187 if (!mx)
188 return -EINVAL;
189
Harald Welte1fa60c82009-02-09 18:13:26 +0000190 trau_frame_up2down(&tf);
191 encode_trau_frame(trau_bits_out, &tf);
192
193 /* and send it to the muxer */
194 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
195 TRAU_FRAME_BITS);
196}
Harald Welte45b407a2009-05-23 15:51:12 +0000197
198/* add receiver instance for lchan and callref */
199int trau_recv_lchan(struct gsm_lchan *lchan, u_int32_t callref)
200{
201 struct gsm_e1_subslot *src_ss;
Harald Welte2cf161b2009-06-20 22:36:41 +0200202 struct upqueue_entry *ue;
Harald Welte45b407a2009-05-23 15:51:12 +0000203
Harald Welte2cf161b2009-06-20 22:36:41 +0200204 if (!tall_upq_ctx)
205 tall_upq_ctx = talloc_named_const(tall_bsc_ctx, 1,
206 "trau_upq_entry");
207
208 ue = talloc(tall_upq_ctx, struct upqueue_entry);
Harald Welte45b407a2009-05-23 15:51:12 +0000209 if (!ue)
210 return -ENOMEM;
211
212 src_ss = &lchan->ts->e1_link;
213
214 DEBUGP(DCC, "Setting up TRAU receiver (e1=%u,ts=%u,ss=%u) "
215 "and (callref 0x%x)\n",
216 src_ss->e1_nr, src_ss->e1_ts, src_ss->e1_ts_ss,
217 callref);
218
219 /* make sure to get rid of any stale old mappings */
220 trau_mux_unmap(src_ss, callref);
221
222 memcpy(&ue->src, src_ss, sizeof(ue->src));
223 ue->net = lchan->ts->trx->bts->network;
224 ue->callref = callref;
225 llist_add(&ue->list, &ss_upqueue);
226
227 return 0;
228}
229
230int trau_send_lchan(struct gsm_lchan *lchan, struct decoded_trau_frame *tf)
231{
232 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
233 struct gsm_e1_subslot *dst_e1_ss = &lchan->ts->e1_link;
234 struct subch_mux *mx;
235
236 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
237 if (!mx)
238 return -EINVAL;
239
240 encode_trau_frame(trau_bits_out, tf);
241
242 /* and send it to the muxer */
243 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
244 TRAU_FRAME_BITS);
245}