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 | |
Harald Welte | 5226e5b | 2020-05-07 23:09:16 +0200 | [diff] [blame] | 43 | static inline void append_bit(struct demux_subch *sch, ubit_t bit) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 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 | bcfd7ea | 2020-07-04 11:18:26 +0200 | [diff] [blame] | 95 | static void append_bit_resync_out(struct subch_demux *dmx, int c, ubit_t bit) |
| 96 | { |
| 97 | struct demux_subch *sch = &dmx->subch[c]; |
| 98 | append_bit(sch, bit); |
| 99 | |
| 100 | if (sync_hdr_complete(sch, bit)) |
| 101 | resync_to_here(sch); |
| 102 | |
| 103 | /* FIXME: verify the first bit in octet 2, 4, 6, ... |
| 104 | * according to TS 08.60 4.8.1 */ |
| 105 | |
| 106 | /* once we have reached TRAU_FRAME_BITS, call |
| 107 | * the TRAU frame handler callback function */ |
| 108 | if (sch->out_idx >= TRAU_FRAME_BITS) { |
| 109 | if (sch->in_sync) { |
| 110 | dmx->out_cb(dmx, c, sch->out_bitbuf, |
| 111 | sch->out_idx, dmx->data); |
| 112 | sch->in_sync = 0; |
| 113 | } |
| 114 | sch->out_idx = 0; |
| 115 | } |
| 116 | } |
| 117 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 118 | /*! \brief Input some data from the 64k full-slot into subchannel demux |
| 119 | * \param[in] dmx subchannel demuxer |
| 120 | * \param[in] data pointer to buffer containing input data |
| 121 | * \param[in] len length of \a data in bytes |
| 122 | * \returns 0 in case of success, <0 otherwise. |
| 123 | * |
| 124 | * 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] | 125 | * split it into the 16k subchannels */ |
| 126 | int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len) |
| 127 | { |
| 128 | int i, c; |
| 129 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 130 | for (i = 0; i < len; i++) { |
| 131 | uint8_t inbyte = data[i]; |
| 132 | |
| 133 | for (c = 0; c < NR_SUBCH; c++) { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 134 | uint8_t inbits; |
| 135 | uint8_t bit; |
| 136 | |
| 137 | /* ignore inactive subchannels */ |
| 138 | if (!(dmx->chan_activ & (1 << c))) |
| 139 | continue; |
| 140 | |
| 141 | inbits = inbyte >> (c << 1); |
| 142 | |
| 143 | /* two bits for each subchannel */ |
| 144 | if (inbits & 0x01) |
| 145 | bit = 1; |
| 146 | else |
| 147 | bit = 0; |
Harald Welte | bcfd7ea | 2020-07-04 11:18:26 +0200 | [diff] [blame] | 148 | append_bit_resync_out(dmx, c, bit); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 149 | |
| 150 | if (inbits & 0x02) |
| 151 | bit = 1; |
| 152 | else |
| 153 | bit = 0; |
Harald Welte | bcfd7ea | 2020-07-04 11:18:26 +0200 | [diff] [blame] | 154 | append_bit_resync_out(dmx, c, bit); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | return i; |
| 158 | } |
| 159 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 160 | /*! \brief activate a given sub-channel |
| 161 | * \param[in] dmx subchannel demuxer |
| 162 | * \param[in] subch sub-channel number |
| 163 | * \returns 0 in case of success, <0 otherwise |
| 164 | * |
| 165 | * Activating a sub-channel will casuse the \ref subch_demux::out_cb |
| 166 | * callback to be called for any incoming data from the full slot |
| 167 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 168 | int subch_demux_activate(struct subch_demux *dmx, int subch) |
| 169 | { |
| 170 | if (subch >= NR_SUBCH) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | dmx->chan_activ |= (1 << subch); |
| 174 | return 0; |
| 175 | } |
| 176 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 177 | /*! \brief deactivate a given sub-channel |
| 178 | * \param[in] dmx subchannel demuxer |
| 179 | * \param[in] subch sub-channel number |
| 180 | * \returns 0 in case of success, <0 otherwise |
| 181 | * |
| 182 | * Deactivating a sub-channel will casuse the \ref subch_demux::out_cb |
| 183 | * callback no longer to be called. |
| 184 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 185 | int subch_demux_deactivate(struct subch_demux *dmx, int subch) |
| 186 | { |
| 187 | if (subch >= NR_SUBCH) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | dmx->chan_activ &= ~(1 << subch); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* MULTIPLEXER */ |
| 195 | |
| 196 | static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr) |
| 197 | { |
| 198 | /* allocate and initialize with idle pattern */ |
| 199 | return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(), |
| 200 | TRAU_FRAME_BITS); |
| 201 | } |
| 202 | |
| 203 | /* return the requested number of bits from the specified subchannel */ |
| 204 | static int get_subch_bits(struct subch_mux *mx, int subch, |
Harald Welte | a684b84 | 2020-05-09 10:33:42 +0200 | [diff] [blame] | 205 | ubit_t *bits, int num_requested) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 206 | { |
| 207 | struct mux_subch *sch = &mx->subch[subch]; |
| 208 | int num_bits = 0; |
| 209 | |
| 210 | while (num_bits < num_requested) { |
| 211 | struct subch_txq_entry *txe; |
| 212 | int num_bits_left; |
| 213 | int num_bits_thistime; |
| 214 | |
| 215 | /* make sure we have a valid entry at top of tx queue. |
| 216 | * if not, add an idle frame */ |
| 217 | if (llist_empty(&sch->tx_queue)) |
| 218 | alloc_add_idle_frame(mx, subch); |
| 219 | |
| 220 | if (llist_empty(&sch->tx_queue)) |
| 221 | return -EIO; |
| 222 | |
| 223 | txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list); |
| 224 | num_bits_left = txe->bit_len - txe->next_bit; |
| 225 | |
| 226 | if (num_bits_left < num_requested) |
| 227 | num_bits_thistime = num_bits_left; |
| 228 | else |
| 229 | num_bits_thistime = num_requested; |
| 230 | |
| 231 | /* pull the bits from the txe */ |
| 232 | memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime); |
| 233 | txe->next_bit += num_bits_thistime; |
| 234 | |
| 235 | /* free the tx_queue entry if it is fully consumed */ |
| 236 | if (txe->next_bit >= txe->bit_len) { |
| 237 | llist_del(&txe->list); |
| 238 | talloc_free(txe); |
| 239 | } |
| 240 | |
| 241 | /* increment global number of bits dequeued */ |
| 242 | num_bits += num_bits_thistime; |
| 243 | } |
| 244 | |
| 245 | return num_requested; |
| 246 | } |
| 247 | |
| 248 | /* compact an array of 8 single-bit bytes into one byte of 8 bits */ |
| 249 | static uint8_t compact_bits(const uint8_t *bits) |
| 250 | { |
| 251 | uint8_t ret = 0; |
| 252 | int i; |
| 253 | |
| 254 | for (i = 0; i < 8; i++) |
| 255 | ret |= (bits[i] ? 1 : 0) << i; |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | /* obtain a single output byte from the subchannel muxer */ |
| 261 | static int mux_output_byte(struct subch_mux *mx, uint8_t *byte) |
| 262 | { |
Harald Welte | a684b84 | 2020-05-09 10:33:42 +0200 | [diff] [blame] | 263 | ubit_t bits[8]; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 264 | int rc; |
| 265 | |
| 266 | /* combine two bits of every subchan */ |
| 267 | rc = get_subch_bits(mx, 0, &bits[0], 2); |
| 268 | rc = get_subch_bits(mx, 1, &bits[2], 2); |
| 269 | rc = get_subch_bits(mx, 2, &bits[4], 2); |
| 270 | rc = get_subch_bits(mx, 3, &bits[6], 2); |
| 271 | |
| 272 | *byte = compact_bits(bits); |
| 273 | |
| 274 | return rc; |
| 275 | } |
| 276 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 277 | /*! \brief Request the output of some muxed bytes from the subchan muxer |
| 278 | * \param[in] mx subchannel muxer |
| 279 | * \param[out] data caller-allocated buffer for data |
| 280 | * \param[in] len number of bytes to be filled into \a data |
| 281 | * \returns actual number of bytes filled into \a data |
| 282 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 283 | int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len) |
| 284 | { |
| 285 | int i; |
| 286 | |
| 287 | for (i = 0; i < len; i++) { |
| 288 | int rc; |
| 289 | rc = mux_output_byte(mx, &data[i]); |
| 290 | if (rc < 0) |
| 291 | break; |
| 292 | } |
| 293 | return i; |
| 294 | } |
| 295 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 296 | /* evict the 'num_evict' number of oldest entries in the queue */ |
| 297 | static void tx_queue_evict(struct mux_subch *sch, int num_evict) |
| 298 | { |
| 299 | struct subch_txq_entry *tqe; |
| 300 | int i; |
| 301 | |
| 302 | for (i = 0; i < num_evict; i++) { |
| 303 | if (llist_empty(&sch->tx_queue)) |
| 304 | return; |
| 305 | |
| 306 | tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list); |
| 307 | llist_del(&tqe->list); |
| 308 | talloc_free(tqe); |
| 309 | } |
| 310 | } |
| 311 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 312 | /*! \brief enqueue some data into the tx_queue of a given subchannel |
| 313 | * \param[in] mx subchannel muxer instance |
| 314 | * \param[in] s_nr subchannel number |
Harald Welte | a684b84 | 2020-05-09 10:33:42 +0200 | [diff] [blame] | 315 | * \param[in] data pointer to buffer with data (unpacked bits) |
| 316 | * \param[in] len length of data (in unpacked bits) |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 317 | * \returns 0 in case of success, <0 in case of error |
| 318 | */ |
Harald Welte | a684b84 | 2020-05-09 10:33:42 +0200 | [diff] [blame] | 319 | int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const ubit_t *data, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 320 | int len) |
| 321 | { |
| 322 | struct mux_subch *sch = &mx->subch[s_nr]; |
Neels Hofmeyr | 30ffa7a | 2017-01-11 00:42:46 +0100 | [diff] [blame] | 323 | unsigned int list_len = llist_count(&sch->tx_queue); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 324 | struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx, |
| 325 | sizeof(*tqe) + len); |
| 326 | if (!tqe) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | tqe->bit_len = len; |
| 330 | memcpy(tqe->bits, data, len); |
| 331 | |
| 332 | if (list_len > 2) |
| 333 | tx_queue_evict(sch, list_len-2); |
| 334 | |
| 335 | llist_add_tail(&tqe->list, &sch->tx_queue); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 340 | /*! \brief initialize one subchannel muxer instance |
| 341 | * \param[in,out] mx subchannel muxer instance, caller-allocated |
| 342 | * \returns 0 in success, < 0 in case of error |
| 343 | */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 344 | int subchan_mux_init(struct subch_mux *mx) |
| 345 | { |
| 346 | int i; |
| 347 | |
| 348 | memset(mx, 0, sizeof(*mx)); |
| 349 | for (i = 0; i < NR_SUBCH; i++) { |
| 350 | struct mux_subch *sch = &mx->subch[i]; |
| 351 | INIT_LLIST_HEAD(&sch->tx_queue); |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 356 | |
| 357 | /* }@ */ |