blob: 432e3b494fb621b8cbb4eebf3324c0e6c4968d23 [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
Thomas Tsouf0782732013-10-29 15:55:47 -040038class Transceiver;
39
40/** Channel descriptor for transceiver object and channel number pair */
41struct TransceiverChannel {
42 TransceiverChannel(Transceiver *trx, int num)
43 {
44 this->trx = trx;
45 this->num = num;
46 }
47
48 ~TransceiverChannel()
49 {
50 }
51
52 Transceiver *trx;
53 size_t num;
54};
55
56/** Internal transceiver state variables */
57struct TransceiverState {
58 TransceiverState();
59 ~TransceiverState();
60
61 /* Initialize a multiframe slot in the filler table */
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010062 bool init(FillerType filler, size_t sps, float scale, size_t rtsc, unsigned rach_delay);
Thomas Tsouf0782732013-10-29 15:55:47 -040063
64 int chanType[8];
65
66 /* Last timestamp of each timeslot's channel estimate */
67 GSM::Time chanEstimateTime[8];
68
69 /* The filler table */
70 signalVector *fillerTable[102][8];
71 int fillerModulus[8];
Thomas Tsou15d743e2014-01-25 02:34:03 -050072 bool mRetrans;
Thomas Tsouf0782732013-10-29 15:55:47 -040073
74 /* Most recent channel estimate of all timeslots */
75 signalVector *chanResponse[8];
76
77 /* Most recent DFE feedback filter of all timeslots */
78 signalVector *DFEForward[8];
79 signalVector *DFEFeedback[8];
80
81 /* Most recent SNR, timing, and channel amplitude estimates */
82 float SNRestimate[8];
83 float chanRespOffset[8];
84 complex chanRespAmplitude[8];
Thomas Tsoua0179e32013-11-14 15:52:04 -050085
86 /* Received noise energy levels */
87 float mNoiseLev;
88 noiseVector mNoises;
Tom Tsoua4d1a412014-11-25 15:46:56 -080089
90 /* Shadowed downlink attenuation */
91 int mPower;
Thomas Tsouf0782732013-10-29 15:55:47 -040092};
93
dburgessb3a0ca42011-10-12 07:44:40 +000094/** The Transceiver class, responsible for physical layer of basestation */
95class Transceiver {
Tom Tsou64ad7122015-05-19 18:26:31 -070096public:
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +020097 /** Transceiver constructor
Tom Tsou64ad7122015-05-19 18:26:31 -070098 @param wBasePort base port number of UDP sockets
Pau Espin Pedrol8c800952017-08-16 16:53:23 +020099 @param TRXAddress IP address of the TRX, as a string
100 @param GSMcoreAddress IP address of the GSM core, as a string
Tom Tsou64ad7122015-05-19 18:26:31 -0700101 @param wSPS number of samples per GSM symbol
102 @param wTransmitLatency initial setting of transmit latency
103 @param radioInterface associated radioInterface object
104 */
105 Transceiver(int wBasePort,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400106 const char *TRXAddress,
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200107 const char *GSMcoreAddress,
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800108 size_t tx_sps, size_t rx_sps, size_t chans,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400109 GSM::Time wTransmitLatency,
110 RadioInterface *wRadioInterface,
111 double wRssiOffset);
Tom Tsou64ad7122015-05-19 18:26:31 -0700112
113 /** Destructor */
114 ~Transceiver();
115
116 /** Start the control loop */
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200117 bool init(FillerType filler, size_t rtsc, unsigned rach_delay,
118 bool edge, bool ext_rach);
Tom Tsou64ad7122015-05-19 18:26:31 -0700119
120 /** attach the radioInterface receive FIFO */
121 bool receiveFIFO(VectorFIFO *wFIFO, size_t chan)
122 {
123 if (chan >= mReceiveFIFO.size())
124 return false;
125
126 mReceiveFIFO[chan] = wFIFO;
127 return true;
128 }
129
130 /** accessor for number of channels */
131 size_t numChans() const { return mChans; };
132
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200133 void setSignalHandler(osmo_signal_cbfn cbfn);
134
Tom Tsou64ad7122015-05-19 18:26:31 -0700135 /** Codes for channel combinations */
136 typedef enum {
137 FILL, ///< Channel is transmitted, but unused
138 I, ///< TCH/FS
139 II, ///< TCH/HS, idle every other slot
140 III, ///< TCH/HS
141 IV, ///< FCCH+SCH+CCCH+BCCH, uplink RACH
142 V, ///< FCCH+SCH+CCCH+BCCH+SDCCH/4+SACCH/4, uplink RACH+SDCCH/4
143 VI, ///< CCCH+BCCH, uplink RACH
144 VII, ///< SDCCH/8 + SACCH/8
145 VIII, ///< TCH/F + FACCH/F + SACCH/M
146 IX, ///< TCH/F + SACCH/M
147 X, ///< TCH/FD + SACCH/MD
148 XI, ///< PBCCH+PCCCH+PDTCH+PACCH+PTCCH
149 XII, ///< PCCCH+PDTCH+PACCH+PTCCH
150 XIII, ///< PDTCH+PACCH+PTCCH
151 NONE, ///< Channel is inactive, default
152 LOOPBACK ///< similar go VII, used in loopback testing
153 } ChannelCombination;
154
dburgessb3a0ca42011-10-12 07:44:40 +0000155private:
Thomas Tsoud647ec52013-10-29 15:17:34 -0400156 int mBasePort;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200157 std::string mLocalAddr;
158 std::string mRemoteAddr;
dburgessb3a0ca42011-10-12 07:44:40 +0000159
Thomas Tsou204a9f12013-10-29 18:34:16 -0400160 std::vector<UDPSocket *> mDataSockets; ///< socket for writing to/reading from GSM core
161 std::vector<UDPSocket *> mCtrlSockets; ///< socket for writing/reading control commands from GSM core
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800162 UDPSocket mClockSocket; ///< socket for writing clock updates to GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000163
Thomas Tsou204a9f12013-10-29 18:34:16 -0400164 std::vector<VectorQueue> mTxPriorityQueues; ///< priority queue of transmit bursts received from GSM core
165 std::vector<VectorFIFO *> mReceiveFIFO; ///< radioInterface FIFO of receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +0000166
Thomas Tsou204a9f12013-10-29 18:34:16 -0400167 std::vector<Thread *> mRxServiceLoopThreads; ///< thread to pull bursts into receive FIFO
168 Thread *mRxLowerLoopThread; ///< thread to pull bursts into receive FIFO
169 Thread *mTxLowerLoopThread; ///< thread to push bursts into transmit FIFO
170 std::vector<Thread *> mControlServiceLoopThreads; ///< thread to process control messages from GSM core
171 std::vector<Thread *> mTxPriorityQueueServiceLoopThreads; ///< thread to process transmit bursts from GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000172
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800173 GSM::Time mTransmitLatency; ///< latency between basestation clock and transmit deadline clock
174 GSM::Time mLatencyUpdateTime; ///< last time latency was updated
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +0200175 GSM::Time mTransmitDeadlineClock; ///< deadline for pushing bursts into transmit FIFO
dburgessb3a0ca42011-10-12 07:44:40 +0000176 GSM::Time mLastClockUpdateTime; ///< last time clock update was sent up to core
177
178 RadioInterface *mRadioInterface; ///< associated radioInterface object
179 double txFullScale; ///< full scale input to radio
180 double rxFullScale; ///< full scale output to radio
181
Alexander Chemerise8905a02015-06-03 23:47:56 -0400182 double rssiOffset; ///< RSSI to dBm conversion offset
183
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200184 osmo_signal_cbfn *sig_cbfn; ///< Registered Signal Handler to announce events.
185
dburgessb3a0ca42011-10-12 07:44:40 +0000186 /** modulate and add a burst to the transmit queue */
Thomas Tsoua2fe91a2013-11-13 22:48:11 -0500187 void addRadioVector(size_t chan, BitVector &bits,
Thomas Tsou204a9f12013-10-29 18:34:16 -0400188 int RSSI, GSM::Time &wTime);
dburgessb3a0ca42011-10-12 07:44:40 +0000189
Thomas Tsou15d743e2014-01-25 02:34:03 -0500190 /** Update filler table */
191 void updateFillerTable(size_t chan, radioVector *burst);
192
dburgessb3a0ca42011-10-12 07:44:40 +0000193 /** Push modulated burst into transmit FIFO corresponding to a particular timestamp */
194 void pushRadioVector(GSM::Time &nowTime);
195
Thomas Tsou204a9f12013-10-29 18:34:16 -0400196 /** Pull and demodulate a burst from the receive FIFO */
Alexander Chemeris2b542102015-06-08 22:46:38 -0400197 SoftVector *pullRadioVector(GSM::Time &wTime, double &RSSI, bool &isRssiValid,
Alexander Chemerisdbe26ab2015-06-04 00:14:51 -0400198 double &timingOffset, double &noise,
199 size_t chan = 0);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400200
dburgessb3a0ca42011-10-12 07:44:40 +0000201 /** Set modulus for specific timeslot */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400202 void setModulus(size_t timeslot, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000203
204 /** return the expected burst type for the specified timestamp */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400205 CorrType expectedCorrType(GSM::Time currTime, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000206
207 /** send messages over the clock socket */
208 void writeClockInterface(void);
209
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400210 int mSPSTx; ///< number of samples per Tx symbol
211 int mSPSRx; ///< number of samples per Rx symbol
Thomas Tsou204a9f12013-10-29 18:34:16 -0400212 size_t mChans;
dburgessb3a0ca42011-10-12 07:44:40 +0000213
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200214 bool mExtRACH;
Tom Tsou64464e62016-07-01 03:46:46 -0700215 bool mEdge;
Alexander Chemeris37229202015-06-20 01:37:54 +0300216 bool mOn; ///< flag to indicate that transceiver is powered on
Pau Espin Pedrol934da482017-07-04 16:25:20 +0200217 bool mForceClockInterface; ///< flag to indicate whether IND CLOCK shall be sent unconditionally after transceiver is started
Alexander Chemeris5a068062015-06-20 01:38:47 +0300218 bool mHandover[8][8]; ///< expect handover to the timeslot/subslot
dburgessb3a0ca42011-10-12 07:44:40 +0000219 double mTxFreq; ///< the transmit frequency
220 double mRxFreq; ///< the receive frequency
dburgessb3a0ca42011-10-12 07:44:40 +0000221 unsigned mTSC; ///< the midamble sequence code
Alexander Chemeris78d1fc92016-03-19 21:16:22 +0300222 unsigned mMaxExpectedDelayAB; ///< maximum expected time-of-arrival offset in GSM symbols for Access Bursts (RACH)
223 unsigned mMaxExpectedDelayNB; ///< maximum expected time-of-arrival offset in GSM symbols for Normal Bursts
Alexander Chemerise692ce92015-06-12 00:15:31 -0400224 unsigned mWriteBurstToDiskMask; ///< debug: bitmask to indicate which timeslots to dump to disk
dburgessb3a0ca42011-10-12 07:44:40 +0000225
Thomas Tsou204a9f12013-10-29 18:34:16 -0400226 std::vector<TransceiverState> mStates;
dburgessb3a0ca42011-10-12 07:44:40 +0000227
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800228 /** Start and stop I/O threads through the control socket API */
229 bool start();
230 void stop();
231
232 /** Protect destructor accessable stop call */
233 Mutex mLock;
234
dburgessb3a0ca42011-10-12 07:44:40 +0000235protected:
Thomas Tsou204a9f12013-10-29 18:34:16 -0400236 /** drive lower receive I/O and burst generation */
237 void driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000238
Thomas Tsou204a9f12013-10-29 18:34:16 -0400239 /** drive demodulation of GSM bursts */
240 void driveReceiveFIFO(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000241
242 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400243 void driveTxFIFO();
dburgessb3a0ca42011-10-12 07:44:40 +0000244
245 /** drive handling of control messages from GSM core */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400246 void driveControl(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000247
248 /**
249 drive modulation and sorting of GSM bursts from GSM core
250 @return true if a burst was transferred successfully
251 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400252 bool driveTxPriorityQueue(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000253
Thomas Tsou204a9f12013-10-29 18:34:16 -0400254 friend void *RxUpperLoopAdapter(TransceiverChannel *);
Thomas Tsou92c16df2013-09-28 18:04:19 -0400255
Thomas Tsou204a9f12013-10-29 18:34:16 -0400256 friend void *TxUpperLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000257
Thomas Tsou204a9f12013-10-29 18:34:16 -0400258 friend void *RxLowerLoopAdapter(Transceiver *);
dburgessb3a0ca42011-10-12 07:44:40 +0000259
Thomas Tsou204a9f12013-10-29 18:34:16 -0400260 friend void *TxLowerLoopAdapter(Transceiver *);
261
262 friend void *ControlServiceLoopAdapter(TransceiverChannel *);
263
dburgessb3a0ca42011-10-12 07:44:40 +0000264
265 void reset();
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000266
267 /** set priority on current thread */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500268 void setPriority(float prio = 0.5) { mRadioInterface->setPriority(prio); }
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000269
Alexander Chemeris58e95912016-03-25 18:20:28 +0300270 void logRxBurst(size_t chan, SoftVector *burst, GSM::Time time, double dbm,
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800271 double rssi, double noise, double toa);
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 *);