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 | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 33 | #define CHUNK 625 |
| 34 | #define NUMCHUNKS 4 |
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, |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 37 | size_t sps, size_t chans, size_t diversity, |
| 38 | int wReceiveOffset, GSM::Time wStartTime) |
| 39 | : mRadio(wRadio), mSPSTx(sps), mSPSRx(1), mChans(chans), mMIMO(diversity), |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 40 | sendCursor(0), recvCursor(0), underrun(false), overrun(false), |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame^] | 41 | receiveOffset(wReceiveOffset), mOn(false), loadTest(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 42 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 43 | mClock.set(wStartTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 46 | RadioInterface::~RadioInterface(void) |
| 47 | { |
| 48 | close(); |
| 49 | } |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 50 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 51 | bool RadioInterface::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 52 | { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 53 | if ((type != RadioDevice::NORMAL) || (mMIMO > 1) || !mChans) { |
| 54 | LOG(ALERT) << "Invalid configuration"; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 58 | close(); |
| 59 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 60 | sendBuffer.resize(mChans); |
| 61 | recvBuffer.resize(mChans); |
| 62 | convertSendBuffer.resize(mChans); |
| 63 | convertRecvBuffer.resize(mChans); |
| 64 | mReceiveFIFO.resize(mChans); |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame^] | 65 | powerScaling.resize(mChans); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 66 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 67 | for (size_t i = 0; i < mChans; i++) { |
| 68 | sendBuffer[i] = new signalVector(CHUNK * mSPSTx); |
| 69 | recvBuffer[i] = new signalVector(NUMCHUNKS * CHUNK * mSPSRx); |
| 70 | |
| 71 | convertSendBuffer[i] = new short[sendBuffer[i]->size() * 2]; |
| 72 | convertRecvBuffer[i] = new short[recvBuffer[i]->size() * 2]; |
| 73 | } |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 74 | |
| 75 | sendCursor = 0; |
| 76 | recvCursor = 0; |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | void RadioInterface::close() |
| 82 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 83 | for (size_t i = 0; i < sendBuffer.size(); i++) |
| 84 | delete sendBuffer[i]; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 85 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 86 | for (size_t i = 0; i < recvBuffer.size(); i++) |
| 87 | delete recvBuffer[i]; |
| 88 | |
| 89 | for (size_t i = 0; i < convertSendBuffer.size(); i++) |
| 90 | delete convertSendBuffer[i]; |
| 91 | |
| 92 | for (size_t i = 0; i < convertRecvBuffer.size(); i++) |
| 93 | delete convertRecvBuffer[i]; |
| 94 | |
| 95 | sendBuffer.resize(0); |
| 96 | recvBuffer.resize(0); |
| 97 | convertSendBuffer.resize(0); |
| 98 | convertRecvBuffer.resize(0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | double RadioInterface::fullScaleInputValue(void) { |
| 102 | return mRadio->fullScaleInputValue(); |
| 103 | } |
| 104 | |
| 105 | double RadioInterface::fullScaleOutputValue(void) { |
| 106 | return mRadio->fullScaleOutputValue(); |
| 107 | } |
| 108 | |
| 109 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 110 | void RadioInterface::setPowerAttenuation(double atten, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 111 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 112 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 113 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 114 | if (chan >= mChans) { |
| 115 | LOG(ALERT) << "Invalid channel requested"; |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - atten, chan); |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 120 | digAtten = atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 121 | |
| 122 | if (digAtten < 1.0) |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame^] | 123 | powerScaling[chan] = 1.0; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 124 | else |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame^] | 125 | powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 126 | } |
| 127 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 128 | int RadioInterface::radioifyVector(signalVector &wVector, |
| 129 | float *retVector, |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 130 | bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 131 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 132 | if (zero) { |
| 133 | memset(retVector, 0, wVector.size() * 2 * sizeof(float)); |
| 134 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 137 | memcpy(retVector, wVector.begin(), wVector.size() * 2 * sizeof(float)); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 138 | |
| 139 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 140 | } |
| 141 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 142 | int RadioInterface::unRadioifyVector(float *floatVector, |
| 143 | signalVector& newVector) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 144 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 145 | signalVector::iterator itr = newVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 146 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 147 | if (newVector.size() > recvCursor) { |
| 148 | LOG(ALERT) << "Insufficient number of samples in receive buffer"; |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | for (int i = 0; i < newVector.size(); i++) { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 153 | *itr++ = Complex<float>(floatVector[2 * i + 0], |
| 154 | floatVector[2 * i + 1]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 155 | } |
| 156 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 157 | return newVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 160 | bool RadioInterface::tuneTx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 161 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 162 | return mRadio->setTxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 165 | bool RadioInterface::tuneRx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 166 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 167 | return mRadio->setRxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| 171 | void RadioInterface::start() |
| 172 | { |
| 173 | LOG(INFO) << "starting radio interface..."; |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 174 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 175 | mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 176 | (void*)this); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 177 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 178 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 179 | readTimestamp = mRadio->initialReadTimestamp(); |
| 180 | mRadio->start(); |
| 181 | LOG(DEBUG) << "Radio started"; |
| 182 | mRadio->updateAlignment(writeTimestamp-10000); |
| 183 | mRadio->updateAlignment(writeTimestamp-10000); |
| 184 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 185 | mOn = true; |
| 186 | |
| 187 | } |
| 188 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 189 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 190 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 191 | { |
| 192 | while (1) { |
| 193 | radioInterface->alignRadio(); |
| 194 | pthread_testcancel(); |
| 195 | } |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | void RadioInterface::alignRadio() { |
| 200 | sleep(60); |
| 201 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 202 | } |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 203 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 204 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 205 | void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 206 | std::vector<bool> &zeros) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 207 | { |
| 208 | if (!mOn) |
| 209 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 210 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 211 | for (size_t i = 0; i < mChans; i++) { |
| 212 | radioifyVector(*bursts[i], |
| 213 | (float *) (sendBuffer[i]->begin() + sendCursor), zeros[i]); |
| 214 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 215 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 216 | sendCursor += bursts[0]->size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 217 | |
| 218 | pushBuffer(); |
| 219 | } |
| 220 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 221 | bool RadioInterface::driveReceiveRadio() |
| 222 | { |
| 223 | radioVector *burst = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 224 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 225 | if (!mOn) |
| 226 | return false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 227 | |
| 228 | pullBuffer(); |
| 229 | |
| 230 | GSM::Time rcvClock = mClock.get(); |
| 231 | rcvClock.decTN(receiveOffset); |
| 232 | unsigned tN = rcvClock.TN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 233 | int recvSz = recvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 234 | int readSz = 0; |
| 235 | const int symbolsPerSlot = gSlotLen + 8; |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 236 | int burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 237 | |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 238 | /* |
| 239 | * Pre-allocate head room for the largest correlation size |
| 240 | * so we can later avoid a re-allocation and copy |
| 241 | * */ |
| 242 | size_t head = GSM::gRACHSynchSequence.size(); |
| 243 | |
| 244 | /* |
| 245 | * Form receive bursts and pass up to transceiver. Use repeating |
| 246 | * pattern of 157-156-156-156 symbols per timeslot |
| 247 | */ |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 248 | while (recvSz > burstSize) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 249 | for (size_t i = 0; i < mChans; i++) { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 250 | burst = new radioVector(rcvClock, burstSize, head, mMIMO); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 251 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 252 | for (size_t n = 0; n < mMIMO; n++) { |
| 253 | unRadioifyVector((float *) |
| 254 | (recvBuffer[mMIMO * i + n]->begin() + readSz), |
| 255 | *burst->getVector(n)); |
| 256 | } |
| 257 | |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 258 | if (mReceiveFIFO[i].size() < 32) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 259 | mReceiveFIFO[i].write(burst); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 260 | else |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 261 | delete burst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 262 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 263 | |
| 264 | mClock.incTN(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 265 | rcvClock.incTN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 266 | readSz += burstSize; |
| 267 | recvSz -= burstSize; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 268 | |
| 269 | tN = rcvClock.TN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 270 | |
| 271 | burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 272 | } |
| 273 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 274 | if (readSz > 0) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 275 | for (size_t i = 0; i < recvBuffer.size(); i++) { |
| 276 | memmove(recvBuffer[i]->begin(), |
| 277 | recvBuffer[i]->begin() + readSz, |
| 278 | (recvCursor - readSz) * 2 * sizeof(float)); |
| 279 | } |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 280 | |
| 281 | recvCursor -= readSz; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 282 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 283 | |
| 284 | return true; |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool RadioInterface::isUnderrun() |
| 288 | { |
| 289 | bool retVal = underrun; |
| 290 | underrun = false; |
| 291 | |
| 292 | return retVal; |
| 293 | } |
| 294 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 295 | VectorFIFO* RadioInterface::receiveFIFO(size_t chan) |
| 296 | { |
| 297 | if (chan >= mReceiveFIFO.size()) |
| 298 | return NULL; |
| 299 | |
| 300 | return &mReceiveFIFO[chan]; |
| 301 | } |
| 302 | |
| 303 | double RadioInterface::setRxGain(double dB, size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 304 | { |
| 305 | if (mRadio) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 306 | return mRadio->setRxGain(dB, chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 307 | else |
| 308 | return -1; |
| 309 | } |
| 310 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 311 | double RadioInterface::getRxGain(size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 312 | { |
| 313 | if (mRadio) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 314 | return mRadio->getRxGain(chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 315 | else |
| 316 | return -1; |
| 317 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 318 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 319 | /* Receive a timestamped chunk from the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 320 | void RadioInterface::pullBuffer() |
| 321 | { |
| 322 | bool local_underrun; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 323 | int num_recv; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 324 | float *output; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 325 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 326 | if (recvCursor > recvBuffer[0]->size() - CHUNK) |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 327 | return; |
| 328 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 329 | /* Outer buffer access size is fixed */ |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 330 | num_recv = mRadio->readSamples(convertRecvBuffer, |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 331 | CHUNK, |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 332 | &overrun, |
| 333 | readTimestamp, |
| 334 | &local_underrun); |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 335 | if (num_recv != CHUNK) { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 336 | LOG(ALERT) << "Receive error " << num_recv; |
| 337 | return; |
| 338 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 339 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 340 | for (size_t i = 0; i < mChans; i++) { |
| 341 | output = (float *) (recvBuffer[i]->begin() + recvCursor); |
| 342 | convert_short_float(output, convertRecvBuffer[i], 2 * num_recv); |
| 343 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 344 | |
| 345 | underrun |= local_underrun; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 346 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 347 | readTimestamp += num_recv; |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 348 | recvCursor += num_recv; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 351 | /* Send timestamped chunk to the device with arbitrary size */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 352 | void RadioInterface::pushBuffer() |
| 353 | { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 354 | int num_sent; |
| 355 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 356 | if (sendCursor < CHUNK) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 357 | return; |
| 358 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 359 | if (sendCursor > sendBuffer[0]->size()) |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 360 | LOG(ALERT) << "Send buffer overflow"; |
| 361 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 362 | for (size_t i = 0; i < mChans; i++) { |
| 363 | convert_float_short(convertSendBuffer[i], |
| 364 | (float *) sendBuffer[i]->begin(), |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame^] | 365 | powerScaling[i], 2 * sendCursor); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 366 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 367 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 368 | /* Send the all samples in the send buffer */ |
| 369 | num_sent = mRadio->writeSamples(convertSendBuffer, |
| 370 | sendCursor, |
| 371 | &underrun, |
| 372 | writeTimestamp); |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 373 | writeTimestamp += num_sent; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 374 | sendCursor = 0; |
| 375 | } |