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