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 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 37 | GprsCodingScheme(CodingScheme s = UNKNOWN); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 38 | |
| 39 | operator bool() const {return m_scheme != UNKNOWN;} |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 40 | operator CodingScheme() const {return m_scheme;} |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 41 | uint8_t to_num() const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 42 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 43 | GprsCodingScheme& operator =(CodingScheme s); |
| 44 | bool operator == (CodingScheme s) const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 45 | GprsCodingScheme& operator =(GprsCodingScheme o); |
| 46 | |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 47 | bool isValid() const {return UNKNOWN <= m_scheme && m_scheme <= MCS9;} |
Max | 8a8e0fb | 2019-03-25 16:32:50 +0100 | [diff] [blame] | 48 | |
Max | a4de02d | 2019-03-13 16:35:09 +0100 | [diff] [blame] | 49 | bool isCompatible(enum mcs_kind mode) const; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 50 | bool isCompatible(GprsCodingScheme o) const; |
Jacob Erlbeck | 2305afd | 2016-02-03 15:25:04 +0100 | [diff] [blame] | 51 | bool isFamilyCompatible(GprsCodingScheme o) const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 52 | |
Max | a4de02d | 2019-03-13 16:35:09 +0100 | [diff] [blame] | 53 | void inc(enum mcs_kind mode); |
| 54 | void dec(enum mcs_kind mode); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 55 | void inc(); |
| 56 | void dec(); |
Jacob Erlbeck | 2305afd | 2016-02-03 15:25:04 +0100 | [diff] [blame] | 57 | void decToSingleBlock(bool *needStuffing); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 58 | |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 59 | uint8_t sizeUL() const; |
| 60 | uint8_t sizeDL() const; |
| 61 | uint8_t usedSizeUL() const; |
| 62 | uint8_t usedSizeDL() const; |
| 63 | uint8_t maxBytesUL() const; |
| 64 | uint8_t maxBytesDL() const; |
| 65 | uint8_t spareBitsUL() const; |
| 66 | uint8_t spareBitsDL() const; |
| 67 | uint8_t maxDataBlockBytes() const; |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 68 | uint8_t optionalPaddingBits() const; |
Max | 136ebcc | 2019-03-05 14:59:03 +0100 | [diff] [blame] | 69 | |
Max | 51754b6 | 2019-03-13 17:14:13 +0100 | [diff] [blame] | 70 | enum HeaderType headerTypeData() const; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 71 | |
| 72 | static GprsCodingScheme getBySizeUL(unsigned size); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 73 | static GprsCodingScheme getGprsByNum(unsigned num); |
| 74 | static GprsCodingScheme getEgprsByNum(unsigned num); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 75 | |
| 76 | private: |
Jacob Erlbeck | 7e7a261 | 2016-01-07 18:07:54 +0100 | [diff] [blame] | 77 | GprsCodingScheme(int s); /* fail on use */ |
| 78 | GprsCodingScheme& operator =(int s); /* fail on use */ |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 79 | enum CodingScheme m_scheme; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Max | 898dddb | 2019-03-12 15:50:57 +0100 | [diff] [blame] | 82 | // FIXME: remove once < comparison operator below is no longer necessary |
Max | b3a17d6 | 2017-12-21 12:11:33 +0100 | [diff] [blame] | 83 | inline uint8_t GprsCodingScheme::to_num() const |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 84 | { |
Max | 8a8e0fb | 2019-03-25 16:32:50 +0100 | [diff] [blame] | 85 | if (mcs_is_gprs(m_scheme)) |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 86 | return (m_scheme - CS1) + 1; |
| 87 | |
Max | 8a8e0fb | 2019-03-25 16:32:50 +0100 | [diff] [blame] | 88 | if (mcs_is_edge(m_scheme)) |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 89 | return (m_scheme - MCS1) + 1; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Max | a4de02d | 2019-03-13 16:35:09 +0100 | [diff] [blame] | 94 | inline bool GprsCodingScheme::isCompatible(enum mcs_kind mode) const |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 95 | { |
| 96 | switch (mode) { |
Max | 8a8e0fb | 2019-03-25 16:32:50 +0100 | [diff] [blame] | 97 | case GPRS: return mcs_is_gprs(m_scheme); |
| 98 | case EGPRS_GMSK: return mcs_is_edge_gmsk(m_scheme); |
| 99 | case EGPRS: return mcs_is_edge(m_scheme); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 105 | inline bool GprsCodingScheme::isCompatible(GprsCodingScheme o) const |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 106 | { |
Max | 8a8e0fb | 2019-03-25 16:32:50 +0100 | [diff] [blame] | 107 | return (mcs_is_gprs(m_scheme) && mcs_is_gprs(o)) || (mcs_is_edge(m_scheme) && mcs_is_edge(o)); |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 110 | inline GprsCodingScheme::GprsCodingScheme(CodingScheme s) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 111 | : m_scheme(s) |
| 112 | { |
| 113 | if (!isValid()) |
| 114 | m_scheme = UNKNOWN; |
| 115 | } |
| 116 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 117 | inline GprsCodingScheme& GprsCodingScheme::operator =(CodingScheme s) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 118 | { |
| 119 | m_scheme = s; |
| 120 | |
| 121 | if (!isValid()) |
| 122 | m_scheme = UNKNOWN; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 123 | |
| 124 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 127 | inline GprsCodingScheme& GprsCodingScheme::operator =(GprsCodingScheme o) |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 128 | { |
| 129 | m_scheme = o.m_scheme; |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 130 | return *this; |
Jacob Erlbeck | 409f980 | 2015-11-30 18:06:50 +0100 | [diff] [blame] | 131 | } |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 132 | |
| 133 | inline GprsCodingScheme GprsCodingScheme::getGprsByNum(unsigned num) |
| 134 | { |
| 135 | if (num < 1 || num > 4) |
| 136 | return GprsCodingScheme(); |
| 137 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 138 | return GprsCodingScheme(CodingScheme(CS1 + (num - 1))); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | inline GprsCodingScheme GprsCodingScheme::getEgprsByNum(unsigned num) |
| 142 | { |
| 143 | if (num < 1 || num > 9) |
| 144 | return GprsCodingScheme(); |
| 145 | |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 146 | return GprsCodingScheme(CodingScheme(MCS1 + (num - 1))); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* The coding schemes form a partial ordering */ |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 150 | inline bool GprsCodingScheme::operator == (CodingScheme scheme) const |
Aravind Sirsikar | e8ccafc | 2016-07-13 11:37:47 +0530 | [diff] [blame] | 151 | { |
| 152 | return this->m_scheme == scheme; |
| 153 | } |
| 154 | |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 155 | inline bool operator !=(GprsCodingScheme a, GprsCodingScheme b) |
| 156 | { |
| 157 | return !(a == b); |
| 158 | } |
| 159 | |
| 160 | inline bool operator <(GprsCodingScheme a, GprsCodingScheme b) |
| 161 | { |
Max | bea2edb | 2019-03-06 17:04:59 +0100 | [diff] [blame] | 162 | return a.isCompatible(b) && a.to_num() < b.to_num(); |
Jacob Erlbeck | 4c9e549 | 2016-01-04 16:00:05 +0100 | [diff] [blame] | 163 | } |