blob: 41fe1d2dd18c1ec62c6b6b7be896dfe75e54a862 [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
49/* check if we have just completed the 16 bit zero sync header,
50 * in accordance with GSM TS 08.60 Chapter 4.8.1 */
51static int sync_hdr_complete(struct demux_subch *sch, uint8_t bit)
52{
53 if (bit == 0)
54 sch->consecutive_zeros++;
55 else
56 sch->consecutive_zeros = 0;
57
58 if (sch->consecutive_zeros >= SYNC_HDR_BITS) {
59 sch->consecutive_zeros = 0;
60 return 1;
61 }
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);
70
71 /* set index in a way that we can continue receiving bits after
72 * the end of the SYNC header */
73 sch->out_idx = SYNC_HDR_BITS;
74 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
105 /* we avoid partially filled bytes in outbuf */
106 if (len % 4)
107 return -EINVAL;
108
109 for (i = 0; i < len; i++) {
110 uint8_t inbyte = data[i];
111
112 for (c = 0; c < NR_SUBCH; c++) {
113 struct demux_subch *sch = &dmx->subch[c];
114 uint8_t inbits;
115 uint8_t bit;
116
117 /* ignore inactive subchannels */
118 if (!(dmx->chan_activ & (1 << c)))
119 continue;
120
121 inbits = inbyte >> (c << 1);
122
123 /* two bits for each subchannel */
124 if (inbits & 0x01)
125 bit = 1;
126 else
127 bit = 0;
128 append_bit(sch, bit);
129
130 if (sync_hdr_complete(sch, bit))
131 resync_to_here(sch);
132
133 if (inbits & 0x02)
134 bit = 1;
135 else
136 bit = 0;
137 append_bit(sch, bit);
138
139 if (sync_hdr_complete(sch, bit))
140 resync_to_here(sch);
141
142 /* FIXME: verify the first bit in octet 2, 4, 6, ...
143 * according to TS 08.60 4.8.1 */
144
145 /* once we have reached TRAU_FRAME_BITS, call
146 * the TRAU frame handler callback function */
147 if (sch->out_idx >= TRAU_FRAME_BITS) {
148 if (sch->in_sync) {
149 dmx->out_cb(dmx, c, sch->out_bitbuf,
150 sch->out_idx, dmx->data);
151 sch->in_sync = 0;
152 }
153 sch->out_idx = 0;
154 }
155 }
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,
205 uint8_t *bits, int num_requested)
206{
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{
263 uint8_t bits[8];
264 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
296static int llist_len(struct llist_head *head)
297{
298 struct llist_head *entry;
299 int i = 0;
300
301 llist_for_each(entry, head)
302 i++;
303
304 return i;
305}
306
307/* evict the 'num_evict' number of oldest entries in the queue */
308static void tx_queue_evict(struct mux_subch *sch, int num_evict)
309{
310 struct subch_txq_entry *tqe;
311 int i;
312
313 for (i = 0; i < num_evict; i++) {
314 if (llist_empty(&sch->tx_queue))
315 return;
316
317 tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
318 llist_del(&tqe->list);
319 talloc_free(tqe);
320 }
321}
322
Harald Weltee4ec40a2011-08-21 11:07:20 +0200323/*! \brief enqueue some data into the tx_queue of a given subchannel
324 * \param[in] mx subchannel muxer instance
325 * \param[in] s_nr subchannel number
326 * \param[in] data pointer to buffer with data
327 * \param[in] len length of \a data
328 * \returns 0 in case of success, <0 in case of error
329 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200330int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data,
331 int len)
332{
333 struct mux_subch *sch = &mx->subch[s_nr];
334 int list_len = llist_len(&sch->tx_queue);
335 struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx,
336 sizeof(*tqe) + len);
337 if (!tqe)
338 return -ENOMEM;
339
340 tqe->bit_len = len;
341 memcpy(tqe->bits, data, len);
342
343 if (list_len > 2)
344 tx_queue_evict(sch, list_len-2);
345
346 llist_add_tail(&tqe->list, &sch->tx_queue);
347
348 return 0;
349}
350
Harald Weltee4ec40a2011-08-21 11:07:20 +0200351/*! \brief initialize one subchannel muxer instance
352 * \param[in,out] mx subchannel muxer instance, caller-allocated
353 * \returns 0 in success, < 0 in case of error
354 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355int subchan_mux_init(struct subch_mux *mx)
356{
357 int i;
358
359 memset(mx, 0, sizeof(*mx));
360 for (i = 0; i < NR_SUBCH; i++) {
361 struct mux_subch *sch = &mx->subch[i];
362 INIT_LLIST_HEAD(&sch->tx_queue);
363 }
364
365 return 0;
366}
Harald Weltee4ec40a2011-08-21 11:07:20 +0200367
368/* }@ */