blob: aa2a716357f4937bbe268ea5922fa0e525edfbed [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"
dburgessb3a0ca42011-10-12 07:44:40 +000028
29#include <sys/types.h>
30#include <sys/socket.h>
31
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010032extern "C" {
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020033#include <osmocom/core/signal.h>
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010034#include "config_defs.h"
35}
36
Thomas Tsouf0782732013-10-29 15:55:47 -040037class Transceiver;
38
39/** Channel descriptor for transceiver object and channel number pair */
40struct TransceiverChannel {
41 TransceiverChannel(Transceiver *trx, int num)
42 {
43 this->trx = trx;
44 this->num = num;
45 }
46
47 ~TransceiverChannel()
48 {
49 }
50
51 Transceiver *trx;
52 size_t num;
53};
54
55/** Internal transceiver state variables */
56struct TransceiverState {
57 TransceiverState();
58 ~TransceiverState();
59
60 /* Initialize a multiframe slot in the filler table */
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010061 bool init(FillerType filler, size_t sps, float scale, size_t rtsc, unsigned rach_delay);
Thomas Tsouf0782732013-10-29 15:55:47 -040062
63 int chanType[8];
64
65 /* Last timestamp of each timeslot's channel estimate */
66 GSM::Time chanEstimateTime[8];
67
68 /* The filler table */
69 signalVector *fillerTable[102][8];
70 int fillerModulus[8];
Thomas Tsou15d743e2014-01-25 02:34:03 -050071 bool mRetrans;
Thomas Tsouf0782732013-10-29 15:55:47 -040072
73 /* Most recent channel estimate of all timeslots */
74 signalVector *chanResponse[8];
75
76 /* Most recent DFE feedback filter of all timeslots */
77 signalVector *DFEForward[8];
78 signalVector *DFEFeedback[8];
79
80 /* Most recent SNR, timing, and channel amplitude estimates */
81 float SNRestimate[8];
82 float chanRespOffset[8];
83 complex chanRespAmplitude[8];
Thomas Tsoua0179e32013-11-14 15:52:04 -050084
85 /* Received noise energy levels */
86 float mNoiseLev;
87 noiseVector mNoises;
Tom Tsoua4d1a412014-11-25 15:46:56 -080088
89 /* Shadowed downlink attenuation */
90 int mPower;
Thomas Tsouf0782732013-10-29 15:55:47 -040091};
92
dburgessb3a0ca42011-10-12 07:44:40 +000093/** The Transceiver class, responsible for physical layer of basestation */
94class Transceiver {
Tom Tsou64ad7122015-05-19 18:26:31 -070095public:
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +020096 /** Transceiver constructor
Tom Tsou64ad7122015-05-19 18:26:31 -070097 @param wBasePort base port number of UDP sockets
Pau Espin Pedrol8c800952017-08-16 16:53:23 +020098 @param TRXAddress IP address of the TRX, as a string
99 @param GSMcoreAddress IP address of the GSM core, as a string
Tom Tsou64ad7122015-05-19 18:26:31 -0700100 @param wSPS number of samples per GSM symbol
101 @param wTransmitLatency initial setting of transmit latency
102 @param radioInterface associated radioInterface object
103 */
104 Transceiver(int wBasePort,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400105 const char *TRXAddress,
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200106 const char *GSMcoreAddress,
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800107 size_t tx_sps, size_t rx_sps, size_t chans,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400108 GSM::Time wTransmitLatency,
109 RadioInterface *wRadioInterface,
Eric Wildac0487e2019-06-17 13:02:44 +0200110 double wRssiOffset, int stackSize);
Tom Tsou64ad7122015-05-19 18:26:31 -0700111
112 /** Destructor */
113 ~Transceiver();
114
115 /** Start the control loop */
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200116 bool init(FillerType filler, size_t rtsc, unsigned rach_delay,
117 bool edge, bool ext_rach);
Tom Tsou64ad7122015-05-19 18:26:31 -0700118
119 /** attach the radioInterface receive FIFO */
120 bool receiveFIFO(VectorFIFO *wFIFO, size_t chan)
121 {
122 if (chan >= mReceiveFIFO.size())
123 return false;
124
125 mReceiveFIFO[chan] = wFIFO;
126 return true;
127 }
128
129 /** accessor for number of channels */
130 size_t numChans() const { return mChans; };
131
132 /** Codes for channel combinations */
133 typedef enum {
134 FILL, ///< Channel is transmitted, but unused
135 I, ///< TCH/FS
136 II, ///< TCH/HS, idle every other slot
137 III, ///< TCH/HS
138 IV, ///< FCCH+SCH+CCCH+BCCH, uplink RACH
139 V, ///< FCCH+SCH+CCCH+BCCH+SDCCH/4+SACCH/4, uplink RACH+SDCCH/4
140 VI, ///< CCCH+BCCH, uplink RACH
141 VII, ///< SDCCH/8 + SACCH/8
142 VIII, ///< TCH/F + FACCH/F + SACCH/M
143 IX, ///< TCH/F + SACCH/M
144 X, ///< TCH/FD + SACCH/MD
145 XI, ///< PBCCH+PCCCH+PDTCH+PACCH+PTCCH
146 XII, ///< PCCCH+PDTCH+PACCH+PTCCH
147 XIII, ///< PDTCH+PACCH+PTCCH
148 NONE, ///< Channel is inactive, default
149 LOOPBACK ///< similar go VII, used in loopback testing
150 } ChannelCombination;
151
dburgessb3a0ca42011-10-12 07:44:40 +0000152private:
Thomas Tsoud647ec52013-10-29 15:17:34 -0400153 int mBasePort;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200154 std::string mLocalAddr;
155 std::string mRemoteAddr;
dburgessb3a0ca42011-10-12 07:44:40 +0000156
Pau Espin Pedrolb9d25152019-07-01 19:03:49 +0200157 std::vector<int> mDataSockets; ///< socket for writing to/reading from GSM core
158 std::vector<int> mCtrlSockets; ///< socket for writing/reading control commands from GSM core
159 int mClockSocket; ///< socket for writing clock updates to GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000160
Thomas Tsou204a9f12013-10-29 18:34:16 -0400161 std::vector<VectorQueue> mTxPriorityQueues; ///< priority queue of transmit bursts received from GSM core
162 std::vector<VectorFIFO *> mReceiveFIFO; ///< radioInterface FIFO of receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +0000163
Thomas Tsou204a9f12013-10-29 18:34:16 -0400164 std::vector<Thread *> mRxServiceLoopThreads; ///< thread to pull bursts into receive FIFO
165 Thread *mRxLowerLoopThread; ///< thread to pull bursts into receive FIFO
166 Thread *mTxLowerLoopThread; ///< thread to push bursts into transmit FIFO
167 std::vector<Thread *> mControlServiceLoopThreads; ///< thread to process control messages from GSM core
168 std::vector<Thread *> mTxPriorityQueueServiceLoopThreads; ///< thread to process transmit bursts from GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000169
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800170 GSM::Time mTransmitLatency; ///< latency between basestation clock and transmit deadline clock
171 GSM::Time mLatencyUpdateTime; ///< last time latency was updated
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +0200172 GSM::Time mTransmitDeadlineClock; ///< deadline for pushing bursts into transmit FIFO
dburgessb3a0ca42011-10-12 07:44:40 +0000173 GSM::Time mLastClockUpdateTime; ///< last time clock update was sent up to core
174
175 RadioInterface *mRadioInterface; ///< associated radioInterface object
176 double txFullScale; ///< full scale input to radio
177 double rxFullScale; ///< full scale output to radio
178
Alexander Chemerise8905a02015-06-03 23:47:56 -0400179 double rssiOffset; ///< RSSI to dBm conversion offset
Eric Wildac0487e2019-06-17 13:02:44 +0200180 int stackSize; ///< stack size for threads, 0 = OS default
Alexander Chemerise8905a02015-06-03 23:47:56 -0400181
dburgessb3a0ca42011-10-12 07:44:40 +0000182 /** modulate and add a burst to the transmit queue */
Thomas Tsoua2fe91a2013-11-13 22:48:11 -0500183 void addRadioVector(size_t chan, BitVector &bits,
Thomas Tsou204a9f12013-10-29 18:34:16 -0400184 int RSSI, GSM::Time &wTime);
dburgessb3a0ca42011-10-12 07:44:40 +0000185
Thomas Tsou15d743e2014-01-25 02:34:03 -0500186 /** Update filler table */
187 void updateFillerTable(size_t chan, radioVector *burst);
188
dburgessb3a0ca42011-10-12 07:44:40 +0000189 /** Push modulated burst into transmit FIFO corresponding to a particular timestamp */
190 void pushRadioVector(GSM::Time &nowTime);
191
Thomas Tsou204a9f12013-10-29 18:34:16 -0400192 /** Pull and demodulate a burst from the receive FIFO */
Pau Espin Pedrolddd18a52019-06-28 17:01:16 +0200193 bool pullRadioVector(size_t chan, struct trx_ul_burst_ind *ind);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400194
dburgessb3a0ca42011-10-12 07:44:40 +0000195 /** Set modulus for specific timeslot */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400196 void setModulus(size_t timeslot, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000197
198 /** return the expected burst type for the specified timestamp */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400199 CorrType expectedCorrType(GSM::Time currTime, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000200
201 /** send messages over the clock socket */
202 void writeClockInterface(void);
203
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400204 int mSPSTx; ///< number of samples per Tx symbol
205 int mSPSRx; ///< number of samples per Rx symbol
Thomas Tsou204a9f12013-10-29 18:34:16 -0400206 size_t mChans;
dburgessb3a0ca42011-10-12 07:44:40 +0000207
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200208 bool mExtRACH;
Tom Tsou64464e62016-07-01 03:46:46 -0700209 bool mEdge;
Alexander Chemeris37229202015-06-20 01:37:54 +0300210 bool mOn; ///< flag to indicate that transceiver is powered on
Pau Espin Pedrol934da482017-07-04 16:25:20 +0200211 bool mForceClockInterface; ///< flag to indicate whether IND CLOCK shall be sent unconditionally after transceiver is started
Alexander Chemeris5a068062015-06-20 01:38:47 +0300212 bool mHandover[8][8]; ///< expect handover to the timeslot/subslot
dburgessb3a0ca42011-10-12 07:44:40 +0000213 double mTxFreq; ///< the transmit frequency
214 double mRxFreq; ///< the receive frequency
dburgessb3a0ca42011-10-12 07:44:40 +0000215 unsigned mTSC; ///< the midamble sequence code
Alexander Chemeris78d1fc92016-03-19 21:16:22 +0300216 unsigned mMaxExpectedDelayAB; ///< maximum expected time-of-arrival offset in GSM symbols for Access Bursts (RACH)
217 unsigned mMaxExpectedDelayNB; ///< maximum expected time-of-arrival offset in GSM symbols for Normal Bursts
Alexander Chemerise692ce92015-06-12 00:15:31 -0400218 unsigned mWriteBurstToDiskMask; ///< debug: bitmask to indicate which timeslots to dump to disk
Pau Espin Pedrol15fa64b2019-07-01 20:41:55 +0200219 unsigned mVersionTRXD; ///< Format version to use for TRXD protocol communication
dburgessb3a0ca42011-10-12 07:44:40 +0000220
Thomas Tsou204a9f12013-10-29 18:34:16 -0400221 std::vector<TransceiverState> mStates;
dburgessb3a0ca42011-10-12 07:44:40 +0000222
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800223 /** Start and stop I/O threads through the control socket API */
224 bool start();
225 void stop();
226
227 /** Protect destructor accessable stop call */
228 Mutex mLock;
229
dburgessb3a0ca42011-10-12 07:44:40 +0000230protected:
Thomas Tsou204a9f12013-10-29 18:34:16 -0400231 /** drive lower receive I/O and burst generation */
232 void driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000233
Thomas Tsou204a9f12013-10-29 18:34:16 -0400234 /** drive demodulation of GSM bursts */
235 void driveReceiveFIFO(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000236
237 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400238 void driveTxFIFO();
dburgessb3a0ca42011-10-12 07:44:40 +0000239
240 /** drive handling of control messages from GSM core */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400241 void driveControl(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000242
243 /**
244 drive modulation and sorting of GSM bursts from GSM core
245 @return true if a burst was transferred successfully
246 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400247 bool driveTxPriorityQueue(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000248
Thomas Tsou204a9f12013-10-29 18:34:16 -0400249 friend void *RxUpperLoopAdapter(TransceiverChannel *);
Thomas Tsou92c16df2013-09-28 18:04:19 -0400250
Thomas Tsou204a9f12013-10-29 18:34:16 -0400251 friend void *TxUpperLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000252
Thomas Tsou204a9f12013-10-29 18:34:16 -0400253 friend void *RxLowerLoopAdapter(Transceiver *);
dburgessb3a0ca42011-10-12 07:44:40 +0000254
Thomas Tsou204a9f12013-10-29 18:34:16 -0400255 friend void *TxLowerLoopAdapter(Transceiver *);
256
257 friend void *ControlServiceLoopAdapter(TransceiverChannel *);
258
dburgessb3a0ca42011-10-12 07:44:40 +0000259
260 void reset();
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000261
262 /** set priority on current thread */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500263 void setPriority(float prio = 0.5) { mRadioInterface->setPriority(prio); }
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000264
Pau Espin Pedrol607a4142019-07-01 13:56:17 +0200265 void logRxBurst(size_t chan, const struct trx_ul_burst_ind *bi);
dburgessb3a0ca42011-10-12 07:44:40 +0000266};
267
Thomas Tsou204a9f12013-10-29 18:34:16 -0400268void *RxUpperLoopAdapter(TransceiverChannel *);
269
Thomas Tsou92c16df2013-09-28 18:04:19 -0400270/** Main drive threads */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400271void *RxLowerLoopAdapter(Transceiver *);
272void *TxLowerLoopAdapter(Transceiver *);
dburgessb3a0ca42011-10-12 07:44:40 +0000273
274/** control message handler thread loop */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400275void *ControlServiceLoopAdapter(TransceiverChannel *);
dburgessb3a0ca42011-10-12 07:44:40 +0000276
277/** transmit queueing thread loop */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400278void *TxUpperLoopAdapter(TransceiverChannel *);