blob: b4928f28303e37a4a762f21aa25bda1e170a7451 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
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
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
Tom Tsou05c6feb2016-06-22 16:09:44 -070054 static RadioDevice *make(size_t tx_sps, size_t rx_sps, InterfaceType type,
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010055 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.heimerl965e7572011-11-26 03:16:54 +000058
kurtis.heimerldb2aae52011-11-26 03:17:13 +000059 /** Initialize the USRP */
Tom Tsou2f3e60b2016-07-17 19:29:08 -070060 virtual int open(const std::string &args, int ref, bool swap_channels)=0;
dburgessb3a0ca42011-10-12 07:44:40 +000061
Thomas Tsou8c336792013-11-15 16:26:05 -050062 virtual ~RadioDevice() { }
63
dburgessb3a0ca42011-10-12 07:44:40 +000064 /** Start the USRP */
65 virtual bool start()=0;
66
67 /** Stop the USRP */
68 virtual bool stop()=0;
69
Thomas Tsou02d88d12013-04-05 15:36:30 -040070 /** Get the Tx window type */
71 virtual enum TxWindowType getWindowType()=0;
kurtis.heimerle380af32011-11-26 03:18:55 +000072
dburgessb3a0ca42011-10-12 07:44:40 +000073 /**
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
80 @param RSSI The received signal strength of the read result
81 @return The number of samples actually read
82 */
Thomas Tsou204a9f12013-10-29 18:34:16 -040083 virtual int readSamples(std::vector<short *> &bufs, int len, bool *overrun,
84 TIMESTAMP timestamp = 0xffffffff, bool *underrun = 0,
85 unsigned *RSSI = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +000086 /**
87 Write samples to the radio.
88 @param buf Contains the data to be written.
89 @param len number of samples to write.
90 @param underrun Set if radio does not have data to transmit, e.g. data not being sent fast enough
91 @param timestamp The timestamp of the first sample of the data buffer.
92 @param isControl Set if data is a control packet, e.g. a ping command
93 @return The number of samples actually written
94 */
Thomas Tsou204a9f12013-10-29 18:34:16 -040095 virtual int writeSamples(std::vector<short *> &bufs, int len, bool *underrun,
96 TIMESTAMP timestamp, bool isControl = false) = 0;
97
dburgessb3a0ca42011-10-12 07:44:40 +000098 /** Update the alignment between the read and write timestamps */
99 virtual bool updateAlignment(TIMESTAMP timestamp)=0;
Thomas Tsou204a9f12013-10-29 18:34:16 -0400100
dburgessb3a0ca42011-10-12 07:44:40 +0000101 /** Set the transmitter frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400102 virtual bool setTxFreq(double wFreq, size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000103
104 /** Set the receiver frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400105 virtual bool setRxFreq(double wFreq, size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000106
107 /** Returns the starting write Timestamp*/
108 virtual TIMESTAMP initialWriteTimestamp(void)=0;
109
110 /** Returns the starting read Timestamp*/
111 virtual TIMESTAMP initialReadTimestamp(void)=0;
112
113 /** returns the full-scale transmit amplitude **/
114 virtual double fullScaleInputValue()=0;
115
116 /** returns the full-scale receive amplitude **/
117 virtual double fullScaleOutputValue()=0;
118
119 /** sets the receive chan gain, returns the gain setting **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400120 virtual double setRxGain(double dB, size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000121
122 /** gets the current receive gain **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400123 virtual double getRxGain(size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000124
125 /** return maximum Rx Gain **/
126 virtual double maxRxGain(void) = 0;
127
128 /** return minimum Rx Gain **/
129 virtual double minRxGain(void) = 0;
130
131 /** sets the transmit chan gain, returns the gain setting **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400132 virtual double setTxGain(double dB, size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000133
134 /** return maximum Tx Gain **/
135 virtual double maxTxGain(void) = 0;
136
137 /** return minimum Tx Gain **/
138 virtual double minTxGain(void) = 0;
139
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100140 /** sets the RX path to use, returns true if successful and false otherwise */
141 virtual bool setRxAntenna(const std::string &ant, size_t chan = 0) = 0;
142
143 /** return the used RX path */
144 virtual std::string getRxAntenna(size_t chan = 0) = 0;
145
146 /** sets the RX path to use, returns true if successful and false otherwise */
147 virtual bool setTxAntenna(const std::string &ant, size_t chan = 0) = 0;
148
149 /** return the used RX path */
150 virtual std::string getTxAntenna(size_t chan = 0) = 0;
151
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200152 /** return whether user drives synchronization of Tx/Rx of USRP */
153 virtual bool requiresRadioAlign() = 0;
154
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200155 /** Minimum latency that the device can achieve */
156 virtual GSM::Time minLatency() = 0;
157
dburgessb3a0ca42011-10-12 07:44:40 +0000158 /** Return internal status values */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400159 virtual double getTxFreq(size_t chan = 0) = 0;
160 virtual double getRxFreq(size_t chan = 0) = 0;
dburgessb3a0ca42011-10-12 07:44:40 +0000161 virtual double getSampleRate()=0;
dburgessb3a0ca42011-10-12 07:44:40 +0000162
Harald Welte61707e82018-06-13 23:21:57 +0200163 protected:
164 size_t tx_sps, rx_sps;
165 InterfaceType iface;
166 size_t chans;
167 double lo_offset;
Harald Welte24073142018-06-13 19:32:15 +0200168 std::vector<std::string> tx_paths, rx_paths;
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200169 std::vector<struct device_counters> m_ctr;
Harald Welte61707e82018-06-13 23:21:57 +0200170
171 RadioDevice(size_t tx_sps, size_t rx_sps, InterfaceType type, size_t chans, double offset,
172 const std::vector<std::string>& tx_paths,
173 const std::vector<std::string>& rx_paths):
174 tx_sps(tx_sps), rx_sps(rx_sps), iface(type), chans(chans), lo_offset(offset),
175 tx_paths(tx_paths), rx_paths(rx_paths)
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200176 {
177 m_ctr.resize(chans);
178 for (size_t i = 0; i < chans; i++) {
179 memset(&m_ctr[i], 0, sizeof(m_ctr[i]));
180 m_ctr[i].chan = i;
181 }
182 }
Harald Welte61707e82018-06-13 23:21:57 +0200183
Harald Welte24073142018-06-13 19:32:15 +0200184 bool set_antennas() {
185 unsigned int i;
186
187 for (i = 0; i < tx_paths.size(); i++) {
188 if (tx_paths[i] == "")
189 continue;
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200190 LOGCHAN(i, DDEV, DEBUG) << "Configuring Tx antenna " << tx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200191 if (!setTxAntenna(tx_paths[i], i)) {
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200192 LOGCHAN(i, DDEV, ALERT) << "Failed configuring Tx antenna " << tx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200193 return false;
194 }
195 }
196
197 for (i = 0; i < rx_paths.size(); i++) {
198 if (rx_paths[i] == "")
199 continue;
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200200 LOGCHAN(i, DDEV, DEBUG) << "Configuring Rx antenna " << rx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200201 if (!setRxAntenna(rx_paths[i], i)) {
Pau Espin Pedrol77f33962019-08-26 17:11:12 +0200202 LOGCHAN(i, DDEV, ALERT) << "Failed configuring Rx antenna " << rx_paths[i];
Harald Welte24073142018-06-13 19:32:15 +0200203 return false;
204 }
205 }
206 LOG(INFO) << "Antennas configured successfully";
207 return true;
208 }
209
210
dburgessb3a0ca42011-10-12 07:44:40 +0000211};
212
213#endif