Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* 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 Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 33 | #include <openbsc/talloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 34 | |
| 35 | struct map_entry { |
| 36 | struct llist_head list; |
| 37 | struct gsm_e1_subslot src, dst; |
| 38 | }; |
| 39 | |
| 40 | struct 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 | |
| 47 | static LLIST_HEAD(ss_map); |
| 48 | static LLIST_HEAD(ss_upqueue); |
| 49 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 50 | static void *tall_map_ctx, *tall_upq_ctx; |
| 51 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 52 | /* map one particular subslot to another subslot */ |
| 53 | int trau_mux_map(const struct gsm_e1_subslot *src, |
| 54 | const struct gsm_e1_subslot *dst) |
| 55 | { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 56 | struct map_entry *me; |
| 57 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 58 | me = talloc(tall_map_ctx, struct map_entry); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 59 | if (!me) |
| 60 | return -ENOMEM; |
| 61 | |
| 62 | 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 | |
| 67 | /* make sure to get rid of any stale old mappings */ |
| 68 | trau_mux_unmap(src, 0); |
| 69 | trau_mux_unmap(dst, 0); |
| 70 | |
| 71 | 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 | |
| 78 | int 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 | |
| 90 | /* unmap one particular subslot from another subslot */ |
| 91 | int trau_mux_unmap(const struct gsm_e1_subslot *ss, u_int32_t callref) |
| 92 | { |
| 93 | struct map_entry *me, *me2; |
| 94 | struct upqueue_entry *ue, *ue2; |
| 95 | |
| 96 | 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); |
| 111 | return 0; |
| 112 | } |
| 113 | } |
| 114 | return -ENOENT; |
| 115 | } |
| 116 | |
| 117 | /* look-up an enty in the TRAU mux map */ |
| 118 | static struct gsm_e1_subslot * |
| 119 | lookup_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 | |
| 132 | /* look-up an enty in the TRAU upqueue */ |
| 133 | struct upqueue_entry * |
| 134 | lookup_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 | |
| 145 | /* we get called by subchan_demux */ |
| 146 | int 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 Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 153 | struct upqueue_entry *ue; |
| 154 | struct msgb *msg; |
| 155 | struct gsm_trau_frame *frame; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 156 | int rc; |
| 157 | |
| 158 | /* decode TRAU, change it to downlink, re-encode */ |
| 159 | rc = decode_trau_frame(&tf, trau_bits); |
| 160 | if (rc) |
| 161 | return rc; |
| 162 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 163 | 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 Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 169 | msg = msgb_alloc(sizeof(struct gsm_trau_frame) + sizeof(tf), |
| 170 | "TRAU"); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 171 | 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 181 | |
| 182 | mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts); |
| 183 | if (!mx) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | 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 | } |
| 193 | |
| 194 | /* add receiver instance for lchan and callref */ |
| 195 | int trau_recv_lchan(struct gsm_lchan *lchan, u_int32_t callref) |
| 196 | { |
| 197 | struct gsm_e1_subslot *src_ss; |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 198 | struct upqueue_entry *ue; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 199 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 200 | ue = talloc(tall_upq_ctx, struct upqueue_entry); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 201 | 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 | |
| 222 | int 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 | } |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 238 | |
| 239 | static __attribute__((constructor)) void on_dso_load_trau_mux(void) |
| 240 | { |
| 241 | tall_map_ctx = talloc_named_const(tall_bsc_ctx, 1, |
| 242 | "trau_map_entry"); |
| 243 | |
| 244 | tall_upq_ctx = talloc_named_const(tall_bsc_ctx, 1, |
| 245 | "trau_upq_entry"); |
| 246 | } |