blob: 8fd2ec32498707675a7e1d5daef6d5f228f9d958 [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
25//#define NDEBUG
26#include "radioInterface.h"
27#include <Logger.h>
28
29
30GSM::Time VectorQueue::nextTime() const
31{
32 GSM::Time retVal;
33 ScopedLock lock(mLock);
34 while (mQ.size()==0) mWriteSignal.wait(mLock);
35 return mQ.top()->time();
36}
37
38radioVector* VectorQueue::getStaleBurst(const GSM::Time& targTime)
39{
40 ScopedLock lock(mLock);
41 if ((mQ.size()==0)) {
42 return NULL;
43 }
44 if (mQ.top()->time() < targTime) {
45 radioVector* retVal = mQ.top();
46 mQ.pop();
47 return retVal;
48 }
49 return NULL;
50}
51
52
53radioVector* VectorQueue::getCurrentBurst(const GSM::Time& targTime)
54{
55 ScopedLock lock(mLock);
56 if ((mQ.size()==0)) {
57 return NULL;
58 }
59 if (mQ.top()->time() == targTime) {
60 radioVector* retVal = mQ.top();
61 mQ.pop();
62 return retVal;
63 }
64 return NULL;
65}
66
67
68
69RadioInterface::RadioInterface(RadioDevice *wRadio,
70 int wReceiveOffset,
71 int wRadioOversampling,
72 int wTransceiverOversampling,
73 GSM::Time wStartTime)
74
75{
76 underrun = false;
77
78 sendCursor = 0;
79 rcvCursor = 0;
80 mOn = false;
81
82 mRadio = wRadio;
83 receiveOffset = wReceiveOffset;
84 samplesPerSymbol = wRadioOversampling;
85 mClock.set(wStartTime);
86 powerScaling = 1.0;
kurtis.heimerlf5eef202011-11-26 03:16:45 +000087 loadTest = false;
dburgessb3a0ca42011-10-12 07:44:40 +000088}
89
90RadioInterface::~RadioInterface(void) {
91 if (rcvBuffer!=NULL) delete rcvBuffer;
92 //mReceiveFIFO.clear();
93}
94
95double RadioInterface::fullScaleInputValue(void) {
96 return mRadio->fullScaleInputValue();
97}
98
99double RadioInterface::fullScaleOutputValue(void) {
100 return mRadio->fullScaleOutputValue();
101}
102
103
104void RadioInterface::setPowerAttenuation(double dBAtten)
105{
106 float HWdBAtten = mRadio->setTxGain(-dBAtten);
107 dBAtten -= (-HWdBAtten);
108 float linearAtten = powf(10.0F,0.1F*dBAtten);
109 if (linearAtten < 1.0)
110 powerScaling = 1.0;
111 else
112 powerScaling = 1.0/sqrt(linearAtten);
113 LOG(INFO) << "setting HW gain to " << HWdBAtten << " and power scaling to " << powerScaling;
114}
115
116
117short *RadioInterface::radioifyVector(signalVector &wVector, short *retVector, double scale, bool zeroOut)
118{
119
120
121 signalVector::iterator itr = wVector.begin();
122 short *shortItr = retVector;
123 if (zeroOut) {
124 while (itr < wVector.end()) {
125 *shortItr++ = 0;
126 *shortItr++ = 0;
127 itr++;
128 }
129 }
130 else if (scale != 1.0) {
131 while (itr < wVector.end()) {
132 *shortItr++ = (short) (itr->real()*scale);
133 *shortItr++ = (short) (itr->imag()*scale);
134 itr++;
135 }
136 }
137 else {
138 while (itr < wVector.end()) {
139 *shortItr++ = (short) (itr->real());
140 *shortItr++ = (short) (itr->imag());
141 itr++;
142 }
143 }
144
145 return retVector;
146
147}
148
149void RadioInterface::unRadioifyVector(short *shortVector, signalVector& newVector)
150{
151
152 signalVector::iterator itr = newVector.begin();
153 short *shortItr = shortVector;
154 while (itr < newVector.end()) {
155 *itr++ = Complex<float>(*shortItr,*(shortItr+1));
156 //LOG(DEBUG) << (*(itr-1));
157 shortItr += 2;
158 }
159
160}
161
162
163bool started = false;
164
165void RadioInterface::pushBuffer(void) {
166
167 if (sendCursor < 2*INCHUNK*samplesPerSymbol) return;
168
169 // send resampleVector
170 int samplesWritten = mRadio->writeSamples(sendBuffer,
171 INCHUNK*samplesPerSymbol,
172 &underrun,
173 writeTimestamp);
174 //LOG(DEBUG) << "writeTimestamp: " << writeTimestamp << ", samplesWritten: " << samplesWritten;
175
176 writeTimestamp += (TIMESTAMP) samplesWritten;
177
178 if (sendCursor > 2*samplesWritten)
179 memcpy(sendBuffer,sendBuffer+samplesWritten*2,sizeof(short)*2*(sendCursor-2*samplesWritten));
180 sendCursor = sendCursor - 2*samplesWritten;
181}
182
183
184void RadioInterface::pullBuffer(void)
185{
186
187 bool localUnderrun;
188
189 // receive receiveVector
190 short* shortVector = rcvBuffer+rcvCursor;
191 //LOG(DEBUG) << "Reading USRP samples at timestamp " << readTimestamp;
192 int samplesRead = mRadio->readSamples(shortVector,OUTCHUNK*samplesPerSymbol,&overrun,readTimestamp,&localUnderrun);
193 underrun |= localUnderrun;
194 readTimestamp += (TIMESTAMP) samplesRead;
195 while (samplesRead < OUTCHUNK*samplesPerSymbol) {
196 int oldSamplesRead = samplesRead;
197 samplesRead += mRadio->readSamples(shortVector+2*samplesRead,
198 OUTCHUNK*samplesPerSymbol-samplesRead,
199 &overrun,
200 readTimestamp,
201 &localUnderrun);
202 underrun |= localUnderrun;
203 readTimestamp += (TIMESTAMP) (samplesRead - oldSamplesRead);
204 }
205 //LOG(DEBUG) << "samplesRead " << samplesRead;
206
207 rcvCursor += samplesRead*2;
208
209}
210
211bool RadioInterface::tuneTx(double freq)
212{
213 return mRadio->setTxFreq(freq);
214}
215
216bool RadioInterface::tuneRx(double freq)
217{
218 return mRadio->setRxFreq(freq);
219}
220
221
222void RadioInterface::start()
223{
224 LOG(INFO) << "starting radio interface...";
225 mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter,
226 (void*)this);
227 writeTimestamp = mRadio->initialWriteTimestamp();
228 readTimestamp = mRadio->initialReadTimestamp();
229 mRadio->start();
230 LOG(DEBUG) << "Radio started";
231 mRadio->updateAlignment(writeTimestamp-10000);
232 mRadio->updateAlignment(writeTimestamp-10000);
233
234 sendBuffer = new short[2*2*INCHUNK*samplesPerSymbol];
235 rcvBuffer = new short[2*2*OUTCHUNK*samplesPerSymbol];
236
237 mOn = true;
238
239}
240
241void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface)
242{
243 while (1) {
244 radioInterface->alignRadio();
245 pthread_testcancel();
246 }
247 return NULL;
248}
249
250void RadioInterface::alignRadio() {
251 sleep(60);
252 mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000);
253}
254
255void RadioInterface::driveTransmitRadio(signalVector &radioBurst, bool zeroBurst) {
256
257 if (!mOn) return;
258
259 radioifyVector(radioBurst, sendBuffer+sendCursor, powerScaling, zeroBurst);
260
261 sendCursor += (radioBurst.size()*2);
262
263 pushBuffer();
264}
265
266void RadioInterface::driveReceiveRadio() {
267
268 if (!mOn) return;
269
270 if (mReceiveFIFO.size() > 8) return;
271
272 pullBuffer();
273
274 GSM::Time rcvClock = mClock.get();
275 rcvClock.decTN(receiveOffset);
276 unsigned tN = rcvClock.TN();
277 int rcvSz = rcvCursor/2;
278 int readSz = 0;
279 const int symbolsPerSlot = gSlotLen + 8;
280
281 // while there's enough data in receive buffer, form received
282 // GSM bursts and pass up to Transceiver
283 // Using the 157-156-156-156 symbols per timeslot format.
284 while (rcvSz > (symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol) {
285 signalVector rxVector((symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol);
286 unRadioifyVector(rcvBuffer+readSz*2,rxVector);
287 GSM::Time tmpTime = rcvClock;
288 if (rcvClock.FN() >= 0) {
289 //LOG(DEBUG) << "FN: " << rcvClock.FN();
290 radioVector *rxBurst = NULL;
291 if (!loadTest)
292 rxBurst = new radioVector(rxVector,tmpTime);
293 else {
294 if (tN % 4 == 0)
295 rxBurst = new radioVector(*finalVec9,tmpTime);
296 else
297 rxBurst = new radioVector(*finalVec,tmpTime);
298 }
299 mReceiveFIFO.put(rxBurst);
300 }
301 mClock.incTN();
302 rcvClock.incTN();
303 //if (mReceiveFIFO.size() >= 16) mReceiveFIFO.wait(8);
304 //LOG(DEBUG) << "receiveFIFO: wrote radio vector at time: " << mClock.get() << ", new size: " << mReceiveFIFO.size() ;
305 readSz += (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol;
306 rcvSz -= (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol;
307
308 tN = rcvClock.TN();
309 }
310
311 if (readSz > 0) {
312 memcpy(rcvBuffer,rcvBuffer+2*readSz,sizeof(short)*2*(rcvCursor-readSz));
313 rcvCursor = rcvCursor-2*readSz;
314 }
315}
316