blob: 004536bf17b504db2b3743ecb43d0e472b2db110 [file] [log] [blame]
Alexander Chemeris040b3052013-06-16 14:29:54 +04001/**@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
43namespace 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. */
48extern const BitVector gTrainingSequence[];
Tom Tsoud3253432016-03-06 03:08:01 -080049extern const BitVector gEdgeTrainingSequence[];
Alexander Chemeris040b3052013-06-16 14:29:54 +040050
51/** C0T0 filler burst, GSM 05.02, 5.2.6 */
52extern const BitVector gDummyBurst;
53
54/** Random access burst synch. sequence */
55extern const BitVector gRACHSynchSequence;
56
57
58/**@name Modulus operations for frame numbers. */
59//@{
60/** The GSM hyperframe is largest time period in the GSM system, GSM 05.02 4.3.3. */
61const uint32_t gHyperframe = 2048UL * 26UL * 51UL;
62
63/** Get a clock difference, within the modulus, v1-v2. */
64int32_t FNDelta(int32_t v1, int32_t v2);
65
66/**
67 Compare two frame clock values.
68 @return 1 if v1>v2, -1 if v1<v2, 0 if v1==v2
69*/
70int FNCompare(int32_t v1, int32_t v2);
71
72
73//@}
74
75
76/**
77 GSM frame clock value. GSM 05.02 4.3
78 No internal thread sync.
79*/
80class Time {
81
82 private:
83
84 int mFN; ///< frame number in the hyperframe
85 int mTN; ///< timeslot number
86
87 public:
88
89 Time(int wFN=0, int wTN=0)
90 :mFN(wFN),mTN(wTN)
91 { }
92
93
94 /** Move the time forward to a given position in a given modulus. */
95 void rollForward(unsigned wFN, unsigned modulus)
96 {
97 assert(modulus<gHyperframe);
98 while ((mFN % modulus) != wFN) mFN=(mFN+1)%gHyperframe;
99 }
100
101 /**@name Accessors. */
102 //@{
103 int FN() const { return mFN; }
104 void FN(unsigned wFN) { mFN = wFN; }
105 unsigned TN() const { return mTN; }
106 void TN(unsigned wTN) { mTN=wTN; }
107 //@}
108
109 /**@name Arithmetic. */
110 //@{
111
112 Time& operator++()
113 {
114 mFN = (mFN+1) % gHyperframe;
115 return *this;
116 }
117
118 Time& decTN(unsigned step=1)
119 {
120 assert(step<=8);
121 mTN -= step;
122 if (mTN<0) {
123 mTN+=8;
124 mFN-=1;
125 if (mFN<0) mFN+=gHyperframe;
126 }
127 return *this;
128 }
129
130 Time& incTN(unsigned step=1)
131 {
132 assert(step<=8);
133 mTN += step;
134 if (mTN>7) {
135 mTN-=8;
136 mFN = (mFN+1) % gHyperframe;
137 }
138 return *this;
139 }
140
141 Time& operator+=(int step)
142 {
143 // Remember the step might be negative.
144 mFN += step;
145 if (mFN<0) mFN+=gHyperframe;
146 mFN = mFN % gHyperframe;
147 return *this;
148 }
149
150 Time operator-(int step) const
151 { return operator+(-step); }
152
153 Time operator+(int step) const
154 {
155 Time newVal = *this;
156 newVal += step;
157 return newVal;
158 }
159
160 Time operator+(const Time& other) const
161 {
162 unsigned newTN = (mTN + other.mTN) % 8;
163 uint64_t newFN = (mFN+other.mFN + (mTN + other.mTN)/8) % gHyperframe;
164 return Time(newFN,newTN);
165 }
166
167 int operator-(const Time& other) const
168 {
169 return FNDelta(mFN,other.mFN);
170 }
171
172 //@}
173
174
175 /**@name Comparisons. */
176 //@{
177
178 bool operator<(const Time& other) const
179 {
180 if (mFN==other.mFN) return (mTN<other.mTN);
181 return FNCompare(mFN,other.mFN)<0;
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 return (mFN == other.mFN) && (mTN==other.mTN);
205 }
206
207 //@}
208
209
210
211 /**@name Standard derivations. */
212 //@{
213
214 /** GSM 05.02 3.3.2.2.1 */
215 unsigned SFN() const { return mFN / (26*51); }
216
217 /** GSM 05.02 3.3.2.2.1 */
218 unsigned T1() const { return SFN() % 2048; }
219
220 /** GSM 05.02 3.3.2.2.1 */
221 unsigned T2() const { return mFN % 26; }
222
223 /** GSM 05.02 3.3.2.2.1 */
224 unsigned T3() const { return mFN % 51; }
225
226 /** GSM 05.02 3.3.2.2.1. */
227 unsigned T3p() const { return (T3()-1)/10; }
228
229 /** GSM 05.02 6.3.1.3. */
230 unsigned TC() const { return (FN()/51) % 8; }
231
232 /** GSM 04.08 10.5.2.30. */
233 unsigned T1p() const { return SFN() % 32; }
234
235 /** GSM 05.02 6.2.3 */
236 unsigned T1R() const { return T1() % 64; }
237
238 //@}
239};
240
241
242std::ostream& operator<<(std::ostream& os, const Time& ts);
243
244}; // namespace GSM
245
246
247#endif
248
249// vim: ts=4 sw=4