blob: 14a4fc2d46d89358c99f5409d2929d92f8ac68cb [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
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
dburgessb3a0ca42011-10-12 07:44:40 +000025#include "radioInterface.h"
Thomas Tsouc1f7c422013-10-11 13:49:55 -040026#include "Resampler.h"
dburgessb3a0ca42011-10-12 07:44:40 +000027#include <Logger.h>
28
Thomas Tsou9471d762013-08-20 21:24:24 -040029extern "C" {
30#include "convert.h"
31}
32
Thomas Tsou3952d802013-10-15 15:12:24 -040033#define CHUNK 625
34#define NUMCHUNKS 4
kurtis.heimerl9b557832011-11-26 03:18:34 +000035
dburgessb3a0ca42011-10-12 07:44:40 +000036RadioInterface::RadioInterface(RadioDevice *wRadio,
Thomas Tsoue90a42b2013-11-13 23:38:09 -050037 size_t sps, size_t chans, size_t diversity,
38 int wReceiveOffset, GSM::Time wStartTime)
39 : mRadio(wRadio), mSPSTx(sps), mSPSRx(1), mChans(chans), mMIMO(diversity),
Thomas Tsou204a9f12013-10-29 18:34:16 -040040 sendCursor(0), recvCursor(0), underrun(false), overrun(false),
Thomas Tsou477b77c2013-11-15 16:13:59 -050041 receiveOffset(wReceiveOffset), mOn(false)
dburgessb3a0ca42011-10-12 07:44:40 +000042{
dburgessb3a0ca42011-10-12 07:44:40 +000043 mClock.set(wStartTime);
dburgessb3a0ca42011-10-12 07:44:40 +000044}
45
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040046RadioInterface::~RadioInterface(void)
47{
48 close();
49}
kurtis.heimerl54354042011-11-26 03:18:49 +000050
Thomas Tsoufe269fe2013-10-14 23:56:51 -040051bool RadioInterface::init(int type)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040052{
Thomas Tsoue90a42b2013-11-13 23:38:09 -050053 if ((type != RadioDevice::NORMAL) || (mMIMO > 1) || !mChans) {
54 LOG(ALERT) << "Invalid configuration";
Thomas Tsou204a9f12013-10-29 18:34:16 -040055 return false;
56 }
57
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040058 close();
59
Thomas Tsou204a9f12013-10-29 18:34:16 -040060 sendBuffer.resize(mChans);
61 recvBuffer.resize(mChans);
62 convertSendBuffer.resize(mChans);
63 convertRecvBuffer.resize(mChans);
64 mReceiveFIFO.resize(mChans);
Thomas Tsoucb269a32013-11-15 14:15:47 -050065 powerScaling.resize(mChans);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040066
Thomas Tsou204a9f12013-10-29 18:34:16 -040067 for (size_t i = 0; i < mChans; i++) {
68 sendBuffer[i] = new signalVector(CHUNK * mSPSTx);
69 recvBuffer[i] = new signalVector(NUMCHUNKS * CHUNK * mSPSRx);
70
71 convertSendBuffer[i] = new short[sendBuffer[i]->size() * 2];
72 convertRecvBuffer[i] = new short[recvBuffer[i]->size() * 2];
73 }
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040074
75 sendCursor = 0;
76 recvCursor = 0;
77
78 return true;
79}
80
81void RadioInterface::close()
82{
Thomas Tsou204a9f12013-10-29 18:34:16 -040083 for (size_t i = 0; i < sendBuffer.size(); i++)
84 delete sendBuffer[i];
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040085
Thomas Tsou204a9f12013-10-29 18:34:16 -040086 for (size_t i = 0; i < recvBuffer.size(); i++)
87 delete recvBuffer[i];
88
89 for (size_t i = 0; i < convertSendBuffer.size(); i++)
90 delete convertSendBuffer[i];
91
92 for (size_t i = 0; i < convertRecvBuffer.size(); i++)
93 delete convertRecvBuffer[i];
94
95 sendBuffer.resize(0);
96 recvBuffer.resize(0);
97 convertSendBuffer.resize(0);
98 convertRecvBuffer.resize(0);
dburgessb3a0ca42011-10-12 07:44:40 +000099}
100
101double RadioInterface::fullScaleInputValue(void) {
102 return mRadio->fullScaleInputValue();
103}
104
105double RadioInterface::fullScaleOutputValue(void) {
106 return mRadio->fullScaleOutputValue();
107}
108
Tom Tsoua4d1a412014-11-25 15:46:56 -0800109int RadioInterface::setPowerAttenuation(int atten, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000110{
kurtis.heimerl16c65402011-11-26 03:18:11 +0000111 double rfGain, digAtten;
kurtis.heimerlee5347a2011-11-26 03:18:05 +0000112
Thomas Tsou204a9f12013-10-29 18:34:16 -0400113 if (chan >= mChans) {
114 LOG(ALERT) << "Invalid channel requested";
Tom Tsoua4d1a412014-11-25 15:46:56 -0800115 return -1;
Thomas Tsou204a9f12013-10-29 18:34:16 -0400116 }
117
Tom Tsoua4d1a412014-11-25 15:46:56 -0800118 if (atten < 0.0)
119 atten = 0.0;
120
121 rfGain = mRadio->setTxGain(mRadio->maxTxGain() - (double) atten, chan);
122 digAtten = (double) atten - mRadio->maxTxGain() + rfGain;
kurtis.heimerlee5347a2011-11-26 03:18:05 +0000123
124 if (digAtten < 1.0)
Thomas Tsoucb269a32013-11-15 14:15:47 -0500125 powerScaling[chan] = 1.0;
kurtis.heimerlee5347a2011-11-26 03:18:05 +0000126 else
Thomas Tsoucb269a32013-11-15 14:15:47 -0500127 powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0));
Tom Tsoua4d1a412014-11-25 15:46:56 -0800128
129 return atten;
dburgessb3a0ca42011-10-12 07:44:40 +0000130}
131
kurtis.heimerl9b557832011-11-26 03:18:34 +0000132int RadioInterface::radioifyVector(signalVector &wVector,
133 float *retVector,
kurtis.heimerl9b557832011-11-26 03:18:34 +0000134 bool zero)
dburgessb3a0ca42011-10-12 07:44:40 +0000135{
kurtis.heimerl9b557832011-11-26 03:18:34 +0000136 if (zero) {
137 memset(retVector, 0, wVector.size() * 2 * sizeof(float));
138 return wVector.size();
dburgessb3a0ca42011-10-12 07:44:40 +0000139 }
140
Thomas Tsou9471d762013-08-20 21:24:24 -0400141 memcpy(retVector, wVector.begin(), wVector.size() * 2 * sizeof(float));
kurtis.heimerl9b557832011-11-26 03:18:34 +0000142
143 return wVector.size();
dburgessb3a0ca42011-10-12 07:44:40 +0000144}
145
kurtis.heimerl9b557832011-11-26 03:18:34 +0000146int RadioInterface::unRadioifyVector(float *floatVector,
147 signalVector& newVector)
dburgessb3a0ca42011-10-12 07:44:40 +0000148{
dburgessb3a0ca42011-10-12 07:44:40 +0000149 signalVector::iterator itr = newVector.begin();
kurtis.heimerl9b557832011-11-26 03:18:34 +0000150
Thomas Tsou9471d762013-08-20 21:24:24 -0400151 if (newVector.size() > recvCursor) {
152 LOG(ALERT) << "Insufficient number of samples in receive buffer";
153 return -1;
154 }
155
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500156 for (size_t i = 0; i < newVector.size(); i++) {
kurtis.heimerl9b557832011-11-26 03:18:34 +0000157 *itr++ = Complex<float>(floatVector[2 * i + 0],
158 floatVector[2 * i + 1]);
dburgessb3a0ca42011-10-12 07:44:40 +0000159 }
160
kurtis.heimerl9b557832011-11-26 03:18:34 +0000161 return newVector.size();
dburgessb3a0ca42011-10-12 07:44:40 +0000162}
163
Thomas Tsou204a9f12013-10-29 18:34:16 -0400164bool RadioInterface::tuneTx(double freq, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000165{
Thomas Tsou204a9f12013-10-29 18:34:16 -0400166 return mRadio->setTxFreq(freq, chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000167}
168
Thomas Tsou204a9f12013-10-29 18:34:16 -0400169bool RadioInterface::tuneRx(double freq, size_t chan)
dburgessb3a0ca42011-10-12 07:44:40 +0000170{
Thomas Tsou204a9f12013-10-29 18:34:16 -0400171 return mRadio->setRxFreq(freq, chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000172}
173
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800174bool RadioInterface::start()
dburgessb3a0ca42011-10-12 07:44:40 +0000175{
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800176 if (mOn)
177 return true;
178
179 LOG(INFO) << "Starting radio device";
Thomas Tsouf2293b82013-04-08 13:35:36 -0400180#ifdef USRP1
dburgessb3a0ca42011-10-12 07:44:40 +0000181 mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter,
182 (void*)this);
Thomas Tsouf2293b82013-04-08 13:35:36 -0400183#endif
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800184
185 if (!mRadio->start())
186 return false;
187
Tom Tsoud7610cf2015-05-07 17:39:49 -0700188 recvCursor = 0;
189 sendCursor = 0;
190
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 Tsouf2293b82013-04-08 13:35:36 -0400218#ifdef USRP1
dburgessb3a0ca42011-10-12 07:44:40 +0000219void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface)
220{
221 while (1) {
222 radioInterface->alignRadio();
223 pthread_testcancel();
224 }
225 return NULL;
226}
227
228void RadioInterface::alignRadio() {
229 sleep(60);
230 mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000);
231}
Thomas Tsouf2293b82013-04-08 13:35:36 -0400232#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000233
Thomas Tsou204a9f12013-10-29 18:34:16 -0400234void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts,
235 std::vector<bool> &zeros)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400236{
237 if (!mOn)
238 return;
dburgessb3a0ca42011-10-12 07:44:40 +0000239
Thomas Tsou204a9f12013-10-29 18:34:16 -0400240 for (size_t i = 0; i < mChans; i++) {
241 radioifyVector(*bursts[i],
242 (float *) (sendBuffer[i]->begin() + sendCursor), zeros[i]);
243 }
dburgessb3a0ca42011-10-12 07:44:40 +0000244
Thomas Tsou204a9f12013-10-29 18:34:16 -0400245 sendCursor += bursts[0]->size();
dburgessb3a0ca42011-10-12 07:44:40 +0000246
247 pushBuffer();
248}
249
Thomas Tsou204a9f12013-10-29 18:34:16 -0400250bool RadioInterface::driveReceiveRadio()
251{
252 radioVector *burst = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000253
Thomas Tsou204a9f12013-10-29 18:34:16 -0400254 if (!mOn)
255 return false;
dburgessb3a0ca42011-10-12 07:44:40 +0000256
257 pullBuffer();
258
259 GSM::Time rcvClock = mClock.get();
260 rcvClock.decTN(receiveOffset);
261 unsigned tN = rcvClock.TN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500262 int recvSz = recvCursor;
dburgessb3a0ca42011-10-12 07:44:40 +0000263 int readSz = 0;
264 const int symbolsPerSlot = gSlotLen + 8;
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500265 int burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx;
dburgessb3a0ca42011-10-12 07:44:40 +0000266
Thomas Tsoud0f3ca32013-11-09 22:05:23 -0500267 /*
268 * Pre-allocate head room for the largest correlation size
269 * so we can later avoid a re-allocation and copy
270 * */
271 size_t head = GSM::gRACHSynchSequence.size();
272
273 /*
274 * Form receive bursts and pass up to transceiver. Use repeating
275 * pattern of 157-156-156-156 symbols per timeslot
276 */
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500277 while (recvSz > burstSize) {
Thomas Tsou204a9f12013-10-29 18:34:16 -0400278 for (size_t i = 0; i < mChans; i++) {
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500279 burst = new radioVector(rcvClock, burstSize, head, mMIMO);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400280
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500281 for (size_t n = 0; n < mMIMO; n++) {
282 unRadioifyVector((float *)
283 (recvBuffer[mMIMO * i + n]->begin() + readSz),
284 *burst->getVector(n));
285 }
286
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500287 if (mReceiveFIFO[i].size() < 32)
Thomas Tsou204a9f12013-10-29 18:34:16 -0400288 mReceiveFIFO[i].write(burst);
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500289 else
Thomas Tsou204a9f12013-10-29 18:34:16 -0400290 delete burst;
dburgessb3a0ca42011-10-12 07:44:40 +0000291 }
Thomas Tsou204a9f12013-10-29 18:34:16 -0400292
293 mClock.incTN();
dburgessb3a0ca42011-10-12 07:44:40 +0000294 rcvClock.incTN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500295 readSz += burstSize;
296 recvSz -= burstSize;
dburgessb3a0ca42011-10-12 07:44:40 +0000297
298 tN = rcvClock.TN();
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -0500299
300 burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx;
dburgessb3a0ca42011-10-12 07:44:40 +0000301 }
302
kurtis.heimerl9b557832011-11-26 03:18:34 +0000303 if (readSz > 0) {
Thomas Tsou204a9f12013-10-29 18:34:16 -0400304 for (size_t i = 0; i < recvBuffer.size(); i++) {
305 memmove(recvBuffer[i]->begin(),
306 recvBuffer[i]->begin() + readSz,
307 (recvCursor - readSz) * 2 * sizeof(float));
308 }
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400309
310 recvCursor -= readSz;
dburgessb3a0ca42011-10-12 07:44:40 +0000311 }
Thomas Tsou204a9f12013-10-29 18:34:16 -0400312
313 return true;
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000314}
315
316bool RadioInterface::isUnderrun()
317{
318 bool retVal = underrun;
319 underrun = false;
320
321 return retVal;
322}
323
Thomas Tsou204a9f12013-10-29 18:34:16 -0400324VectorFIFO* RadioInterface::receiveFIFO(size_t chan)
325{
326 if (chan >= mReceiveFIFO.size())
327 return NULL;
328
329 return &mReceiveFIFO[chan];
330}
331
332double RadioInterface::setRxGain(double dB, size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000333{
334 if (mRadio)
Thomas Tsou204a9f12013-10-29 18:34:16 -0400335 return mRadio->setRxGain(dB, chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000336 else
337 return -1;
338}
339
Thomas Tsou204a9f12013-10-29 18:34:16 -0400340double RadioInterface::getRxGain(size_t chan)
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000341{
342 if (mRadio)
Thomas Tsou204a9f12013-10-29 18:34:16 -0400343 return mRadio->getRxGain(chan);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000344 else
345 return -1;
346}
Thomas Tsoucb69f082013-04-08 14:18:26 -0400347
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400348/* Receive a timestamped chunk from the device */
Thomas Tsoucb69f082013-04-08 14:18:26 -0400349void RadioInterface::pullBuffer()
350{
351 bool local_underrun;
Thomas Tsou3952d802013-10-15 15:12:24 -0400352 int num_recv;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400353 float *output;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400354
Thomas Tsou204a9f12013-10-29 18:34:16 -0400355 if (recvCursor > recvBuffer[0]->size() - CHUNK)
Thomas Tsou3952d802013-10-15 15:12:24 -0400356 return;
357
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400358 /* Outer buffer access size is fixed */
Thomas Tsou9471d762013-08-20 21:24:24 -0400359 num_recv = mRadio->readSamples(convertRecvBuffer,
Thomas Tsou3952d802013-10-15 15:12:24 -0400360 CHUNK,
Thomas Tsou9471d762013-08-20 21:24:24 -0400361 &overrun,
362 readTimestamp,
363 &local_underrun);
Thomas Tsou3952d802013-10-15 15:12:24 -0400364 if (num_recv != CHUNK) {
Thomas Tsou9471d762013-08-20 21:24:24 -0400365 LOG(ALERT) << "Receive error " << num_recv;
366 return;
367 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400368
Thomas Tsou204a9f12013-10-29 18:34:16 -0400369 for (size_t i = 0; i < mChans; i++) {
370 output = (float *) (recvBuffer[i]->begin() + recvCursor);
371 convert_short_float(output, convertRecvBuffer[i], 2 * num_recv);
372 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400373
374 underrun |= local_underrun;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400375
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400376 readTimestamp += num_recv;
Thomas Tsou9471d762013-08-20 21:24:24 -0400377 recvCursor += num_recv;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400378}
379
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400380/* Send timestamped chunk to the device with arbitrary size */
Thomas Tsoucb69f082013-04-08 14:18:26 -0400381void RadioInterface::pushBuffer()
382{
Thomas Tsou9471d762013-08-20 21:24:24 -0400383 int num_sent;
384
Thomas Tsou3952d802013-10-15 15:12:24 -0400385 if (sendCursor < CHUNK)
Thomas Tsoucb69f082013-04-08 14:18:26 -0400386 return;
387
Thomas Tsou204a9f12013-10-29 18:34:16 -0400388 if (sendCursor > sendBuffer[0]->size())
Thomas Tsou3952d802013-10-15 15:12:24 -0400389 LOG(ALERT) << "Send buffer overflow";
390
Thomas Tsou204a9f12013-10-29 18:34:16 -0400391 for (size_t i = 0; i < mChans; i++) {
392 convert_float_short(convertSendBuffer[i],
393 (float *) sendBuffer[i]->begin(),
Thomas Tsoucb269a32013-11-15 14:15:47 -0500394 powerScaling[i], 2 * sendCursor);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400395 }
Thomas Tsoucb69f082013-04-08 14:18:26 -0400396
Thomas Tsou9471d762013-08-20 21:24:24 -0400397 /* Send the all samples in the send buffer */
398 num_sent = mRadio->writeSamples(convertSendBuffer,
399 sendCursor,
400 &underrun,
401 writeTimestamp);
Thomas Tsou9471d762013-08-20 21:24:24 -0400402 writeTimestamp += num_sent;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400403 sendCursor = 0;
404}