Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 1 | /* gprs_coding_scheme.h |
| 2 | * |
| 3 | * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 4 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <stddef.h> |
| 25 | |
Max | 1962136 | 2017-09-08 12:33:34 +0200 | [diff] [blame] | 26 | extern "C" { |
| 27 | #include <osmocom/core/utils.h> |
| 28 | } |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 29 | |
| 30 | class GprsCodingScheme { |
| 31 | public: |
Aravind Sirsikar | 9149552 | 2016-07-12 14:17:12 +0530 | [diff] [blame] | 32 | |
| 33 | #define MAX_NUM_ARQ 2 /* max. number of ARQ */ |
| 34 | #define MAX_NUM_MCS 9 /* max. number of MCS */ |
| 35 | #define EGPRS_ARQ1 0x0 |
| 36 | #define EGPRS_ARQ2 0x1 |
| 37 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 38 | enum Scheme { |
| 39 | UNKNOWN, |
| 40 | CS1, CS2, CS3, CS4, |
| 41 | MCS1, MCS2, MCS3, MCS4, |
| 42 | MCS5, MCS6, MCS7, MCS8, MCS9, |
| 43 | NUM_SCHEMES |
| 44 | }; |
| 45 | |
| 46 | enum Mode { |
| 47 | GPRS, |
| 48 | EGPRS_GMSK, |
| 49 | EGPRS, |
| 50 | }; |
| 51 | |
Jacob Erlbeck | 6c3dc61 | 2015-12-14 10:21:26 +0100 | [diff] [blame] | 52 | enum HeaderType { |
| 53 | HEADER_INVALID, |
| 54 | HEADER_GPRS_CONTROL, |
| 55 | HEADER_GPRS_DATA, |
| 56 | HEADER_EGPRS_DATA_TYPE_1, |
| 57 | HEADER_EGPRS_DATA_TYPE_2, |
| 58 | HEADER_EGPRS_DATA_TYPE_3, |
Jacob Erlbeck | 22f8087 | 2016-01-11 10:56:50 +0100 | [diff] [blame] | 59 | NUM_HEADER_TYPES |
Jacob Erlbeck | 6c3dc61 | 2015-12-14 10:21:26 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 62 | GprsCodingScheme(Scheme s = UNKNOWN); |
| 63 | |
| 64 | operator bool() const {return m_scheme != UNKNOWN;} |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 65 | operator Scheme() const {return m_scheme;} |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 66 | uint8_t to_num() const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 67 | |
| 68 | GprsCodingScheme& operator =(Scheme s); |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 69 | bool operator == (Scheme s) const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 70 | GprsCodingScheme& operator =(GprsCodingScheme o); |
| 71 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 72 | bool isValid() const {return UNKNOWN <= m_scheme && m_scheme <= MCS9;} |
| 73 | bool isGprs() const {return CS1 <= m_scheme && m_scheme <= CS4;} |
| 74 | bool isEgprs() const {return m_scheme >= MCS1;} |
| 75 | bool isEgprsGmsk() const {return isEgprs() && m_scheme <= MCS4;} |
| 76 | bool isCompatible(Mode mode) const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 77 | bool isCompatible(GprsCodingScheme o) const; |
Jacob Erlbeck | 2305afd | 2016-02-03 15:25:04 +0100 | [diff] [blame] | 78 | bool isFamilyCompatible(GprsCodingScheme o) const; |
| 79 | bool isCombinable(GprsCodingScheme o) const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 80 | |
| 81 | void inc(Mode mode); |
| 82 | void dec(Mode mode); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 83 | void inc(); |
| 84 | void dec(); |
Jacob Erlbeck | 2305afd | 2016-02-03 15:25:04 +0100 | [diff] [blame] | 85 | void decToSingleBlock(bool *needStuffing); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 86 | |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 87 | uint8_t sizeUL() const; |
| 88 | uint8_t sizeDL() const; |
| 89 | uint8_t usedSizeUL() const; |
| 90 | uint8_t usedSizeDL() const; |
| 91 | uint8_t maxBytesUL() const; |
| 92 | uint8_t maxBytesDL() const; |
| 93 | uint8_t spareBitsUL() const; |
| 94 | uint8_t spareBitsDL() const; |
| 95 | uint8_t maxDataBlockBytes() const; |
| 96 | uint8_t numDataBlocks() const; |
| 97 | uint8_t numDataHeaderBitsUL() const; |
| 98 | uint8_t numDataHeaderBitsDL() const; |
| 99 | uint8_t numDataBlockHeaderBits() const; |
| 100 | uint8_t optionalPaddingBits() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 101 | const char *name() const; |
Jacob Erlbeck | 6c3dc61 | 2015-12-14 10:21:26 +0100 | [diff] [blame] | 102 | HeaderType headerTypeData() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 103 | |
| 104 | static GprsCodingScheme getBySizeUL(unsigned size); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 105 | static GprsCodingScheme getGprsByNum(unsigned num); |
| 106 | static GprsCodingScheme getEgprsByNum(unsigned num); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 107 | |
Jacob Erlbeck | 7b57997 | 2016-01-05 15:54:24 +0100 | [diff] [blame] | 108 | static const char *modeName(Mode mode); |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 109 | static Scheme get_retx_mcs(const GprsCodingScheme mcs, |
Aravind Sirsikar | 50b0970 | 2016-08-22 17:21:10 +0530 | [diff] [blame] | 110 | const GprsCodingScheme retx_mcs, |
| 111 | const unsigned arq_type); |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 112 | |
Aravind Sirsikar | 9149552 | 2016-07-12 14:17:12 +0530 | [diff] [blame] | 113 | static enum Scheme egprs_mcs_retx_tbl[MAX_NUM_ARQ] |
| 114 | [MAX_NUM_MCS][MAX_NUM_MCS]; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 115 | private: |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 116 | GprsCodingScheme(int s); /* fail on use */ |
| 117 | GprsCodingScheme& operator =(int s); /* fail on use */ |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 118 | enum Scheme m_scheme; |
| 119 | }; |
| 120 | |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 121 | inline uint8_t GprsCodingScheme::to_num() const |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 122 | { |
| 123 | if (isGprs()) |
| 124 | return (m_scheme - CS1) + 1; |
| 125 | |
| 126 | if (isEgprs()) |
| 127 | return (m_scheme - MCS1) + 1; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 132 | inline bool GprsCodingScheme::isCompatible(Mode mode) const |
| 133 | { |
| 134 | switch (mode) { |
| 135 | case GPRS: return isGprs(); |
| 136 | case EGPRS_GMSK: return isEgprsGmsk(); |
| 137 | case EGPRS: return isEgprs(); |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 143 | inline bool GprsCodingScheme::isCompatible(GprsCodingScheme o) const |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 144 | { |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 145 | return (isGprs() && o.isGprs()) || (isEgprs() && o.isEgprs()); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 148 | inline GprsCodingScheme::GprsCodingScheme(Scheme s) |
| 149 | : m_scheme(s) |
| 150 | { |
| 151 | if (!isValid()) |
| 152 | m_scheme = UNKNOWN; |
| 153 | } |
| 154 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 155 | inline GprsCodingScheme& GprsCodingScheme::operator =(Scheme s) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 156 | { |
| 157 | m_scheme = s; |
| 158 | |
| 159 | if (!isValid()) |
| 160 | m_scheme = UNKNOWN; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 161 | |
| 162 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 165 | inline GprsCodingScheme& GprsCodingScheme::operator =(GprsCodingScheme o) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 166 | { |
| 167 | m_scheme = o.m_scheme; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 168 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 169 | } |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 170 | |
| 171 | inline GprsCodingScheme GprsCodingScheme::getGprsByNum(unsigned num) |
| 172 | { |
| 173 | if (num < 1 || num > 4) |
| 174 | return GprsCodingScheme(); |
| 175 | |
| 176 | return GprsCodingScheme(Scheme(CS1 + (num - 1))); |
| 177 | } |
| 178 | |
| 179 | inline GprsCodingScheme GprsCodingScheme::getEgprsByNum(unsigned num) |
| 180 | { |
| 181 | if (num < 1 || num > 9) |
| 182 | return GprsCodingScheme(); |
| 183 | |
| 184 | return GprsCodingScheme(Scheme(MCS1 + (num - 1))); |
| 185 | } |
| 186 | |
| 187 | /* The coding schemes form a partial ordering */ |
| 188 | inline bool operator ==(GprsCodingScheme a, GprsCodingScheme b) |
| 189 | { |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 190 | return GprsCodingScheme::Scheme(a) == GprsCodingScheme::Scheme(b); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 193 | inline bool GprsCodingScheme::operator == (Scheme scheme) const |
| 194 | { |
| 195 | return this->m_scheme == scheme; |
| 196 | } |
| 197 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 198 | inline bool operator !=(GprsCodingScheme a, GprsCodingScheme b) |
| 199 | { |
| 200 | return !(a == b); |
| 201 | } |
| 202 | |
| 203 | inline bool operator <(GprsCodingScheme a, GprsCodingScheme b) |
| 204 | { |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 205 | return a.isCompatible(b) && |
| 206 | GprsCodingScheme::Scheme(a) < GprsCodingScheme::Scheme(b); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 209 | inline GprsCodingScheme::Scheme GprsCodingScheme::get_retx_mcs( |
| 210 | const GprsCodingScheme mcs, |
Aravind Sirsikar | 50b0970 | 2016-08-22 17:21:10 +0530 | [diff] [blame] | 211 | const GprsCodingScheme demanded_mcs, |
| 212 | const unsigned arq_type) |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 213 | { |
Max | 1962136 | 2017-09-08 12:33:34 +0200 | [diff] [blame] | 214 | OSMO_ASSERT(mcs.to_num() > 0); |
| 215 | OSMO_ASSERT(demanded_mcs.to_num() > 0); |
| 216 | |
Aravind Sirsikar | 50b0970 | 2016-08-22 17:21:10 +0530 | [diff] [blame] | 217 | return egprs_mcs_retx_tbl[arq_type][mcs.to_num() - 1] |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 218 | [demanded_mcs.to_num() - 1]; |
| 219 | } |