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