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), |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 37 | underrun(false), overrun(false), receiveOffset(wReceiveOffset), mOn(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 38 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 39 | mClock.set(wStartTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 42 | RadioInterface::~RadioInterface(void) |
| 43 | { |
| 44 | close(); |
| 45 | } |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 46 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 47 | bool RadioInterface::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 48 | { |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 49 | if ((type != RadioDevice::NORMAL) || !mChans) { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 50 | LOG(ALERT) << "Invalid configuration"; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 54 | close(); |
| 55 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 56 | sendBuffer.resize(mChans); |
| 57 | recvBuffer.resize(mChans); |
| 58 | convertSendBuffer.resize(mChans); |
| 59 | convertRecvBuffer.resize(mChans); |
| 60 | mReceiveFIFO.resize(mChans); |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 61 | powerScaling.resize(mChans); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 62 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 63 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 64 | sendBuffer[i] = new RadioBuffer(NUMCHUNKS, CHUNK * mSPSTx, 0, true); |
| 65 | recvBuffer[i] = new RadioBuffer(NUMCHUNKS, CHUNK * mSPSRx, 0, false); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 66 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 67 | convertSendBuffer[i] = new short[CHUNK * mSPSTx * 2]; |
| 68 | convertRecvBuffer[i] = new short[CHUNK * mSPSRx * 2]; |
Alexander Chemeris | 19174f5 | 2016-06-18 11:16:54 +0300 | [diff] [blame] | 69 | |
| 70 | powerScaling[i] = 1.0; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 71 | } |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 72 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
| 76 | void RadioInterface::close() |
| 77 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 78 | sendBuffer.resize(0); |
| 79 | recvBuffer.resize(0); |
| 80 | convertSendBuffer.resize(0); |
| 81 | convertRecvBuffer.resize(0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | double RadioInterface::fullScaleInputValue(void) { |
| 85 | return mRadio->fullScaleInputValue(); |
| 86 | } |
| 87 | |
| 88 | double RadioInterface::fullScaleOutputValue(void) { |
| 89 | return mRadio->fullScaleOutputValue(); |
| 90 | } |
| 91 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 92 | int RadioInterface::setPowerAttenuation(int atten, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 93 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 94 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 95 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 96 | if (chan >= mChans) { |
| 97 | LOG(ALERT) << "Invalid channel requested"; |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 98 | return -1; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 101 | if (atten < 0.0) |
| 102 | atten = 0.0; |
| 103 | |
| 104 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - (double) atten, chan); |
| 105 | digAtten = (double) atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 106 | |
| 107 | if (digAtten < 1.0) |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 108 | powerScaling[chan] = 1.0; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 109 | else |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 110 | powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0)); |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 111 | |
| 112 | return atten; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 113 | } |
| 114 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 115 | int RadioInterface::radioifyVector(signalVector &wVector, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 116 | size_t chan, bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 117 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 118 | if (zero) |
| 119 | sendBuffer[chan]->zero(wVector.size()); |
| 120 | else |
| 121 | sendBuffer[chan]->write((float *) wVector.begin(), wVector.size()); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 122 | |
| 123 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 126 | int RadioInterface::unRadioifyVector(signalVector *newVector, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 127 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 128 | if (newVector->size() > recvBuffer[chan]->getAvailSamples()) { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 129 | LOG(ALERT) << "Insufficient number of samples in receive buffer"; |
| 130 | return -1; |
| 131 | } |
| 132 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 133 | recvBuffer[chan]->read((float *) newVector->begin(), newVector->size()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 134 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 135 | return newVector->size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 138 | bool RadioInterface::tuneTx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 139 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 140 | return mRadio->setTxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 143 | bool RadioInterface::tuneRx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 144 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 145 | return mRadio->setRxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 148 | /** synchronization thread loop */ |
| 149 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 150 | { |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 151 | set_selfthread_name("AlignRadio"); |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 152 | while (1) { |
| 153 | sleep(60); |
| 154 | radioInterface->alignRadio(); |
| 155 | pthread_testcancel(); |
| 156 | } |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | void RadioInterface::alignRadio() { |
| 161 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 162 | } |
| 163 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 164 | bool RadioInterface::start() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 165 | { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 166 | if (mOn) |
| 167 | return true; |
| 168 | |
| 169 | LOG(INFO) << "Starting radio device"; |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 170 | if (mRadio->requiresRadioAlign()) |
| 171 | mAlignRadioServiceLoopThread.start( |
| 172 | (void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 173 | (void*)this); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 174 | |
| 175 | if (!mRadio->start()) |
| 176 | return false; |
| 177 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 178 | for (size_t i = 0; i < mChans; i++) { |
| 179 | sendBuffer[i]->reset(); |
| 180 | recvBuffer[i]->reset(); |
| 181 | } |
Tom Tsou | d7610cf | 2015-05-07 17:39:49 -0700 | [diff] [blame] | 182 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 183 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 184 | readTimestamp = mRadio->initialReadTimestamp(); |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 185 | |
| 186 | mRadio->updateAlignment(writeTimestamp-10000); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 187 | mRadio->updateAlignment(writeTimestamp-10000); |
| 188 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 189 | mOn = true; |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 190 | LOG(INFO) << "Radio started"; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Stop the radio device |
| 196 | * |
| 197 | * This is a pass-through call to the device interface. Because the underlying |
| 198 | * stop command issuance generally doesn't return confirmation on device status, |
| 199 | * this call will only return false if the device is already stopped. |
| 200 | */ |
| 201 | bool RadioInterface::stop() |
| 202 | { |
| 203 | if (!mOn || !mRadio->stop()) |
| 204 | return false; |
| 205 | |
| 206 | mOn = false; |
| 207 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 210 | void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 211 | std::vector<bool> &zeros) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 212 | { |
| 213 | if (!mOn) |
| 214 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 215 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 216 | for (size_t i = 0; i < mChans; i++) |
| 217 | radioifyVector(*bursts[i], i, zeros[i]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 218 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 219 | while (pushBuffer()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 222 | int RadioInterface::driveReceiveRadio() |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 223 | { |
| 224 | radioVector *burst = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 225 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 226 | if (!mOn) |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 227 | return 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 228 | |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 229 | if (pullBuffer() < 0) |
| 230 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 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 | |
Pau Espin Pedrol | 4644463 | 2018-09-03 16:42:04 +0200 | [diff] [blame] | 244 | /* |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 245 | * Pre-allocate head room for the largest correlation size |
| 246 | * so we can later avoid a re-allocation and copy |
| 247 | * */ |
Vadim Yanitskiy | a79bc70 | 2018-10-17 11:01:58 +0200 | [diff] [blame] | 248 | size_t head = GSM::gRACHSynchSequenceTS0.size(); |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 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++) { |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 256 | burst = new radioVector(rcvClock, burstSize, head); |
| 257 | unRadioifyVector(burst->getVector(), i); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 258 | |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 259 | if (mReceiveFIFO[i].size() < 32) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 260 | mReceiveFIFO[i].write(burst); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 261 | else |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 262 | delete burst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 263 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 264 | |
| 265 | mClock.incTN(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 266 | rcvClock.incTN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 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 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 271 | if (mSPSRx != 4) |
| 272 | burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 275 | return 1; |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | bool RadioInterface::isUnderrun() |
| 279 | { |
| 280 | bool retVal = underrun; |
| 281 | underrun = false; |
| 282 | |
| 283 | return retVal; |
| 284 | } |
| 285 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 286 | VectorFIFO* RadioInterface::receiveFIFO(size_t chan) |
| 287 | { |
| 288 | if (chan >= mReceiveFIFO.size()) |
| 289 | return NULL; |
| 290 | |
| 291 | return &mReceiveFIFO[chan]; |
| 292 | } |
| 293 | |
| 294 | double RadioInterface::setRxGain(double dB, size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 295 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 296 | return mRadio->setRxGain(dB, chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 299 | double RadioInterface::getRxGain(size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 300 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 301 | return mRadio->getRxGain(chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 302 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 303 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 304 | /* Receive a timestamped chunk from the device */ |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 305 | int RadioInterface::pullBuffer() |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 306 | { |
| 307 | bool local_underrun; |
Pau Espin Pedrol | 9700969 | 2018-09-03 17:01:59 +0200 | [diff] [blame] | 308 | int numRecv; |
| 309 | size_t segmentLen = recvBuffer[0]->getSegmentLen(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 310 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 311 | if (recvBuffer[0]->getFreeSegments() <= 0) |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 312 | return -1; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 313 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 314 | /* Outer buffer access size is fixed */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 315 | numRecv = mRadio->readSamples(convertRecvBuffer, |
| 316 | segmentLen, |
| 317 | &overrun, |
| 318 | readTimestamp, |
| 319 | &local_underrun); |
| 320 | |
Pau Espin Pedrol | 9700969 | 2018-09-03 17:01:59 +0200 | [diff] [blame] | 321 | if ((size_t) numRecv != segmentLen) { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 322 | LOG(ALERT) << "Receive error " << numRecv; |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 323 | return -1; |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 324 | } |
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 | for (size_t i = 0; i < mChans; i++) { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 327 | convert_short_float(recvBuffer[i]->getWriteSegment(), |
| 328 | convertRecvBuffer[i], |
| 329 | segmentLen * 2); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 330 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 331 | |
| 332 | underrun |= local_underrun; |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 333 | readTimestamp += numRecv; |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 334 | return 0; |
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 | b577ef0 | 2016-07-12 16:07:48 -0700 | [diff] [blame] | 347 | (float *) sendBuffer[i]->getReadSegment(), |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 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 | } |