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, |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 34 | size_t rx_sps, size_t chans, |
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), |
Harald Welte | 2896cec | 2019-07-21 11:55:22 +0200 | [diff] [blame] | 37 | underrun(false), overrun(false), writeTimestamp(0), readTimestamp(0), |
| 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 | { |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 50 | if ((type != RadioDevice::NORMAL) || !mChans) { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 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 | { |
Pau Espin Pedrol | 39e0bba | 2018-12-03 12:19:51 +0100 | [diff] [blame] | 79 | for (std::vector<RadioBuffer*>::iterator it = sendBuffer.begin(); it != sendBuffer.end(); ++it) |
| 80 | delete *it; |
| 81 | for (std::vector<RadioBuffer*>::iterator it = recvBuffer.begin(); it != recvBuffer.end(); ++it) |
| 82 | delete *it; |
| 83 | for (std::vector<short*>::iterator it = convertSendBuffer.begin(); it != convertSendBuffer.end(); ++it) |
| 84 | delete[] *it; |
| 85 | for (std::vector<short*>::iterator it = convertRecvBuffer.begin(); it != convertRecvBuffer.end(); ++it) |
| 86 | delete[] *it; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 87 | sendBuffer.resize(0); |
| 88 | recvBuffer.resize(0); |
| 89 | convertSendBuffer.resize(0); |
| 90 | convertRecvBuffer.resize(0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | double RadioInterface::fullScaleInputValue(void) { |
| 94 | return mRadio->fullScaleInputValue(); |
| 95 | } |
| 96 | |
| 97 | double RadioInterface::fullScaleOutputValue(void) { |
| 98 | return mRadio->fullScaleOutputValue(); |
| 99 | } |
| 100 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 101 | int RadioInterface::setPowerAttenuation(int atten, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 102 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 103 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 104 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 105 | if (chan >= mChans) { |
| 106 | LOG(ALERT) << "Invalid channel requested"; |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 107 | return -1; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 110 | if (atten < 0.0) |
| 111 | atten = 0.0; |
| 112 | |
| 113 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - (double) atten, chan); |
| 114 | digAtten = (double) atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 115 | |
| 116 | if (digAtten < 1.0) |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 117 | powerScaling[chan] = 1.0; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 118 | else |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 119 | powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0)); |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 120 | |
| 121 | return atten; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 122 | } |
| 123 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 124 | int RadioInterface::radioifyVector(signalVector &wVector, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 125 | size_t chan, bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 126 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 127 | if (zero) |
| 128 | sendBuffer[chan]->zero(wVector.size()); |
| 129 | else |
| 130 | sendBuffer[chan]->write((float *) wVector.begin(), wVector.size()); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 131 | |
| 132 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 135 | int RadioInterface::unRadioifyVector(signalVector *newVector, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 136 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 137 | if (newVector->size() > recvBuffer[chan]->getAvailSamples()) { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 138 | LOG(ALERT) << "Insufficient number of samples in receive buffer"; |
| 139 | return -1; |
| 140 | } |
| 141 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 142 | recvBuffer[chan]->read((float *) newVector->begin(), newVector->size()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 143 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 144 | return newVector->size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 147 | bool RadioInterface::tuneTx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 148 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 149 | return mRadio->setTxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 152 | bool RadioInterface::tuneRx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 153 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 154 | return mRadio->setRxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 157 | /** synchronization thread loop */ |
| 158 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 159 | { |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 160 | set_selfthread_name("AlignRadio"); |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 161 | while (1) { |
| 162 | sleep(60); |
| 163 | radioInterface->alignRadio(); |
| 164 | pthread_testcancel(); |
| 165 | } |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | void RadioInterface::alignRadio() { |
| 170 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 171 | } |
| 172 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 173 | bool RadioInterface::start() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 174 | { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 175 | if (mOn) |
| 176 | return true; |
| 177 | |
| 178 | LOG(INFO) << "Starting radio device"; |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 179 | if (mRadio->requiresRadioAlign()) |
| 180 | mAlignRadioServiceLoopThread.start( |
| 181 | (void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 182 | (void*)this); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 183 | |
| 184 | if (!mRadio->start()) |
| 185 | return false; |
| 186 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 187 | for (size_t i = 0; i < mChans; i++) { |
| 188 | sendBuffer[i]->reset(); |
| 189 | recvBuffer[i]->reset(); |
| 190 | } |
Tom Tsou | d7610cf | 2015-05-07 17:39:49 -0700 | [diff] [blame] | 191 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 192 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 193 | readTimestamp = mRadio->initialReadTimestamp(); |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 194 | |
| 195 | mRadio->updateAlignment(writeTimestamp-10000); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 196 | mRadio->updateAlignment(writeTimestamp-10000); |
| 197 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 198 | mOn = true; |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 199 | LOG(INFO) << "Radio started"; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Stop the radio device |
| 205 | * |
| 206 | * This is a pass-through call to the device interface. Because the underlying |
| 207 | * stop command issuance generally doesn't return confirmation on device status, |
| 208 | * this call will only return false if the device is already stopped. |
| 209 | */ |
| 210 | bool RadioInterface::stop() |
| 211 | { |
| 212 | if (!mOn || !mRadio->stop()) |
| 213 | return false; |
| 214 | |
| 215 | mOn = false; |
| 216 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 219 | void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 220 | std::vector<bool> &zeros) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 221 | { |
| 222 | if (!mOn) |
| 223 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 224 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 225 | for (size_t i = 0; i < mChans; i++) |
| 226 | radioifyVector(*bursts[i], i, zeros[i]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 227 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 228 | while (pushBuffer()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 231 | int RadioInterface::driveReceiveRadio() |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 232 | { |
| 233 | radioVector *burst = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 234 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 235 | if (!mOn) |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 236 | return 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 237 | |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 238 | if (pullBuffer() < 0) |
| 239 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 240 | |
| 241 | GSM::Time rcvClock = mClock.get(); |
| 242 | rcvClock.decTN(receiveOffset); |
| 243 | unsigned tN = rcvClock.TN(); |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 244 | int recvSz = recvBuffer[0]->getAvailSamples(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 245 | const int symbolsPerSlot = gSlotLen + 8; |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 246 | int burstSize; |
| 247 | |
| 248 | if (mSPSRx == 4) |
| 249 | burstSize = 625; |
| 250 | else |
| 251 | burstSize = symbolsPerSlot + (tN % 4 == 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 252 | |
Pau Espin Pedrol | 4644463 | 2018-09-03 16:42:04 +0200 | [diff] [blame] | 253 | /* |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 254 | * Pre-allocate head room for the largest correlation size |
| 255 | * so we can later avoid a re-allocation and copy |
| 256 | * */ |
Vadim Yanitskiy | a79bc70 | 2018-10-17 11:01:58 +0200 | [diff] [blame] | 257 | size_t head = GSM::gRACHSynchSequenceTS0.size(); |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * Form receive bursts and pass up to transceiver. Use repeating |
| 261 | * pattern of 157-156-156-156 symbols per timeslot |
| 262 | */ |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 263 | while (recvSz > burstSize) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 264 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 265 | burst = new radioVector(rcvClock, burstSize, head); |
| 266 | unRadioifyVector(burst->getVector(), i); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 267 | |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 268 | if (mReceiveFIFO[i].size() < 32) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 269 | mReceiveFIFO[i].write(burst); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 270 | else |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 271 | delete burst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 272 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 273 | |
| 274 | mClock.incTN(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 275 | rcvClock.incTN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 276 | recvSz -= burstSize; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 277 | |
| 278 | tN = rcvClock.TN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 279 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 280 | if (mSPSRx != 4) |
| 281 | burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 284 | return 1; |
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 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 305 | return mRadio->setRxGain(dB, chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 308 | double RadioInterface::getRxGain(size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 309 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 310 | return mRadio->getRxGain(chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 311 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 312 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 313 | /* Receive a timestamped chunk from the device */ |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 314 | int RadioInterface::pullBuffer() |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 315 | { |
| 316 | bool local_underrun; |
Pau Espin Pedrol | 9700969 | 2018-09-03 17:01:59 +0200 | [diff] [blame] | 317 | int numRecv; |
| 318 | size_t segmentLen = recvBuffer[0]->getSegmentLen(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 319 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 320 | if (recvBuffer[0]->getFreeSegments() <= 0) |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 321 | return -1; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 322 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 323 | /* Outer buffer access size is fixed */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 324 | numRecv = mRadio->readSamples(convertRecvBuffer, |
| 325 | segmentLen, |
| 326 | &overrun, |
| 327 | readTimestamp, |
| 328 | &local_underrun); |
| 329 | |
Pau Espin Pedrol | 9700969 | 2018-09-03 17:01:59 +0200 | [diff] [blame] | 330 | if ((size_t) numRecv != segmentLen) { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 331 | LOG(ALERT) << "Receive error " << numRecv; |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 332 | return -1; |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 333 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 334 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 335 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 336 | convert_short_float(recvBuffer[i]->getWriteSegment(), |
| 337 | convertRecvBuffer[i], |
| 338 | segmentLen * 2); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 339 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 340 | |
| 341 | underrun |= local_underrun; |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 342 | readTimestamp += numRecv; |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 343 | return 0; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 346 | /* Send timestamped chunk to the device with arbitrary size */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 347 | bool RadioInterface::pushBuffer() |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 348 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 349 | size_t numSent, segmentLen = sendBuffer[0]->getSegmentLen(); |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 350 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 351 | if (sendBuffer[0]->getAvailSegments() < 1) |
| 352 | return false; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 353 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 354 | for (size_t i = 0; i < mChans; i++) { |
| 355 | convert_float_short(convertSendBuffer[i], |
Tom Tsou | b577ef0 | 2016-07-12 16:07:48 -0700 | [diff] [blame] | 356 | (float *) sendBuffer[i]->getReadSegment(), |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 357 | powerScaling[i], |
| 358 | segmentLen * 2); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 359 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 360 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 361 | /* Send the all samples in the send buffer */ |
| 362 | numSent = mRadio->writeSamples(convertSendBuffer, |
| 363 | segmentLen, |
| 364 | &underrun, |
| 365 | writeTimestamp); |
| 366 | writeTimestamp += numSent; |
| 367 | |
| 368 | return true; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 369 | } |