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 | #ifndef __RADIO_DEVICE_H__ |
| 16 | #define __RADIO_DEVICE_H__ |
| 17 | |
ttsou | f60dafa | 2012-10-22 00:07:14 +0000 | [diff] [blame] | 18 | #include <string> |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 19 | #include <vector> |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 20 | |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 21 | #include "GSMCommon.h" |
Harald Welte | 2407314 | 2018-06-13 19:32:15 +0200 | [diff] [blame] | 22 | #include "Logger.h" |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 23 | |
Pau Espin Pedrol | efac20b | 2018-02-21 14:59:19 +0100 | [diff] [blame] | 24 | extern "C" { |
| 25 | #include "config_defs.h" |
| 26 | } |
| 27 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
Alexander Chemeris | f931cf2 | 2016-06-18 10:42:33 +0300 | [diff] [blame] | 32 | #define GSMRATE (1625e3/6) |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 33 | #define MCBTS_SPACING 800000.0 |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 34 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 35 | /** a 64-bit virtual timestamp for radio data */ |
| 36 | typedef unsigned long long TIMESTAMP; |
| 37 | |
| 38 | /** A class to handle a USRP rev 4, with a two RFX900 daughterboards */ |
| 39 | class RadioDevice { |
| 40 | |
| 41 | public: |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 42 | /* Available transport bus types */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 43 | enum TxWindowType { TX_WINDOW_USRP1, TX_WINDOW_FIXED, TX_WINDOW_LMS1 }; |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 44 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 45 | /* Radio interface types */ |
Tom Tsou | 05c6feb | 2016-06-22 16:09:44 -0700 | [diff] [blame] | 46 | enum InterfaceType { |
| 47 | NORMAL, |
| 48 | RESAMP_64M, |
| 49 | RESAMP_100M, |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 50 | MULTI_ARFCN, |
Tom Tsou | 05c6feb | 2016-06-22 16:09:44 -0700 | [diff] [blame] | 51 | }; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 52 | |
Tom Tsou | 05c6feb | 2016-06-22 16:09:44 -0700 | [diff] [blame] | 53 | static RadioDevice *make(size_t tx_sps, size_t rx_sps, InterfaceType type, |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 54 | size_t chans = 1, double offset = 0.0, |
| 55 | const std::vector<std::string>& tx_paths = std::vector<std::string>(1, ""), |
| 56 | const std::vector<std::string>& rx_paths = std::vector<std::string>(1, "")); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 57 | |
kurtis.heimerl | db2aae5 | 2011-11-26 03:17:13 +0000 | [diff] [blame] | 58 | /** Initialize the USRP */ |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 59 | virtual int open(const std::string &args, int ref, bool swap_channels)=0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 60 | |
Thomas Tsou | 8c33679 | 2013-11-15 16:26:05 -0500 | [diff] [blame] | 61 | virtual ~RadioDevice() { } |
| 62 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 63 | /** Start the USRP */ |
| 64 | virtual bool start()=0; |
| 65 | |
| 66 | /** Stop the USRP */ |
| 67 | virtual bool stop()=0; |
| 68 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 69 | /** Get the Tx window type */ |
| 70 | virtual enum TxWindowType getWindowType()=0; |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 71 | |
kurtis.heimerl | db2aae5 | 2011-11-26 03:17:13 +0000 | [diff] [blame] | 72 | /** Enable thread priority */ |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 73 | virtual void setPriority(float prio = 0.5) = 0; |
kurtis.heimerl | db2aae5 | 2011-11-26 03:17:13 +0000 | [diff] [blame] | 74 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 75 | /** |
| 76 | Read samples from the radio. |
| 77 | @param buf preallocated buf to contain read result |
| 78 | @param len number of samples desired |
| 79 | @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough |
| 80 | @param timestamp The timestamp of the first samples to be read |
| 81 | @param underrun Set if radio does not have data to transmit, e.g. data not being sent fast enough |
| 82 | @param RSSI The received signal strength of the read result |
| 83 | @return The number of samples actually read |
| 84 | */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 85 | virtual int readSamples(std::vector<short *> &bufs, int len, bool *overrun, |
| 86 | TIMESTAMP timestamp = 0xffffffff, bool *underrun = 0, |
| 87 | unsigned *RSSI = 0) = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 88 | /** |
| 89 | Write samples to the radio. |
| 90 | @param buf Contains the data to be written. |
| 91 | @param len number of samples to write. |
| 92 | @param underrun Set if radio does not have data to transmit, e.g. data not being sent fast enough |
| 93 | @param timestamp The timestamp of the first sample of the data buffer. |
| 94 | @param isControl Set if data is a control packet, e.g. a ping command |
| 95 | @return The number of samples actually written |
| 96 | */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 97 | virtual int writeSamples(std::vector<short *> &bufs, int len, bool *underrun, |
| 98 | TIMESTAMP timestamp, bool isControl = false) = 0; |
| 99 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 100 | /** Update the alignment between the read and write timestamps */ |
| 101 | virtual bool updateAlignment(TIMESTAMP timestamp)=0; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 102 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 103 | /** Set the transmitter frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 104 | virtual bool setTxFreq(double wFreq, size_t chan = 0) = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 105 | |
| 106 | /** Set the receiver frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 107 | virtual bool setRxFreq(double wFreq, size_t chan = 0) = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 108 | |
| 109 | /** Returns the starting write Timestamp*/ |
| 110 | virtual TIMESTAMP initialWriteTimestamp(void)=0; |
| 111 | |
| 112 | /** Returns the starting read Timestamp*/ |
| 113 | virtual TIMESTAMP initialReadTimestamp(void)=0; |
| 114 | |
| 115 | /** returns the full-scale transmit amplitude **/ |
| 116 | virtual double fullScaleInputValue()=0; |
| 117 | |
| 118 | /** returns the full-scale receive amplitude **/ |
| 119 | virtual double fullScaleOutputValue()=0; |
| 120 | |
| 121 | /** sets the receive chan gain, returns the gain setting **/ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 122 | virtual double setRxGain(double dB, size_t chan = 0) = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 123 | |
| 124 | /** gets the current receive gain **/ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 125 | virtual double getRxGain(size_t chan = 0) = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 126 | |
| 127 | /** return maximum Rx Gain **/ |
| 128 | virtual double maxRxGain(void) = 0; |
| 129 | |
| 130 | /** return minimum Rx Gain **/ |
| 131 | virtual double minRxGain(void) = 0; |
| 132 | |
| 133 | /** sets the transmit chan gain, returns the gain setting **/ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 134 | virtual double setTxGain(double dB, size_t chan = 0) = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 135 | |
| 136 | /** return maximum Tx Gain **/ |
| 137 | virtual double maxTxGain(void) = 0; |
| 138 | |
| 139 | /** return minimum Tx Gain **/ |
| 140 | virtual double minTxGain(void) = 0; |
| 141 | |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 142 | /** sets the RX path to use, returns true if successful and false otherwise */ |
| 143 | virtual bool setRxAntenna(const std::string &ant, size_t chan = 0) = 0; |
| 144 | |
| 145 | /** return the used RX path */ |
| 146 | virtual std::string getRxAntenna(size_t chan = 0) = 0; |
| 147 | |
| 148 | /** sets the RX path to use, returns true if successful and false otherwise */ |
| 149 | virtual bool setTxAntenna(const std::string &ant, size_t chan = 0) = 0; |
| 150 | |
| 151 | /** return the used RX path */ |
| 152 | virtual std::string getTxAntenna(size_t chan = 0) = 0; |
| 153 | |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 154 | /** return whether user drives synchronization of Tx/Rx of USRP */ |
| 155 | virtual bool requiresRadioAlign() = 0; |
| 156 | |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 157 | /** Minimum latency that the device can achieve */ |
| 158 | virtual GSM::Time minLatency() = 0; |
| 159 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 160 | /** Return internal status values */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 161 | virtual double getTxFreq(size_t chan = 0) = 0; |
| 162 | virtual double getRxFreq(size_t chan = 0) = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 163 | virtual double getSampleRate()=0; |
| 164 | virtual double numberRead()=0; |
| 165 | virtual double numberWritten()=0; |
| 166 | |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 167 | protected: |
| 168 | size_t tx_sps, rx_sps; |
| 169 | InterfaceType iface; |
| 170 | size_t chans; |
| 171 | double lo_offset; |
Harald Welte | 2407314 | 2018-06-13 19:32:15 +0200 | [diff] [blame] | 172 | std::vector<std::string> tx_paths, rx_paths; |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 173 | |
| 174 | RadioDevice(size_t tx_sps, size_t rx_sps, InterfaceType type, size_t chans, double offset, |
| 175 | const std::vector<std::string>& tx_paths, |
| 176 | const std::vector<std::string>& rx_paths): |
| 177 | tx_sps(tx_sps), rx_sps(rx_sps), iface(type), chans(chans), lo_offset(offset), |
| 178 | tx_paths(tx_paths), rx_paths(rx_paths) |
| 179 | { } |
| 180 | |
Harald Welte | 2407314 | 2018-06-13 19:32:15 +0200 | [diff] [blame] | 181 | bool set_antennas() { |
| 182 | unsigned int i; |
| 183 | |
| 184 | for (i = 0; i < tx_paths.size(); i++) { |
| 185 | if (tx_paths[i] == "") |
| 186 | continue; |
| 187 | LOG(DEBUG) << "Configuring channel " << i << " with antenna " << tx_paths[i]; |
| 188 | if (!setTxAntenna(tx_paths[i], i)) { |
| 189 | LOG(ALERT) << "Failed configuring channel " << i << " with antenna " << tx_paths[i]; |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | for (i = 0; i < rx_paths.size(); i++) { |
| 195 | if (rx_paths[i] == "") |
| 196 | continue; |
| 197 | LOG(DEBUG) << "Configuring channel " << i << " with antenna " << rx_paths[i]; |
| 198 | if (!setRxAntenna(rx_paths[i], i)) { |
| 199 | LOG(ALERT) << "Failed configuring channel " << i << " with antenna " << rx_paths[i]; |
| 200 | return false; |
| 201 | } |
| 202 | } |
| 203 | LOG(INFO) << "Antennas configured successfully"; |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | #endif |