aboutsummaryrefslogtreecommitdiffstats
path: root/Transceiver52M/DriveLoop.cpp
blob: cd84d035bfbf026d947ac38e0f786f284fafc4fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright 2008, 2009, 2010, 2012 Free Software Foundation, Inc.
*
* This software is distributed under the terms of the GNU 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 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include "DriveLoop.h"
#include <Logger.h>

DriveLoop::DriveLoop(int wBasePort, const char *TRXAddress,
                     RadioInterface *wRadioInterface,
		     int wChanM, int wC0, int wSamplesPerSymbol,
                     GSM::Time wTransmitLatency)
	:mClockSocket(wBasePort, TRXAddress, wBasePort + 100), mC0(wC0)
{
  mChanM = wChanM;
  mRadioDriveLoopThread = NULL;
  mSamplesPerSymbol = wSamplesPerSymbol;
  mRadioInterface = wRadioInterface;

  mStartTime = (random() % gHyperframe, 0);

  mTransmitDeadlineClock = mStartTime;
  mLatencyUpdateTime = mStartTime;
  mTransmitLatency = wTransmitLatency;
  mLastClockUpdateTime = mStartTime;

  mRadioInterface->getClock()->set(mStartTime);

  // generate pulse and setup up signal processing library
  gsmPulse = generateGSMPulse(2, mSamplesPerSymbol);
  LOG(DEBUG) << "gsmPulse: " << *gsmPulse;
  sigProcLibSetup(mSamplesPerSymbol);

  txFullScale = mRadioInterface->fullScaleInputValue();

  // initialize filler tables with dummy bursts on C0, empty bursts otherwise
  for (int i = 0; i < 8; i++) {
    signalVector* modBurst = modulateBurst(gDummyBurst, *gsmPulse,
                                           8 + (i % 4 == 0), mSamplesPerSymbol);
    scaleVector(*modBurst, txFullScale);
    for (int j = 0; j < 102; j++) {
      for (int n = 0; n < mChanM; n++) {
        if (n == mC0)
          fillerTable[n][j][i] = new signalVector(*modBurst);
        else
          fillerTable[n][j][i] = new signalVector(modBurst->size());
      }
    }
    delete modBurst;

    for (int n = 0; n < mChanM; n++) {
      fillerModulus[n][i] = 26;
      mChanType[n][i] = NONE;
    }
  }

  mOn = false;
}

DriveLoop::~DriveLoop()
{
  if (mOn) {
    mOn = false;

    if (mRadioDriveLoopThread)
      delete mRadioDriveLoopThread;
  }

  delete gsmPulse;
  sigProcLibDestroy();
}

void DriveLoop::start()
{
  if (mOn)
    return;

  mOn = true;
  mRadioDriveLoopThread = new Thread(32768);
  mRadioDriveLoopThread->start((void * (*)(void*))RadioDriveLoopAdapter, (void*) this);
}

void DriveLoop::pushRadioVector(GSM::Time &nowTime)
{
  int i;
  radioVector *staleBurst;
  radioVector *next;

  for (i = 0; i < mChanM; i++) {
    // dump stale bursts, if any
    while (staleBurst = mTransmitPriorityQueue[i].getStaleBurst(nowTime)) {
      // Even if the burst is stale, put it in the fillter table.
      // (It might be an idle pattern.)
      LOG(NOTICE) << "dumping STALE burst in TRX->USRP interface";
      if (i == mC0) {
        const GSM::Time& nextTime = staleBurst->getTime();
        int TN = nextTime.TN();
        int modFN = nextTime.FN() % fillerModulus[i][TN];
        delete fillerTable[i][modFN][TN];
        fillerTable[i][modFN][TN] = staleBurst;
      }
    }

    int TN = nowTime.TN();
    int modFN = nowTime.FN() % fillerModulus[i][nowTime.TN()];

    mTxBursts[i] = fillerTable[i][modFN][TN];
    mIsFiller[i] = true;
    mIsZero[i] = (mChanType[i][TN] == NONE);

    // if queue contains data at the desired timestamp, stick it into FIFO
    if (next = (radioVector*) mTransmitPriorityQueue[i].getCurrentBurst(nowTime)) {
      LOG(DEBUG) << "transmitFIFO: wrote burst " << next << " at time: " << nowTime;
      if (i == mC0) {
        delete fillerTable[i][modFN][TN];
        fillerTable[i][modFN][TN] = new signalVector(*(next));
        mIsFiller[i] = false;
      } 
      mTxBursts[i] = next;
    }
  }

  mRadioInterface->driveTransmitRadio(mTxBursts, mIsZero);

  for (i = 0; i < mChanM; i++) {
    if (!mIsFiller[i])
      delete mTxBursts[i];
  }
}

