blob: 3978d73d8389f5b6e6108b71cb9e01fcb461d870 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001#ifndef _SUBCH_DEMUX_H
2#define _SUBCH_DEMUX_H
3/* A E1 sub-channel (de)multiplexer with TRAU frame sync */
4
5/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
6 * All Rights Reserved
7 *
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 <stdint.h>
24#include <osmocom/core/linuxlist.h>
Harald Welte5226e5b2020-05-07 23:09:16 +020025#include <osmocom/core/bits.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020026
Harald Weltee4ec40a2011-08-21 11:07:20 +020027/*! \defgroup subchan_demux
28 * \brief E1 sub-channel multiplexer/demultiplexer
29 * @{
30 *
31 * \file subchan_demux.h
32 */
33
34/*! \brief number of 16k sub-channels inside one 64k E1 timeslot */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020035#define NR_SUBCH 4
Harald Weltee4ec40a2011-08-21 11:07:20 +020036/*! \brief size of TRAU frames in bytes */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020037#define TRAU_FRAME_SIZE 40
Harald Weltee4ec40a2011-08-21 11:07:20 +020038/*! \brief size of TRAU farmes in bits */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020039#define TRAU_FRAME_BITS (TRAU_FRAME_SIZE*8)
40
41/***********************************************************************/
42/* DEMULTIPLEXER */
43/***********************************************************************/
44
Harald Weltee4ec40a2011-08-21 11:07:20 +020045/*! \brief one subchannel inside the demultplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020046struct demux_subch {
Harald Weltee4ec40a2011-08-21 11:07:20 +020047 /*! \brief bit-buffer for output bits */
Harald Welte5226e5b2020-05-07 23:09:16 +020048 ubit_t out_bitbuf[TRAU_FRAME_BITS];
Harald Weltee4ec40a2011-08-21 11:07:20 +020049 /*! \brief next bit to be written in out_bitbuf */
50 uint16_t out_idx;
51 /*! \brief number of consecutive zeros that we have received (for sync) */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052 unsigned int consecutive_zeros;
Harald Weltee4ec40a2011-08-21 11:07:20 +020053 /*! \brief are we in TRAU frame sync or not? */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020054 unsigned int in_sync;
55};
56
Harald Weltee4ec40a2011-08-21 11:07:20 +020057/*! \brief one instance of a subchannel demultiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020058struct subch_demux {
Harald Weltee4ec40a2011-08-21 11:07:20 +020059 /*! \brief bitmask of currently active subchannels */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020060 uint8_t chan_activ;
Harald Weltee4ec40a2011-08-21 11:07:20 +020061 /*! \brief one demux_subch struct for every subchannel */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020062 struct demux_subch subch[NR_SUBCH];
Harald Weltee4ec40a2011-08-21 11:07:20 +020063 /*! \brief callback to be called once we have received a
64 * complete frame on a given subchannel */
Harald Welte5226e5b2020-05-07 23:09:16 +020065 int (*out_cb)(struct subch_demux *dmx, int ch, const ubit_t *data, int len,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020066 void *);
Harald Weltee4ec40a2011-08-21 11:07:20 +020067 /*! \brief user-provided data, transparently passed to out_cb() */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020068 void *data;
69};
70
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020071int subch_demux_init(struct subch_demux *dmx);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020072int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020073int subch_demux_activate(struct subch_demux *dmx, int subch);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020074int subch_demux_deactivate(struct subch_demux *dmx, int subch);
75
76/***********************************************************************/
77/* MULTIPLEXER */
78/***********************************************************************/
79
Harald Weltee4ec40a2011-08-21 11:07:20 +020080/*! \brief one element in the tx_queue of a muxer sub-channel */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020081struct subch_txq_entry {
Harald Weltee4ec40a2011-08-21 11:07:20 +020082 /*! \brief internal linked list header */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020083 struct llist_head list;
84
Harald Weltee4ec40a2011-08-21 11:07:20 +020085 unsigned int bit_len; /*!< \brief total number of bits in 'bits' */
86 unsigned int next_bit; /*!< \brief next bit to be transmitted */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020087
Harald Weltee4ec40a2011-08-21 11:07:20 +020088 uint8_t bits[0]; /*!< \brief one bit per byte */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020089};
90
Harald Weltee4ec40a2011-08-21 11:07:20 +020091/*! \brief one sub-channel inside a multiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020092struct mux_subch {
Harald Weltee4ec40a2011-08-21 11:07:20 +020093 /*! \brief linked list of \ref subch_txq_entry */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020094 struct llist_head tx_queue;
95};
96
Harald Weltee4ec40a2011-08-21 11:07:20 +020097/*! \brief one instance of the subchannel multiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020098struct subch_mux {
Harald Weltee4ec40a2011-08-21 11:07:20 +020099 /*! \brief array of sub-channels inside the multiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200100 struct mux_subch subch[NR_SUBCH];
101};
102
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200103int subchan_mux_init(struct subch_mux *mx);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200104int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200105int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data,
106 int len);
107
Harald Weltee4ec40a2011-08-21 11:07:20 +0200108/* }@ */
109
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200110#endif /* _SUBCH_DEMUX_H */