dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 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.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 21 | #include "radioVector.h" |
| 22 | #include "radioClock.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 23 | |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 24 | static const unsigned gSlotLen = 148; ///< number of symbols per slot, not counting guard periods |
| 25 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 26 | /** class to interface the transceiver with the USRP */ |
| 27 | class RadioInterface { |
| 28 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 29 | protected: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 30 | |
| 31 | Thread mAlignRadioServiceLoopThread; ///< thread that synchronizes transmit and receive sections |
| 32 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 33 | std::vector<VectorFIFO> mReceiveFIFO; ///< FIFO that holds receive bursts |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 34 | |
| 35 | RadioDevice *mRadio; ///< the USRP object |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 36 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 37 | size_t mSPSTx; |
| 38 | size_t mSPSRx; |
| 39 | size_t mChans; |
| 40 | std::vector<signalVector *> sendBuffer; |
| 41 | std::vector<signalVector *> recvBuffer; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 42 | unsigned sendCursor; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 43 | unsigned recvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 44 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 45 | std::vector<short *> convertRecvBuffer; |
| 46 | std::vector<short *> convertSendBuffer; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 47 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 48 | bool underrun; ///< indicates writes to USRP are too slow |
| 49 | bool overrun; ///< indicates reads from USRP are too slow |
| 50 | TIMESTAMP writeTimestamp; ///< sample timestamp of next packet written to USRP |
| 51 | TIMESTAMP readTimestamp; ///< sample timestamp of next packet read from USRP |
| 52 | |
| 53 | RadioClock mClock; ///< the basestation clock! |
| 54 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 55 | int receiveOffset; ///< offset b/w transmit and receive GSM timestamps, in timeslots |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 56 | |
| 57 | bool mOn; ///< indicates radio is on |
| 58 | |
| 59 | double powerScaling; |
| 60 | |
| 61 | bool loadTest; |
| 62 | int mNumARFCNs; |
| 63 | signalVector *finalVec, *finalVec9; |
| 64 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 65 | private: |
| 66 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 67 | /** format samples to USRP */ |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 68 | int radioifyVector(signalVector &wVector, |
| 69 | float *floatVector, |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 70 | bool zero); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 71 | |
| 72 | /** format samples from USRP */ |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 73 | int unRadioifyVector(float *floatVector, signalVector &wVector); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 74 | |
| 75 | /** push GSM bursts into the transmit buffer */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 76 | virtual void pushBuffer(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 77 | |
| 78 | /** pull GSM bursts from the receive buffer */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 79 | virtual void pullBuffer(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 80 | |
| 81 | public: |
| 82 | |
| 83 | /** start the interface */ |
| 84 | void start(); |
| 85 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 86 | /** intialization */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 87 | virtual bool init(int type); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 88 | virtual void close(); |
| 89 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 90 | /** constructor */ |
| 91 | RadioInterface(RadioDevice* wRadio = NULL, |
| 92 | int receiveOffset = 3, |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 93 | size_t sps = 4, size_t chans = 1, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 94 | GSM::Time wStartTime = GSM::Time(0)); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 95 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 96 | /** destructor */ |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 97 | virtual ~RadioInterface(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 98 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 99 | /** check for underrun, resets underrun value */ |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 100 | bool isUnderrun(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 101 | |
| 102 | /** return the receive FIFO */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 103 | VectorFIFO* receiveFIFO(size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 104 | |
| 105 | /** return the basestation clock */ |
| 106 | RadioClock* getClock(void) { return &mClock;}; |
| 107 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 108 | /** set transmit frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 109 | bool tuneTx(double freq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 110 | |
| 111 | /** set receive frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 112 | bool tuneRx(double freq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 113 | |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 114 | /** set receive gain */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 115 | double setRxGain(double dB, size_t chan = 0); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 116 | |
| 117 | /** get receive gain */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 118 | double getRxGain(size_t chan = 0); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 119 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 120 | /** drive transmission of GSM bursts */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 121 | void driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 122 | std::vector<bool> &zeros); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 123 | |
| 124 | /** drive reception of GSM bursts */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 125 | bool driveReceiveRadio(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 126 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 127 | void setPowerAttenuation(double atten, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 128 | |
| 129 | /** returns the full-scale transmit amplitude **/ |
| 130 | double fullScaleInputValue(); |
| 131 | |
| 132 | /** returns the full-scale receive amplitude **/ |
| 133 | double fullScaleOutputValue(); |
| 134 | |
kurtis.heimerl | 6b495a5 | 2011-11-26 03:17:21 +0000 | [diff] [blame] | 135 | /** set thread priority on current thread */ |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame^] | 136 | void setPriority(float prio = 0.5) { mRadio->setPriority(prio); } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 137 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 138 | /** get transport window type of attached device */ |
| 139 | enum RadioDevice::TxWindowType getWindowType() { return mRadio->getWindowType(); } |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 140 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 141 | #if USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 142 | protected: |
| 143 | |
| 144 | /** drive synchronization of Tx/Rx of USRP */ |
| 145 | void alignRadio(); |
| 146 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 147 | friend void *AlignRadioServiceLoopAdapter(RadioInterface*); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 148 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 151 | #if USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 152 | /** synchronization thread loop */ |
| 153 | void *AlignRadioServiceLoopAdapter(RadioInterface*); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 154 | #endif |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 155 | |
| 156 | class RadioInterfaceResamp : public RadioInterface { |
| 157 | |
| 158 | private: |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 159 | signalVector *innerSendBuffer; |
| 160 | signalVector *outerSendBuffer; |
| 161 | signalVector *innerRecvBuffer; |
| 162 | signalVector *outerRecvBuffer; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 163 | |
| 164 | void pushBuffer(); |
| 165 | void pullBuffer(); |
| 166 | |
| 167 | public: |
| 168 | |
| 169 | RadioInterfaceResamp(RadioDevice* wRadio = NULL, |
| 170 | int receiveOffset = 3, |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 171 | size_t wSPS = 4, size_t chans = 1, |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 172 | GSM::Time wStartTime = GSM::Time(0)); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 173 | |
| 174 | ~RadioInterfaceResamp(); |
| 175 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 176 | bool init(int type); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 177 | void close(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 178 | }; |