Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 1 | /* A E1 sub-channel (de)multiplexer with TRAU frame sync */ |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 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 <unistd.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include <openbsc/subchan_demux.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 29 | #include <openbsc/trau_frame.h> |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 31 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 32 | static inline void append_bit(struct demux_subch *sch, u_int8_t bit) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 33 | { |
| 34 | sch->out_bitbuf[sch->out_idx++] = bit; |
| 35 | } |
| 36 | |
| 37 | #define SYNC_HDR_BITS 16 |
| 38 | static const u_int8_t nullbytes[SYNC_HDR_BITS]; |
| 39 | |
| 40 | /* check if we have just completed the 16 bit zero sync header, |
| 41 | * in accordance with GSM TS 08.60 Chapter 4.8.1 */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 42 | static int sync_hdr_complete(struct demux_subch *sch, u_int8_t bit) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 43 | { |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 44 | if (bit == 0) |
| 45 | sch->consecutive_zeros++; |
| 46 | else |
| 47 | sch->consecutive_zeros = 0; |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 48 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 49 | if (sch->consecutive_zeros >= SYNC_HDR_BITS) { |
| 50 | sch->consecutive_zeros = 0; |
| 51 | return 1; |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 52 | } |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 53 | |
| 54 | return 0; |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* resynchronize to current location */ |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 58 | static void resync_to_here(struct demux_subch *sch) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 59 | { |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 60 | memset(sch->out_bitbuf, 0, SYNC_HDR_BITS); |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 61 | |
| 62 | /* set index in a way that we can continue receiving bits after |
| 63 | * the end of the SYNC header */ |
| 64 | sch->out_idx = SYNC_HDR_BITS; |
Harald Welte | f1e6f96 | 2009-02-19 17:05:13 +0000 | [diff] [blame] | 65 | sch->in_sync = 1; |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Harald Welte | ce281c0 | 2009-01-05 20:14:14 +0000 | [diff] [blame] | 68 | int subch_demux_init(struct subch_demux *dmx) |
| 69 | { |
| 70 | int i; |
| 71 | |
| 72 | dmx->chan_activ = 0; |
| 73 | for (i = 0; i < NR_SUBCH; i++) { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 74 | struct demux_subch *sch = &dmx->subch[i]; |
Harald Welte | ce281c0 | 2009-01-05 20:14:14 +0000 | [diff] [blame] | 75 | sch->out_idx = 0; |
| 76 | memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf)); |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 81 | /* input some arbitrary (modulo 4) number of bytes of a 64k E1 channel, |
| 82 | * split it into the 16k subchannels */ |
| 83 | int subch_demux_in(struct subch_demux *dmx, u_int8_t *data, int len) |
| 84 | { |
| 85 | int i, c; |
| 86 | |
| 87 | /* we avoid partially filled bytes in outbuf */ |
| 88 | if (len % 4) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | for (i = 0; i < len; i++) { |
| 92 | u_int8_t inbyte = data[i]; |
| 93 | |
| 94 | for (c = 0; c < NR_SUBCH; c++) { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 95 | struct demux_subch *sch = &dmx->subch[c]; |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 96 | u_int8_t inbits; |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 97 | u_int8_t bit; |
| 98 | |
| 99 | /* ignore inactive subchannels */ |
| 100 | if (!(dmx->chan_activ & (1 << c))) |
| 101 | continue; |
| 102 | |
Harald Welte | 360f06c | 2009-04-29 16:21:18 +0000 | [diff] [blame] | 103 | inbits = inbyte >> (c << 1); |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 104 | |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 105 | /* two bits for each subchannel */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 106 | if (inbits & 0x01) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 107 | bit = 1; |
| 108 | else |
| 109 | bit = 0; |
| 110 | append_bit(sch, bit); |
| 111 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 112 | if (sync_hdr_complete(sch, bit)) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 113 | resync_to_here(sch); |
| 114 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 115 | if (inbits & 0x02) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 116 | bit = 1; |
| 117 | else |
| 118 | bit = 0; |
| 119 | append_bit(sch, bit); |
| 120 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 121 | if (sync_hdr_complete(sch, bit)) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 122 | resync_to_here(sch); |
| 123 | |
| 124 | /* FIXME: verify the first bit in octet 2, 4, 6, ... |
| 125 | * according to TS 08.60 4.8.1 */ |
| 126 | |
| 127 | /* once we have reached TRAU_FRAME_BITS, call |
| 128 | * the TRAU frame handler callback function */ |
| 129 | if (sch->out_idx >= TRAU_FRAME_BITS) { |
Harald Welte | f1e6f96 | 2009-02-19 17:05:13 +0000 | [diff] [blame] | 130 | if (sch->in_sync) { |
| 131 | dmx->out_cb(dmx, c, sch->out_bitbuf, |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 132 | sch->out_idx, dmx->data); |
Harald Welte | f1e6f96 | 2009-02-19 17:05:13 +0000 | [diff] [blame] | 133 | sch->in_sync = 0; |
| 134 | } |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 135 | sch->out_idx = 0; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | return i; |
| 140 | } |
| 141 | |
| 142 | int subch_demux_activate(struct subch_demux *dmx, int subch) |
| 143 | { |
| 144 | if (subch >= NR_SUBCH) |
| 145 | return -EINVAL; |
| 146 | |
| 147 | dmx->chan_activ |= (1 << subch); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int subch_demux_deactivate(struct subch_demux *dmx, int subch) |
| 152 | { |
| 153 | if (subch >= NR_SUBCH) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | dmx->chan_activ &= ~(1 << subch); |
| 157 | return 0; |
| 158 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 159 | |
| 160 | /* MULTIPLEXER */ |
| 161 | |
| 162 | static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr) |
| 163 | { |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 164 | /* allocate and initialize with idle pattern */ |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 165 | return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(), |
| 166 | TRAU_FRAME_BITS); |
| 167 | } |
| 168 | |
| 169 | /* return the requested number of bits from the specified subchannel */ |
| 170 | static int get_subch_bits(struct subch_mux *mx, int subch, |
| 171 | u_int8_t *bits, int num_requested) |
| 172 | { |
| 173 | struct mux_subch *sch = &mx->subch[subch]; |
| 174 | int num_bits = 0; |
| 175 | |
| 176 | while (num_bits < num_requested) { |
| 177 | struct subch_txq_entry *txe; |
| 178 | int num_bits_left; |
| 179 | int num_bits_thistime; |
| 180 | |
| 181 | /* make sure we have a valid entry at top of tx queue. |
| 182 | * if not, add an idle frame */ |
Holger Freyther | 3630eab | 2009-02-09 21:05:56 +0000 | [diff] [blame] | 183 | if (llist_empty(&sch->tx_queue)) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 184 | alloc_add_idle_frame(mx, subch); |
| 185 | |
| 186 | if (llist_empty(&sch->tx_queue)) |
| 187 | return -EIO; |
| 188 | |
Holger Freyther | 3630eab | 2009-02-09 21:05:56 +0000 | [diff] [blame] | 189 | txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 190 | num_bits_left = txe->bit_len - txe->next_bit; |
| 191 | |
| 192 | if (num_bits_left < num_requested) |
| 193 | num_bits_thistime = num_bits_left; |
| 194 | else |
| 195 | num_bits_thistime = num_requested; |
| 196 | |
| 197 | /* pull the bits from the txe */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 198 | memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 199 | txe->next_bit += num_bits_thistime; |
| 200 | |
| 201 | /* free the tx_queue entry if it is fully consumed */ |
| 202 | if (txe->next_bit >= txe->bit_len) { |
| 203 | llist_del(&txe->list); |
| 204 | free(txe); |
| 205 | } |
| 206 | |
| 207 | /* increment global number of bits dequeued */ |
| 208 | num_bits += num_bits_thistime; |
| 209 | } |
| 210 | |
| 211 | return num_requested; |
| 212 | } |
| 213 | |
| 214 | /* compact an array of 8 single-bit bytes into one byte of 8 bits */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 215 | static u_int8_t compact_bits(const u_int8_t *bits) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 216 | { |
| 217 | u_int8_t ret = 0; |
| 218 | int i; |
| 219 | |
| 220 | for (i = 0; i < 8; i++) |
| 221 | ret |= (bits[i] ? 1 : 0) << i; |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | /* obtain a single output byte from the subchannel muxer */ |
| 227 | static int mux_output_byte(struct subch_mux *mx, u_int8_t *byte) |
| 228 | { |
| 229 | u_int8_t bits[8]; |
| 230 | int rc; |
| 231 | |
| 232 | /* combine two bits of every subchan */ |
Harald Welte | 360f06c | 2009-04-29 16:21:18 +0000 | [diff] [blame] | 233 | rc = get_subch_bits(mx, 0, &bits[0], 2); |
| 234 | rc = get_subch_bits(mx, 1, &bits[2], 2); |
| 235 | rc = get_subch_bits(mx, 2, &bits[4], 2); |
| 236 | rc = get_subch_bits(mx, 3, &bits[6], 2); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 237 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 238 | *byte = compact_bits(bits); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 239 | |
| 240 | return rc; |
| 241 | } |
| 242 | |
| 243 | /* Request the output of some muxed bytes from the subchan muxer */ |
| 244 | int subchan_mux_out(struct subch_mux *mx, u_int8_t *data, int len) |
| 245 | { |
| 246 | int i; |
| 247 | |
| 248 | for (i = 0; i < len; i++) { |
| 249 | int rc; |
| 250 | rc = mux_output_byte(mx, &data[i]); |
| 251 | if (rc < 0) |
| 252 | break; |
| 253 | } |
| 254 | return i; |
| 255 | } |
| 256 | |
Harald Welte | b49fe96 | 2009-02-22 22:28:19 +0000 | [diff] [blame] | 257 | static int llist_len(struct llist_head *head) |
| 258 | { |
| 259 | struct llist_head *entry; |
| 260 | int i = 0; |
| 261 | |
| 262 | llist_for_each(entry, head) |
| 263 | i++; |
| 264 | |
| 265 | return i; |
| 266 | } |
| 267 | |
| 268 | /* evict the 'num_evict' number of oldest entries in the queue */ |
| 269 | static void tx_queue_evict(struct mux_subch *sch, int num_evict) |
| 270 | { |
| 271 | struct subch_txq_entry *tqe; |
| 272 | int i; |
| 273 | |
| 274 | for (i = 0; i < num_evict; i++) { |
| 275 | if (llist_empty(&sch->tx_queue)) |
| 276 | return; |
| 277 | |
| 278 | tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list); |
| 279 | llist_del(&tqe->list); |
| 280 | free(tqe); |
| 281 | } |
| 282 | } |
| 283 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 284 | /* enqueue some data into the tx_queue of a given subchannel */ |
| 285 | int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data, |
| 286 | int len) |
| 287 | { |
| 288 | struct mux_subch *sch = &mx->subch[s_nr]; |
| 289 | struct subch_txq_entry *tqe = malloc(sizeof(*tqe) + len); |
Harald Welte | b49fe96 | 2009-02-22 22:28:19 +0000 | [diff] [blame] | 290 | int list_len = llist_len(&sch->tx_queue); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 291 | |
| 292 | if (!tqe) |
| 293 | return -ENOMEM; |
| 294 | |
| 295 | memset(tqe, 0, sizeof(*tqe)); |
| 296 | tqe->bit_len = len; |
| 297 | memcpy(tqe->bits, data, len); |
| 298 | |
Harald Welte | b49fe96 | 2009-02-22 22:28:19 +0000 | [diff] [blame] | 299 | if (list_len > 2) |
| 300 | tx_queue_evict(sch, list_len-2); |
| 301 | |
Holger Freyther | 3630eab | 2009-02-09 21:05:56 +0000 | [diff] [blame] | 302 | llist_add_tail(&tqe->list, &sch->tx_queue); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /* initialize one subchannel muxer instance */ |
| 308 | int subchan_mux_init(struct subch_mux *mx) |
| 309 | { |
| 310 | int i; |
| 311 | |
| 312 | memset(mx, 0, sizeof(*mx)); |
| 313 | for (i = 0; i < NR_SUBCH; i++) { |
| 314 | struct mux_subch *sch = &mx->subch[i]; |
| 315 | INIT_LLIST_HEAD(&sch->tx_queue); |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |