blob: c75a983d08f4644ef4e0c8f0d315eb48ab96e0ee [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
dburgessb3a0ca42011-10-12 07:44:40 +0000112 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400113 void driveTransmitRadio(std::vector<signalVector *> &bursts,
114 std::vector<bool> &zeros);
dburgessb3a0ca42011-10-12 07:44:40 +0000115
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200116 /** drive reception of GSM bursts. -1: Error. 0: Radio off. 1: Received something. */
117 int driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000118
Tom Tsoua4d1a412014-11-25 15:46:56 -0800119 int setPowerAttenuation(int atten, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000120
121 /** returns the full-scale transmit amplitude **/
122 double fullScaleInputValue();
123
124 /** returns the full-scale receive amplitude **/
125 double fullScaleOutputValue();
126
Pau Espin Pedrol46444632018-09-03 16:42:04 +0200127 /** get transport window type of attached device */
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200128 enum RadioDevice::TxWindowType getWindowType() { return mDevice->getWindowType(); }
kurtis.heimerle380af32011-11-26 03:18:55 +0000129
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200130 /** Minimum latency that the device can achieve */
Pau Espin Pedrola801ae52019-09-13 15:59:29 +0200131 GSM::Time minLatency() { return mDevice->minLatency(); }
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200132
dburgessb3a0ca42011-10-12 07:44:40 +0000133protected:
dburgessb3a0ca42011-10-12 07:44:40 +0000134 /** drive synchronization of Tx/Rx of USRP */
135 void alignRadio();
136
Pau Espin Pedrol62845242019-09-13 17:29:21 +0200137 /** set transmit gain */
138 virtual double setTxGain(double dB, size_t chan = 0);
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
Pau Espin Pedrolfd99c6c2019-12-21 00:40:09 +0100159struct freq_cfg_state {
160 bool set;
161 double freq_hz;
162};
163
Tom Tsou76764272016-06-24 14:25:39 -0700164class RadioInterfaceMulti : public RadioInterface {
165private:
166 bool pushBuffer();
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200167 int pullBuffer();
Pau Espin Pedrolfd99c6c2019-12-21 00:40:09 +0100168 bool verify_arfcn_consistency(double freq, size_t chan, bool tx);
Pau Espin Pedrol62845242019-09-13 17:29:21 +0200169 virtual double setTxGain(double dB, size_t chan);
Tom Tsou76764272016-06-24 14:25:39 -0700170
171 signalVector *outerSendBuffer;
172 signalVector *outerRecvBuffer;
173 std::vector<signalVector *> history;
174 std::vector<bool> active;
Pau Espin Pedrolfd99c6c2019-12-21 00:40:09 +0100175 std::vector<struct freq_cfg_state> rx_freq_state;
176 std::vector<struct freq_cfg_state> tx_freq_state;
Tom Tsou76764272016-06-24 14:25:39 -0700177
178 Resampler *dnsampler;
179 Resampler *upsampler;
180 Channelizer *channelizer;
181 Synthesis *synthesis;
182
183public:
184 RadioInterfaceMulti(RadioDevice* radio, size_t tx_sps,
185 size_t rx_sps, size_t chans = 1);
186 ~RadioInterfaceMulti();
187
188 bool init(int type);
189 void close();
190
191 bool tuneTx(double freq, size_t chan);
192 bool tuneRx(double freq, size_t chan);
Pau Espin Pedrol80d053a2019-09-16 17:20:46 +0200193 virtual double setRxGain(double dB, size_t chan);
Tom Tsou76764272016-06-24 14:25:39 -0700194};