blob: f488b0db3bd024777afb3cf87dec977db886502f [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{
Pau Espin Pedrol39e0bba2018-12-03 12:19:51 +010078 for (std::vector<RadioBuffer*>::iterator it = sendBuffer.begin(); it != sendBuffer.end(); ++it)
79 delete *it;
80 for (std::vector<RadioBuffer*>::iterator it = recvBuffer.begin(); it != recvBuffer.end(); ++it)
81 delete *it;
82 for (std::vector<short*>::iterator it = convertSendBuffer.begin(); it != convertSendBuffer.end(); ++it)
83 delete[] *it;
84 for (std::vector<short*>::iterator it = convertRecvBuffer.begin(); it != convertRecvBuffer.end(); ++it)
85 delete[] *it;
Thomas Tsou204a9f12013-10-29 18:34:16 -040086 sendBuffer.resize(0);
87 recvBuffer.resize(0);
88 convertSendBuffer.resize(0);
89 convertRecvBuffer.resize(0);
dburgessb3a0ca42011-10-12 07:44:40 +000090}
91
92double RadioInterface::fullScaleInputValue(void) {
93 return mRadio->fullScaleInputValue();
94}
95
96double RadioInterface::fullScaleOutputValue(void) {
97 return mRadio->fullScaleOutputValue();
98}
99
Tom Tsoua4d1a412014-11-25 15:46:56 -0800100int RadioInterface::setPowerAttenuation(int atten, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000101{
kurtis.heimerl16c65402011-11-26 03:18:11 +0000102 double rfGain, digAtten;
kurtis.heimerlee5347a2011-11-26 03:18:05 +0000103
Thomas Tsou204a9f12013-10-29 18:34:16 -0400104 if (chan >= mChans) {
105 LOG(ALERT) << "Invalid channel requested";
Tom Tsoua4d1a412014-11-25 15:46:56 -0800106 return -1;
Thomas Tsou204a9f12013-10-29 18:34:16 -0400107 }
108
Tom Tsoua4d1a412014-11-25 15:46:56 -0800109 if (atten < 0.0)
110 atten = 0.0;
111
112 rfGain = mRadio->setTxGain(mRadio->maxTxGain() - (double) atten, chan);
113 digAtten = (double) atten - mRadio->maxTxGain() + rfGain;
kurtis.heimerlee5347a2011-11-26 03:18:05 +0000114
115 if (digAtten < 1.0)
Thomas Tsoucb269a32013-11-15 14:15:47 -0500116 powerScaling[chan] = 1.0;
kurtis.heimerlee5347a2011-11-26 03:18:05 +0000117 else
Thomas Tsoucb269a32013-11-15 14:15:47 -0500118 powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0));
Tom Tsoua4d1a412014-11-25 15:46:56 -0800119
120 return atten;
dburgessb3a0ca42011-10-12 07:44:40 +0000121}
122
kurtis.heimerl9b557832011-11-26 03:18:34 +0000123int RadioInterface::radioifyVector(signalVector &wVector,
Tom Tsou28670fb2015-08-21 19:32:58 -0700124 size_t chan, bool zero)
dburgessb3a0ca42011-10-12 07:44:40 +0000125{
Tom Tsou28670fb2015-08-21 19:32:58 -0700126 if (zero)
127 sendBuffer[chan]->zero(wVector.size());
128 else
129 sendBuffer[chan]->write((float *) wVector.begin(), wVector.size());
kurtis.heimerl9b557832011-11-26 03:18:34 +0000130
131 return wVector.size();
dburgessb3a0ca42011-10-12 07:44:40 +0000132}
133
Tom Tsou28670fb2015-08-21 19:32:58 -0700134int RadioInterface::unRadioifyVector(signalVector *newVector, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000135{
Tom Tsou28670fb2015-08-21 19:32:58 -0700136 if (newVector->size() > recvBuffer[chan]->getAvailSamples()) {
Thomas Tsou9471d762013-08-20 21:24:24 -0400137 LOG(ALERT) << "Insufficient number of samples in receive buffer";
138 return -1;
139 }
140
Tom Tsou28670fb2015-08-21 19:32:58 -0700141 recvBuffer[chan]->read((float *) newVector->begin(), newVector->size());
dburgessb3a0ca42011-10-12 07:44:40 +0000142
Tom Tsou28670fb2015-08-21 19:32:58 -0700143 return newVector->size();
dburgessb3a0ca42011-10-12 07:44:40 +0000144}
145
Thomas Tsou204a9f12013-10-29 18:34:16 -0400146bool RadioInterface::tuneTx(double freq, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000147{
Thomas Tsou204a9f12013-10-29 18:34:16 -0400148 return mRadio->setTxFreq(freq, chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000149}
150
Thomas Tsou204a9f12013-10-29 18:34:16 -0400151bool RadioInterface::tuneRx(double freq, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000152{
Thomas Tsou204a9f12013-10-29 18:34:16 -0400153 return mRadio->setRxFreq(freq, chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000154}
155
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200156/** synchronization thread loop */
157void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface)
158{
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +0200159 set_selfthread_name("AlignRadio");
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200160 while (1) {
161 sleep(60);
162 radioInterface->alignRadio();
163 pthread_testcancel();
164 }
165 return NULL;
166}
167
168void RadioInterface::alignRadio() {
169 mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000);
170}
171
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800172bool RadioInterface::start()
dburgessb3a0ca42011-10-12 07:44:40 +0000173{
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800174 if (mOn)
175 return true;
176
177 LOG(INFO) << "Starting radio device";
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200178 if (mRadio->requiresRadioAlign())
179 mAlignRadioServiceLoopThread.start(
180 (void * (*)(void*))AlignRadioServiceLoopAdapter,
181 (void*)this);
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800182
183 if (!mRadio->start())
184 return false;
185
Tom Tsou28670fb2015-08-21 19:32:58 -0700186 for (size_t i = 0; i < mChans; i++) {
187 sendBuffer[i]->reset();
188 recvBuffer[i]->reset();
189 }
Tom Tsoud7610cf2015-05-07 17:39:49 -0700190
dburgessb3a0ca42011-10-12 07:44:40 +0000191 writeTimestamp = mRadio->initialWriteTimestamp();
192 readTimestamp = mRadio->initialReadTimestamp();
Thomas Tsou18d3b832014-02-13 14:55:23 -0500193
194 mRadio->updateAlignment(writeTimestamp-10000);
dburgessb3a0ca42011-10-12 07:44:40 +0000195 mRadio->updateAlignment(writeTimestamp-10000);
196
dburgessb3a0ca42011-10-12 07:44:40 +0000197 mOn = true;
Thomas Tsou18d3b832014-02-13 14:55:23 -0500198 LOG(INFO) << "Radio started";
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800199 return true;
200}
201
202/*
203 * Stop the radio device
204 *
205 * This is a pass-through call to the device interface. Because the underlying
206 * stop command issuance generally doesn't return confirmation on device status,
207 * this call will only return false if the device is already stopped.
208 */
209bool RadioInterface::stop()
210{
211 if (!mOn || !mRadio->stop())
212 return false;
213
214 mOn = false;
215 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000216}
217
Thomas Tsou204a9f12013-10-29 18:34:16 -0400218void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts,
219 std::vector<bool> &zeros)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400220{
221 if (!mOn)
222 return;
dburgessb3a0ca42011-10-12 07:44:40 +0000223
Tom Tsou28670fb2015-08-21 19:32:58 -0700224 for (size_t i = 0; i < mChans; i++)
225 radioifyVector(*bursts[i], i, zeros[i]);
dburgessb3a0ca42011-10-12 07:44:40 +0000226
Tom Tsou28670fb2015-08-21 19:32:58 -0700227 while (pushBuffer());
dburgessb3a0ca42011-10-12 07:44:40 +0000228}
229
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200230int RadioInterface::driveReceiveRadio()
Thomas Tsou204a9f12013-10-29 18:34:16 -0400231{
232 radioVector *burst = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000233
Thomas Tsou204a9f12013-10-29 18:34:16 -0400234 if (!mOn)
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200235 return 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000236
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200237 if (pullBuffer() < 0)
238 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +0000239
240 GSM::Time rcvClock = mClock.get();
241 rcvClock.decTN(receiveOffset);
242 unsigned tN = rcvClock.TN();
Tom Tsou28670fb2015-08-21 19:32:58 -0700243 int recvSz = recvBuffer[0]->getAvailSamples();
dburgessb3a0ca42011-10-12 07:44:40 +0000244 const int symbolsPerSlot = gSlotLen + 8;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800245 int burstSize;
246
247 if (mSPSRx == 4)
248 burstSize = 625;
249 else
250 burstSize = symbolsPerSlot + (tN % 4 == 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000251
Pau Espin Pedrol46444632018-09-03 16:42:04 +0200252 /*
Thomas Tsoud0f3ca32013-11-09 22:05:23 -0500253 * Pre-allocate head room for the largest correlation size
254 * so we can later avoid a re-allocation and copy
255 * */
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200256 size_t head = GSM::gRACHSynchSequenceTS0.size();
Thomas Tsoud0f3ca32013-11-09 22:05:23 -0500257
258 /*
259 * Form receive bursts and pass up to transceiver. Use repeating
260 * pattern of 157-156-156-156 symbols per timeslot
261 */
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500262 while (recvSz > burstSize) {
Thomas Tsou204a9f12013-10-29 18:34:16 -0400263 for (size_t i = 0; i < mChans; i++) {
Tom Tsoud6ae8642017-03-30 17:22:58 -0700264 burst = new radioVector(rcvClock, burstSize, head);
265 unRadioifyVector(burst->getVector(), i);
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500266
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500267 if (mReceiveFIFO[i].size() < 32)
Thomas Tsou204a9f12013-10-29 18:34:16 -0400268 mReceiveFIFO[i].write(burst);
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500269 else
Thomas Tsou204a9f12013-10-29 18:34:16 -0400270 delete burst;
dburgessb3a0ca42011-10-12 07:44:40 +0000271 }
Thomas Tsou204a9f12013-10-29 18:34:16 -0400272
273 mClock.incTN();
dburgessb3a0ca42011-10-12 07:44:40 +0000274 rcvClock.incTN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500275 recvSz -= burstSize;
dburgessb3a0ca42011-10-12 07:44:40 +0000276
277 tN = rcvClock.TN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500278
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800279 if (mSPSRx != 4)
280 burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx;
dburgessb3a0ca42011-10-12 07:44:40 +0000281 }
282
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200283 return 1;
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000284}
285
286bool RadioInterface::isUnderrun()
287{
288 bool retVal = underrun;
289 underrun = false;
290
291 return retVal;
292}
293
Thomas Tsou204a9f12013-10-29 18:34:16 -0400294VectorFIFO* RadioInterface::receiveFIFO(size_t chan)
295{
296 if (chan >= mReceiveFIFO.size())
297 return NULL;
298
299 return &mReceiveFIFO[chan];
300}
301
302double RadioInterface::setRxGain(double dB, size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000303{
Tom Tsou28670fb2015-08-21 19:32:58 -0700304 return mRadio->setRxGain(dB, chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000305}
306
Thomas Tsou204a9f12013-10-29 18:34:16 -0400307double RadioInterface::getRxGain(size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000308{
Tom Tsou28670fb2015-08-21 19:32:58 -0700309 return mRadio->getRxGain(chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000310}
Thomas Tsoucb69f082013-04-08 14:18:26 -0400311
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400312/* Receive a timestamped chunk from the device */
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200313int RadioInterface::pullBuffer()
Thomas Tsoucb69f082013-04-08 14:18:26 -0400314{
315 bool local_underrun;
Pau Espin Pedrol97009692018-09-03 17:01:59 +0200316 int numRecv;
317 size_t segmentLen = recvBuffer[0]->getSegmentLen();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400318
Tom Tsou28670fb2015-08-21 19:32:58 -0700319 if (recvBuffer[0]->getFreeSegments() <= 0)
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200320 return -1;
Thomas Tsou3952d802013-10-15 15:12:24 -0400321
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400322 /* Outer buffer access size is fixed */
Tom Tsou28670fb2015-08-21 19:32:58 -0700323 numRecv = mRadio->readSamples(convertRecvBuffer,
324 segmentLen,
325 &overrun,
326 readTimestamp,
327 &local_underrun);
328
Pau Espin Pedrol97009692018-09-03 17:01:59 +0200329 if ((size_t) numRecv != segmentLen) {
Tom Tsou28670fb2015-08-21 19:32:58 -0700330 LOG(ALERT) << "Receive error " << numRecv;
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200331 return -1;
Thomas Tsou9471d762013-08-20 21:24:24 -0400332 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400333
Thomas Tsou204a9f12013-10-29 18:34:16 -0400334 for (size_t i = 0; i < mChans; i++) {
Tom Tsou28670fb2015-08-21 19:32:58 -0700335 convert_short_float(recvBuffer[i]->getWriteSegment(),
336 convertRecvBuffer[i],
337 segmentLen * 2);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400338 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400339
340 underrun |= local_underrun;
Tom Tsou28670fb2015-08-21 19:32:58 -0700341 readTimestamp += numRecv;
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200342 return 0;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400343}
344
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400345/* Send timestamped chunk to the device with arbitrary size */
Tom Tsou28670fb2015-08-21 19:32:58 -0700346bool RadioInterface::pushBuffer()
Thomas Tsoucb69f082013-04-08 14:18:26 -0400347{
Tom Tsou28670fb2015-08-21 19:32:58 -0700348 size_t numSent, segmentLen = sendBuffer[0]->getSegmentLen();
Thomas Tsou9471d762013-08-20 21:24:24 -0400349
Tom Tsou28670fb2015-08-21 19:32:58 -0700350 if (sendBuffer[0]->getAvailSegments() < 1)
351 return false;
Thomas Tsou3952d802013-10-15 15:12:24 -0400352
Thomas Tsou204a9f12013-10-29 18:34:16 -0400353 for (size_t i = 0; i < mChans; i++) {
354 convert_float_short(convertSendBuffer[i],
Tom Tsoub577ef02016-07-12 16:07:48 -0700355 (float *) sendBuffer[i]->getReadSegment(),
Tom Tsou28670fb2015-08-21 19:32:58 -0700356 powerScaling[i],
357 segmentLen * 2);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400358 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400359
Tom Tsou28670fb2015-08-21 19:32:58 -0700360 /* Send the all samples in the send buffer */
361 numSent = mRadio->writeSamples(convertSendBuffer,
362 segmentLen,
363 &underrun,
364 writeTimestamp);
365 writeTimestamp += numSent;
366
367 return true;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400368}