dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2009 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This software is distributed under the terms of the GNU Affero 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 Affero 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 Affero General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU Affero General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | */ |
| 24 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 25 | #include "radioInterface.h" |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 26 | #include "Resampler.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 27 | #include <Logger.h> |
| 28 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 29 | extern "C" { |
| 30 | #include "convert.h" |
| 31 | } |
| 32 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 33 | #define INCHUNK (625 * SAMPSPERSYM) |
| 34 | #define OUTCHUNK (625 * SAMPSPERSYM) |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 35 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 36 | RadioInterface::RadioInterface(RadioDevice *wRadio, |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 37 | int wReceiveOffset, |
Thomas Tsou | 312e387 | 2013-04-08 13:45:55 -0400 | [diff] [blame] | 38 | int wSPS, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 39 | GSM::Time wStartTime) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 40 | : underrun(false), sendCursor(0), recvCursor(0), mOn(false), |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 41 | mRadio(wRadio), receiveOffset(wReceiveOffset), |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 42 | mSPSTx(wSPS), mSPSRx(1), powerScaling(1.0), |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 43 | loadTest(false), sendBuffer(NULL), recvBuffer(NULL), |
| 44 | convertRecvBuffer(NULL), convertSendBuffer(NULL) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 45 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 46 | mClock.set(wStartTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 49 | RadioInterface::~RadioInterface(void) |
| 50 | { |
| 51 | close(); |
| 52 | } |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 53 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 54 | bool RadioInterface::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 55 | { |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 56 | if (type != RadioDevice::NORMAL) |
| 57 | return false; |
| 58 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 59 | close(); |
| 60 | |
| 61 | sendBuffer = new signalVector(OUTCHUNK * 20); |
| 62 | recvBuffer = new signalVector(INCHUNK * 20); |
| 63 | |
| 64 | convertSendBuffer = new short[OUTCHUNK * 2 * 20]; |
| 65 | convertRecvBuffer = new short[OUTCHUNK * 2 * 2]; |
| 66 | |
| 67 | sendCursor = 0; |
| 68 | recvCursor = 0; |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | void RadioInterface::close() |
| 74 | { |
| 75 | delete sendBuffer; |
| 76 | delete recvBuffer; |
| 77 | delete convertSendBuffer; |
| 78 | delete convertRecvBuffer; |
| 79 | |
| 80 | sendBuffer = NULL; |
| 81 | recvBuffer = NULL; |
| 82 | convertRecvBuffer = NULL; |
| 83 | convertSendBuffer = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 86 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 87 | double RadioInterface::fullScaleInputValue(void) { |
| 88 | return mRadio->fullScaleInputValue(); |
| 89 | } |
| 90 | |
| 91 | double RadioInterface::fullScaleOutputValue(void) { |
| 92 | return mRadio->fullScaleOutputValue(); |
| 93 | } |
| 94 | |
| 95 | |
kurtis.heimerl | 58d6a01 | 2011-11-26 03:17:38 +0000 | [diff] [blame] | 96 | void RadioInterface::setPowerAttenuation(double atten) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 97 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 98 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 99 | |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 100 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - atten); |
| 101 | digAtten = atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 102 | |
| 103 | if (digAtten < 1.0) |
| 104 | powerScaling = 1.0; |
| 105 | else |
| 106 | powerScaling = 1.0/sqrt(pow(10, (digAtten/10.0))); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 107 | } |
| 108 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 109 | int RadioInterface::radioifyVector(signalVector &wVector, |
| 110 | float *retVector, |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 111 | bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 112 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 113 | if (zero) { |
| 114 | memset(retVector, 0, wVector.size() * 2 * sizeof(float)); |
| 115 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 118 | memcpy(retVector, wVector.begin(), wVector.size() * 2 * sizeof(float)); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 119 | |
| 120 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 121 | } |
| 122 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 123 | int RadioInterface::unRadioifyVector(float *floatVector, |
| 124 | signalVector& newVector) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 125 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 126 | signalVector::iterator itr = newVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 127 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 128 | if (newVector.size() > recvCursor) { |
| 129 | LOG(ALERT) << "Insufficient number of samples in receive buffer"; |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | for (int i = 0; i < newVector.size(); i++) { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 134 | *itr++ = Complex<float>(floatVector[2 * i + 0], |
| 135 | floatVector[2 * i + 1]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 136 | } |
| 137 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 138 | return newVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool RadioInterface::tuneTx(double freq) |
| 142 | { |
| 143 | return mRadio->setTxFreq(freq); |
| 144 | } |
| 145 | |
| 146 | bool RadioInterface::tuneRx(double freq) |
| 147 | { |
| 148 | return mRadio->setRxFreq(freq); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void RadioInterface::start() |
| 153 | { |
| 154 | LOG(INFO) << "starting radio interface..."; |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 155 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 156 | mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 157 | (void*)this); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 158 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 159 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 160 | readTimestamp = mRadio->initialReadTimestamp(); |
| 161 | mRadio->start(); |
| 162 | LOG(DEBUG) << "Radio started"; |
| 163 | mRadio->updateAlignment(writeTimestamp-10000); |
| 164 | mRadio->updateAlignment(writeTimestamp-10000); |
| 165 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 166 | mOn = true; |
| 167 | |
| 168 | } |
| 169 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 170 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 171 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 172 | { |
| 173 | while (1) { |
| 174 | radioInterface->alignRadio(); |
| 175 | pthread_testcancel(); |
| 176 | } |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | void RadioInterface::alignRadio() { |
| 181 | sleep(60); |
| 182 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 183 | } |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 184 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 185 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 186 | void RadioInterface::driveTransmitRadio(signalVector &radioBurst, bool zeroBurst) |
| 187 | { |
| 188 | if (!mOn) |
| 189 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 190 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 191 | radioifyVector(radioBurst, |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 192 | (float *) (sendBuffer->begin() + sendCursor), zeroBurst); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 193 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 194 | sendCursor += radioBurst.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 195 | |
| 196 | pushBuffer(); |
| 197 | } |
| 198 | |
| 199 | void RadioInterface::driveReceiveRadio() { |
| 200 | |
| 201 | if (!mOn) return; |
| 202 | |
| 203 | if (mReceiveFIFO.size() > 8) return; |
| 204 | |
| 205 | pullBuffer(); |
| 206 | |
| 207 | GSM::Time rcvClock = mClock.get(); |
| 208 | rcvClock.decTN(receiveOffset); |
| 209 | unsigned tN = rcvClock.TN(); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 210 | int rcvSz = recvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 211 | int readSz = 0; |
| 212 | const int symbolsPerSlot = gSlotLen + 8; |
| 213 | |
| 214 | // while there's enough data in receive buffer, form received |
| 215 | // GSM bursts and pass up to Transceiver |
| 216 | // Using the 157-156-156-156 symbols per timeslot format. |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 217 | while (rcvSz > (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx) { |
| 218 | signalVector rxVector((symbolsPerSlot + (tN % 4 == 0)) * mSPSRx); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 219 | unRadioifyVector((float *) (recvBuffer->begin() + readSz), rxVector); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 220 | GSM::Time tmpTime = rcvClock; |
| 221 | if (rcvClock.FN() >= 0) { |
| 222 | //LOG(DEBUG) << "FN: " << rcvClock.FN(); |
| 223 | radioVector *rxBurst = NULL; |
| 224 | if (!loadTest) |
| 225 | rxBurst = new radioVector(rxVector,tmpTime); |
| 226 | else { |
| 227 | if (tN % 4 == 0) |
| 228 | rxBurst = new radioVector(*finalVec9,tmpTime); |
| 229 | else |
| 230 | rxBurst = new radioVector(*finalVec,tmpTime); |
| 231 | } |
| 232 | mReceiveFIFO.put(rxBurst); |
| 233 | } |
| 234 | mClock.incTN(); |
| 235 | rcvClock.incTN(); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 236 | readSz += (symbolsPerSlot+(tN % 4 == 0)) * mSPSRx; |
| 237 | rcvSz -= (symbolsPerSlot+(tN % 4 == 0)) * mSPSRx; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 238 | |
| 239 | tN = rcvClock.TN(); |
| 240 | } |
| 241 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 242 | if (readSz > 0) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 243 | memmove(recvBuffer->begin(), |
| 244 | recvBuffer->begin() + readSz, |
| 245 | (recvCursor - readSz) * 2 * sizeof(float)); |
| 246 | |
| 247 | recvCursor -= readSz; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 248 | } |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | bool RadioInterface::isUnderrun() |
| 252 | { |
| 253 | bool retVal = underrun; |
| 254 | underrun = false; |
| 255 | |
| 256 | return retVal; |
| 257 | } |
| 258 | |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 259 | double RadioInterface::setRxGain(double dB) |
| 260 | { |
| 261 | if (mRadio) |
| 262 | return mRadio->setRxGain(dB); |
| 263 | else |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | double RadioInterface::getRxGain() |
| 268 | { |
| 269 | if (mRadio) |
| 270 | return mRadio->getRxGain(); |
| 271 | else |
| 272 | return -1; |
| 273 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 274 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 275 | /* Receive a timestamped chunk from the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 276 | void RadioInterface::pullBuffer() |
| 277 | { |
| 278 | bool local_underrun; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 279 | int num_recv, len = OUTCHUNK / mSPSTx; |
| 280 | float *output; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 281 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 282 | /* Outer buffer access size is fixed */ |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 283 | num_recv = mRadio->readSamples(convertRecvBuffer, |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 284 | len, |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 285 | &overrun, |
| 286 | readTimestamp, |
| 287 | &local_underrun); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 288 | if (num_recv != len) { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 289 | LOG(ALERT) << "Receive error " << num_recv; |
| 290 | return; |
| 291 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 292 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 293 | output = (float *) (recvBuffer->begin() + recvCursor); |
| 294 | |
| 295 | convert_short_float(output, convertRecvBuffer, 2 * len); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 296 | |
| 297 | underrun |= local_underrun; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 298 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 299 | readTimestamp += num_recv; |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 300 | recvCursor += num_recv; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 303 | /* Send timestamped chunk to the device with arbitrary size */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 304 | void RadioInterface::pushBuffer() |
| 305 | { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 306 | int num_sent; |
| 307 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 308 | if (sendCursor < INCHUNK) |
| 309 | return; |
| 310 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 311 | convert_float_short(convertSendBuffer, |
| 312 | (float *) sendBuffer->begin(), |
| 313 | powerScaling, 2 * sendCursor); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 314 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 315 | /* Send the all samples in the send buffer */ |
| 316 | num_sent = mRadio->writeSamples(convertSendBuffer, |
| 317 | sendCursor, |
| 318 | &underrun, |
| 319 | writeTimestamp); |
| 320 | if (num_sent != sendCursor) { |
| 321 | LOG(ALERT) << "Transmit error " << num_sent; |
| 322 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 323 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 324 | writeTimestamp += num_sent; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 325 | sendCursor = 0; |
| 326 | } |