blob: 7ea65ce35a423ead3ae5ccac7bf908e2a4ae3b2c [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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>
32#include <openbsc/debug.h>
Harald Weltea8379772009-06-20 22:36:41 +020033#include <openbsc/talloc.h>
Harald Welte59b04682009-06-10 05:40:52 +080034
35struct map_entry {
36 struct llist_head list;
37 struct gsm_e1_subslot src, dst;
38};
39
40struct 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
47static LLIST_HEAD(ss_map);
48static LLIST_HEAD(ss_upqueue);
49
Harald Welte (local)8751ee92009-08-15 02:30:58 +020050void *tall_map_ctx, *tall_upq_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020051
Harald Welte59b04682009-06-10 05:40:52 +080052/* 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 Weltea8379772009-06-20 22:36:41 +020056 struct map_entry *me;
57
Harald Weltea8379772009-06-20 22:36:41 +020058 me = talloc(tall_map_ctx, struct map_entry);
Harald Weltecf2ec4a2009-12-17 23:10:46 +010059 if (!me) {
60 LOGP(DMIB, LOGL_FATAL, "Out of memory\n");
Harald Welte59b04682009-06-10 05:40:52 +080061 return -ENOMEM;
Harald Weltecf2ec4a2009-12-17 23:10:46 +010062 }
Harald Welte59b04682009-06-10 05:40:52 +080063
64 DEBUGP(DCC, "Setting up TRAU mux map between (e1=%u,ts=%u,ss=%u) "
65 "and (e1=%u,ts=%u,ss=%u)\n",
66 src->e1_nr, src->e1_ts, src->e1_ts_ss,
67 dst->e1_nr, dst->e1_ts, dst->e1_ts_ss);
68
69 /* make sure to get rid of any stale old mappings */
70 trau_mux_unmap(src, 0);
71 trau_mux_unmap(dst, 0);
72
73 memcpy(&me->src, src, sizeof(me->src));
74 memcpy(&me->dst, dst, sizeof(me->dst));
75 llist_add(&me->list, &ss_map);
76
77 return 0;
78}
79
80int trau_mux_map_lchan(const struct gsm_lchan *src,
81 const struct gsm_lchan *dst)
82{
83 struct gsm_e1_subslot *src_ss, *dst_ss;
84
85 src_ss = &src->ts->e1_link;
86 dst_ss = &dst->ts->e1_link;
87
88 return trau_mux_map(src_ss, dst_ss);
89}
90
91
92/* unmap one particular subslot from another subslot */
93int trau_mux_unmap(const struct gsm_e1_subslot *ss, u_int32_t callref)
94{
95 struct map_entry *me, *me2;
96 struct upqueue_entry *ue, *ue2;
97
98 if (ss)
99 llist_for_each_entry_safe(me, me2, &ss_map, list) {
100 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
101 !memcmp(&me->dst, ss, sizeof(*ss))) {
102 llist_del(&me->list);
103 return 0;
104 }
105 }
106 llist_for_each_entry_safe(ue, ue2, &ss_upqueue, list) {
107 if (ue->callref == callref) {
108 llist_del(&ue->list);
109 return 0;
110 }
111 if (ss && !memcmp(&ue->src, ss, sizeof(*ss))) {
112 llist_del(&ue->list);
113 return 0;
114 }
115 }
116 return -ENOENT;
117}
118
119/* look-up an enty in the TRAU mux map */
120static struct gsm_e1_subslot *
121lookup_trau_mux_map(const struct gsm_e1_subslot *src)
122{
123 struct map_entry *me;
124
125 llist_for_each_entry(me, &ss_map, list) {
126 if (!memcmp(&me->src, src, sizeof(*src)))
127 return &me->dst;
128 if (!memcmp(&me->dst, src, sizeof(*src)))
129 return &me->src;
130 }
131 return NULL;
132}
133
134/* look-up an enty in the TRAU upqueue */
135struct upqueue_entry *
136lookup_trau_upqueue(const struct gsm_e1_subslot *src)
137{
138 struct upqueue_entry *ue;
139
140 llist_for_each_entry(ue, &ss_upqueue, list) {
141 if (!memcmp(&ue->src, src, sizeof(*src)))
142 return ue;
143 }
144 return NULL;
145}
146
147/* we get called by subchan_demux */
148int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
149 const u_int8_t *trau_bits, int num_bits)
150{
151 struct decoded_trau_frame tf;
152 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
153 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
154 struct subch_mux *mx;
Harald Welte03740842009-06-10 23:11:52 +0800155 struct upqueue_entry *ue;
156 struct msgb *msg;
157 struct gsm_trau_frame *frame;
Harald Welte59b04682009-06-10 05:40:52 +0800158 int rc;
159
160 /* decode TRAU, change it to downlink, re-encode */
161 rc = decode_trau_frame(&tf, trau_bits);
162 if (rc)
163 return rc;
164
Harald Welte03740842009-06-10 23:11:52 +0800165 if (!dst_e1_ss) {
166 /* frame shall be sent to upqueue */
167 if (!(ue = lookup_trau_upqueue(src_e1_ss)))
168 return -EINVAL;
169 if (!ue->callref)
170 return -EINVAL;
Harald Welte9cfc9352009-06-26 19:39:35 +0200171 msg = msgb_alloc(sizeof(struct gsm_trau_frame) + sizeof(tf),
172 "TRAU");
Harald Welte03740842009-06-10 23:11:52 +0800173 if (!msg)
174 return -ENOMEM;
175 frame = (struct gsm_trau_frame *)msg->data;
176 frame->msg_type = GSM_TRAU_FRAME;
177 frame->callref = ue->callref;
178 memcpy(frame->data, &tf, sizeof(tf));
179 msgb_enqueue(&ue->net->upqueue, msg);
180
181 return 0;
182 }
Harald Welte59b04682009-06-10 05:40:52 +0800183
184 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
185 if (!mx)
186 return -EINVAL;
187
188 trau_frame_up2down(&tf);
189 encode_trau_frame(trau_bits_out, &tf);
190
191 /* and send it to the muxer */
192 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
193 TRAU_FRAME_BITS);
194}
195
196/* add receiver instance for lchan and callref */
197int trau_recv_lchan(struct gsm_lchan *lchan, u_int32_t callref)
198{
199 struct gsm_e1_subslot *src_ss;
Harald Weltea8379772009-06-20 22:36:41 +0200200 struct upqueue_entry *ue;
Harald Welte59b04682009-06-10 05:40:52 +0800201
Harald Weltea8379772009-06-20 22:36:41 +0200202 ue = talloc(tall_upq_ctx, struct upqueue_entry);
Harald Welte59b04682009-06-10 05:40:52 +0800203 if (!ue)
204 return -ENOMEM;
205
206 src_ss = &lchan->ts->e1_link;
207
208 DEBUGP(DCC, "Setting up TRAU receiver (e1=%u,ts=%u,ss=%u) "
209 "and (callref 0x%x)\n",
210 src_ss->e1_nr, src_ss->e1_ts, src_ss->e1_ts_ss,
211 callref);
212
213 /* make sure to get rid of any stale old mappings */
214 trau_mux_unmap(src_ss, callref);
215
216 memcpy(&ue->src, src_ss, sizeof(ue->src));
217 ue->net = lchan->ts->trx->bts->network;
218 ue->callref = callref;
219 llist_add(&ue->list, &ss_upqueue);
220
221 return 0;
222}
223
224int trau_send_lchan(struct gsm_lchan *lchan, struct decoded_trau_frame *tf)
225{
226 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
227 struct gsm_e1_subslot *dst_e1_ss = &lchan->ts->e1_link;
228 struct subch_mux *mx;
229
230 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
231 if (!mx)
232 return -EINVAL;
233
234 encode_trau_frame(trau_bits_out, tf);
235
236 /* and send it to the muxer */
237 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
238 TRAU_FRAME_BITS);
239}