blob: c662bcd0e8f7e287fd1a46fab7bb5891e4b1a080 [file] [log] [blame]
Harald Welte1fa60c82009-02-09 18:13:26 +00001/* A E1 sub-channel (de)multiplexer with TRAU frame sync */
Harald Welte54bd94d2009-01-05 19:00:01 +00002
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 General Public License as published by
8 * the Free Software Foundation; either version 2 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <unistd.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <errno.h>
27
28#include <openbsc/subchan_demux.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000029#include <openbsc/trau_frame.h>
Harald Welte7eb1f622009-02-18 03:40:58 +000030#include <openbsc/debug.h>
Harald Welte54bd94d2009-01-05 19:00:01 +000031
Harald Welte1fa60c82009-02-09 18:13:26 +000032static inline void append_bit(struct demux_subch *sch, u_int8_t bit)
Harald Welte54bd94d2009-01-05 19:00:01 +000033{
34 sch->out_bitbuf[sch->out_idx++] = bit;
35}
36
37#define SYNC_HDR_BITS 16
38static const u_int8_t nullbytes[SYNC_HDR_BITS];
39
40/* check if we have just completed the 16 bit zero sync header,
41 * in accordance with GSM TS 08.60 Chapter 4.8.1 */
Harald Welte7eb1f622009-02-18 03:40:58 +000042static int sync_hdr_complete(struct demux_subch *sch, u_int8_t bit)
Harald Welte54bd94d2009-01-05 19:00:01 +000043{
Harald Welte7eb1f622009-02-18 03:40:58 +000044 if (bit == 0)
45 sch->consecutive_zeros++;
46 else
47 sch->consecutive_zeros = 0;
Harald Welte54bd94d2009-01-05 19:00:01 +000048
Harald Welte7eb1f622009-02-18 03:40:58 +000049 if (sch->consecutive_zeros >= SYNC_HDR_BITS) {
50 sch->consecutive_zeros = 0;
51 return 1;
Harald Welte54bd94d2009-01-05 19:00:01 +000052 }
Harald Welte7eb1f622009-02-18 03:40:58 +000053
54 return 0;
Harald Welte54bd94d2009-01-05 19:00:01 +000055}
56
57/* resynchronize to current location */
Harald Welte1fa60c82009-02-09 18:13:26 +000058static void resync_to_here(struct demux_subch *sch)
Harald Welte54bd94d2009-01-05 19:00:01 +000059{
Harald Welte54bd94d2009-01-05 19:00:01 +000060 memset(sch->out_bitbuf, 0, SYNC_HDR_BITS);
Harald Welte54bd94d2009-01-05 19:00:01 +000061
62 /* set index in a way that we can continue receiving bits after
63 * the end of the SYNC header */
64 sch->out_idx = SYNC_HDR_BITS;
Harald Weltef1e6f962009-02-19 17:05:13 +000065 sch->in_sync = 1;
Harald Welte54bd94d2009-01-05 19:00:01 +000066}
67
Harald Weltece281c02009-01-05 20:14:14 +000068int subch_demux_init(struct subch_demux *dmx)
69{
70 int i;
71
72 dmx->chan_activ = 0;
73 for (i = 0; i < NR_SUBCH; i++) {
Harald Welte1fa60c82009-02-09 18:13:26 +000074 struct demux_subch *sch = &dmx->subch[i];
Harald Weltece281c02009-01-05 20:14:14 +000075 sch->out_idx = 0;
76 memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
77 }
78 return 0;
79}
80
Harald Welte54bd94d2009-01-05 19:00:01 +000081/* input some arbitrary (modulo 4) number of bytes of a 64k E1 channel,
82 * split it into the 16k subchannels */
83int subch_demux_in(struct subch_demux *dmx, u_int8_t *data, int len)
84{
85 int i, c;
86
87 /* we avoid partially filled bytes in outbuf */
88 if (len % 4)
89 return -EINVAL;
90
91 for (i = 0; i < len; i++) {
92 u_int8_t inbyte = data[i];
93
94 for (c = 0; c < NR_SUBCH; c++) {
Harald Welte1fa60c82009-02-09 18:13:26 +000095 struct demux_subch *sch = &dmx->subch[c];
Harald Welte7eb1f622009-02-18 03:40:58 +000096 u_int8_t inbits;
Harald Welte54bd94d2009-01-05 19:00:01 +000097 u_int8_t bit;
98
99 /* ignore inactive subchannels */
100 if (!(dmx->chan_activ & (1 << c)))
101 continue;
102
Harald Welte360f06c2009-04-29 16:21:18 +0000103 inbits = inbyte >> (c << 1);
Harald Welte7eb1f622009-02-18 03:40:58 +0000104
Harald Welte54bd94d2009-01-05 19:00:01 +0000105 /* two bits for each subchannel */
Harald Welte7eb1f622009-02-18 03:40:58 +0000106 if (inbits & 0x01)
Harald Welte54bd94d2009-01-05 19:00:01 +0000107 bit = 1;
108 else
109 bit = 0;
110 append_bit(sch, bit);
111
Harald Welte7eb1f622009-02-18 03:40:58 +0000112 if (sync_hdr_complete(sch, bit))
Harald Welte54bd94d2009-01-05 19:00:01 +0000113 resync_to_here(sch);
114
Harald Welte7eb1f622009-02-18 03:40:58 +0000115 if (inbits & 0x02)
Harald Welte54bd94d2009-01-05 19:00:01 +0000116 bit = 1;
117 else
118 bit = 0;
119 append_bit(sch, bit);
120
Harald Welte7eb1f622009-02-18 03:40:58 +0000121 if (sync_hdr_complete(sch, bit))
Harald Welte54bd94d2009-01-05 19:00:01 +0000122 resync_to_here(sch);
123
124 /* FIXME: verify the first bit in octet 2, 4, 6, ...
125 * according to TS 08.60 4.8.1 */
126
127 /* once we have reached TRAU_FRAME_BITS, call
128 * the TRAU frame handler callback function */
129 if (sch->out_idx >= TRAU_FRAME_BITS) {
Harald Weltef1e6f962009-02-19 17:05:13 +0000130 if (sch->in_sync) {
131 dmx->out_cb(dmx, c, sch->out_bitbuf,
Harald Welte54bd94d2009-01-05 19:00:01 +0000132 sch->out_idx, dmx->data);
Harald Weltef1e6f962009-02-19 17:05:13 +0000133 sch->in_sync = 0;
134 }
Harald Welte54bd94d2009-01-05 19:00:01 +0000135 sch->out_idx = 0;
136 }
137 }
138 }
139 return i;
140}
141
142int subch_demux_activate(struct subch_demux *dmx, int subch)
143{
144 if (subch >= NR_SUBCH)
145 return -EINVAL;
146
147 dmx->chan_activ |= (1 << subch);
148 return 0;
149}
150
151int subch_demux_deactivate(struct subch_demux *dmx, int subch)
152{
153 if (subch >= NR_SUBCH)
154 return -EINVAL;
155
156 dmx->chan_activ &= ~(1 << subch);
157 return 0;
158}
Harald Welte1fa60c82009-02-09 18:13:26 +0000159
160/* MULTIPLEXER */
161
162static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
163{
Harald Welte7eb1f622009-02-18 03:40:58 +0000164 /* allocate and initialize with idle pattern */
Harald Welte1fa60c82009-02-09 18:13:26 +0000165 return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(),
166 TRAU_FRAME_BITS);
167}
168
169/* return the requested number of bits from the specified subchannel */
170static int get_subch_bits(struct subch_mux *mx, int subch,
171 u_int8_t *bits, int num_requested)
172{
173 struct mux_subch *sch = &mx->subch[subch];
174 int num_bits = 0;
175
176 while (num_bits < num_requested) {
177 struct subch_txq_entry *txe;
178 int num_bits_left;
179 int num_bits_thistime;
180
181 /* make sure we have a valid entry at top of tx queue.
182 * if not, add an idle frame */
Holger Freyther3630eab2009-02-09 21:05:56 +0000183 if (llist_empty(&sch->tx_queue))
Harald Welte1fa60c82009-02-09 18:13:26 +0000184 alloc_add_idle_frame(mx, subch);
185
186 if (llist_empty(&sch->tx_queue))
187 return -EIO;
188
Holger Freyther3630eab2009-02-09 21:05:56 +0000189 txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000190 num_bits_left = txe->bit_len - txe->next_bit;
191
192 if (num_bits_left < num_requested)
193 num_bits_thistime = num_bits_left;
194 else
195 num_bits_thistime = num_requested;
196
197 /* pull the bits from the txe */
Harald Welte7eb1f622009-02-18 03:40:58 +0000198 memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime);
Harald Welte1fa60c82009-02-09 18:13:26 +0000199 txe->next_bit += num_bits_thistime;
200
201 /* free the tx_queue entry if it is fully consumed */
202 if (txe->next_bit >= txe->bit_len) {
203 llist_del(&txe->list);
204 free(txe);
205 }
206
207 /* increment global number of bits dequeued */
208 num_bits += num_bits_thistime;
209 }
210
211 return num_requested;
212}
213
214/* compact an array of 8 single-bit bytes into one byte of 8 bits */
Harald Welte7eb1f622009-02-18 03:40:58 +0000215static u_int8_t compact_bits(const u_int8_t *bits)
Harald Welte1fa60c82009-02-09 18:13:26 +0000216{
217 u_int8_t ret = 0;
218 int i;
219
220 for (i = 0; i < 8; i++)
221 ret |= (bits[i] ? 1 : 0) << i;
222
223 return ret;
224}
225
226/* obtain a single output byte from the subchannel muxer */
227static int mux_output_byte(struct subch_mux *mx, u_int8_t *byte)
228{
229 u_int8_t bits[8];
230 int rc;
231
232 /* combine two bits of every subchan */
Harald Welte360f06c2009-04-29 16:21:18 +0000233 rc = get_subch_bits(mx, 0, &bits[0], 2);
234 rc = get_subch_bits(mx, 1, &bits[2], 2);
235 rc = get_subch_bits(mx, 2, &bits[4], 2);
236 rc = get_subch_bits(mx, 3, &bits[6], 2);
Harald Welte1fa60c82009-02-09 18:13:26 +0000237
Harald Welte7eb1f622009-02-18 03:40:58 +0000238 *byte = compact_bits(bits);
Harald Welte1fa60c82009-02-09 18:13:26 +0000239
240 return rc;
241}
242
243/* Request the output of some muxed bytes from the subchan muxer */
244int subchan_mux_out(struct subch_mux *mx, u_int8_t *data, int len)
245{
246 int i;
247
248 for (i = 0; i < len; i++) {
249 int rc;
250 rc = mux_output_byte(mx, &data[i]);
251 if (rc < 0)
252 break;
253 }
254 return i;
255}
256
Harald Welteb49fe962009-02-22 22:28:19 +0000257static int llist_len(struct llist_head *head)
258{
259 struct llist_head *entry;
260 int i = 0;
261
262 llist_for_each(entry, head)
263 i++;
264
265 return i;
266}
267
268/* evict the 'num_evict' number of oldest entries in the queue */
269static void tx_queue_evict(struct mux_subch *sch, int num_evict)
270{
271 struct subch_txq_entry *tqe;
272 int i;
273
274 for (i = 0; i < num_evict; i++) {
275 if (llist_empty(&sch->tx_queue))
276 return;
277
278 tqe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
279 llist_del(&tqe->list);
280 free(tqe);
281 }
282}
283
Harald Welte1fa60c82009-02-09 18:13:26 +0000284/* enqueue some data into the tx_queue of a given subchannel */
285int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data,
286 int len)
287{
288 struct mux_subch *sch = &mx->subch[s_nr];
289 struct subch_txq_entry *tqe = malloc(sizeof(*tqe) + len);
Harald Welteb49fe962009-02-22 22:28:19 +0000290 int list_len = llist_len(&sch->tx_queue);
Harald Welte1fa60c82009-02-09 18:13:26 +0000291
292 if (!tqe)
293 return -ENOMEM;
294
295 memset(tqe, 0, sizeof(*tqe));
296 tqe->bit_len = len;
297 memcpy(tqe->bits, data, len);
298
Harald Welteb49fe962009-02-22 22:28:19 +0000299 if (list_len > 2)
300 tx_queue_evict(sch, list_len-2);
301
Holger Freyther3630eab2009-02-09 21:05:56 +0000302 llist_add_tail(&tqe->list, &sch->tx_queue);
Harald Welte1fa60c82009-02-09 18:13:26 +0000303
304 return 0;
305}
306
307/* initialize one subchannel muxer instance */
308int subchan_mux_init(struct subch_mux *mx)
309{
310 int i;
311
312 memset(mx, 0, sizeof(*mx));
313 for (i = 0; i < NR_SUBCH; i++) {
314 struct mux_subch *sch = &mx->subch[i];
315 INIT_LLIST_HEAD(&sch->tx_queue);
316 }
317
318 return 0;
319}