blob: 5c962d1960684b79723f440a6e3bf9e38a37b6fa [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#ifndef __RADIO_DEVICE_H__
16#define __RADIO_DEVICE_H__
17
ttsouf60dafa2012-10-22 00:07:14 +000018#include <string>
Thomas Tsou204a9f12013-10-29 18:34:16 -040019#include <vector>
dburgessb3a0ca42011-10-12 07:44:40 +000020
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +020021#include "GSMCommon.h"
Harald Welte24073142018-06-13 19:32:15 +020022#include "Logger.h"
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +020023
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010024extern "C" {
25#include "config_defs.h"
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +020026#include "osmo_signal.h"
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010027}
28
dburgessb3a0ca42011-10-12 07:44:40 +000029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
Alexander Chemerisf931cf22016-06-18 10:42:33 +030033#define GSMRATE (1625e3/6)
Tom Tsou76764272016-06-24 14:25:39 -070034#define MCBTS_SPACING 800000.0
Thomas Tsoucb69f082013-04-08 14:18:26 -040035
dburgessb3a0ca42011-10-12 07:44:40 +000036/** a 64-bit virtual timestamp for radio data */
37typedef unsigned long long TIMESTAMP;
38
39/** A class to handle a USRP rev 4, with a two RFX900 daughterboards */
40class RadioDevice {
41
42 public:
kurtis.heimerle380af32011-11-26 03:18:55 +000043 /* Available transport bus types */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020044 enum TxWindowType { TX_WINDOW_USRP1, TX_WINDOW_FIXED, TX_WINDOW_LMS1 };
kurtis.heimerle380af32011-11-26 03:18:55 +000045
Thomas Tsoucb69f082013-04-08 14:18:26 -040046 /* Radio interface types */
Tom Tsou05c6feb2016-06-22 16:09:44 -070047 enum InterfaceType {
48 NORMAL,
49 RESAMP_64M,
50 RESAMP_100M,
Tom Tsou76764272016-06-24 14:25:39 -070051 MULTI_ARFCN,
Tom Tsou05c6feb2016-06-22 16:09:44 -070052 };
Thomas Tsoucb69f082013-04-08 14:18:26 -040053
Eric19e134a2023-05-10 23:50:38 +020054 static RadioDevice *make(InterfaceType type, const struct trx_cfg *cfg);
kurtis.heimerl965e7572011-11-26 03:16:54 +000055
kurtis.heimerldb2aae52011-11-26 03:17:13 +000056 /** Initialize the USRP */
Eric19e134a2023-05-10 23:50:38 +020057 virtual int open() = 0;
dburgessb3a0ca42011-10-12 07:44:40 +000058
Thomas Tsou8c336792013-11-15 16:26:05 -050059 virtual ~RadioDevice() { }
60
dburgessb3a0ca42011-10-12 07:44:40 +000061 /** Start the USRP */
62 virtual bool start()=0;
63
64 /** Stop the USRP */
65 virtual bool stop()=0;
66
Thomas Tsou02d88d12013-04-05 15:36:30 -040067 /** Get the Tx window type */
68 virtual enum TxWindowType getWindowType()=0;
kurtis.heimerle380af32011-11-26 03:18:55 +000069
dburgessb3a0ca42011-10-12 07:44:40 +000070 /**
71 Read samples from the radio.
72 @param buf preallocated buf to contain read result
73 @param len number of samples desired
74 @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough
75 @param timestamp The timestamp of the first samples to be read
76 @param underrun Set if radio does not have data to transmit, e.g. data not being sent fast enough
dburgessb3a0ca42011-10-12 07:44:40 +000077 @return The number of samples actually read
78 */
Thomas Tsou204a9f12013-10-29 18:34:16 -040079 virtual int readSamples(std::vector<short *> &bufs, int len, bool *overrun,
Pau Espin Pedrolf8c0c462020-03-12 19:08:46 +010080 TIMESTAMP timestamp = 0xffffffff, bool *underrun = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +000081 /**
82 Write samples to the radio.
83 @param buf Contains the data to be written.
84 @param len number of samples to write.
85 @param underrun Set if radio does not have data to transmit, e.g. data not being sent fast enough
86 @param timestamp The timestamp of the first sample of the data buffer.
dburgessb3a0ca42011-10-12 07:44:40 +000087 @return The number of samples actually written
88 */
Thomas Tsou204a9f12013-10-29 18:34:16 -040089 virtual int writeSamples(std::vector<short *> &bufs, int len, bool *underrun,
Pau Espin Pedroldfc6e5f2020-03-12 19:35:33 +010090 TIMESTAMP timestamp) = 0;
Thomas Tsou204a9f12013-10-29 18:34:16 -040091
dburgessb3a0ca42011-10-12 07:44:40 +000092 /** Update the alignment between the read and write timestamps */
93 virtual bool updateAlignment(TIMESTAMP timestamp)=0;
Thomas Tsou204a9f12013-10-29 18:34:16 -040094
dburgessb3a0ca42011-10-12 07:44:40 +000095 /** Set the transmitter frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -040096 virtual bool setTxFreq(double wFreq, size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +000097
98 /** Set the receiver frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -040099 virtual bool setRxFreq(double wFreq, size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000100
101 /** Returns the starting write Timestamp*/
102 virtual TIMESTAMP initialWriteTimestamp(void)=0;
103
104 /** Returns the starting read Timestamp*/
105 virtual TIMESTAMP initialReadTimestamp(void)=0;
106
107 /** returns the full-scale transmit amplitude **/
108 virtual double fullScaleInputValue()=0;
109
110 /** returns the full-scale receive amplitude **/
111 virtual double fullScaleOutputValue()=0;
112
113 /** sets the receive chan gain, returns the gain setting **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400114 virtual double setRxGain(double dB, size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000115
116 /** gets the current receive gain **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400117 virtual double getRxGain(size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000118
119 /** return maximum Rx Gain **/
120 virtual double maxRxGain(void) = 0;
121
122 /** return minimum Rx Gain **/
123 virtual double minRxGain(void) = 0;
124
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200125 /** return base RSSI offset to apply for received samples **/
126 virtual double rssiOffset(size_t chan) = 0;
127
Pau Espin Pedrol0e09e7c2020-05-29 16:39:07 +0200128 /** returns the Nominal transmit output power of the transceiver in dBm, negative on error **/
129 virtual int getNominalTxPower(size_t chan = 0) = 0;
130
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100131 /** sets the RX path to use, returns true if successful and false otherwise */
132 virtual bool setRxAntenna(const std::string &ant, size_t chan = 0) = 0;
133
134 /** return the used RX path */
135 virtual std::string getRxAntenna(size_t chan = 0) = 0;
136
137 /** sets the RX path to use, returns true if successful and false otherwise */
138 virtual bool setTxAntenna(const std::string &ant, size_t chan = 0) = 0;
139
140 /** return the used RX path */
141 virtual std::string getTxAntenna(size_t chan = 0) = 0;
142
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200143 /** return whether user drives synchronization of Tx/Rx of USRP */
144 virtual bool requiresRadioAlign() = 0;
145
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200146 /** Minimum latency that the device can achieve */
147 virtual GSM::Time minLatency() = 0;
148
dburgessb3a0ca42011-10-12 07:44:40 +0000149 /** Return internal status values */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400150 virtual double getTxFreq(size_t chan = 0) = 0;
151 virtual double getRxFreq(size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000152 virtual double getSampleRate()=0;
dburgessb3a0ca42011-10-12 07:44:40 +0000153
Pau Espin Pedrol5bd3d422020-06-19 18:11:54 +0200154 virtual double setPowerAttenuation(int atten, size_t chan) = 0;
155 virtual double getPowerAttenuation(size_t chan=0) = 0;
Pau Espin Pedrol992c9bd2020-06-08 13:44:24 +0200156
Harald Welte61707e82018-06-13 23:21:57 +0200157 protected:
158 size_t tx_sps, rx_sps;
159 InterfaceType iface;
160 size_t chans;
161 double lo_offset;
Harald Welte24073142018-06-13 19:32:15 +0200162 std::vector<std::string> tx_paths, rx_paths;
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200163 std::vector<struct device_counters> m_ctr;
Eric19e134a2023-05-10 23:50:38 +0200164 const struct trx_cfg *cfg;
Harald Welte61707e82018-06-13 23:21:57 +0200165
Eric19e134a2023-05-10 23:50:38 +0200166#define charp2str(a) ((a) ? std::string(a) : std::string(""))
Pau Espin Pedrolee2ba192019-09-16 13:52:40 +0200167
Eric19e134a2023-05-10 23:50:38 +0200168 RadioDevice(InterfaceType type, const struct trx_cfg *cfg)
169 : tx_sps(cfg->tx_sps), rx_sps(cfg->rx_sps), iface(type), chans(cfg->num_chans), lo_offset(cfg->offset),
170 m_ctr(chans), cfg(cfg)
171 {
172 /* Generate vector of rx/tx_path: */
173 for (unsigned int i = 0; i < cfg->num_chans; i++) {
174 rx_paths.push_back(charp2str(cfg->chans[i].rx_path));
175 tx_paths.push_back(charp2str(cfg->chans[i].tx_path));
176 }
177
178 if (iface == MULTI_ARFCN) {
179 LOGC(DDEV, INFO) << "Multi-ARFCN: " << chans << " logical chans -> 1 physical chans";
180 chans = 1;
181 }
182
183 for (size_t i = 0; i < chans; i++) {
184 memset(&m_ctr[i], 0, sizeof(m_ctr[i]));
185 m_ctr[i].chan = i;
186 }
187 }
Harald Welte61707e82018-06-13 23:21:57 +0200188
Harald Welte24073142018-06-13 19:32:15 +0200189 bool set_antennas() {
190 unsigned int i;
191
192 for (i = 0; i < tx_paths.size(); i++) {
193 if (tx_paths[i] == "")
194 continue;
Vadim Yanitskiyb7c6f1e2020-09-11 18:03:56 +0700195 if (iface == MULTI_ARFCN && i > 0) {
196 LOGCHAN(i, DDEV, NOTICE) << "Not setting Tx antenna "
197 << tx_paths[i]
198 << " for a logical channel";
199 continue;
200 }
201
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200202 LOGCHAN(i, DDEV, DEBUG) << "Configuring Tx antenna " << tx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200203 if (!setTxAntenna(tx_paths[i], i)) {
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200204 LOGCHAN(i, DDEV, ALERT) << "Failed configuring Tx antenna " << tx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200205 return false;
206 }
207 }
208
209 for (i = 0; i < rx_paths.size(); i++) {
210 if (rx_paths[i] == "")
211 continue;
Vadim Yanitskiyb7c6f1e2020-09-11 18:03:56 +0700212 if (iface == MULTI_ARFCN && i > 0) {
213 LOGCHAN(i, DDEV, NOTICE) << "Not setting Rx antenna "
214 << rx_paths[i]
215 << " for a logical channel";
216 continue;
217 }
218
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200219 LOGCHAN(i, DDEV, DEBUG) << "Configuring Rx antenna " << rx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200220 if (!setRxAntenna(rx_paths[i], i)) {
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200221 LOGCHAN(i, DDEV, ALERT) << "Failed configuring Rx antenna " << rx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200222 return false;
223 }
224 }
225 LOG(INFO) << "Antennas configured successfully";
226 return true;
227 }
228
229
dburgessb3a0ca42011-10-12 07:44:40 +0000230};
231
232#endif