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