blob: 48723b408de82c18fc3994b08e0d40dffbe28b40 [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*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02005* SPDX-License-Identifier: AGPL-3.0+
6*
Alexander Chemeris040b3052013-06-16 14:29:54 +04007* 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
45namespace 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. */
50extern const BitVector gTrainingSequence[];
Tom Tsoud3253432016-03-06 03:08:01 -080051extern const BitVector gEdgeTrainingSequence[];
Alexander Chemeris040b3052013-06-16 14:29:54 +040052
53/** C0T0 filler burst, GSM 05.02, 5.2.6 */
54extern const BitVector gDummyBurst;
55
56/** Random access burst synch. sequence */
Vadim Yanitskiya79bc702018-10-17 11:01:58 +020057extern const BitVector gRACHSynchSequenceTS0;
58extern const BitVector gRACHSynchSequenceTS1;
59extern const BitVector gRACHSynchSequenceTS2;
Alexander Chemeris5efe0502016-03-23 17:06:32 +030060/** Random access burst synch. sequence, GSM 05.02 5.2.7 */
61extern const BitVector gRACHBurst;
Alexander Chemeris040b3052013-06-16 14:29:54 +040062
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. */
67const uint32_t gHyperframe = 2048UL * 26UL * 51UL;
68
69/** Get a clock difference, within the modulus, v1-v2. */
70int32_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*/
76int 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*/
86class 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 Pedrolbdb970e2019-07-22 12:03:39 +0200171 }
Alexander Chemeris040b3052013-06-16 14:29:54 +0400172
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
248std::ostream& operator<<(std::ostream& os, const Time& ts);
249
250}; // namespace GSM
251
252
253#endif
254
255// vim: ts=4 sw=4