blob: efe560600f8496bf51cb85bc4bdc3d9af28cde37 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
Martin Hauke066fd042019-10-13 19:08:00 +02004* This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribution.
dburgessb3a0ca42011-10-12 07:44:40 +00005*
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;
Pau Espin Pedrole503c982019-09-13 18:56:08 +020051 int underrun; ///< indicates writes to USRP are too slow
dburgessb3a0ca42011-10-12 07:44:40 +000052 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
Martin Hauke066fd042019-10-13 19:08:00 +020082 /** initialization */
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 */
Pau Espin Pedrol80d053a2019-09-16 17:20:46 +0200110 virtual double setRxGain(double dB, size_t chan = 0);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000111
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200112 /** return base RSSI offset to apply for received samples **/
113 virtual double rssiOffset(size_t chan = 0);
114
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
Pau Espin Pedrol992c9bd2020-06-08 13:44:24 +0200122 /** set transmit power attenuation */
123 virtual int setPowerAttenuation(int atten, size_t chan = 0);
Pau Espin Pedrol0e09e7c2020-05-29 16:39:07 +0200124 int getNominalTxPower(size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000125
126 /** returns the full-scale transmit amplitude **/
127 double fullScaleInputValue();
128
129 /** returns the full-scale receive amplitude **/
130 double fullScaleOutputValue();
131
Pau Espin Pedrol46444632018-09-03 16:42:04 +0200132 /** get transport window type of attached device */
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200133 enum RadioDevice::TxWindowType getWindowType() { return mDevice->getWindowType(); }
kurtis.heimerle380af32011-11-26 03:18:55 +0000134
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200135 /** Minimum latency that the device can achieve */
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200136 GSM::Time minLatency() { return mDevice->minLatency(); }
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200137
dburgessb3a0ca42011-10-12 07:44:40 +0000138protected:
dburgessb3a0ca42011-10-12 07:44:40 +0000139 /** drive synchronization of Tx/Rx of USRP */
140 void alignRadio();
141
dburgessb3a0ca42011-10-12 07:44:40 +0000142 friend void *AlignRadioServiceLoopAdapter(RadioInterface*);
dburgessb3a0ca42011-10-12 07:44:40 +0000143};
144
Thomas Tsoucb69f082013-04-08 14:18:26 -0400145class RadioInterfaceResamp : public RadioInterface {
Thomas Tsoucb69f082013-04-08 14:18:26 -0400146private:
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400147 signalVector *outerSendBuffer;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400148 signalVector *outerRecvBuffer;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400149
Tom Tsou28670fb2015-08-21 19:32:58 -0700150 bool pushBuffer();
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200151 int pullBuffer();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400152
153public:
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200154 RadioInterfaceResamp(RadioDevice* wDevice, size_t tx_sps, size_t rx_sps);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400155 ~RadioInterfaceResamp();
156
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400157 bool init(int type);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400158 void close();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400159};
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500160
Pau Espin Pedrolfd99c6c2019-12-21 00:40:09 +0100161struct freq_cfg_state {
162 bool set;
163 double freq_hz;
164};
165
Tom Tsou76764272016-06-24 14:25:39 -0700166class RadioInterfaceMulti : public RadioInterface {
167private:
168 bool pushBuffer();
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200169 int pullBuffer();
Pau Espin Pedrolfd99c6c2019-12-21 00:40:09 +0100170 bool verify_arfcn_consistency(double freq, size_t chan, bool tx);
Pau Espin Pedrol992c9bd2020-06-08 13:44:24 +0200171 virtual int setPowerAttenuation(int atten, size_t chan = 0);
Tom Tsou76764272016-06-24 14:25:39 -0700172
173 signalVector *outerSendBuffer;
174 signalVector *outerRecvBuffer;
175 std::vector<signalVector *> history;
176 std::vector<bool> active;
Pau Espin Pedrolfd99c6c2019-12-21 00:40:09 +0100177 std::vector<struct freq_cfg_state> rx_freq_state;
178 std::vector<struct freq_cfg_state> tx_freq_state;
Tom Tsou76764272016-06-24 14:25:39 -0700179
180 Resampler *dnsampler;
181 Resampler *upsampler;
182 Channelizer *channelizer;
183 Synthesis *synthesis;
184
185public:
186 RadioInterfaceMulti(RadioDevice* radio, size_t tx_sps,
187 size_t rx_sps, size_t chans = 1);
188 ~RadioInterfaceMulti();
189
190 bool init(int type);
191 void close();
192
193 bool tuneTx(double freq, size_t chan);
194 bool tuneRx(double freq, size_t chan);
Pau Espin Pedrol80d053a2019-09-16 17:20:46 +0200195 virtual double setRxGain(double dB, size_t chan);
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200196 virtual double rssiOffset(size_t chan = 0);
Tom Tsou76764272016-06-24 14:25:39 -0700197};