blob: ee62d41d389d5b05d8f6a5bbc94a77730fb353a5 [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
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * 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
Harald Welte54bd94d2009-01-05 19:00:01 +00009 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Harald Welte54bd94d2009-01-05 19:00:01 +000015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * 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/>.
Harald Welte54bd94d2009-01-05 19:00:01 +000018 *
19 */
20
21#include <unistd.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <errno.h>
26
27#include <openbsc/subchan_demux.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000028#include <openbsc/trau_frame.h>
Harald Welte7eb1f622009-02-18 03:40:58 +000029#include <openbsc/debug.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010030#include <osmocom/core/talloc.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020031#include <openbsc/gsm_data.h>
32
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020033void *tall_tqe_ctx;
Harald Welte54bd94d2009-01-05 19:00:01 +000034
Harald Welte1fa60c82009-02-09 18:13:26 +000035static inline void append_bit(struct demux_subch *sch, u_int8_t bit)
Harald Welte54bd94d2009-01-05 19:00:01 +000036{
37 sch->out_bitbuf[sch->out_idx++] = bit;
38}
39
40#define SYNC_HDR_BITS 16
41static const u_int8_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 */
Harald Welte7eb1f622009-02-18 03:40:58 +000045static int sync_hdr_complete(struct demux_subch *sch, u_int8_t bit)
Harald Welte54bd94d2009-01-05 19:00:01 +000046{
Harald Welte7eb1f622009-02-18 03:40:58 +000047 if (bit == 0)
48 sch->consecutive_zeros++;
49 else
50 sch->consecutive_zeros = 0;
Harald Welte54bd94d2009-01-05 19:00:01 +000051
Harald Welte7eb1f622009-02-18 03:40:58 +000052 if (sch->consecutive_zeros >= SYNC_HDR_BITS) {
53 sch->consecutive_zeros = 0;
54 return 1;
Harald Welte54bd94d2009-01-05 19:00:01 +000055 }
Harald Welte7eb1f622009-02-18 03:40:58 +000056
57 return 0;
Harald Welte54bd94d2009-01-05 19:00:01 +000058}
59
60/* resynchronize to current location */
Harald Welte1fa60c82009-02-09 18:13:26 +000061static void resync_to_here(struct demux_subch *sch)
Harald Welte54bd94d2009-01-05 19:00:01 +000062{
Harald Welte54bd94d2009-01-05 19:00:01 +000063 memset(sch->out_bitbuf, 0, SYNC_HDR_BITS);
Harald Welte54bd94d2009-01-05 19:00:01 +000064
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;
Harald Weltef1e6f962009-02-19 17:05:13 +000068 sch->in_sync = 1;
Harald Welte54bd94d2009-01-05 19:00:01 +000069}
70
Harald Weltece281c02009-01-05 20:14:14 +000071int 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++) {
Harald Welte1fa60c82009-02-09 18:13:26 +000077 struct demux_subch *sch = &dmx->subch[i];
Harald Weltece281c02009-01-05 20:14:14 +000078 sch->out_idx = 0;
79 memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
80 }
81 return 0;
82}
83
Harald Welte54bd94d2009-01-05 19:00:01 +000084/* 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, u_int8_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 u_int8_t inbyte = data[i];
96
97 for (c = 0; c < NR_SUBCH; c++) {
Harald Welte1fa60c82009-02-09 18:13:26 +000098 struct demux_subch *sch = &dmx->subch[c];
Harald Welte7eb1f622009-02-18 03:40:58 +000099 u_int8_t inbits;
Harald Welte54bd94d2009-01-05 19:00:01 +0000100 u_int8_t bit;
101
102 /* ignore inactive subchannels */
103 if (!(dmx->chan_activ & (1 << c)))
104 continue;
105
Harald Welte360f06c2009-04-29 16:21:18 +0000106 inbits = inbyte >> (c << 1);
Harald Welte7eb1f622009-02-18 03:40:58 +0000107
Harald Welte54bd94d2009-01-05 19:00:01 +0000108 /* two bits for each subchannel */
Harald Welte7eb1f622009-02-18 03:40:58 +0000109 if (inbits & 0x01)
Harald Welte54bd94d2009-01-05 19:00:01 +0000110 bit = 1;
111 else
112 bit = 0;
113 append_bit(sch, bit);
114
Harald Welte7eb1f622009-02-18 03:40:58 +0000115 if (sync_hdr_complete(sch, bit))
Harald Welte54bd94d2009-01-05 19:00:01 +0000116 resync_to_here(sch);
117
Harald Welte7eb1f622009-02-18 03:40:58 +0000118 if (inbits & 0x02)
Harald Welte54bd94d2009-01-05 19:00:01 +0000119 bit = 1;
120 else
121 bit = 0;
122 append_bit(sch, bit);
123
Harald Welte7eb1f622009-02-18 03:40:58 +0000124 if (sync_hdr_complete(sch, bit))
Harald Welte54bd94d2009-01-05 19:00:01 +0000125 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) {
Harald Weltef1e6f962009-02-19 17:05:13 +0000133 if (sch->in_sync) {
134 dmx->out_cb(dmx, c, sch->out_bitbuf,
Harald Welte54bd94d2009-01-05 19:00:01 +0000135 sch->out_idx, dmx->data);
Harald Weltef1e6f962009-02-19 17:05:13 +0000136 sch->in_sync = 0;
137 }
Harald Welte54bd94d2009-01-05 19:00:01 +0000138 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}
Harald Welte1fa60c82009-02-09 18:13:26 +0000162
163/* MULTIPLEXER */
164
165static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
166{
Harald Welte7eb1f622009-02-18 03:40:58 +0000167 /* allocate and initialize with idle pattern */
Harald Welte1fa60c82009-02-09 18:13:26 +0000168 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 u_int8_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 */
Holger Freyther3630eab2009-02-09 21:05:56 +0000186 if (llist_empty(&sch->tx_queue))
Harald Welte1fa60c82009-02-09 18:13:26 +0000187 alloc_add_idle_frame(mx, subch);
188
189 if (llist_empty(&sch->tx_queue))
190 return -EIO;
191
Holger Freyther3630eab2009-02-09 21:05:56 +0000192 txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000193 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 */
Harald Welte7eb1f622009-02-18 03:40:58 +0000201 memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime);
Harald Welte1fa60c82009-02-09 18:13:26 +0000202 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);
Harald Welte2cf161b2009-06-20 22:36:41 +0200207 talloc_free(txe);
Harald Welte1fa60c82009-02-09 18:13:26 +0000208 }
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 */
Harald Welte7eb1f622009-02-18 03:40:58 +0000218static u_int8_t compact_bits(const u_int8_t *bits)
Harald Welte1fa60c82009-02-09 18:13:26 +0000219{
220 u_int8_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, u_int8_t *byte)
231{
232 u_int8_t bits[8];
233 int rc;
234
235 /* combine two bits of every subchan */
Harald Welte360f06c2009-04-29 16:21:18 +0000236 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);
Harald Welte1fa60c82009-02-09 18:13:26 +0000240
Harald Welte7eb1f622009-02-18 03:40:58 +0000241 *byte = compact_bits(bits);
Harald Welte1fa60c82009-02-09 18:13:26 +0000242
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, u_int8_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
Harald Welteb49fe962009-02-22 22:28:19 +0000260static 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);
Harald Welte2cf161b2009-06-20 22:36:41 +0200283 talloc_free(tqe);
Harald Welteb49fe962009-02-22 22:28:19 +0000284 }
285}
286
Harald Welte1fa60c82009-02-09 18:13:26 +0000287/* enqueue some data into the tx_queue of a given subchannel */
288int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data,
289 int len)
290{
291 struct mux_subch *sch = &mx->subch[s_nr];
Harald Welteb49fe962009-02-22 22:28:19 +0000292 int list_len = llist_len(&sch->tx_queue);
Harald Welte470ec292009-06-26 20:25:23 +0200293 struct subch_txq_entry *tqe = talloc_zero_size(tall_tqe_ctx,
294 sizeof(*tqe) + len);
Harald Welte1fa60c82009-02-09 18:13:26 +0000295 if (!tqe)
296 return -ENOMEM;
297
Harald Welte1fa60c82009-02-09 18:13:26 +0000298 tqe->bit_len = len;
299 memcpy(tqe->bits, data, len);
300
Harald Welteb49fe962009-02-22 22:28:19 +0000301 if (list_len > 2)
302 tx_queue_evict(sch, list_len-2);
303
Holger Freyther3630eab2009-02-09 21:05:56 +0000304 llist_add_tail(&tqe->list, &sch->tx_queue);
Harald Welte1fa60c82009-02-09 18:13:26 +0000305
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}