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