blob: 196d15fa7b99bf381aca3513e4510aa6c78ea397 [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>
33
34struct map_entry {
35 struct llist_head list;
36 struct gsm_e1_subslot src, dst;
37};
38
39struct 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
46static LLIST_HEAD(ss_map);
47static LLIST_HEAD(ss_upqueue);
48
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
57 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
62 /* make sure to get rid of any stale old mappings */
63 trau_mux_unmap(src, 0);
64 trau_mux_unmap(dst, 0);
65
66 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
73int 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
85/* unmap one particular subslot from another subslot */
86int trau_mux_unmap(const struct gsm_e1_subslot *ss, u_int32_t callref)
87{
88 struct map_entry *me, *me2;
89 struct upqueue_entry *ue, *ue2;
90
91 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);
106 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
127/* 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
140/* 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 Welte03740842009-06-10 23:11:52 +0800148 struct upqueue_entry *ue;
149 struct msgb *msg;
150 struct gsm_trau_frame *frame;
Harald Welte59b04682009-06-10 05:40:52 +0800151 int rc;
152
153 /* decode TRAU, change it to downlink, re-encode */
154 rc = decode_trau_frame(&tf, trau_bits);
155 if (rc)
156 return rc;
157
Harald Welte03740842009-06-10 23:11:52 +0800158 if (!dst_e1_ss) {
159 /* frame shall be sent to upqueue */
160 if (!(ue = lookup_trau_upqueue(src_e1_ss)))
161 return -EINVAL;
162 if (!ue->callref)
163 return -EINVAL;
164 msg = msgb_alloc(sizeof(struct gsm_trau_frame) + sizeof(tf));
165 if (!msg)
166 return -ENOMEM;
167 frame = (struct gsm_trau_frame *)msg->data;
168 frame->msg_type = GSM_TRAU_FRAME;
169 frame->callref = ue->callref;
170 memcpy(frame->data, &tf, sizeof(tf));
171 msgb_enqueue(&ue->net->upqueue, msg);
172
173 return 0;
174 }
Harald Welte59b04682009-06-10 05:40:52 +0800175
176 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
177 if (!mx)
178 return -EINVAL;
179
180 trau_frame_up2down(&tf);
181 encode_trau_frame(trau_bits_out, &tf);
182
183 /* and send it to the muxer */
184 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
185 TRAU_FRAME_BITS);
186}
187
188/* add receiver instance for lchan and callref */
189int trau_recv_lchan(struct gsm_lchan *lchan, u_int32_t callref)
190{
191 struct gsm_e1_subslot *src_ss;
192 struct upqueue_entry *ue = malloc(sizeof(*ue));
193
194 if (!ue)
195 return -ENOMEM;
196
197 src_ss = &lchan->ts->e1_link;
198
199 DEBUGP(DCC, "Setting up TRAU receiver (e1=%u,ts=%u,ss=%u) "
200 "and (callref 0x%x)\n",
201 src_ss->e1_nr, src_ss->e1_ts, src_ss->e1_ts_ss,
202 callref);
203
204 /* make sure to get rid of any stale old mappings */
205 trau_mux_unmap(src_ss, callref);
206
207 memcpy(&ue->src, src_ss, sizeof(ue->src));
208 ue->net = lchan->ts->trx->bts->network;
209 ue->callref = callref;
210 llist_add(&ue->list, &ss_upqueue);
211
212 return 0;
213}
214
215int trau_send_lchan(struct gsm_lchan *lchan, struct decoded_trau_frame *tf)
216{
217 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
218 struct gsm_e1_subslot *dst_e1_ss = &lchan->ts->e1_link;
219 struct subch_mux *mx;
220
221 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
222 if (!mx)
223 return -EINVAL;
224
225 encode_trau_frame(trau_bits_out, tf);
226
227 /* and send it to the muxer */
228 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
229 TRAU_FRAME_BITS);
230}