Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 1 | #ifndef _SUBCH_DEMUX_H |
| 2 | #define _SUBCH_DEMUX_H |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 3 | /* A E1 sub-channel (de)multiplexer with TRAU frame sync */ |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 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 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 Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 25 | #include <openbsc/linuxlist.h> |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 26 | |
| 27 | #define NR_SUBCH 4 |
| 28 | #define TRAU_FRAME_SIZE 40 |
| 29 | #define TRAU_FRAME_BITS (TRAU_FRAME_SIZE*8) |
| 30 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 31 | /***********************************************************************/ |
| 32 | /* DEMULTIPLEXER */ |
| 33 | /***********************************************************************/ |
| 34 | |
| 35 | struct demux_subch { |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 36 | u_int8_t out_bitbuf[TRAU_FRAME_BITS]; |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 37 | u_int16_t out_idx; /* next bit to be written in out_bitbuf */ |
Harald Welte | f1e6f96 | 2009-02-19 17:05:13 +0000 | [diff] [blame] | 38 | /* number of consecutive zeros that we have received (for sync) */ |
Harald Welte | 7eb1f62 | 2009-02-18 03:40:58 +0000 | [diff] [blame] | 39 | unsigned int consecutive_zeros; |
Harald Welte | f1e6f96 | 2009-02-19 17:05:13 +0000 | [diff] [blame] | 40 | /* are we in TRAU frame sync or not? */ |
| 41 | unsigned int in_sync; |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct subch_demux { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 45 | /* bitmask of currently active subchannels */ |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 46 | u_int8_t chan_activ; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 47 | /* 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 Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 51 | int (*out_cb)(struct subch_demux *dmx, int ch, u_int8_t *data, int len, |
| 52 | void *); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 53 | /* user-provided data, transparently passed to out_cb() */ |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 54 | void *data; |
| 55 | }; |
| 56 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 57 | /* initialize one demultiplexer instance */ |
Harald Welte | ce281c0 | 2009-01-05 20:14:14 +0000 | [diff] [blame] | 58 | int subch_demux_init(struct subch_demux *dmx); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 59 | |
| 60 | /* feed 'len' number of muxed bytes into the demultiplexer */ |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 61 | int subch_demux_in(struct subch_demux *dmx, u_int8_t *data, int len); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 62 | |
| 63 | /* activate decoding/processing for one subchannel */ |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 64 | int subch_demux_activate(struct subch_demux *dmx, int subch); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 65 | |
| 66 | /* deactivate decoding/processing for one subchannel */ |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 67 | int subch_demux_deactivate(struct subch_demux *dmx, int subch); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 68 | |
| 69 | /***********************************************************************/ |
| 70 | /* MULTIPLEXER */ |
| 71 | /***********************************************************************/ |
| 72 | |
| 73 | /* one element in the tx_queue of a muxer sub-channel */ |
| 74 | struct 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 | |
| 83 | struct mux_subch { |
| 84 | struct llist_head tx_queue; |
| 85 | }; |
| 86 | |
| 87 | /* structure representing one instance of the subchannel muxer */ |
| 88 | struct subch_mux { |
| 89 | struct mux_subch subch[NR_SUBCH]; |
| 90 | }; |
| 91 | |
| 92 | /* initialize a subchannel muxer instance */ |
| 93 | int subchan_mux_init(struct subch_mux *mx); |
| 94 | |
| 95 | /* request the output of 'len' multiplexed bytes */ |
| 96 | int 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 */ |
| 99 | int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data, |
| 100 | int len); |
| 101 | |
Harald Welte | 54bd94d | 2009-01-05 19:00:01 +0000 | [diff] [blame] | 102 | #endif /* _SUBCH_DEMUX_H */ |