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