void DriveLoop::setModulus(int channel, int timeslot)
{
  switch (mChanType[channel][timeslot]) {
  case NONE:
  case I:
  case II:
  case III:
  case FILL:
    fillerModulus[channel][timeslot] = 26;
    break;
  case IV:
  case VI:
  case V:
    fillerModulus[channel][timeslot] = 51;
    break;
    //case V: 
  case VII:
    fillerModulus[channel][timeslot] = 102;
    break;
  default:
    break;
  }
}

DriveLoop::CorrType DriveLoop::expectedCorrType(int channel, GSM::Time currTime)
{
  unsigned burstTN = currTime.TN();
  unsigned burstFN = currTime.FN();

  switch (mChanType[channel][burstTN]) {
  case NONE:
    return OFF;
    break;
  case FILL:
    return IDLE;
    break;
  case I:
    return TSC;
    /*if (burstFN % 26 == 25) 
      return IDLE;
    else
      return TSC;*/
    break;
  case II:
    if (burstFN % 2 == 1)
      return IDLE;
    else
      return TSC;
    break;
  case III:
    return TSC;
    break;
  case IV:
  case VI:
    return RACH;
    break;
  case V: {
    int mod51 = burstFN % 51;
    if ((mod51 <= 36) && (mod51 >= 14))
      return RACH;
    else if ((mod51 == 4) || (mod51 == 5))
      return RACH;
    else if ((mod51 == 45) || (mod51 == 46))
      return RACH;
    else
      return TSC;
    break;
  }
  case VII:
    if ((burstFN % 51 <= 14) && (burstFN % 51 >= 12))
      return IDLE;
    else
      return TSC;
    break;
  case LOOPBACK:
    if ((burstFN % 51 <= 50) && (burstFN % 51 >=48))
      return IDLE;
    else
      return TSC;
    break;
  default:
    return OFF;
    break;
  }
}
 
void DriveLoop::driveReceiveFIFO() 
{
  SoftVector *rxBurst = NULL;
  int RSSI;
  int TOA;  // in 1/256 of a symbol
  GSM::Time burstTime;

  mRadioInterface->driveReceiveRadio();
}

/*
 *  Features a carefully controlled latency mechanism, to
 *  assure that transmit packets arrive at the radio/USRP
 *  before they need to be transmitted.
 *  
 *  Deadline clock indicates the burst that needs to be
 *  pushed into the FIFO right NOW.  If transmit queue does
 *  not have a burst, stick in filler data.
 */
void DriveLoop::driveTransmitFIFO() 
{
  int i;

  RadioClock *radioClock = (mRadioInterface->getClock());
  while (radioClock->get() + mTransmitLatency > mTransmitDeadlineClock) {
    pushRadioVector(mTransmitDeadlineClock);
    mTransmitDeadlineClock.incTN();
  }

  // FIXME -- This should not be a hard spin.
  // But any delay here causes us to throw omni_thread_fatal.
  //else radioClock->wait();
}

void DriveLoop::writeClockInterface()
{
  char command[50];
  // FIXME -- This should be adaptive.
  sprintf(command,"IND CLOCK %llu",
          (unsigned long long) (mTransmitDeadlineClock.FN() + 2));

  LOG(INFO) << "ClockInterface: sending " << command;

  mClockSocket.write(command,strlen(command)+1);

  mLastClockUpdateTime = mTransmitDeadlineClock;
}

void *RadioDriveLoopAdapter(DriveLoop *drive)
{
  drive->setPriority();

  while (drive->on()) {
    drive->driveReceiveFIFO();
    drive->driveTransmitFIFO();
    pthread_testcancel();
  }

  return NULL;
}