dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 2 | * Radio device interface |
| 3 | * |
| 4 | * Copyright (C) 2008-2014 Free Software Foundation, Inc. |
| 5 | * Copyright (C) 2015 Ettus Research LLC |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * See the COPYING file in the main directory for details. |
| 20 | */ |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 21 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 22 | #include "radioInterface.h" |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 23 | #include "Resampler.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 24 | #include <Logger.h> |
| 25 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 26 | extern "C" { |
| 27 | #include "convert.h" |
| 28 | } |
| 29 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 30 | #define CHUNK 625 |
| 31 | #define NUMCHUNKS 4 |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 32 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 33 | RadioInterface::RadioInterface(RadioDevice *wRadio, size_t tx_sps, |
| 34 | size_t rx_sps, size_t chans, size_t diversity, |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 35 | int wReceiveOffset, GSM::Time wStartTime) |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 36 | : mRadio(wRadio), mSPSTx(tx_sps), mSPSRx(rx_sps), mChans(chans), |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 37 | mMIMO(diversity), underrun(false), overrun(false), |
| 38 | receiveOffset(wReceiveOffset), mOn(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 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 43 | RadioInterface::~RadioInterface(void) |
| 44 | { |
| 45 | close(); |
| 46 | } |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 47 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 48 | bool RadioInterface::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 49 | { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 50 | if ((type != RadioDevice::NORMAL) || (mMIMO > 1) || !mChans) { |
| 51 | LOG(ALERT) << "Invalid configuration"; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 55 | close(); |
| 56 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 57 | sendBuffer.resize(mChans); |
| 58 | recvBuffer.resize(mChans); |
| 59 | convertSendBuffer.resize(mChans); |
| 60 | convertRecvBuffer.resize(mChans); |
| 61 | mReceiveFIFO.resize(mChans); |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 62 | powerScaling.resize(mChans); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 63 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 64 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 65 | sendBuffer[i] = new RadioBuffer(NUMCHUNKS, CHUNK * mSPSTx, 0, true); |
| 66 | recvBuffer[i] = new RadioBuffer(NUMCHUNKS, CHUNK * mSPSRx, 0, false); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 67 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 68 | convertSendBuffer[i] = new short[CHUNK * mSPSTx * 2]; |
| 69 | convertRecvBuffer[i] = new short[CHUNK * mSPSRx * 2]; |
Alexander Chemeris | 19174f5 | 2016-06-18 11:16:54 +0300 | [diff] [blame] | 70 | |
| 71 | powerScaling[i] = 1.0; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 72 | } |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 73 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | void RadioInterface::close() |
| 78 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 79 | sendBuffer.resize(0); |
| 80 | recvBuffer.resize(0); |
| 81 | convertSendBuffer.resize(0); |
| 82 | convertRecvBuffer.resize(0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | double RadioInterface::fullScaleInputValue(void) { |
| 86 | return mRadio->fullScaleInputValue(); |
| 87 | } |
| 88 | |
| 89 | double RadioInterface::fullScaleOutputValue(void) { |
| 90 | return mRadio->fullScaleOutputValue(); |
| 91 | } |
| 92 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 93 | int RadioInterface::setPowerAttenuation(int atten, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 94 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 95 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 96 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 97 | if (chan >= mChans) { |
| 98 | LOG(ALERT) << "Invalid channel requested"; |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 99 | return -1; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 102 | if (atten < 0.0) |
| 103 | atten = 0.0; |
| 104 | |
| 105 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - (double) atten, chan); |
| 106 | digAtten = (double) atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 107 | |
| 108 | if (digAtten < 1.0) |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 109 | powerScaling[chan] = 1.0; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 110 | else |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 111 | powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0)); |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 112 | |
| 113 | return atten; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 114 | } |
| 115 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 116 | int RadioInterface::radioifyVector(signalVector &wVector, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 117 | size_t chan, bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 118 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 119 | if (zero) |
| 120 | sendBuffer[chan]->zero(wVector.size()); |
| 121 | else |
| 122 | sendBuffer[chan]->write((float *) wVector.begin(), wVector.size()); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 123 | |
| 124 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 127 | int RadioInterface::unRadioifyVector(signalVector *newVector, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 128 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 129 | if (newVector->size() > recvBuffer[chan]->getAvailSamples()) { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 130 | LOG(ALERT) << "Insufficient number of samples in receive buffer"; |
| 131 | return -1; |
| 132 | } |
| 133 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 134 | recvBuffer[chan]->read((float *) newVector->begin(), newVector->size()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 135 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 136 | return newVector->size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 139 | bool RadioInterface::tuneTx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 140 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 141 | return mRadio->setTxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 144 | bool RadioInterface::tuneRx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 145 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 146 | return mRadio->setRxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 149 | bool RadioInterface::start() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 150 | { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 151 | if (mOn) |
| 152 | return true; |
| 153 | |
| 154 | LOG(INFO) << "Starting radio device"; |
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 |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 159 | |
| 160 | if (!mRadio->start()) |
| 161 | return false; |
| 162 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 163 | for (size_t i = 0; i < mChans; i++) { |
| 164 | sendBuffer[i]->reset(); |
| 165 | recvBuffer[i]->reset(); |
| 166 | } |
Tom Tsou | d7610cf | 2015-05-07 17:39:49 -0700 | [diff] [blame] | 167 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 168 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 169 | readTimestamp = mRadio->initialReadTimestamp(); |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 170 | |
| 171 | mRadio->updateAlignment(writeTimestamp-10000); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 172 | mRadio->updateAlignment(writeTimestamp-10000); |
| 173 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 174 | mOn = true; |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 175 | LOG(INFO) << "Radio started"; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Stop the radio device |
| 181 | * |
| 182 | * This is a pass-through call to the device interface. Because the underlying |
| 183 | * stop command issuance generally doesn't return confirmation on device status, |
| 184 | * this call will only return false if the device is already stopped. |
| 185 | */ |
| 186 | bool RadioInterface::stop() |
| 187 | { |
| 188 | if (!mOn || !mRadio->stop()) |
| 189 | return false; |
| 190 | |
| 191 | mOn = false; |
| 192 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 195 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 196 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 197 | { |
| 198 | while (1) { |
| 199 | radioInterface->alignRadio(); |
| 200 | pthread_testcancel(); |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | void RadioInterface::alignRadio() { |
| 206 | sleep(60); |
| 207 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 208 | } |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 209 | #endif |
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 | void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 212 | std::vector<bool> &zeros) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 213 | { |
| 214 | if (!mOn) |
| 215 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 216 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 217 | for (size_t i = 0; i < mChans; i++) |
| 218 | radioifyVector(*bursts[i], i, zeros[i]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 219 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 220 | while (pushBuffer()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 223 | bool RadioInterface::driveReceiveRadio() |
| 224 | { |
| 225 | radioVector *burst = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 226 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 227 | if (!mOn) |
| 228 | return false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 229 | |
| 230 | pullBuffer(); |
| 231 | |
| 232 | GSM::Time rcvClock = mClock.get(); |
| 233 | rcvClock.decTN(receiveOffset); |
| 234 | unsigned tN = rcvClock.TN(); |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 235 | int recvSz = recvBuffer[0]->getAvailSamples(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 236 | const int symbolsPerSlot = gSlotLen + 8; |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 237 | int burstSize; |
| 238 | |
| 239 | if (mSPSRx == 4) |
| 240 | burstSize = 625; |
| 241 | else |
| 242 | burstSize = symbolsPerSlot + (tN % 4 == 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 243 | |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 244 | /* |
| 245 | * Pre-allocate head room for the largest correlation size |
| 246 | * so we can later avoid a re-allocation and copy |
| 247 | * */ |
| 248 | size_t head = GSM::gRACHSynchSequence.size(); |
| 249 | |
| 250 | /* |
| 251 | * Form receive bursts and pass up to transceiver. Use repeating |
| 252 | * pattern of 157-156-156-156 symbols per timeslot |
| 253 | */ |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 254 | while (recvSz > burstSize) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 255 | for (size_t i = 0; i < mChans; i++) { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 256 | burst = new radioVector(rcvClock, burstSize, head, mMIMO); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 257 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 258 | for (size_t n = 0; n < mMIMO; n++) |
| 259 | unRadioifyVector(burst->getVector(n), i); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 260 | |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 261 | if (mReceiveFIFO[i].size() < 32) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 262 | mReceiveFIFO[i].write(burst); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 263 | else |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 264 | delete burst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 265 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 266 | |
| 267 | mClock.incTN(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 268 | rcvClock.incTN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 269 | recvSz -= burstSize; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 270 | |
| 271 | tN = rcvClock.TN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 272 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 273 | if (mSPSRx != 4) |
| 274 | burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 277 | return true; |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | bool RadioInterface::isUnderrun() |
| 281 | { |
| 282 | bool retVal = underrun; |
| 283 | underrun = false; |
| 284 | |
| 285 | return retVal; |
| 286 | } |
| 287 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 288 | VectorFIFO* RadioInterface::receiveFIFO(size_t chan) |
| 289 | { |
| 290 | if (chan >= mReceiveFIFO.size()) |
| 291 | return NULL; |
| 292 | |
| 293 | return &mReceiveFIFO[chan]; |
| 294 | } |
| 295 | |
| 296 | double RadioInterface::setRxGain(double dB, size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 297 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 298 | return mRadio->setRxGain(dB, chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 301 | double RadioInterface::getRxGain(size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 302 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 303 | return mRadio->getRxGain(chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 304 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 305 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 306 | /* Receive a timestamped chunk from the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 307 | void RadioInterface::pullBuffer() |
| 308 | { |
| 309 | bool local_underrun; |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 310 | size_t numRecv, segmentLen = recvBuffer[0]->getSegmentLen(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 311 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 312 | if (recvBuffer[0]->getFreeSegments() <= 0) |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 313 | return; |
| 314 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 315 | /* Outer buffer access size is fixed */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 316 | numRecv = mRadio->readSamples(convertRecvBuffer, |
| 317 | segmentLen, |
| 318 | &overrun, |
| 319 | readTimestamp, |
| 320 | &local_underrun); |
| 321 | |
| 322 | if (numRecv != segmentLen) { |
| 323 | LOG(ALERT) << "Receive error " << numRecv; |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 324 | return; |
| 325 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 326 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 327 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 328 | convert_short_float(recvBuffer[i]->getWriteSegment(), |
| 329 | convertRecvBuffer[i], |
| 330 | segmentLen * 2); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 331 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 332 | |
| 333 | underrun |= local_underrun; |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 334 | readTimestamp += numRecv; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 335 | } |
| 336 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 337 | /* Send timestamped chunk to the device with arbitrary size */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 338 | bool RadioInterface::pushBuffer() |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 339 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 340 | size_t numSent, segmentLen = sendBuffer[0]->getSegmentLen(); |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 341 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 342 | if (sendBuffer[0]->getAvailSegments() < 1) |
| 343 | return false; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 344 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 345 | for (size_t i = 0; i < mChans; i++) { |
| 346 | convert_float_short(convertSendBuffer[i], |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 347 | (float *) sendBuffer[0]->getReadSegment(), |
| 348 | powerScaling[i], |
| 349 | segmentLen * 2); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 350 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 351 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 352 | /* Send the all samples in the send buffer */ |
| 353 | numSent = mRadio->writeSamples(convertSendBuffer, |
| 354 | segmentLen, |
| 355 | &underrun, |
| 356 | writeTimestamp); |
| 357 | writeTimestamp += numSent; |
| 358 | |
| 359 | return true; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 360 | } |