dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 25 | #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 Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 33 | class Transceiver; |
| 34 | |
| 35 | /** Channel descriptor for transceiver object and channel number pair */ |
| 36 | struct 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 */ |
| 52 | struct TransceiverState { |
| 53 | TransceiverState(); |
| 54 | ~TransceiverState(); |
| 55 | |
| 56 | /* Initialize a multiframe slot in the filler table */ |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 57 | bool init(int filler, size_t sps, float scale, size_t rtsc); |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 58 | |
| 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 Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 67 | bool mRetrans; |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 68 | |
| 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 Tsou | a0179e3 | 2013-11-14 15:52:04 -0500 | [diff] [blame] | 80 | |
| 81 | /* Received noise energy levels */ |
| 82 | float mNoiseLev; |
| 83 | noiseVector mNoises; |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 84 | |
| 85 | /* Shadowed downlink attenuation */ |
| 86 | int mPower; |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 89 | /** The Transceiver class, responsible for physical layer of basestation */ |
| 90 | class Transceiver { |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 91 | public: |
| 92 | /** Transceiver constructor |
| 93 | @param wBasePort base port number of UDP sockets |
| 94 | @param TRXAddress IP address of the TRX manager, as a string |
| 95 | @param wSPS number of samples per GSM symbol |
| 96 | @param wTransmitLatency initial setting of transmit latency |
| 97 | @param radioInterface associated radioInterface object |
| 98 | */ |
| 99 | Transceiver(int wBasePort, |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 100 | const char *TRXAddress, |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 101 | size_t tx_sps, size_t rx_sps, size_t chans, |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 102 | GSM::Time wTransmitLatency, |
| 103 | RadioInterface *wRadioInterface, |
| 104 | double wRssiOffset); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 105 | |
| 106 | /** Destructor */ |
| 107 | ~Transceiver(); |
| 108 | |
| 109 | /** Start the control loop */ |
| 110 | bool init(int filler, size_t rtsc); |
| 111 | |
| 112 | /** attach the radioInterface receive FIFO */ |
| 113 | bool receiveFIFO(VectorFIFO *wFIFO, size_t chan) |
| 114 | { |
| 115 | if (chan >= mReceiveFIFO.size()) |
| 116 | return false; |
| 117 | |
| 118 | mReceiveFIFO[chan] = wFIFO; |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | /** accessor for number of channels */ |
| 123 | size_t numChans() const { return mChans; }; |
| 124 | |
| 125 | /** Codes for channel combinations */ |
| 126 | typedef enum { |
| 127 | FILL, ///< Channel is transmitted, but unused |
| 128 | I, ///< TCH/FS |
| 129 | II, ///< TCH/HS, idle every other slot |
| 130 | III, ///< TCH/HS |
| 131 | IV, ///< FCCH+SCH+CCCH+BCCH, uplink RACH |
| 132 | V, ///< FCCH+SCH+CCCH+BCCH+SDCCH/4+SACCH/4, uplink RACH+SDCCH/4 |
| 133 | VI, ///< CCCH+BCCH, uplink RACH |
| 134 | VII, ///< SDCCH/8 + SACCH/8 |
| 135 | VIII, ///< TCH/F + FACCH/F + SACCH/M |
| 136 | IX, ///< TCH/F + SACCH/M |
| 137 | X, ///< TCH/FD + SACCH/MD |
| 138 | XI, ///< PBCCH+PCCCH+PDTCH+PACCH+PTCCH |
| 139 | XII, ///< PCCCH+PDTCH+PACCH+PTCCH |
| 140 | XIII, ///< PDTCH+PACCH+PTCCH |
| 141 | NONE, ///< Channel is inactive, default |
| 142 | LOOPBACK ///< similar go VII, used in loopback testing |
| 143 | } ChannelCombination; |
| 144 | |
| 145 | /** Codes for burst types of received bursts*/ |
| 146 | typedef enum { |
| 147 | OFF, ///< timeslot is off |
| 148 | TSC, ///< timeslot should contain a normal burst |
| 149 | RACH, ///< timeslot should contain an access burst |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 150 | EDGE, ///< timeslot should contain an EDGE burst |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 151 | IDLE ///< timeslot is an idle (or dummy) burst |
| 152 | } CorrType; |
| 153 | |
Alexander Chemeris | f5fd578 | 2015-05-24 18:56:51 -0400 | [diff] [blame] | 154 | enum FillerType { |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 155 | FILLER_DUMMY, |
| 156 | FILLER_ZERO, |
Tom Tsou | af717b2 | 2016-03-06 22:19:15 -0800 | [diff] [blame^] | 157 | FILLER_NORM_RAND, |
| 158 | FILLER_EDGE_RAND, |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 161 | private: |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 162 | int mBasePort; |
| 163 | std::string mAddr; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 164 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 165 | std::vector<UDPSocket *> mDataSockets; ///< socket for writing to/reading from GSM core |
| 166 | std::vector<UDPSocket *> mCtrlSockets; ///< socket for writing/reading control commands from GSM core |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 167 | UDPSocket mClockSocket; ///< socket for writing clock updates to GSM core |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 168 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 169 | std::vector<VectorQueue> mTxPriorityQueues; ///< priority queue of transmit bursts received from GSM core |
| 170 | std::vector<VectorFIFO *> mReceiveFIFO; ///< radioInterface FIFO of receive bursts |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 171 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 172 | std::vector<Thread *> mRxServiceLoopThreads; ///< thread to pull bursts into receive FIFO |
| 173 | Thread *mRxLowerLoopThread; ///< thread to pull bursts into receive FIFO |
| 174 | Thread *mTxLowerLoopThread; ///< thread to push bursts into transmit FIFO |
| 175 | std::vector<Thread *> mControlServiceLoopThreads; ///< thread to process control messages from GSM core |
| 176 | std::vector<Thread *> mTxPriorityQueueServiceLoopThreads; ///< thread to process transmit bursts from GSM core |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 177 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 178 | GSM::Time mTransmitLatency; ///< latency between basestation clock and transmit deadline clock |
| 179 | GSM::Time mLatencyUpdateTime; ///< last time latency was updated |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 180 | GSM::Time mTransmitDeadlineClock; ///< deadline for pushing bursts into transmit FIFO |
| 181 | GSM::Time mLastClockUpdateTime; ///< last time clock update was sent up to core |
| 182 | |
| 183 | RadioInterface *mRadioInterface; ///< associated radioInterface object |
| 184 | double txFullScale; ///< full scale input to radio |
| 185 | double rxFullScale; ///< full scale output to radio |
| 186 | |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 187 | double rssiOffset; ///< RSSI to dBm conversion offset |
| 188 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 189 | /** modulate and add a burst to the transmit queue */ |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 190 | void addRadioVector(size_t chan, BitVector &bits, |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 191 | int RSSI, GSM::Time &wTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 192 | |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 193 | /** Update filler table */ |
| 194 | void updateFillerTable(size_t chan, radioVector *burst); |
| 195 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 196 | /** Push modulated burst into transmit FIFO corresponding to a particular timestamp */ |
| 197 | void pushRadioVector(GSM::Time &nowTime); |
| 198 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 199 | /** Pull and demodulate a burst from the receive FIFO */ |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 200 | SoftVector *pullRadioVector(GSM::Time &wTime, double &RSSI, bool &isRssiValid, |
Alexander Chemeris | dbe26ab | 2015-06-04 00:14:51 -0400 | [diff] [blame] | 201 | double &timingOffset, double &noise, |
| 202 | size_t chan = 0); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 203 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 204 | /** Set modulus for specific timeslot */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 205 | void setModulus(size_t timeslot, size_t chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 206 | |
| 207 | /** return the expected burst type for the specified timestamp */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 208 | CorrType expectedCorrType(GSM::Time currTime, size_t chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 209 | |
| 210 | /** send messages over the clock socket */ |
| 211 | void writeClockInterface(void); |
| 212 | |
Tom Tsou | 4656940 | 2016-03-06 01:59:38 -0800 | [diff] [blame] | 213 | /** Detectbursts */ |
| 214 | int detectBurst(TransceiverState *state, signalVector &burst, |
| 215 | complex &, float &toa, CorrType type); |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 216 | |
Tom Tsou | 4656940 | 2016-03-06 01:59:38 -0800 | [diff] [blame] | 217 | /** Demodulate burst and output soft bits */ |
| 218 | SoftVector *demodulate(TransceiverState *state, signalVector &burst, |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 219 | complex amp, float toa, CorrType type); |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 220 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 221 | int mSPSTx; ///< number of samples per Tx symbol |
| 222 | int mSPSRx; ///< number of samples per Rx symbol |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 223 | size_t mChans; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 224 | |
Alexander Chemeris | 3722920 | 2015-06-20 01:37:54 +0300 | [diff] [blame] | 225 | bool mOn; ///< flag to indicate that transceiver is powered on |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 226 | bool mHandover[8][8]; ///< expect handover to the timeslot/subslot |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 227 | double mTxFreq; ///< the transmit frequency |
| 228 | double mRxFreq; ///< the receive frequency |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 229 | unsigned mTSC; ///< the midamble sequence code |
Alexander Chemeris | 3722920 | 2015-06-20 01:37:54 +0300 | [diff] [blame] | 230 | unsigned mMaxExpectedDelay; ///< maximum expected time-of-arrival offset in GSM symbols |
Alexander Chemeris | e692ce9 | 2015-06-12 00:15:31 -0400 | [diff] [blame] | 231 | unsigned mWriteBurstToDiskMask; ///< debug: bitmask to indicate which timeslots to dump to disk |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 232 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 233 | std::vector<TransceiverState> mStates; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 234 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 235 | /** Start and stop I/O threads through the control socket API */ |
| 236 | bool start(); |
| 237 | void stop(); |
| 238 | |
| 239 | /** Protect destructor accessable stop call */ |
| 240 | Mutex mLock; |
| 241 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 242 | protected: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 243 | /** drive lower receive I/O and burst generation */ |
| 244 | void driveReceiveRadio(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 245 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 246 | /** drive demodulation of GSM bursts */ |
| 247 | void driveReceiveFIFO(size_t chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 248 | |
| 249 | /** drive transmission of GSM bursts */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 250 | void driveTxFIFO(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 251 | |
| 252 | /** drive handling of control messages from GSM core */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 253 | void driveControl(size_t chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | drive modulation and sorting of GSM bursts from GSM core |
| 257 | @return true if a burst was transferred successfully |
| 258 | */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 259 | bool driveTxPriorityQueue(size_t chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 260 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 261 | friend void *RxUpperLoopAdapter(TransceiverChannel *); |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 262 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 263 | friend void *TxUpperLoopAdapter(TransceiverChannel *); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 264 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 265 | friend void *RxLowerLoopAdapter(Transceiver *); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 266 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 267 | friend void *TxLowerLoopAdapter(Transceiver *); |
| 268 | |
| 269 | friend void *ControlServiceLoopAdapter(TransceiverChannel *); |
| 270 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 271 | |
| 272 | void reset(); |
kurtis.heimerl | 6b495a5 | 2011-11-26 03:17:21 +0000 | [diff] [blame] | 273 | |
| 274 | /** set priority on current thread */ |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 275 | void setPriority(float prio = 0.5) { mRadioInterface->setPriority(prio); } |
kurtis.heimerl | 6b495a5 | 2011-11-26 03:17:21 +0000 | [diff] [blame] | 276 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 277 | void logRxBurst(SoftVector *burst, GSM::Time time, double dbm, |
| 278 | double rssi, double noise, double toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 281 | void *RxUpperLoopAdapter(TransceiverChannel *); |
| 282 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 283 | /** Main drive threads */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 284 | void *RxLowerLoopAdapter(Transceiver *); |
| 285 | void *TxLowerLoopAdapter(Transceiver *); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 286 | |
| 287 | /** control message handler thread loop */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 288 | void *ControlServiceLoopAdapter(TransceiverChannel *); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 289 | |
| 290 | /** transmit queueing thread loop */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 291 | void *TxUpperLoopAdapter(TransceiverChannel *); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 292 | |