blob: 537e325780abd8aaa79bc25b351657eed405deab [file] [log] [blame]
Harald Welted55a2092022-11-29 22:33:54 +01001/*! \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
19#pragma once
20#include <stdint.h>
21#include <osmocom/core/bits.h>
22#include <osmocom/core/linuxlist.h>
23#include <osmocom/core/msgb.h>
24
Philipp Maier6b69b552023-02-22 12:40:18 +010025#define OSMO_I460_NUM_SUBCHAN 8
26
Harald Welted55a2092022-11-29 22:33:54 +010027/* I.460 sub-slot rate */
28enum osmo_i460_rate {
29 OSMO_I460_RATE_NONE, /* disabled */
30 OSMO_I460_RATE_64k,
31 OSMO_I460_RATE_32k,
32 OSMO_I460_RATE_16k,
33 OSMO_I460_RATE_8k,
34};
35
36struct osmo_i460_subchan;
37
38typedef void (*out_cb_bits_t)(struct osmo_i460_subchan *schan, void *user_data,
39 const ubit_t *bits, unsigned int num_bits);
40typedef void (*out_cb_bytes_t)(struct osmo_i460_subchan *schan, void *user_data,
41 const uint8_t *bytes, unsigned int num_bytes);
42
43struct osmo_i460_subchan_demux {
44 /*! bit-buffer for output bits */
45 uint8_t *out_bitbuf;
46 /*! size of out_bitbuf in bytes */
47 unsigned int out_bitbuf_size;
48 /*! offset of next bit to be written in out_bitbuf */
49 unsigned int out_idx;
50 /*! callback to be called once we have received out_bitbuf_size bits */
51 out_cb_bits_t out_cb_bits;
52 out_cb_bytes_t out_cb_bytes;
53 void *user_data;
54};
55
56typedef void (*in_cb_queue_empty_t)(struct osmo_i460_subchan *schan, void *user_data);
57
58struct osmo_i460_subchan_mux {
59 /*! list of to-be-transmitted message buffers */
60 struct llist_head tx_queue;
61 in_cb_queue_empty_t in_cb_queue_empty;
62 void *user_data;
63};
64
65struct osmo_i460_subchan {
66 struct osmo_i460_timeslot *ts; /* back-pointer */
67 enum osmo_i460_rate rate; /* 8/16/32/64k */
68 uint8_t bit_offset; /* bit offset inside each byte of the B-channel */
69 struct osmo_i460_subchan_demux demux;
70 struct osmo_i460_subchan_mux mux;
71};
72
73struct osmo_i460_timeslot {
Philipp Maier6b69b552023-02-22 12:40:18 +010074 struct osmo_i460_subchan schan[OSMO_I460_NUM_SUBCHAN];
Harald Welted55a2092022-11-29 22:33:54 +010075};
76
77/*! description of a sub-channel; passed by caller */
78struct osmo_i460_schan_desc {
79 enum osmo_i460_rate rate;
80 uint8_t bit_offset;
81 struct {
82 /* size (in bits) of the internal buffer; determines granularity */
83 size_t num_bits;
84 /*! call-back function called whenever we received num_bits */
85 out_cb_bits_t out_cb_bits;
86 /*! out_cb_bytes call-back function called whenever we received num_bits.
87 * The user is usually expected to provide either out_cb_bits or out_cb_bytes. If only
88 * out_cb_bits is provided, output data will always be provided as unpacked bits; if only
89 * out_cb_bytes is provided, output data will always be provided as packet bits (bytes). If
90 * both are provided, it is up to the I.460 multiplex to decide if it calls either of the two,
91 * depending on what can be provided without extra conversion. */
92 out_cb_bytes_t out_cb_bytes;
93 /* opaque user data pointer to pass to out_cb */
94 void *user_data;
95 } demux;
96
97 struct {
98 /* call-back function whenever the muxer requires more input data from the sub-channels,
99 * but has nothing enqueued yet. A typical function would then call osmo_i460_mux_enqueue() */
100 in_cb_queue_empty_t in_cb_queue_empty;
101 /* opaque user data pointer to pass to in_cb */
102 void *user_data;
103 } mux;
104};
105
106void osmo_i460_demux_in(struct osmo_i460_timeslot *ts, const uint8_t *data, size_t data_len);
107
108void osmo_i460_mux_enqueue(struct osmo_i460_subchan *schan, struct msgb *msg);
109int osmo_i460_mux_out(struct osmo_i460_timeslot *ts, uint8_t *out, size_t out_len);
110
111void osmo_i460_ts_init(struct osmo_i460_timeslot *ts);
112
113struct osmo_i460_subchan *
114osmo_i460_subchan_add(void *ctx, struct osmo_i460_timeslot *ts, const struct osmo_i460_schan_desc *chd);
115
116void osmo_i460_subchan_del(struct osmo_i460_subchan *schan);
117
Philipp Maier54e17822023-02-14 13:50:04 +0100118int osmo_i460_subchan_count(struct osmo_i460_timeslot *ts);
119
Harald Welted55a2092022-11-29 22:33:54 +0100120/*! @} */