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 | |
| 26 | |
| 27 | class GprsCodingScheme { |
| 28 | public: |
| 29 | enum Scheme { |
| 30 | UNKNOWN, |
| 31 | CS1, CS2, CS3, CS4, |
| 32 | MCS1, MCS2, MCS3, MCS4, |
| 33 | MCS5, MCS6, MCS7, MCS8, MCS9, |
| 34 | NUM_SCHEMES |
| 35 | }; |
| 36 | |
| 37 | enum Mode { |
| 38 | GPRS, |
| 39 | EGPRS_GMSK, |
| 40 | EGPRS, |
| 41 | }; |
| 42 | |
Jacob Erlbeck | 6c3dc61 | 2015-12-14 10:21:26 +0100 | [diff] [blame] | 43 | enum HeaderType { |
| 44 | HEADER_INVALID, |
| 45 | HEADER_GPRS_CONTROL, |
| 46 | HEADER_GPRS_DATA, |
| 47 | HEADER_EGPRS_DATA_TYPE_1, |
| 48 | HEADER_EGPRS_DATA_TYPE_2, |
| 49 | HEADER_EGPRS_DATA_TYPE_3, |
Jacob Erlbeck | 22f8087 | 2016-01-11 10:56:50 +0100 | [diff] [blame] | 50 | NUM_HEADER_TYPES |
Jacob Erlbeck | 6c3dc61 | 2015-12-14 10:21:26 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 53 | GprsCodingScheme(Scheme s = UNKNOWN); |
| 54 | |
| 55 | operator bool() const {return m_scheme != UNKNOWN;} |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 56 | operator Scheme() const {return m_scheme;} |
| 57 | unsigned int to_num() const; |
| 58 | |
| 59 | GprsCodingScheme& operator =(Scheme s); |
| 60 | GprsCodingScheme& operator =(GprsCodingScheme o); |
| 61 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 62 | bool isValid() const {return UNKNOWN <= m_scheme && m_scheme <= MCS9;} |
| 63 | bool isGprs() const {return CS1 <= m_scheme && m_scheme <= CS4;} |
| 64 | bool isEgprs() const {return m_scheme >= MCS1;} |
| 65 | bool isEgprsGmsk() const {return isEgprs() && m_scheme <= MCS4;} |
| 66 | bool isCompatible(Mode mode) const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 67 | bool isCompatible(GprsCodingScheme o) const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 68 | |
| 69 | void inc(Mode mode); |
| 70 | void dec(Mode mode); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 71 | void inc(); |
| 72 | void dec(); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 73 | |
| 74 | unsigned int sizeUL() const; |
| 75 | unsigned int sizeDL() const; |
Jacob Erlbeck | fc1b3e6 | 2016-01-11 09:58:11 +0100 | [diff] [blame] | 76 | unsigned int usedSizeUL() const; |
| 77 | unsigned int usedSizeDL() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 78 | unsigned int maxBytesUL() const; |
| 79 | unsigned int maxBytesDL() const; |
| 80 | unsigned int spareBitsUL() const; |
| 81 | unsigned int spareBitsDL() const; |
Jacob Erlbeck | 392a545 | 2015-12-14 10:38:29 +0100 | [diff] [blame] | 82 | unsigned int maxDataBlockBytes() const; |
| 83 | unsigned int numDataBlocks() const; |
Jacob Erlbeck | 22f8087 | 2016-01-11 10:56:50 +0100 | [diff] [blame] | 84 | unsigned int numDataHeaderBitsUL() const; |
| 85 | unsigned int numDataHeaderBitsDL() const; |
| 86 | unsigned int numDataBlockHeaderBits() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 87 | const char *name() const; |
Jacob Erlbeck | 6c3dc61 | 2015-12-14 10:21:26 +0100 | [diff] [blame] | 88 | HeaderType headerTypeData() const; |
| 89 | HeaderType headerTypeControl() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 90 | |
| 91 | static GprsCodingScheme getBySizeUL(unsigned size); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 92 | static GprsCodingScheme getGprsByNum(unsigned num); |
| 93 | static GprsCodingScheme getEgprsByNum(unsigned num); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 94 | |
Jacob Erlbeck | 7b57997 | 2016-01-05 15:54:24 +0100 | [diff] [blame] | 95 | static const char *modeName(Mode mode); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 96 | private: |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 97 | GprsCodingScheme(int s); /* fail on use */ |
| 98 | GprsCodingScheme& operator =(int s); /* fail on use */ |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 99 | enum Scheme m_scheme; |
| 100 | }; |
| 101 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 102 | inline unsigned int GprsCodingScheme::to_num() const |
| 103 | { |
| 104 | if (isGprs()) |
| 105 | return (m_scheme - CS1) + 1; |
| 106 | |
| 107 | if (isEgprs()) |
| 108 | return (m_scheme - MCS1) + 1; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 113 | inline bool GprsCodingScheme::isCompatible(Mode mode) const |
| 114 | { |
| 115 | switch (mode) { |
| 116 | case GPRS: return isGprs(); |
| 117 | case EGPRS_GMSK: return isEgprsGmsk(); |
| 118 | case EGPRS: return isEgprs(); |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 124 | inline bool GprsCodingScheme::isCompatible(GprsCodingScheme o) const |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 125 | { |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 126 | return (isGprs() && o.isGprs()) || (isEgprs() && o.isEgprs()); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Jacob Erlbeck | 6c3dc61 | 2015-12-14 10:21:26 +0100 | [diff] [blame] | 129 | inline GprsCodingScheme::HeaderType GprsCodingScheme::headerTypeControl() const |
| 130 | { |
| 131 | return HEADER_GPRS_CONTROL; |
| 132 | } |
| 133 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 134 | inline GprsCodingScheme::GprsCodingScheme(Scheme s) |
| 135 | : m_scheme(s) |
| 136 | { |
| 137 | if (!isValid()) |
| 138 | m_scheme = UNKNOWN; |
| 139 | } |
| 140 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 141 | inline GprsCodingScheme& GprsCodingScheme::operator =(Scheme s) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 142 | { |
| 143 | m_scheme = s; |
| 144 | |
| 145 | if (!isValid()) |
| 146 | m_scheme = UNKNOWN; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 147 | |
| 148 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 151 | inline GprsCodingScheme& GprsCodingScheme::operator =(GprsCodingScheme o) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 152 | { |
| 153 | m_scheme = o.m_scheme; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 154 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 155 | } |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 156 | |
| 157 | inline GprsCodingScheme GprsCodingScheme::getGprsByNum(unsigned num) |
| 158 | { |
| 159 | if (num < 1 || num > 4) |
| 160 | return GprsCodingScheme(); |
| 161 | |
| 162 | return GprsCodingScheme(Scheme(CS1 + (num - 1))); |
| 163 | } |
| 164 | |
| 165 | inline GprsCodingScheme GprsCodingScheme::getEgprsByNum(unsigned num) |
| 166 | { |
| 167 | if (num < 1 || num > 9) |
| 168 | return GprsCodingScheme(); |
| 169 | |
| 170 | return GprsCodingScheme(Scheme(MCS1 + (num - 1))); |
| 171 | } |
| 172 | |
| 173 | /* The coding schemes form a partial ordering */ |
| 174 | inline bool operator ==(GprsCodingScheme a, GprsCodingScheme b) |
| 175 | { |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 176 | return GprsCodingScheme::Scheme(a) == GprsCodingScheme::Scheme(b); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | inline bool operator !=(GprsCodingScheme a, GprsCodingScheme b) |
| 180 | { |
| 181 | return !(a == b); |
| 182 | } |
| 183 | |
| 184 | inline bool operator <(GprsCodingScheme a, GprsCodingScheme b) |
| 185 | { |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 186 | return a.isCompatible(b) && |
| 187 | GprsCodingScheme::Scheme(a) < GprsCodingScheme::Scheme(b); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | inline bool operator >(GprsCodingScheme a, GprsCodingScheme b) |
| 191 | { |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 192 | return b < a; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | inline bool operator <=(GprsCodingScheme a, GprsCodingScheme b) |
| 196 | { |
| 197 | return a == b || a < b; |
| 198 | } |
| 199 | |
| 200 | inline bool operator >=(GprsCodingScheme a, GprsCodingScheme b) |
| 201 | { |
| 202 | return a == b || a > b; |
| 203 | } |
| 204 | |