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> |
| 25 | #include "Transceiver.h" |
| 26 | #include <Logger.h> |
| 27 | |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 31 | |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 32 | using namespace GSM; |
| 33 | |
kurtis.heimerl | ec842de | 2012-11-23 08:37:32 +0000 | [diff] [blame] | 34 | #define USB_LATENCY_INTRVL 10,0 |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 35 | |
| 36 | #if USE_UHD |
kurtis.heimerl | ec842de | 2012-11-23 08:37:32 +0000 | [diff] [blame] | 37 | # define USB_LATENCY_MIN 6,7 |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 38 | #else |
kurtis.heimerl | ec842de | 2012-11-23 08:37:32 +0000 | [diff] [blame] | 39 | # define USB_LATENCY_MIN 1,1 |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 40 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 41 | |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 42 | /* Number of running values use in noise average */ |
| 43 | #define NOISE_CNT 20 |
ttsou | e8dde02 | 2012-12-06 15:43:55 +0000 | [diff] [blame] | 44 | |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 45 | TransceiverState::TransceiverState() |
| 46 | { |
| 47 | for (int i = 0; i < 8; i++) { |
| 48 | chanType[i] = Transceiver::NONE; |
| 49 | fillerModulus[i] = 26; |
| 50 | chanResponse[i] = NULL; |
| 51 | DFEForward[i] = NULL; |
| 52 | DFEFeedback[i] = NULL; |
| 53 | |
| 54 | for (int n = 0; n < 102; n++) |
| 55 | fillerTable[n][i] = NULL; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | TransceiverState::~TransceiverState() |
| 60 | { |
| 61 | for (int i = 0; i < 8; i++) { |
| 62 | delete chanResponse[i]; |
| 63 | delete DFEForward[i]; |
| 64 | delete DFEFeedback[i]; |
| 65 | |
| 66 | for (int n = 0; n < 102; n++) |
| 67 | delete fillerTable[n][i]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void TransceiverState::init(size_t slot, signalVector *burst) |
| 72 | { |
| 73 | for (int i = 0; i < 102; i++) |
| 74 | fillerTable[i][slot] = new signalVector(*burst); |
| 75 | } |
| 76 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 77 | Transceiver::Transceiver(int wBasePort, |
| 78 | const char *TRXAddress, |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 79 | size_t wSPS, size_t wChans, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 80 | GSM::Time wTransmitLatency, |
| 81 | RadioInterface *wRadioInterface) |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 82 | : mBasePort(wBasePort), mAddr(TRXAddress), |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 83 | mTransmitLatency(wTransmitLatency), mClockSocket(NULL), mRadioInterface(wRadioInterface), |
| 84 | mNoiseLev(0.0), mNoises(NOISE_CNT), mSPSTx(wSPS), mSPSRx(1), mChans(wChans), |
| 85 | mOn(false), mTxFreq(0.0), mRxFreq(0.0), mPower(-10), mMaxExpectedDelay(0) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 86 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 87 | GSM::Time startTime(random() % gHyperframe,0); |
| 88 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 89 | mRxLowerLoopThread = new Thread(32768); |
| 90 | mTxLowerLoopThread = new Thread(32768); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 91 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 92 | mTransmitDeadlineClock = startTime; |
| 93 | mLastClockUpdateTime = startTime; |
| 94 | mLatencyUpdateTime = startTime; |
| 95 | mRadioInterface->getClock()->set(startTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 96 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 97 | txFullScale = mRadioInterface->fullScaleInputValue(); |
| 98 | rxFullScale = mRadioInterface->fullScaleOutputValue(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | Transceiver::~Transceiver() |
| 102 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 103 | sigProcLibDestroy(); |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 104 | |
| 105 | delete mClockSocket; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 106 | |
| 107 | for (size_t i = 0; i < mChans; i++) { |
| 108 | mTxPriorityQueues[i].clear(); |
| 109 | delete mCtrlSockets[i]; |
| 110 | delete mDataSockets[i]; |
| 111 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 112 | } |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 113 | |
| 114 | bool Transceiver::init() |
| 115 | { |
Thomas Tsou | e1ce925 | 2013-11-13 22:40:44 -0500 | [diff] [blame] | 116 | int d_srcport, d_dstport, c_srcport, c_dstport; |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 117 | signalVector *burst; |
| 118 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 119 | if (!mChans) { |
| 120 | LOG(ALERT) << "No channels assigned"; |
| 121 | return false; |
| 122 | } |
| 123 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 124 | if (!sigProcLibSetup(mSPSTx)) { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 125 | LOG(ALERT) << "Failed to initialize signal processing library"; |
| 126 | return false; |
| 127 | } |
| 128 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 129 | mDataSockets.resize(mChans); |
| 130 | mCtrlSockets.resize(mChans); |
| 131 | |
| 132 | mControlServiceLoopThreads.resize(mChans); |
| 133 | mTxPriorityQueueServiceLoopThreads.resize(mChans); |
| 134 | mRxServiceLoopThreads.resize(mChans); |
| 135 | |
| 136 | mTxPriorityQueues.resize(mChans); |
| 137 | mReceiveFIFO.resize(mChans); |
| 138 | mStates.resize(mChans); |
| 139 | |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 140 | mClockSocket = new UDPSocket(mBasePort, mAddr.c_str(), mBasePort + 100); |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 141 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 142 | for (size_t i = 0; i < mChans; i++) { |
Thomas Tsou | e1ce925 | 2013-11-13 22:40:44 -0500 | [diff] [blame] | 143 | c_srcport = mBasePort + 2 * i + 1; |
| 144 | c_dstport = mBasePort + 2 * i + 101; |
| 145 | d_srcport = mBasePort + 2 * i + 2; |
| 146 | d_dstport = mBasePort + 2 * i + 102; |
| 147 | |
| 148 | mCtrlSockets[i] = new UDPSocket(c_srcport, mAddr.c_str(), c_dstport); |
| 149 | mDataSockets[i] = new UDPSocket(d_srcport, mAddr.c_str(), d_dstport); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 150 | } |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 151 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 152 | for (size_t i = 0; i < mChans; i++) { |
| 153 | mControlServiceLoopThreads[i] = new Thread(32768); |
| 154 | mTxPriorityQueueServiceLoopThreads[i] = new Thread(32768); |
| 155 | mRxServiceLoopThreads[i] = new Thread(32768); |
| 156 | |
| 157 | for (size_t n = 0; n < 8; n++) { |
| 158 | burst = modulateBurst(gDummyBurst, 8 + (n % 4 == 0), mSPSTx); |
| 159 | scaleVector(*burst, txFullScale); |
| 160 | mStates[i].init(n, burst); |
| 161 | delete burst; |
| 162 | } |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 167 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 168 | void Transceiver::addRadioVector(size_t chan, BitVector &bits, |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 169 | int RSSI, GSM::Time &wTime) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 170 | { |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 171 | signalVector *burst; |
| 172 | radioVector *radio_burst; |
| 173 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 174 | if (chan >= mTxPriorityQueues.size()) { |
| 175 | LOG(ALERT) << "Invalid channel " << chan; |
| 176 | return; |
| 177 | } |
| 178 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 179 | burst = modulateBurst(bits, 8 + (wTime.TN() % 4 == 0), mSPSTx); |
| 180 | scaleVector(*burst, txFullScale * pow(10, -RSSI / 10)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 181 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 182 | radio_burst = new radioVector(wTime, burst); |
| 183 | |
| 184 | mTxPriorityQueues[chan].write(radio_burst); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 185 | } |
| 186 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 187 | void Transceiver::pushRadioVector(GSM::Time &nowTime) |
| 188 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 189 | int TN, modFN; |
| 190 | radioVector *burst; |
| 191 | TransceiverState *state; |
| 192 | std::vector<signalVector *> bursts(mChans); |
| 193 | std::vector<bool> zeros(mChans); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 194 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 195 | for (size_t i = 0; i < mChans; i ++) { |
| 196 | state = &mStates[i]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 197 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 198 | while ((burst = mTxPriorityQueues[i].getStaleBurst(nowTime))) { |
| 199 | LOG(NOTICE) << "dumping STALE burst in TRX->USRP interface"; |
| 200 | |
| 201 | TN = burst->getTime().TN(); |
| 202 | modFN = burst->getTime().FN() % state->fillerModulus[TN]; |
| 203 | |
| 204 | delete state->fillerTable[modFN][TN]; |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 205 | state->fillerTable[modFN][TN] = burst->getVector(); |
| 206 | burst->setVector(NULL); |
| 207 | delete burst; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | TN = nowTime.TN(); |
| 211 | modFN = nowTime.FN() % state->fillerModulus[TN]; |
| 212 | |
| 213 | bursts[i] = state->fillerTable[modFN][TN]; |
| 214 | zeros[i] = state->chanType[TN] == NONE; |
| 215 | |
| 216 | if ((burst = mTxPriorityQueues[i].getCurrentBurst(nowTime))) { |
| 217 | delete state->fillerTable[modFN][TN]; |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 218 | state->fillerTable[modFN][TN] = burst->getVector(); |
| 219 | bursts[i] = burst->getVector(); |
| 220 | burst->setVector(NULL); |
| 221 | delete burst; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 222 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 225 | mRadioInterface->driveTransmitRadio(bursts, zeros); |
| 226 | |
| 227 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 230 | void Transceiver::setModulus(size_t timeslot, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 231 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 232 | TransceiverState *state = &mStates[chan]; |
| 233 | |
| 234 | switch (state->chanType[timeslot]) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 235 | case NONE: |
| 236 | case I: |
| 237 | case II: |
| 238 | case III: |
| 239 | case FILL: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 240 | state->fillerModulus[timeslot] = 26; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 241 | break; |
| 242 | case IV: |
| 243 | case VI: |
| 244 | case V: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 245 | state->fillerModulus[timeslot] = 51; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 246 | break; |
| 247 | //case V: |
| 248 | case VII: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 249 | state->fillerModulus[timeslot] = 102; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 250 | break; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 251 | case XIII: |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 252 | state->fillerModulus[timeslot] = 52; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 253 | break; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 260 | Transceiver::CorrType Transceiver::expectedCorrType(GSM::Time currTime, |
| 261 | size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 262 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 263 | TransceiverState *state = &mStates[chan]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 264 | unsigned burstTN = currTime.TN(); |
| 265 | unsigned burstFN = currTime.FN(); |
| 266 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 267 | switch (state->chanType[burstTN]) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 268 | case NONE: |
| 269 | return OFF; |
| 270 | break; |
| 271 | case FILL: |
| 272 | return IDLE; |
| 273 | break; |
| 274 | case I: |
| 275 | return TSC; |
| 276 | /*if (burstFN % 26 == 25) |
| 277 | return IDLE; |
| 278 | else |
| 279 | return TSC;*/ |
| 280 | break; |
| 281 | case II: |
ttsou | 2064297 | 2013-03-27 22:00:25 +0000 | [diff] [blame] | 282 | return TSC; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 283 | break; |
| 284 | case III: |
| 285 | return TSC; |
| 286 | break; |
| 287 | case IV: |
| 288 | case VI: |
| 289 | return RACH; |
| 290 | break; |
| 291 | case V: { |
| 292 | int mod51 = burstFN % 51; |
| 293 | if ((mod51 <= 36) && (mod51 >= 14)) |
| 294 | return RACH; |
| 295 | else if ((mod51 == 4) || (mod51 == 5)) |
| 296 | return RACH; |
| 297 | else if ((mod51 == 45) || (mod51 == 46)) |
| 298 | return RACH; |
| 299 | else |
| 300 | return TSC; |
| 301 | break; |
| 302 | } |
| 303 | case VII: |
| 304 | if ((burstFN % 51 <= 14) && (burstFN % 51 >= 12)) |
| 305 | return IDLE; |
| 306 | else |
| 307 | return TSC; |
| 308 | break; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 309 | case XIII: { |
| 310 | int mod52 = burstFN % 52; |
| 311 | if ((mod52 == 12) || (mod52 == 38)) |
| 312 | return RACH; |
| 313 | else if ((mod52 == 25) || (mod52 == 51)) |
| 314 | return IDLE; |
| 315 | else |
| 316 | return TSC; |
| 317 | break; |
| 318 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 319 | case LOOPBACK: |
| 320 | if ((burstFN % 51 <= 50) && (burstFN % 51 >=48)) |
| 321 | return IDLE; |
| 322 | else |
| 323 | return TSC; |
| 324 | break; |
| 325 | default: |
| 326 | return OFF; |
| 327 | break; |
| 328 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 329 | } |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 330 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 331 | /* |
| 332 | * Detect RACH synchronization sequence within a burst. No equalization |
| 333 | * is used or available on the RACH channel. |
| 334 | */ |
| 335 | bool Transceiver::detectRACH(TransceiverState *state, |
| 336 | signalVector &burst, |
| 337 | complex &, float &toa) |
| 338 | { |
| 339 | float threshold = 6.0; |
| 340 | |
| 341 | return detectRACHBurst(burst, threshold, mSPSRx, &, &toa); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Detect normal burst training sequence midamble. Update equalization |
| 346 | * state information and channel estimate if necessary. Equalization |
| 347 | * is currently disabled. |
| 348 | */ |
| 349 | bool Transceiver::detectTSC(TransceiverState *state, |
| 350 | signalVector &burst, |
| 351 | complex &, float &toa, GSM::Time &time) |
| 352 | { |
| 353 | int tn = time.TN(); |
| 354 | float chanOffset, threshold = 5.0; |
| 355 | bool needDFE = false, estimateChan = false; |
| 356 | double elapsed = time - state->chanEstimateTime[tn]; |
| 357 | signalVector *chanResp; |
| 358 | |
| 359 | /* Check equalization update state */ |
| 360 | if ((elapsed > 50) || (!state->chanResponse[tn])) { |
| 361 | delete state->DFEForward[tn]; |
| 362 | delete state->DFEFeedback[tn]; |
| 363 | state->DFEForward[tn] = NULL; |
| 364 | state->DFEFeedback[tn] = NULL; |
| 365 | |
| 366 | estimateChan = true; |
| 367 | } |
| 368 | |
| 369 | /* Detect normal burst midambles */ |
| 370 | if (!analyzeTrafficBurst(burst, mTSC, threshold, mSPSRx, &, |
| 371 | &toa, mMaxExpectedDelay, estimateChan, |
| 372 | &chanResp, &chanOffset)) { |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | state->SNRestimate[tn] = amp.norm2() / (mNoiseLev * mNoiseLev + 1.0); |
| 377 | |
| 378 | /* Set equalizer if unabled */ |
| 379 | if (needDFE && estimateChan) { |
| 380 | state->chanResponse[tn] = chanResp; |
| 381 | state->chanRespOffset[tn] = chanOffset; |
| 382 | state->chanRespAmplitude[tn] = amp; |
| 383 | |
| 384 | scaleVector(*chanResp, complex(1.0, 0.0) / amp); |
| 385 | |
| 386 | designDFE(*chanResp, state->SNRestimate[tn], |
| 387 | 7, &state->DFEForward[tn], &state->DFEFeedback[tn]); |
| 388 | |
| 389 | state->chanEstimateTime[tn] = time; |
| 390 | } |
| 391 | |
| 392 | return true;; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Demodulate GMSK burst using equalization if requested. Otherwise |
| 397 | * demodulate by direct rotation and soft slicing. |
| 398 | */ |
| 399 | SoftVector *Transceiver::demodulate(TransceiverState *state, |
| 400 | signalVector &burst, complex amp, |
| 401 | float toa, size_t tn, bool equalize) |
| 402 | { |
| 403 | if (equalize) { |
| 404 | scaleVector(burst, complex(1.0, 0.0) / amp); |
| 405 | return equalizeBurst(burst, |
| 406 | toa - state->chanRespOffset[tn], |
| 407 | mSPSRx, |
| 408 | *state->DFEForward[tn], |
| 409 | *state->DFEFeedback[tn]); |
| 410 | } |
| 411 | |
| 412 | return demodulateBurst(burst, mSPSRx, amp, toa); |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Pull bursts from the FIFO and handle according to the slot |
| 417 | * and burst correlation type. Equalzation is currently disabled. |
| 418 | */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 419 | SoftVector *Transceiver::pullRadioVector(GSM::Time &wTime, int &RSSI, |
| 420 | int &timingOffset, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 421 | { |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 422 | bool success, equalize = false; |
| 423 | complex amp; |
| 424 | float toa, pow, max = -1.0, avg = 0.0; |
| 425 | signalVector *burst; |
| 426 | SoftVector *bits; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 427 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 428 | /* Blocking FIFO read */ |
| 429 | radioVector *radio_burst = mReceiveFIFO[chan]->read(); |
| 430 | if (!radio_burst) |
| 431 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 432 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 433 | /* Set time and determine correlation type */ |
| 434 | GSM::Time time = radio_burst->getTime(); |
| 435 | CorrType type = expectedCorrType(time, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 436 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 437 | if ((type == OFF) || (type == IDLE)) { |
| 438 | delete radio_burst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
kurtis.heimerl | 3ed6fb7 | 2011-11-26 03:17:52 +0000 | [diff] [blame] | 441 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 442 | /* Average noise on diversity paths and update global levels */ |
| 443 | burst = radio_burst->getVector(); |
| 444 | avg = avg / radio_burst->chans(); |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 445 | mNoiseLev = mNoises.avg(); |
Thomas Tsou | a1a3ab4 | 2013-10-18 10:50:52 -0400 | [diff] [blame] | 446 | avg = sqrt(avg); |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 447 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 448 | /* Detect normal or RACH bursts */ |
| 449 | if (type == TSC) |
| 450 | success = detectTSC(&mStates[chan], *burst, amp, toa, time); |
| 451 | else |
| 452 | success = detectRACH(&mStates[chan], *burst, amp, toa); |
Thomas Tsou | f078273 | 2013-10-29 15:55:47 -0400 | [diff] [blame] | 453 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 454 | if (!success) { |
| 455 | mNoises.insert(avg); |
| 456 | delete radio_burst; |
| 457 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 458 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 459 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 460 | /* Demodulate and set output info */ |
| 461 | if (equalize && (type != TSC)) |
| 462 | equalize = false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 463 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 464 | bits = demodulate(&mStates[chan], *burst, amp, toa, time.TN(), equalize); |
| 465 | wTime = time; |
| 466 | RSSI = (int) floor(20.0 * log10(rxFullScale / avg)); |
| 467 | timingOffset = (int) round(toa * 256.0 / mSPSRx); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 468 | |
Thomas Tsou | 30421a7 | 2013-11-13 23:14:48 -0500 | [diff] [blame^] | 469 | delete radio_burst; |
| 470 | |
| 471 | return bits; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void Transceiver::start() |
| 475 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 476 | TransceiverChannel *chan; |
| 477 | |
| 478 | for (size_t i = 0; i < mControlServiceLoopThreads.size(); i++) { |
| 479 | chan = new TransceiverChannel(this, i); |
| 480 | mControlServiceLoopThreads[i]->start((void * (*)(void*)) |
| 481 | ControlServiceLoopAdapter, (void*) chan); |
| 482 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | void Transceiver::reset() |
| 486 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 487 | for (size_t i = 0; i < mTxPriorityQueues.size(); i++) |
| 488 | mTxPriorityQueues[i].clear(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 492 | void Transceiver::driveControl(size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 493 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 494 | int MAX_PACKET_LENGTH = 100; |
| 495 | |
| 496 | // check control socket |
| 497 | char buffer[MAX_PACKET_LENGTH]; |
| 498 | int msgLen = -1; |
| 499 | buffer[0] = '\0'; |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 500 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 501 | msgLen = mCtrlSockets[chan]->read(buffer); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 502 | |
| 503 | if (msgLen < 1) { |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | char cmdcheck[4]; |
| 508 | char command[MAX_PACKET_LENGTH]; |
| 509 | char response[MAX_PACKET_LENGTH]; |
| 510 | |
| 511 | sscanf(buffer,"%3s %s",cmdcheck,command); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 512 | |
| 513 | if (!chan) |
| 514 | writeClockInterface(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 515 | |
| 516 | if (strcmp(cmdcheck,"CMD")!=0) { |
| 517 | LOG(WARNING) << "bogus message on control interface"; |
| 518 | return; |
| 519 | } |
| 520 | LOG(INFO) << "command is " << buffer; |
| 521 | |
| 522 | if (strcmp(command,"POWEROFF")==0) { |
| 523 | // turn off transmitter/demod |
| 524 | sprintf(response,"RSP POWEROFF 0"); |
| 525 | } |
| 526 | else if (strcmp(command,"POWERON")==0) { |
| 527 | // turn on transmitter/demod |
| 528 | if (!mTxFreq || !mRxFreq) |
| 529 | sprintf(response,"RSP POWERON 1"); |
| 530 | else { |
| 531 | sprintf(response,"RSP POWERON 0"); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 532 | if (!chan && !mOn) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 533 | // Prepare for thread start |
| 534 | mPower = -20; |
| 535 | mRadioInterface->start(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 536 | |
| 537 | // Start radio interface threads. |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 538 | mTxLowerLoopThread->start((void * (*)(void*)) |
| 539 | TxLowerLoopAdapter,(void*) this); |
| 540 | mRxLowerLoopThread->start((void * (*)(void*)) |
| 541 | RxLowerLoopAdapter,(void*) this); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 542 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 543 | for (size_t i = 0; i < mChans; i++) { |
| 544 | TransceiverChannel *chan = new TransceiverChannel(this, i); |
| 545 | mRxServiceLoopThreads[i]->start((void * (*)(void*)) |
| 546 | RxUpperLoopAdapter, (void*) chan); |
| 547 | |
| 548 | chan = new TransceiverChannel(this, i); |
| 549 | mTxPriorityQueueServiceLoopThreads[i]->start((void * (*)(void*)) |
| 550 | TxUpperLoopAdapter, (void*) chan); |
| 551 | } |
| 552 | |
| 553 | writeClockInterface(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 554 | mOn = true; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | else if (strcmp(command,"SETMAXDLY")==0) { |
| 559 | //set expected maximum time-of-arrival |
| 560 | int maxDelay; |
| 561 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&maxDelay); |
| 562 | mMaxExpectedDelay = maxDelay; // 1 GSM symbol is approx. 1 km |
| 563 | sprintf(response,"RSP SETMAXDLY 0 %d",maxDelay); |
| 564 | } |
| 565 | else if (strcmp(command,"SETRXGAIN")==0) { |
| 566 | //set expected maximum time-of-arrival |
| 567 | int newGain; |
| 568 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&newGain); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 569 | newGain = mRadioInterface->setRxGain(newGain, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 570 | sprintf(response,"RSP SETRXGAIN 0 %d",newGain); |
| 571 | } |
| 572 | else if (strcmp(command,"NOISELEV")==0) { |
| 573 | if (mOn) { |
| 574 | sprintf(response,"RSP NOISELEV 0 %d", |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 575 | (int) round(20.0*log10(rxFullScale/mNoiseLev))); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 576 | } |
| 577 | else { |
| 578 | sprintf(response,"RSP NOISELEV 1 0"); |
| 579 | } |
| 580 | } |
| 581 | else if (strcmp(command,"SETPOWER")==0) { |
| 582 | // set output power in dB |
| 583 | int dbPwr; |
| 584 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&dbPwr); |
| 585 | if (!mOn) |
| 586 | sprintf(response,"RSP SETPOWER 1 %d",dbPwr); |
| 587 | else { |
| 588 | mPower = dbPwr; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 589 | mRadioInterface->setPowerAttenuation(dbPwr, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 590 | sprintf(response,"RSP SETPOWER 0 %d",dbPwr); |
| 591 | } |
| 592 | } |
| 593 | else if (strcmp(command,"ADJPOWER")==0) { |
| 594 | // adjust power in dB steps |
| 595 | int dbStep; |
| 596 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&dbStep); |
| 597 | if (!mOn) |
| 598 | sprintf(response,"RSP ADJPOWER 1 %d",mPower); |
| 599 | else { |
| 600 | mPower += dbStep; |
| 601 | sprintf(response,"RSP ADJPOWER 0 %d",mPower); |
| 602 | } |
| 603 | } |
| 604 | #define FREQOFFSET 0//11.2e3 |
| 605 | else if (strcmp(command,"RXTUNE")==0) { |
| 606 | // tune receiver |
| 607 | int freqKhz; |
| 608 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&freqKhz); |
| 609 | mRxFreq = freqKhz*1.0e3+FREQOFFSET; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 610 | if (!mRadioInterface->tuneRx(mRxFreq, chan)) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 611 | LOG(ALERT) << "RX failed to tune"; |
| 612 | sprintf(response,"RSP RXTUNE 1 %d",freqKhz); |
| 613 | } |
| 614 | else |
| 615 | sprintf(response,"RSP RXTUNE 0 %d",freqKhz); |
| 616 | } |
| 617 | else if (strcmp(command,"TXTUNE")==0) { |
| 618 | // tune txmtr |
| 619 | int freqKhz; |
| 620 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&freqKhz); |
| 621 | //freqKhz = 890e3; |
| 622 | mTxFreq = freqKhz*1.0e3+FREQOFFSET; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 623 | if (!mRadioInterface->tuneTx(mTxFreq, chan)) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 624 | LOG(ALERT) << "TX failed to tune"; |
| 625 | sprintf(response,"RSP TXTUNE 1 %d",freqKhz); |
| 626 | } |
| 627 | else |
| 628 | sprintf(response,"RSP TXTUNE 0 %d",freqKhz); |
| 629 | } |
| 630 | else if (strcmp(command,"SETTSC")==0) { |
| 631 | // set TSC |
| 632 | int TSC; |
| 633 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&TSC); |
| 634 | if (mOn) |
| 635 | sprintf(response,"RSP SETTSC 1 %d",TSC); |
| 636 | else { |
| 637 | mTSC = TSC; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 638 | generateMidamble(mSPSRx, TSC); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 639 | sprintf(response,"RSP SETTSC 0 %d", TSC); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | else if (strcmp(command,"SETSLOT")==0) { |
| 643 | // set TSC |
| 644 | int corrCode; |
| 645 | int timeslot; |
| 646 | sscanf(buffer,"%3s %s %d %d",cmdcheck,command,×lot,&corrCode); |
| 647 | if ((timeslot < 0) || (timeslot > 7)) { |
| 648 | LOG(WARNING) << "bogus message on control interface"; |
| 649 | sprintf(response,"RSP SETSLOT 1 %d %d",timeslot,corrCode); |
| 650 | return; |
| 651 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 652 | mStates[chan].chanType[timeslot] = (ChannelCombination) corrCode; |
| 653 | setModulus(timeslot, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 654 | sprintf(response,"RSP SETSLOT 0 %d %d",timeslot,corrCode); |
| 655 | |
| 656 | } |
| 657 | else { |
| 658 | LOG(WARNING) << "bogus command " << command << " on control interface."; |
| 659 | } |
| 660 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 661 | mCtrlSockets[chan]->write(response, strlen(response) + 1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 664 | bool Transceiver::driveTxPriorityQueue(size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 665 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 666 | char buffer[gSlotLen+50]; |
| 667 | |
| 668 | // check data socket |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 669 | size_t msgLen = mDataSockets[chan]->read(buffer); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 670 | |
| 671 | if (msgLen!=gSlotLen+1+4+1) { |
| 672 | LOG(ERR) << "badly formatted packet on GSM->TRX interface"; |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | int timeSlot = (int) buffer[0]; |
| 677 | uint64_t frameNum = 0; |
| 678 | for (int i = 0; i < 4; i++) |
| 679 | frameNum = (frameNum << 8) | (0x0ff & buffer[i+1]); |
| 680 | |
| 681 | /* |
| 682 | if (GSM::Time(frameNum,timeSlot) > mTransmitDeadlineClock + GSM::Time(51,0)) { |
| 683 | // stale burst |
| 684 | //LOG(DEBUG) << "FAST! "<< GSM::Time(frameNum,timeSlot); |
| 685 | //writeClockInterface(); |
| 686 | }*/ |
| 687 | |
| 688 | /* |
| 689 | DAB -- Just let these go through the demod. |
| 690 | if (GSM::Time(frameNum,timeSlot) < mTransmitDeadlineClock) { |
| 691 | // stale burst from GSM core |
| 692 | LOG(NOTICE) << "STALE packet on GSM->TRX interface at time "<< GSM::Time(frameNum,timeSlot); |
| 693 | return false; |
| 694 | } |
| 695 | */ |
| 696 | |
| 697 | // periodically update GSM core clock |
| 698 | LOG(DEBUG) << "mTransmitDeadlineClock " << mTransmitDeadlineClock |
| 699 | << " mLastClockUpdateTime " << mLastClockUpdateTime; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 700 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 701 | if (!chan) { |
| 702 | if (mTransmitDeadlineClock > mLastClockUpdateTime + GSM::Time(216,0)) |
| 703 | writeClockInterface(); |
| 704 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 705 | |
| 706 | LOG(DEBUG) << "rcvd. burst at: " << GSM::Time(frameNum,timeSlot); |
| 707 | |
| 708 | int RSSI = (int) buffer[5]; |
| 709 | static BitVector newBurst(gSlotLen); |
| 710 | BitVector::iterator itr = newBurst.begin(); |
| 711 | char *bufferItr = buffer+6; |
| 712 | while (itr < newBurst.end()) |
| 713 | *itr++ = *bufferItr++; |
| 714 | |
| 715 | GSM::Time currTime = GSM::Time(frameNum,timeSlot); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 716 | |
| 717 | addRadioVector(chan, newBurst, RSSI, currTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 718 | |
| 719 | return true; |
| 720 | |
| 721 | |
| 722 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 723 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 724 | void Transceiver::driveReceiveRadio() |
| 725 | { |
| 726 | if (!mRadioInterface->driveReceiveRadio()) |
| 727 | usleep(100000); |
| 728 | } |
| 729 | |
| 730 | void Transceiver::driveReceiveFIFO(size_t chan) |
| 731 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 732 | SoftVector *rxBurst = NULL; |
| 733 | int RSSI; |
| 734 | int TOA; // in 1/256 of a symbol |
| 735 | GSM::Time burstTime; |
| 736 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 737 | rxBurst = pullRadioVector(burstTime, RSSI, TOA, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 738 | |
| 739 | if (rxBurst) { |
| 740 | |
| 741 | LOG(DEBUG) << "burst parameters: " |
| 742 | << " time: " << burstTime |
| 743 | << " RSSI: " << RSSI |
| 744 | << " TOA: " << TOA |
| 745 | << " bits: " << *rxBurst; |
| 746 | |
| 747 | char burstString[gSlotLen+10]; |
| 748 | burstString[0] = burstTime.TN(); |
| 749 | for (int i = 0; i < 4; i++) |
| 750 | burstString[1+i] = (burstTime.FN() >> ((3-i)*8)) & 0x0ff; |
| 751 | burstString[5] = RSSI; |
| 752 | burstString[6] = (TOA >> 8) & 0x0ff; |
| 753 | burstString[7] = TOA & 0x0ff; |
| 754 | SoftVector::iterator burstItr = rxBurst->begin(); |
| 755 | |
| 756 | for (unsigned int i = 0; i < gSlotLen; i++) { |
| 757 | burstString[8+i] =(char) round((*burstItr++)*255.0); |
| 758 | } |
| 759 | burstString[gSlotLen+9] = '\0'; |
| 760 | delete rxBurst; |
| 761 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 762 | mDataSockets[chan]->write(burstString,gSlotLen+10); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 763 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 766 | void Transceiver::driveTxFIFO() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 767 | { |
| 768 | |
| 769 | /** |
| 770 | Features a carefully controlled latency mechanism, to |
| 771 | assure that transmit packets arrive at the radio/USRP |
| 772 | before they need to be transmitted. |
| 773 | |
| 774 | Deadline clock indicates the burst that needs to be |
| 775 | pushed into the FIFO right NOW. If transmit queue does |
| 776 | not have a burst, stick in filler data. |
| 777 | */ |
| 778 | |
| 779 | |
| 780 | RadioClock *radioClock = (mRadioInterface->getClock()); |
| 781 | |
| 782 | if (mOn) { |
| 783 | //radioClock->wait(); // wait until clock updates |
| 784 | LOG(DEBUG) << "radio clock " << radioClock->get(); |
| 785 | while (radioClock->get() + mTransmitLatency > mTransmitDeadlineClock) { |
| 786 | // if underrun, then we're not providing bursts to radio/USRP fast |
| 787 | // enough. Need to increase latency by one GSM frame. |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 788 | if (mRadioInterface->getWindowType() == RadioDevice::TX_WINDOW_USRP1) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 789 | if (mRadioInterface->isUnderrun()) { |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 790 | // only update latency at the defined frame interval |
| 791 | if (radioClock->get() > mLatencyUpdateTime + GSM::Time(USB_LATENCY_INTRVL)) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 792 | mTransmitLatency = mTransmitLatency + GSM::Time(1,0); |
| 793 | LOG(INFO) << "new latency: " << mTransmitLatency; |
| 794 | mLatencyUpdateTime = radioClock->get(); |
| 795 | } |
| 796 | } |
| 797 | else { |
| 798 | // if underrun hasn't occurred in the last sec (216 frames) drop |
| 799 | // transmit latency by a timeslot |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 800 | if (mTransmitLatency > GSM::Time(USB_LATENCY_MIN)) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 801 | if (radioClock->get() > mLatencyUpdateTime + GSM::Time(216,0)) { |
| 802 | mTransmitLatency.decTN(); |
| 803 | LOG(INFO) << "reduced latency: " << mTransmitLatency; |
| 804 | mLatencyUpdateTime = radioClock->get(); |
| 805 | } |
| 806 | } |
| 807 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 808 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 809 | // time to push burst to transmit FIFO |
| 810 | pushRadioVector(mTransmitDeadlineClock); |
| 811 | mTransmitDeadlineClock.incTN(); |
| 812 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 813 | } |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 814 | |
| 815 | radioClock->wait(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
| 819 | |
| 820 | void Transceiver::writeClockInterface() |
| 821 | { |
| 822 | char command[50]; |
| 823 | // FIXME -- This should be adaptive. |
| 824 | sprintf(command,"IND CLOCK %llu",(unsigned long long) (mTransmitDeadlineClock.FN()+2)); |
| 825 | |
| 826 | LOG(INFO) << "ClockInterface: sending " << command; |
| 827 | |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame] | 828 | mClockSocket->write(command, strlen(command) + 1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 829 | |
| 830 | mLastClockUpdateTime = mTransmitDeadlineClock; |
| 831 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 832 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 833 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 834 | void *RxUpperLoopAdapter(TransceiverChannel *chan) |
| 835 | { |
| 836 | Transceiver *trx = chan->trx; |
| 837 | size_t num = chan->num; |
| 838 | |
| 839 | delete chan; |
| 840 | |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 841 | trx->setPriority(0.42); |
| 842 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 843 | while (1) { |
| 844 | trx->driveReceiveFIFO(num); |
| 845 | pthread_testcancel(); |
| 846 | } |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | void *RxLowerLoopAdapter(Transceiver *transceiver) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 851 | { |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 852 | transceiver->setPriority(0.45); |
kurtis.heimerl | 6b495a5 | 2011-11-26 03:17:21 +0000 | [diff] [blame] | 853 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 854 | while (1) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 855 | transceiver->driveReceiveRadio(); |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 856 | pthread_testcancel(); |
| 857 | } |
| 858 | return NULL; |
| 859 | } |
| 860 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 861 | void *TxLowerLoopAdapter(Transceiver *transceiver) |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 862 | { |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 863 | transceiver->setPriority(0.44); |
| 864 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 865 | while (1) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 866 | transceiver->driveTxFIFO(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 867 | pthread_testcancel(); |
| 868 | } |
| 869 | return NULL; |
| 870 | } |
| 871 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 872 | void *ControlServiceLoopAdapter(TransceiverChannel *chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 873 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 874 | Transceiver *trx = chan->trx; |
| 875 | size_t num = chan->num; |
| 876 | |
| 877 | delete chan; |
| 878 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 879 | while (1) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 880 | trx->driveControl(num); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 881 | pthread_testcancel(); |
| 882 | } |
| 883 | return NULL; |
| 884 | } |
| 885 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 886 | void *TxUpperLoopAdapter(TransceiverChannel *chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 887 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 888 | Transceiver *trx = chan->trx; |
| 889 | size_t num = chan->num; |
| 890 | |
| 891 | delete chan; |
| 892 | |
Thomas Tsou | a4cf48c | 2013-11-09 21:44:26 -0500 | [diff] [blame] | 893 | trx->setPriority(0.40); |
| 894 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 895 | while (1) { |
| 896 | bool stale = false; |
| 897 | // Flush the UDP packets until a successful transfer. |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 898 | while (!trx->driveTxPriorityQueue(num)) { |
| 899 | stale = true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 900 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 901 | if (!num && stale) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 902 | // If a packet was stale, remind the GSM stack of the clock. |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 903 | trx->writeClockInterface(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 904 | } |
| 905 | pthread_testcancel(); |
| 906 | } |
| 907 | return NULL; |
| 908 | } |