Sergey Kostanbaev | 3e3507d | 2018-10-23 14:32:07 +0300 | [diff] [blame] | 1 | #ifndef _XTRX_DEVICE_H_ |
| 2 | #define _XTRX_DEVICE_H_ |
| 3 | |
| 4 | #ifdef HAVE_CONFIG_H |
| 5 | #include "config.h" |
| 6 | #endif |
| 7 | |
| 8 | #include "radioDevice.h" |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | #include <sys/time.h> |
| 12 | #include <string> |
| 13 | #include <iostream> |
| 14 | |
| 15 | #include "Threads.h" |
| 16 | #include <xtrx_api.h> |
| 17 | |
| 18 | class XTRXDevice: public RadioDevice { |
| 19 | private: |
| 20 | int txsps; |
| 21 | int rxsps; |
| 22 | double actualTXSampleRate; ///< the actual XTRX sampling rate |
| 23 | double actualRXSampleRate; ///< the actual XTRX sampling rate |
| 24 | //unsigned int decimRate; ///< the XTRX decimation rate |
| 25 | //unsigned int interRate; ///< the XTRX decimation rate |
| 26 | |
| 27 | unsigned long long samplesRead; ///< number of samples read from XTRX |
| 28 | unsigned long long samplesWritten; ///< number of samples sent to XTRX |
| 29 | |
| 30 | bool started; ///< flag indicates XTRX has started |
| 31 | |
| 32 | short *data; |
| 33 | unsigned long dataStart; |
| 34 | unsigned long dataEnd; |
| 35 | TIMESTAMP timeStart; |
| 36 | TIMESTAMP timeEnd; |
| 37 | |
| 38 | TIMESTAMP timeRx; |
| 39 | bool isAligned; |
| 40 | |
| 41 | Mutex writeLock; |
| 42 | |
| 43 | short *currData; ///< internal data buffer when reading from XTRX |
| 44 | TIMESTAMP currTimestamp; ///< timestamp of internal data buffer |
| 45 | unsigned currLen; ///< size of internal data buffer |
| 46 | |
| 47 | TIMESTAMP timestampOffset; ///< timestamp offset b/w Tx and Rx blocks |
| 48 | TIMESTAMP latestWriteTimestamp; ///< timestamp of most recent ping command |
| 49 | TIMESTAMP pingTimestamp; ///< timestamp of most recent ping response |
| 50 | |
| 51 | unsigned long hi32Timestamp; |
| 52 | unsigned long lastPktTimestamp; |
| 53 | |
| 54 | double rxGain; |
| 55 | double txGain; |
| 56 | bool loopback; |
| 57 | |
| 58 | xtrx_dev* device; |
| 59 | public: |
| 60 | |
| 61 | /** Object constructor */ |
| 62 | XTRXDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, size_t chans, double lo_offset, |
| 63 | const std::vector<std::string>& tx_paths, |
| 64 | const std::vector<std::string>& rx_paths); |
| 65 | |
| 66 | ~XTRXDevice(); |
| 67 | |
| 68 | /** Instantiate the XTRX */ |
| 69 | int open(const std::string &args, int ref, bool swap_channels); |
| 70 | |
| 71 | /** Start the XTRX */ |
| 72 | bool start(); |
| 73 | |
| 74 | /** Stop the XTRX */ |
| 75 | bool stop(); |
| 76 | |
| 77 | /** Set priority not supported */ |
| 78 | void setPriority(float prio = 0.5) { } |
| 79 | |
| 80 | enum TxWindowType getWindowType() { return TX_WINDOW_FIXED; } |
| 81 | |
| 82 | /** |
| 83 | Read samples from the XTRX. |
| 84 | @param buf preallocated buf to contain read result |
| 85 | @param len number of samples desired |
| 86 | @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough |
| 87 | @param timestamp The timestamp of the first samples to be read |
| 88 | @param underrun Set if XTRX does not have data to transmit, e.g. data not being sent fast enough |
| 89 | @param RSSI The received signal strength of the read result |
| 90 | @return The number of samples actually read |
| 91 | */ |
| 92 | int readSamples(std::vector<short *> &buf, int len, bool *overrun, |
| 93 | TIMESTAMP timestamp = 0xffffffff, bool *underrun = NULL, |
| 94 | unsigned *RSSI = NULL); |
| 95 | /** |
| 96 | Write samples to the XTRX. |
| 97 | @param buf Contains the data to be written. |
| 98 | @param len number of samples to write. |
| 99 | @param underrun Set if XTRX does not have data to transmit, e.g. data not being sent fast enough |
| 100 | @param timestamp The timestamp of the first sample of the data buffer. |
| 101 | @param isControl Set if data is a control packet, e.g. a ping command |
| 102 | @return The number of samples actually written |
| 103 | */ |
| 104 | int writeSamples(std::vector<short *> &bufs, int len, bool *underrun, |
| 105 | TIMESTAMP timestamp = 0xffffffff, bool isControl = false); |
| 106 | |
| 107 | /** Update the alignment between the read and write timestamps */ |
| 108 | bool updateAlignment(TIMESTAMP timestamp); |
| 109 | |
| 110 | /** Set the transmitter frequency */ |
| 111 | bool setTxFreq(double wFreq, size_t chan = 0); |
| 112 | |
| 113 | /** Set the receiver frequency */ |
| 114 | bool setRxFreq(double wFreq, size_t chan = 0); |
| 115 | |
| 116 | /** Returns the starting write Timestamp*/ |
| 117 | TIMESTAMP initialWriteTimestamp(void); |
| 118 | |
| 119 | /** Returns the starting read Timestamp*/ |
| 120 | TIMESTAMP initialReadTimestamp(void) { return 20000;} |
| 121 | |
| 122 | /** returns the full-scale transmit amplitude **/ |
| 123 | double fullScaleInputValue() {return (double) 32767*0.7;} |
| 124 | |
| 125 | /** returns the full-scale receive amplitude **/ |
| 126 | double fullScaleOutputValue() {return (double) 32767;} |
| 127 | |
| 128 | /** sets the receive chan gain, returns the gain setting **/ |
| 129 | double setRxGain(double dB, size_t chan = 0); |
| 130 | |
| 131 | /** get the current receive gain */ |
| 132 | double getRxGain(size_t chan = 0) { return rxGain; } |
| 133 | |
| 134 | /** return maximum Rx Gain **/ |
| 135 | double maxRxGain(void); |
| 136 | |
| 137 | /** return minimum Rx Gain **/ |
| 138 | double minRxGain(void); |
| 139 | |
| 140 | /** sets the transmit chan gain, returns the gain setting **/ |
| 141 | double setTxGain(double dB, size_t chan = 0); |
| 142 | |
| 143 | /** gets the current transmit gain **/ |
| 144 | double getTxGain(size_t chan = 0) { return txGain; } |
| 145 | |
| 146 | /** return maximum Tx Gain **/ |
| 147 | double maxTxGain(void); |
| 148 | |
| 149 | /** return minimum Rx Gain **/ |
| 150 | double minTxGain(void); |
| 151 | |
| 152 | /** sets the RX path to use, returns true if successful and false otherwise */ |
| 153 | bool setRxAntenna(const std::string & ant, size_t chan = 0); |
| 154 | |
| 155 | /** return the used RX path */ |
| 156 | std::string getRxAntenna(size_t chan = 0); |
| 157 | |
| 158 | /** sets the RX path to use, returns true if successful and false otherwise */ |
| 159 | bool setTxAntenna(const std::string & ant, size_t chan = 0); |
| 160 | |
| 161 | /** return the used RX path */ |
| 162 | std::string getTxAntenna(size_t chan = 0); |
| 163 | |
| 164 | /** return whether user drives synchronization of Tx/Rx of USRP */ |
| 165 | bool requiresRadioAlign(); |
| 166 | |
| 167 | /** return whether user drives synchronization of Tx/Rx of USRP */ |
| 168 | virtual GSM::Time minLatency(); |
| 169 | |
| 170 | /** Return internal status values */ |
| 171 | inline double getTxFreq(size_t chan = 0) { return 0; } |
| 172 | inline double getRxFreq(size_t chan = 0) { return 0; } |
| 173 | inline double getSampleRate() { return actualTXSampleRate; } |
| 174 | inline double numberRead() { return samplesRead; } |
| 175 | inline double numberWritten() { return samplesWritten; } |
| 176 | |
| 177 | }; |
| 178 | |
| 179 | #endif // _XTRX_DEVICE_H_ |
| 180 | |