blob: 2e33b37e2bde566a111420e9737bf9b193032d62 [file] [log] [blame]
Harald Welteb795f032020-05-14 11:42:53 +02001/*! \file i460_mux.h
2 * ITU-T I.460 sub-channel multiplexer + demultiplexer */
3/*
4 * (C) 2020 by Harald Welte <laforge@gnumonks.org>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23
24#pragma once
25#include <stdint.h>
26#include <osmocom/core/bits.h>
27#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/msgb.h>
29
30/* I.460 sub-slot rate */
31enum osmo_i460_rate {
32 OSMO_I460_RATE_NONE, /* disabled */
33 OSMO_I460_RATE_64k,
34 OSMO_I460_RATE_32k,
35 OSMO_I460_RATE_16k,
36 OSMO_I460_RATE_8k,
37};
38
39typedef void (*out_cb_bits_t)(void *user_data, const ubit_t *bits, unsigned int num_bits);
40typedef void (*out_cb_bytes_t)(void *user_data, const uint8_t *bytes, unsigned int num_bytes);
41
42struct osmo_i460_subchan_demux {
43 /*! bit-buffer for output bits */
44 uint8_t *out_bitbuf;
45 /*! size of out_bitbuf in bytes */
46 unsigned int out_bitbuf_size;
47 /*! offset of next bit to be written in out_bitbuf */
48 unsigned int out_idx;
49 /*! callback to be called once we have received out_bitbuf_size bits */
50 out_cb_bits_t out_cb_bits;
51 out_cb_bytes_t out_cb_bytes;
52 void *user_data;
53};
54
55struct osmo_i460_subchan_mux {
56 /*! list of to-be-transmitted message buffers */
57 struct llist_head tx_queue;
58};
59
60struct osmo_i460_subchan {
61 enum osmo_i460_rate rate; /* 8/16/32/64k */
62 uint8_t bit_offset; /* bit offset inside each byte of the B-channel */
63 struct osmo_i460_subchan_demux demux;
64 struct osmo_i460_subchan_mux mux;
65};
66
67struct osmo_i460_timeslot {
68 struct osmo_i460_subchan schan[8];
69};
70
71/*! description of a sub-channel; passed by caller */
72struct osmo_i460_schan_desc {
73 enum osmo_i460_rate rate;
74 uint8_t bit_offset;
75 struct {
76 /* size (in bits) of the internal buffer; determines granularity */
77 size_t num_bits;
78 /*! call-back function called whenever we received num_bits */
79 out_cb_bits_t out_cb_bits;
80 /*! out_cb_bytes call-back function called whenever we received num_bits.
81 * The user is usually expected to provide either out_cb_bits or out_cb_bytes. If only
82 * out_cb_bits is provided, output data will always be provided as unpacked bits; if only
83 * out_cb_bytes is provided, output data will always be provided as packet bits (bytes). If
84 * both are provided, it is up to the I.460 multiplex to decide if it calls either of the two,
85 * depending on what can be provided without extra conversion. */
86 out_cb_bytes_t out_cb_bytes;
87 /* opaque user data pointer to pass to out_cb */
88 void *user_data;
89 } demux;
90};
91
92void osmo_i460_demux_in(struct osmo_i460_timeslot *ts, const uint8_t *data, size_t data_len);
93
94void osmo_i460_mux_enqueue(struct osmo_i460_subchan *schan, struct msgb *msg);
95int osmo_i460_mux_out(struct osmo_i460_timeslot *ts, uint8_t *out, size_t out_len);
96
97void osmo_i460_ts_init(struct osmo_i460_timeslot *ts);
98
99struct osmo_i460_subchan *
100osmo_i460_subchan_add(void *ctx, struct osmo_i460_timeslot *ts, const struct osmo_i460_schan_desc *chd);
101
102void osmo_i460_subchan_del(struct osmo_i460_subchan *schan);
103
104/*! @} */