/* * Copyright 2026 Wavelet Lab * * SPDX-License-Identifier: AGPL-3.0+ * * This software is distributed under multiple licenses; see the COPYING file in * the main directory for licensing information for this specific distribution. * * This use of this software may be subject to additional restrictions. * See the LEGAL file in the main directory for details. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * */ #ifndef _USDR_DEVICE_H_ #define _USDR_DEVICE_H_ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "radioDevice.h" #include #include #include #include #include #include #include #include "Threads.h" extern "C" { #include #include #include #include } enum usdr_dev_type { UNKNOWN_DEV = 0, /* Unknown device type */ USDR, /* USDR device */ XSDR, /* XSDR device */ SSDR, /* SSDR device */ }; struct parm_range { double min; double max; }; struct dev_desc { /* RX gain range values for the device */ struct parm_range rx_gain_range; /* TX gain range values for the device */ struct parm_range tx_gain_range; /** * Time correction for TX timestamp, in seconds, to align it with RX timestamp, * which is needed to achieve correct timing of the transmitted signal. * This is a device-specific parameter and defaults to 0 if not specified. */ std::chrono::duration tx_offset; /* Description string for the device, which can be used for logging or debugging purposes. */ std::string desc_str; }; using devname_map_t = std::map; using dev_map_t = std::map; class USDRDevice: public RadioDevice { public: USDRDevice(InterfaceType iface, const struct trx_cfg *cfg); ~USDRDevice(); int open(); bool start(); bool stop(); enum TxWindowType getWindowType() { return TX_WINDOW_FIXED; } int readSamples(std::vector &buf, int len, bool *overrun, TIMESTAMP timestamp = 0xffffffff, bool *underrun = nullptr); int writeSamples(std::vector &bufs, int len, bool *underrun, TIMESTAMP timestamp = 0xffffffff); bool updateAlignment(TIMESTAMP timestamp) { return true; } bool setTxFreq(double wFreq, size_t chan = 0); bool setRxFreq(double wFreq, size_t chan = 0); TIMESTAMP initialWriteTimestamp(void); TIMESTAMP initialReadTimestamp(void) { return ts_initial; } double fullScaleInputValue() { return 32767.0 * 0.7071; } double fullScaleOutputValue() { return 32767.0; } double setRxGain(double dB, size_t chan = 0); double getRxGain(size_t chan = 0); double maxRxGain(void); double minRxGain(void); double rssiOffset(size_t chan); int getNominalTxPower(size_t chan = 0); bool setRxAntenna(const std::string &ant, size_t chan = 0); std::string getRxAntenna(size_t chan = 0); bool setTxAntenna(const std::string &ant, size_t chan = 0); std::string getTxAntenna(size_t chan = 0); bool requiresRadioAlign() { return false; } GSM::Time minLatency(); double getTxFreq(size_t chan = 0); double getRxFreq(size_t chan = 0); double getSampleRate() { return actualSampleRate; } double setPowerAttenuation(int atten, size_t chan); double getPowerAttenuation(size_t chan = 0); double maxTxGain(void); double minTxGain(void); protected: /* flag indicates USDR has started */ bool started = false; /* actual sample rate of the USDR device, which may differ from the configured sample rate due to hardware * limitations */ double actualSampleRate = 0; /* Device type */ enum usdr_dev_type devType = UNKNOWN_DEV; /* Pointer to device description based on the device type (it always points to a valid dev_desc) */ std::reference_wrapper devDesc; /* Cached TX/RX gains */ std::vector tx_gains, rx_gains; /* Cached TX/RX frequencies */ std::vector tx_freqs, rx_freqs; /* time correction for TX timestamp, in samples, to align it with RX timestamp, which is needed to achieve * correct timing of the transmitted signal. */ TIMESTAMP timeTxCorr = 0; /* Initial timestamp of the first received sample */ TIMESTAMP ts_initial = 0; /* Timestamp of the last received sample, used to detect RX underrun */ TIMESTAMP ts_last_recv = 0; /* USDR device handle */ pdm_dev_t dev = nullptr; /* DMS stream handles for Rx and Tx */ pusdr_dms_t usds_rx = nullptr; pusdr_dms_t usds_tx = nullptr; /* DMS stream info for Rx and Tx */ usdr_dms_nfo_t snfo_rx; usdr_dms_nfo_t snfo_tx; }; #endif // _USDR_DEVICE_H_