/***********************************************************************
 * Transmit thread
 **********************************************************************/
static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_freq, const double tx_wave_ampl)
{
    uhd::set_thread_priority_safe();

    // set max TX gain
    usrp->set_tx_gain(usrp->get_tx_gain_range().stop());

    //create a transmit streamer
    uhd::stream_args_t stream_args("fc32"); //complex floats
    uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);

    //setup variables and allocate buffer
    uhd::tx_metadata_t md;
    md.has_time_spec = false;
    std::vector<samp_type> buff(tx_stream->get_max_num_samps()*10);

    //values for the wave table lookup
    size_t index = 0;
    const double tx_rate = usrp->get_tx_rate();
    const size_t step = boost::math::iround(wave_table_len * tx_wave_freq / tx_rate);
    wave_table table(tx_wave_ampl);

    //fill buff and send until interrupted
    while (not boost::this_thread::interruption_requested())
    {
        for (size_t i = 0; i < buff.size(); i++)
            buff[i] = table(index += step);
        tx_stream->send(&buff.front(), buff.size(), md);
    }

    //send a mini EOB packet
    md.end_of_burst = true;
    tx_stream->send("", 0, md);
}
void uhd_device::init_gains()
{
	uhd::gain_range_t range;

	range = usrp_dev->get_tx_gain_range();
	tx_gain_min = range.start();
	tx_gain_max = range.stop();

	range = usrp_dev->get_rx_gain_range();
	rx_gain_min = range.start();
	rx_gain_max = range.stop();

	usrp_dev->set_tx_gain((tx_gain_min + tx_gain_max) / 2);
	usrp_dev->set_rx_gain((rx_gain_min + rx_gain_max) / 2);

	tx_gain = usrp_dev->get_tx_gain();
	rx_gain = usrp_dev->get_rx_gain();

	return;
}
void get_tx_parameters(uhd::usrp::multi_usrp::sptr usrp, size_t mboard, std::ostream & os)
{
	using namespace std;
	namespace radio = uhd::usrp;

	size_t nchan = 0;

	// CONFIGURATION SUB_DEVICE
	
	os << std::endl << "********** TX Sub Device ***********" << std::endl;
	// Get sub device specification
	os << std::endl << "-----> Get TX Subdevice" << std::endl;
	try
	{
	os << "TX Subdevice Specification: ";
	radio::subdev_spec_t tx_subdev = usrp->get_tx_subdev_spec(mboard);
	os << tx_subdev.to_pp_string() << endl;
	}
	catch(uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}
	
	// Number of tx channels
	os << std::endl << "-----> Get number of TX channels" << std::endl;
	try
	{
	size_t num_tx = usrp->get_tx_num_channels();
	os << "Number of TX channels: " ;
	os << num_tx << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}
	
	// TX Device Name
	os << std::endl << "-----> Get TX Subdevice Name" << std::endl;
	try
	{
	os << "TX Subdevice Name: ";
	string tx_name = usrp->get_tx_subdev_name(nchan);
	os << tx_name << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}
	
	// TX SAMPLE RATE
	
	os << std::endl << "********** TX Sample Rate ***********" << std::endl;
	
	// Get Current TX rate
	os << std::endl << "-----> Get TX Rate" << std::endl;
	try
	{
		os << "TX Rate: " ;
		double tx_rate = usrp->get_tx_rate(nchan);
		os << tx_rate << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}
	
	// Get list of TX rates
	os << std::endl << "-----> Get TX Rate List" << std::endl;
	try
	{
		os << "TX Rate List: " ;
		uhd::meta_range_t tx_rates = usrp->get_tx_rates(nchan);
		os << "Start: " << tx_rates.start() << "   Stop: " << tx_rates.stop() << "   Step: " << tx_rates.step() << endl;
		os << tx_rates.to_pp_string() << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}

	// TX FREQUENCIES
	
	os << std::endl << "********** TX Frequencies ***********" << std::endl;
	
	// Current TX frequency
	os << std::endl << "-----> Get TX Center Frequency" << std::endl;
	try
	{
		os << "TX Freq: ";
		double tx_freq = usrp->get_tx_freq(nchan);
		os << tx_freq << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}

	// TX Frequency Range
	os << std::endl << "-----> Get TX Center Frequency Range" << std::endl;
	try
	{
		os << "TX Frequency Range: ";
		uhd::freq_range_t tx_freq_range = usrp->get_tx_freq_range(nchan);
		os << "Start: " << tx_freq_range.start() << "   Stop: " << tx_freq_range.stop() << "   Step: " << tx_freq_range.step() << endl;
		os << tx_freq_range.to_pp_string() << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}
	
	// Front end TX frequency range
	os << std::endl << "-----> Get TX RF Front End Center Frequency Range" << std::endl;
	try
	{
		os << "TX Front End Frequency Range: ";
		uhd::freq_range_t tx_fe_freq_range = usrp->get_fe_tx_freq_range(nchan);
		os << "Start: " << tx_fe_freq_range.start() << "   Stop: " << tx_fe_freq_range.stop() << "   Step: " << tx_fe_freq_range.step() << endl;
		os << tx_fe_freq_range.to_pp_string() << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << " Exception occurred : " << e.code() << endl;
	}

	// TX GAIN

	os << std::endl << "********** TX Gain  ***********" << std::endl;
	
	// Total combined gain
	os << endl << "-----> Get TX Total Gain" << endl;
	os << "TX Total Gain: " ;	
	try
	{
		double tx_total_gain = usrp->get_tx_gain(nchan);
		os << tx_total_gain << endl;
	}
	catch(uhd::runtime_error &e)
	{
		os << "Exception code: " << e.code() << endl;
	}
	
	// List of all gain elements
	os << std::endl << "-----> Get TX gain names" << std::endl;
	std::vector<std::string> tx_gain_names = usrp->get_tx_gain_names(nchan);
	os << "Tx Gain Names: " << std::endl;
	for (int index =0; index < tx_gain_names.size(); index++)
	{
		// Name
		os << "\t" << tx_gain_names[index] << endl;
	}
	for (int index =0; index < tx_gain_names.size(); index++)
	{
		// Name
		os << "\t" << "Name: " << tx_gain_names[index] << "  Value: ";
		// Value
		try
		{
		double element_gain = usrp->get_tx_gain(tx_gain_names[index], nchan);
		os << element_gain << endl;
		}
		catch(uhd::runtime_error &e)
		{
			os << "Exception code while getting value: " << e.code() << endl;
		}
	}

	// Gain ranges for each of the gain elements
	os << std::endl << "-----> Get TX element gain ranges" << std::endl;
	for (int index =0; index < tx_gain_names.size(); index++)
	{
		// Name
		os << "\t" << "Name: " << tx_gain_names[index] << "  Value: ";
		// Value
		try
		{
		uhd::gain_range_t element_gain_range = usrp->get_tx_gain_range(tx_gain_names[index], nchan);
		os << "Start: " << element_gain_range.start() << " End: " << element_gain_range.stop() << " Step: " << element_gain_range.step() << endl;
		}
		catch(uhd::runtime_error &e)
		{
			os << "Exception code while getting value: " << e.code() << endl;
		}
	}

	// Total Gain range
	try
	{
		os << endl << "-----> Get TX Total Gain Range" << endl;
		uhd::gain_range_t tx_total_gain_range = usrp->get_tx_gain_range(nchan);
		os << "TX Total Gain Range: " ;
		os << "Start: " << tx_total_gain_range.start() << " End: " << tx_total_gain_range.stop() << " Step: " << tx_total_gain_range.step() << endl;
	}
	catch(uhd::runtime_error &e)
	{
		os << "Exception code: " << e.code() <<endl;
	}

	// ANTENNA FUNCTIONS
	
	os << std::endl << "********** TX ANTENNA ***********" << std::endl;
	// Current Tx Antenna
	os << std::endl << "-----> Get TX Antenna" << std::endl;
	try
	{
	os << "TX Antenna: " ;
	string tx_antenna = usrp->get_tx_antenna(nchan);
	os << tx_antenna << endl;
	}
	catch(uhd::runtime_error &e)
	{
		os << "Exception code: " << e.code() <<endl;
	}

	// TX Antenna choices
	os << std::endl << "-----> Get Tx Antenna List" << std::endl;
	try
	{
	os << "TX Antennas : " << std::endl;
	std::vector<std::string> tx_antennas = usrp->get_tx_antennas(nchan);
	for (int index =0; index < tx_antennas.size(); index++)
		os << "\t" << tx_antennas[index] << std::endl;
	}
	catch(uhd::runtime_error &e)
	{
		os << "Exception code: " << e.code() <<endl;
	}
		
	// TX BANDWIDTH FUNCTIONS

	os << std::endl << "********** TX BANDWIDTH ***********" << std::endl;
	// Current TX Bandwidth
	os << endl << "-----> Get TX Bandwidth" << endl;
	try
	{
		os << "TX Bandwidth " ;
		double tx_bandwidth = usrp->get_tx_bandwidth(nchan);
		os << tx_bandwidth << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << "Exception occured " << e.code() << endl;
	}
	
	// TX Bandwidth Range
	os << endl << "-----> Get TX Bandwidth Range" << endl;
	try
	{
		os << "TX Bandwidth Range: " ;
		uhd::gain_range_t tx_bandwidth_range = usrp->get_tx_bandwidth_range(nchan);
		os << "Start: " << tx_bandwidth_range .start() << " End: " << tx_bandwidth_range .stop() << " Step: " << tx_bandwidth_range .step() << endl;
	}
	catch(uhd::runtime_error &e)
	{
		os << "Exception code: " << e.code() <<endl;
	}
		
	// TX DBOARD INTERFACE OBJECT

	os << std::endl << "********** TX DBOARD INTERFACE ***********" << std::endl;
	// TX Dboard Interface
	os << endl << "-----> Get tx_dboard_iface()" << endl;
	try
	{
		os << "TX Dboard Interface " ;
		uhd::usrp::dboard_iface::sptr tx_dboard_iface = usrp->get_tx_dboard_iface(nchan);
		os << tx_dboard_iface << endl;
	}
	catch (uhd::runtime_error &e)
	{
		os << "Exception occured " << e.code() << endl;
	}
	
	// TX _SENSORS

	os << std::endl << "********** TX Sensors  ***********" << std::endl;

	// List of all available sensors
	os << std::endl << "-----> Get TX Sensors Name" << std::endl;
	std::vector<std::string> tx_sensor_names = usrp->get_tx_sensor_names(nchan);
	os << "Sensor Names: " << std::endl;
	for (int index =0; index < tx_sensor_names.size(); index++)
	{
		// Name
		os << "\t" << tx_sensor_names[index] << ":  ";
		// Value
		try
		{
			uhd::sensor_value_t tx_sensor_value = usrp->get_tx_sensor(tx_sensor_names[index], nchan);
			os << tx_sensor_value.to_pp_string()<< std::endl;
		}
		catch(uhd::runtime_error &e)
		{
			os << "Exception occured " << e.code() << endl;
		}
	}
	
}