blob: 73efd318add495d9456cfc9917d7e6f4045f8c7a [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>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020031#include <talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020032
33void *tall_tqe_ctx;
34
35static inline void append_bit(struct demux_subch *sch, uint8_t bit)
36{
37 sch->out_bitbuf[sch->out_idx++] = bit;
38}
39
40#define SYNC_HDR_BITS 16
41static const uint8_t nullbytes[SYNC_HDR_BITS];
42
43/* check if we have just completed the 16 bit zero sync header,
44 * in accordance with GSM TS 08.60 Chapter 4.8.1 */
45static int sync_hdr_complete(struct demux_subch *sch, uint8_t bit)
46{
47 if (bit == 0)
48 sch->consecutive_zeros++;
49 else
50 sch->consecutive_zeros = 0;
51
52 if (sch->consecutive_zeros >= SYNC_HDR_BITS) {
53 sch->consecutive_zeros = 0;
54 return 1;
55 }
56
57 return 0;
58}
59
60/* resynchronize to current location */
61static void resync_to_here(struct demux_subch *sch)
62{
63 memset(sch->out_bitbuf, 0, SYNC_HDR_BITS);
64
65 /* set index in a way that we can continue receiving bits after
66 * the end of the SYNC header */
67 sch->out_idx = SYNC_HDR_BITS;
68 sch->in_sync = 1;
69}
70
71int subch_demux_init(struct subch_demux *dmx)
72{
73 int i;
74
75 dmx->chan_activ = 0;
76 for (i = 0; i < NR_SUBCH; i++) {
77 struct demux_subch *sch = &dmx->subch[i];
78 sch->out_idx = 0;
79 memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
80 }
81 return 0;
82}
83
84/* input some arbitrary (modulo 4) number of bytes of a 64k E1 channel,
85 * split it into the 16k subchannels */
86int subch_demux_in(struct subch_demux *dmx, uint8_t *data, int len)
87{
88 int i, c;
89
90 /* we avoid partially filled bytes in outbuf */
91 if (len % 4)
92 return -EINVAL;
93
94 for (i = 0; i < len; i++) {
95 uint8_t inbyte = data[i];
96
97 for (c = 0; c < NR_SUBCH; c++) {
98 struct demux_subch *sch = &dmx->subch[c];
99 uint8_t inbits;
100 uint8_t bit;
101
102 /* ignore inactive subchannels */
103 if (!(dmx->chan_activ & (1 << c)))
104 continue;
105
106 inbits = inbyte >> (c << 1);
107
108 /* two bits for each subchannel */
109 if (inbits & 0x01)
110 bit = 1;
111 else
112 bit = 0;
113 append_bit(sch, bit);
114
115 if (sync_hdr_complete(sch, bit))
116 resync_to_here(sch);
117
118 if (inbits & 0x02)
119 bit = 1;
120 else
121 bit = 0;
122 append_bit(sch, bit);
123
124 if (sync_hdr_complete(sch, bit))
125 resync_to_here(sch);
126
127 /* FIXME: verify the first bit in octet 2, 4, 6, ...
128 * according to TS 08.60 4.8.1 */
129
130 /* once we have reached TRAU_FRAME_BITS, call
131 * the TRAU frame handler callback function */
132 if (sch->out_idx >= TRAU_FRAME_BITS) {
133 if (sch->in_sync) {
134 dmx->out_cb(dmx, c, sch->out_bitbuf,
135 sch->out_idx, dmx->data);
136 sch->in_sync = 0;
137 }
138 sch->out_idx = 0;
139 }
140 }
141 }
142 return i;
143}
144
145int subch_demux_activate(struct subch_demux *dmx, int subch)
146{
147 if (subch >= NR_SUBCH)
148 return -EINVAL;
149
150 dmx->chan_activ |= (1 << subch);
151 return 0;
152}
153
154int subch_demux_deactivate(struct subch_demux *dmx, int subch)
155{
156 if (subch >= NR_SUBCH)
157 return -EINVAL;
158
159 dmx->chan_activ &= ~(1 << subch);
160 return 0;
161}
162
163/* MULTIPLEXER */
164
165static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
166{
167 /* allocate and initialize with idle pattern */
168 return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(),
169 TRAU_FRAME_BITS);
170}
171
172/* return the requested number of bits from the specified subchannel */
173static int get_subch_bits(struct subch_mux *mx, int subch,
174 uint8_t *bits, int num_requested)
175{
176 struct mux_subch *sch = &mx->subch[subch];
177 int num_bits = 0;
178
179 while (num_bits < num_requested) {
180 struct subch_txq_entry *txe;
181 int num_bits_left;
182 int num_bits_thistime;
183
184 /* make sure we have a valid entry at top of tx queue.
185 * if not, add an idle frame */
186 if (llist_empty(&sch->tx_queue))
187 alloc_add_idle_frame(mx, subch);
188
189 if (llist_empty(&sch->tx_queue))
190 return -EIO;
191
192 txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
193 num_bits_left = txe->bit_len - txe->next_bit;
194
195 if (num_bits_left < num_requested)
196 num_bits_thistime = num_bits_left;
197 else
198 num_bits_thistime = num_requested;
199
200 /* pull the bits from the txe */
201 memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime);
202 txe->next_bit += num_bits_thistime;
203
204 /* free the tx_queue entry if it is fully consumed */
205 if (txe->next_bit >= txe->bit_len) {
206 llist_del(&txe->list);
207 talloc_free(txe);
208 }
209
210 /* increment global number of bits dequeued */
211 num_bits += num_bits_thistime;
212 }
213
214 return num_requested;
215}
216
217/* compact an array of 8 single-bit bytes into one byte of 8 bits */
218static uint8_t compact_bits(const uint8_t *bits)
219{
220 uint8_t ret = 0;
221 int i;
222
223 for (i = 0; i < 8; i++)
224 ret |= (bits[i] ? 1 : 0) << i;
225
226 return ret;
227}
228
229/* obtain a single output byte from the subchannel muxer */
230static int mux_output_byte(struct subch_mux *mx, uint8_t *byte)
231{
232 uint8_t bits[8];
233 int rc;
234
235 /* combine two bits of every subchan */
236 rc = get_subch_bits(mx, 0, &bits[0], 2);
237 rc = get_subch_bits(mx, 1, &bits[2], 2);
238 rc = get_subch_bits(mx, 2, &bits[4], 2);
239 rc = get_subch_bits(mx, 3, &bits[6], 2);
240
241 *byte = compact_bits(bits);
242
243 return rc;
244}
245
246/* Request the output of some muxed bytes from the subchan muxer */
247int subchan_mux_out(struct subch_mux *mx, uint8_t *data, int len)
248{
249 int i;
250
251 for (i = 0; i < len; i++) {
252 int rc;
253 rc = mux_output_byte(mx, &data[i]);
254 if (rc < 0)
255 break;
256 }
257 return i;
258}
259
260static int llist_len(struct llist_head *head)
261{
262 struct llist_head *entry;
263 int i = 0;
264
265 llist_for_each(entry, head)
266 i++;
267
268 return i;
269}
270
271/* evict the 'num_evict' number of oldest entries in the queue */
272static void tx_queue_evict(struct mux_subch *sch, int num_evict)
273{
274 struct subch_txq_entry *tqe;
275 int i;
276
277 for (i = 0; i < num_evict; i++) {
278 if (llist_empty(&sch->tx_queue))
279 return;
280
281 tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
282 llist_del(&tqe->list);
283 talloc_free(tqe);
284 }
285}
286
287/* enqueue some data into the tx_queue of a given subchannel */
288int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const uint8_t *data,
289 int len)
290{
291 struct mux_subch *sch = &mx->subch[s_nr];
292 int list_len = llist_len(&sch->tx_queue);
293 struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx,
294 sizeof(*tqe) + len);
295 if (!tqe)
296 return -ENOMEM;
297
298 tqe->bit_len = len;
299 memcpy(tqe->bits, data, len);
300
301 if (list_len > 2)
302 tx_queue_evict(sch, list_len-2);
303
304 llist_add_tail(&tqe->list, &sch->tx_queue);
305
306 return 0;
307}
308
309/* initialize one subchannel muxer instance */
310int subchan_mux_init(struct subch_mux *mx)
311{
312 int i;
313
314 memset(mx, 0, sizeof(*mx));
315 for (i = 0; i < NR_SUBCH; i++) {
316 struct mux_subch *sch = &mx->subch[i];
317 INIT_LLIST_HEAD(&sch->tx_queue);
318 }
319
320 return 0;
321}