blob: 235d888d64e56f564faf10c32dcd15f4da2d0bd1 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4* This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribuion.
5*
6* This use of this software may be subject to additional restrictions.
7* See the LEGAL file in the main directory for details.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13*/
14
15
16
Pau Espin Pedrol46444632018-09-03 16:42:04 +020017#include "sigProcLib.h"
dburgessb3a0ca42011-10-12 07:44:40 +000018#include "GSMCommon.h"
19#include "LinkedLists.h"
20#include "radioDevice.h"
kurtis.heimerl8aea56e2011-11-26 03:18:30 +000021#include "radioVector.h"
22#include "radioClock.h"
Tom Tsou28670fb2015-08-21 19:32:58 -070023#include "radioBuffer.h"
Thomas Tsoue90a42b2013-11-13 23:38:09 -050024#include "Resampler.h"
Tom Tsou76764272016-06-24 14:25:39 -070025#include "Channelizer.h"
26#include "Synthesis.h"
dburgessb3a0ca42011-10-12 07:44:40 +000027
Alexander Chemerisd734e2d2013-06-16 14:30:58 +040028static const unsigned gSlotLen = 148; ///< number of symbols per slot, not counting guard periods
29
dburgessb3a0ca42011-10-12 07:44:40 +000030/** class to interface the transceiver with the USRP */
31class RadioInterface {
32
Thomas Tsoucb69f082013-04-08 14:18:26 -040033protected:
dburgessb3a0ca42011-10-12 07:44:40 +000034
35 Thread mAlignRadioServiceLoopThread; ///< thread that synchronizes transmit and receive sections
36
Thomas Tsou204a9f12013-10-29 18:34:16 -040037 std::vector<VectorFIFO> mReceiveFIFO; ///< FIFO that holds receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +000038
Pau Espin Pedrola801ae52019-09-13 15:59:29 +020039 RadioDevice *mDevice; ///< the USRP object
Thomas Tsouc1f7c422013-10-11 13:49:55 -040040
Thomas Tsou204a9f12013-10-29 18:34:16 -040041 size_t mSPSTx;
42 size_t mSPSRx;
43 size_t mChans;
Thomas Tsoue90a42b2013-11-13 23:38:09 -050044
Tom Tsou28670fb2015-08-21 19:32:58 -070045 std::vector<RadioBuffer *> sendBuffer;
46 std::vector<RadioBuffer *> recvBuffer;
dburgessb3a0ca42011-10-12 07:44:40 +000047
Thomas Tsou204a9f12013-10-29 18:34:16 -040048 std::vector<short *> convertRecvBuffer;
49 std::vector<short *> convertSendBuffer;
Thomas Tsoucb269a32013-11-15 14:15:47 -050050 std::vector<float> powerScaling;
dburgessb3a0ca42011-10-12 07:44:40 +000051 bool underrun; ///< indicates writes to USRP are too slow
52 bool overrun; ///< indicates reads from USRP are too slow
53 TIMESTAMP writeTimestamp; ///< sample timestamp of next packet written to USRP
54 TIMESTAMP readTimestamp; ///< sample timestamp of next packet read from USRP
55
56 RadioClock mClock; ///< the basestation clock!
57
dburgessb3a0ca42011-10-12 07:44:40 +000058 int receiveOffset; ///< offset b/w transmit and receive GSM timestamps, in timeslots
dburgessb3a0ca42011-10-12 07:44:40 +000059
60 bool mOn; ///< indicates radio is on
61
Thomas Tsoucb69f082013-04-08 14:18:26 -040062private:
63
Tom Tsou28670fb2015-08-21 19:32:58 -070064 /** format samples to USRP */
65 int radioifyVector(signalVector &wVector, size_t chan, bool zero);
dburgessb3a0ca42011-10-12 07:44:40 +000066
67 /** format samples from USRP */
Tom Tsou28670fb2015-08-21 19:32:58 -070068 int unRadioifyVector(signalVector *wVector, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +000069
70 /** push GSM bursts into the transmit buffer */
Tom Tsou28670fb2015-08-21 19:32:58 -070071 virtual bool pushBuffer(void);
dburgessb3a0ca42011-10-12 07:44:40 +000072
73 /** pull GSM bursts from the receive buffer */
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +020074 virtual int pullBuffer(void);
dburgessb3a0ca42011-10-12 07:44:40 +000075
76public:
77
78 /** start the interface */
Tom Tsoueb54bdd2014-11-25 16:06:32 -080079 bool start();
80 bool stop();
dburgessb3a0ca42011-10-12 07:44:40 +000081
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040082 /** intialization */
Thomas Tsoufe269fe2013-10-14 23:56:51 -040083 virtual bool init(int type);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040084 virtual void close();
85
dburgessb3a0ca42011-10-12 07:44:40 +000086 /** constructor */
Pau Espin Pedrola801ae52019-09-13 15:59:29 +020087 RadioInterface(RadioDevice* wDevice, size_t tx_sps, size_t rx_sps,
Tom Tsoud6ae8642017-03-30 17:22:58 -070088 size_t chans = 1, int receiveOffset = 3,
89 GSM::Time wStartTime = GSM::Time(0));
Thomas Tsou204a9f12013-10-29 18:34:16 -040090
dburgessb3a0ca42011-10-12 07:44:40 +000091 /** destructor */
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040092 virtual ~RadioInterface();
dburgessb3a0ca42011-10-12 07:44:40 +000093
dburgessb3a0ca42011-10-12 07:44:40 +000094 /** check for underrun, resets underrun value */
kurtis.heimerle724d6d2011-11-26 03:18:46 +000095 bool isUnderrun();
dburgessb3a0ca42011-10-12 07:44:40 +000096
97 /** return the receive FIFO */
Thomas Tsou204a9f12013-10-29 18:34:16 -040098 VectorFIFO* receiveFIFO(size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +000099
100 /** return the basestation clock */
101 RadioClock* getClock(void) { return &mClock;};
102
dburgessb3a0ca42011-10-12 07:44:40 +0000103 /** set transmit frequency */
Tom Tsou76764272016-06-24 14:25:39 -0700104 virtual bool tuneTx(double freq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000105
106 /** set receive frequency */
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500107 virtual bool tuneRx(double freq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000108
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000109 /** set receive gain */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400110 double setRxGain(double dB, size_t chan = 0);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000111
112 /** get receive gain */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400113 double getRxGain(size_t chan = 0);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000114
dburgessb3a0ca42011-10-12 07:44:40 +0000115 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400116 void driveTransmitRadio(std::vector<signalVector *> &bursts,
117 std::vector<bool> &zeros);
dburgessb3a0ca42011-10-12 07:44:40 +0000118
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200119 /** drive reception of GSM bursts. -1: Error. 0: Radio off. 1: Received something. */
120 int driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000121
Tom Tsoua4d1a412014-11-25 15:46:56 -0800122 int setPowerAttenuation(int atten, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000123
124 /** returns the full-scale transmit amplitude **/
125 double fullScaleInputValue();
126
127 /** returns the full-scale receive amplitude **/
128 double fullScaleOutputValue();
129
Pau Espin Pedrol46444632018-09-03 16:42:04 +0200130 /** get transport window type of attached device */
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200131 enum RadioDevice::TxWindowType getWindowType() { return mDevice->getWindowType(); }
kurtis.heimerle380af32011-11-26 03:18:55 +0000132
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200133 /** Minimum latency that the device can achieve */
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200134 GSM::Time minLatency() { return mDevice->minLatency(); }
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200135
dburgessb3a0ca42011-10-12 07:44:40 +0000136protected:
dburgessb3a0ca42011-10-12 07:44:40 +0000137 /** drive synchronization of Tx/Rx of USRP */
138 void alignRadio();
139
dburgessb3a0ca42011-10-12 07:44:40 +0000140 friend void *AlignRadioServiceLoopAdapter(RadioInterface*);
dburgessb3a0ca42011-10-12 07:44:40 +0000141};
142
Thomas Tsoucb69f082013-04-08 14:18:26 -0400143class RadioInterfaceResamp : public RadioInterface {
Thomas Tsoucb69f082013-04-08 14:18:26 -0400144private:
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400145 signalVector *outerSendBuffer;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400146 signalVector *outerRecvBuffer;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400147
Tom Tsou28670fb2015-08-21 19:32:58 -0700148 bool pushBuffer();
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200149 int pullBuffer();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400150
151public:
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200152 RadioInterfaceResamp(RadioDevice* wDevice, size_t tx_sps, size_t rx_sps);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400153 ~RadioInterfaceResamp();
154
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400155 bool init(int type);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400156 void close();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400157};
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500158
Tom Tsou76764272016-06-24 14:25:39 -0700159class RadioInterfaceMulti : public RadioInterface {
160private:
161 bool pushBuffer();
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200162 int pullBuffer();
Tom Tsou76764272016-06-24 14:25:39 -0700163
164 signalVector *outerSendBuffer;
165 signalVector *outerRecvBuffer;
166 std::vector<signalVector *> history;
167 std::vector<bool> active;
168
169 Resampler *dnsampler;
170 Resampler *upsampler;
171 Channelizer *channelizer;
172 Synthesis *synthesis;
173
174public:
175 RadioInterfaceMulti(RadioDevice* radio, size_t tx_sps,
176 size_t rx_sps, size_t chans = 1);
177 ~RadioInterfaceMulti();
178
179 bool init(int type);
180 void close();
181
182 bool tuneTx(double freq, size_t chan);
183 bool tuneRx(double freq, size_t chan);
184 double setRxGain(double dB, size_t chan);
185};