Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | #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 Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 26 | /*! \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 Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 34 | #define NR_SUBCH 4 |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 35 | /*! \brief size of TRAU frames in bytes */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 36 | #define TRAU_FRAME_SIZE 40 |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 37 | /*! \brief size of TRAU farmes in bits */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 38 | #define TRAU_FRAME_BITS (TRAU_FRAME_SIZE*8) |
| 39 | |
| 40 | /***********************************************************************/ |
| 41 | /* DEMULTIPLEXER */ |
| 42 | /***********************************************************************/ |
| 43 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 44 | /*! \brief one subchannel inside the demultplexer */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 45 | struct demux_subch { |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 46 | /*! \brief bit-buffer for output bits */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 47 | uint8_t out_bitbuf[TRAU_FRAME_BITS]; |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 48 | /*! \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 Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 51 | unsigned int consecutive_zeros; |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 52 | /*! \brief are we in TRAU frame sync or not? */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 53 | unsigned int in_sync; |
| 54 | }; |
| 55 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 56 | /*! \brief one instance of a subchannel demultiplexer */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 57 | struct subch_demux { |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 58 | /*! \brief bitmask of currently active subchannels */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 59 | uint8_t chan_activ; |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 60 | /*! \brief one demux_subch struct for every subchannel */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 61 | struct demux_subch subch[NR_SUBCH]; |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 62 | /*! \brief callback to be called once we have received a |
| 63 | * complete frame on a given subchannel */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 64 | int (*out_cb)(struct subch_demux *dmx, int ch, uint8_t *data, int len, |
| 65 | void *); |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 66 | /*! \brief user-provided data, transparently passed to out_cb() */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 67 | void *data; |
| 68 | }; |
| 69 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 70 | int subch_demux_init(struct subch_demux *dmx); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 71 | int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 72 | int subch_demux_activate(struct subch_demux *dmx, int subch); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 73 | int subch_demux_deactivate(struct subch_demux *dmx, int subch); |
| 74 | |
| 75 | /***********************************************************************/ |
| 76 | /* MULTIPLEXER */ |
| 77 | /***********************************************************************/ |
| 78 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 79 | /*! \brief one element in the tx_queue of a muxer sub-channel */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 80 | struct subch_txq_entry { |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 81 | /*! \brief internal linked list header */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 82 | struct llist_head list; |
| 83 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 84 | unsigned int bit_len; /*!< \brief total number of bits in 'bits' */ |
| 85 | unsigned int next_bit; /*!< \brief next bit to be transmitted */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 86 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 87 | uint8_t bits[0]; /*!< \brief one bit per byte */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 90 | /*! \brief one sub-channel inside a multiplexer */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 91 | struct mux_subch { |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 92 | /*! \brief linked list of \ref subch_txq_entry */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 93 | struct llist_head tx_queue; |
| 94 | }; |
| 95 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 96 | /*! \brief one instance of the subchannel multiplexer */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 97 | struct subch_mux { |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 98 | /*! \brief array of sub-channels inside the multiplexer */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 99 | struct mux_subch subch[NR_SUBCH]; |
| 100 | }; |
| 101 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 102 | int subchan_mux_init(struct subch_mux *mx); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 103 | int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 104 | int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data, |
| 105 | int len); |
| 106 | |
Harald Welte | e4ec40a | 2011-08-21 11:07:20 +0200 | [diff] [blame] | 107 | /* }@ */ |
| 108 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 109 | #endif /* _SUBCH_DEMUX_H */ |