blob: 238056c9f1f0a04d7fa53803985d338572951f58 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* A E1 sub-channel (de)multiplexer with TRAU frame sync */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include "internal.h"
22
23#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <errno.h>
28
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020029#include <osmocom/abis/subchan_demux.h>
30#include <osmocom/abis/trau_frame.h>
Harald Welte71d87b22011-07-18 14:49:56 +020031#include <osmocom/core/talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020032
Harald Weltee4ec40a2011-08-21 11:07:20 +020033/*! \addtogroup subchan_demux
34 * @{
35 *
36 * \file subchan_demux.c
37 */
38
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020039void *tall_tqe_ctx;
40
41static inline void append_bit(struct demux_subch *sch, uint8_t bit)
42{
43 sch->out_bitbuf[sch->out_idx++] = bit;
44}
45
46#define SYNC_HDR_BITS 16
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020047
Dieter Spaar08c10322012-10-08 21:36:03 +020048/* check if we have just completed the 16 bit zero + 1 bit one sync
49 * header, in accordance with GSM TS 08.60 Chapter 4.8.1 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020050static int sync_hdr_complete(struct demux_subch *sch, uint8_t bit)
51{
52 if (bit == 0)
53 sch->consecutive_zeros++;
Dieter Spaar08c10322012-10-08 21:36:03 +020054 else {
55 if (sch->consecutive_zeros >= SYNC_HDR_BITS) {
56 sch->consecutive_zeros = 0;
57 return 1;
58 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020059 sch->consecutive_zeros = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020060 }
61
62 return 0;
63}
64
65/* resynchronize to current location */
66static void resync_to_here(struct demux_subch *sch)
67{
68 memset(sch->out_bitbuf, 0, SYNC_HDR_BITS);
Dieter Spaar08c10322012-10-08 21:36:03 +020069 sch->out_bitbuf[SYNC_HDR_BITS] = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020070
71 /* set index in a way that we can continue receiving bits after
72 * the end of the SYNC header */
Dieter Spaar08c10322012-10-08 21:36:03 +020073 sch->out_idx = SYNC_HDR_BITS + 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020074 sch->in_sync = 1;
75}
76
Harald Weltee4ec40a2011-08-21 11:07:20 +020077/*! \brief initialize subchannel demuxer structure
78 * \param[in,out] dmx subchannel demuxer
79 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020080int subch_demux_init(struct subch_demux *dmx)
81{
82 int i;
83
84 dmx->chan_activ = 0;
85 for (i = 0; i < NR_SUBCH; i++) {
86 struct demux_subch *sch = &dmx->subch[i];
87 sch->out_idx = 0;
88 memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
89 }
90 return 0;
91}
92
Harald Weltee4ec40a2011-08-21 11:07:20 +020093/*! \brief Input some data from the 64k full-slot into subchannel demux
94 * \param[in] dmx subchannel demuxer
95 * \param[in] data pointer to buffer containing input data
96 * \param[in] len length of \a data in bytes
97 * \returns 0 in case of success, <0 otherwise.
98 *
99 * Input some arbitrary (modulo 4) number of bytes of a 64k E1 channel,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200100 * split it into the 16k subchannels */
101int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len)
102{
103 int i, c;
104
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200105 for (i = 0; i < len; i++) {
106 uint8_t inbyte = data[i];
107
108 for (c = 0; c < NR_SUBCH; c++) {
109 struct demux_subch *sch = &dmx->subch[c];
110 uint8_t inbits;
111 uint8_t bit;
112
113 /* ignore inactive subchannels */
114 if (!(dmx->chan_activ & (1 << c)))
115 continue;
116
117 inbits = inbyte >> (c << 1);
118
119 /* two bits for each subchannel */
120 if (inbits & 0x01)
121 bit = 1;
122 else
123 bit = 0;
124 append_bit(sch, bit);
125
126 if (sync_hdr_complete(sch, bit))
127 resync_to_here(sch);
128
129 if (inbits & 0x02)
130 bit = 1;
131 else
132 bit = 0;
133 append_bit(sch, bit);
134
135 if (sync_hdr_complete(sch, bit))
136 resync_to_here(sch);
137
138 /* FIXME: verify the first bit in octet 2, 4, 6, ...
139 * according to TS 08.60 4.8.1 */
140
141 /* once we have reached TRAU_FRAME_BITS, call
142 * the TRAU frame handler callback function */
143 if (sch->out_idx >= TRAU_FRAME_BITS) {
144 if (sch->in_sync) {
145 dmx->out_cb(dmx, c, sch->out_bitbuf,
146 sch->out_idx, dmx->data);
147 sch->in_sync = 0;
148 }
149 sch->out_idx = 0;
150 }
151 }
152 }
153 return i;
154}
155
Harald Weltee4ec40a2011-08-21 11:07:20 +0200156/*! \brief activate a given sub-channel
157 * \param[in] dmx subchannel demuxer
158 * \param[in] subch sub-channel number
159 * \returns 0 in case of success, <0 otherwise
160 *
161 * Activating a sub-channel will casuse the \ref subch_demux::out_cb
162 * callback to be called for any incoming data from the full slot
163 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200164int subch_demux_activate(struct subch_demux *dmx, int subch)
165{
166 if (subch >= NR_SUBCH)
167 return -EINVAL;
168
169 dmx->chan_activ |= (1 << subch);
170 return 0;
171}
172
Harald Weltee4ec40a2011-08-21 11:07:20 +0200173/*! \brief deactivate a given sub-channel
174 * \param[in] dmx subchannel demuxer
175 * \param[in] subch sub-channel number
176 * \returns 0 in case of success, <0 otherwise
177 *
178 * Deactivating a sub-channel will casuse the \ref subch_demux::out_cb
179 * callback no longer to be called.
180 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200181int subch_demux_deactivate(struct subch_demux *dmx, int subch)
182{
183 if (subch >= NR_SUBCH)
184 return -EINVAL;
185
186 dmx->chan_activ &= ~(1 << subch);
187 return 0;
188}
189
190/* MULTIPLEXER */
191
192static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
193{
194 /* allocate and initialize with idle pattern */
195 return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(),
196 TRAU_FRAME_BITS);
197}
198
199/* return the requested number of bits from the specified subchannel */
200static int get_subch_bits(struct subch_mux *mx, int subch,
201 uint8_t *bits, int num_requested)
202{
203 struct mux_subch *sch = &mx->subch[subch];
204 int num_bits = 0;
205
206 while (num_bits < num_requested) {
207 struct subch_txq_entry *txe;
208 int num_bits_left;
209 int num_bits_thistime;
210
211 /* make sure we have a valid entry at top of tx queue.
212 * if not, add an idle frame */
213 if (llist_empty(&sch->tx_queue))
214 alloc_add_idle_frame(mx, subch);
215
216 if (llist_empty(&sch->tx_queue))
217 return -EIO;
218
219 txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
220 num_bits_left = txe->bit_len - txe->next_bit;
221
222 if (num_bits_left < num_requested)
223 num_bits_thistime = num_bits_left;
224 else
225 num_bits_thistime = num_requested;
226
227 /* pull the bits from the txe */
228 memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime);
229 txe->next_bit += num_bits_thistime;
230
231 /* free the tx_queue entry if it is fully consumed */
232 if (txe->next_bit >= txe->bit_len) {
233 llist_del(&txe->list);
234 talloc_free(txe);
235 }
236
237 /* increment global number of bits dequeued */
238 num_bits += num_bits_thistime;
239 }
240
241 return num_requested;
242}
243
244/* compact an array of 8 single-bit bytes into one byte of 8 bits */
245static uint8_t compact_bits(const uint8_t *bits)
246{
247 uint8_t ret = 0;
248 int i;
249
250 for (i = 0; i < 8; i++)
251 ret |= (bits[i] ? 1 : 0) << i;
252
253 return ret;
254}
255
256/* obtain a single output byte from the subchannel muxer */
257static int mux_output_byte(struct subch_mux *mx, uint8_t *byte)
258{
259 uint8_t bits[8];
260 int rc;
261
262 /* combine two bits of every subchan */
263 rc = get_subch_bits(mx, 0, &bits[0], 2);
264 rc = get_subch_bits(mx, 1, &bits[2], 2);
265 rc = get_subch_bits(mx, 2, &bits[4], 2);
266 rc = get_subch_bits(mx, 3, &bits[6], 2);
267
268 *byte = compact_bits(bits);
269
270 return rc;
271}
272
Harald Weltee4ec40a2011-08-21 11:07:20 +0200273/*! \brief Request the output of some muxed bytes from the subchan muxer
274 * \param[in] mx subchannel muxer
275 * \param[out] data caller-allocated buffer for data
276 * \param[in] len number of bytes to be filled into \a data
277 * \returns actual number of bytes filled into \a data
278 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200279int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len)
280{
281 int i;
282
283 for (i = 0; i < len; i++) {
284 int rc;
285 rc = mux_output_byte(mx, &data[i]);
286 if (rc < 0)
287 break;
288 }
289 return i;
290}
291
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200292/* evict the 'num_evict' number of oldest entries in the queue */
293static void tx_queue_evict(struct mux_subch *sch, int num_evict)
294{
295 struct subch_txq_entry *tqe;
296 int i;
297
298 for (i = 0; i < num_evict; i++) {
299 if (llist_empty(&sch->tx_queue))
300 return;
301
302 tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
303 llist_del(&tqe->list);
304 talloc_free(tqe);
305 }
306}
307
Harald Weltee4ec40a2011-08-21 11:07:20 +0200308/*! \brief enqueue some data into the tx_queue of a given subchannel
309 * \param[in] mx subchannel muxer instance
310 * \param[in] s_nr subchannel number
311 * \param[in] data pointer to buffer with data
312 * \param[in] len length of \a data
313 * \returns 0 in case of success, <0 in case of error
314 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200315int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data,
316 int len)
317{
318 struct mux_subch *sch = &mx->subch[s_nr];
Neels Hofmeyr30ffa7a2017-01-11 00:42:46 +0100319 unsigned int list_len = llist_count(&sch->tx_queue);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200320 struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx,
321 sizeof(*tqe) + len);
322 if (!tqe)
323 return -ENOMEM;
324
325 tqe->bit_len = len;
326 memcpy(tqe->bits, data, len);
327
328 if (list_len > 2)
329 tx_queue_evict(sch, list_len-2);
330
331 llist_add_tail(&tqe->list, &sch->tx_queue);
332
333 return 0;
334}
335
Harald Weltee4ec40a2011-08-21 11:07:20 +0200336/*! \brief initialize one subchannel muxer instance
337 * \param[in,out] mx subchannel muxer instance, caller-allocated
338 * \returns 0 in success, < 0 in case of error
339 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200340int subchan_mux_init(struct subch_mux *mx)
341{
342 int i;
343
344 memset(mx, 0, sizeof(*mx));
345 for (i = 0; i < NR_SUBCH; i++) {
346 struct mux_subch *sch = &mx->subch[i];
347 INIT_LLIST_HEAD(&sch->tx_queue);
348 }
349
350 return 0;
351}
Harald Weltee4ec40a2011-08-21 11:07:20 +0200352
353/* }@ */