Harald Welte | 254b924 | 2020-05-11 08:38:51 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | /* TRAU frame handling according to GSM TS 48.060 / 48.061 */ |
| 3 | |
| 4 | /* (C) 2020 by Harald Welte <laforge@gnumonks.org> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <osmocom/core/bits.h> |
| 25 | #include <osmocom/core/utils.h> |
| 26 | |
| 27 | /*! \defgroup trau_frame TRAU frame handling |
| 28 | * @{ |
| 29 | * |
| 30 | * \file trau_frame.h |
| 31 | */ |
| 32 | |
| 33 | /*! \brief Maximum number of C-bits in a TRAU frame: |
| 34 | * 21 for FR/EFR, 25 for AMR, 15 for OM, 15 for data, 13 for E-data, 21 idle */ |
| 35 | #define MAX_C_BITS 25 |
| 36 | /*! \brief Maximum number of D-bits in a TRAU frame: |
| 37 | * 260 for FR/EFR, 256 for AMR, 264 for OM, 288 for E-data */ |
| 38 | #define MAX_D_BITS 288 |
| 39 | /*! \brief Maximum number of T-bits in a TRAU frame for all speech frames */ |
| 40 | #define MAX_T_BITS 4 |
| 41 | /*! \brief Maximum number of S-bits in a TRAU frame for OM */ |
| 42 | #define MAX_S_BITS 6 |
| 43 | /*! \brief Maximum number of M-bits in a TRAU frame for E-data */ |
| 44 | #define MAX_M_BITS 2 |
| 45 | /*! \brief Maximum number of XC-bits in a TRAU frame for 8k */ |
| 46 | #define MAX_XC_BITS 6 |
| 47 | |
| 48 | /*! 16k sub-slot types: Bits C1..C5 */ |
| 49 | #define TRAU_FT_FR_UP 0x02 /* 0 0 0 1 0 - 3.5.1.1.1 */ |
| 50 | #define TRAU_FT_HR_UP 0x03 /* 0 0 0 1 1 - TS 08.61 5.1.4.1.1 */ |
| 51 | #define TRAU_FT_FR_DOWN 0x1c /* 1 1 1 0 0 - 3.5.1.1.1 */ |
| 52 | #define TRAU_FT_HR_DOWN 0x1d /* 1 1 1 0 1 - TS 08.61 5.1.4.1.1 */ |
| 53 | #define TRAU_FT_EFR 0x1a /* 1 1 0 1 0 - 3.5.1.1.1 */ |
| 54 | #define TRAU_FT_AMR 0x06 /* 0 0 1 1 0 - 3.5.1.2 */ |
| 55 | #define TRAU_FT_OM_UP 0x05 /* 0 0 1 0 1 - 3.5.2 */ |
| 56 | #define TRAU_FT_OM_DOWN 0x1b /* 1 1 0 1 1 - 3.5.2 */ |
| 57 | #define TRAU_FT_DATA_UP 0x08 /* 0 1 0 0 0 - 3.5.3 */ |
| 58 | #define TRAU_FT_DATA_UP_HR 0x09 /* 0 1 0 0 1 - TS 08.61 5.1.4.2 */ |
| 59 | #define TRAU_FT_DATA_DOWN 0x16 /* 1 0 1 1 0 - 3.5.3 */ |
| 60 | #define TRAU_FT_DATA_DOWN_HR 0x17 /* 1 0 1 1 1 - TS 08.61 5.1.4.2 */ |
| 61 | #define TRAU_FT_D145_SYNC 0x14 /* 1 0 1 0 0 - 3.5.3 */ |
| 62 | #define TRAU_FT_EDATA 0x1f /* 1 1 1 1 1 - 3.5.4 */ |
| 63 | #define TRAU_FT_IDLE_UP 0x10 /* 1 0 0 0 0 - 3.5.5 */ |
| 64 | #define TRAU_FT_IDLE_DOWN 0x0e /* 0 1 1 1 0 - 3.5.5 */ |
| 65 | |
| 66 | /* 8k sub-slot types: Bits C1..C5*/ |
| 67 | #define TRAU8_FT_SPEECH_UP 0x02 /* 0 0 0 1 P - TS 08.61 5.2.4.1.1 */ |
| 68 | #define TRAU8_FT_DATA_UP 0x06 /* 0 0 1 1 P - TS 08.61 5.2.4.1.1 */ |
| 69 | #define TRAU8_FT_OM_UP 0x0a /* 0 1 0 1 P - TS 08.61 5.2.4.1.1 */ |
| 70 | #define TRAU8_FT_SPEECH_DOWN 0x00 /* 0 0 0 U P - TS 08.61 5.2.4.1.2 */ |
| 71 | #define TRAU8_FT_DATA_DOWN 0x04 /* 0 0 1 U P - TS 08.61 5.2.4.1.2 */ |
| 72 | #define TRAU8_FT_OM_DOWN 0x08 /* 0 1 0 U P - TS 08.61 5.2.4.1.2 */ |
| 73 | |
| 74 | /********************************************************************************* |
| 75 | * New API |
| 76 | *********************************************************************************/ |
| 77 | |
| 78 | enum osmo_trau_frame_type { |
| 79 | OSMO_TRAU_FT_NONE, |
| 80 | OSMO_TRAU16_FT_FR = 1, |
| 81 | OSMO_TRAU16_FT_HR, |
| 82 | OSMO_TRAU16_FT_EFR, |
| 83 | OSMO_TRAU16_FT_AMR, |
| 84 | OSMO_TRAU16_FT_OAM, |
| 85 | OSMO_TRAU16_FT_DATA, |
| 86 | OSMO_TRAU16_FT_EDATA, |
| 87 | OSMO_TRAU16_FT_D145_SYNC, |
| 88 | OSMO_TRAU16_FT_DATA_HR, |
| 89 | OSMO_TRAU16_FT_IDLE, |
| 90 | |
| 91 | OSMO_TRAU8_SPEECH, |
| 92 | OSMO_TRAU8_DATA, |
| 93 | OSMO_TRAU8_OAM, |
| 94 | OSMO_TRAU8_AMR_LOW, |
| 95 | OSMO_TRAU8_AMR_6k7, |
| 96 | OSMO_TRAU8_AMR_7k4, |
| 97 | }; |
| 98 | |
| 99 | extern const struct value_string osmo_trau_frame_type_names[]; |
| 100 | static inline const char *osmo_trau_frame_type_name(enum osmo_trau_frame_type ft) { |
| 101 | return get_value_string(osmo_trau_frame_type_names, ft); |
| 102 | } |
| 103 | |
| 104 | enum osmo_trau_frame_direction { |
| 105 | OSMO_TRAU_DIR_UL = 0, |
| 106 | OSMO_TRAU_DIR_DL = 1, |
| 107 | }; |
| 108 | |
| 109 | struct osmo_trau_frame { |
| 110 | enum osmo_trau_frame_type type; |
| 111 | enum osmo_trau_frame_direction dir; |
| 112 | |
Harald Welte | dd9ee98 | 2020-08-02 15:53:46 +0200 | [diff] [blame] | 113 | /* timing alignment: 0 = no change; negative: less bits; positive: more bits */ |
| 114 | int dl_ta_usec; |
| 115 | |
Harald Welte | 254b924 | 2020-05-11 08:38:51 +0200 | [diff] [blame] | 116 | ubit_t c_bits[MAX_C_BITS]; |
| 117 | ubit_t d_bits[MAX_D_BITS]; |
| 118 | ubit_t t_bits[MAX_T_BITS]; |
| 119 | ubit_t s_bits[MAX_S_BITS]; |
| 120 | ubit_t m_bits[MAX_M_BITS]; |
| 121 | ubit_t ufi; /*!< Unreliable Frame Indication (HR) */ |
| 122 | ubit_t crc_bits[3]; /*!< CRC0..CRC2 (HR) */ |
| 123 | ubit_t xc_bits[MAX_XC_BITS]; /*!< XC1..XC6 (8k) */ |
| 124 | }; |
| 125 | |
| 126 | /*! decode an 8k TRAU frame |
| 127 | * \param[out] fr caller-allocated output data structure |
| 128 | * \param[in] bits unpacked input bits |
| 129 | * \param[in] dir direction |
| 130 | * \return 0 on success; negative in case of error */ |
| 131 | int osmo_trau_frame_decode_8k(struct osmo_trau_frame *fr, const ubit_t *bits, |
| 132 | enum osmo_trau_frame_direction dir); |
| 133 | |
| 134 | /*! decode an 16k TRAU frame |
| 135 | * \param[out] fr caller-allocated output data structure |
| 136 | * \param[in] bits unpacked input bits |
| 137 | * \param[in] dir direction |
| 138 | * \return 0 on success; negative in case of error */ |
| 139 | int osmo_trau_frame_decode_16k(struct osmo_trau_frame *fr, const ubit_t *bits, |
| 140 | enum osmo_trau_frame_direction dir); |
| 141 | |
| 142 | /*! encode a TRAU frame |
| 143 | * \param[out] bits caller-allocated memory for unpacked output bits |
| 144 | * \param[out] fr input data structure describing TRAU frame |
| 145 | * \return number of bits encoded */ |
| 146 | int osmo_trau_frame_encode(ubit_t *bits, size_t n_bits, const struct osmo_trau_frame *fr); |
| 147 | |
Harald Welte | dd9ee98 | 2020-08-02 15:53:46 +0200 | [diff] [blame] | 148 | /*! Determine the time alignment in us requested by CCU in a UL frame */ |
| 149 | int osmo_trau_frame_dl_ta_us(const struct osmo_trau_frame *fr); |
Harald Welte | 254b924 | 2020-05-11 08:38:51 +0200 | [diff] [blame] | 150 | |
| 151 | /* }@ */ |