#ifndef UHD_DEVICE_H
#define UHD_DEVICE_H
#include <functional>
#include <vector>
#include <atomic>
#include <QThread>
#include <uhd.h>
#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:
	std::atomic<bool> stop_signal_called_rx;
	std::atomic<bool> stop_signal_called_tx;
	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_handle	usrp = 0;
	char error_string[512];
	int return_code = 0;
	int m_rx_channel = 0;
	int m_tx_channel = 0;
protected:
	uhd_io_thread * m_initThread;
	//初始化队列
	short	 		(*m_data_iq_rx)[2] = nullptr;
	size_t			m_rx_bufsz = 1024*1024*16;
	short	 		(*m_data_iq_tx)[2] = nullptr;
	size_t			m_tx_bufsz = 1024*1024*16;
	//收发计数
	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, const char * name);
	bool set_rx_agc(bool);
	double set_rx_bandwidth(double bw);
public:
	std::vector<std::string> get_rx_atns();
	std::vector<std::string> get_tx_atns();
	std::vector<std::string> get_rx_gain_names();
	std::vector<std::string> get_tx_gain_names();
public:
	quint64 set_tx_freq(const quint64 freq_in_hz);
	double set_tx_rate(const double sprate);
	double set_tx_gain(double, const char * name);
	double set_tx_bandwidth(double bw);
};

#endif // UHD_DEVICE_H
