Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | /* A E1 sub-channel (de)multiplexer with TRAU frame sync */ |
| 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 Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "internal.h" |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | |
Pablo Neira Ayuso | 177094b | 2011-06-07 12:21:51 +0200 | [diff] [blame] | 29 | #include <osmocom/abis/subchan_demux.h> |
| 30 | #include <osmocom/abis/trau_frame.h> |
Harald Welte | 71d87b2 | 2011-07-18 14:49:56 +0200 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 32 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 33 | /*! \addtogroup subchan_demux |
| 34 | * @{ |
| 35 | * |
| 36 | * \file subchan_demux.c |
| 37 | */ |
| 38 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 39 | void *tall_tqe_ctx; |
| 40 | |
| 41 | static inline void append_bit(struct demux_subch *sch, uint8_t bit) |
| 42 | { |
| 43 | sch->out_bitbuf[sch->out_idx++] = bit; |
| 44 | } |
| 45 | |
| 46 | #define SYNC_HDR_BITS 16 |
| 47 | static const uint8_t nullbytes[SYNC_HDR_BITS]; |
| 48 | |
| 49 | /* check if we have just completed the 16 bit zero sync header, |
| 50 | * in accordance with GSM TS 08.60 Chapter 4.8.1 */ |
| 51 | static int sync_hdr_complete(struct demux_subch *sch, uint8_t bit) |
| 52 | { |
| 53 | if (bit == 0) |
| 54 | sch->consecutive_zeros++; |
| 55 | else |
| 56 | sch->consecutive_zeros = 0; |
| 57 | |
| 58 | if (sch->consecutive_zeros >= SYNC_HDR_BITS) { |
| 59 | sch->consecutive_zeros = 0; |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* resynchronize to current location */ |
| 67 | static void resync_to_here(struct demux_subch *sch) |
| 68 | { |
| 69 | memset(sch->out_bitbuf, 0, SYNC_HDR_BITS); |
| 70 | |
| 71 | /* set index in a way that we can continue receiving bits after |
| 72 | * the end of the SYNC header */ |
| 73 | sch->out_idx = SYNC_HDR_BITS; |
| 74 | sch->in_sync = 1; |
| 75 | } |
| 76 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 77 | /*! \brief initialize subchannel demuxer structure |
| 78 | * \param[in,out] dmx subchannel demuxer |
| 79 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 80 | int subch_demux_init(struct subch_demux *dmx) |
| 81 | { |
| 82 | int i; |
| 83 | |
| 84 | dmx->chan_activ = 0; |
| 85 | for (i = 0; i < NR_SUBCH; i++) { |
| 86 | struct demux_subch *sch = &dmx->subch[i]; |
| 87 | sch->out_idx = 0; |
| 88 | memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf)); |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 93 | /*! \brief Input some data from the 64k full-slot into subchannel demux |
| 94 | * \param[in] dmx subchannel demuxer |
| 95 | * \param[in] data pointer to buffer containing input data |
| 96 | * \param[in] len length of \a data in bytes |
| 97 | * \returns 0 in case of success, <0 otherwise. |
| 98 | * |
| 99 | * Input some arbitrary (modulo 4) number of bytes of a 64k E1 channel, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 100 | * split it into the 16k subchannels */ |
| 101 | int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len) |
| 102 | { |
| 103 | int i, c; |
| 104 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 105 | for (i = 0; i < len; i++) { |
| 106 | uint8_t inbyte = data[i]; |
| 107 | |
| 108 | for (c = 0; c < NR_SUBCH; c++) { |
| 109 | struct demux_subch *sch = &dmx->subch[c]; |
| 110 | uint8_t inbits; |
| 111 | uint8_t bit; |
| 112 | |
| 113 | /* ignore inactive subchannels */ |
| 114 | if (!(dmx->chan_activ & (1 << c))) |
| 115 | continue; |
| 116 | |
| 117 | inbits = inbyte >> (c << 1); |
| 118 | |
| 119 | /* two bits for each subchannel */ |
| 120 | if (inbits & 0x01) |
| 121 | bit = 1; |
| 122 | else |
| 123 | bit = 0; |
| 124 | append_bit(sch, bit); |
| 125 | |
| 126 | if (sync_hdr_complete(sch, bit)) |
| 127 | resync_to_here(sch); |
| 128 | |
| 129 | if (inbits & 0x02) |
| 130 | bit = 1; |
| 131 | else |
| 132 | bit = 0; |
| 133 | append_bit(sch, bit); |
| 134 | |
| 135 | if (sync_hdr_complete(sch, bit)) |
| 136 | resync_to_here(sch); |
| 137 | |
| 138 | /* FIXME: verify the first bit in octet 2, 4, 6, ... |
| 139 | * according to TS 08.60 4.8.1 */ |
| 140 | |
| 141 | /* once we have reached TRAU_FRAME_BITS, call |
| 142 | * the TRAU frame handler callback function */ |
| 143 | if (sch->out_idx >= TRAU_FRAME_BITS) { |
| 144 | if (sch->in_sync) { |
| 145 | dmx->out_cb(dmx, c, sch->out_bitbuf, |
| 146 | sch->out_idx, dmx->data); |
| 147 | sch->in_sync = 0; |
| 148 | } |
| 149 | sch->out_idx = 0; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | return i; |
| 154 | } |
| 155 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 156 | /*! \brief activate a given sub-channel |
| 157 | * \param[in] dmx subchannel demuxer |
| 158 | * \param[in] subch sub-channel number |
| 159 | * \returns 0 in case of success, <0 otherwise |
| 160 | * |
| 161 | * Activating a sub-channel will casuse the \ref subch_demux::out_cb |
| 162 | * callback to be called for any incoming data from the full slot |
| 163 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 164 | int subch_demux_activate(struct subch_demux *dmx, int subch) |
| 165 | { |
| 166 | if (subch >= NR_SUBCH) |
| 167 | return -EINVAL; |
| 168 | |
| 169 | dmx->chan_activ |= (1 << subch); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 173 | /*! \brief deactivate a given sub-channel |
| 174 | * \param[in] dmx subchannel demuxer |
| 175 | * \param[in] subch sub-channel number |
| 176 | * \returns 0 in case of success, <0 otherwise |
| 177 | * |
| 178 | * Deactivating a sub-channel will casuse the \ref subch_demux::out_cb |
| 179 | * callback no longer to be called. |
| 180 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 181 | int subch_demux_deactivate(struct subch_demux *dmx, int subch) |
| 182 | { |
| 183 | if (subch >= NR_SUBCH) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | dmx->chan_activ &= ~(1 << subch); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* MULTIPLEXER */ |
| 191 | |
| 192 | static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr) |
| 193 | { |
| 194 | /* allocate and initialize with idle pattern */ |
| 195 | return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(), |
| 196 | TRAU_FRAME_BITS); |
| 197 | } |
| 198 | |
| 199 | /* return the requested number of bits from the specified subchannel */ |
| 200 | static int get_subch_bits(struct subch_mux *mx, int subch, |
| 201 | uint8_t *bits, int num_requested) |
| 202 | { |
| 203 | struct mux_subch *sch = &mx->subch[subch]; |
| 204 | int num_bits = 0; |
| 205 | |
| 206 | while (num_bits < num_requested) { |
| 207 | struct subch_txq_entry *txe; |
| 208 | int num_bits_left; |
| 209 | int num_bits_thistime; |
| 210 | |
| 211 | /* make sure we have a valid entry at top of tx queue. |
| 212 | * if not, add an idle frame */ |
| 213 | if (llist_empty(&sch->tx_queue)) |
| 214 | alloc_add_idle_frame(mx, subch); |
| 215 | |
| 216 | if (llist_empty(&sch->tx_queue)) |
| 217 | return -EIO; |
| 218 | |
| 219 | txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list); |
| 220 | num_bits_left = txe->bit_len - txe->next_bit; |
| 221 | |
| 222 | if (num_bits_left < num_requested) |
| 223 | num_bits_thistime = num_bits_left; |
| 224 | else |
| 225 | num_bits_thistime = num_requested; |
| 226 | |
| 227 | /* pull the bits from the txe */ |
| 228 | memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime); |
| 229 | txe->next_bit += num_bits_thistime; |
| 230 | |
| 231 | /* free the tx_queue entry if it is fully consumed */ |
| 232 | if (txe->next_bit >= txe->bit_len) { |
| 233 | llist_del(&txe->list); |
| 234 | talloc_free(txe); |
| 235 | } |
| 236 | |
| 237 | /* increment global number of bits dequeued */ |
| 238 | num_bits += num_bits_thistime; |
| 239 | } |
| 240 | |
| 241 | return num_requested; |
| 242 | } |
| 243 | |
| 244 | /* compact an array of 8 single-bit bytes into one byte of 8 bits */ |
| 245 | static uint8_t compact_bits(const uint8_t *bits) |
| 246 | { |
| 247 | uint8_t ret = 0; |
| 248 | int i; |
| 249 | |
| 250 | for (i = 0; i < 8; i++) |
| 251 | ret |= (bits[i] ? 1 : 0) << i; |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | /* obtain a single output byte from the subchannel muxer */ |
| 257 | static int mux_output_byte(struct subch_mux *mx, uint8_t *byte) |
| 258 | { |
| 259 | uint8_t bits[8]; |
| 260 | int rc; |
| 261 | |
| 262 | /* combine two bits of every subchan */ |
| 263 | rc = get_subch_bits(mx, 0, &bits[0], 2); |
| 264 | rc = get_subch_bits(mx, 1, &bits[2], 2); |
| 265 | rc = get_subch_bits(mx, 2, &bits[4], 2); |
| 266 | rc = get_subch_bits(mx, 3, &bits[6], 2); |
| 267 | |
| 268 | *byte = compact_bits(bits); |
| 269 | |
| 270 | return rc; |
| 271 | } |
| 272 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 273 | /*! \brief Request the output of some muxed bytes from the subchan muxer |
| 274 | * \param[in] mx subchannel muxer |
| 275 | * \param[out] data caller-allocated buffer for data |
| 276 | * \param[in] len number of bytes to be filled into \a data |
| 277 | * \returns actual number of bytes filled into \a data |
| 278 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 279 | int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len) |
| 280 | { |
| 281 | int i; |
| 282 | |
| 283 | for (i = 0; i < len; i++) { |
| 284 | int rc; |
| 285 | rc = mux_output_byte(mx, &data[i]); |
| 286 | if (rc < 0) |
| 287 | break; |
| 288 | } |
| 289 | return i; |
| 290 | } |
| 291 | |
| 292 | static int llist_len(struct llist_head *head) |
| 293 | { |
| 294 | struct llist_head *entry; |
| 295 | int i = 0; |
| 296 | |
| 297 | llist_for_each(entry, head) |
| 298 | i++; |
| 299 | |
| 300 | return i; |
| 301 | } |
| 302 | |
| 303 | /* evict the 'num_evict' number of oldest entries in the queue */ |
| 304 | static void tx_queue_evict(struct mux_subch *sch, int num_evict) |
| 305 | { |
| 306 | struct subch_txq_entry *tqe; |
| 307 | int i; |
| 308 | |
| 309 | for (i = 0; i < num_evict; i++) { |
| 310 | if (llist_empty(&sch->tx_queue)) |
| 311 | return; |
| 312 | |
| 313 | tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list); |
| 314 | llist_del(&tqe->list); |
| 315 | talloc_free(tqe); |
| 316 | } |
| 317 | } |
| 318 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 319 | /*! \brief enqueue some data into the tx_queue of a given subchannel |
| 320 | * \param[in] mx subchannel muxer instance |
| 321 | * \param[in] s_nr subchannel number |
| 322 | * \param[in] data pointer to buffer with data |
| 323 | * \param[in] len length of \a data |
| 324 | * \returns 0 in case of success, <0 in case of error |
| 325 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 326 | int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data, |
| 327 | int len) |
| 328 | { |
| 329 | struct mux_subch *sch = &mx->subch[s_nr]; |
| 330 | int list_len = llist_len(&sch->tx_queue); |
| 331 | struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx, |
| 332 | sizeof(*tqe) + len); |
| 333 | if (!tqe) |
| 334 | return -ENOMEM; |
| 335 | |
| 336 | tqe->bit_len = len; |
| 337 | memcpy(tqe->bits, data, len); |
| 338 | |
| 339 | if (list_len > 2) |
| 340 | tx_queue_evict(sch, list_len-2); |
| 341 | |
| 342 | llist_add_tail(&tqe->list, &sch->tx_queue); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 347 | /*! \brief initialize one subchannel muxer instance |
| 348 | * \param[in,out] mx subchannel muxer instance, caller-allocated |
| 349 | * \returns 0 in success, < 0 in case of error |
| 350 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 351 | int subchan_mux_init(struct subch_mux *mx) |
| 352 | { |
| 353 | int i; |
| 354 | |
| 355 | memset(mx, 0, sizeof(*mx)); |
| 356 | for (i = 0; i < NR_SUBCH; i++) { |
| 357 | struct mux_subch *sch = &mx->subch[i]; |
| 358 | INIT_LLIST_HEAD(&sch->tx_queue); |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 363 | |
| 364 | /* }@ */ |