blob: 0f452c1233a0ad2f8639c20c9f166ed962588392 [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
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010033extern "C" {
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020034#include <osmocom/core/signal.h>
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010035#include "config_defs.h"
36}
37
Pau Espin Pedrolddd18a52019-06-28 17:01:16 +020038struct trx_ul_burst_ind {
39 SoftVector *rxBurst;
40 GSM::Time burstTime;
41 double rssi; // in dBFS
42 double toa; // in symbols
43 double noise; // noise level in dBFS
44 bool rssi_valid; // are RSSI, noise and burstTime valid
45};
46
Thomas Tsouf0782732013-10-29 15:55:47 -040047class Transceiver;
48
49/** Channel descriptor for transceiver object and channel number pair */
50struct TransceiverChannel {
51 TransceiverChannel(Transceiver *trx, int num)
52 {
53 this->trx = trx;
54 this->num = num;
55 }
56
57 ~TransceiverChannel()
58 {
59 }
60
61 Transceiver *trx;
62 size_t num;
63};
64
65/** Internal transceiver state variables */
66struct TransceiverState {
67 TransceiverState();
68 ~TransceiverState();
69
70 /* Initialize a multiframe slot in the filler table */
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010071 bool init(FillerType filler, size_t sps, float scale, size_t rtsc, unsigned rach_delay);
Thomas Tsouf0782732013-10-29 15:55:47 -040072
73 int chanType[8];
74
75 /* Last timestamp of each timeslot's channel estimate */
76 GSM::Time chanEstimateTime[8];
77
78 /* The filler table */
79 signalVector *fillerTable[102][8];
80 int fillerModulus[8];
Thomas Tsou15d743e2014-01-25 02:34:03 -050081 bool mRetrans;
Thomas Tsouf0782732013-10-29 15:55:47 -040082
83 /* Most recent channel estimate of all timeslots */
84 signalVector *chanResponse[8];
85
86 /* Most recent DFE feedback filter of all timeslots */
87 signalVector *DFEForward[8];
88 signalVector *DFEFeedback[8];
89
90 /* Most recent SNR, timing, and channel amplitude estimates */
91 float SNRestimate[8];
92 float chanRespOffset[8];
93 complex chanRespAmplitude[8];
Thomas Tsoua0179e32013-11-14 15:52:04 -050094
95 /* Received noise energy levels */
96 float mNoiseLev;
97 noiseVector mNoises;
Tom Tsoua4d1a412014-11-25 15:46:56 -080098
99 /* Shadowed downlink attenuation */
100 int mPower;
Thomas Tsouf0782732013-10-29 15:55:47 -0400101};
102
dburgessb3a0ca42011-10-12 07:44:40 +0000103/** The Transceiver class, responsible for physical layer of basestation */
104class Transceiver {
Tom Tsou64ad7122015-05-19 18:26:31 -0700105public:
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +0200106 /** Transceiver constructor
Tom Tsou64ad7122015-05-19 18:26:31 -0700107 @param wBasePort base port number of UDP sockets
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200108 @param TRXAddress IP address of the TRX, as a string
109 @param GSMcoreAddress IP address of the GSM core, as a string
Tom Tsou64ad7122015-05-19 18:26:31 -0700110 @param wSPS number of samples per GSM symbol
111 @param wTransmitLatency initial setting of transmit latency
112 @param radioInterface associated radioInterface object
113 */
114 Transceiver(int wBasePort,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400115 const char *TRXAddress,
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200116 const char *GSMcoreAddress,
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800117 size_t tx_sps, size_t rx_sps, size_t chans,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400118 GSM::Time wTransmitLatency,
119 RadioInterface *wRadioInterface,
Eric Wildac0487e2019-06-17 13:02:44 +0200120 double wRssiOffset, int stackSize);
Tom Tsou64ad7122015-05-19 18:26:31 -0700121
122 /** Destructor */
123 ~Transceiver();
124
125 /** Start the control loop */
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200126 bool init(FillerType filler, size_t rtsc, unsigned rach_delay,
127 bool edge, bool ext_rach);
Tom Tsou64ad7122015-05-19 18:26:31 -0700128
129 /** attach the radioInterface receive FIFO */
130 bool receiveFIFO(VectorFIFO *wFIFO, size_t chan)
131 {
132 if (chan >= mReceiveFIFO.size())
133 return false;
134
135 mReceiveFIFO[chan] = wFIFO;
136 return true;
137 }
138
139 /** accessor for number of channels */
140 size_t numChans() const { return mChans; };
141
142 /** Codes for channel combinations */
143 typedef enum {
144 FILL, ///< Channel is transmitted, but unused
145 I, ///< TCH/FS
146 II, ///< TCH/HS, idle every other slot
147 III, ///< TCH/HS
148 IV, ///< FCCH+SCH+CCCH+BCCH, uplink RACH
149 V, ///< FCCH+SCH+CCCH+BCCH+SDCCH/4+SACCH/4, uplink RACH+SDCCH/4
150 VI, ///< CCCH+BCCH, uplink RACH
151 VII, ///< SDCCH/8 + SACCH/8
152 VIII, ///< TCH/F + FACCH/F + SACCH/M
153 IX, ///< TCH/F + SACCH/M
154 X, ///< TCH/FD + SACCH/MD
155 XI, ///< PBCCH+PCCCH+PDTCH+PACCH+PTCCH
156 XII, ///< PCCCH+PDTCH+PACCH+PTCCH
157 XIII, ///< PDTCH+PACCH+PTCCH
158 NONE, ///< Channel is inactive, default
159 LOOPBACK ///< similar go VII, used in loopback testing
160 } ChannelCombination;
161
dburgessb3a0ca42011-10-12 07:44:40 +0000162private:
Thomas Tsoud647ec52013-10-29 15:17:34 -0400163 int mBasePort;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200164 std::string mLocalAddr;
165 std::string mRemoteAddr;
dburgessb3a0ca42011-10-12 07:44:40 +0000166
Thomas Tsou204a9f12013-10-29 18:34:16 -0400167 std::vector<UDPSocket *> mDataSockets; ///< socket for writing to/reading from GSM core
168 std::vector<UDPSocket *> mCtrlSockets; ///< socket for writing/reading control commands from GSM core
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800169 UDPSocket mClockSocket; ///< socket for writing clock updates to GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000170
Thomas Tsou204a9f12013-10-29 18:34:16 -0400171 std::vector<VectorQueue> mTxPriorityQueues; ///< priority queue of transmit bursts received from GSM core
172 std::vector<VectorFIFO *> mReceiveFIFO; ///< radioInterface FIFO of receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +0000173
Thomas Tsou204a9f12013-10-29 18:34:16 -0400174 std::vector<Thread *> mRxServiceLoopThreads; ///< thread to pull bursts into receive FIFO
175 Thread *mRxLowerLoopThread; ///< thread to pull bursts into receive FIFO
176 Thread *mTxLowerLoopThread; ///< thread to push bursts into transmit FIFO
177 std::vector<Thread *> mControlServiceLoopThreads; ///< thread to process control messages from GSM core
178 std::vector<Thread *> mTxPriorityQueueServiceLoopThreads; ///< thread to process transmit bursts from GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000179
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800180 GSM::Time mTransmitLatency; ///< latency between basestation clock and transmit deadline clock
181 GSM::Time mLatencyUpdateTime; ///< last time latency was updated
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +0200182 GSM::Time mTransmitDeadlineClock; ///< deadline for pushing bursts into transmit FIFO
dburgessb3a0ca42011-10-12 07:44:40 +0000183 GSM::Time mLastClockUpdateTime; ///< last time clock update was sent up to core
184
185 RadioInterface *mRadioInterface; ///< associated radioInterface object
186 double txFullScale; ///< full scale input to radio
187 double rxFullScale; ///< full scale output to radio
188
Alexander Chemerise8905a02015-06-03 23:47:56 -0400189 double rssiOffset; ///< RSSI to dBm conversion offset
Eric Wildac0487e2019-06-17 13:02:44 +0200190 int stackSize; ///< stack size for threads, 0 = OS default
Alexander Chemerise8905a02015-06-03 23:47:56 -0400191
dburgessb3a0ca42011-10-12 07:44:40 +0000192 /** modulate and add a burst to the transmit queue */
Thomas Tsoua2fe91a2013-11-13 22:48:11 -0500193 void addRadioVector(size_t chan, BitVector &bits,
Thomas Tsou204a9f12013-10-29 18:34:16 -0400194 int RSSI, GSM::Time &wTime);
dburgessb3a0ca42011-10-12 07:44:40 +0000195
Thomas Tsou15d743e2014-01-25 02:34:03 -0500196 /** Update filler table */
197 void updateFillerTable(size_t chan, radioVector *burst);
198
dburgessb3a0ca42011-10-12 07:44:40 +0000199 /** Push modulated burst into transmit FIFO corresponding to a particular timestamp */
200 void pushRadioVector(GSM::Time &nowTime);
201
Thomas Tsou204a9f12013-10-29 18:34:16 -0400202 /** Pull and demodulate a burst from the receive FIFO */
Pau Espin Pedrolddd18a52019-06-28 17:01:16 +0200203 bool pullRadioVector(size_t chan, struct trx_ul_burst_ind *ind);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400204
dburgessb3a0ca42011-10-12 07:44:40 +0000205 /** Set modulus for specific timeslot */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400206 void setModulus(size_t timeslot, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000207
208 /** return the expected burst type for the specified timestamp */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400209 CorrType expectedCorrType(GSM::Time currTime, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000210
211 /** send messages over the clock socket */
212 void writeClockInterface(void);
213
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400214 int mSPSTx; ///< number of samples per Tx symbol
215 int mSPSRx; ///< number of samples per Rx symbol
Thomas Tsou204a9f12013-10-29 18:34:16 -0400216 size_t mChans;
dburgessb3a0ca42011-10-12 07:44:40 +0000217
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200218 bool mExtRACH;
Tom Tsou64464e62016-07-01 03:46:46 -0700219 bool mEdge;
Alexander Chemeris37229202015-06-20 01:37:54 +0300220 bool mOn; ///< flag to indicate that transceiver is powered on
Pau Espin Pedrol934da482017-07-04 16:25:20 +0200221 bool mForceClockInterface; ///< flag to indicate whether IND CLOCK shall be sent unconditionally after transceiver is started
Alexander Chemeris5a068062015-06-20 01:38:47 +0300222 bool mHandover[8][8]; ///< expect handover to the timeslot/subslot
dburgessb3a0ca42011-10-12 07:44:40 +0000223 double mTxFreq; ///< the transmit frequency
224 double mRxFreq; ///< the receive frequency
dburgessb3a0ca42011-10-12 07:44:40 +0000225 unsigned mTSC; ///< the midamble sequence code
Alexander Chemeris78d1fc92016-03-19 21:16:22 +0300226 unsigned mMaxExpectedDelayAB; ///< maximum expected time-of-arrival offset in GSM symbols for Access Bursts (RACH)
227 unsigned mMaxExpectedDelayNB; ///< maximum expected time-of-arrival offset in GSM symbols for Normal Bursts
Alexander Chemerise692ce92015-06-12 00:15:31 -0400228 unsigned mWriteBurstToDiskMask; ///< debug: bitmask to indicate which timeslots to dump to disk
dburgessb3a0ca42011-10-12 07:44:40 +0000229
Thomas Tsou204a9f12013-10-29 18:34:16 -0400230 std::vector<TransceiverState> mStates;
dburgessb3a0ca42011-10-12 07:44:40 +0000231
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800232 /** Start and stop I/O threads through the control socket API */
233 bool start();
234 void stop();
235
236 /** Protect destructor accessable stop call */
237 Mutex mLock;
238
dburgessb3a0ca42011-10-12 07:44:40 +0000239protected:
Thomas Tsou204a9f12013-10-29 18:34:16 -0400240 /** drive lower receive I/O and burst generation */
241 void driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000242
Thomas Tsou204a9f12013-10-29 18:34:16 -0400243 /** drive demodulation of GSM bursts */
244 void driveReceiveFIFO(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000245
246 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400247 void driveTxFIFO();
dburgessb3a0ca42011-10-12 07:44:40 +0000248
249 /** drive handling of control messages from GSM core */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400250 void driveControl(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000251
252 /**
253 drive modulation and sorting of GSM bursts from GSM core
254 @return true if a burst was transferred successfully
255 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400256 bool driveTxPriorityQueue(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000257
Thomas Tsou204a9f12013-10-29 18:34:16 -0400258 friend void *RxUpperLoopAdapter(TransceiverChannel *);
Thomas Tsou92c16df2013-09-28 18:04:19 -0400259
Thomas Tsou204a9f12013-10-29 18:34:16 -0400260 friend void *TxUpperLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000261
Thomas Tsou204a9f12013-10-29 18:34:16 -0400262 friend void *RxLowerLoopAdapter(Transceiver *);
dburgessb3a0ca42011-10-12 07:44:40 +0000263
Thomas Tsou204a9f12013-10-29 18:34:16 -0400264 friend void *TxLowerLoopAdapter(Transceiver *);
265
266 friend void *ControlServiceLoopAdapter(TransceiverChannel *);
267
dburgessb3a0ca42011-10-12 07:44:40 +0000268
269 void reset();
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000270
271 /** set priority on current thread */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500272 void setPriority(float prio = 0.5) { mRadioInterface->setPriority(prio); }
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000273
Pau Espin Pedrolddd18a52019-06-28 17:01:16 +0200274 void logRxBurst(size_t chan, const struct trx_ul_burst_ind *bi, double dbm);
dburgessb3a0ca42011-10-12 07:44:40 +0000275};
276
Thomas Tsou204a9f12013-10-29 18:34:16 -0400277void *RxUpperLoopAdapter(TransceiverChannel *);
278
Thomas Tsou92c16df2013-09-28 18:04:19 -0400279/** Main drive threads */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400280void *RxLowerLoopAdapter(Transceiver *);
281void *TxLowerLoopAdapter(Transceiver *);
dburgessb3a0ca42011-10-12 07:44:40 +0000282
283/** control message handler thread loop */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400284void *ControlServiceLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000285
286/** transmit queueing thread loop */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400287void *TxUpperLoopAdapter(TransceiverChannel *);