blob: 0b815112633a063a1bfc7de54d3ee6b41a054ce4 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4* This software is distributed under the terms of the GNU Public License.
5* See the COPYING file in the main directory for details.
6*
7* This use of this software may be subject to additional restrictions.
8* See the LEGAL file in the main directory for details.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23*/
24
dburgessb3a0ca42011-10-12 07:44:40 +000025#include "radioInterface.h"
26#include "Interthread.h"
27#include "GSMCommon.h"
28#include "Sockets.h"
29
30#include <sys/types.h>
31#include <sys/socket.h>
32
Thomas Tsouf0782732013-10-29 15:55:47 -040033class Transceiver;
34
35/** Channel descriptor for transceiver object and channel number pair */
36struct TransceiverChannel {
37 TransceiverChannel(Transceiver *trx, int num)
38 {
39 this->trx = trx;
40 this->num = num;
41 }
42
43 ~TransceiverChannel()
44 {
45 }
46
47 Transceiver *trx;
48 size_t num;
49};
50
51/** Internal transceiver state variables */
52struct TransceiverState {
53 TransceiverState();
54 ~TransceiverState();
55
56 /* Initialize a multiframe slot in the filler table */
Thomas Tsou15d743e2014-01-25 02:34:03 -050057 void init(size_t slot, signalVector *burst, bool fill);
Thomas Tsouf0782732013-10-29 15:55:47 -040058
59 int chanType[8];
60
61 /* Last timestamp of each timeslot's channel estimate */
62 GSM::Time chanEstimateTime[8];
63
64 /* The filler table */
65 signalVector *fillerTable[102][8];
66 int fillerModulus[8];
Thomas Tsou15d743e2014-01-25 02:34:03 -050067 bool mRetrans;
Thomas Tsouf0782732013-10-29 15:55:47 -040068
69 /* Most recent channel estimate of all timeslots */
70 signalVector *chanResponse[8];
71
72 /* Most recent DFE feedback filter of all timeslots */
73 signalVector *DFEForward[8];
74 signalVector *DFEFeedback[8];
75
76 /* Most recent SNR, timing, and channel amplitude estimates */
77 float SNRestimate[8];
78 float chanRespOffset[8];
79 complex chanRespAmplitude[8];
Thomas Tsoua0179e32013-11-14 15:52:04 -050080
81 /* Received noise energy levels */
82 float mNoiseLev;
83 noiseVector mNoises;
Tom Tsoua4d1a412014-11-25 15:46:56 -080084
85 /* Shadowed downlink attenuation */
86 int mPower;
Thomas Tsouf0782732013-10-29 15:55:47 -040087};
88
dburgessb3a0ca42011-10-12 07:44:40 +000089/** The Transceiver class, responsible for physical layer of basestation */
90class Transceiver {
dburgessb3a0ca42011-10-12 07:44:40 +000091private:
Thomas Tsoud647ec52013-10-29 15:17:34 -040092 int mBasePort;
93 std::string mAddr;
dburgessb3a0ca42011-10-12 07:44:40 +000094
Thomas Tsou204a9f12013-10-29 18:34:16 -040095 std::vector<UDPSocket *> mDataSockets; ///< socket for writing to/reading from GSM core
96 std::vector<UDPSocket *> mCtrlSockets; ///< socket for writing/reading control commands from GSM core
Tom Tsoueb54bdd2014-11-25 16:06:32 -080097 UDPSocket mClockSocket; ///< socket for writing clock updates to GSM core
dburgessb3a0ca42011-10-12 07:44:40 +000098
Thomas Tsou204a9f12013-10-29 18:34:16 -040099 std::vector<VectorQueue> mTxPriorityQueues; ///< priority queue of transmit bursts received from GSM core
100 std::vector<VectorFIFO *> mReceiveFIFO; ///< radioInterface FIFO of receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +0000101
Thomas Tsou204a9f12013-10-29 18:34:16 -0400102 std::vector<Thread *> mRxServiceLoopThreads; ///< thread to pull bursts into receive FIFO
103 Thread *mRxLowerLoopThread; ///< thread to pull bursts into receive FIFO
104 Thread *mTxLowerLoopThread; ///< thread to push bursts into transmit FIFO
105 std::vector<Thread *> mControlServiceLoopThreads; ///< thread to process control messages from GSM core
106 std::vector<Thread *> mTxPriorityQueueServiceLoopThreads; ///< thread to process transmit bursts from GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000107
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800108 GSM::Time mTransmitLatency; ///< latency between basestation clock and transmit deadline clock
109 GSM::Time mLatencyUpdateTime; ///< last time latency was updated
dburgessb3a0ca42011-10-12 07:44:40 +0000110 GSM::Time mTransmitDeadlineClock; ///< deadline for pushing bursts into transmit FIFO
111 GSM::Time mLastClockUpdateTime; ///< last time clock update was sent up to core
112
113 RadioInterface *mRadioInterface; ///< associated radioInterface object
114 double txFullScale; ///< full scale input to radio
115 double rxFullScale; ///< full scale output to radio
116
117 /** Codes for burst types of received bursts*/
118 typedef enum {
119 OFF, ///< timeslot is off
120 TSC, ///< timeslot should contain a normal burst
121 RACH, ///< timeslot should contain an access burst
122 IDLE ///< timeslot is an idle (or dummy) burst
123 } CorrType;
124
dburgessb3a0ca42011-10-12 07:44:40 +0000125 /** modulate and add a burst to the transmit queue */
Thomas Tsoua2fe91a2013-11-13 22:48:11 -0500126 void addRadioVector(size_t chan, BitVector &bits,
Thomas Tsou204a9f12013-10-29 18:34:16 -0400127 int RSSI, GSM::Time &wTime);
dburgessb3a0ca42011-10-12 07:44:40 +0000128
Thomas Tsou15d743e2014-01-25 02:34:03 -0500129 /** Update filler table */
130 void updateFillerTable(size_t chan, radioVector *burst);
131
dburgessb3a0ca42011-10-12 07:44:40 +0000132 /** Push modulated burst into transmit FIFO corresponding to a particular timestamp */
133 void pushRadioVector(GSM::Time &nowTime);
134
Thomas Tsou204a9f12013-10-29 18:34:16 -0400135 /** Pull and demodulate a burst from the receive FIFO */
136 SoftVector *pullRadioVector(GSM::Time &wTime, int &RSSI,
137 int &timingOffset, size_t chan = 0);
138
dburgessb3a0ca42011-10-12 07:44:40 +0000139 /** Set modulus for specific timeslot */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400140 void setModulus(size_t timeslot, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000141
142 /** return the expected burst type for the specified timestamp */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400143 CorrType expectedCorrType(GSM::Time currTime, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000144
145 /** send messages over the clock socket */
146 void writeClockInterface(void);
147
Thomas Tsou30421a72013-11-13 23:14:48 -0500148 /** Detect RACH bursts */
149 bool detectRACH(TransceiverState *state,
150 signalVector &burst,
151 complex &amp, float &toa);
152
153 /** Detect normal bursts */
154 bool detectTSC(TransceiverState *state,
155 signalVector &burst,
156 complex &amp, float &toa, GSM::Time &time);
157
158 /** Demodulat burst and output soft bits */
159 SoftVector *demodulate(TransceiverState *state,
160 signalVector &burst, complex amp,
161 float toa, size_t tn, bool equalize);
162
163
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400164 int mSPSTx; ///< number of samples per Tx symbol
165 int mSPSRx; ///< number of samples per Rx symbol
Thomas Tsou204a9f12013-10-29 18:34:16 -0400166 size_t mChans;
dburgessb3a0ca42011-10-12 07:44:40 +0000167
168 bool mOn; ///< flag to indicate that transceiver is powered on
dburgessb3a0ca42011-10-12 07:44:40 +0000169 double mTxFreq; ///< the transmit frequency
170 double mRxFreq; ///< the receive frequency
dburgessb3a0ca42011-10-12 07:44:40 +0000171 unsigned mTSC; ///< the midamble sequence code
dburgessb3a0ca42011-10-12 07:44:40 +0000172 unsigned mMaxExpectedDelay; ///< maximum expected time-of-arrival offset in GSM symbols
173
Thomas Tsou204a9f12013-10-29 18:34:16 -0400174 std::vector<TransceiverState> mStates;
dburgessb3a0ca42011-10-12 07:44:40 +0000175
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800176 /** Start and stop I/O threads through the control socket API */
177 bool start();
178 void stop();
179
180 /** Protect destructor accessable stop call */
181 Mutex mLock;
182
dburgessb3a0ca42011-10-12 07:44:40 +0000183public:
184
185 /** Transceiver constructor
186 @param wBasePort base port number of UDP sockets
187 @param TRXAddress IP address of the TRX manager, as a string
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400188 @param wSPS number of samples per GSM symbol
dburgessb3a0ca42011-10-12 07:44:40 +0000189 @param wTransmitLatency initial setting of transmit latency
190 @param radioInterface associated radioInterface object
191 */
192 Transceiver(int wBasePort,
193 const char *TRXAddress,
Thomas Tsou204a9f12013-10-29 18:34:16 -0400194 size_t wSPS, size_t chans,
dburgessb3a0ca42011-10-12 07:44:40 +0000195 GSM::Time wTransmitLatency,
196 RadioInterface *wRadioInterface);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400197
dburgessb3a0ca42011-10-12 07:44:40 +0000198 /** Destructor */
199 ~Transceiver();
200
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800201 /** Start the control loop */
Thomas Tsou15d743e2014-01-25 02:34:03 -0500202 bool init(bool filler);
dburgessb3a0ca42011-10-12 07:44:40 +0000203
204 /** attach the radioInterface receive FIFO */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400205 bool receiveFIFO(VectorFIFO *wFIFO, size_t chan)
206 {
207 if (chan >= mReceiveFIFO.size())
208 return false;
dburgessb3a0ca42011-10-12 07:44:40 +0000209
Thomas Tsou204a9f12013-10-29 18:34:16 -0400210 mReceiveFIFO[chan] = wFIFO;
211 return true;
212 }
dburgessb3a0ca42011-10-12 07:44:40 +0000213
Thomas Tsou85b179d2013-11-15 21:14:33 -0500214 /** accessor for number of channels */
215 size_t numChans() const { return mChans; };
216
Thomas Tsouf0782732013-10-29 15:55:47 -0400217 /** Codes for channel combinations */
218 typedef enum {
219 FILL, ///< Channel is transmitted, but unused
220 I, ///< TCH/FS
221 II, ///< TCH/HS, idle every other slot
222 III, ///< TCH/HS
223 IV, ///< FCCH+SCH+CCCH+BCCH, uplink RACH
224 V, ///< FCCH+SCH+CCCH+BCCH+SDCCH/4+SACCH/4, uplink RACH+SDCCH/4
225 VI, ///< CCCH+BCCH, uplink RACH
226 VII, ///< SDCCH/8 + SACCH/8
227 VIII, ///< TCH/F + FACCH/F + SACCH/M
228 IX, ///< TCH/F + SACCH/M
229 X, ///< TCH/FD + SACCH/MD
230 XI, ///< PBCCH+PCCCH+PDTCH+PACCH+PTCCH
231 XII, ///< PCCCH+PDTCH+PACCH+PTCCH
232 XIII, ///< PDTCH+PACCH+PTCCH
233 NONE, ///< Channel is inactive, default
234 LOOPBACK ///< similar go VII, used in loopback testing
235 } ChannelCombination;
236
dburgessb3a0ca42011-10-12 07:44:40 +0000237protected:
Thomas Tsou204a9f12013-10-29 18:34:16 -0400238 /** drive lower receive I/O and burst generation */
239 void driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000240
Thomas Tsou204a9f12013-10-29 18:34:16 -0400241 /** drive demodulation of GSM bursts */
242 void driveReceiveFIFO(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000243
244 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400245 void driveTxFIFO();
dburgessb3a0ca42011-10-12 07:44:40 +0000246
247 /** drive handling of control messages from GSM core */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400248 void driveControl(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000249
250 /**
251 drive modulation and sorting of GSM bursts from GSM core
252 @return true if a burst was transferred successfully
253 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400254 bool driveTxPriorityQueue(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000255
Thomas Tsou204a9f12013-10-29 18:34:16 -0400256 friend void *RxUpperLoopAdapter(TransceiverChannel *);
Thomas Tsou92c16df2013-09-28 18:04:19 -0400257
Thomas Tsou204a9f12013-10-29 18:34:16 -0400258 friend void *TxUpperLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000259
Thomas Tsou204a9f12013-10-29 18:34:16 -0400260 friend void *RxLowerLoopAdapter(Transceiver *);
dburgessb3a0ca42011-10-12 07:44:40 +0000261
Thomas Tsou204a9f12013-10-29 18:34:16 -0400262 friend void *TxLowerLoopAdapter(Transceiver *);
263
264 friend void *ControlServiceLoopAdapter(TransceiverChannel *);
265
dburgessb3a0ca42011-10-12 07:44:40 +0000266
267 void reset();
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000268
269 /** set priority on current thread */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500270 void setPriority(float prio = 0.5) { mRadioInterface->setPriority(prio); }
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000271
dburgessb3a0ca42011-10-12 07:44:40 +0000272};
273
Thomas Tsou204a9f12013-10-29 18:34:16 -0400274void *RxUpperLoopAdapter(TransceiverChannel *);
275
Thomas Tsou92c16df2013-09-28 18:04:19 -0400276/** Main drive threads */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400277void *RxLowerLoopAdapter(Transceiver *);
278void *TxLowerLoopAdapter(Transceiver *);
dburgessb3a0ca42011-10-12 07:44:40 +0000279
280/** control message handler thread loop */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400281void *ControlServiceLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000282
283/** transmit queueing thread loop */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400284void *TxUpperLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000285