blob: ffcacddeea3618935616be119f42009e6914d5aa [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
17#include "sigProcLib.h"
18#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
39 RadioDevice *mRadio; ///< 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 */
Thomas Tsoucb69f082013-04-08 14:18:26 -040074 virtual void 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 */
Tom Tsou8f0ccf62016-07-20 16:35:03 -070087 RadioInterface(RadioDevice* wRadio, 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
119 /** drive reception of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400120 bool 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
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000130 /** set thread priority on current thread */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500131 void setPriority(float prio = 0.5) { mRadio->setPriority(prio); }
dburgessb3a0ca42011-10-12 07:44:40 +0000132
Thomas Tsou02d88d12013-04-05 15:36:30 -0400133 /** get transport window type of attached device */
134 enum RadioDevice::TxWindowType getWindowType() { return mRadio->getWindowType(); }
kurtis.heimerle380af32011-11-26 03:18:55 +0000135
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200136 /** Minimum latency that the device can achieve */
137 GSM::Time minLatency() { return mRadio->minLatency(); }
138
dburgessb3a0ca42011-10-12 07:44:40 +0000139protected:
dburgessb3a0ca42011-10-12 07:44:40 +0000140 /** drive synchronization of Tx/Rx of USRP */
141 void alignRadio();
142
dburgessb3a0ca42011-10-12 07:44:40 +0000143 friend void *AlignRadioServiceLoopAdapter(RadioInterface*);
dburgessb3a0ca42011-10-12 07:44:40 +0000144};
145
Thomas Tsoucb69f082013-04-08 14:18:26 -0400146class RadioInterfaceResamp : public RadioInterface {
Thomas Tsoucb69f082013-04-08 14:18:26 -0400147private:
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400148 signalVector *outerSendBuffer;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400149 signalVector *outerRecvBuffer;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400150
Tom Tsou28670fb2015-08-21 19:32:58 -0700151 bool pushBuffer();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400152 void pullBuffer();
153
154public:
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700155 RadioInterfaceResamp(RadioDevice* wRadio, size_t tx_sps, size_t rx_sps);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400156 ~RadioInterfaceResamp();
157
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400158 bool init(int type);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400159 void close();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400160};
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500161
Tom Tsou76764272016-06-24 14:25:39 -0700162class RadioInterfaceMulti : public RadioInterface {
163private:
164 bool pushBuffer();
165 void pullBuffer();
166
167 signalVector *outerSendBuffer;
168 signalVector *outerRecvBuffer;
169 std::vector<signalVector *> history;
170 std::vector<bool> active;
171
172 Resampler *dnsampler;
173 Resampler *upsampler;
174 Channelizer *channelizer;
175 Synthesis *synthesis;
176
177public:
178 RadioInterfaceMulti(RadioDevice* radio, size_t tx_sps,
179 size_t rx_sps, size_t chans = 1);
180 ~RadioInterfaceMulti();
181
182 bool init(int type);
183 void close();
184
185 bool tuneTx(double freq, size_t chan);
186 bool tuneRx(double freq, size_t chan);
187 double setRxGain(double dB, size_t chan);
188};