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