blob: 4cd6094d8d18b60e1d1c6818e2263b88f904b71d [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
47static const uint8_t nullbytes[SYNC_HDR_BITS];
48
Dieter Spaar08c10322012-10-08 21:36:03 +020049/* check if we have just completed the 16 bit zero + 1 bit one sync
50 * header, in accordance with GSM TS 08.60 Chapter 4.8.1 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051static int sync_hdr_complete(struct demux_subch *sch, uint8_t bit)
52{
53 if (bit == 0)
54 sch->consecutive_zeros++;
Dieter Spaar08c10322012-10-08 21:36:03 +020055 else {
56 if (sch->consecutive_zeros >= SYNC_HDR_BITS) {
57 sch->consecutive_zeros = 0;
58 return 1;
59 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020060 sch->consecutive_zeros = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020061 }
62
63 return 0;
64}
65
66/* resynchronize to current location */
67static void resync_to_here(struct demux_subch *sch)
68{
69 memset(sch->out_bitbuf, 0, SYNC_HDR_BITS);
Dieter Spaar08c10322012-10-08 21:36:03 +020070 sch->out_bitbuf[SYNC_HDR_BITS] = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020071
72 /* set index in a way that we can continue receiving bits after
73 * the end of the SYNC header */
Dieter Spaar08c10322012-10-08 21:36:03 +020074 sch->out_idx = SYNC_HDR_BITS + 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020075 sch->in_sync = 1;
76}
77
Harald Weltee4ec40a2011-08-21 11:07:20 +020078/*! \brief initialize subchannel demuxer structure
79 * \param[in,out] dmx subchannel demuxer
80 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020081int subch_demux_init(struct subch_demux *dmx)
82{
83 int i;
84
85 dmx->chan_activ = 0;
86 for (i = 0; i < NR_SUBCH; i++) {
87 struct demux_subch *sch = &dmx->subch[i];
88 sch->out_idx = 0;
89 memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
90 }
91 return 0;
92}
93
Harald Weltee4ec40a2011-08-21 11:07:20 +020094/*! \brief Input some data from the 64k full-slot into subchannel demux
95 * \param[in] dmx subchannel demuxer
96 * \param[in] data pointer to buffer containing input data
97 * \param[in] len length of \a data in bytes
98 * \returns 0 in case of success, <0 otherwise.
99 *
100 * Input some arbitrary (modulo 4) number of bytes of a 64k E1 channel,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200101 * split it into the 16k subchannels */
102int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len)
103{
104 int i, c;
105
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200106 for (i = 0; i < len; i++) {
107 uint8_t inbyte = data[i];
108
109 for (c = 0; c < NR_SUBCH; c++) {
110 struct demux_subch *sch = &dmx->subch[c];
111 uint8_t inbits;
112 uint8_t bit;
113
114 /* ignore inactive subchannels */
115 if (!(dmx->chan_activ & (1 << c)))
116 continue;
117
118 inbits = inbyte >> (c << 1);
119
120 /* two bits for each subchannel */
121 if (inbits & 0x01)
122 bit = 1;
123 else
124 bit = 0;
125 append_bit(sch, bit);
126
127 if (sync_hdr_complete(sch, bit))
128 resync_to_here(sch);
129
130 if (inbits & 0x02)
131 bit = 1;
132 else
133 bit = 0;
134 append_bit(sch, bit);
135
136 if (sync_hdr_complete(sch, bit))
137 resync_to_here(sch);
138
139 /* FIXME: verify the first bit in octet 2, 4, 6, ...
140 * according to TS 08.60 4.8.1 */
141
142 /* once we have reached TRAU_FRAME_BITS, call
143 * the TRAU frame handler callback function */
144 if (sch->out_idx >= TRAU_FRAME_BITS) {
145 if (sch->in_sync) {
146 dmx->out_cb(dmx, c, sch->out_bitbuf,
147 sch->out_idx, dmx->data);
148 sch->in_sync = 0;
149 }
150 sch->out_idx = 0;
151 }
152 }
153 }
154 return i;
155}
156
Harald Weltee4ec40a2011-08-21 11:07:20 +0200157/*! \brief activate a given sub-channel
158 * \param[in] dmx subchannel demuxer
159 * \param[in] subch sub-channel number
160 * \returns 0 in case of success, <0 otherwise
161 *
162 * Activating a sub-channel will casuse the \ref subch_demux::out_cb
163 * callback to be called for any incoming data from the full slot
164 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200165int subch_demux_activate(struct subch_demux *dmx, int subch)
166{
167 if (subch >= NR_SUBCH)
168 return -EINVAL;
169
170 dmx->chan_activ |= (1 << subch);
171 return 0;
172}
173
Harald Weltee4ec40a2011-08-21 11:07:20 +0200174/*! \brief deactivate a given sub-channel
175 * \param[in] dmx subchannel demuxer
176 * \param[in] subch sub-channel number
177 * \returns 0 in case of success, <0 otherwise
178 *
179 * Deactivating a sub-channel will casuse the \ref subch_demux::out_cb
180 * callback no longer to be called.
181 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200182int subch_demux_deactivate(struct subch_demux *dmx, int subch)
183{
184 if (subch >= NR_SUBCH)
185 return -EINVAL;
186
187 dmx->chan_activ &= ~(1 << subch);
188 return 0;
189}
190
191/* MULTIPLEXER */
192
193static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
194{
195 /* allocate and initialize with idle pattern */
196 return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(),
197 TRAU_FRAME_BITS);
198}
199
200/* return the requested number of bits from the specified subchannel */
201static int get_subch_bits(struct subch_mux *mx, int subch,
202 uint8_t *bits, int num_requested)
203{
204 struct mux_subch *sch = &mx->subch[subch];
205 int num_bits = 0;
206
207 while (num_bits < num_requested) {
208 struct subch_txq_entry *txe;
209 int num_bits_left;
210 int num_bits_thistime;
211
212 /* make sure we have a valid entry at top of tx queue.
213 * if not, add an idle frame */
214 if (llist_empty(&sch->tx_queue))
215 alloc_add_idle_frame(mx, subch);
216
217 if (llist_empty(&sch->tx_queue))
218 return -EIO;
219
220 txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
221 num_bits_left = txe->bit_len - txe->next_bit;
222
223 if (num_bits_left < num_requested)
224 num_bits_thistime = num_bits_left;
225 else
226 num_bits_thistime = num_requested;
227
228 /* pull the bits from the txe */
229 memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime);
230 txe->next_bit += num_bits_thistime;
231
232 /* free the tx_queue entry if it is fully consumed */
233 if (txe->next_bit >= txe->bit_len) {
234 llist_del(&txe->list);
235 talloc_free(txe);
236 }
237
238 /* increment global number of bits dequeued */
239 num_bits += num_bits_thistime;
240 }
241
242 return num_requested;
243}
244
245/* compact an array of 8 single-bit bytes into one byte of 8 bits */
246static uint8_t compact_bits(const uint8_t *bits)
247{
248 uint8_t ret = 0;
249 int i;
250
251 for (i = 0; i < 8; i++)
252 ret |= (bits[i] ? 1 : 0) << i;
253
254 return ret;
255}
256
257/* obtain a single output byte from the subchannel muxer */
258static int mux_output_byte(struct subch_mux *mx, uint8_t *byte)
259{
260 uint8_t bits[8];
261 int rc;
262
263 /* combine two bits of every subchan */
264 rc = get_subch_bits(mx, 0, &bits[0], 2);
265 rc = get_subch_bits(mx, 1, &bits[2], 2);
266 rc = get_subch_bits(mx, 2, &bits[4], 2);
267 rc = get_subch_bits(mx, 3, &bits[6], 2);
268
269 *byte = compact_bits(bits);
270
271 return rc;
272}
273
Harald Weltee4ec40a2011-08-21 11:07:20 +0200274/*! \brief Request the output of some muxed bytes from the subchan muxer
275 * \param[in] mx subchannel muxer
276 * \param[out] data caller-allocated buffer for data
277 * \param[in] len number of bytes to be filled into \a data
278 * \returns actual number of bytes filled into \a data
279 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200280int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len)
281{
282 int i;
283
284 for (i = 0; i < len; i++) {
285 int rc;
286 rc = mux_output_byte(mx, &data[i]);
287 if (rc < 0)
288 break;
289 }
290 return i;
291}
292
293static int llist_len(struct llist_head *head)
294{
295 struct llist_head *entry;
296 int i = 0;
297
298 llist_for_each(entry, head)
299 i++;
300
301 return i;
302}
303
304/* evict the 'num_evict' number of oldest entries in the queue */
305static void tx_queue_evict(struct mux_subch *sch, int num_evict)
306{
307 struct subch_txq_entry *tqe;
308 int i;
309
310 for (i = 0; i < num_evict; i++) {
311 if (llist_empty(&sch->tx_queue))
312 return;
313
314 tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
315 llist_del(&tqe->list);
316 talloc_free(tqe);
317 }
318}
319
Harald Weltee4ec40a2011-08-21 11:07:20 +0200320/*! \brief enqueue some data into the tx_queue of a given subchannel
321 * \param[in] mx subchannel muxer instance
322 * \param[in] s_nr subchannel number
323 * \param[in] data pointer to buffer with data
324 * \param[in] len length of \a data
325 * \returns 0 in case of success, <0 in case of error
326 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200327int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data,
328 int len)
329{
330 struct mux_subch *sch = &mx->subch[s_nr];
331 int list_len = llist_len(&sch->tx_queue);
332 struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx,
333 sizeof(*tqe) + len);
334 if (!tqe)
335 return -ENOMEM;
336
337 tqe->bit_len = len;
338 memcpy(tqe->bits, data, len);
339
340 if (list_len > 2)
341 tx_queue_evict(sch, list_len-2);
342
343 llist_add_tail(&tqe->list, &sch->tx_queue);
344
345 return 0;
346}
347
Harald Weltee4ec40a2011-08-21 11:07:20 +0200348/*! \brief initialize one subchannel muxer instance
349 * \param[in,out] mx subchannel muxer instance, caller-allocated
350 * \returns 0 in success, < 0 in case of error
351 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200352int subchan_mux_init(struct subch_mux *mx)
353{
354 int i;
355
356 memset(mx, 0, sizeof(*mx));
357 for (i = 0; i < NR_SUBCH; i++) {
358 struct mux_subch *sch = &mx->subch[i];
359 INIT_LLIST_HEAD(&sch->tx_queue);
360 }
361
362 return 0;
363}
Harald Weltee4ec40a2011-08-21 11:07:20 +0200364
365/* }@ */