blob: 86ddc86cdff24c656b5088de91e0853d1dd6ffaf [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;
65}
66
Harald Weltece281c02009-01-05 20:14:14 +000067int subch_demux_init(struct subch_demux *dmx)
68{
69 int i;
70
71 dmx->chan_activ = 0;
72 for (i = 0; i < NR_SUBCH; i++) {
Harald Welte1fa60c82009-02-09 18:13:26 +000073 struct demux_subch *sch = &dmx->subch[i];
Harald Weltece281c02009-01-05 20:14:14 +000074 sch->out_idx = 0;
75 memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
76 }
77 return 0;
78}
79
Harald Welte54bd94d2009-01-05 19:00:01 +000080/* input some arbitrary (modulo 4) number of bytes of a 64k E1 channel,
81 * split it into the 16k subchannels */
82int subch_demux_in(struct subch_demux *dmx, u_int8_t *data, int len)
83{
84 int i, c;
85
86 /* we avoid partially filled bytes in outbuf */
87 if (len % 4)
88 return -EINVAL;
89
90 for (i = 0; i < len; i++) {
91 u_int8_t inbyte = data[i];
92
93 for (c = 0; c < NR_SUBCH; c++) {
Harald Welte1fa60c82009-02-09 18:13:26 +000094 struct demux_subch *sch = &dmx->subch[c];
Harald Welte7eb1f622009-02-18 03:40:58 +000095 u_int8_t inbits;
Harald Welte54bd94d2009-01-05 19:00:01 +000096 u_int8_t bit;
97
98 /* ignore inactive subchannels */
99 if (!(dmx->chan_activ & (1 << c)))
100 continue;
101
Harald Welte7eb1f622009-02-18 03:40:58 +0000102 inbits = inbyte >> ((3-c)*2);
103
Harald Welte54bd94d2009-01-05 19:00:01 +0000104 /* two bits for each subchannel */
Harald Welte7eb1f622009-02-18 03:40:58 +0000105 if (inbits & 0x01)
Harald Welte54bd94d2009-01-05 19:00:01 +0000106 bit = 1;
107 else
108 bit = 0;
109 append_bit(sch, bit);
110
Harald Welte7eb1f622009-02-18 03:40:58 +0000111 if (sync_hdr_complete(sch, bit))
Harald Welte54bd94d2009-01-05 19:00:01 +0000112 resync_to_here(sch);
113
Harald Welte7eb1f622009-02-18 03:40:58 +0000114 if (inbits & 0x02)
Harald Welte54bd94d2009-01-05 19:00:01 +0000115 bit = 1;
116 else
117 bit = 0;
118 append_bit(sch, bit);
119
Harald Welte7eb1f622009-02-18 03:40:58 +0000120 if (sync_hdr_complete(sch, bit))
Harald Welte54bd94d2009-01-05 19:00:01 +0000121 resync_to_here(sch);
122
123 /* FIXME: verify the first bit in octet 2, 4, 6, ...
124 * according to TS 08.60 4.8.1 */
125
126 /* once we have reached TRAU_FRAME_BITS, call
127 * the TRAU frame handler callback function */
128 if (sch->out_idx >= TRAU_FRAME_BITS) {
129 dmx->out_cb(dmx, c, sch->out_bitbuf,
130 sch->out_idx, dmx->data);
131 sch->out_idx = 0;
132 }
133 }
134 }
135 return i;
136}
137
138int subch_demux_activate(struct subch_demux *dmx, int subch)
139{
140 if (subch >= NR_SUBCH)
141 return -EINVAL;
142
143 dmx->chan_activ |= (1 << subch);
144 return 0;
145}
146
147int subch_demux_deactivate(struct subch_demux *dmx, int subch)
148{
149 if (subch >= NR_SUBCH)
150 return -EINVAL;
151
152 dmx->chan_activ &= ~(1 << subch);
153 return 0;
154}
Harald Welte1fa60c82009-02-09 18:13:26 +0000155
156/* MULTIPLEXER */
157
158static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
159{
Harald Welte7eb1f622009-02-18 03:40:58 +0000160 /* allocate and initialize with idle pattern */
Harald Welte1fa60c82009-02-09 18:13:26 +0000161 return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(),
162 TRAU_FRAME_BITS);
163}
164
165/* return the requested number of bits from the specified subchannel */
166static int get_subch_bits(struct subch_mux *mx, int subch,
167 u_int8_t *bits, int num_requested)
168{
169 struct mux_subch *sch = &mx->subch[subch];
170 int num_bits = 0;
171
172 while (num_bits < num_requested) {
173 struct subch_txq_entry *txe;
174 int num_bits_left;
175 int num_bits_thistime;
176
177 /* make sure we have a valid entry at top of tx queue.
178 * if not, add an idle frame */
Holger Freyther3630eab2009-02-09 21:05:56 +0000179 if (llist_empty(&sch->tx_queue))
Harald Welte1fa60c82009-02-09 18:13:26 +0000180 alloc_add_idle_frame(mx, subch);
181
182 if (llist_empty(&sch->tx_queue))
183 return -EIO;
184
Holger Freyther3630eab2009-02-09 21:05:56 +0000185 txe = llist_entry(sch->tx_queue.next, struct subch_txq_entry, list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000186 num_bits_left = txe->bit_len - txe->next_bit;
187
188 if (num_bits_left < num_requested)
189 num_bits_thistime = num_bits_left;
190 else
191 num_bits_thistime = num_requested;
192
193 /* pull the bits from the txe */
Harald Welte7eb1f622009-02-18 03:40:58 +0000194 memcpy(bits + num_bits, txe->bits + txe->next_bit, num_bits_thistime);
Harald Welte1fa60c82009-02-09 18:13:26 +0000195 txe->next_bit += num_bits_thistime;
196
197 /* free the tx_queue entry if it is fully consumed */
198 if (txe->next_bit >= txe->bit_len) {
199 llist_del(&txe->list);
200 free(txe);
201 }
202
203 /* increment global number of bits dequeued */
204 num_bits += num_bits_thistime;
205 }
206
207 return num_requested;
208}
209
210/* compact an array of 8 single-bit bytes into one byte of 8 bits */
Harald Welte7eb1f622009-02-18 03:40:58 +0000211static u_int8_t compact_bits(const u_int8_t *bits)
Harald Welte1fa60c82009-02-09 18:13:26 +0000212{
213 u_int8_t ret = 0;
214 int i;
215
216 for (i = 0; i < 8; i++)
217 ret |= (bits[i] ? 1 : 0) << i;
218
219 return ret;
220}
221
222/* obtain a single output byte from the subchannel muxer */
223static int mux_output_byte(struct subch_mux *mx, u_int8_t *byte)
224{
225 u_int8_t bits[8];
226 int rc;
227
228 /* combine two bits of every subchan */
Harald Welte7eb1f622009-02-18 03:40:58 +0000229 rc = get_subch_bits(mx, 3, &bits[0], 2);
230 rc = get_subch_bits(mx, 2, &bits[2], 2);
231 rc = get_subch_bits(mx, 1, &bits[4], 2);
232 rc = get_subch_bits(mx, 0, &bits[6], 2);
Harald Welte1fa60c82009-02-09 18:13:26 +0000233
Harald Welte7eb1f622009-02-18 03:40:58 +0000234 *byte = compact_bits(bits);
Harald Welte1fa60c82009-02-09 18:13:26 +0000235
236 return rc;
237}
238
239/* Request the output of some muxed bytes from the subchan muxer */
240int subchan_mux_out(struct subch_mux *mx, u_int8_t *data, int len)
241{
242 int i;
243
244 for (i = 0; i < len; i++) {
245 int rc;
246 rc = mux_output_byte(mx, &data[i]);
247 if (rc < 0)
248 break;
249 }
250 return i;
251}
252
253/* enqueue some data into the tx_queue of a given subchannel */
254int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data,
255 int len)
256{
257 struct mux_subch *sch = &mx->subch[s_nr];
258 struct subch_txq_entry *tqe = malloc(sizeof(*tqe) + len);
259
260 if (!tqe)
261 return -ENOMEM;
262
263 memset(tqe, 0, sizeof(*tqe));
264 tqe->bit_len = len;
265 memcpy(tqe->bits, data, len);
266
Holger Freyther3630eab2009-02-09 21:05:56 +0000267 llist_add_tail(&tqe->list, &sch->tx_queue);
Harald Welte1fa60c82009-02-09 18:13:26 +0000268
269 return 0;
270}
271
272/* initialize one subchannel muxer instance */
273int subchan_mux_init(struct subch_mux *mx)
274{
275 int i;
276
277 memset(mx, 0, sizeof(*mx));
278 for (i = 0; i < NR_SUBCH; i++) {
279 struct mux_subch *sch = &mx->subch[i];
280 INIT_LLIST_HEAD(&sch->tx_queue);
281 }
282
283 return 0;
284}