dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 4 | * This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribution. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 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 Pedrol | 4644463 | 2018-09-03 16:42:04 +0200 | [diff] [blame] | 17 | #include "sigProcLib.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 18 | #include "GSMCommon.h" |
| 19 | #include "LinkedLists.h" |
| 20 | #include "radioDevice.h" |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 21 | #include "radioVector.h" |
| 22 | #include "radioClock.h" |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 23 | #include "radioBuffer.h" |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 24 | #include "Resampler.h" |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 25 | #include "Channelizer.h" |
| 26 | #include "Synthesis.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 27 | |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 28 | static const unsigned gSlotLen = 148; ///< number of symbols per slot, not counting guard periods |
| 29 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 30 | /** class to interface the transceiver with the USRP */ |
| 31 | class RadioInterface { |
| 32 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 33 | protected: |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 34 | size_t mSPSTx; |
| 35 | size_t mSPSRx; |
| 36 | size_t mChans; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 37 | |
| 38 | Thread mAlignRadioServiceLoopThread; ///< thread that synchronizes transmit and receive sections |
| 39 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 40 | std::vector<VectorFIFO> mReceiveFIFO; ///< FIFO that holds receive bursts |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 41 | |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 42 | RadioDevice *mDevice; ///< the USRP object |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 43 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 44 | std::vector<RadioBuffer *> sendBuffer; |
| 45 | std::vector<RadioBuffer *> recvBuffer; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 46 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 47 | std::vector<short *> convertRecvBuffer; |
| 48 | std::vector<short *> convertSendBuffer; |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 49 | std::vector<float> powerScaling; |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 50 | int underrun; ///< indicates writes to USRP are too slow |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 51 | bool overrun; ///< indicates reads from USRP are too slow |
| 52 | TIMESTAMP writeTimestamp; ///< sample timestamp of next packet written to USRP |
| 53 | TIMESTAMP readTimestamp; ///< sample timestamp of next packet read from USRP |
| 54 | |
| 55 | RadioClock mClock; ///< the basestation clock! |
| 56 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 57 | int receiveOffset; ///< offset b/w transmit and receive GSM timestamps, in timeslots |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 58 | |
| 59 | bool mOn; ///< indicates radio is on |
| 60 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 61 | private: |
| 62 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 63 | /** format samples to USRP */ |
| 64 | int radioifyVector(signalVector &wVector, size_t chan, bool zero); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 65 | |
| 66 | /** format samples from USRP */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 67 | int unRadioifyVector(signalVector *wVector, size_t chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 68 | |
| 69 | /** push GSM bursts into the transmit buffer */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 70 | virtual bool pushBuffer(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 71 | |
| 72 | /** pull GSM bursts from the receive buffer */ |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 73 | virtual int pullBuffer(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 74 | |
| 75 | public: |
| 76 | |
| 77 | /** start the interface */ |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 78 | bool start(); |
| 79 | bool stop(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 80 | |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 81 | /** initialization */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 82 | virtual bool init(int type); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 83 | virtual void close(); |
| 84 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 85 | /** constructor */ |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 86 | RadioInterface(RadioDevice* wDevice, size_t tx_sps, size_t rx_sps, |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 87 | size_t chans = 1, int receiveOffset = 3, |
| 88 | GSM::Time wStartTime = GSM::Time(0)); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 89 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 90 | /** destructor */ |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 91 | virtual ~RadioInterface(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 92 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 93 | /** check for underrun, resets underrun value */ |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 94 | bool isUnderrun(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 95 | |
| 96 | /** return the receive FIFO */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 97 | VectorFIFO* receiveFIFO(size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 98 | |
| 99 | /** return the basestation clock */ |
| 100 | RadioClock* getClock(void) { return &mClock;}; |
| 101 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 102 | /** set transmit frequency */ |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 103 | virtual bool tuneTx(double freq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 104 | |
| 105 | /** set receive frequency */ |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 106 | virtual bool tuneRx(double freq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 107 | |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 108 | /** set receive gain */ |
Pau Espin Pedrol | 80d053a | 2019-09-16 17:20:46 +0200 | [diff] [blame] | 109 | virtual double setRxGain(double dB, size_t chan = 0); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 110 | |
Pau Espin Pedrol | e91544d | 2020-10-13 17:03:37 +0200 | [diff] [blame] | 111 | /** return base RSSI offset to apply for received samples **/ |
| 112 | virtual double rssiOffset(size_t chan = 0); |
| 113 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 114 | /** drive transmission of GSM bursts */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 115 | void driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 116 | std::vector<bool> &zeros); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 117 | |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 118 | /** drive reception of GSM bursts. -1: Error. 0: Radio off. 1: Received something. */ |
| 119 | int driveReceiveRadio(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 120 | |
Pau Espin Pedrol | 992c9bd | 2020-06-08 13:44:24 +0200 | [diff] [blame] | 121 | /** set transmit power attenuation */ |
| 122 | virtual int setPowerAttenuation(int atten, size_t chan = 0); |
Pau Espin Pedrol | 0e09e7c | 2020-05-29 16:39:07 +0200 | [diff] [blame] | 123 | int getNominalTxPower(size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 124 | |
| 125 | /** returns the full-scale transmit amplitude **/ |
| 126 | double fullScaleInputValue(); |
| 127 | |
| 128 | /** returns the full-scale receive amplitude **/ |
| 129 | double fullScaleOutputValue(); |
| 130 | |
Pau Espin Pedrol | 4644463 | 2018-09-03 16:42:04 +0200 | [diff] [blame] | 131 | /** get transport window type of attached device */ |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 132 | enum RadioDevice::TxWindowType getWindowType() { return mDevice->getWindowType(); } |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 133 | |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 134 | /** Minimum latency that the device can achieve */ |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 135 | GSM::Time minLatency() { return mDevice->minLatency(); } |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 136 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 137 | protected: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 138 | /** drive synchronization of Tx/Rx of USRP */ |
| 139 | void alignRadio(); |
| 140 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 141 | friend void *AlignRadioServiceLoopAdapter(RadioInterface*); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 144 | class RadioInterfaceResamp : public RadioInterface { |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 145 | private: |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 146 | signalVector *outerSendBuffer; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 147 | signalVector *outerRecvBuffer; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 148 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 149 | bool pushBuffer(); |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 150 | int pullBuffer(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 151 | |
| 152 | public: |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 153 | RadioInterfaceResamp(RadioDevice* wDevice, size_t tx_sps, size_t rx_sps); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 154 | ~RadioInterfaceResamp(); |
| 155 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 156 | bool init(int type); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 157 | void close(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 158 | }; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 159 | |
Pau Espin Pedrol | fd99c6c | 2019-12-21 00:40:09 +0100 | [diff] [blame] | 160 | struct freq_cfg_state { |
| 161 | bool set; |
| 162 | double freq_hz; |
| 163 | }; |
| 164 | |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 165 | class RadioInterfaceMulti : public RadioInterface { |
| 166 | private: |
| 167 | bool pushBuffer(); |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 168 | int pullBuffer(); |
Pau Espin Pedrol | fd99c6c | 2019-12-21 00:40:09 +0100 | [diff] [blame] | 169 | bool verify_arfcn_consistency(double freq, size_t chan, bool tx); |
Pau Espin Pedrol | 992c9bd | 2020-06-08 13:44:24 +0200 | [diff] [blame] | 170 | virtual int setPowerAttenuation(int atten, size_t chan = 0); |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 171 | |
| 172 | signalVector *outerSendBuffer; |
| 173 | signalVector *outerRecvBuffer; |
| 174 | std::vector<signalVector *> history; |
| 175 | std::vector<bool> active; |
Pau Espin Pedrol | fd99c6c | 2019-12-21 00:40:09 +0100 | [diff] [blame] | 176 | std::vector<struct freq_cfg_state> rx_freq_state; |
| 177 | std::vector<struct freq_cfg_state> tx_freq_state; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 178 | |
| 179 | Resampler *dnsampler; |
| 180 | Resampler *upsampler; |
| 181 | Channelizer *channelizer; |
| 182 | Synthesis *synthesis; |
| 183 | |
| 184 | public: |
| 185 | RadioInterfaceMulti(RadioDevice* radio, size_t tx_sps, |
| 186 | size_t rx_sps, size_t chans = 1); |
| 187 | ~RadioInterfaceMulti(); |
| 188 | |
| 189 | bool init(int type); |
| 190 | void close(); |
| 191 | |
| 192 | bool tuneTx(double freq, size_t chan); |
| 193 | bool tuneRx(double freq, size_t chan); |
Pau Espin Pedrol | 80d053a | 2019-09-16 17:20:46 +0200 | [diff] [blame] | 194 | virtual double setRxGain(double dB, size_t chan); |
Pau Espin Pedrol | e91544d | 2020-10-13 17:03:37 +0200 | [diff] [blame] | 195 | virtual double rssiOffset(size_t chan = 0); |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 196 | }; |