2011-10-12 07:44:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2008, 2009 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This software is distributed under the terms of the GNU Affero Public License.
|
|
|
|
* See the COPYING file in the main directory for details.
|
|
|
|
*
|
|
|
|
* This use of this software may be subject to additional restrictions.
|
|
|
|
* See the LEGAL file in the main directory for details.
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "radioInterface.h"
|
|
|
|
#include <Logger.h>
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
bool started = false;
|
|
|
|
|
2011-10-12 07:44:40 +00:00
|
|
|
RadioInterface::RadioInterface(RadioDevice *wRadio,
|
2011-11-26 03:18:49 +00:00
|
|
|
int wReceiveOffset,
|
2011-10-12 07:44:40 +00:00
|
|
|
int wRadioOversampling,
|
|
|
|
int wTransceiverOversampling,
|
|
|
|
GSM::Time wStartTime)
|
2011-11-26 03:18:49 +00:00
|
|
|
: underrun(false), sendCursor(0), rcvCursor(0), mOn(false),
|
|
|
|
mRadio(wRadio), receiveOffset(wReceiveOffset),
|
|
|
|
samplesPerSymbol(wRadioOversampling), powerScaling(1.0),
|
|
|
|
loadTest(false)
|
2011-10-12 07:44:40 +00:00
|
|
|
{
|
|
|
|
mClock.set(wStartTime);
|
|
|
|
}
|
|
|
|
|
2011-11-26 03:18:49 +00:00
|
|
|
|
2011-10-12 07:44:40 +00:00
|
|
|
RadioInterface::~RadioInterface(void) {
|
|
|
|
if (rcvBuffer!=NULL) delete rcvBuffer;
|
|
|
|
//mReceiveFIFO.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
double RadioInterface::fullScaleInputValue(void) {
|
|
|
|
return mRadio->fullScaleInputValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
double RadioInterface::fullScaleOutputValue(void) {
|
|
|
|
return mRadio->fullScaleOutputValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-26 03:17:38 +00:00
|
|
|
void RadioInterface::setPowerAttenuation(double atten)
|
2011-10-12 07:44:40 +00:00
|
|
|
{
|
2011-11-26 03:18:11 +00:00
|
|
|
double rfGain, digAtten;
|
2011-11-26 03:18:05 +00:00
|
|
|
|
2011-11-26 03:18:11 +00:00
|
|
|
rfGain = mRadio->setTxGain(mRadio->maxTxGain() - atten);
|
|
|
|
digAtten = atten - mRadio->maxTxGain() + rfGain;
|
2011-11-26 03:18:05 +00:00
|
|
|
|
|
|
|
if (digAtten < 1.0)
|
|
|
|
powerScaling = 1.0;
|
|
|
|
else
|
|
|
|
powerScaling = 1.0/sqrt(pow(10, (digAtten/10.0)));
|
2011-10-12 07:44:40 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
int RadioInterface::radioifyVector(signalVector &wVector,
|
|
|
|
float *retVector,
|
|
|
|
float scale,
|
|
|
|
bool zero)
|
2011-10-12 07:44:40 +00:00
|
|
|
{
|
2011-11-26 03:18:34 +00:00
|
|
|
int i;
|
2011-10-12 07:44:40 +00:00
|
|
|
signalVector::iterator itr = wVector.begin();
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
if (zero) {
|
|
|
|
memset(retVector, 0, wVector.size() * 2 * sizeof(float));
|
|
|
|
return wVector.size();
|
2011-10-12 07:44:40 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
for (i = 0; i < wVector.size(); i++) {
|
|
|
|
retVector[2 * i + 0] = itr->real() * scale;
|
|
|
|
retVector[2 * i + 1] = itr->imag() * scale;
|
|
|
|
itr++;
|
|
|
|
}
|
2011-10-12 07:44:40 +00:00
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
return wVector.size();
|
2011-10-12 07:44:40 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
int RadioInterface::unRadioifyVector(float *floatVector,
|
|
|
|
signalVector& newVector)
|
2011-10-12 07:44:40 +00:00
|
|
|
{
|
2011-11-26 03:18:34 +00:00
|
|
|
int i;
|
|
|
|
signalVector::iterator itr = newVector.begin();
|
2011-10-12 07:44:40 +00:00
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
for (i = 0; i < newVector.size(); i++) {
|
|
|
|
*itr++ = Complex<float>(floatVector[2 * i + 0],
|
|
|
|
floatVector[2 * i + 1]);
|
|
|
|
}
|
2011-10-12 07:44:40 +00:00
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
return newVector.size();
|
2011-10-12 07:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RadioInterface::tuneTx(double freq)
|
|
|
|
{
|
|
|
|
return mRadio->setTxFreq(freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RadioInterface::tuneRx(double freq)
|
|
|
|
{
|
|
|
|
return mRadio->setRxFreq(freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RadioInterface::start()
|
|
|
|
{
|
|
|
|
LOG(INFO) << "starting radio interface...";
|
|
|
|
mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter,
|
|
|
|
(void*)this);
|
|
|
|
writeTimestamp = mRadio->initialWriteTimestamp();
|
|
|
|
readTimestamp = mRadio->initialReadTimestamp();
|
|
|
|
mRadio->start();
|
|
|
|
LOG(DEBUG) << "Radio started";
|
|
|
|
mRadio->updateAlignment(writeTimestamp-10000);
|
|
|
|
mRadio->updateAlignment(writeTimestamp-10000);
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
sendBuffer = new float[2*2*INCHUNK*samplesPerSymbol];
|
|
|
|
rcvBuffer = new float[2*2*OUTCHUNK*samplesPerSymbol];
|
2011-10-12 07:44:40 +00:00
|
|
|
|
|
|
|
mOn = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
radioInterface->alignRadio();
|
|
|
|
pthread_testcancel();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioInterface::alignRadio() {
|
|
|
|
sleep(60);
|
|
|
|
mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioInterface::driveTransmitRadio(signalVector &radioBurst, bool zeroBurst) {
|
|
|
|
|
|
|
|
if (!mOn) return;
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
radioifyVector(radioBurst, sendBuffer + 2 * sendCursor, powerScaling, zeroBurst);
|
2011-10-12 07:44:40 +00:00
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
sendCursor += radioBurst.size();
|
2011-10-12 07:44:40 +00:00
|
|
|
|
|
|
|
pushBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioInterface::driveReceiveRadio() {
|
|
|
|
|
|
|
|
if (!mOn) return;
|
|
|
|
|
|
|
|
if (mReceiveFIFO.size() > 8) return;
|
|
|
|
|
|
|
|
pullBuffer();
|
|
|
|
|
|
|
|
GSM::Time rcvClock = mClock.get();
|
|
|
|
rcvClock.decTN(receiveOffset);
|
|
|
|
unsigned tN = rcvClock.TN();
|
2011-11-26 03:18:34 +00:00
|
|
|
int rcvSz = rcvCursor;
|
2011-10-12 07:44:40 +00:00
|
|
|
int readSz = 0;
|
|
|
|
const int symbolsPerSlot = gSlotLen + 8;
|
|
|
|
|
|
|
|
// while there's enough data in receive buffer, form received
|
|
|
|
// GSM bursts and pass up to Transceiver
|
|
|
|
// Using the 157-156-156-156 symbols per timeslot format.
|
|
|
|
while (rcvSz > (symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol) {
|
|
|
|
signalVector rxVector((symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol);
|
|
|
|
unRadioifyVector(rcvBuffer+readSz*2,rxVector);
|
|
|
|
GSM::Time tmpTime = rcvClock;
|
|
|
|
if (rcvClock.FN() >= 0) {
|
|
|
|
//LOG(DEBUG) << "FN: " << rcvClock.FN();
|
|
|
|
radioVector *rxBurst = NULL;
|
|
|
|
if (!loadTest)
|
|
|
|
rxBurst = new radioVector(rxVector,tmpTime);
|
|
|
|
else {
|
|
|
|
if (tN % 4 == 0)
|
|
|
|
rxBurst = new radioVector(*finalVec9,tmpTime);
|
|
|
|
else
|
|
|
|
rxBurst = new radioVector(*finalVec,tmpTime);
|
|
|
|
}
|
|
|
|
mReceiveFIFO.put(rxBurst);
|
|
|
|
}
|
|
|
|
mClock.incTN();
|
|
|
|
rcvClock.incTN();
|
|
|
|
//if (mReceiveFIFO.size() >= 16) mReceiveFIFO.wait(8);
|
|
|
|
//LOG(DEBUG) << "receiveFIFO: wrote radio vector at time: " << mClock.get() << ", new size: " << mReceiveFIFO.size() ;
|
|
|
|
readSz += (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol;
|
|
|
|
rcvSz -= (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol;
|
|
|
|
|
|
|
|
tN = rcvClock.TN();
|
|
|
|
}
|
|
|
|
|
2011-11-26 03:18:34 +00:00
|
|
|
if (readSz > 0) {
|
|
|
|
rcvCursor -= readSz;
|
|
|
|
memmove(rcvBuffer,rcvBuffer+2*readSz,sizeof(float) * 2 * rcvCursor);
|
2011-10-12 07:44:40 +00:00
|
|
|
}
|
2011-11-26 03:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RadioInterface::isUnderrun()
|
|
|
|
{
|
|
|
|
bool retVal = underrun;
|
|
|
|
underrun = false;
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioInterface::attach(RadioDevice *wRadio, int wRadioOversampling)
|
|
|
|
{
|
|
|
|
if (!mOn) {
|
|
|
|
mRadio = wRadio;
|
|
|
|
mRadioOversampling = SAMPSPERSYM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double RadioInterface::setRxGain(double dB)
|
|
|
|
{
|
|
|
|
if (mRadio)
|
|
|
|
return mRadio->setRxGain(dB);
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
double RadioInterface::getRxGain()
|
|
|
|
{
|
|
|
|
if (mRadio)
|
|
|
|
return mRadio->getRxGain();
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|