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" |
| 26 | #include <Logger.h> |
| 27 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 28 | bool started = false; |
| 29 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 30 | RadioInterface::RadioInterface(RadioDevice *wRadio, |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 31 | int wReceiveOffset, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 32 | int wRadioOversampling, |
| 33 | int wTransceiverOversampling, |
| 34 | GSM::Time wStartTime) |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 35 | : underrun(false), sendCursor(0), rcvCursor(0), mOn(false), |
| 36 | mRadio(wRadio), receiveOffset(wReceiveOffset), |
| 37 | samplesPerSymbol(wRadioOversampling), powerScaling(1.0), |
| 38 | loadTest(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 39 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 40 | mClock.set(wStartTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 41 | } |
| 42 | |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 43 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 44 | RadioInterface::~RadioInterface(void) { |
| 45 | if (rcvBuffer!=NULL) delete rcvBuffer; |
| 46 | //mReceiveFIFO.clear(); |
| 47 | } |
| 48 | |
| 49 | double RadioInterface::fullScaleInputValue(void) { |
| 50 | return mRadio->fullScaleInputValue(); |
| 51 | } |
| 52 | |
| 53 | double RadioInterface::fullScaleOutputValue(void) { |
| 54 | return mRadio->fullScaleOutputValue(); |
| 55 | } |
| 56 | |
| 57 | |
kurtis.heimerl | 58d6a01 | 2011-11-26 03:17:38 +0000 | [diff] [blame] | 58 | void RadioInterface::setPowerAttenuation(double atten) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 59 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 60 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 61 | |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 62 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - atten); |
| 63 | digAtten = atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 64 | |
| 65 | if (digAtten < 1.0) |
| 66 | powerScaling = 1.0; |
| 67 | else |
| 68 | powerScaling = 1.0/sqrt(pow(10, (digAtten/10.0))); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 71 | int RadioInterface::radioifyVector(signalVector &wVector, |
| 72 | float *retVector, |
| 73 | float scale, |
| 74 | bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 75 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 76 | int i; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 77 | signalVector::iterator itr = wVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 78 | |
| 79 | if (zero) { |
| 80 | memset(retVector, 0, wVector.size() * 2 * sizeof(float)); |
| 81 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 82 | } |
| 83 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 84 | for (i = 0; i < wVector.size(); i++) { |
| 85 | retVector[2 * i + 0] = itr->real() * scale; |
| 86 | retVector[2 * i + 1] = itr->imag() * scale; |
| 87 | itr++; |
| 88 | } |
| 89 | |
| 90 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 93 | int RadioInterface::unRadioifyVector(float *floatVector, |
| 94 | signalVector& newVector) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 95 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 96 | int i; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 97 | signalVector::iterator itr = newVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 98 | |
| 99 | for (i = 0; i < newVector.size(); i++) { |
| 100 | *itr++ = Complex<float>(floatVector[2 * i + 0], |
| 101 | floatVector[2 * i + 1]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 102 | } |
| 103 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 104 | return newVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool RadioInterface::tuneTx(double freq) |
| 108 | { |
| 109 | return mRadio->setTxFreq(freq); |
| 110 | } |
| 111 | |
| 112 | bool RadioInterface::tuneRx(double freq) |
| 113 | { |
| 114 | return mRadio->setRxFreq(freq); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void RadioInterface::start() |
| 119 | { |
| 120 | LOG(INFO) << "starting radio interface..."; |
| 121 | mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 122 | (void*)this); |
| 123 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 124 | readTimestamp = mRadio->initialReadTimestamp(); |
| 125 | mRadio->start(); |
| 126 | LOG(DEBUG) << "Radio started"; |
| 127 | mRadio->updateAlignment(writeTimestamp-10000); |
| 128 | mRadio->updateAlignment(writeTimestamp-10000); |
| 129 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 130 | sendBuffer = new float[2*2*INCHUNK*samplesPerSymbol]; |
| 131 | rcvBuffer = new float[2*2*OUTCHUNK*samplesPerSymbol]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 132 | |
| 133 | mOn = true; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 138 | { |
| 139 | while (1) { |
| 140 | radioInterface->alignRadio(); |
| 141 | pthread_testcancel(); |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | void RadioInterface::alignRadio() { |
| 147 | sleep(60); |
| 148 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 149 | } |
| 150 | |
| 151 | void RadioInterface::driveTransmitRadio(signalVector &radioBurst, bool zeroBurst) { |
| 152 | |
| 153 | if (!mOn) return; |
| 154 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 155 | radioifyVector(radioBurst, sendBuffer + 2 * sendCursor, powerScaling, zeroBurst); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 156 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 157 | sendCursor += radioBurst.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 158 | |
| 159 | pushBuffer(); |
| 160 | } |
| 161 | |
| 162 | void RadioInterface::driveReceiveRadio() { |
| 163 | |
| 164 | if (!mOn) return; |
| 165 | |
| 166 | if (mReceiveFIFO.size() > 8) return; |
| 167 | |
| 168 | pullBuffer(); |
| 169 | |
| 170 | GSM::Time rcvClock = mClock.get(); |
| 171 | rcvClock.decTN(receiveOffset); |
| 172 | unsigned tN = rcvClock.TN(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 173 | int rcvSz = rcvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 174 | int readSz = 0; |
| 175 | const int symbolsPerSlot = gSlotLen + 8; |
| 176 | |
| 177 | // while there's enough data in receive buffer, form received |
| 178 | // GSM bursts and pass up to Transceiver |
| 179 | // Using the 157-156-156-156 symbols per timeslot format. |
| 180 | while (rcvSz > (symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol) { |
| 181 | signalVector rxVector((symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol); |
| 182 | unRadioifyVector(rcvBuffer+readSz*2,rxVector); |
| 183 | GSM::Time tmpTime = rcvClock; |
| 184 | if (rcvClock.FN() >= 0) { |
| 185 | //LOG(DEBUG) << "FN: " << rcvClock.FN(); |
| 186 | radioVector *rxBurst = NULL; |
| 187 | if (!loadTest) |
| 188 | rxBurst = new radioVector(rxVector,tmpTime); |
| 189 | else { |
| 190 | if (tN % 4 == 0) |
| 191 | rxBurst = new radioVector(*finalVec9,tmpTime); |
| 192 | else |
| 193 | rxBurst = new radioVector(*finalVec,tmpTime); |
| 194 | } |
| 195 | mReceiveFIFO.put(rxBurst); |
| 196 | } |
| 197 | mClock.incTN(); |
| 198 | rcvClock.incTN(); |
| 199 | //if (mReceiveFIFO.size() >= 16) mReceiveFIFO.wait(8); |
| 200 | //LOG(DEBUG) << "receiveFIFO: wrote radio vector at time: " << mClock.get() << ", new size: " << mReceiveFIFO.size() ; |
| 201 | readSz += (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol; |
| 202 | rcvSz -= (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol; |
| 203 | |
| 204 | tN = rcvClock.TN(); |
| 205 | } |
| 206 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 207 | if (readSz > 0) { |
| 208 | rcvCursor -= readSz; |
| 209 | memmove(rcvBuffer,rcvBuffer+2*readSz,sizeof(float) * 2 * rcvCursor); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 210 | } |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bool RadioInterface::isUnderrun() |
| 214 | { |
| 215 | bool retVal = underrun; |
| 216 | underrun = false; |
| 217 | |
| 218 | return retVal; |
| 219 | } |
| 220 | |
| 221 | void RadioInterface::attach(RadioDevice *wRadio, int wRadioOversampling) |
| 222 | { |
| 223 | if (!mOn) { |
| 224 | mRadio = wRadio; |
| 225 | mRadioOversampling = SAMPSPERSYM; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | double RadioInterface::setRxGain(double dB) |
| 230 | { |
| 231 | if (mRadio) |
| 232 | return mRadio->setRxGain(dB); |
| 233 | else |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | double RadioInterface::getRxGain() |
| 238 | { |
| 239 | if (mRadio) |
| 240 | return mRadio->getRxGain(); |
| 241 | else |
| 242 | return -1; |
| 243 | } |