Esempio n. 1
0
int main(int argc, char** argv)
{
	int opt;
	char path_file[PATH_FILE_MAX_LEN];
	char date_time[DATE_TIME_MAX_LEN];
	t_u64toa ascii_u64_data1;
	t_u64toa ascii_u64_data2;
	const char* path = NULL;
	int result;
	time_t rawtime;
	struct tm * timeinfo;
	uint32_t file_pos;
	int exit_code = EXIT_SUCCESS;
	struct timeval t_end;
	float time_diff;

	while( (opt = getopt(argc, argv, "wr:f:n:v:m:l:")) != EOF )
	{
		result = AIRSPY_SUCCESS;
		switch( opt ) 
		{
		case 'w':
			receive_wav = true;
			break;

		case 'r':
			receive = true;
			path = optarg;
			break;

		case 'f':
			freq = true;
			result = parse_u64(optarg, &freq_hz);
			break;

		case 'v':
			result = parse_u32(optarg, &vga_gain);
			break;

		case 'l':
			result = parse_u32(optarg, &lna_gain);
			break;

		case 'm':
			result = parse_u32(optarg, &mixer_gain);
			break;

		case 'n':
			limit_num_samples = true;
			result = parse_u64(optarg, &samples_to_xfer);
			bytes_to_xfer = samples_to_xfer * 2;
			break;

		default:
			printf("unknown argument '-%c %s'\n", opt, optarg);
			usage();
			return EXIT_FAILURE;
		}
		
		if( result != AIRSPY_SUCCESS ) {
			printf("argument error: '-%c %s' %s (%d)\n", opt, optarg, airspy_error_name(result), result);
			usage();
			return EXIT_FAILURE;
		}		
	}

	if (samples_to_xfer >= SAMPLES_TO_XFER_MAX) {
		printf("argument error: num_samples must be less than %s/%sMio\n",
				u64toa(SAMPLES_TO_XFER_MAX, &ascii_u64_data1), u64toa(SAMPLES_TO_XFER_MAX/(FREQ_ONE_MHZ), &ascii_u64_data2) );
		usage();
		return EXIT_FAILURE;
	}

	if( freq ) {
		if( (freq_hz >= FREQ_MAX_HZ) || (freq_hz < FREQ_MIN_HZ) )
		{
			printf("argument error: set_freq_hz shall be between [%s, %s[.\n", u64toa(FREQ_MIN_HZ, &ascii_u64_data1), u64toa(FREQ_MAX_HZ, &ascii_u64_data2));
			usage();
			return EXIT_FAILURE;
		}
	}else
	{
		/* Use default freq */
		freq_hz = DEFAULT_FREQ_HZ;
	}

	receiver_mode = RECEIVER_MODE_RX;

	if( receive_wav ) 
	{
		time (&rawtime);
		timeinfo = localtime (&rawtime);
		receiver_mode = RECEIVER_MODE_RX;
		/* File format AirSpy Year(2013), Month(11), Day(28), Hour Min Sec+Z, Freq kHz, IQ.wav */
		strftime(date_time, DATE_TIME_MAX_LEN, "%Y%m%d_%H%M%S", timeinfo);
		snprintf(path_file, PATH_FILE_MAX_LEN, "AirSpy_%sZ_%ukHz_IQ.wav", date_time, (uint32_t)(freq_hz/(1000ull)) );
		path = path_file;
		printf("Receive wav file: %s\n", path);
	}	

	if( path == NULL ) {
		printf("specify a path to a file to receive\n");
		usage();
		return EXIT_FAILURE;
	}
	
	if(vga_gain > MAX_VGA_GAIN) {
		printf("vga_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(mixer_gain > MAX_MIXER_GAIN) {
		printf("mixer_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(lna_gain > MAX_LNA_GAIN) {
		printf("lna_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	result = airspy_init();
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_init() failed: %s (%d)\n", airspy_error_name(result), result);
		usage();
		return EXIT_FAILURE;
	}

	result = airspy_open(&device);
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_open() failed: %s (%d)\n", airspy_error_name(result), result);
		usage();
		return EXIT_FAILURE;
	}
	
	result = airspy_set_sample_type(device, sample_type);
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_open() failed: %s (%d)\n", airspy_error_name(result), result);
		usage();
		return EXIT_FAILURE;
	}

	fd = fopen(path, "wb");

	if( fd == NULL ) {
		printf("Failed to open file: %s\n", path);
		return EXIT_FAILURE;
	}
	/* Change fd buffer to have bigger one to store or read data on/to HDD */
	result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE);
	if( result != 0 ) {
		printf("setvbuf() failed: %d\n", result);
		usage();
		return EXIT_FAILURE;
	}
	
	/* Write Wav header */
	if( receive_wav ) 
	{
		fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd);
	}
	
