blob: 6245cfc60833051e88d649333f3c58a02ef48d66 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
Tom Tsou28670fb2015-08-21 19:32:58 -07002 * 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 */
dburgessb3a0ca42011-10-12 07:44:40 +000021
dburgessb3a0ca42011-10-12 07:44:40 +000022#include "radioInterface.h"
Thomas Tsouc1f7c422013-10-11 13:49:55 -040023#include "Resampler.h"
dburgessb3a0ca42011-10-12 07:44:40 +000024#include <Logger.h>
25
Thomas Tsou9471d762013-08-20 21:24:24 -040026extern "C" {
27#include "convert.h"
28}
29
Thomas Tsou3952d802013-10-15 15:12:24 -040030#define CHUNK 625
31#define NUMCHUNKS 4
kurtis.heimerl9b557832011-11-26 03:18:34 +000032
Tom Tsou5cd70dc2016-03-06 01:28:40 -080033RadioInterface::RadioInterface(RadioDevice *wRadio, size_t tx_sps,
Tom Tsoud6ae8642017-03-30 17:22:58 -070034 size_t rx_sps, size_t chans,
Thomas Tsoue90a42b2013-11-13 23:38:09 -050035 int wReceiveOffset, GSM::Time wStartTime)
Tom Tsou5cd70dc2016-03-06 01:28:40 -080036 : mRadio(wRadio), mSPSTx(tx_sps), mSPSRx(rx_sps), mChans(chans),
Tom Tsoud6ae8642017-03-30 17:22:58 -070037 underrun(false), overrun(false), receiveOffset(wReceiveOffset), mOn(false)
dburgessb3a0ca42011-10-12 07:44:40 +000038{
dburgessb3a0ca42011-10-12 07:44:40 +000039 mClock.set(wStartTime);
dburgessb3a0ca42011-10-12 07:44:40 +000040}
41
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040042RadioInterface::~RadioInterface(void)
43{
44 close();
45}
kurtis.heimerl54354042011-11-26 03:18:49 +000046
Thomas Tsoufe269fe2013-10-14 23:56:51 -040047bool RadioInterface::init(int type)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040048{
Tom Tsoud6ae8642017-03-30 17:22:58 -070049 if ((type != RadioDevice::NORMAL) || !mChans) {
Thomas Tsoue90a42b2013-11-13 23:38:09 -050050 LOG(ALERT) << "Invalid configuration";
Thomas Tsou204a9f12013-10-29 18:34:16 -040051 return false;
52 }
53
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040054 close();
55
Thomas Tsou204a9f12013-10-29 18:34:16 -040056 sendBuffer.resize(mChans);
57 recvBuffer.resize(mChans);
58 convertSendBuffer.resize(mChans);
59 convertRecvBuffer.resize(mChans);
60 mReceiveFIFO.resize(mChans);
Thomas Tsoucb269a32013-11-15 14:15:47 -050061 powerScaling.resize(mChans);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040062
Thomas Tsou204a9f12013-10-29 18:34:16 -040063 for (size_t i = 0; i < mChans; i++) {
Tom Tsou28670fb2015-08-21 19:32:58 -070064 sendBuffer[i] = new RadioBuffer(NUMCHUNKS, CHUNK * mSPSTx, 0, true);
65 recvBuffer[i] = new RadioBuffer(NUMCHUNKS, CHUNK * mSPSRx, 0, false);
Thomas Tsou204a9f12013-10-29 18:34:16 -040066
Tom Tsou28670fb2015-08-21 19:32:58 -070067 convertSendBuffer[i] = new short[CHUNK * mSPSTx * 2];
68 convertRecvBuffer[i] = new short[CHUNK * mSPSRx * 2];
Alexander Chemeris19174f52016-06-18 11:16:54 +030069
70 powerScaling[i] = 1.0;
Thomas Tsou204a9f12013-10-29 18:34:16 -040071 }
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040072
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040073 return true;
74}
75
76void RadioInterface::close()
77{
Thomas Tsou204a9f12013-10-29 18:34:16 -040078 sendBuffer.resize(0);
79 recvBuffer.resize(0);
80 convertSendBuffer.resize(0);
81 convertRecvBuffer.resize(0);
dburgessb3a0ca42011-10-12 07:44:40 +000082}
83
84double RadioInterface::fullScaleInputValue(void) {
85 return mRadio->fullScaleInputValue();
86}
87
88double RadioInterface::fullScaleOutputValue(void) {
89 return mRadio->fullScaleOutputValue();
90}
91
Tom Tsoua4d1a412014-11-25 15:46:56 -080092int RadioInterface::setPowerAttenuation(int atten, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +000093{
kurtis.heimerl16c65402011-11-26 03:18:11 +000094 double rfGain, digAtten;
kurtis.heimerlee5347a2011-11-26 03:18:05 +000095
Thomas Tsou204a9f12013-10-29 18:34:16 -040096 if (chan >= mChans) {
97 LOG(ALERT) << "Invalid channel requested";
Tom Tsoua4d1a412014-11-25 15:46:56 -080098 return -1;
Thomas Tsou204a9f12013-10-29 18:34:16 -040099 }
100
Tom Tsoua4d1a412014-11-25 15:46:56 -0800101 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.heimerlee5347a2011-11-26 03:18:05 +0000106
107 if (digAtten < 1.0)
Thomas Tsoucb269a32013-11-15 14:15:47 -0500108 powerScaling[chan] = 1.0;
kurtis.heimerlee5347a2011-11-26 03:18:05 +0000109 else
Thomas Tsoucb269a32013-11-15 14:15:47 -0500110 powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0));
Tom Tsoua4d1a412014-11-25 15:46:56 -0800111
112 return atten;
dburgessb3a0ca42011-10-12 07:44:40 +0000113}
114
kurtis.heimerl9b557832011-11-26 03:18:34 +0000115int RadioInterface::radioifyVector(signalVector &wVector,
Tom Tsou28670fb2015-08-21 19:32:58 -0700116 size_t chan, bool zero)
dburgessb3a0ca42011-10-12 07:44:40 +0000117{
Tom Tsou28670fb2015-08-21 19:32:58 -0700118 if (zero)
119 sendBuffer[chan]->zero(wVector.size());
120 else
121 sendBuffer[chan]->write((float *) wVector.begin(), wVector.size());
kurtis.heimerl9b557832011-11-26 03:18:34 +0000122
123 return wVector.size();
dburgessb3a0ca42011-10-12 07:44:40 +0000124}
125
Tom Tsou28670fb2015-08-21 19:32:58 -0700126int RadioInterface::unRadioifyVector(signalVector *newVector, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000127{
Tom Tsou28670fb2015-08-21 19:32:58 -0700128 if (newVector->size() > recvBuffer[chan]->getAvailSamples()) {
Thomas Tsou9471d762013-08-20 21:24:24 -0400129 LOG(ALERT) << "Insufficient number of samples in receive buffer";
130 return -1;
131 }
132
Tom Tsou28670fb2015-08-21 19:32:58 -0700133 recvBuffer[chan]->read((float *) newVector->begin(), newVector->size());
dburgessb3a0ca42011-10-12 07:44:40 +0000134
Tom Tsou28670fb2015-08-21 19:32:58 -0700135 return newVector->size();
dburgessb3a0ca42011-10-12 07:44:40 +0000136}
137
Thomas Tsou204a9f12013-10-29 18:34:16 -0400138bool RadioInterface::tuneTx(double freq, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000139{
Thomas Tsou204a9f12013-10-29 18:34:16 -0400140 return mRadio->setTxFreq(freq, chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000141}
142
Thomas Tsou204a9f12013-10-29 18:34:16 -0400143bool RadioInterface::tuneRx(double freq, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000144{
Thomas Tsou204a9f12013-10-29 18:34:16 -0400145 return mRadio->setRxFreq(freq, chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000146}
147
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200148/** synchronization thread loop */
149void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface)
150{
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +0200151 set_selfthread_name("AlignRadio");
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200152 while (1) {
153 sleep(60);
154 radioInterface->alignRadio();
155 pthread_testcancel();
156 }
157 return NULL;
158}
159
160void RadioInterface::alignRadio() {
161 mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000);
162}
163
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800164bool RadioInterface::start()
dburgessb3a0ca42011-10-12 07:44:40 +0000165{
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800166 if (mOn)
167 return true;
168
169 LOG(INFO) << "Starting radio device";
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200170 if (mRadio->requiresRadioAlign())
171 mAlignRadioServiceLoopThread.start(
172 (void * (*)(void*))AlignRadioServiceLoopAdapter,
173 (void*)this);
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800174
175 if (!mRadio->start())
176 return false;
177
Tom Tsou28670fb2015-08-21 19:32:58 -0700178 for (size_t i = 0; i < mChans; i++) {
179 sendBuffer[i]->reset();
180 recvBuffer[i]->reset();
181 }
Tom Tsoud7610cf2015-05-07 17:39:49 -0700182
dburgessb3a0ca42011-10-12 07:44:40 +0000183 writeTimestamp = mRadio->initialWriteTimestamp();
184 readTimestamp = mRadio->initialReadTimestamp();
Thomas Tsou18d3b832014-02-13 14:55:23 -0500185
186 mRadio->updateAlignment(writeTimestamp-10000);
dburgessb3a0ca42011-10-12 07:44:40 +0000187 mRadio->updateAlignment(writeTimestamp-10000);
188
dburgessb3a0ca42011-10-12 07:44:40 +0000189 mOn = true;
Thomas Tsou18d3b832014-02-13 14:55:23 -0500190 LOG(INFO) << "Radio started";
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800191 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 */
201bool RadioInterface::stop()
202{
203 if (!mOn || !mRadio->stop())
204 return false;
205
206 mOn = false;
207 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000208}
209
Thomas Tsou204a9f12013-10-29 18:34:16 -0400210void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts,
211 std::vector<bool> &zeros)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400212{
213 if (!mOn)
214 return;
dburgessb3a0ca42011-10-12 07:44:40 +0000215
Tom Tsou28670fb2015-08-21 19:32:58 -0700216 for (size_t i = 0; i < mChans; i++)
217 radioifyVector(*bursts[i], i, zeros[i]);
dburgessb3a0ca42011-10-12 07:44:40 +0000218
Tom Tsou28670fb2015-08-21 19:32:58 -0700219 while (pushBuffer());
dburgessb3a0ca42011-10-12 07:44:40 +0000220}
221
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200222int RadioInterface::driveReceiveRadio()
Thomas Tsou204a9f12013-10-29 18:34:16 -0400223{
224 radioVector *burst = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000225
Thomas Tsou204a9f12013-10-29 18:34:16 -0400226 if (!mOn)
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200227 return 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000228
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200229 if (pullBuffer() < 0)
230 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +0000231
232 GSM::Time rcvClock = mClock.get();
233 rcvClock.decTN(receiveOffset);
234 unsigned tN = rcvClock.TN();
Tom Tsou28670fb2015-08-21 19:32:58 -0700235 int recvSz = recvBuffer[0]->getAvailSamples();
dburgessb3a0ca42011-10-12 07:44:40 +0000236 const int symbolsPerSlot = gSlotLen + 8;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800237 int burstSize;
238
239 if (mSPSRx == 4)
240 burstSize = 625;
241 else
242 burstSize = symbolsPerSlot + (tN % 4 == 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000243
Pau Espin Pedrol46444632018-09-03 16:42:04 +0200244 /*
Thomas Tsoud0f3ca32013-11-09 22:05:23 -0500245 * Pre-allocate head room for the largest correlation size
246 * so we can later avoid a re-allocation and copy
247 * */
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200248 size_t head = GSM::gRACHSynchSequenceTS0.size();
Thomas Tsoud0f3ca32013-11-09 22:05:23 -0500249
250 /*
251 * Form receive bursts and pass up to transceiver. Use repeating
252 * pattern of 157-156-156-156 symbols per timeslot
253 */
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500254 while (recvSz > burstSize) {
Thomas Tsou204a9f12013-10-29 18:34:16 -0400255 for (size_t i = 0; i < mChans; i++) {
Tom Tsoud6ae8642017-03-30 17:22:58 -0700256 burst = new radioVector(rcvClock, burstSize, head);
257 unRadioifyVector(burst->getVector(), i);
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500258
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500259 if (mReceiveFIFO[i].size() < 32)
Thomas Tsou204a9f12013-10-29 18:34:16 -0400260 mReceiveFIFO[i].write(burst);
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500261 else
Thomas Tsou204a9f12013-10-29 18:34:16 -0400262 delete burst;
dburgessb3a0ca42011-10-12 07:44:40 +0000263 }
Thomas Tsou204a9f12013-10-29 18:34:16 -0400264
265 mClock.incTN();
dburgessb3a0ca42011-10-12 07:44:40 +0000266 rcvClock.incTN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500267 recvSz -= burstSize;
dburgessb3a0ca42011-10-12 07:44:40 +0000268
269 tN = rcvClock.TN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500270
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800271 if (mSPSRx != 4)
272 burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx;
dburgessb3a0ca42011-10-12 07:44:40 +0000273 }
274
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200275 return 1;
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000276}
277
278bool RadioInterface::isUnderrun()
279{
280 bool retVal = underrun;
281 underrun = false;
282
283 return retVal;
284}
285
Thomas Tsou204a9f12013-10-29 18:34:16 -0400286VectorFIFO* RadioInterface::receiveFIFO(size_t chan)
287{
288 if (chan >= mReceiveFIFO.size())
289 return NULL;
290
291 return &mReceiveFIFO[chan];
292}
293
294double RadioInterface::setRxGain(double dB, size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000295{
Tom Tsou28670fb2015-08-21 19:32:58 -0700296 return mRadio->setRxGain(dB, chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000297}
298
Thomas Tsou204a9f12013-10-29 18:34:16 -0400299double RadioInterface::getRxGain(size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000300{
Tom Tsou28670fb2015-08-21 19:32:58 -0700301 return mRadio->getRxGain(chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000302}
Thomas Tsoucb69f082013-04-08 14:18:26 -0400303
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400304/* Receive a timestamped chunk from the device */
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200305int RadioInterface::pullBuffer()
Thomas Tsoucb69f082013-04-08 14:18:26 -0400306{
307 bool local_underrun;
Pau Espin Pedrol97009692018-09-03 17:01:59 +0200308 int numRecv;
309 size_t segmentLen = recvBuffer[0]->getSegmentLen();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400310
Tom Tsou28670fb2015-08-21 19:32:58 -0700311 if (recvBuffer[0]->getFreeSegments() <= 0)
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200312 return -1;
Thomas Tsou3952d802013-10-15 15:12:24 -0400313
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400314 /* Outer buffer access size is fixed */
Tom Tsou28670fb2015-08-21 19:32:58 -0700315 numRecv = mRadio->readSamples(convertRecvBuffer,
316 segmentLen,
317 &overrun,
318 readTimestamp,
319 &local_underrun);
320
Pau Espin Pedrol97009692018-09-03 17:01:59 +0200321 if ((size_t) numRecv != segmentLen) {
Tom Tsou28670fb2015-08-21 19:32:58 -0700322 LOG(ALERT) << "Receive error " << numRecv;
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200323 return -1;
Thomas Tsou9471d762013-08-20 21:24:24 -0400324 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400325
Thomas Tsou204a9f12013-10-29 18:34:16 -0400326 for (size_t i = 0; i < mChans; i++) {
Tom Tsou28670fb2015-08-21 19:32:58 -0700327 convert_short_float(recvBuffer[i]->getWriteSegment(),
328 convertRecvBuffer[i],
329 segmentLen * 2);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400330 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400331
332 underrun |= local_underrun;
Tom Tsou28670fb2015-08-21 19:32:58 -0700333 readTimestamp += numRecv;
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200334 return 0;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400335}
336
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400337/* Send timestamped chunk to the device with arbitrary size */
Tom Tsou28670fb2015-08-21 19:32:58 -0700338bool RadioInterface::pushBuffer()
Thomas Tsoucb69f082013-04-08 14:18:26 -0400339{
Tom Tsou28670fb2015-08-21 19:32:58 -0700340 size_t numSent, segmentLen = sendBuffer[0]->getSegmentLen();
Thomas Tsou9471d762013-08-20 21:24:24 -0400341
Tom Tsou28670fb2015-08-21 19:32:58 -0700342 if (sendBuffer[0]->getAvailSegments() < 1)
343 return false;
Thomas Tsou3952d802013-10-15 15:12:24 -0400344
Thomas Tsou204a9f12013-10-29 18:34:16 -0400345 for (size_t i = 0; i < mChans; i++) {
346 convert_float_short(convertSendBuffer[i],
Tom Tsoub577ef02016-07-12 16:07:48 -0700347 (float *) sendBuffer[i]->getReadSegment(),
Tom Tsou28670fb2015-08-21 19:32:58 -0700348 powerScaling[i],
349 segmentLen * 2);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400350 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400351
Tom Tsou28670fb2015-08-21 19:32:58 -0700352 /* 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 Tsoucb69f082013-04-08 14:18:26 -0400360}