blob: e8eea6f3fae825b242a5d8ec8792f41042983fe5 [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{
kurtis.heimerld1ae0cd2011-11-26 03:17:35 +0000106 double HWatten = mRadio->setTxGain(mRadio->maxTxGain() - atten);
107 atten -= (-HWatten);
108 if (atten < 1.0)
dburgessb3a0ca42011-10-12 07:44:40 +0000109 powerScaling = 1.0;
110 else
111 powerScaling = 1.0/sqrt(linearAtten);
112 LOG(INFO) << "setting HW gain to " << HWdBAtten << " and power scaling to " << powerScaling;
113}
114
115
116short *RadioInterface::radioifyVector(signalVector &wVector, short *retVector, double scale, bool zeroOut)
117{
118
119
120 signalVector::iterator itr = wVector.begin();
121 short *shortItr = retVector;
122 if (zeroOut) {
123 while (itr < wVector.end()) {
124 *shortItr++ = 0;
125 *shortItr++ = 0;
126 itr++;
127 }
128 }
129 else if (scale != 1.0) {
130 while (itr < wVector.end()) {
131 *shortItr++ = (short) (itr->real()*scale);
132 *shortItr++ = (short) (itr->imag()*scale);
133 itr++;
134 }
135 }
136 else {
137 while (itr < wVector.end()) {
138 *shortItr++ = (short) (itr->real());
139 *shortItr++ = (short) (itr->imag());
140 itr++;
141 }
142 }
143
144 return retVector;
145
146}
147
148void RadioInterface::unRadioifyVector(short *shortVector, signalVector& newVector)
149{
150
151 signalVector::iterator itr = newVector.begin();
152 short *shortItr = shortVector;
153 while (itr < newVector.end()) {
154 *itr++ = Complex<float>(*shortItr,*(shortItr+1));
155 //LOG(DEBUG) << (*(itr-1));
156 shortItr += 2;
157 }
158
159}
160
161
162bool started = false;
163
164void RadioInterface::pushBuffer(void) {
165
166 if (sendCursor < 2*INCHUNK*samplesPerSymbol) return;
167
168 // send resampleVector
169 int samplesWritten = mRadio->writeSamples(sendBuffer,
170 INCHUNK*samplesPerSymbol,
171 &underrun,
172 writeTimestamp);
173 //LOG(DEBUG) << "writeTimestamp: " << writeTimestamp << ", samplesWritten: " << samplesWritten;
174
175 writeTimestamp += (TIMESTAMP) samplesWritten;
176
177 if (sendCursor > 2*samplesWritten)
178 memcpy(sendBuffer,sendBuffer+samplesWritten*2,sizeof(short)*2*(sendCursor-2*samplesWritten));
179 sendCursor = sendCursor - 2*samplesWritten;
180}
181
182
183void RadioInterface::pullBuffer(void)
184{
185
186 bool localUnderrun;
187
188 // receive receiveVector
189 short* shortVector = rcvBuffer+rcvCursor;
190 //LOG(DEBUG) << "Reading USRP samples at timestamp " << readTimestamp;
191 int samplesRead = mRadio->readSamples(shortVector,OUTCHUNK*samplesPerSymbol,&overrun,readTimestamp,&localUnderrun);
192 underrun |= localUnderrun;
193 readTimestamp += (TIMESTAMP) samplesRead;
194 while (samplesRead < OUTCHUNK*samplesPerSymbol) {
195 int oldSamplesRead = samplesRead;
196 samplesRead += mRadio->readSamples(shortVector+2*samplesRead,
197 OUTCHUNK*samplesPerSymbol-samplesRead,
198 &overrun,
199 readTimestamp,
200 &localUnderrun);
201 underrun |= localUnderrun;
202 readTimestamp += (TIMESTAMP) (samplesRead - oldSamplesRead);
203 }
204 //LOG(DEBUG) << "samplesRead " << samplesRead;
205
206 rcvCursor += samplesRead*2;
207
208}
209
210bool RadioInterface::tuneTx(double freq)
211{
212 return mRadio->setTxFreq(freq);
213}
214
215bool RadioInterface::tuneRx(double freq)
216{
217 return mRadio->setRxFreq(freq);
218}
219
220
221void RadioInterface::start()
222{
223 LOG(INFO) << "starting radio interface...";
224 mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter,
225 (void*)this);
226 writeTimestamp = mRadio->initialWriteTimestamp();
227 readTimestamp = mRadio->initialReadTimestamp();
228 mRadio->start();
229 LOG(DEBUG) << "Radio started";
230 mRadio->updateAlignment(writeTimestamp-10000);
231 mRadio->updateAlignment(writeTimestamp-10000);
232
233 sendBuffer = new short[2*2*INCHUNK*samplesPerSymbol];
234 rcvBuffer = new short[2*2*OUTCHUNK*samplesPerSymbol];
235
236 mOn = true;
237
238}
239
240void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface)
241{
242 while (1) {
243 radioInterface->alignRadio();
244 pthread_testcancel();
245 }
246 return NULL;
247}
248
249void RadioInterface::alignRadio() {
250 sleep(60);
251 mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000);
252}
253
254void RadioInterface::driveTransmitRadio(signalVector &radioBurst, bool zeroBurst) {
255
256 if (!mOn) return;
257
258 radioifyVector(radioBurst, sendBuffer+sendCursor, powerScaling, zeroBurst);
259
260 sendCursor += (radioBurst.size()*2);
261
262 pushBuffer();
263}
264
265void RadioInterface::driveReceiveRadio() {
266
267 if (!mOn) return;
268
269 if (mReceiveFIFO.size() > 8) return;
270
271 pullBuffer();
272
273 GSM::Time rcvClock = mClock.get();
274 rcvClock.decTN(receiveOffset);
275 unsigned tN = rcvClock.TN();
276 int rcvSz = rcvCursor/2;
277 int readSz = 0;
278 const int symbolsPerSlot = gSlotLen + 8;
279
280 // while there's enough data in receive buffer, form received
281 // GSM bursts and pass up to Transceiver
282 // Using the 157-156-156-156 symbols per timeslot format.
283 while (rcvSz > (symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol) {
284 signalVector rxVector((symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol);
285 unRadioifyVector(rcvBuffer+readSz*2,rxVector);
286 GSM::Time tmpTime = rcvClock;
287 if (rcvClock.FN() >= 0) {
288 //LOG(DEBUG) << "FN: " << rcvClock.FN();
289 radioVector *rxBurst = NULL;
290 if (!loadTest)
291 rxBurst = new radioVector(rxVector,tmpTime);
292 else {
293 if (tN % 4 == 0)
294 rxBurst = new radioVector(*finalVec9,tmpTime);
295 else
296 rxBurst = new radioVector(*finalVec,tmpTime);
297 }
298 mReceiveFIFO.put(rxBurst);
299 }
300 mClock.incTN();
301 rcvClock.incTN();
302 //if (mReceiveFIFO.size() >= 16) mReceiveFIFO.wait(8);
303 //LOG(DEBUG) << "receiveFIFO: wrote radio vector at time: " << mClock.get() << ", new size: " << mReceiveFIFO.size() ;
304 readSz += (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol;
305 rcvSz -= (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol;
306
307 tN = rcvClock.TN();
308 }
309
310 if (readSz > 0) {
311 memcpy(rcvBuffer,rcvBuffer+2*readSz,sizeof(short)*2*(rcvCursor-readSz));
312 rcvCursor = rcvCursor-2*readSz;
313 }
314}
315