blob: 4e2f464e8522207524d9aa6d13b149587b58d916 [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>
25
Harald Weltee4ec40a2011-08-21 11:07:20 +020026/*! \defgroup subchan_demux
27 * \brief E1 sub-channel multiplexer/demultiplexer
28 * @{
29 *
30 * \file subchan_demux.h
31 */
32
33/*! \brief number of 16k sub-channels inside one 64k E1 timeslot */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020034#define NR_SUBCH 4
Harald Weltee4ec40a2011-08-21 11:07:20 +020035/*! \brief size of TRAU frames in bytes */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020036#define TRAU_FRAME_SIZE 40
Harald Weltee4ec40a2011-08-21 11:07:20 +020037/*! \brief size of TRAU farmes in bits */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020038#define TRAU_FRAME_BITS (TRAU_FRAME_SIZE*8)
39
40/***********************************************************************/
41/* DEMULTIPLEXER */
42/***********************************************************************/
43
Harald Weltee4ec40a2011-08-21 11:07:20 +020044/*! \brief one subchannel inside the demultplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020045struct demux_subch {
Harald Weltee4ec40a2011-08-21 11:07:20 +020046 /*! \brief bit-buffer for output bits */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020047 uint8_t out_bitbuf[TRAU_FRAME_BITS];
Harald Weltee4ec40a2011-08-21 11:07:20 +020048 /*! \brief next bit to be written in out_bitbuf */
49 uint16_t out_idx;
50 /*! \brief number of consecutive zeros that we have received (for sync) */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051 unsigned int consecutive_zeros;
Harald Weltee4ec40a2011-08-21 11:07:20 +020052 /*! \brief are we in TRAU frame sync or not? */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020053 unsigned int in_sync;
54};
55
Harald Weltee4ec40a2011-08-21 11:07:20 +020056/*! \brief one instance of a subchannel demultiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020057struct subch_demux {
Harald Weltee4ec40a2011-08-21 11:07:20 +020058 /*! \brief bitmask of currently active subchannels */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020059 uint8_t chan_activ;
Harald Weltee4ec40a2011-08-21 11:07:20 +020060 /*! \brief one demux_subch struct for every subchannel */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020061 struct demux_subch subch[NR_SUBCH];
Harald Weltee4ec40a2011-08-21 11:07:20 +020062 /*! \brief callback to be called once we have received a
63 * complete frame on a given subchannel */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020064 int (*out_cb)(struct subch_demux *dmx, int ch, uint8_t *data, int len,
65 void *);
Harald Weltee4ec40a2011-08-21 11:07:20 +020066 /*! \brief user-provided data, transparently passed to out_cb() */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020067 void *data;
68};
69
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020070int subch_demux_init(struct subch_demux *dmx);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020071int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020072int subch_demux_activate(struct subch_demux *dmx, int subch);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020073int subch_demux_deactivate(struct subch_demux *dmx, int subch);
74
75/***********************************************************************/
76/* MULTIPLEXER */
77/***********************************************************************/
78
Harald Weltee4ec40a2011-08-21 11:07:20 +020079/*! \brief one element in the tx_queue of a muxer sub-channel */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020080struct subch_txq_entry {
Harald Weltee4ec40a2011-08-21 11:07:20 +020081 /*! \brief internal linked list header */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020082 struct llist_head list;
83
Harald Weltee4ec40a2011-08-21 11:07:20 +020084 unsigned int bit_len; /*!< \brief total number of bits in 'bits' */
85 unsigned int next_bit; /*!< \brief next bit to be transmitted */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020086
Harald Weltee4ec40a2011-08-21 11:07:20 +020087 uint8_t bits[0]; /*!< \brief one bit per byte */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020088};
89
Harald Weltee4ec40a2011-08-21 11:07:20 +020090/*! \brief one sub-channel inside a multiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020091struct mux_subch {
Harald Weltee4ec40a2011-08-21 11:07:20 +020092 /*! \brief linked list of \ref subch_txq_entry */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020093 struct llist_head tx_queue;
94};
95
Harald Weltee4ec40a2011-08-21 11:07:20 +020096/*! \brief one instance of the subchannel multiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020097struct subch_mux {
Harald Weltee4ec40a2011-08-21 11:07:20 +020098 /*! \brief array of sub-channels inside the multiplexer */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020099 struct mux_subch subch[NR_SUBCH];
100};
101
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200102int subchan_mux_init(struct subch_mux *mx);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200103int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200104int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data,
105 int len);
106
Harald Weltee4ec40a2011-08-21 11:07:20 +0200107/* }@ */
108
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200109#endif /* _SUBCH_DEMUX_H */