blob: 6a19f0c99bab6bf02d92a5222f66596c2d0a379b [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 Welte (local)d19e58b2009-08-15 02:30:58 +020050void *tall_map_ctx, *tall_upq_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020051
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
Harald Welte2cf161b2009-06-20 22:36:41 +020058 me = talloc(tall_map_ctx, struct map_entry);
Harald Welte1fa60c82009-02-09 18:13:26 +000059 if (!me)
60 return -ENOMEM;
61
Harald Weltedbd2ea82009-02-19 17:07:01 +000062 DEBUGP(DCC, "Setting up TRAU mux map between (e1=%u,ts=%u,ss=%u) "
63 "and (e1=%u,ts=%u,ss=%u)\n",
64 src->e1_nr, src->e1_ts, src->e1_ts_ss,
65 dst->e1_nr, dst->e1_ts, dst->e1_ts_ss);
66
Harald Welte26aa6a12009-02-19 15:14:23 +000067 /* make sure to get rid of any stale old mappings */
Harald Welte45b407a2009-05-23 15:51:12 +000068 trau_mux_unmap(src, 0);
69 trau_mux_unmap(dst, 0);
Harald Welte26aa6a12009-02-19 15:14:23 +000070
Harald Welte1fa60c82009-02-09 18:13:26 +000071 memcpy(&me->src, src, sizeof(me->src));
72 memcpy(&me->dst, dst, sizeof(me->dst));
73 llist_add(&me->list, &ss_map);
74
75 return 0;
76}
77
Harald Welte26aa6a12009-02-19 15:14:23 +000078int trau_mux_map_lchan(const struct gsm_lchan *src,
79 const struct gsm_lchan *dst)
80{
81 struct gsm_e1_subslot *src_ss, *dst_ss;
82
83 src_ss = &src->ts->e1_link;
84 dst_ss = &dst->ts->e1_link;
85
86 return trau_mux_map(src_ss, dst_ss);
87}
88
89
Harald Welte1fa60c82009-02-09 18:13:26 +000090/* unmap one particular subslot from another subslot */
Harald Welte45b407a2009-05-23 15:51:12 +000091int trau_mux_unmap(const struct gsm_e1_subslot *ss, u_int32_t callref)
Harald Welte1fa60c82009-02-09 18:13:26 +000092{
93 struct map_entry *me, *me2;
Harald Welte45b407a2009-05-23 15:51:12 +000094 struct upqueue_entry *ue, *ue2;
Harald Welte1fa60c82009-02-09 18:13:26 +000095
Harald Welte45b407a2009-05-23 15:51:12 +000096 if (ss)
97 llist_for_each_entry_safe(me, me2, &ss_map, list) {
98 if (!memcmp(&me->src, ss, sizeof(*ss)) ||
99 !memcmp(&me->dst, ss, sizeof(*ss))) {
100 llist_del(&me->list);
101 return 0;
102 }
103 }
104 llist_for_each_entry_safe(ue, ue2, &ss_upqueue, list) {
105 if (ue->callref == callref) {
106 llist_del(&ue->list);
107 return 0;
108 }
109 if (ss && !memcmp(&ue->src, ss, sizeof(*ss))) {
110 llist_del(&ue->list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000111 return 0;
112 }
113 }
114 return -ENOENT;
115}
116
117/* look-up an enty in the TRAU mux map */
118static struct gsm_e1_subslot *
119lookup_trau_mux_map(const struct gsm_e1_subslot *src)
120{
121 struct map_entry *me;
122
123 llist_for_each_entry(me, &ss_map, list) {
124 if (!memcmp(&me->src, src, sizeof(*src)))
125 return &me->dst;
126 if (!memcmp(&me->dst, src, sizeof(*src)))
127 return &me->src;
128 }
129 return NULL;
130}
131
Harald Welte45b407a2009-05-23 15:51:12 +0000132/* look-up an enty in the TRAU upqueue */
133struct upqueue_entry *
134lookup_trau_upqueue(const struct gsm_e1_subslot *src)
135{
136 struct upqueue_entry *ue;
137
138 llist_for_each_entry(ue, &ss_upqueue, list) {
139 if (!memcmp(&ue->src, src, sizeof(*src)))
140 return ue;
141 }
142 return NULL;
143}
144
Harald Welte1fa60c82009-02-09 18:13:26 +0000145/* we get called by subchan_demux */
146int trau_mux_input(struct gsm_e1_subslot *src_e1_ss,
147 const u_int8_t *trau_bits, int num_bits)
148{
149 struct decoded_trau_frame tf;
150 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
151 struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss);
152 struct subch_mux *mx;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800153 struct upqueue_entry *ue;
154 struct msgb *msg;
155 struct gsm_trau_frame *frame;
Harald Welte41e16682009-02-18 03:34:55 +0000156 int rc;
Harald Welte1fa60c82009-02-09 18:13:26 +0000157
Harald Welte45b407a2009-05-23 15:51:12 +0000158 /* decode TRAU, change it to downlink, re-encode */
159 rc = decode_trau_frame(&tf, trau_bits);
160 if (rc)
161 return rc;
162
Harald Welte4bfdfe72009-06-10 23:11:52 +0800163 if (!dst_e1_ss) {
164 /* frame shall be sent to upqueue */
165 if (!(ue = lookup_trau_upqueue(src_e1_ss)))
166 return -EINVAL;
167 if (!ue->callref)
168 return -EINVAL;
Harald Welte966636f2009-06-26 19:39:35 +0200169 msg = msgb_alloc(sizeof(struct gsm_trau_frame) + sizeof(tf),
170 "TRAU");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800171 if (!msg)
172 return -ENOMEM;
173 frame = (struct gsm_trau_frame *)msg->data;
174 frame->msg_type = GSM_TRAU_FRAME;
175 frame->callref = ue->callref;
176 memcpy(frame->data, &tf, sizeof(tf));
177 msgb_enqueue(&ue->net->upqueue, msg);
178
179 return 0;
180 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000181
182 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
183 if (!mx)
184 return -EINVAL;
185
Harald Welte1fa60c82009-02-09 18:13:26 +0000186 trau_frame_up2down(&tf);
187 encode_trau_frame(trau_bits_out, &tf);
188
189 /* and send it to the muxer */
190 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
191 TRAU_FRAME_BITS);
192}
Harald Welte45b407a2009-05-23 15:51:12 +0000193
194/* add receiver instance for lchan and callref */
195int trau_recv_lchan(struct gsm_lchan *lchan, u_int32_t callref)
196{
197 struct gsm_e1_subslot *src_ss;
Harald Welte2cf161b2009-06-20 22:36:41 +0200198 struct upqueue_entry *ue;
Harald Welte45b407a2009-05-23 15:51:12 +0000199
Harald Welte2cf161b2009-06-20 22:36:41 +0200200 ue = talloc(tall_upq_ctx, struct upqueue_entry);
Harald Welte45b407a2009-05-23 15:51:12 +0000201 if (!ue)
202 return -ENOMEM;
203
204 src_ss = &lchan->ts->e1_link;
205
206 DEBUGP(DCC, "Setting up TRAU receiver (e1=%u,ts=%u,ss=%u) "
207 "and (callref 0x%x)\n",
208 src_ss->e1_nr, src_ss->e1_ts, src_ss->e1_ts_ss,
209 callref);
210
211 /* make sure to get rid of any stale old mappings */
212 trau_mux_unmap(src_ss, callref);
213
214 memcpy(&ue->src, src_ss, sizeof(ue->src));
215 ue->net = lchan->ts->trx->bts->network;
216 ue->callref = callref;
217 llist_add(&ue->list, &ss_upqueue);
218
219 return 0;
220}
221
222int trau_send_lchan(struct gsm_lchan *lchan, struct decoded_trau_frame *tf)
223{
224 u_int8_t trau_bits_out[TRAU_FRAME_BITS];
225 struct gsm_e1_subslot *dst_e1_ss = &lchan->ts->e1_link;
226 struct subch_mux *mx;
227
228 mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts);
229 if (!mx)
230 return -EINVAL;
231
232 encode_trau_frame(trau_bits_out, tf);
233
234 /* and send it to the muxer */
235 return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out,
236 TRAU_FRAME_BITS);
237}