blob: 770b1e15bd66d74f07637f8b45d22696c38a5189 [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.
Harald Welteb795f032020-05-14 11:42:53 +020017 */
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
25/* I.460 sub-slot rate */
26enum osmo_i460_rate {
27 OSMO_I460_RATE_NONE, /* disabled */
28 OSMO_I460_RATE_64k,
29 OSMO_I460_RATE_32k,
30 OSMO_I460_RATE_16k,
31 OSMO_I460_RATE_8k,
32};
33
Harald Welteb3b474d2020-08-02 11:54:56 +020034struct osmo_i460_subchan;
35
36typedef void (*out_cb_bits_t)(struct osmo_i460_subchan *schan, void *user_data,
37 const ubit_t *bits, unsigned int num_bits);
38typedef void (*out_cb_bytes_t)(struct osmo_i460_subchan *schan, void *user_data,
39 const uint8_t *bytes, unsigned int num_bytes);
Harald Welteb795f032020-05-14 11:42:53 +020040
41struct osmo_i460_subchan_demux {
42 /*! bit-buffer for output bits */
43 uint8_t *out_bitbuf;
44 /*! size of out_bitbuf in bytes */
45 unsigned int out_bitbuf_size;
46 /*! offset of next bit to be written in out_bitbuf */
47 unsigned int out_idx;
48 /*! callback to be called once we have received out_bitbuf_size bits */
49 out_cb_bits_t out_cb_bits;
50 out_cb_bytes_t out_cb_bytes;
51 void *user_data;
52};
53
Harald Welteb3b474d2020-08-02 11:54:56 +020054typedef void (*in_cb_queue_empty_t)(struct osmo_i460_subchan *schan, void *user_data);
Philipp Maierb5518a82020-07-31 19:04:00 +020055
Harald Welteb795f032020-05-14 11:42:53 +020056struct osmo_i460_subchan_mux {
57 /*! list of to-be-transmitted message buffers */
58 struct llist_head tx_queue;
Philipp Maierb5518a82020-07-31 19:04:00 +020059 in_cb_queue_empty_t in_cb_queue_empty;
60 void *user_data;
Harald Welteb795f032020-05-14 11:42:53 +020061};
62
63struct osmo_i460_subchan {
Harald Welteeb8240d2020-08-02 11:54:15 +020064 struct osmo_i460_timeslot *ts; /* back-pointer */
Harald Welteb795f032020-05-14 11:42:53 +020065 enum osmo_i460_rate rate; /* 8/16/32/64k */
66 uint8_t bit_offset; /* bit offset inside each byte of the B-channel */
67 struct osmo_i460_subchan_demux demux;
68 struct osmo_i460_subchan_mux mux;
69};
70
71struct osmo_i460_timeslot {
72 struct osmo_i460_subchan schan[8];
73};
74
75/*! description of a sub-channel; passed by caller */
76struct osmo_i460_schan_desc {
77 enum osmo_i460_rate rate;
78 uint8_t bit_offset;
79 struct {
80 /* size (in bits) of the internal buffer; determines granularity */
81 size_t num_bits;
82 /*! call-back function called whenever we received num_bits */
83 out_cb_bits_t out_cb_bits;
84 /*! out_cb_bytes call-back function called whenever we received num_bits.
85 * The user is usually expected to provide either out_cb_bits or out_cb_bytes. If only
86 * out_cb_bits is provided, output data will always be provided as unpacked bits; if only
87 * out_cb_bytes is provided, output data will always be provided as packet bits (bytes). If
88 * both are provided, it is up to the I.460 multiplex to decide if it calls either of the two,
89 * depending on what can be provided without extra conversion. */
90 out_cb_bytes_t out_cb_bytes;
91 /* opaque user data pointer to pass to out_cb */
92 void *user_data;
93 } demux;
Philipp Maierb5518a82020-07-31 19:04:00 +020094
95 struct {
96 /* call-back function whenever the muxer requires more input data from the sub-channels,
97 * but has nothing enqueued yet. A typical function would then call osmo_i460_mux_enqueue() */
98 in_cb_queue_empty_t in_cb_queue_empty;
99 /* opaque user data pointer to pass to in_cb */
100 void *user_data;
101 } mux;
Harald Welteb795f032020-05-14 11:42:53 +0200102};
103
104void osmo_i460_demux_in(struct osmo_i460_timeslot *ts, const uint8_t *data, size_t data_len);
105
106void osmo_i460_mux_enqueue(struct osmo_i460_subchan *schan, struct msgb *msg);
107int osmo_i460_mux_out(struct osmo_i460_timeslot *ts, uint8_t *out, size_t out_len);
108
109void osmo_i460_ts_init(struct osmo_i460_timeslot *ts);
110
111struct osmo_i460_subchan *
112osmo_i460_subchan_add(void *ctx, struct osmo_i460_timeslot *ts, const struct osmo_i460_schan_desc *chd);
113
114void osmo_i460_subchan_del(struct osmo_i460_subchan *schan);
115
116/*! @} */