blob: a3a44d958f0319cb0499b34b61afb66037260642 [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 *
Harald Welte323d39d2017-11-13 01:09:21 +09006 * SPDX-License-Identifier: AGPL-3.0+
7 *
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02008 * 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 "internal.h"
24
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <errno.h>
30
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020031#include <osmocom/abis/subchan_demux.h>
32#include <osmocom/abis/trau_frame.h>
Harald Welte71d87b22011-07-18 14:49:56 +020033#include <osmocom/core/talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020034
Harald Weltee4ec40a2011-08-21 11:07:20 +020035/*! \addtogroup subchan_demux
36 * @{
37 *
38 * \file subchan_demux.c
39 */
40
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020041void *tall_tqe_ctx;
42
Harald Welte5226e5b2020-05-07 23:09:16 +020043static inline void append_bit(struct demux_subch *sch, ubit_t bit)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020044{
45 sch->out_bitbuf[sch->out_idx++] = bit;
46}
47
48#define SYNC_HDR_BITS 16
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049
Dieter Spaar08c10322012-10-08 21:36:03 +020050/* check if we have just completed the 16 bit zero + 1 bit one sync
51 * header, in accordance with GSM TS 08.60 Chapter 4.8.1 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052static int sync_hdr_complete(struct demux_subch *sch, uint8_t bit)
53{
54 if (bit == 0)
55 sch->consecutive_zeros++;
Dieter Spaar08c10322012-10-08 21:36:03 +020056 else {
57 if (sch->consecutive_zeros >= SYNC_HDR_BITS) {
58 sch->consecutive_zeros = 0;
59 return 1;
60 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020061 sch->consecutive_zeros = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020062 }
63
64 return 0;
65}
66
67/* resynchronize to current location */
68static void resync_to_here(struct demux_subch *sch)
69{
70 memset(sch->out_bitbuf, 0, SYNC_HDR_BITS);
Dieter Spaar08c10322012-10-08 21:36:03 +020071 sch->out_bitbuf[SYNC_HDR_BITS] = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020072
73 /* set index in a way that we can continue receiving bits after
74 * the end of the SYNC header */
Dieter Spaar08c10322012-10-08 21:36:03 +020075 sch->out_idx = SYNC_HDR_BITS + 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020076 sch->in_sync = 1;
77}
78
Harald Weltee4ec40a2011-08-21 11:07:20 +020079/*! \brief initialize subchannel demuxer structure
80 * \param[in,out] dmx subchannel demuxer
81 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020082int subch_demux_init(struct subch_demux *dmx)
83{
84 int i;
85
86 dmx->chan_activ = 0;
87 for (i = 0; i < NR_SUBCH; i++) {
88 struct demux_subch *sch = &dmx->subch[i];
89 sch->out_idx = 0;
90 memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
91 }
92 return 0;
93}
94
Harald Weltebcfd7ea2020-07-04 11:18:26 +020095static void append_bit_resync_out(struct subch_demux *dmx, int c, ubit_t bit)
96{
97 struct demux_subch *sch = &dmx->subch[c];
98 append_bit(sch, bit);
99
100 if (sync_hdr_complete(sch, bit))
101 resync_to_here(sch);
102
103 /* FIXME: verify the first bit in octet 2, 4, 6, ...
104 * according to TS 08.60 4.8.1 */
105
106 /* once we have reached TRAU_FRAME_BITS, call
107 * the TRAU frame handler callback function */
108 if (sch->out_idx >= TRAU_FRAME_BITS) {
109 if (sch->in_sync) {
110 dmx->out_cb(dmx, c, sch->out_bitbuf,
111 sch->out_idx, dmx->data);
112 sch->in_sync = 0;
113 }
114 sch->out_idx = 0;
115 }
116}
117
Harald Weltee4ec40a2011-08-21 11:07:20 +0200118/*! \brief Input some data from the 64k full-slot into subchannel demux
119 * \param[in] dmx subchannel demuxer
120 * \param[in] data pointer to buffer containing input data
121 * \param[in] len length of \a data in bytes
122 * \returns 0 in case of success, <0 otherwise.
123 *
124 * Input some arbitrary (modulo 4) number of bytes of a 64k E1 channel,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200125 * split it into the 16k subchannels */
126int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len)
127{
128 int i, c;
129
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200130 for (i = 0; i < len; i++) {
131 uint8_t inbyte = data[i];
132
133 for (c = 0; c < NR_SUBCH; c++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200134 uint8_t inbits;
135 uint8_t bit;
136
137 /* ignore inactive subchannels */
138 if (!(dmx->chan_activ & (1 << c)))
139 continue;
140
141 inbits = inbyte >> (c << 1);
142
143 /* two bits for each subchannel */
144 if (inbits & 0x01)
145 bit = 1;
146 else
147 bit = 0;
Harald Weltebcfd7ea2020-07-04 11:18:26 +0200148 append_bit_resync_out(dmx, c, bit);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200149
150 if (inbits & 0x02)
151 bit = 1;
152 else
153 bit = 0;
Harald Weltebcfd7ea2020-07-04 11:18:26 +0200154 append_bit_resync_out(dmx, c, bit);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200155 }
156 }
157 return i;
158}
159
Harald Weltee4ec40a2011-08-21 11:07:20 +0200160/*! \brief activate a given sub-channel
161 * \param[in] dmx subchannel demuxer
162 * \param[in] subch sub-channel number
163 * \returns 0 in case of success, <0 otherwise
164 *
165 * Activating a sub-channel will casuse the \ref subch_demux::out_cb
166 * callback to be called for any incoming data from the full slot
167 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200168int subch_demux_activate(struct subch_demux *dmx, int subch)
169{
170 if (subch >= NR_SUBCH)
171 return -EINVAL;
172
173 dmx->chan_activ |= (1 << subch);
174 return 0;
175}
176
Harald Weltee4ec40a2011-08-21 11:07:20 +0200177/*! \brief deactivate a given sub-channel
178 * \param[in] dmx subchannel demuxer
179 * \param[in] subch sub-channel number
180 * \returns 0 in case of success, <0 otherwise
181 *
182 * Deactivating a sub-channel will casuse the \ref subch_demux::out_cb
183 * callback no longer to be called.
184 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200185int subch_demux_deactivate(struct subch_demux *dmx, int subch)
186{
187 if (subch >= NR_SUBCH)
188 return -EINVAL;
189
190 dmx->chan_activ &= ~(1 << subch);
191 return 0;
192}
193
194/* MULTIPLEXER */
195
196static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
197{
198 /* allocate and initialize with idle pattern */
199 return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(),
200 TRAU_FRAME_BITS);
201}
202
203/* return the requested number of bits from the specified subchannel */
204static int get_subch_bits(struct subch_mux *mx, int subch,
Harald Weltea684b842020-05-09 10:33:42 +0200205 ubit_t *bits, int num_requested)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200206{
207 struct mux_subch *sch = &mx->subch[subch];
208 int num_bits = 0;
209
210 while (num_bits < num_requested) {
211 struct subch_txq_entry *txe;
212 int num_bits_left;
213 int num_bits_thistime;
214
215 /* make sure we have a valid entry at top of tx queue.
216 * if not, add an idle frame */
217 if (llist_empty(&sch->tx_queue))
218 alloc_add_idle_frame(mx, subch);
219
220 if (llist_empty(&sch->tx_queue))
221 return -EIO;
222
223 txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
224 num_bits_left = txe->bit_len - txe->next_bit;
225
226 if (num_bits_left < num_requested)
227 num_bits_thistime = num_bits_left;
228 else
229 num_bits_thistime = num_requested;
230
231 /* pull the bits from the txe */
232 memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime);
233 txe->next_bit += num_bits_thistime;
234
235 /* free the tx_queue entry if it is fully consumed */
236 if (txe->next_bit >= txe->bit_len) {
237 llist_del(&txe->list);
238 talloc_free(txe);
239 }
240
241 /* increment global number of bits dequeued */
242 num_bits += num_bits_thistime;
243 }
244
245 return num_requested;
246}
247
248/* compact an array of 8 single-bit bytes into one byte of 8 bits */
249static uint8_t compact_bits(const uint8_t *bits)
250{
251 uint8_t ret = 0;
252 int i;
253
254 for (i = 0; i < 8; i++)
255 ret |= (bits[i] ? 1 : 0) << i;
256
257 return ret;
258}
259
260/* obtain a single output byte from the subchannel muxer */
261static int mux_output_byte(struct subch_mux *mx, uint8_t *byte)
262{
Harald Weltea684b842020-05-09 10:33:42 +0200263 ubit_t bits[8];
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200264 int rc;
265
266 /* combine two bits of every subchan */
267 rc = get_subch_bits(mx, 0, &bits[0], 2);
268 rc = get_subch_bits(mx, 1, &bits[2], 2);
269 rc = get_subch_bits(mx, 2, &bits[4], 2);
270 rc = get_subch_bits(mx, 3, &bits[6], 2);
271
272 *byte = compact_bits(bits);
273
274 return rc;
275}
276
Harald Weltee4ec40a2011-08-21 11:07:20 +0200277/*! \brief Request the output of some muxed bytes from the subchan muxer
278 * \param[in] mx subchannel muxer
279 * \param[out] data caller-allocated buffer for data
280 * \param[in] len number of bytes to be filled into \a data
281 * \returns actual number of bytes filled into \a data
282 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200283int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len)
284{
285 int i;
286
287 for (i = 0; i < len; i++) {
288 int rc;
289 rc = mux_output_byte(mx, &data[i]);
290 if (rc < 0)
291 break;
292 }
293 return i;
294}
295
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200296/* evict the 'num_evict' number of oldest entries in the queue */
297static void tx_queue_evict(struct mux_subch *sch, int num_evict)
298{
299 struct subch_txq_entry *tqe;
300 int i;
301
302 for (i = 0; i < num_evict; i++) {
303 if (llist_empty(&sch->tx_queue))
304 return;
305
306 tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
307 llist_del(&tqe->list);
308 talloc_free(tqe);
309 }
310}
311
Harald Weltee4ec40a2011-08-21 11:07:20 +0200312/*! \brief enqueue some data into the tx_queue of a given subchannel
313 * \param[in] mx subchannel muxer instance
314 * \param[in] s_nr subchannel number
Harald Weltea684b842020-05-09 10:33:42 +0200315 * \param[in] data pointer to buffer with data (unpacked bits)
316 * \param[in] len length of data (in unpacked bits)
Harald Weltee4ec40a2011-08-21 11:07:20 +0200317 * \returns 0 in case of success, <0 in case of error
318 */
Harald Weltea684b842020-05-09 10:33:42 +0200319int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const ubit_t *data,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200320 int len)
321{
322 struct mux_subch *sch = &mx->subch[s_nr];
Neels Hofmeyr30ffa7a2017-01-11 00:42:46 +0100323 unsigned int list_len = llist_count(&sch->tx_queue);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200324 struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx,
325 sizeof(*tqe) + len);
326 if (!tqe)
327 return -ENOMEM;
328
329 tqe->bit_len = len;
330 memcpy(tqe->bits, data, len);
331
332 if (list_len > 2)
333 tx_queue_evict(sch, list_len-2);
334
335 llist_add_tail(&tqe->list, &sch->tx_queue);
336
337 return 0;
338}
339
Harald Weltee4ec40a2011-08-21 11:07:20 +0200340/*! \brief initialize one subchannel muxer instance
341 * \param[in,out] mx subchannel muxer instance, caller-allocated
342 * \returns 0 in success, < 0 in case of error
343 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200344int subchan_mux_init(struct subch_mux *mx)
345{
346 int i;
347
348 memset(mx, 0, sizeof(*mx));
349 for (i = 0; i < NR_SUBCH; i++) {
350 struct mux_subch *sch = &mx->subch[i];
351 INIT_LLIST_HEAD(&sch->tx_queue);
352 }
353
354 return 0;
355}
Harald Weltee4ec40a2011-08-21 11:07:20 +0200356
357/* }@ */