#ifndef UHD_DEVICE_H
#define UHD_DEVICE_H
#include <uhd/types/tune_request.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/utils/thread.hpp>
#include <chrono>
#include <complex>
#include <csignal>
#include <fstream>
#include <iostream>
#include <functional>
#include <vector>
#include <atomic>
#include <QThread>
#include <string>
#include <QAtomicInteger>
class uhd_io_thread: public QThread{
	Q_OBJECT
public:
	explicit uhd_io_thread (QObject * p);
	explicit uhd_io_thread (std::function<void (void)> runner,QObject * p);
	void setRunner(std::function<void (void)> r){ m_runner = r;}
public:
	void run() override;
private:
	std::function<void (void)> m_runner;
};

class uhd_device{
public:
	uhd_device();
	uhd_device(const std::string &args);
	~uhd_device();
public:
	void setDevArgs(const std::string &args);
	std::string devArgs() const ;
public:
	bool open_device();
	bool close_device();
	void run_RxIO();
	void run_TxIO();
protected:
	uhd_io_thread * m_initThread;
	std::atomic<bool> stop_signal_called;
	std::atomic<bool> m_rx_streaming;
	std::atomic<bool> m_tx_streaming;
	std::string m_dev_args;
	size_t m_rxblock_size = 10000;
	size_t m_txblock_size = 10000;
	uhd::usrp::multi_usrp::sptr		usrp;
	size_t m_rx_channel = 0;
	size_t m_tx_channel = 0;
protected:
	//初始化队列
	short	 		(*m_data_iq_rx)[2] = nullptr;
	short	 		(*m_data_iq_tx)[2] = nullptr;
	size_t			m_rx_bufsz = 1024*1024;
	size_t			m_tx_bufsz = 1024*1024;
	//收发计数
	QAtomicInteger<unsigned long long>		m_rx_points = 0;
	QAtomicInteger<unsigned long long>		m_tx_points = 0;
public:
	quint64 set_rx_freq(const quint64 freq_in_hz);
	double set_rx_rate(const double sprate);
	void set_rx_atn(const std::string & ana);
	double set_rx_gain(double gain);
	double set_rx_bandwidth(double bw);
public:
	quint64 set_tx_freq(const quint64 freq_in_hz);
	double set_tx_rate(const double sprate);
	double set_tx_gain(double);
	double set_tx_bandwidth(double bw);
};

#endif // UHD_DEVICE_H
