Alexander Chemeris | 040b305 | 2013-06-16 14:29:54 +0400 | [diff] [blame] | 1 | /**@file Common-use GSM declarations, most from the GSM 04.xx and 05.xx series. */ |
| 2 | /* |
| 3 | * Copyright 2008-2011 Free Software Foundation, Inc. |
| 4 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: AGPL-3.0+ |
| 6 | * |
Alexander Chemeris | 040b305 | 2013-06-16 14:29:54 +0400 | [diff] [blame] | 7 | * This software is distributed under the terms of the GNU Affero Public License. |
| 8 | * See the COPYING file in the main directory for details. |
| 9 | * |
| 10 | * This use of this software may be subject to additional restrictions. |
| 11 | * See the LEGAL file in the main directory for details. |
| 12 | |
| 13 | This program is free software: you can redistribute it and/or modify |
| 14 | it under the terms of the GNU Affero General Public License as published by |
| 15 | the Free Software Foundation, either version 3 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU Affero General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU Affero General Public License |
| 24 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | |
| 30 | #ifndef GSMCOMMON_H |
| 31 | #define GSMCOMMON_H |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <ostream> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include <Threads.h> |
| 39 | #include <Timeval.h> |
| 40 | #include <BitVector.h> |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | namespace GSM { |
| 46 | |
| 47 | /**@namespace GSM This namespace covers L1 FEC, L2 and L3 message translation. */ |
| 48 | |
| 49 | /** GSM Training sequences from GSM 05.02 5.2.3. */ |
| 50 | extern const BitVector gTrainingSequence[]; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 51 | extern const BitVector gEdgeTrainingSequence[]; |
Alexander Chemeris | 040b305 | 2013-06-16 14:29:54 +0400 | [diff] [blame] | 52 | |
| 53 | /** C0T0 filler burst, GSM 05.02, 5.2.6 */ |
| 54 | extern const BitVector gDummyBurst; |
| 55 | |
| 56 | /** Random access burst synch. sequence */ |
Vadim Yanitskiy | a79bc70 | 2018-10-17 11:01:58 +0200 | [diff] [blame] | 57 | extern const BitVector gRACHSynchSequenceTS0; |
| 58 | extern const BitVector gRACHSynchSequenceTS1; |
| 59 | extern const BitVector gRACHSynchSequenceTS2; |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 60 | /** Random access burst synch. sequence, GSM 05.02 5.2.7 */ |
| 61 | extern const BitVector gRACHBurst; |
Alexander Chemeris | 040b305 | 2013-06-16 14:29:54 +0400 | [diff] [blame] | 62 | |
| 63 | |
| 64 | /**@name Modulus operations for frame numbers. */ |
| 65 | //@{ |
| 66 | /** The GSM hyperframe is largest time period in the GSM system, GSM 05.02 4.3.3. */ |
| 67 | const uint32_t gHyperframe = 2048UL * 26UL * 51UL; |
| 68 | |
| 69 | /** Get a clock difference, within the modulus, v1-v2. */ |
| 70 | int32_t FNDelta(int32_t v1, int32_t v2); |
| 71 | |
| 72 | /** |
| 73 | Compare two frame clock values. |
| 74 | @return 1 if v1>v2, -1 if v1<v2, 0 if v1==v2 |
| 75 | */ |
| 76 | int FNCompare(int32_t v1, int32_t v2); |
| 77 | |
| 78 | |
| 79 | //@} |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | GSM frame clock value. GSM 05.02 4.3 |
| 84 | No internal thread sync. |
| 85 | */ |
| 86 | class Time { |
| 87 | |
| 88 | private: |
| 89 | |
| 90 | int mFN; ///< frame number in the hyperframe |
| 91 | int mTN; ///< timeslot number |
| 92 | |
| 93 | public: |
| 94 | |
| 95 | Time(int wFN=0, int wTN=0) |
| 96 | :mFN(wFN),mTN(wTN) |
| 97 | { } |
| 98 | |
| 99 | |
| 100 | /** Move the time forward to a given position in a given modulus. */ |
| 101 | void rollForward(unsigned wFN, unsigned modulus) |
| 102 | { |
| 103 | assert(modulus<gHyperframe); |
| 104 | while ((mFN % modulus) != wFN) mFN=(mFN+1)%gHyperframe; |
| 105 | } |
| 106 | |
| 107 | /**@name Accessors. */ |
| 108 | //@{ |
| 109 | int FN() const { return mFN; } |
| 110 | void FN(unsigned wFN) { mFN = wFN; } |
| 111 | unsigned TN() const { return mTN; } |
| 112 | void TN(unsigned wTN) { mTN=wTN; } |
| 113 | //@} |
| 114 | |
| 115 | /**@name Arithmetic. */ |
| 116 | //@{ |
| 117 | |
| 118 | Time& operator++() |
| 119 | { |
| 120 | mFN = (mFN+1) % gHyperframe; |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | Time& decTN(unsigned step=1) |
| 125 | { |
| 126 | assert(step<=8); |
| 127 | mTN -= step; |
| 128 | if (mTN<0) { |
| 129 | mTN+=8; |
| 130 | mFN-=1; |
| 131 | if (mFN<0) mFN+=gHyperframe; |
| 132 | } |
| 133 | return *this; |
| 134 | } |
| 135 | |
| 136 | Time& incTN(unsigned step=1) |
| 137 | { |
| 138 | assert(step<=8); |
| 139 | mTN += step; |
| 140 | if (mTN>7) { |
| 141 | mTN-=8; |
| 142 | mFN = (mFN+1) % gHyperframe; |
| 143 | } |
| 144 | return *this; |
| 145 | } |
| 146 | |
| 147 | Time& operator+=(int step) |
| 148 | { |
| 149 | // Remember the step might be negative. |
| 150 | mFN += step; |
| 151 | if (mFN<0) mFN+=gHyperframe; |
| 152 | mFN = mFN % gHyperframe; |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | Time operator-(int step) const |
| 157 | { return operator+(-step); } |
| 158 | |
| 159 | Time operator+(int step) const |
| 160 | { |
| 161 | Time newVal = *this; |
| 162 | newVal += step; |
| 163 | return newVal; |
| 164 | } |
| 165 | |
| 166 | Time operator+(const Time& other) const |
| 167 | { |
| 168 | unsigned newTN = (mTN + other.mTN) % 8; |
| 169 | uint64_t newFN = (mFN+other.mFN + (mTN + other.mTN)/8) % gHyperframe; |
| 170 | return Time(newFN,newTN); |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 171 | } |
Alexander Chemeris | 040b305 | 2013-06-16 14:29:54 +0400 | [diff] [blame] | 172 | |
| 173 | int operator-(const Time& other) const |
| 174 | { |
| 175 | return FNDelta(mFN,other.mFN); |
| 176 | } |
| 177 | |
| 178 | //@} |
| 179 | |
| 180 | |
| 181 | /**@name Comparisons. */ |
| 182 | //@{ |
| 183 | |
| 184 | bool operator<(const Time& other) const |
| 185 | { |
| 186 | if (mFN==other.mFN) return (mTN<other.mTN); |
| 187 | return FNCompare(mFN,other.mFN)<0; |
| 188 | } |
| 189 | |
| 190 | bool operator>(const Time& other) const |
| 191 | { |
| 192 | if (mFN==other.mFN) return (mTN>other.mTN); |
| 193 | return FNCompare(mFN,other.mFN)>0; |
| 194 | } |
| 195 | |
| 196 | bool operator<=(const Time& other) const |
| 197 | { |
| 198 | if (mFN==other.mFN) return (mTN<=other.mTN); |
| 199 | return FNCompare(mFN,other.mFN)<=0; |
| 200 | } |
| 201 | |
| 202 | bool operator>=(const Time& other) const |
| 203 | { |
| 204 | if (mFN==other.mFN) return (mTN>=other.mTN); |
| 205 | return FNCompare(mFN,other.mFN)>=0; |
| 206 | } |
| 207 | |
| 208 | bool operator==(const Time& other) const |
| 209 | { |
| 210 | return (mFN == other.mFN) && (mTN==other.mTN); |
| 211 | } |
| 212 | |
| 213 | //@} |
| 214 | |
| 215 | |
| 216 | |
| 217 | /**@name Standard derivations. */ |
| 218 | //@{ |
| 219 | |
| 220 | /** GSM 05.02 3.3.2.2.1 */ |
| 221 | unsigned SFN() const { return mFN / (26*51); } |
| 222 | |
| 223 | /** GSM 05.02 3.3.2.2.1 */ |
| 224 | unsigned T1() const { return SFN() % 2048; } |
| 225 | |
| 226 | /** GSM 05.02 3.3.2.2.1 */ |
| 227 | unsigned T2() const { return mFN % 26; } |
| 228 | |
| 229 | /** GSM 05.02 3.3.2.2.1 */ |
| 230 | unsigned T3() const { return mFN % 51; } |
| 231 | |
| 232 | /** GSM 05.02 3.3.2.2.1. */ |
| 233 | unsigned T3p() const { return (T3()-1)/10; } |
| 234 | |
| 235 | /** GSM 05.02 6.3.1.3. */ |
| 236 | unsigned TC() const { return (FN()/51) % 8; } |
| 237 | |
| 238 | /** GSM 04.08 10.5.2.30. */ |
| 239 | unsigned T1p() const { return SFN() % 32; } |
| 240 | |
| 241 | /** GSM 05.02 6.2.3 */ |
| 242 | unsigned T1R() const { return T1() % 64; } |
| 243 | |
| 244 | //@} |
| 245 | }; |
| 246 | |
| 247 | |
| 248 | std::ostream& operator<<(std::ostream& os, const Time& ts); |
| 249 | |
| 250 | }; // namespace GSM |
| 251 | |
| 252 | |
| 253 | #endif |
| 254 | |
| 255 | // vim: ts=4 sw=4 |