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