#ifdef _MSC_VER
	SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#else
	signal(SIGINT, &sigint_callback_handler);
	signal(SIGILL, &sigint_callback_handler);
	signal(SIGFPE, &sigint_callback_handler);
	signal(SIGSEGV, &sigint_callback_handler);
	signal(SIGTERM, &sigint_callback_handler);
	signal(SIGABRT, &sigint_callback_handler);
#endif

	printf("call airspy_set_vga_gain(%u)\n", vga_gain);
	result = airspy_set_vga_gain(device, vga_gain);
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_set_vga_gain() failed: %s (%d)\n", airspy_error_name(result), result);
		//usage();
		//return EXIT_FAILURE;
	}

	printf("call airspy_set_mixer_gain(%u)\n", mixer_gain);
	result = airspy_set_mixer_gain(device, mixer_gain);
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_set_mixer_gain() failed: %s (%d)\n", airspy_error_name(result), result);
		//usage();
		//return EXIT_FAILURE;
	}

	printf("call airspy_set_lna_gain(%u)\n", lna_gain);
	result = airspy_set_lna_gain(device, lna_gain);
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_set_lna_gain() failed: %s (%d)\n", airspy_error_name(result), result);
		//usage();
		//return EXIT_FAILURE;
	}

	result = airspy_start_rx(device, rx_callback, NULL);
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_start_rx() failed: %s (%d)\n", airspy_error_name(result), result);
		usage();
		return EXIT_FAILURE;
	}

	printf("call airspy_set_freq(%s Hz / %.03f MHz)\n", u64toa(freq_hz, &ascii_u64_data1),((double)freq_hz/(double)FREQ_ONE_MHZ) );
	result = airspy_set_freq(device, freq_hz);
	if( result != AIRSPY_SUCCESS ) {
		printf("airspy_set_freq() failed: %s (%d)\n", airspy_error_name(result), result);
		usage();
		return EXIT_FAILURE;
	}
	
	if( limit_num_samples ) {
		printf("samples_to_xfer %s/%sMio\n", u64toa(samples_to_xfer, &ascii_u64_data1), u64toa((samples_to_xfer/FREQ_ONE_MHZ), &ascii_u64_data2) );
	}
	
	gettimeofday(&t_start, NULL);
	gettimeofday(&time_start, NULL);

	printf("Stop with Ctrl-C\n");
	while( (airspy_is_streaming(device) == AIRSPY_TRUE) &&
			(do_exit == false) ) 
	{
		uint32_t byte_count_now;
		struct timeval time_now;
		float time_difference, rate;
		sleep(1);
		
		gettimeofday(&time_now, NULL);
		
		byte_count_now = byte_count;
		byte_count = 0;
		
		time_difference = TimevalDiff(&time_now, &time_start);
		rate = (float)byte_count_now / time_difference;
		printf("%4.1f MiB / %5.3f sec = %4.1f MiB/second\n",
				(byte_count_now / 1e6f), time_difference, (rate / 1e6f) );

		time_start = time_now;

		if (byte_count_now == 0) {
			exit_code = EXIT_FAILURE;
			printf("\nCouldn't transfer any bytes for one second.\n");
			break;
		}
	}
	
	result = airspy_is_streaming(device);	
	if (do_exit)
	{
		printf("\nUser cancel, exiting...\n");
	} else {
		printf("\nExiting... airspy_is_streaming() result: %s (%d)\n", airspy_error_name(result), result);
	}
	
	gettimeofday(&t_end, NULL);
	time_diff = TimevalDiff(&t_end, &t_start);
	printf("Total time: %5.5f s\n", time_diff);
	
	if(device != NULL)
	{
		result = airspy_stop_rx(device);
		if( result != AIRSPY_SUCCESS ) {
			printf("airspy_stop_rx() failed: %s (%d)\n", airspy_error_name(result), result);
		}else {
			printf("airspy_stop_rx() done\n");
		}

		result = airspy_close(device);
		if( result != AIRSPY_SUCCESS ) 
		{
			printf("airspy_close() failed: %s (%d)\n", airspy_error_name(result), result);
		}else {
			printf("airspy_close() done\n");
		}
		
		airspy_exit();
		printf("airspy_exit() done\n");
	}
		
	if(fd != NULL)
	{
		if( receive_wav ) 
		{
			/* Get size of file */
			file_pos = ftell(fd);
			/* Update Wav Header */
			wave_file_hdr.hdr.size = file_pos+8;
			wave_file_hdr.fmt_chunk.dwSamplesPerSec = (uint32_t)DEFAULT_SAMPLE_RATE_HZ;
			wave_file_hdr.fmt_chunk.dwAvgBytesPerSec = wave_file_hdr.fmt_chunk.dwSamplesPerSec*2;
			wave_file_hdr.data_chunk.chunkSize = file_pos - sizeof(t_wav_file_hdr);
			/* Overwrite header with updated data */
			rewind(fd);
			fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd);
		}	
		fclose(fd);
		fd = NULL;
		printf("fclose(fd) done\n");
	}
	printf("exit\n");
	return exit_code;
}
Esempio n. 2
0
bool AirspySource::configure(std::uint32_t changeFlags,
        int sampleRateIndex,
        uint32_t frequency,
        bool bias_ant,
        int lna_gain,
        int mix_gain,
        int vga_gain,
        bool lna_agc,
        bool mix_agc
)
{
    airspy_error rc;

    if (!m_dev) {
        return false;
    }

    if (changeFlags & 0x1)
    {
        m_frequency = frequency;

        rc = (airspy_error) airspy_set_freq(m_dev, m_frequency);

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set center frequency to " << m_frequency << " Hz";
            m_error = err_ostr.str();
            return false;
        }
    }

    if (changeFlags & 0x2)
    {
        rc = (airspy_error) airspy_set_samplerate(m_dev, static_cast<airspy_samplerate_t>(sampleRateIndex));

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set center sample rate to " << m_srates[sampleRateIndex] << " Hz";
            m_error = err_ostr.str();
            return false;
        }
        else
        {
            m_sampleRate = m_srates[sampleRateIndex];
        }
    }

    if (changeFlags & 0x4)
    {
        m_lnaGain = lna_gain;

        rc = (airspy_error) airspy_set_lna_gain(m_dev, m_lnaGain);

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set LNA gain to " << m_lnaGain << " dB";
            m_error = err_ostr.str();
            return false;
        }
    }

    if (changeFlags & 0x8)
    {
        m_mixGain = mix_gain;

        rc = (airspy_error) airspy_set_mixer_gain(m_dev, m_mixGain);

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set mixer gain to " << m_mixGain << " dB";
            m_error = err_ostr.str();
            return false;
        }
    }

    if (changeFlags & 0x10)
    {
        m_vgaGain = vga_gain;

        rc = (airspy_error) airspy_set_vga_gain(m_dev, m_vgaGain);

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set VGA gain to " << m_vgaGain << " dB";
            m_error = err_ostr.str();
            return false;
        }
    }

    if (changeFlags & 0x20)
    {
        m_biasAnt = bias_ant;

        rc = (airspy_error) airspy_set_rf_bias(m_dev, (m_biasAnt ? 1 : 0));

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set bias antenna to " << m_biasAnt;
            m_error = err_ostr.str();
            return false;
        }
    }

    if (changeFlags & 0x40)
    {
        m_lnaAGC = lna_agc;

        rc = (airspy_error) airspy_set_lna_agc(m_dev, (m_lnaAGC ? 1 : 0));

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set LNA AGC to " << m_lnaAGC;
            m_error = err_ostr.str();
            return false;
        }
    }

    if (changeFlags & 0x80)
    {
        m_mixAGC = mix_agc;

        rc = (airspy_error) airspy_set_mixer_agc(m_dev, (m_mixAGC ? 1 : 0));

        if (rc != AIRSPY_SUCCESS)
        {
            std::ostringstream err_ostr;
            err_ostr << "Could not set mixer AGC to " << m_mixAGC;
            m_error = err_ostr.str();
            return false;
        }
    }

    return true;
}