dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2009, 2010 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 25 | #include <iomanip> // std::setprecision |
Alexander Chemeris | e692ce9 | 2015-06-12 00:15:31 -0400 | [diff] [blame] | 26 | #include <fstream> |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 27 | #include "Transceiver.h" |
| 28 | #include <Logger.h> |
| 29 | |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include "config.h" |
| 32 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 33 | |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 34 | using namespace GSM; |
| 35 | |
kurtis.heimerl | ec842de | 2012-11-23 08:37:32 +0000 | [diff] [blame] | 36 | #define USB_LATENCY_INTRVL 10,0 |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 37 | |
| 38 | #if USE_UHD |
kurtis.heimerl | ec842de | 2012-11-23 08:37:32 +0000 | [diff] [blame] | 39 | # define USB_LATENCY_MIN 6,7 |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 40 | #else |
kurtis.heimerl | ec842de | 2012-11-23 08:37:32 +0000 | [diff] [blame] | 41 | # define USB_LATENCY_MIN 1,1 |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 42 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 43 | |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 44 | /* Number of running values use in noise average */ |
| 45 | #define NOISE_CNT 20 |
ttsou | e8dde02 | 2012-12-06 15:43:55 +0000 | [diff] [blame] | 46 | |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 47 | TransceiverState::TransceiverState() |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 48 | : mRetrans(false), mNoiseLev(0.0), mNoises(NOISE_CNT), mPower(0.0) |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 49 | { |
| 50 | for (int i = 0; i < 8; i++) { |
| 51 | chanType[i] = Transceiver::NONE; |
| 52 | fillerModulus[i] = 26; |
| 53 | chanResponse[i] = NULL; |
| 54 | DFEForward[i] = NULL; |
| 55 | DFEFeedback[i] = NULL; |
| 56 | |
| 57 | for (int n = 0; n < 102; n++) |
| 58 | fillerTable[n][i] = NULL; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | TransceiverState::~TransceiverState() |
| 63 | { |
| 64 | for (int i = 0; i < 8; i++) { |
| 65 | delete chanResponse[i]; |
| 66 | delete DFEForward[i]; |
| 67 | delete DFEFeedback[i]; |
| 68 | |
| 69 | for (int n = 0; n < 102; n++) |
| 70 | delete fillerTable[n][i]; |
| 71 | } |
| 72 | } |
| 73 | |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 74 | bool TransceiverState::init(FillerType filler, size_t sps, float scale, size_t rtsc, unsigned rach_delay) |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 75 | { |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 76 | signalVector *burst; |
| 77 | |
| 78 | if ((sps != 1) && (sps != 4)) |
| 79 | return false; |
| 80 | |
| 81 | for (size_t n = 0; n < 8; n++) { |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 82 | for (size_t i = 0; i < 102; i++) { |
| 83 | switch (filler) { |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 84 | case FILLER_DUMMY: |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 85 | burst = generateDummyBurst(sps, n); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 86 | break; |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 87 | case FILLER_NORM_RAND: |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 88 | burst = genRandNormalBurst(rtsc, sps, n); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 89 | break; |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 90 | case FILLER_EDGE_RAND: |
Tom Tsou | af717b2 | 2016-03-06 22:19:15 -0800 | [diff] [blame] | 91 | burst = generateEdgeBurst(rtsc); |
| 92 | break; |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 93 | case FILLER_ACCESS_RAND: |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 94 | burst = genRandAccessBurst(rach_delay, sps, n); |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 95 | break; |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 96 | case FILLER_ZERO: |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 97 | default: |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 98 | burst = generateEmptyBurst(sps, n); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | scaleVector(*burst, scale); |
| 102 | fillerTable[i][n] = burst; |
| 103 | } |
| 104 | |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 105 | if ((filler == FILLER_NORM_RAND) || |
| 106 | (filler == FILLER_EDGE_RAND)) { |
Alexander Chemeris | f9e78be | 2017-03-17 15:00:34 -0700 | [diff] [blame] | 107 | chanType[n] = TSC; |
Tom Tsou | af717b2 | 2016-03-06 22:19:15 -0800 | [diff] [blame] | 108 | } |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 109 | } |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 110 | |
| 111 | return false; |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 112 | } |
| 113 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 114 | Transceiver::Transceiver(int wBasePort, |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 115 | const char *TRXAddress, |
| 116 | const char *GSMcoreAddress, |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 117 | size_t tx_sps, size_t rx_sps, size_t chans, |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 118 | GSM::Time wTransmitLatency, |
| 119 | RadioInterface *wRadioInterface, |
| 120 | double wRssiOffset) |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 121 | : mBasePort(wBasePort), mLocalAddr(TRXAddress), mRemoteAddr(GSMcoreAddress), |
| 122 | mClockSocket(TRXAddress, wBasePort, GSMcoreAddress, wBasePort + 100), |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 123 | mTransmitLatency(wTransmitLatency), mRadioInterface(wRadioInterface), |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 124 | rssiOffset(wRssiOffset), |
Pau Espin Pedrol | 934da48 | 2017-07-04 16:25:20 +0200 | [diff] [blame] | 125 | mSPSTx(tx_sps), mSPSRx(rx_sps), mChans(chans), mEdge(false), mOn(false), mForceClockInterface(false), |
Alexander Chemeris | 78d1fc9 | 2016-03-19 21:16:22 +0300 | [diff] [blame] | 126 | mTxFreq(0.0), mRxFreq(0.0), mTSC(0), mMaxExpectedDelayAB(0), mMaxExpectedDelayNB(0), |
| 127 | mWriteBurstToDiskMask(0) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 128 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 129 | txFullScale = mRadioInterface->fullScaleInputValue(); |
| 130 | rxFullScale = mRadioInterface->fullScaleOutputValue(); |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 131 | |
| 132 | for (int i = 0; i < 8; i++) { |
| 133 | for (int j = 0; j < 8; j++) |
| 134 | mHandover[i][j] = false; |
| 135 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Transceiver::~Transceiver() |
| 139 | { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 140 | stop(); |
| 141 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 142 | sigProcLibDestroy(); |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 143 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 144 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 145 | mControlServiceLoopThreads[i]->cancel(); |
| 146 | mControlServiceLoopThreads[i]->join(); |
| 147 | delete mControlServiceLoopThreads[i]; |
| 148 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 149 | mTxPriorityQueues[i].clear(); |
| 150 | delete mCtrlSockets[i]; |
| 151 | delete mDataSockets[i]; |
| 152 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 153 | } |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 154 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 155 | /* |
| 156 | * Initialize transceiver |
| 157 | * |
| 158 | * Start or restart the control loop. Any further control is handled through the |
| 159 | * socket API. Randomize the central radio clock set the downlink burst |
| 160 | * counters. Note that the clock will not update until the radio starts, but we |
| 161 | * are still expected to report clock indications through control channel |
| 162 | * activity. |
| 163 | */ |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 164 | bool Transceiver::init(FillerType filler, size_t rtsc, unsigned rach_delay, bool edge) |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 165 | { |
Thomas Tsou | e1ce925 | 2013-11-13 22:40:44 -0500 | [diff] [blame] | 166 | int d_srcport, d_dstport, c_srcport, c_dstport; |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 167 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 168 | if (!mChans) { |
| 169 | LOG(ALERT) << "No channels assigned"; |
| 170 | return false; |
| 171 | } |
| 172 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 173 | if (!sigProcLibSetup()) { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 174 | LOG(ALERT) << "Failed to initialize signal processing library"; |
| 175 | return false; |
| 176 | } |
| 177 | |
Tom Tsou | 64464e6 | 2016-07-01 03:46:46 -0700 | [diff] [blame] | 178 | mEdge = edge; |
| 179 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 180 | mDataSockets.resize(mChans); |
| 181 | mCtrlSockets.resize(mChans); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 182 | mControlServiceLoopThreads.resize(mChans); |
| 183 | mTxPriorityQueueServiceLoopThreads.resize(mChans); |
| 184 | mRxServiceLoopThreads.resize(mChans); |
| 185 | |
| 186 | mTxPriorityQueues.resize(mChans); |
| 187 | mReceiveFIFO.resize(mChans); |
| 188 | mStates.resize(mChans); |
| 189 | |
Thomas Tsou | ccb73e1 | 2014-04-15 17:41:28 -0400 | [diff] [blame] | 190 | /* Filler table retransmissions - support only on channel 0 */ |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 191 | if (filler == FILLER_DUMMY) |
Thomas Tsou | ccb73e1 | 2014-04-15 17:41:28 -0400 | [diff] [blame] | 192 | mStates[0].mRetrans = true; |
| 193 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 194 | /* Setup sockets */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 195 | for (size_t i = 0; i < mChans; i++) { |
Thomas Tsou | e1ce925 | 2013-11-13 22:40:44 -0500 | [diff] [blame] | 196 | c_srcport = mBasePort + 2 * i + 1; |
| 197 | c_dstport = mBasePort + 2 * i + 101; |
| 198 | d_srcport = mBasePort + 2 * i + 2; |
| 199 | d_dstport = mBasePort + 2 * i + 102; |
| 200 | |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 201 | mCtrlSockets[i] = new UDPSocket(mLocalAddr.c_str(), c_srcport, mRemoteAddr.c_str(), c_dstport); |
| 202 | mDataSockets[i] = new UDPSocket(mLocalAddr.c_str(), d_srcport, mRemoteAddr.c_str(), d_dstport); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 203 | } |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 204 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 205 | /* Randomize the central clock */ |
| 206 | GSM::Time startTime(random() % gHyperframe, 0); |
| 207 | mRadioInterface->getClock()->set(startTime); |
| 208 | mTransmitDeadlineClock = startTime; |
| 209 | mLastClockUpdateTime = startTime; |
| 210 | mLatencyUpdateTime = startTime; |
| 211 | |
| 212 | /* Start control threads */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 213 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 214 | TransceiverChannel *chan = new TransceiverChannel(this, i); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 215 | mControlServiceLoopThreads[i] = new Thread(32768); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 216 | mControlServiceLoopThreads[i]->start((void * (*)(void*)) |
| 217 | ControlServiceLoopAdapter, (void*) chan); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 218 | |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 219 | if (i && filler == FILLER_DUMMY) |
| 220 | filler = FILLER_ZERO; |
| 221 | |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 222 | mStates[i].init(filler, mSPSTx, txFullScale, rtsc, rach_delay); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return true; |
| 226 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 227 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 228 | /* |
| 229 | * Start the transceiver |
| 230 | * |
| 231 | * Submit command(s) to the radio device to commence streaming samples and |
| 232 | * launch threads to handle sample I/O. Re-synchronize the transmit burst |
| 233 | * counters to the central radio clock here as well. |
| 234 | */ |
| 235 | bool Transceiver::start() |
| 236 | { |
| 237 | ScopedLock lock(mLock); |
| 238 | |
| 239 | if (mOn) { |
| 240 | LOG(ERR) << "Transceiver already running"; |
Ivan Kluchnikov | 194a9b1 | 2015-04-23 17:08:27 +0300 | [diff] [blame] | 241 | return true; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | LOG(NOTICE) << "Starting the transceiver"; |
| 245 | |
| 246 | GSM::Time time = mRadioInterface->getClock()->get(); |
| 247 | mTransmitDeadlineClock = time; |
| 248 | mLastClockUpdateTime = time; |
| 249 | mLatencyUpdateTime = time; |
| 250 | |
| 251 | if (!mRadioInterface->start()) { |
| 252 | LOG(ALERT) << "Device failed to start"; |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | /* Device is running - launch I/O threads */ |
| 257 | mRxLowerLoopThread = new Thread(32768); |
| 258 | mTxLowerLoopThread = new Thread(32768); |
| 259 | mTxLowerLoopThread->start((void * (*)(void*)) |
| 260 | TxLowerLoopAdapter,(void*) this); |
| 261 | mRxLowerLoopThread->start((void * (*)(void*)) |
| 262 | RxLowerLoopAdapter,(void*) this); |
| 263 | |
| 264 | /* Launch uplink and downlink burst processing threads */ |
| 265 | for (size_t i = 0; i < mChans; i++) { |
| 266 | TransceiverChannel *chan = new TransceiverChannel(this, i); |
| 267 | mRxServiceLoopThreads[i] = new Thread(32768); |
| 268 | mRxServiceLoopThreads[i]->start((void * (*)(void*)) |
| 269 | RxUpperLoopAdapter, (void*) chan); |
| 270 | |
| 271 | chan = new TransceiverChannel(this, i); |
| 272 | mTxPriorityQueueServiceLoopThreads[i] = new Thread(32768); |
| 273 | mTxPriorityQueueServiceLoopThreads[i]->start((void * (*)(void*)) |
| 274 | TxUpperLoopAdapter, (void*) chan); |
| 275 | } |
| 276 | |
Pau Espin Pedrol | 934da48 | 2017-07-04 16:25:20 +0200 | [diff] [blame] | 277 | mForceClockInterface = true; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 278 | mOn = true; |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Stop the transceiver |
| 284 | * |
| 285 | * Perform stopping by disabling receive streaming and issuing cancellation |
| 286 | * requests to running threads. Most threads will timeout and terminate once |
| 287 | * device is disabled, but the transmit loop may block waiting on the central |
| 288 | * UMTS clock. Explicitly signal the clock to make sure that the transmit loop |
| 289 | * makes it to the thread cancellation point. |
| 290 | */ |
| 291 | void Transceiver::stop() |
| 292 | { |
| 293 | ScopedLock lock(mLock); |
| 294 | |
| 295 | if (!mOn) |
| 296 | return; |
| 297 | |
| 298 | LOG(NOTICE) << "Stopping the transceiver"; |
| 299 | mTxLowerLoopThread->cancel(); |
| 300 | mRxLowerLoopThread->cancel(); |
Tom Tsou | d67bd60 | 2017-06-15 15:35:02 -0700 | [diff] [blame] | 301 | mTxLowerLoopThread->join(); |
| 302 | mRxLowerLoopThread->join(); |
| 303 | delete mTxLowerLoopThread; |
| 304 | delete mRxLowerLoopThread; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 305 | |
| 306 | for (size_t i = 0; i < mChans; i++) { |
| 307 | mRxServiceLoopThreads[i]->cancel(); |
| 308 | mTxPriorityQueueServiceLoopThreads[i]->cancel(); |
| 309 | } |
| 310 | |
| 311 | LOG(INFO) << "Stopping the device"; |
| 312 | mRadioInterface->stop(); |
| 313 | |
| 314 | for (size_t i = 0; i < mChans; i++) { |
| 315 | mRxServiceLoopThreads[i]->join(); |
| 316 | mTxPriorityQueueServiceLoopThreads[i]->join(); |
| 317 | delete mRxServiceLoopThreads[i]; |
| 318 | delete mTxPriorityQueueServiceLoopThreads[i]; |
| 319 | |
| 320 | mTxPriorityQueues[i].clear(); |
| 321 | } |
| 322 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 323 | mOn = false; |
| 324 | LOG(NOTICE) << "Transceiver stopped"; |
| 325 | } |
| 326 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 327 | void Transceiver::addRadioVector(size_t chan, BitVector &bits, |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 328 | int RSSI, GSM::Time &wTime) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 329 | { |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 330 | signalVector *burst; |
| 331 | radioVector *radio_burst; |
| 332 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 333 | if (chan >= mTxPriorityQueues.size()) { |
| 334 | LOG(ALERT) << "Invalid channel " << chan; |
| 335 | return; |
| 336 | } |
| 337 | |
Thomas Tsou | 2d0c00b | 2013-11-14 15:28:23 -0500 | [diff] [blame] | 338 | if (wTime.TN() > 7) { |
| 339 | LOG(ALERT) << "Received burst with invalid slot " << wTime.TN(); |
| 340 | return; |
| 341 | } |
| 342 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 343 | /* Use the number of bits as the EDGE burst indicator */ |
| 344 | if (bits.size() == EDGE_BURST_NBITS) |
| 345 | burst = modulateEdgeBurst(bits, mSPSTx); |
| 346 | else |
| 347 | burst = modulateBurst(bits, 8 + (wTime.TN() % 4 == 0), mSPSTx); |
| 348 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 349 | scaleVector(*burst, txFullScale * pow(10, -RSSI / 10)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 350 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 351 | radio_burst = new radioVector(wTime, burst); |
| 352 | |
| 353 | mTxPriorityQueues[chan].write(radio_burst); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 356 | void Transceiver::updateFillerTable(size_t chan, radioVector *burst) |
| 357 | { |
| 358 | int TN, modFN; |
| 359 | TransceiverState *state = &mStates[chan]; |
| 360 | |
| 361 | TN = burst->getTime().TN(); |
| 362 | modFN = burst->getTime().FN() % state->fillerModulus[TN]; |
| 363 | |
| 364 | delete state->fillerTable[modFN][TN]; |
| 365 | state->fillerTable[modFN][TN] = burst->getVector(); |
| 366 | burst->setVector(NULL); |
| 367 | } |
| 368 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 369 | void Transceiver::pushRadioVector(GSM::Time &nowTime) |
| 370 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 371 | int TN, modFN; |
| 372 | radioVector *burst; |
| 373 | TransceiverState *state; |
| 374 | std::vector<signalVector *> bursts(mChans); |
| 375 | std::vector<bool> zeros(mChans); |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 376 | std::vector<bool> filler(mChans, true); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 377 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 378 | for (size_t i = 0; i < mChans; i ++) { |
| 379 | state = &mStates[i]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 380 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 381 | while ((burst = mTxPriorityQueues[i].getStaleBurst(nowTime))) { |
| 382 | LOG(NOTICE) << "dumping STALE burst in TRX->USRP interface"; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 383 | if (state->mRetrans) |
| 384 | updateFillerTable(i, burst); |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 385 | delete burst; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | TN = nowTime.TN(); |
| 389 | modFN = nowTime.FN() % state->fillerModulus[TN]; |
| 390 | |
| 391 | bursts[i] = state->fillerTable[modFN][TN]; |
| 392 | zeros[i] = state->chanType[TN] == NONE; |
| 393 | |
| 394 | if ((burst = mTxPriorityQueues[i].getCurrentBurst(nowTime))) { |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 395 | bursts[i] = burst->getVector(); |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 396 | |
| 397 | if (state->mRetrans) { |
| 398 | updateFillerTable(i, burst); |
| 399 | } else { |
| 400 | burst->setVector(NULL); |
| 401 | filler[i] = false; |
| 402 | } |
| 403 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 404 | delete burst; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 405 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 408 | mRadioInterface->driveTransmitRadio(bursts, zeros); |
| 409 | |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 410 | for (size_t i = 0; i < mChans; i++) { |
| 411 | if (!filler[i]) |
| 412 | delete bursts[i]; |
| 413 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 416 | void Transceiver::setModulus(size_t timeslot, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 417 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 418 | TransceiverState *state = &mStates[chan]; |
| 419 | |
| 420 | switch (state->chanType[timeslot]) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 421 | case NONE: |
| 422 | case I: |
| 423 | case II: |
| 424 | case III: |
| 425 | case FILL: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 426 | state->fillerModulus[timeslot] = 26; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 427 | break; |
| 428 | case IV: |
| 429 | case VI: |
| 430 | case V: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 431 | state->fillerModulus[timeslot] = 51; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 432 | break; |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 433 | //case V: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 434 | case VII: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 435 | state->fillerModulus[timeslot] = 102; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 436 | break; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 437 | case XIII: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 438 | state->fillerModulus[timeslot] = 52; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 439 | break; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 440 | default: |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | |
Alexander Chemeris | f9e78be | 2017-03-17 15:00:34 -0700 | [diff] [blame] | 446 | CorrType Transceiver::expectedCorrType(GSM::Time currTime, |
| 447 | size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 448 | { |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 449 | static int tchh_subslot[26] = { 0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,1 }; |
| 450 | static int sdcch4_subslot[102] = { 3,3,3,3,0,0,2,2,2,2,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,2,2,2,2, |
| 451 | 3,3,3,3,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,2,2,2,2 }; |
| 452 | static int sdcch8_subslot[102] = { 5,5,5,5,6,6,6,6,7,7,7,7,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,0,0,0,0, |
| 453 | 1,1,1,1,2,2,2,2,3,3,3,3,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,4,4,4,4 }; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 454 | TransceiverState *state = &mStates[chan]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 455 | unsigned burstTN = currTime.TN(); |
| 456 | unsigned burstFN = currTime.FN(); |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 457 | int subch; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 458 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 459 | switch (state->chanType[burstTN]) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 460 | case NONE: |
| 461 | return OFF; |
| 462 | break; |
| 463 | case FILL: |
| 464 | return IDLE; |
| 465 | break; |
| 466 | case I: |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 467 | // TODO: Are we expecting RACH on an IDLE frame? |
| 468 | /* if (burstFN % 26 == 25) |
| 469 | return IDLE;*/ |
| 470 | if (mHandover[burstTN][0]) |
| 471 | return RACH; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 472 | return TSC; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 473 | break; |
| 474 | case II: |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 475 | subch = tchh_subslot[burstFN % 26]; |
| 476 | if (subch == 1) |
| 477 | return IDLE; |
| 478 | if (mHandover[burstTN][0]) |
| 479 | return RACH; |
ttsou | 2064297 | 2013-03-27 22:00:25 +0000 | [diff] [blame] | 480 | return TSC; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 481 | break; |
| 482 | case III: |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 483 | subch = tchh_subslot[burstFN % 26]; |
| 484 | if (mHandover[burstTN][subch]) |
| 485 | return RACH; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 486 | return TSC; |
| 487 | break; |
| 488 | case IV: |
| 489 | case VI: |
| 490 | return RACH; |
| 491 | break; |
| 492 | case V: { |
| 493 | int mod51 = burstFN % 51; |
| 494 | if ((mod51 <= 36) && (mod51 >= 14)) |
| 495 | return RACH; |
| 496 | else if ((mod51 == 4) || (mod51 == 5)) |
| 497 | return RACH; |
| 498 | else if ((mod51 == 45) || (mod51 == 46)) |
| 499 | return RACH; |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 500 | else if (mHandover[burstTN][sdcch4_subslot[burstFN % 102]]) |
| 501 | return RACH; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 502 | else |
| 503 | return TSC; |
| 504 | break; |
| 505 | } |
| 506 | case VII: |
| 507 | if ((burstFN % 51 <= 14) && (burstFN % 51 >= 12)) |
| 508 | return IDLE; |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 509 | else if (mHandover[burstTN][sdcch8_subslot[burstFN % 102]]) |
| 510 | return RACH; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 511 | else |
| 512 | return TSC; |
| 513 | break; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 514 | case XIII: { |
| 515 | int mod52 = burstFN % 52; |
| 516 | if ((mod52 == 12) || (mod52 == 38)) |
| 517 | return RACH; |
| 518 | else if ((mod52 == 25) || (mod52 == 51)) |
| 519 | return IDLE; |
| 520 | else |
| 521 | return TSC; |
| 522 | break; |
| 523 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 524 | case LOOPBACK: |
| 525 | if ((burstFN % 51 <= 50) && (burstFN % 51 >=48)) |
| 526 | return IDLE; |
| 527 | else |
| 528 | return TSC; |
| 529 | break; |
| 530 | default: |
| 531 | return OFF; |
| 532 | break; |
| 533 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 534 | } |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 535 | |
Alexander Chemeris | e692ce9 | 2015-06-12 00:15:31 -0400 | [diff] [blame] | 536 | void writeToFile(radioVector *radio_burst, size_t chan) |
| 537 | { |
| 538 | GSM::Time time = radio_burst->getTime(); |
| 539 | std::ostringstream fname; |
| 540 | fname << chan << "_" << time.FN() << "_" << time.TN() << ".fc"; |
| 541 | std::ofstream outfile (fname.str().c_str(), std::ofstream::binary); |
| 542 | outfile.write((char*)radio_burst->getVector()->begin(), radio_burst->getVector()->size() * 2 * sizeof(float)); |
| 543 | outfile.close(); |
| 544 | } |
| 545 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 546 | /* |
| 547 | * Pull bursts from the FIFO and handle according to the slot |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 548 | * and burst correlation type. Equalzation is currently disabled. |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 549 | */ |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 550 | SoftVector *Transceiver::pullRadioVector(GSM::Time &wTime, double &RSSI, bool &isRssiValid, |
Alexander Chemeris | dbe26ab | 2015-06-04 00:14:51 -0400 | [diff] [blame] | 551 | double &timingOffset, double &noise, |
| 552 | size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 553 | { |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 554 | int rc; |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 555 | complex amp; |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 556 | float toa, max = -1.0, avg = 0.0; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 557 | int max_i = -1; |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 558 | signalVector *burst; |
Thomas Tsou | ef25dba | 2013-11-14 15:31:24 -0500 | [diff] [blame] | 559 | SoftVector *bits = NULL; |
Thomas Tsou | a0179e3 | 2013-11-14 15:52:04 -0500 | [diff] [blame] | 560 | TransceiverState *state = &mStates[chan]; |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 561 | isRssiValid = false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 562 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 563 | /* Blocking FIFO read */ |
| 564 | radioVector *radio_burst = mReceiveFIFO[chan]->read(); |
| 565 | if (!radio_burst) |
| 566 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 567 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 568 | /* Set time and determine correlation type */ |
| 569 | GSM::Time time = radio_burst->getTime(); |
| 570 | CorrType type = expectedCorrType(time, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 571 | |
Tom Tsou | 64464e6 | 2016-07-01 03:46:46 -0700 | [diff] [blame] | 572 | /* Enable 8-PSK burst detection if EDGE is enabled */ |
| 573 | if (mEdge && (type == TSC)) |
| 574 | type = EDGE; |
| 575 | |
Alexander Chemeris | e692ce9 | 2015-06-12 00:15:31 -0400 | [diff] [blame] | 576 | /* Debug: dump bursts to disk */ |
| 577 | /* bits 0-7 - chan 0 timeslots |
| 578 | * bits 8-15 - chan 1 timeslots */ |
| 579 | if (mWriteBurstToDiskMask & ((1<<time.TN()) << (8*chan))) |
| 580 | writeToFile(radio_burst, chan); |
| 581 | |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 582 | /* No processing if the timeslot is off. |
| 583 | * Not even power level or noise calculation. */ |
| 584 | if (type == OFF) { |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 585 | delete radio_burst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 586 | return NULL; |
| 587 | } |
kurtis.heimerl | 3ed6fb7 | 2011-11-26 03:17:52 +0000 | [diff] [blame] | 588 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 589 | /* Select the diversity channel with highest energy */ |
| 590 | for (size_t i = 0; i < radio_burst->chans(); i++) { |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 591 | float pow = energyDetect(*radio_burst->getVector(i), 20 * mSPSRx); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 592 | if (pow > max) { |
| 593 | max = pow; |
| 594 | max_i = i; |
| 595 | } |
| 596 | avg += pow; |
| 597 | } |
| 598 | |
| 599 | if (max_i < 0) { |
| 600 | LOG(ALERT) << "Received empty burst"; |
| 601 | delete radio_burst; |
| 602 | return NULL; |
| 603 | } |
| 604 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 605 | /* Average noise on diversity paths and update global levels */ |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 606 | burst = radio_burst->getVector(max_i); |
Thomas Tsou | ef25dba | 2013-11-14 15:31:24 -0500 | [diff] [blame] | 607 | avg = sqrt(avg / radio_burst->chans()); |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 608 | |
| 609 | wTime = time; |
| 610 | RSSI = 20.0 * log10(rxFullScale / avg); |
| 611 | |
| 612 | /* RSSI estimation are valid */ |
| 613 | isRssiValid = true; |
| 614 | |
| 615 | if (type == IDLE) { |
| 616 | /* Update noise levels */ |
| 617 | state->mNoises.insert(avg); |
| 618 | state->mNoiseLev = state->mNoises.avg(); |
| 619 | noise = 20.0 * log10(rxFullScale / state->mNoiseLev); |
| 620 | |
| 621 | delete radio_burst; |
| 622 | return NULL; |
| 623 | } else { |
| 624 | /* Do not update noise levels */ |
| 625 | noise = 20.0 * log10(rxFullScale / state->mNoiseLev); |
| 626 | } |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 627 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 628 | /* Detect normal or RACH bursts */ |
Alexander Chemeris | 4e6c938 | 2017-03-17 15:24:18 -0700 | [diff] [blame] | 629 | rc = detectAnyBurst(*burst, mTSC, BURST_THRESH, mSPSRx, type, amp, toa, |
| 630 | (type==RACH)?mMaxExpectedDelayAB:mMaxExpectedDelayNB); |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 631 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 632 | if (rc > 0) { |
| 633 | type = (CorrType) rc; |
| 634 | } else if (rc <= 0) { |
| 635 | if (rc == -SIGERR_CLIP) { |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 636 | LOG(WARNING) << "Clipping detected on received RACH or Normal Burst"; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 637 | } else if (rc != SIGERR_NONE) { |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 638 | LOG(WARNING) << "Unhandled RACH or Normal Burst detection error"; |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 641 | delete radio_burst; |
| 642 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 643 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 644 | |
Tom Tsou | 4609f32 | 2016-07-19 11:28:51 -0700 | [diff] [blame] | 645 | timingOffset = toa; |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 646 | |
Alexander Chemeris | 6e1dffd | 2017-03-17 16:13:51 -0700 | [diff] [blame] | 647 | bits = demodAnyBurst(*burst, mSPSRx, amp, toa, type); |
Thomas Tsou | ef25dba | 2013-11-14 15:31:24 -0500 | [diff] [blame] | 648 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 649 | delete radio_burst; |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame] | 650 | return bits; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 651 | } |
| 652 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 653 | void Transceiver::reset() |
| 654 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 655 | for (size_t i = 0; i < mTxPriorityQueues.size(); i++) |
| 656 | mTxPriorityQueues[i].clear(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 659 | |
Vadim Yanitskiy | bd0efb0 | 2018-03-09 02:45:07 +0700 | [diff] [blame^] | 660 | #define MAX_PACKET_LENGTH 100 |
| 661 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 662 | void Transceiver::driveControl(size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 663 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 664 | // check control socket |
| 665 | char buffer[MAX_PACKET_LENGTH]; |
| 666 | int msgLen = -1; |
| 667 | buffer[0] = '\0'; |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 668 | |
Tom Tsou | 2c650a6 | 2016-04-28 21:55:17 -0700 | [diff] [blame] | 669 | msgLen = mCtrlSockets[chan]->read(buffer, sizeof(buffer)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 670 | |
| 671 | if (msgLen < 1) { |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | char cmdcheck[4]; |
| 676 | char command[MAX_PACKET_LENGTH]; |
| 677 | char response[MAX_PACKET_LENGTH]; |
| 678 | |
| 679 | sscanf(buffer,"%3s %s",cmdcheck,command); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 680 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 681 | if (strcmp(cmdcheck,"CMD")!=0) { |
| 682 | LOG(WARNING) << "bogus message on control interface"; |
| 683 | return; |
| 684 | } |
| 685 | LOG(INFO) << "command is " << buffer; |
| 686 | |
| 687 | if (strcmp(command,"POWEROFF")==0) { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 688 | stop(); |
| 689 | sprintf(response,"RSP POWEROFF 0"); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 690 | } |
| 691 | else if (strcmp(command,"POWERON")==0) { |
Tom Tsou | 365bc38 | 2016-10-19 15:26:04 -0700 | [diff] [blame] | 692 | if (!start()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 693 | sprintf(response,"RSP POWERON 1"); |
Tom Tsou | 365bc38 | 2016-10-19 15:26:04 -0700 | [diff] [blame] | 694 | } else { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 695 | sprintf(response,"RSP POWERON 0"); |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 696 | for (int i = 0; i < 8; i++) { |
| 697 | for (int j = 0; j < 8; j++) |
| 698 | mHandover[i][j] = false; |
| 699 | } |
Tom Tsou | 365bc38 | 2016-10-19 15:26:04 -0700 | [diff] [blame] | 700 | } |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 701 | } |
| 702 | else if (strcmp(command,"HANDOVER")==0){ |
| 703 | int ts=0,ss=0; |
| 704 | sscanf(buffer,"%3s %s %d %d",cmdcheck,command,&ts,&ss); |
| 705 | mHandover[ts][ss] = true; |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 706 | sprintf(response,"RSP HANDOVER 0 %d %d",ts,ss); |
| 707 | } |
| 708 | else if (strcmp(command,"NOHANDOVER")==0){ |
| 709 | int ts=0,ss=0; |
| 710 | sscanf(buffer,"%3s %s %d %d",cmdcheck,command,&ts,&ss); |
| 711 | mHandover[ts][ss] = false; |
Alexander Chemeris | 5a06806 | 2015-06-20 01:38:47 +0300 | [diff] [blame] | 712 | sprintf(response,"RSP NOHANDOVER 0 %d %d",ts,ss); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 713 | } |
| 714 | else if (strcmp(command,"SETMAXDLY")==0) { |
| 715 | //set expected maximum time-of-arrival |
| 716 | int maxDelay; |
| 717 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&maxDelay); |
Alexander Chemeris | 78d1fc9 | 2016-03-19 21:16:22 +0300 | [diff] [blame] | 718 | mMaxExpectedDelayAB = maxDelay; // 1 GSM symbol is approx. 1 km |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 719 | sprintf(response,"RSP SETMAXDLY 0 %d",maxDelay); |
| 720 | } |
Alexander Chemeris | 78d1fc9 | 2016-03-19 21:16:22 +0300 | [diff] [blame] | 721 | else if (strcmp(command,"SETMAXDLYNB")==0) { |
| 722 | //set expected maximum time-of-arrival |
| 723 | int maxDelay; |
| 724 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&maxDelay); |
| 725 | mMaxExpectedDelayNB = maxDelay; // 1 GSM symbol is approx. 1 km |
| 726 | sprintf(response,"RSP SETMAXDLYNB 0 %d",maxDelay); |
| 727 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 728 | else if (strcmp(command,"SETRXGAIN")==0) { |
| 729 | //set expected maximum time-of-arrival |
| 730 | int newGain; |
| 731 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&newGain); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 732 | newGain = mRadioInterface->setRxGain(newGain, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 733 | sprintf(response,"RSP SETRXGAIN 0 %d",newGain); |
| 734 | } |
| 735 | else if (strcmp(command,"NOISELEV")==0) { |
| 736 | if (mOn) { |
Thomas Tsou | a0179e3 | 2013-11-14 15:52:04 -0500 | [diff] [blame] | 737 | float lev = mStates[chan].mNoiseLev; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 738 | sprintf(response,"RSP NOISELEV 0 %d", |
Thomas Tsou | a0179e3 | 2013-11-14 15:52:04 -0500 | [diff] [blame] | 739 | (int) round(20.0 * log10(rxFullScale / lev))); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 740 | } |
| 741 | else { |
| 742 | sprintf(response,"RSP NOISELEV 1 0"); |
| 743 | } |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 744 | } |
| 745 | else if (!strcmp(command, "SETPOWER")) { |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 746 | int power; |
| 747 | sscanf(buffer, "%3s %s %d", cmdcheck, command, &power); |
| 748 | power = mRadioInterface->setPowerAttenuation(power, chan); |
| 749 | mStates[chan].mPower = power; |
| 750 | sprintf(response, "RSP SETPOWER 0 %d", power); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 751 | } |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 752 | else if (!strcmp(command,"ADJPOWER")) { |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 753 | int power, step; |
| 754 | sscanf(buffer, "%3s %s %d", cmdcheck, command, &step); |
| 755 | power = mStates[chan].mPower + step; |
| 756 | power = mRadioInterface->setPowerAttenuation(power, chan); |
| 757 | mStates[chan].mPower = power; |
| 758 | sprintf(response, "RSP ADJPOWER 0 %d", power); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 759 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 760 | else if (strcmp(command,"RXTUNE")==0) { |
| 761 | // tune receiver |
| 762 | int freqKhz; |
| 763 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&freqKhz); |
Thomas Tsou | 477b77c | 2013-11-15 16:13:59 -0500 | [diff] [blame] | 764 | mRxFreq = freqKhz * 1e3; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 765 | if (!mRadioInterface->tuneRx(mRxFreq, chan)) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 766 | LOG(ALERT) << "RX failed to tune"; |
| 767 | sprintf(response,"RSP RXTUNE 1 %d",freqKhz); |
| 768 | } |
| 769 | else |
| 770 | sprintf(response,"RSP RXTUNE 0 %d",freqKhz); |
| 771 | } |
| 772 | else if (strcmp(command,"TXTUNE")==0) { |
| 773 | // tune txmtr |
| 774 | int freqKhz; |
| 775 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&freqKhz); |
Thomas Tsou | 477b77c | 2013-11-15 16:13:59 -0500 | [diff] [blame] | 776 | mTxFreq = freqKhz * 1e3; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 777 | if (!mRadioInterface->tuneTx(mTxFreq, chan)) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 778 | LOG(ALERT) << "TX failed to tune"; |
| 779 | sprintf(response,"RSP TXTUNE 1 %d",freqKhz); |
| 780 | } |
| 781 | else |
| 782 | sprintf(response,"RSP TXTUNE 0 %d",freqKhz); |
| 783 | } |
Thomas Tsou | d3fccea | 2013-11-15 14:22:53 -0500 | [diff] [blame] | 784 | else if (!strcmp(command,"SETTSC")) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 785 | // set TSC |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 786 | unsigned TSC; |
Thomas Tsou | d3fccea | 2013-11-15 14:22:53 -0500 | [diff] [blame] | 787 | sscanf(buffer, "%3s %s %d", cmdcheck, command, &TSC); |
Tom Tsou | 6031734 | 2017-03-30 19:36:41 -0700 | [diff] [blame] | 788 | if (TSC > 7) { |
Thomas Tsou | d3fccea | 2013-11-15 14:22:53 -0500 | [diff] [blame] | 789 | sprintf(response, "RSP SETTSC 1 %d", TSC); |
Tom Tsou | 6031734 | 2017-03-30 19:36:41 -0700 | [diff] [blame] | 790 | } else { |
Tom Tsou | c37594f | 2016-07-08 14:39:42 -0700 | [diff] [blame] | 791 | LOG(NOTICE) << "Changing TSC from " << mTSC << " to " << TSC; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 792 | mTSC = TSC; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 793 | sprintf(response,"RSP SETTSC 0 %d", TSC); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | else if (strcmp(command,"SETSLOT")==0) { |
Alexander Chemeris | 1fe5282 | 2015-05-20 12:02:29 -0700 | [diff] [blame] | 797 | // set slot type |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 798 | int corrCode; |
| 799 | int timeslot; |
| 800 | sscanf(buffer,"%3s %s %d %d",cmdcheck,command,×lot,&corrCode); |
| 801 | if ((timeslot < 0) || (timeslot > 7)) { |
| 802 | LOG(WARNING) << "bogus message on control interface"; |
| 803 | sprintf(response,"RSP SETSLOT 1 %d %d",timeslot,corrCode); |
| 804 | return; |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 805 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 806 | mStates[chan].chanType[timeslot] = (ChannelCombination) corrCode; |
| 807 | setModulus(timeslot, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 808 | sprintf(response,"RSP SETSLOT 0 %d %d",timeslot,corrCode); |
| 809 | |
| 810 | } |
Alexander Chemeris | e692ce9 | 2015-06-12 00:15:31 -0400 | [diff] [blame] | 811 | else if (strcmp(command,"_SETBURSTTODISKMASK")==0) { |
| 812 | // debug command! may change or disapear without notice |
| 813 | // set a mask which bursts to dump to disk |
| 814 | int mask; |
| 815 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&mask); |
| 816 | mWriteBurstToDiskMask = mask; |
| 817 | sprintf(response,"RSP _SETBURSTTODISKMASK 0 %d",mask); |
| 818 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 819 | else { |
| 820 | LOG(WARNING) << "bogus command " << command << " on control interface."; |
Alexander Chemeris | 4438a9f | 2013-04-08 00:14:08 +0200 | [diff] [blame] | 821 | sprintf(response,"RSP ERR 1"); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 824 | mCtrlSockets[chan]->write(response, strlen(response) + 1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 827 | bool Transceiver::driveTxPriorityQueue(size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 828 | { |
Tom Tsou | e887108 | 2016-07-01 02:46:04 -0700 | [diff] [blame] | 829 | int burstLen; |
| 830 | char buffer[EDGE_BURST_NBITS + 50]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 831 | |
| 832 | // check data socket |
Tom Tsou | 2c650a6 | 2016-04-28 21:55:17 -0700 | [diff] [blame] | 833 | size_t msgLen = mDataSockets[chan]->read(buffer, sizeof(buffer)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 834 | |
Tom Tsou | e887108 | 2016-07-01 02:46:04 -0700 | [diff] [blame] | 835 | if (msgLen == gSlotLen + 1 + 4 + 1) { |
| 836 | burstLen = gSlotLen; |
| 837 | } else if (msgLen == EDGE_BURST_NBITS + 1 + 4 + 1) { |
| 838 | if (mSPSTx != 4) |
| 839 | return false; |
| 840 | |
| 841 | burstLen = EDGE_BURST_NBITS; |
| 842 | } else { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 843 | LOG(ERR) << "badly formatted packet on GSM->TRX interface"; |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | int timeSlot = (int) buffer[0]; |
| 848 | uint64_t frameNum = 0; |
| 849 | for (int i = 0; i < 4; i++) |
| 850 | frameNum = (frameNum << 8) | (0x0ff & buffer[i+1]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 851 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 852 | LOG(DEBUG) << "rcvd. burst at: " << GSM::Time(frameNum,timeSlot); |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 853 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 854 | int RSSI = (int) buffer[5]; |
Tom Tsou | 7c741ec | 2016-07-19 11:20:59 -0700 | [diff] [blame] | 855 | BitVector newBurst(burstLen); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 856 | BitVector::iterator itr = newBurst.begin(); |
| 857 | char *bufferItr = buffer+6; |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 858 | while (itr < newBurst.end()) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 859 | *itr++ = *bufferItr++; |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 860 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 861 | GSM::Time currTime = GSM::Time(frameNum,timeSlot); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 862 | |
| 863 | addRadioVector(chan, newBurst, RSSI, currTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 864 | |
| 865 | return true; |
| 866 | |
| 867 | |
| 868 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 869 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 870 | void Transceiver::driveReceiveRadio() |
| 871 | { |
Alexander Chemeris | 6a2bf0d | 2015-05-24 19:28:09 -0400 | [diff] [blame] | 872 | if (!mRadioInterface->driveReceiveRadio()) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 873 | usleep(100000); |
Pau Espin Pedrol | 934da48 | 2017-07-04 16:25:20 +0200 | [diff] [blame] | 874 | } else if (mForceClockInterface || mTransmitDeadlineClock > mLastClockUpdateTime + GSM::Time(216,0)) { |
| 875 | mForceClockInterface = false; |
| 876 | writeClockInterface(); |
Alexander Chemeris | 6a2bf0d | 2015-05-24 19:28:09 -0400 | [diff] [blame] | 877 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 878 | } |
| 879 | |
Alexander Chemeris | 58e9591 | 2016-03-25 18:20:28 +0300 | [diff] [blame] | 880 | void Transceiver::logRxBurst(size_t chan, SoftVector *burst, GSM::Time time, double dbm, |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 881 | double rssi, double noise, double toa) |
| 882 | { |
| 883 | LOG(DEBUG) << std::fixed << std::right |
Alexander Chemeris | 58e9591 | 2016-03-25 18:20:28 +0300 | [diff] [blame] | 884 | << " chan: " << chan |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 885 | << " time: " << time |
| 886 | << " RSSI: " << std::setw(5) << std::setprecision(1) << rssi |
| 887 | << "dBFS/" << std::setw(6) << -dbm << "dBm" |
| 888 | << " noise: " << std::setw(5) << std::setprecision(1) << noise |
| 889 | << "dBFS/" << std::setw(6) << -(noise + rssiOffset) << "dBm" |
| 890 | << " TOA: " << std::setw(5) << std::setprecision(2) << toa |
| 891 | << " bits: " << *burst; |
| 892 | } |
| 893 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 894 | void Transceiver::driveReceiveFIFO(size_t chan) |
| 895 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 896 | SoftVector *rxBurst = NULL; |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 897 | double RSSI; // in dBFS |
| 898 | double dBm; // in dBm |
| 899 | double TOA; // in symbols |
| 900 | int TOAint; // in 1/256 symbols |
Alexander Chemeris | dbe26ab | 2015-06-04 00:14:51 -0400 | [diff] [blame] | 901 | double noise; // noise level in dBFS |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 902 | GSM::Time burstTime; |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 903 | bool isRssiValid; // are RSSI, noise and burstTime valid |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 904 | unsigned nbits = gSlotLen; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 905 | |
Alexander Chemeris | 2b54210 | 2015-06-08 22:46:38 -0400 | [diff] [blame] | 906 | rxBurst = pullRadioVector(burstTime, RSSI, isRssiValid, TOA, noise, chan); |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 907 | if (!rxBurst) |
| 908 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 909 | |
Alexander Chemeris | b61c610 | 2017-03-17 18:22:19 -0700 | [diff] [blame] | 910 | // Convert -1..+1 soft bits to 0..1 soft bits |
| 911 | vectorSlicer(rxBurst); |
| 912 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 913 | /* |
| 914 | * EDGE demodulator returns 444 (148 * 3) bits |
| 915 | */ |
| 916 | if (rxBurst->size() == gSlotLen * 3) |
| 917 | nbits = gSlotLen * 3; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 918 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 919 | dBm = RSSI + rssiOffset; |
Alexander Chemeris | 58e9591 | 2016-03-25 18:20:28 +0300 | [diff] [blame] | 920 | logRxBurst(chan, rxBurst, burstTime, dBm, RSSI, noise, TOA); |
Alexander Chemeris | dbe26ab | 2015-06-04 00:14:51 -0400 | [diff] [blame] | 921 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 922 | TOAint = (int) (TOA * 256.0 + 0.5); // round to closest integer |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 923 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 924 | char burstString[nbits + 10]; |
| 925 | burstString[0] = burstTime.TN(); |
| 926 | for (int i = 0; i < 4; i++) |
| 927 | burstString[1+i] = (burstTime.FN() >> ((3-i)*8)) & 0x0ff; |
| 928 | burstString[5] = (int)dBm; |
| 929 | burstString[6] = (TOAint >> 8) & 0x0ff; |
| 930 | burstString[7] = TOAint & 0x0ff; |
| 931 | SoftVector::iterator burstItr = rxBurst->begin(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 932 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 933 | for (unsigned i = 0; i < nbits; i++) |
| 934 | burstString[8 + i] = (char) round((*burstItr++) * 255.0); |
| 935 | |
| 936 | burstString[nbits + 9] = '\0'; |
| 937 | delete rxBurst; |
| 938 | |
| 939 | mDataSockets[chan]->write(burstString, nbits + 10); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 942 | void Transceiver::driveTxFIFO() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 943 | { |
| 944 | |
| 945 | /** |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 946 | Features a carefully controlled latency mechanism, to |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 947 | assure that transmit packets arrive at the radio/USRP |
| 948 | before they need to be transmitted. |
| 949 | |
| 950 | Deadline clock indicates the burst that needs to be |
| 951 | pushed into the FIFO right NOW. If transmit queue does |
| 952 | not have a burst, stick in filler data. |
| 953 | */ |
| 954 | |
| 955 | |
| 956 | RadioClock *radioClock = (mRadioInterface->getClock()); |
Pau Espin Pedrol | 7c405a0 | 2017-07-04 16:24:06 +0200 | [diff] [blame] | 957 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 958 | if (mOn) { |
| 959 | //radioClock->wait(); // wait until clock updates |
| 960 | LOG(DEBUG) << "radio clock " << radioClock->get(); |
| 961 | while (radioClock->get() + mTransmitLatency > mTransmitDeadlineClock) { |
| 962 | // if underrun, then we're not providing bursts to radio/USRP fast |
| 963 | // enough. Need to increase latency by one GSM frame. |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 964 | if (mRadioInterface->getWindowType() == RadioDevice::TX_WINDOW_USRP1) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 965 | if (mRadioInterface->isUnderrun()) { |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 966 | // only update latency at the defined frame interval |
| 967 | if (radioClock->get() > mLatencyUpdateTime + GSM::Time(USB_LATENCY_INTRVL)) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 968 | mTransmitLatency = mTransmitLatency + GSM::Time(1,0); |
| 969 | LOG(INFO) << "new latency: " << mTransmitLatency; |
| 970 | mLatencyUpdateTime = radioClock->get(); |
| 971 | } |
| 972 | } |
| 973 | else { |
| 974 | // if underrun hasn't occurred in the last sec (216 frames) drop |
| 975 | // transmit latency by a timeslot |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 976 | if (mTransmitLatency > GSM::Time(USB_LATENCY_MIN)) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 977 | if (radioClock->get() > mLatencyUpdateTime + GSM::Time(216,0)) { |
| 978 | mTransmitLatency.decTN(); |
| 979 | LOG(INFO) << "reduced latency: " << mTransmitLatency; |
| 980 | mLatencyUpdateTime = radioClock->get(); |
| 981 | } |
| 982 | } |
| 983 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 984 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 985 | // time to push burst to transmit FIFO |
| 986 | pushRadioVector(mTransmitDeadlineClock); |
| 987 | mTransmitDeadlineClock.incTN(); |
| 988 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 989 | } |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 990 | |
| 991 | radioClock->wait(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | |
| 995 | |
| 996 | void Transceiver::writeClockInterface() |
| 997 | { |
| 998 | char command[50]; |
| 999 | // FIXME -- This should be adaptive. |
| 1000 | sprintf(command,"IND CLOCK %llu",(unsigned long long) (mTransmitDeadlineClock.FN()+2)); |
| 1001 | |
| 1002 | LOG(INFO) << "ClockInterface: sending " << command; |
| 1003 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 1004 | mClockSocket.write(command, strlen(command) + 1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1005 | |
| 1006 | mLastClockUpdateTime = mTransmitDeadlineClock; |
| 1007 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 1008 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1009 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1010 | void *RxUpperLoopAdapter(TransceiverChannel *chan) |
| 1011 | { |
| 1012 | Transceiver *trx = chan->trx; |
| 1013 | size_t num = chan->num; |
| 1014 | |
| 1015 | delete chan; |
| 1016 | |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 1017 | trx->setPriority(0.42); |
| 1018 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1019 | while (1) { |
| 1020 | trx->driveReceiveFIFO(num); |
| 1021 | pthread_testcancel(); |
| 1022 | } |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | |
| 1026 | void *RxLowerLoopAdapter(Transceiver *transceiver) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1027 | { |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 1028 | transceiver->setPriority(0.45); |
kurtis.heimerl | 6b495a5 | 2011-11-26 03:17:21 +0000 | [diff] [blame] | 1029 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1030 | while (1) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1031 | transceiver->driveReceiveRadio(); |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 1032 | pthread_testcancel(); |
| 1033 | } |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1037 | void *TxLowerLoopAdapter(Transceiver *transceiver) |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 1038 | { |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 1039 | transceiver->setPriority(0.44); |
| 1040 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 1041 | while (1) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1042 | transceiver->driveTxFIFO(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1043 | pthread_testcancel(); |
| 1044 | } |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1048 | void *ControlServiceLoopAdapter(TransceiverChannel *chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1049 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1050 | Transceiver *trx = chan->trx; |
| 1051 | size_t num = chan->num; |
| 1052 | |
| 1053 | delete chan; |
| 1054 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1055 | while (1) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1056 | trx->driveControl(num); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1057 | pthread_testcancel(); |
| 1058 | } |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1062 | void *TxUpperLoopAdapter(TransceiverChannel *chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1063 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1064 | Transceiver *trx = chan->trx; |
| 1065 | size_t num = chan->num; |
| 1066 | |
| 1067 | delete chan; |
| 1068 | |
Thomas Tsou | a4cf48c | 2013-11-09 21:44:26 -0500 | [diff] [blame] | 1069 | trx->setPriority(0.40); |
| 1070 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1071 | while (1) { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 1072 | trx->driveTxPriorityQueue(num); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1073 | pthread_testcancel(); |
| 1074 | } |
| 1075 | return NULL; |
| 1076 | } |