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; |
| 65 | } |
| 66 | |
Harald Welte | ce281c0 | 2009-01-05 20:14:14 +0000 | [diff] [blame] | 67 | int subch_demux_init(struct subch_demux *dmx) |
| 68 | { |
| 69 | int i; |
| 70 | |
| 71 | dmx->chan_activ = 0; |
| 72 | for (i = 0; i < NR_SUBCH; i++) { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 73 | struct demux_subch *sch = &dmx->subch[i]; |
Harald Welte | ce281c0 | 2009-01-05 20:14:14 +0000 | [diff] [blame] | 74 | sch->out_idx = 0; |
| 75 | memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf)); |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 80 | /* input some arbitrary (modulo 4) number of bytes of a 64k E1 channel, |
| 81 | * split it into the 16k subchannels */ |
| 82 | int subch_demux_in(struct subch_demux *dmx, u_int8_t *data, int len) |
| 83 | { |
| 84 | int i, c; |
| 85 | |
| 86 | /* we avoid partially filled bytes in outbuf */ |
| 87 | if (len % 4) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | for (i = 0; i < len; i++) { |
| 91 | u_int8_t inbyte = data[i]; |
| 92 | |
| 93 | for (c = 0; c < NR_SUBCH; c++) { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 94 | struct demux_subch *sch = &dmx->subch[c]; |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 95 | u_int8_t inbits; |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 96 | u_int8_t bit; |
| 97 | |
| 98 | /* ignore inactive subchannels */ |
| 99 | if (!(dmx->chan_activ & (1 << c))) |
| 100 | continue; |
| 101 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 102 | inbits = inbyte >> ((3-c)*2); |
| 103 | |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 104 | /* two bits for each subchannel */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 105 | if (inbits & 0x01) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 106 | bit = 1; |
| 107 | else |
| 108 | bit = 0; |
| 109 | append_bit(sch, bit); |
| 110 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 111 | if (sync_hdr_complete(sch, bit)) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 112 | resync_to_here(sch); |
| 113 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 114 | if (inbits & 0x02) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 115 | bit = 1; |
| 116 | else |
| 117 | bit = 0; |
| 118 | append_bit(sch, bit); |
| 119 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 120 | if (sync_hdr_complete(sch, bit)) |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 121 | resync_to_here(sch); |
| 122 | |
| 123 | /* FIXME: verify the first bit in octet 2, 4, 6, ... |
| 124 | * according to TS 08.60 4.8.1 */ |
| 125 | |
| 126 | /* once we have reached TRAU_FRAME_BITS, call |
| 127 | * the TRAU frame handler callback function */ |
| 128 | if (sch->out_idx >= TRAU_FRAME_BITS) { |
| 129 | dmx->out_cb(dmx, c, sch->out_bitbuf, |
| 130 | sch->out_idx, dmx->data); |
| 131 | sch->out_idx = 0; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return i; |
| 136 | } |
| 137 | |
| 138 | int subch_demux_activate(struct subch_demux *dmx, int subch) |
| 139 | { |
| 140 | if (subch >= NR_SUBCH) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | dmx->chan_activ |= (1 << subch); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int subch_demux_deactivate(struct subch_demux *dmx, int subch) |
| 148 | { |
| 149 | if (subch >= NR_SUBCH) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | dmx->chan_activ &= ~(1 << subch); |
| 153 | return 0; |
| 154 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 155 | |
| 156 | /* MULTIPLEXER */ |
| 157 | |
| 158 | static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr) |
| 159 | { |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 160 | /* allocate and initialize with idle pattern */ |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 161 | return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(), |
| 162 | TRAU_FRAME_BITS); |
| 163 | } |
| 164 | |
| 165 | /* return the requested number of bits from the specified subchannel */ |
| 166 | static int get_subch_bits(struct subch_mux *mx, int subch, |
| 167 | u_int8_t *bits, int num_requested) |
| 168 | { |
| 169 | struct mux_subch *sch = &mx->subch[subch]; |
| 170 | int num_bits = 0; |
| 171 | |
| 172 | while (num_bits < num_requested) { |
| 173 | struct subch_txq_entry *txe; |
| 174 | int num_bits_left; |
| 175 | int num_bits_thistime; |
| 176 | |
| 177 | /* make sure we have a valid entry at top of tx queue. |
| 178 | * if not, add an idle frame */ |
Holger Freyther | 3630eab | 2009-02-09 21:05:56 +0000 | [diff] [blame] | 179 | if (llist_empty(&sch->tx_queue)) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 180 | alloc_add_idle_frame(mx, subch); |
| 181 | |
| 182 | if (llist_empty(&sch->tx_queue)) |
| 183 | return -EIO; |
| 184 | |
Holger Freyther | 3630eab | 2009-02-09 21:05:56 +0000 | [diff] [blame] | 185 | txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 186 | num_bits_left = txe->bit_len - txe->next_bit; |
| 187 | |
| 188 | if (num_bits_left < num_requested) |
| 189 | num_bits_thistime = num_bits_left; |
| 190 | else |
| 191 | num_bits_thistime = num_requested; |
| 192 | |
| 193 | /* pull the bits from the txe */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 194 | memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 195 | txe->next_bit += num_bits_thistime; |
| 196 | |
| 197 | /* free the tx_queue entry if it is fully consumed */ |
| 198 | if (txe->next_bit >= txe->bit_len) { |
| 199 | llist_del(&txe->list); |
| 200 | free(txe); |
| 201 | } |
| 202 | |
| 203 | /* increment global number of bits dequeued */ |
| 204 | num_bits += num_bits_thistime; |
| 205 | } |
| 206 | |
| 207 | return num_requested; |
| 208 | } |
| 209 | |
| 210 | /* 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] | 211 | static u_int8_t compact_bits(const u_int8_t *bits) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 212 | { |
| 213 | u_int8_t ret = 0; |
| 214 | int i; |
| 215 | |
| 216 | for (i = 0; i < 8; i++) |
| 217 | ret |= (bits[i] ? 1 : 0) << i; |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | /* obtain a single output byte from the subchannel muxer */ |
| 223 | static int mux_output_byte(struct subch_mux *mx, u_int8_t *byte) |
| 224 | { |
| 225 | u_int8_t bits[8]; |
| 226 | int rc; |
| 227 | |
| 228 | /* combine two bits of every subchan */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 229 | rc = get_subch_bits(mx, 3, &bits[0], 2); |
| 230 | rc = get_subch_bits(mx, 2, &bits[2], 2); |
| 231 | rc = get_subch_bits(mx, 1, &bits[4], 2); |
| 232 | rc = get_subch_bits(mx, 0, &bits[6], 2); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 233 | |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 234 | *byte = compact_bits(bits); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 235 | |
| 236 | return rc; |
| 237 | } |
| 238 | |
| 239 | /* Request the output of some muxed bytes from the subchan muxer */ |
| 240 | int subchan_mux_out(struct subch_mux *mx, u_int8_t *data, int len) |
| 241 | { |
| 242 | int i; |
| 243 | |
| 244 | for (i = 0; i < len; i++) { |
| 245 | int rc; |
| 246 | rc = mux_output_byte(mx, &data[i]); |
| 247 | if (rc < 0) |
| 248 | break; |
| 249 | } |
| 250 | return i; |
| 251 | } |
| 252 | |
| 253 | /* enqueue some data into the tx_queue of a given subchannel */ |
| 254 | int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data, |
| 255 | int len) |
| 256 | { |
| 257 | struct mux_subch *sch = &mx->subch[s_nr]; |
| 258 | struct subch_txq_entry *tqe = malloc(sizeof(*tqe) + len); |
| 259 | |
| 260 | if (!tqe) |
| 261 | return -ENOMEM; |
| 262 | |
| 263 | memset(tqe, 0, sizeof(*tqe)); |
| 264 | tqe->bit_len = len; |
| 265 | memcpy(tqe->bits, data, len); |
| 266 | |
Holger Freyther | 3630eab | 2009-02-09 21:05:56 +0000 | [diff] [blame] | 267 | llist_add_tail(&tqe->list, &sch->tx_queue); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /* initialize one subchannel muxer instance */ |
| 273 | int subchan_mux_init(struct subch_mux *mx) |
| 274 | { |
| 275 | int i; |
| 276 | |
| 277 | memset(mx, 0, sizeof(*mx)); |
| 278 | for (i = 0; i < NR_SUBCH; i++) { |
| 279 | struct mux_subch *sch = &mx->subch[i]; |
| 280 | INIT_LLIST_HEAD(&sch->tx_queue); |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |