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