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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 45 | Transceiver::Transceiver(int wBasePort, |
| 46 | const char *TRXAddress, |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 47 | int wSPS, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 48 | GSM::Time wTransmitLatency, |
| 49 | RadioInterface *wRadioInterface) |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 50 | : mBasePort(wBasePort), mAddr(TRXAddress), |
| 51 | mDataSocket(NULL), mCtrlSocket(NULL), mClockSocket(NULL), |
| 52 | mSPSTx(wSPS), mSPSRx(1), mNoises(NOISE_CNT) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 53 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 54 | GSM::Time startTime(random() % gHyperframe,0); |
| 55 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 56 | mRxServiceLoopThread = new Thread(32768); |
| 57 | mTxServiceLoopThread = new Thread(32768); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 58 | mControlServiceLoopThread = new Thread(32768); ///< thread to process control messages from GSM core |
| 59 | mTransmitPriorityQueueServiceLoopThread = new Thread(32768);///< thread to process transmit bursts from GSM core |
| 60 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 61 | mRadioInterface = wRadioInterface; |
| 62 | mTransmitLatency = wTransmitLatency; |
| 63 | mTransmitDeadlineClock = startTime; |
| 64 | mLastClockUpdateTime = startTime; |
| 65 | mLatencyUpdateTime = startTime; |
| 66 | mRadioInterface->getClock()->set(startTime); |
| 67 | mMaxExpectedDelay = 0; |
| 68 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 69 | txFullScale = mRadioInterface->fullScaleInputValue(); |
| 70 | rxFullScale = mRadioInterface->fullScaleOutputValue(); |
| 71 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 72 | mOn = false; |
| 73 | mTxFreq = 0.0; |
| 74 | mRxFreq = 0.0; |
| 75 | mPower = -10; |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 76 | mNoiseLev = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Transceiver::~Transceiver() |
| 80 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 81 | sigProcLibDestroy(); |
| 82 | mTransmitPriorityQueue.clear(); |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 83 | |
| 84 | delete mClockSocket; |
| 85 | delete mCtrlSocket; |
| 86 | delete mDataSocket; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 87 | } |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 88 | |
| 89 | bool Transceiver::init() |
| 90 | { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 91 | if (!sigProcLibSetup(mSPSTx)) { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 92 | LOG(ALERT) << "Failed to initialize signal processing library"; |
| 93 | return false; |
| 94 | } |
| 95 | |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 96 | mClockSocket = new UDPSocket(mBasePort, mAddr.c_str(), mBasePort + 100); |
| 97 | mCtrlSocket = new UDPSocket(mBasePort + 1, mAddr.c_str(), mBasePort + 101); |
| 98 | mDataSocket = new UDPSocket(mBasePort + 2, mAddr.c_str(), mBasePort + 102); |
| 99 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 100 | // initialize filler tables with dummy bursts |
| 101 | for (int i = 0; i < 8; i++) { |
| 102 | signalVector* modBurst = modulateBurst(gDummyBurst, |
| 103 | 8 + (i % 4 == 0), |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 104 | mSPSTx); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 105 | if (!modBurst) { |
| 106 | sigProcLibDestroy(); |
| 107 | LOG(ALERT) << "Failed to initialize filler table"; |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | scaleVector(*modBurst,txFullScale); |
| 112 | fillerModulus[i]=26; |
| 113 | for (int j = 0; j < 102; j++) { |
| 114 | fillerTable[j][i] = new signalVector(*modBurst); |
| 115 | } |
| 116 | |
| 117 | delete modBurst; |
| 118 | mChanType[i] = NONE; |
| 119 | channelResponse[i] = NULL; |
| 120 | DFEForward[i] = NULL; |
| 121 | DFEFeedback[i] = NULL; |
| 122 | channelEstimateTime[i] = mTransmitDeadlineClock; |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 127 | |
| 128 | void Transceiver::addRadioVector(BitVector &burst, |
| 129 | int RSSI, |
| 130 | GSM::Time &wTime) |
| 131 | { |
| 132 | // modulate and stick into queue |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 133 | signalVector* modBurst = modulateBurst(burst, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 134 | 8 + (wTime.TN() % 4 == 0), |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 135 | mSPSTx); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 136 | scaleVector(*modBurst,txFullScale * pow(10,-RSSI/10)); |
| 137 | radioVector *newVec = new radioVector(*modBurst,wTime); |
| 138 | mTransmitPriorityQueue.write(newVec); |
| 139 | |
| 140 | delete modBurst; |
| 141 | } |
| 142 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 143 | void Transceiver::pushRadioVector(GSM::Time &nowTime) |
| 144 | { |
| 145 | |
| 146 | // dump stale bursts, if any |
| 147 | while (radioVector* staleBurst = mTransmitPriorityQueue.getStaleBurst(nowTime)) { |
| 148 | // Even if the burst is stale, put it in the fillter table. |
| 149 | // (It might be an idle pattern.) |
| 150 | LOG(NOTICE) << "dumping STALE burst in TRX->USRP interface"; |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 151 | const GSM::Time& nextTime = staleBurst->getTime(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 152 | int TN = nextTime.TN(); |
| 153 | int modFN = nextTime.FN() % fillerModulus[TN]; |
| 154 | delete fillerTable[modFN][TN]; |
| 155 | fillerTable[modFN][TN] = staleBurst; |
| 156 | } |
| 157 | |
| 158 | int TN = nowTime.TN(); |
| 159 | int modFN = nowTime.FN() % fillerModulus[nowTime.TN()]; |
| 160 | |
| 161 | // if queue contains data at the desired timestamp, stick it into FIFO |
| 162 | if (radioVector *next = (radioVector*) mTransmitPriorityQueue.getCurrentBurst(nowTime)) { |
| 163 | LOG(DEBUG) << "transmitFIFO: wrote burst " << next << " at time: " << nowTime; |
| 164 | delete fillerTable[modFN][TN]; |
| 165 | fillerTable[modFN][TN] = new signalVector(*(next)); |
| 166 | mRadioInterface->driveTransmitRadio(*(next),(mChanType[TN]==NONE)); //fillerTable[modFN][TN])); |
| 167 | delete next; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
| 171 | // otherwise, pull filler data, and push to radio FIFO |
| 172 | mRadioInterface->driveTransmitRadio(*(fillerTable[modFN][TN]),(mChanType[TN]==NONE)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void Transceiver::setModulus(int timeslot) |
| 176 | { |
| 177 | switch (mChanType[timeslot]) { |
| 178 | case NONE: |
| 179 | case I: |
| 180 | case II: |
| 181 | case III: |
| 182 | case FILL: |
| 183 | fillerModulus[timeslot] = 26; |
| 184 | break; |
| 185 | case IV: |
| 186 | case VI: |
| 187 | case V: |
| 188 | fillerModulus[timeslot] = 51; |
| 189 | break; |
| 190 | //case V: |
| 191 | case VII: |
| 192 | fillerModulus[timeslot] = 102; |
| 193 | break; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 194 | case XIII: |
| 195 | fillerModulus[timeslot] = 52; |
| 196 | break; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | Transceiver::CorrType Transceiver::expectedCorrType(GSM::Time currTime) |
| 204 | { |
| 205 | |
| 206 | unsigned burstTN = currTime.TN(); |
| 207 | unsigned burstFN = currTime.FN(); |
| 208 | |
| 209 | switch (mChanType[burstTN]) { |
| 210 | case NONE: |
| 211 | return OFF; |
| 212 | break; |
| 213 | case FILL: |
| 214 | return IDLE; |
| 215 | break; |
| 216 | case I: |
| 217 | return TSC; |
| 218 | /*if (burstFN % 26 == 25) |
| 219 | return IDLE; |
| 220 | else |
| 221 | return TSC;*/ |
| 222 | break; |
| 223 | case II: |
ttsou | 2064297 | 2013-03-27 22:00:25 +0000 | [diff] [blame] | 224 | return TSC; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 225 | break; |
| 226 | case III: |
| 227 | return TSC; |
| 228 | break; |
| 229 | case IV: |
| 230 | case VI: |
| 231 | return RACH; |
| 232 | break; |
| 233 | case V: { |
| 234 | int mod51 = burstFN % 51; |
| 235 | if ((mod51 <= 36) && (mod51 >= 14)) |
| 236 | return RACH; |
| 237 | else if ((mod51 == 4) || (mod51 == 5)) |
| 238 | return RACH; |
| 239 | else if ((mod51 == 45) || (mod51 == 46)) |
| 240 | return RACH; |
| 241 | else |
| 242 | return TSC; |
| 243 | break; |
| 244 | } |
| 245 | case VII: |
| 246 | if ((burstFN % 51 <= 14) && (burstFN % 51 >= 12)) |
| 247 | return IDLE; |
| 248 | else |
| 249 | return TSC; |
| 250 | break; |
ttsou | fc40a84 | 2013-06-09 22:38:18 +0000 | [diff] [blame] | 251 | case XIII: { |
| 252 | int mod52 = burstFN % 52; |
| 253 | if ((mod52 == 12) || (mod52 == 38)) |
| 254 | return RACH; |
| 255 | else if ((mod52 == 25) || (mod52 == 51)) |
| 256 | return IDLE; |
| 257 | else |
| 258 | return TSC; |
| 259 | break; |
| 260 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 261 | case LOOPBACK: |
| 262 | if ((burstFN % 51 <= 50) && (burstFN % 51 >=48)) |
| 263 | return IDLE; |
| 264 | else |
| 265 | return TSC; |
| 266 | break; |
| 267 | default: |
| 268 | return OFF; |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | } |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 273 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 274 | SoftVector *Transceiver::pullRadioVector(GSM::Time &wTime, |
| 275 | int &RSSI, |
| 276 | int &timingOffset) |
| 277 | { |
Thomas Tsou | d5a80c3 | 2013-09-19 11:24:14 -0400 | [diff] [blame] | 278 | bool needDFE = false; |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 279 | bool success = false; |
| 280 | complex amplitude = 0.0; |
| 281 | float TOA = 0.0, avg = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 282 | |
| 283 | radioVector *rxBurst = (radioVector *) mReceiveFIFO->get(); |
| 284 | |
| 285 | if (!rxBurst) return NULL; |
| 286 | |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 287 | int timeslot = rxBurst->getTime().TN(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 288 | |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 289 | CorrType corrType = expectedCorrType(rxBurst->getTime()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 290 | |
| 291 | if ((corrType==OFF) || (corrType==IDLE)) { |
| 292 | delete rxBurst; |
| 293 | return NULL; |
| 294 | } |
kurtis.heimerl | 3ed6fb7 | 2011-11-26 03:17:52 +0000 | [diff] [blame] | 295 | |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 296 | signalVector *vectorBurst = rxBurst; |
| 297 | |
| 298 | energyDetect(*vectorBurst, 20 * mSPSRx, 0.0, &avg); |
| 299 | |
| 300 | // Update noise level |
| 301 | mNoiseLev = mNoises.avg(); |
Thomas Tsou | a1a3ab4 | 2013-10-18 10:50:52 -0400 | [diff] [blame] | 302 | avg = sqrt(avg); |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 303 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 304 | // run the proper correlator |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 305 | if (corrType==TSC) { |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 306 | LOG(DEBUG) << "looking for TSC at time: " << rxBurst->getTime(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 307 | signalVector *channelResp; |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 308 | double framesElapsed = rxBurst->getTime()-channelEstimateTime[timeslot]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 309 | bool estimateChannel = false; |
| 310 | if ((framesElapsed > 50) || (channelResponse[timeslot]==NULL)) { |
| 311 | if (channelResponse[timeslot]) delete channelResponse[timeslot]; |
| 312 | if (DFEForward[timeslot]) delete DFEForward[timeslot]; |
| 313 | if (DFEFeedback[timeslot]) delete DFEFeedback[timeslot]; |
| 314 | channelResponse[timeslot] = NULL; |
| 315 | DFEForward[timeslot] = NULL; |
| 316 | DFEFeedback[timeslot] = NULL; |
| 317 | estimateChannel = true; |
| 318 | } |
| 319 | if (!needDFE) estimateChannel = false; |
| 320 | float chanOffset; |
| 321 | success = analyzeTrafficBurst(*vectorBurst, |
| 322 | mTSC, |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 323 | 5.0, |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 324 | mSPSRx, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 325 | &litude, |
| 326 | &TOA, |
| 327 | mMaxExpectedDelay, |
| 328 | estimateChannel, |
| 329 | &channelResp, |
| 330 | &chanOffset); |
| 331 | if (success) { |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 332 | SNRestimate[timeslot] = amplitude.norm2()/(mNoiseLev*mNoiseLev+1.0); // this is not highly accurate |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 333 | if (estimateChannel) { |
| 334 | LOG(DEBUG) << "estimating channel..."; |
| 335 | channelResponse[timeslot] = channelResp; |
| 336 | chanRespOffset[timeslot] = chanOffset; |
| 337 | chanRespAmplitude[timeslot] = amplitude; |
| 338 | scaleVector(*channelResp, complex(1.0,0.0)/amplitude); |
| 339 | designDFE(*channelResp, SNRestimate[timeslot], 7, &DFEForward[timeslot], &DFEFeedback[timeslot]); |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 340 | channelEstimateTime[timeslot] = rxBurst->getTime(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 341 | LOG(DEBUG) << "SNR: " << SNRestimate[timeslot] << ", DFE forward: " << *DFEForward[timeslot] << ", DFE backward: " << *DFEFeedback[timeslot]; |
| 342 | } |
| 343 | } |
| 344 | else { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 345 | channelResponse[timeslot] = NULL; |
Thomas Tsou | a1a3ab4 | 2013-10-18 10:50:52 -0400 | [diff] [blame] | 346 | mNoises.insert(avg); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | else { |
| 350 | // RACH burst |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 351 | if (success = detectRACHBurst(*vectorBurst, 6.0, mSPSRx, &litude, &TOA)) |
| 352 | channelResponse[timeslot] = NULL; |
| 353 | else |
Thomas Tsou | a1a3ab4 | 2013-10-18 10:50:52 -0400 | [diff] [blame] | 354 | mNoises.insert(avg); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 355 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 356 | |
| 357 | // demodulate burst |
| 358 | SoftVector *burst = NULL; |
| 359 | if ((rxBurst) && (success)) { |
| 360 | if ((corrType==RACH) || (!needDFE)) { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 361 | burst = demodulateBurst(*vectorBurst, mSPSRx, amplitude, TOA); |
| 362 | } else { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 363 | scaleVector(*vectorBurst,complex(1.0,0.0)/amplitude); |
| 364 | burst = equalizeBurst(*vectorBurst, |
| 365 | TOA-chanRespOffset[timeslot], |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 366 | mSPSRx, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 367 | *DFEForward[timeslot], |
| 368 | *DFEFeedback[timeslot]); |
| 369 | } |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 370 | wTime = rxBurst->getTime(); |
Thomas Tsou | a1a3ab4 | 2013-10-18 10:50:52 -0400 | [diff] [blame] | 371 | RSSI = (int) floor(20.0*log10(rxFullScale/avg)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 372 | LOG(DEBUG) << "RSSI: " << RSSI; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 373 | timingOffset = (int) round(TOA * 256.0 / mSPSRx); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | //if (burst) LOG(DEBUG) << "burst: " << *burst << '\n'; |
| 377 | |
| 378 | delete rxBurst; |
| 379 | |
| 380 | return burst; |
| 381 | } |
| 382 | |
| 383 | void Transceiver::start() |
| 384 | { |
| 385 | mControlServiceLoopThread->start((void * (*)(void*))ControlServiceLoopAdapter,(void*) this); |
| 386 | } |
| 387 | |
| 388 | void Transceiver::reset() |
| 389 | { |
| 390 | mTransmitPriorityQueue.clear(); |
| 391 | //mTransmitFIFO->clear(); |
| 392 | //mReceiveFIFO->clear(); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | void Transceiver::driveControl() |
| 397 | { |
| 398 | |
| 399 | int MAX_PACKET_LENGTH = 100; |
| 400 | |
| 401 | // check control socket |
| 402 | char buffer[MAX_PACKET_LENGTH]; |
| 403 | int msgLen = -1; |
| 404 | buffer[0] = '\0'; |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 405 | |
| 406 | msgLen = mCtrlSocket->read(buffer); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 407 | |
| 408 | if (msgLen < 1) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | char cmdcheck[4]; |
| 413 | char command[MAX_PACKET_LENGTH]; |
| 414 | char response[MAX_PACKET_LENGTH]; |
| 415 | |
| 416 | sscanf(buffer,"%3s %s",cmdcheck,command); |
| 417 | |
| 418 | writeClockInterface(); |
| 419 | |
| 420 | if (strcmp(cmdcheck,"CMD")!=0) { |
| 421 | LOG(WARNING) << "bogus message on control interface"; |
| 422 | return; |
| 423 | } |
| 424 | LOG(INFO) << "command is " << buffer; |
| 425 | |
| 426 | if (strcmp(command,"POWEROFF")==0) { |
| 427 | // turn off transmitter/demod |
| 428 | sprintf(response,"RSP POWEROFF 0"); |
| 429 | } |
| 430 | else if (strcmp(command,"POWERON")==0) { |
| 431 | // turn on transmitter/demod |
| 432 | if (!mTxFreq || !mRxFreq) |
| 433 | sprintf(response,"RSP POWERON 1"); |
| 434 | else { |
| 435 | sprintf(response,"RSP POWERON 0"); |
| 436 | if (!mOn) { |
| 437 | // Prepare for thread start |
| 438 | mPower = -20; |
| 439 | mRadioInterface->start(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 440 | |
| 441 | // Start radio interface threads. |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 442 | mTxServiceLoopThread->start((void * (*)(void*))TxServiceLoopAdapter,(void*) this); |
| 443 | mRxServiceLoopThread->start((void * (*)(void*))RxServiceLoopAdapter,(void*) this); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 444 | mTransmitPriorityQueueServiceLoopThread->start((void * (*)(void*))TransmitPriorityQueueServiceLoopAdapter,(void*) this); |
| 445 | writeClockInterface(); |
| 446 | |
| 447 | mOn = true; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | else if (strcmp(command,"SETMAXDLY")==0) { |
| 452 | //set expected maximum time-of-arrival |
| 453 | int maxDelay; |
| 454 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&maxDelay); |
| 455 | mMaxExpectedDelay = maxDelay; // 1 GSM symbol is approx. 1 km |
| 456 | sprintf(response,"RSP SETMAXDLY 0 %d",maxDelay); |
| 457 | } |
| 458 | else if (strcmp(command,"SETRXGAIN")==0) { |
| 459 | //set expected maximum time-of-arrival |
| 460 | int newGain; |
| 461 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&newGain); |
| 462 | newGain = mRadioInterface->setRxGain(newGain); |
| 463 | sprintf(response,"RSP SETRXGAIN 0 %d",newGain); |
| 464 | } |
| 465 | else if (strcmp(command,"NOISELEV")==0) { |
| 466 | if (mOn) { |
| 467 | sprintf(response,"RSP NOISELEV 0 %d", |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 468 | (int) round(20.0*log10(rxFullScale/mNoiseLev))); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 469 | } |
| 470 | else { |
| 471 | sprintf(response,"RSP NOISELEV 1 0"); |
| 472 | } |
| 473 | } |
| 474 | else if (strcmp(command,"SETPOWER")==0) { |
| 475 | // set output power in dB |
| 476 | int dbPwr; |
| 477 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&dbPwr); |
| 478 | if (!mOn) |
| 479 | sprintf(response,"RSP SETPOWER 1 %d",dbPwr); |
| 480 | else { |
| 481 | mPower = dbPwr; |
kurtis.heimerl | 58d6a01 | 2011-11-26 03:17:38 +0000 | [diff] [blame] | 482 | mRadioInterface->setPowerAttenuation(dbPwr); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 483 | sprintf(response,"RSP SETPOWER 0 %d",dbPwr); |
| 484 | } |
| 485 | } |
| 486 | else if (strcmp(command,"ADJPOWER")==0) { |
| 487 | // adjust power in dB steps |
| 488 | int dbStep; |
| 489 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&dbStep); |
| 490 | if (!mOn) |
| 491 | sprintf(response,"RSP ADJPOWER 1 %d",mPower); |
| 492 | else { |
| 493 | mPower += dbStep; |
| 494 | sprintf(response,"RSP ADJPOWER 0 %d",mPower); |
| 495 | } |
| 496 | } |
| 497 | #define FREQOFFSET 0//11.2e3 |
| 498 | else if (strcmp(command,"RXTUNE")==0) { |
| 499 | // tune receiver |
| 500 | int freqKhz; |
| 501 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&freqKhz); |
| 502 | mRxFreq = freqKhz*1.0e3+FREQOFFSET; |
| 503 | if (!mRadioInterface->tuneRx(mRxFreq)) { |
| 504 | LOG(ALERT) << "RX failed to tune"; |
| 505 | sprintf(response,"RSP RXTUNE 1 %d",freqKhz); |
| 506 | } |
| 507 | else |
| 508 | sprintf(response,"RSP RXTUNE 0 %d",freqKhz); |
| 509 | } |
| 510 | else if (strcmp(command,"TXTUNE")==0) { |
| 511 | // tune txmtr |
| 512 | int freqKhz; |
| 513 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&freqKhz); |
| 514 | //freqKhz = 890e3; |
| 515 | mTxFreq = freqKhz*1.0e3+FREQOFFSET; |
| 516 | if (!mRadioInterface->tuneTx(mTxFreq)) { |
| 517 | LOG(ALERT) << "TX failed to tune"; |
| 518 | sprintf(response,"RSP TXTUNE 1 %d",freqKhz); |
| 519 | } |
| 520 | else |
| 521 | sprintf(response,"RSP TXTUNE 0 %d",freqKhz); |
| 522 | } |
| 523 | else if (strcmp(command,"SETTSC")==0) { |
| 524 | // set TSC |
| 525 | int TSC; |
| 526 | sscanf(buffer,"%3s %s %d",cmdcheck,command,&TSC); |
| 527 | if (mOn) |
| 528 | sprintf(response,"RSP SETTSC 1 %d",TSC); |
| 529 | else { |
| 530 | mTSC = TSC; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 531 | generateMidamble(mSPSRx, TSC); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 532 | sprintf(response,"RSP SETTSC 0 %d", TSC); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | else if (strcmp(command,"SETSLOT")==0) { |
| 536 | // set TSC |
| 537 | int corrCode; |
| 538 | int timeslot; |
| 539 | sscanf(buffer,"%3s %s %d %d",cmdcheck,command,×lot,&corrCode); |
| 540 | if ((timeslot < 0) || (timeslot > 7)) { |
| 541 | LOG(WARNING) << "bogus message on control interface"; |
| 542 | sprintf(response,"RSP SETSLOT 1 %d %d",timeslot,corrCode); |
| 543 | return; |
| 544 | } |
| 545 | mChanType[timeslot] = (ChannelCombination) corrCode; |
| 546 | setModulus(timeslot); |
| 547 | sprintf(response,"RSP SETSLOT 0 %d %d",timeslot,corrCode); |
| 548 | |
| 549 | } |
| 550 | else { |
| 551 | LOG(WARNING) << "bogus command " << command << " on control interface."; |
| 552 | } |
| 553 | |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 554 | mCtrlSocket->write(response, strlen(response) + 1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | bool Transceiver::driveTransmitPriorityQueue() |
| 558 | { |
| 559 | |
| 560 | char buffer[gSlotLen+50]; |
| 561 | |
| 562 | // check data socket |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 563 | size_t msgLen = mDataSocket->read(buffer); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 564 | |
| 565 | if (msgLen!=gSlotLen+1+4+1) { |
| 566 | LOG(ERR) << "badly formatted packet on GSM->TRX interface"; |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | int timeSlot = (int) buffer[0]; |
| 571 | uint64_t frameNum = 0; |
| 572 | for (int i = 0; i < 4; i++) |
| 573 | frameNum = (frameNum << 8) | (0x0ff & buffer[i+1]); |
| 574 | |
| 575 | /* |
| 576 | if (GSM::Time(frameNum,timeSlot) > mTransmitDeadlineClock + GSM::Time(51,0)) { |
| 577 | // stale burst |
| 578 | //LOG(DEBUG) << "FAST! "<< GSM::Time(frameNum,timeSlot); |
| 579 | //writeClockInterface(); |
| 580 | }*/ |
| 581 | |
| 582 | /* |
| 583 | DAB -- Just let these go through the demod. |
| 584 | if (GSM::Time(frameNum,timeSlot) < mTransmitDeadlineClock) { |
| 585 | // stale burst from GSM core |
| 586 | LOG(NOTICE) << "STALE packet on GSM->TRX interface at time "<< GSM::Time(frameNum,timeSlot); |
| 587 | return false; |
| 588 | } |
| 589 | */ |
| 590 | |
| 591 | // periodically update GSM core clock |
| 592 | LOG(DEBUG) << "mTransmitDeadlineClock " << mTransmitDeadlineClock |
| 593 | << " mLastClockUpdateTime " << mLastClockUpdateTime; |
| 594 | if (mTransmitDeadlineClock > mLastClockUpdateTime + GSM::Time(216,0)) |
| 595 | writeClockInterface(); |
| 596 | |
| 597 | |
| 598 | LOG(DEBUG) << "rcvd. burst at: " << GSM::Time(frameNum,timeSlot); |
| 599 | |
| 600 | int RSSI = (int) buffer[5]; |
| 601 | static BitVector newBurst(gSlotLen); |
| 602 | BitVector::iterator itr = newBurst.begin(); |
| 603 | char *bufferItr = buffer+6; |
| 604 | while (itr < newBurst.end()) |
| 605 | *itr++ = *bufferItr++; |
| 606 | |
| 607 | GSM::Time currTime = GSM::Time(frameNum,timeSlot); |
| 608 | |
| 609 | addRadioVector(newBurst,RSSI,currTime); |
| 610 | |
| 611 | LOG(DEBUG) "added burst - time: " << currTime << ", RSSI: " << RSSI; // << ", data: " << newBurst; |
| 612 | |
| 613 | return true; |
| 614 | |
| 615 | |
| 616 | } |
| 617 | |
| 618 | void Transceiver::driveReceiveFIFO() |
| 619 | { |
| 620 | |
| 621 | SoftVector *rxBurst = NULL; |
| 622 | int RSSI; |
| 623 | int TOA; // in 1/256 of a symbol |
| 624 | GSM::Time burstTime; |
| 625 | |
| 626 | mRadioInterface->driveReceiveRadio(); |
| 627 | |
| 628 | rxBurst = pullRadioVector(burstTime,RSSI,TOA); |
| 629 | |
| 630 | if (rxBurst) { |
| 631 | |
| 632 | LOG(DEBUG) << "burst parameters: " |
| 633 | << " time: " << burstTime |
| 634 | << " RSSI: " << RSSI |
| 635 | << " TOA: " << TOA |
| 636 | << " bits: " << *rxBurst; |
| 637 | |
| 638 | char burstString[gSlotLen+10]; |
| 639 | burstString[0] = burstTime.TN(); |
| 640 | for (int i = 0; i < 4; i++) |
| 641 | burstString[1+i] = (burstTime.FN() >> ((3-i)*8)) & 0x0ff; |
| 642 | burstString[5] = RSSI; |
| 643 | burstString[6] = (TOA >> 8) & 0x0ff; |
| 644 | burstString[7] = TOA & 0x0ff; |
| 645 | SoftVector::iterator burstItr = rxBurst->begin(); |
| 646 | |
| 647 | for (unsigned int i = 0; i < gSlotLen; i++) { |
| 648 | burstString[8+i] =(char) round((*burstItr++)*255.0); |
| 649 | } |
| 650 | burstString[gSlotLen+9] = '\0'; |
| 651 | delete rxBurst; |
| 652 | |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 653 | mDataSocket->write(burstString, gSlotLen + 10); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 654 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void Transceiver::driveTransmitFIFO() |
| 658 | { |
| 659 | |
| 660 | /** |
| 661 | Features a carefully controlled latency mechanism, to |
| 662 | assure that transmit packets arrive at the radio/USRP |
| 663 | before they need to be transmitted. |
| 664 | |
| 665 | Deadline clock indicates the burst that needs to be |
| 666 | pushed into the FIFO right NOW. If transmit queue does |
| 667 | not have a burst, stick in filler data. |
| 668 | */ |
| 669 | |
| 670 | |
| 671 | RadioClock *radioClock = (mRadioInterface->getClock()); |
| 672 | |
| 673 | if (mOn) { |
| 674 | //radioClock->wait(); // wait until clock updates |
| 675 | LOG(DEBUG) << "radio clock " << radioClock->get(); |
| 676 | while (radioClock->get() + mTransmitLatency > mTransmitDeadlineClock) { |
| 677 | // if underrun, then we're not providing bursts to radio/USRP fast |
| 678 | // enough. Need to increase latency by one GSM frame. |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 679 | if (mRadioInterface->getWindowType() == RadioDevice::TX_WINDOW_USRP1) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 680 | if (mRadioInterface->isUnderrun()) { |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 681 | // only update latency at the defined frame interval |
| 682 | if (radioClock->get() > mLatencyUpdateTime + GSM::Time(USB_LATENCY_INTRVL)) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 683 | mTransmitLatency = mTransmitLatency + GSM::Time(1,0); |
| 684 | LOG(INFO) << "new latency: " << mTransmitLatency; |
| 685 | mLatencyUpdateTime = radioClock->get(); |
| 686 | } |
| 687 | } |
| 688 | else { |
| 689 | // if underrun hasn't occurred in the last sec (216 frames) drop |
| 690 | // transmit latency by a timeslot |
ttsou | 2173abf | 2012-08-08 00:51:31 +0000 | [diff] [blame] | 691 | if (mTransmitLatency > GSM::Time(USB_LATENCY_MIN)) { |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 692 | if (radioClock->get() > mLatencyUpdateTime + GSM::Time(216,0)) { |
| 693 | mTransmitLatency.decTN(); |
| 694 | LOG(INFO) << "reduced latency: " << mTransmitLatency; |
| 695 | mLatencyUpdateTime = radioClock->get(); |
| 696 | } |
| 697 | } |
| 698 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 699 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 700 | // time to push burst to transmit FIFO |
| 701 | pushRadioVector(mTransmitDeadlineClock); |
| 702 | mTransmitDeadlineClock.incTN(); |
| 703 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 704 | } |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 705 | |
| 706 | radioClock->wait(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
| 710 | |
| 711 | void Transceiver::writeClockInterface() |
| 712 | { |
| 713 | char command[50]; |
| 714 | // FIXME -- This should be adaptive. |
| 715 | sprintf(command,"IND CLOCK %llu",(unsigned long long) (mTransmitDeadlineClock.FN()+2)); |
| 716 | |
| 717 | LOG(INFO) << "ClockInterface: sending " << command; |
| 718 | |
Thomas Tsou | d647ec5 | 2013-10-29 15:17:34 -0400 | [diff] [blame^] | 719 | mClockSocket->write(command, strlen(command) + 1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 720 | |
| 721 | mLastClockUpdateTime = mTransmitDeadlineClock; |
| 722 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 723 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 724 | |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 725 | void *RxServiceLoopAdapter(Transceiver *transceiver) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 726 | { |
kurtis.heimerl | 6b495a5 | 2011-11-26 03:17:21 +0000 | [diff] [blame] | 727 | transceiver->setPriority(); |
| 728 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 729 | while (1) { |
| 730 | transceiver->driveReceiveFIFO(); |
Thomas Tsou | 92c16df | 2013-09-28 18:04:19 -0400 | [diff] [blame] | 731 | pthread_testcancel(); |
| 732 | } |
| 733 | return NULL; |
| 734 | } |
| 735 | |
| 736 | void *TxServiceLoopAdapter(Transceiver *transceiver) |
| 737 | { |
| 738 | while (1) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 739 | transceiver->driveTransmitFIFO(); |
| 740 | pthread_testcancel(); |
| 741 | } |
| 742 | return NULL; |
| 743 | } |
| 744 | |
| 745 | void *ControlServiceLoopAdapter(Transceiver *transceiver) |
| 746 | { |
| 747 | while (1) { |
| 748 | transceiver->driveControl(); |
| 749 | pthread_testcancel(); |
| 750 | } |
| 751 | return NULL; |
| 752 | } |
| 753 | |
| 754 | void *TransmitPriorityQueueServiceLoopAdapter(Transceiver *transceiver) |
| 755 | { |
| 756 | while (1) { |
| 757 | bool stale = false; |
| 758 | // Flush the UDP packets until a successful transfer. |
| 759 | while (!transceiver->driveTransmitPriorityQueue()) { |
| 760 | stale = true; |
| 761 | } |
| 762 | if (stale) { |
| 763 | // If a packet was stale, remind the GSM stack of the clock. |
| 764 | transceiver->writeClockInterface(); |
| 765 | } |
| 766 | pthread_testcancel(); |
| 767 | } |
| 768 | return NULL; |
| 769 | } |