blob: f74897f841b0acf1a3f8da7d423bdb3d1da81c95 [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{
151 while (1) {
152 sleep(60);
153 radioInterface->alignRadio();
154 pthread_testcancel();
155 }
156 return NULL;
157}
158
159void RadioInterface::alignRadio() {
160 mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000);
161}
162
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800163bool RadioInterface::start()
dburgessb3a0ca42011-10-12 07:44:40 +0000164{
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800165 if (mOn)
166 return true;
167
168 LOG(INFO) << "Starting radio device";
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200169 if (mRadio->requiresRadioAlign())
170 mAlignRadioServiceLoopThread.start(
171 (void * (*)(void*))AlignRadioServiceLoopAdapter,
172 (void*)this);
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800173
174 if (!mRadio->start())
175 return false;
176
Tom Tsou28670fb2015-08-21 19:32:58 -0700177 for (size_t i = 0; i < mChans; i++) {
178 sendBuffer[i]->reset();
179 recvBuffer[i]->reset();
180 }
Tom Tsoud7610cf2015-05-07 17:39:49 -0700181
dburgessb3a0ca42011-10-12 07:44:40 +0000182 writeTimestamp = mRadio->initialWriteTimestamp();
183 readTimestamp = mRadio->initialReadTimestamp();
Thomas Tsou18d3b832014-02-13 14:55:23 -0500184
185 mRadio->updateAlignment(writeTimestamp-10000);
dburgessb3a0ca42011-10-12 07:44:40 +0000186 mRadio->updateAlignment(writeTimestamp-10000);
187
dburgessb3a0ca42011-10-12 07:44:40 +0000188 mOn = true;
Thomas Tsou18d3b832014-02-13 14:55:23 -0500189 LOG(INFO) << "Radio started";
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800190 return true;
191}
192
193/*
194 * Stop the radio device
195 *
196 * This is a pass-through call to the device interface. Because the underlying
197 * stop command issuance generally doesn't return confirmation on device status,
198 * this call will only return false if the device is already stopped.
199 */
200bool RadioInterface::stop()
201{
202 if (!mOn || !mRadio->stop())
203 return false;
204
205 mOn = false;
206 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000207}
208
Thomas Tsou204a9f12013-10-29 18:34:16 -0400209void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts,
210 std::vector<bool> &zeros)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400211{
212 if (!mOn)
213 return;
dburgessb3a0ca42011-10-12 07:44:40 +0000214
Tom Tsou28670fb2015-08-21 19:32:58 -0700215 for (size_t i = 0; i < mChans; i++)
216 radioifyVector(*bursts[i], i, zeros[i]);
dburgessb3a0ca42011-10-12 07:44:40 +0000217
Tom Tsou28670fb2015-08-21 19:32:58 -0700218 while (pushBuffer());
dburgessb3a0ca42011-10-12 07:44:40 +0000219}
220
Thomas Tsou204a9f12013-10-29 18:34:16 -0400221bool RadioInterface::driveReceiveRadio()
222{
223 radioVector *burst = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000224
Thomas Tsou204a9f12013-10-29 18:34:16 -0400225 if (!mOn)
226 return false;
dburgessb3a0ca42011-10-12 07:44:40 +0000227
228 pullBuffer();
229
230 GSM::Time rcvClock = mClock.get();
231 rcvClock.decTN(receiveOffset);
232 unsigned tN = rcvClock.TN();
Tom Tsou28670fb2015-08-21 19:32:58 -0700233 int recvSz = recvBuffer[0]->getAvailSamples();
dburgessb3a0ca42011-10-12 07:44:40 +0000234 const int symbolsPerSlot = gSlotLen + 8;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800235 int burstSize;
236
237 if (mSPSRx == 4)
238 burstSize = 625;
239 else
240 burstSize = symbolsPerSlot + (tN % 4 == 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000241
Pau Espin Pedrol46444632018-09-03 16:42:04 +0200242 /*
Thomas Tsoud0f3ca32013-11-09 22:05:23 -0500243 * 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 Tsoue0fa2bf2013-11-09 02:46:29 -0500252 while (recvSz > burstSize) {
Thomas Tsou204a9f12013-10-29 18:34:16 -0400253 for (size_t i = 0; i < mChans; i++) {
Tom Tsoud6ae8642017-03-30 17:22:58 -0700254 burst = new radioVector(rcvClock, burstSize, head);
255 unRadioifyVector(burst->getVector(), i);
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500256
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500257 if (mReceiveFIFO[i].size() < 32)
Thomas Tsou204a9f12013-10-29 18:34:16 -0400258 mReceiveFIFO[i].write(burst);
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500259 else
Thomas Tsou204a9f12013-10-29 18:34:16 -0400260 delete burst;
dburgessb3a0ca42011-10-12 07:44:40 +0000261 }
Thomas Tsou204a9f12013-10-29 18:34:16 -0400262
263 mClock.incTN();
dburgessb3a0ca42011-10-12 07:44:40 +0000264 rcvClock.incTN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500265 recvSz -= burstSize;
dburgessb3a0ca42011-10-12 07:44:40 +0000266
267 tN = rcvClock.TN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500268
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800269 if (mSPSRx != 4)
270 burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx;
dburgessb3a0ca42011-10-12 07:44:40 +0000271 }
272
Thomas Tsou204a9f12013-10-29 18:34:16 -0400273 return true;
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000274}
275
276bool RadioInterface::isUnderrun()
277{
278 bool retVal = underrun;
279 underrun = false;
280
281 return retVal;
282}
283
Thomas Tsou204a9f12013-10-29 18:34:16 -0400284VectorFIFO* RadioInterface::receiveFIFO(size_t chan)
285{
286 if (chan >= mReceiveFIFO.size())
287 return NULL;
288
289 return &mReceiveFIFO[chan];
290}
291
292double RadioInterface::setRxGain(double dB, size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000293{
Tom Tsou28670fb2015-08-21 19:32:58 -0700294 return mRadio->setRxGain(dB, chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000295}
296
Thomas Tsou204a9f12013-10-29 18:34:16 -0400297double RadioInterface::getRxGain(size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000298{
Tom Tsou28670fb2015-08-21 19:32:58 -0700299 return mRadio->getRxGain(chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000300}
Thomas Tsoucb69f082013-04-08 14:18:26 -0400301
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400302/* Receive a timestamped chunk from the device */
Thomas Tsoucb69f082013-04-08 14:18:26 -0400303void RadioInterface::pullBuffer()
304{
305 bool local_underrun;
Tom Tsou28670fb2015-08-21 19:32:58 -0700306 size_t numRecv, segmentLen = recvBuffer[0]->getSegmentLen();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400307
Tom Tsou28670fb2015-08-21 19:32:58 -0700308 if (recvBuffer[0]->getFreeSegments() <= 0)
Thomas Tsou3952d802013-10-15 15:12:24 -0400309 return;
310
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400311 /* Outer buffer access size is fixed */
Tom Tsou28670fb2015-08-21 19:32:58 -0700312 numRecv = mRadio->readSamples(convertRecvBuffer,
313 segmentLen,
314 &overrun,
315 readTimestamp,
316 &local_underrun);
317
318 if (numRecv != segmentLen) {
319 LOG(ALERT) << "Receive error " << numRecv;
Thomas Tsou9471d762013-08-20 21:24:24 -0400320 return;
321 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400322
Thomas Tsou204a9f12013-10-29 18:34:16 -0400323 for (size_t i = 0; i < mChans; i++) {
Tom Tsou28670fb2015-08-21 19:32:58 -0700324 convert_short_float(recvBuffer[i]->getWriteSegment(),
325 convertRecvBuffer[i],
326 segmentLen * 2);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400327 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400328
329 underrun |= local_underrun;
Tom Tsou28670fb2015-08-21 19:32:58 -0700330 readTimestamp += numRecv;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400331}
332
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400333/* Send timestamped chunk to the device with arbitrary size */
Tom Tsou28670fb2015-08-21 19:32:58 -0700334bool RadioInterface::pushBuffer()
Thomas Tsoucb69f082013-04-08 14:18:26 -0400335{
Tom Tsou28670fb2015-08-21 19:32:58 -0700336 size_t numSent, segmentLen = sendBuffer[0]->getSegmentLen();
Thomas Tsou9471d762013-08-20 21:24:24 -0400337
Tom Tsou28670fb2015-08-21 19:32:58 -0700338 if (sendBuffer[0]->getAvailSegments() < 1)
339 return false;
Thomas Tsou3952d802013-10-15 15:12:24 -0400340
Thomas Tsou204a9f12013-10-29 18:34:16 -0400341 for (size_t i = 0; i < mChans; i++) {
342 convert_float_short(convertSendBuffer[i],
Tom Tsoub577ef02016-07-12 16:07:48 -0700343 (float *) sendBuffer[i]->getReadSegment(),
Tom Tsou28670fb2015-08-21 19:32:58 -0700344 powerScaling[i],
345 segmentLen * 2);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400346 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400347
Tom Tsou28670fb2015-08-21 19:32:58 -0700348 /* Send the all samples in the send buffer */
349 numSent = mRadio->writeSamples(convertSendBuffer,
350 segmentLen,
351 &underrun,
352 writeTimestamp);
353 writeTimestamp += numSent;
354
355 return true;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400356}