blob: 9661b0481f7e3a97606c242a62115fccad25630c [file] [log] [blame]
Harald Welte54bd94d2009-01-05 19:00:01 +00001#ifndef _SUBCH_DEMUX_H
2#define _SUBCH_DEMUX_H
Harald Welte1fa60c82009-02-09 18:13:26 +00003/* A E1 sub-channel (de)multiplexer with TRAU frame sync */
Harald Welte54bd94d2009-01-05 19:00:01 +00004
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 General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <sys/types.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000025#include <openbsc/linuxlist.h>
Harald Welte54bd94d2009-01-05 19:00:01 +000026
27#define NR_SUBCH 4
28#define TRAU_FRAME_SIZE 40
29#define TRAU_FRAME_BITS (TRAU_FRAME_SIZE*8)
30
Harald Welte1fa60c82009-02-09 18:13:26 +000031/***********************************************************************/
32/* DEMULTIPLEXER */
33/***********************************************************************/
34
35struct demux_subch {
Harald Welte54bd94d2009-01-05 19:00:01 +000036 u_int8_t out_bitbuf[TRAU_FRAME_BITS];
Harald Welte7eb1f622009-02-18 03:40:58 +000037 u_int16_t out_idx; /* next bit to be written in out_bitbuf */
Harald Weltef1e6f962009-02-19 17:05:13 +000038 /* number of consecutive zeros that we have received (for sync) */
Harald Welte7eb1f622009-02-18 03:40:58 +000039 unsigned int consecutive_zeros;
Harald Weltef1e6f962009-02-19 17:05:13 +000040 /* are we in TRAU frame sync or not? */
41 unsigned int in_sync;
Harald Welte54bd94d2009-01-05 19:00:01 +000042};
43
44struct subch_demux {
Harald Welte1fa60c82009-02-09 18:13:26 +000045 /* bitmask of currently active subchannels */
Harald Welte54bd94d2009-01-05 19:00:01 +000046 u_int8_t chan_activ;
Harald Welte1fa60c82009-02-09 18:13:26 +000047 /* one demux_subch struct for every subchannel */
48 struct demux_subch subch[NR_SUBCH];
49 /* callback to be called once we have received a complete
50 * frame on a given subchannel */
Harald Welte54bd94d2009-01-05 19:00:01 +000051 int (*out_cb)(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
52 void *);
Harald Welte1fa60c82009-02-09 18:13:26 +000053 /* user-provided data, transparently passed to out_cb() */
Harald Welte54bd94d2009-01-05 19:00:01 +000054 void *data;
55};
56
Harald Welte1fa60c82009-02-09 18:13:26 +000057/* initialize one demultiplexer instance */
Harald Weltece281c02009-01-05 20:14:14 +000058int subch_demux_init(struct subch_demux *dmx);
Harald Welte1fa60c82009-02-09 18:13:26 +000059
60/* feed 'len' number of muxed bytes into the demultiplexer */
Harald Welte54bd94d2009-01-05 19:00:01 +000061int subch_demux_in(struct subch_demux *dmx, u_int8_t *data, int len);
Harald Welte1fa60c82009-02-09 18:13:26 +000062
63/* activate decoding/processing for one subchannel */
Harald Welte54bd94d2009-01-05 19:00:01 +000064int subch_demux_activate(struct subch_demux *dmx, int subch);
Harald Welte1fa60c82009-02-09 18:13:26 +000065
66/* deactivate decoding/processing for one subchannel */
Harald Welte54bd94d2009-01-05 19:00:01 +000067int subch_demux_deactivate(struct subch_demux *dmx, int subch);
Harald Welte1fa60c82009-02-09 18:13:26 +000068
69/***********************************************************************/
70/* MULTIPLEXER */
71/***********************************************************************/
72
73/* one element in the tx_queue of a muxer sub-channel */
74struct subch_txq_entry {
75 struct llist_head list;
76
77 unsigned int bit_len; /* total number of bits in 'bits' */
78 unsigned int next_bit; /* next bit to be transmitted */
79
80 u_int8_t bits[0]; /* one bit per byte */
81};
82
83struct mux_subch {
84 struct llist_head tx_queue;
85};
86
87/* structure representing one instance of the subchannel muxer */
88struct subch_mux {
89 struct mux_subch subch[NR_SUBCH];
90};
91
92/* initialize a subchannel muxer instance */
93int subchan_mux_init(struct subch_mux *mx);
94
95/* request the output of 'len' multiplexed bytes */
96int subchan_mux_out(struct subch_mux *mx, u_int8_t *data, int len);
97
98/* enqueue some data into one sub-channel of the muxer */
99int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data,
100 int len);
101
Harald Welte54bd94d2009-01-05 19:00:01 +0000102#endif /* _SUBCH_DEMUX_H */