Пример #1
0
void AirspySource::get_device_names(std::vector<std::string>& devices)
{
    airspy_device *airspy_ptr;
    airspy_read_partid_serialno_t read_partid_serialno;
    uint32_t serial_msb = 0;
    uint32_t serial_lsb = 0;
    airspy_error rc;
    int i;

    rc = (airspy_error) airspy_init();

    if (rc != AIRSPY_SUCCESS)
    {
        std::cerr << "AirspySource::get_device_names: Failed to open Airspy library: " << rc << ": " << airspy_error_name(rc) << std::endl;
        return;
    }

    for (i=0; i < AIRSPY_MAX_DEVICE; i++)
    {
        rc = (airspy_error) airspy_open(&airspy_ptr);
        std::cerr << "AirspySource::get_device_names: try to get device " << i << " serial number" << std::endl;

        if (rc == AIRSPY_SUCCESS)
        {
            std::cerr << "AirspySource::get_device_names: device " << i << " open OK" << std::endl;

            rc = (airspy_error) airspy_board_partid_serialno_read(airspy_ptr, &read_partid_serialno);

            if (rc == AIRSPY_SUCCESS)
            {
                serial_msb = read_partid_serialno.serial_no[2];
                serial_lsb = read_partid_serialno.serial_no[3];
                std::ostringstream devname_ostr;
                devname_ostr << "Serial " << std::hex << std::setw(8) << std::setfill('0') << serial_msb << serial_lsb;
                devices.push_back(devname_ostr.str());
            }
            else
            {
                std::cerr << "AirspySource::get_device_names: failed to get device " << i << " serial number: " << rc << ": " << airspy_error_name(rc) << std::endl;
            }

            airspy_close(airspy_ptr);
        }
        else
        {
            std::cerr << "AirspySource::get_device_names: enumerated " << i << " Airspy devices: " << airspy_error_name(rc) << std::endl;
            break; // finished
        }
    }

    rc = (airspy_error) airspy_exit();
    std::cerr << "AirspySource::get_device_names: Airspy library exit: " << rc << ": " << airspy_error_name(rc) << std::endl;
}
Пример #2
0
int main(int argc, char** argv)
{
	int i;
	uint32_t j;
	int result;
	int opt;
	uint32_t count;
	uint32_t *samplerates;
	uint32_t serial_number_msb_val;
	uint32_t serial_number_lsb_val;
	airspy_lib_version_t lib_version;
	uint8_t board_id = AIRSPY_BOARD_ID_INVALID;

	while( (opt = getopt(argc, argv, "s:")) != EOF )
	{
		result = AIRSPY_SUCCESS;
		switch( opt ) 
		{
		case 's':
			serial_number = true;
			result = parse_u64(optarg, &serial_number_val);
			serial_number_msb_val = (uint32_t)(serial_number_val >> 32);
			serial_number_lsb_val = (uint32_t)(serial_number_val & 0xFFFFFFFF);
			printf("Board serial number to open: 0x%08X%08X\n", serial_number_msb_val, serial_number_lsb_val);
			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;
		}		
	}

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

	airspy_lib_version(&lib_version);
	printf("airspy_lib_version: %d.%d.%d\n", 
					lib_version.major_version, lib_version.minor_version, lib_version.revision); 

	for (i = 0; i < AIRSPY_MAX_DEVICE; i++)
	{
		if(serial_number == true)
		{
			result = airspy_open_sn(&devices[i], serial_number_val);
		}else
		{
			result = airspy_open(&devices[i]);
		}
		if (result != AIRSPY_SUCCESS)
		{
			if(i == 0)
			{
				fprintf(stderr, "airspy_open() board %d failed: %s (%d)\n",
						i+1, airspy_error_name(result), result);
			}
			break;
		}
	}

	for(i = 0; i < AIRSPY_MAX_DEVICE; i++)
	{
		if(devices[i] != NULL)
		{
			printf("\nFound AirSpy board %d\n", i + 1);
			fflush(stdout);
			result = airspy_board_id_read(devices[i], &board_id);
			if (result != AIRSPY_SUCCESS) {
				fprintf(stderr, "airspy_board_id_read() failed: %s (%d)\n",
					airspy_error_name(result), result);
				continue;
			}
			printf("Board ID Number: %d (%s)\n", board_id,
				airspy_board_id_name(board_id));

			result = airspy_version_string_read(devices[i], &version[0], 255);
			if (result != AIRSPY_SUCCESS) {
				fprintf(stderr, "airspy_version_string_read() failed: %s (%d)\n",
					airspy_error_name(result), result);
				continue;
			}
			printf("Firmware Version: %s\n", version);

			result = airspy_board_partid_serialno_read(devices[i], &read_partid_serialno);
			if (result != AIRSPY_SUCCESS) {
				fprintf(stderr, "airspy_board_partid_serialno_read() failed: %s (%d)\n",
					airspy_error_name(result), result);
				continue;
			}
			printf("Part ID Number: 0x%08X 0x%08X\n",
				read_partid_serialno.part_id[0],
				read_partid_serialno.part_id[1]);
			printf("Serial Number: 0x%08X%08X\n",
				read_partid_serialno.serial_no[2],
				read_partid_serialno.serial_no[3]);

			printf("Supported sample rates:\n");
			airspy_get_samplerates(devices[i], &count, 0);
			samplerates = (uint32_t *) malloc(count * sizeof(uint32_t));
			airspy_get_samplerates(devices[i], samplerates, count);
			for (j = 0; j < count; j++)
			{
				printf("\t%f MSPS\n", samplerates[j] * 0.000001f);
			}
			free(samplerates);

			printf("Close board %d\n", i+1);
			result = airspy_close(devices[i]);
			if (result != AIRSPY_SUCCESS) {
				fprintf(stderr, "airspy_close() board %d failed: %s (%d)\n",
						i+1, airspy_error_name(result), result);
				continue;
			}
		}
	}

	airspy_exit();
	return EXIT_SUCCESS;
}
Пример #3
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;
}
Пример #4
0
int main(int argc, char** argv)
{
	struct airspy_device* device = NULL;
	int result = AIRSPY_SUCCESS;
	uint8_t board_id = AIRSPY_BOARD_ID_INVALID;

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

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

	printf("Found AirSpy board.\n");

	result = airspy_board_id_read(device, &board_id);
	if (result != AIRSPY_SUCCESS) {
		fprintf(stderr, "airspy_board_id_read() failed: %s (%d)\n",
				airspy_error_name(result), result);
		return EXIT_FAILURE;
	}
	printf("Board ID Number: %d (%s)\n", board_id,
			airspy_board_id_name(board_id));

	result = airspy_version_string_read(device, &version[0], 255);
	if (result != AIRSPY_SUCCESS) {
		fprintf(stderr, "airspy_version_string_read() failed: %s (%d)\n",
				airspy_error_name(result), result);
		return EXIT_FAILURE;
	}
	printf("Firmware Version: %s\n", version);

	result = airspy_board_partid_serialno_read(device, &read_partid_serialno);	
	if (result != AIRSPY_SUCCESS) {
		fprintf(stderr, "airspy_board_partid_serialno_read() failed: %s (%d)\n",
				airspy_error_name(result), result);
		return EXIT_FAILURE;
	}
	printf("Part ID Number: 0x%08X 0x%08X\n", 
				read_partid_serialno.part_id[0],
				read_partid_serialno.part_id[1]);
	printf("Serial Number: 0x%08X 0x%08X 0x%08X 0x%08X\n", 
				read_partid_serialno.serial_no[0],
				read_partid_serialno.serial_no[1],
				read_partid_serialno.serial_no[2],
				read_partid_serialno.serial_no[3]);
	
	result = airspy_close(device);
	if (result != AIRSPY_SUCCESS) {
		fprintf(stderr, "airspy_close() failed: %s (%d)\n",
				airspy_error_name(result), result);
		return EXIT_FAILURE;
	}

	airspy_exit();

	return EXIT_SUCCESS;
}
Пример #5
0
// Open Airspy device.
AirspySource::AirspySource(int dev_index) :
    m_dev(0),
    m_sampleRate(10000000),
    m_frequency(100000000),
	m_ppm(0),
    m_lnaGain(8),
    m_mixGain(8),
    m_vgaGain(0),
    m_biasAnt(false),
    m_lnaAGC(false),
    m_mixAGC(false),
    m_running(false),
    m_thread(0)
{
    airspy_error rc = (airspy_error) airspy_init();

    if (rc != AIRSPY_SUCCESS)
    {
        std::ostringstream err_ostr;
        err_ostr << "Failed to open Airspy library (" << rc << ": " << airspy_error_name(rc) << ")";
        m_error = err_ostr.str();
        m_dev = 0;
    }
    else
    {
        for (int i = 0; i < AIRSPY_MAX_DEVICE; i++)
        {
            rc = (airspy_error) airspy_open(&m_dev);

            if (rc == AIRSPY_SUCCESS)
            {
                if (i == dev_index)
                {
                    break;
                }
            }
            else
            {
                std::ostringstream err_ostr;
                err_ostr << "Failed to open Airspy device at sequence " << i;
                m_error = err_ostr.str();
                m_dev = 0;
            }
        }
    }

    if (m_dev)
    {
        uint32_t nbSampleRates;
        uint32_t *sampleRates;

        airspy_get_samplerates(m_dev, &nbSampleRates, 0);

        sampleRates = new uint32_t[nbSampleRates];

        airspy_get_samplerates(m_dev, sampleRates, nbSampleRates);

        if (nbSampleRates == 0)
        {
            m_error = "Failed to get Airspy device sample rate list";
            airspy_close(m_dev);
            m_dev = 0;
        }
        else
        {
            for (uint32_t i=0; i<nbSampleRates; i++)
            {
                m_srates.push_back(sampleRates[i]);
            }
        }

        delete[] sampleRates;

        std::ostringstream srates_ostr;

        for (int s: m_srates) {
            srates_ostr << s << " ";
        }

        m_sratesStr = srates_ostr.str();

        rc = (airspy_error) airspy_set_sample_type(m_dev, AIRSPY_SAMPLE_INT16_IQ);

        if (rc != AIRSPY_SUCCESS)
        {
            m_error = "AirspyInput::start: could not set sample type to INT16_IQ";
        }
    }

    std::ostringstream lgains_ostr;

    for (int g: m_lgains) {
        lgains_ostr << g << " ";
    }

    m_lgainsStr = lgains_ostr.str();

    std::ostringstream mgains_ostr;

    for (int g: m_mgains) {
        mgains_ostr << g << " ";
    }

    m_mgainsStr = mgains_ostr.str();

    std::ostringstream vgains_ostr;

    for (int g: m_vgains) {
        vgains_ostr << g << " ";
    }

    m_vgainsStr = vgains_ostr.str();

    std::ostringstream bwfilt_ostr;
    bwfilt_ostr << std::fixed << std::setprecision(2);

    m_this = this;
}