#ifndef DEVICE_UHD_H
#define DEVICE_UHD_H

#include "device_uhd_struct.h"
#include <memory>
#include <iostream>
#include <thread>
#include <string>
#include <fstream>
#include <functional>
#include <vector>
#include <deque>

#include <mutex>


class Device_UHD
{
public:
    uhd::device_addr_t devAddr; //设备地址
    bool bFindedDevice;
    std::shared_ptr<std::thread> spThread_Recv[2];
    std::shared_ptr<std::thread> spThread_Send[2];
    uhd::property_tree::sptr pTree;
    int kd_centerfreq_sw;
    double fParamMin;
    double fParamMax;
    UHD_FFTWin fftWin;//窗类型
    UHD_FFTLen fftLen;//窗长度
    int fftPeriod;//频谱周期 1-1000，单位ms
    UHD_KDParam rxKDParam[2];//rx宽带通道参数
    UHD_KDParam txKDParam[2];//tx宽带通道参数
    int FilterCoef[256];//滤波器系数数组
public:
      std::string gps_info;
    int led_val;

public:
    //! Device type, used as a filter in make
    //enum device_filter_t { ANY, USRP, CLOCK };
    bool isDevOpened() const;
    uhd::device_addrs_t findDevices();
    bool openDevice(uhd::device_addr_t addr);
	//关闭设备
    void closeDevice();
	//获取设备信息
    string findDevInfos();
	//获取设备信息2
    string findDevInfos2();
	//获取设备树
    string getDevTree();
	//设置时钟源
    bool setDevClockSource(string sourceStr);
	//获取时钟源
    string getDevClockSource();
    bool setDevInClock(double clockFreq);
    void getDevInClockRange();
    double getDevInClock();
    size_t getDevRXChCount();
    size_t getDevTXChCount();
    bool setDevParam(string chStr, UHD_ParamType paramType, double paramValue);
    double getDevParam(string chStr, UHD_ParamType paramType);
    void getDevParamRange(string chStr, UHD_ParamType paramType, double& paramMin, double& paramMax);
    double getDevParamMin() const;
    double getDevParamMax() const;
    bool setAllAntenna();
    bool setDevDDCParam_Any(string chStr, unsigned int jcqOffset, unsigned int jcqData);
    bool getDevDDCParam_Any(string chStr, unsigned int jcqOffset, unsigned int& jcqData);
    bool setDevDataSwitch(string chStr, UHD_ParamType dataType, unsigned char ZDCh, bool bSwitch);
	bool getDevDataSwitch(string chStr, UHD_ParamType dataType, unsigned char ZDCh, bool& bSwitch);
	//采样率为0，表示该通道停止使用
    bool ischeck_head =true;
	int setDevZDParam(string chStr, unsigned char ZDCh, long long centerfreq, int bw, int sample);
	//采样率为0，表示该通道未使用
	bool getDevZDParam(string chStr, unsigned char ZDCh, long long& centerfreq, int& bw, int& sample);
	void getDevZDCenterRange(string chStr, long long& min, long long& max);
	void getDevZDBandWidthRange(string chStr, int& min, int& max);
	vector<int> getDevZDSampleFreqRange(string chStr);
	bool setDevFFTParam(UHD_FFTWin fftWin, UHD_FFTLen fftLen, int fftPeriod_ms);
	bool getDevFFTParam(UHD_FFTWin& fftWin, UHD_FFTLen& fftLen, int& fftPeriod_ms);

	bool bThread_Recv[2] = { 0, 0 };
	double fRecvSpeed[2] = { 0, 0 };
	void* pRecvBuf[2] = { 0, 0 };
	int iRecvBufSize[2] = { 0, 0 };
	int iRecvBufPosi[2] = { 0, 0 };
    double getDevRecvSpeed(int channel) const;
    bool isDevRecving(int channel) const;
    void startDevRecv(int channel);
    void stopDevRecv(int channel);
    double getDevSendSpeed(int channel) const;
    bool isDevSending(int channel) const;
    bool bThread_Send[2]= {0,0};
    double fSendSpeed[2] = {0,0};

    int send_chan_sw =0;
    int send_all_sw = 0;
    int rx_kd_ddc_sw = 0;

    void startDevSend(int channel ,unsigned long long send_cf, unsigned long long data_sf,char*filename,bool bwhile);
    void stopDevSend(int channel);
    //uhd::usrp::multi_usrp::sptr pUSRP1;
    uhd::usrp::multi_usrp::sptr p_usrp;
    std::function<void(int channel, void* pdata, int length)> callBackData;

    std::string  getGPSInfo();
    bool setLedEnable(int led_num,bool bEnable);
    bool getLedEnable(int led_num,bool &bEnable);



};
#endif // DEVICE_UHD_H
