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> |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 28 | #include "coding_scheme.h" |
Max | 1962136 | 2017-09-08 12:33:34 +0200 | [diff] [blame] | 29 | } |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 30 | |
| 31 | class GprsCodingScheme { |
| 32 | public: |
Aravind Sirsikar | 9149552 | 2016-07-12 14:17:12 +0530 | [diff] [blame] | 33 | |
Aravind Sirsikar | 9149552 | 2016-07-12 14:17:12 +0530 | [diff] [blame] | 34 | #define EGPRS_ARQ1 0x0 |
| 35 | #define EGPRS_ARQ2 0x1 |
| 36 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 37 | enum Mode { |
| 38 | GPRS, |
| 39 | EGPRS_GMSK, |
| 40 | EGPRS, |
| 41 | }; |
| 42 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 43 | GprsCodingScheme(CodingScheme s = UNKNOWN); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 44 | |
| 45 | operator bool() const {return m_scheme != UNKNOWN;} |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 46 | operator CodingScheme() const {return m_scheme;} |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 47 | uint8_t to_num() const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 48 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 49 | GprsCodingScheme& operator =(CodingScheme s); |
| 50 | bool operator == (CodingScheme s) const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 51 | GprsCodingScheme& operator =(GprsCodingScheme o); |
| 52 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 53 | bool isValid() const {return UNKNOWN <= m_scheme && m_scheme <= MCS9;} |
| 54 | bool isGprs() const {return CS1 <= m_scheme && m_scheme <= CS4;} |
| 55 | bool isEgprs() const {return m_scheme >= MCS1;} |
| 56 | bool isEgprsGmsk() const {return isEgprs() && m_scheme <= MCS4;} |
| 57 | bool isCompatible(Mode mode) const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 58 | bool isCompatible(GprsCodingScheme o) const; |
Jacob Erlbeck | 2305afd | 2016-02-03 15:25:04 +0100 | [diff] [blame] | 59 | bool isFamilyCompatible(GprsCodingScheme o) const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 60 | |
| 61 | void inc(Mode mode); |
| 62 | void dec(Mode mode); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 63 | void inc(); |
| 64 | void dec(); |
Jacob Erlbeck | 2305afd | 2016-02-03 15:25:04 +0100 | [diff] [blame] | 65 | void decToSingleBlock(bool *needStuffing); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 66 | |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 67 | uint8_t sizeUL() const; |
| 68 | uint8_t sizeDL() const; |
| 69 | uint8_t usedSizeUL() const; |
| 70 | uint8_t usedSizeDL() const; |
| 71 | uint8_t maxBytesUL() const; |
| 72 | uint8_t maxBytesDL() const; |
| 73 | uint8_t spareBitsUL() const; |
| 74 | uint8_t spareBitsDL() const; |
| 75 | uint8_t maxDataBlockBytes() const; |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 76 | uint8_t optionalPaddingBits() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 77 | const char *name() const; |
Max | 51754b6 | 2019-03-13 17:14:13 +0100 | [diff] [blame^] | 78 | enum HeaderType headerTypeData() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 79 | |
| 80 | static GprsCodingScheme getBySizeUL(unsigned size); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 81 | static GprsCodingScheme getGprsByNum(unsigned num); |
| 82 | static GprsCodingScheme getEgprsByNum(unsigned num); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 83 | |
Jacob Erlbeck | 7b57997 | 2016-01-05 15:54:24 +0100 | [diff] [blame] | 84 | static const char *modeName(Mode mode); |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 85 | static CodingScheme get_retx_mcs(const GprsCodingScheme mcs, |
Aravind Sirsikar | 50b0970 | 2016-08-22 17:21:10 +0530 | [diff] [blame] | 86 | const GprsCodingScheme retx_mcs, |
| 87 | const unsigned arq_type); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 88 | private: |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 89 | GprsCodingScheme(int s); /* fail on use */ |
| 90 | GprsCodingScheme& operator =(int s); /* fail on use */ |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 91 | enum CodingScheme m_scheme; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 94 | inline uint8_t GprsCodingScheme::to_num() const |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 95 | { |
| 96 | if (isGprs()) |
| 97 | return (m_scheme - CS1) + 1; |
| 98 | |
| 99 | if (isEgprs()) |
| 100 | return (m_scheme - MCS1) + 1; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 105 | inline bool GprsCodingScheme::isCompatible(Mode mode) const |
| 106 | { |
| 107 | switch (mode) { |
| 108 | case GPRS: return isGprs(); |
| 109 | case EGPRS_GMSK: return isEgprsGmsk(); |
| 110 | case EGPRS: return isEgprs(); |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 116 | inline bool GprsCodingScheme::isCompatible(GprsCodingScheme o) const |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 117 | { |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 118 | return (isGprs() && o.isGprs()) || (isEgprs() && o.isEgprs()); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 121 | inline GprsCodingScheme::GprsCodingScheme(CodingScheme s) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 122 | : m_scheme(s) |
| 123 | { |
| 124 | if (!isValid()) |
| 125 | m_scheme = UNKNOWN; |
| 126 | } |
| 127 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 128 | inline GprsCodingScheme& GprsCodingScheme::operator =(CodingScheme s) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 129 | { |
| 130 | m_scheme = s; |
| 131 | |
| 132 | if (!isValid()) |
| 133 | m_scheme = UNKNOWN; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 134 | |
| 135 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 138 | inline GprsCodingScheme& GprsCodingScheme::operator =(GprsCodingScheme o) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 139 | { |
| 140 | m_scheme = o.m_scheme; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 141 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 142 | } |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 143 | |
| 144 | inline GprsCodingScheme GprsCodingScheme::getGprsByNum(unsigned num) |
| 145 | { |
| 146 | if (num < 1 || num > 4) |
| 147 | return GprsCodingScheme(); |
| 148 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 149 | return GprsCodingScheme(CodingScheme(CS1 + (num - 1))); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | inline GprsCodingScheme GprsCodingScheme::getEgprsByNum(unsigned num) |
| 153 | { |
| 154 | if (num < 1 || num > 9) |
| 155 | return GprsCodingScheme(); |
| 156 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 157 | return GprsCodingScheme(CodingScheme(MCS1 + (num - 1))); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* The coding schemes form a partial ordering */ |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 161 | inline bool GprsCodingScheme::operator == (CodingScheme scheme) const |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 162 | { |
| 163 | return this->m_scheme == scheme; |
| 164 | } |
| 165 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 166 | inline bool operator !=(GprsCodingScheme a, GprsCodingScheme b) |
| 167 | { |
| 168 | return !(a == b); |
| 169 | } |
| 170 | |
| 171 | inline bool operator <(GprsCodingScheme a, GprsCodingScheme b) |
| 172 | { |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 173 | return a.isCompatible(b) && a.to_num() < b.to_num(); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 174 | } |