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