コード例 #1
1
ファイル: hackrf_rxtx.c プロジェクト: glenoverby/hackrf
int main(int argc, char** argv) {
	int opt;
	char path_file[PATH_FILE_MAX_LEN];
	char date_time[DATE_TIME_MAX_LEN];
	const char* rxpath = NULL;
	const char* txpath = NULL;
	int result;
	time_t rawtime;
	struct tm * timeinfo;
	long int file_pos;
	int exit_code = EXIT_SUCCESS;
	struct timeval t_end;
	float time_diff;
	unsigned int lna_gain=8, vga_gain=20, txvga_gain=0;
	int udpport = 8192;
  
	while( (opt = getopt(argc, argv, "wr:t:f:i:o:m:a:p:s:n:b:l:g:x:c:u:")) != EOF )
	{
		result = HACKRF_SUCCESS;
		switch( opt ) 
		{
		case 'w':
			receive_wav = true;
			break;
		
		case 'r':
			receive = true;
			rxpath = optarg;
			break;
		
		case 't':
			transmit = true;
			txpath = optarg;
			break;

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

		case 'i':
			if_freq = true;
			result = parse_u64(optarg, &if_freq_hz);
			break;

		case 'o':
			lo_freq = true;
			result = parse_u64(optarg, &lo_freq_hz);
			break;

		case 'm':
			image_reject = true;
			result = parse_u32(optarg, &image_reject_selection);
			break;

		case 'a':
			amp = true;
			result = parse_u32(optarg, &amp_enable);
			break;

		case 'p':
			antenna = true;
			result = parse_u32(optarg, &antenna_enable);
			break;

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

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

		case 'x':
			result = parse_u32(optarg, &txvga_gain);
			break;

		case 's':
			sample_rate = true;
			result = parse_u32(optarg, &sample_rate_hz);
			break;

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

		case 'b':
			baseband_filter_bw = true;
			result = parse_u32(optarg, &baseband_filter_bw_hz);
			break;

		case 'c':
			transmit = true;
			signalsource = true;
			result = parse_u32(optarg, &amplitude);
			break;

		case 'u':
			udpport = atoi(optarg);
			break;

		default:
			printf("unknown argument '-%c %s'\n", opt, optarg);
			usage();
			return EXIT_FAILURE;
		}
		
		if( result != HACKRF_SUCCESS ) {
			printf("argument error: '-%c %s' %s (%d)\n", opt, optarg, hackrf_error_name(result), result);
			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));
		return EXIT_FAILURE;
	}

	if (if_freq || lo_freq || image_reject) {
		/* explicit tuning selected */
		if (!if_freq) {
			printf("argument error: if_freq_hz must be specified for explicit tuning.\n");
			return EXIT_FAILURE;
		}
		if (!image_reject) {
			printf("argument error: image_reject must be specified for explicit tuning.\n");
			return EXIT_FAILURE;
		}
		if (!lo_freq && (image_reject_selection != RF_PATH_FILTER_BYPASS)) {
			printf("argument error: lo_freq_hz must be specified for explicit tuning unless image_reject is set to bypass.\n");
			return EXIT_FAILURE;
		}
		if ((if_freq_hz > IF_MAX_HZ) || (if_freq_hz < IF_MIN_HZ)) {
			printf("argument error: if_freq_hz shall be between %s and %s.\n",
				u64toa(IF_MIN_HZ,&ascii_u64_data1),
				u64toa(IF_MAX_HZ,&ascii_u64_data2));
			return EXIT_FAILURE;
		}
		if ((lo_freq_hz > LO_MAX_HZ) || (lo_freq_hz < LO_MIN_HZ)) {
			printf("argument error: lo_freq_hz shall be between %s and %s.\n",
				u64toa(LO_MIN_HZ,&ascii_u64_data1),
				u64toa(LO_MAX_HZ,&ascii_u64_data2));
			return EXIT_FAILURE;
		}
		if (image_reject_selection > 2) {
			printf("argument error: image_reject must be 0, 1, or 2 .\n");
			return EXIT_FAILURE;
		}
		if (automatic_tuning) {
			printf("warning: freq_hz ignored by explicit tuning selection.\n");
			automatic_tuning = false;
		}
		switch (image_reject_selection) {
		case RF_PATH_FILTER_BYPASS:
			freq_hz = if_freq_hz;
			break;
		case RF_PATH_FILTER_LOW_PASS:
			freq_hz = abs(if_freq_hz - lo_freq_hz);
			break;
		case RF_PATH_FILTER_HIGH_PASS:
			freq_hz = if_freq_hz + lo_freq_hz;
			break;
		default:
			freq_hz = DEFAULT_FREQ_HZ;
			break;
		}
		printf("explicit tuning specified for %s Hz.\n",
			u64toa(freq_hz,&ascii_u64_data1));

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

	if( amp ) {
		if( amp_enable > 1 )
		{
			printf("argument error: amp_enable shall be 0 or 1.\n");
			return EXIT_FAILURE;
		}
	}

	if (antenna) {
		if (antenna_enable > 1) {
			printf("argument error: antenna_enable shall be 0 or 1.\n");
			return EXIT_FAILURE;
		}
	}

	if( sample_rate == false ) 
	{
		sample_rate_hz = DEFAULT_SAMPLE_RATE_HZ;
	}

	if( baseband_filter_bw )
	{
		/* Compute nearest freq for bw filter */
		baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw(baseband_filter_bw_hz);
	}else
	{
		/* Compute default value depending on sample rate */
		baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw_round_down_lt(sample_rate_hz);
	}

	if (baseband_filter_bw_hz > BASEBAND_FILTER_BW_MAX) {
		printf("argument error: baseband_filter_bw_hz must be less or equal to %u Hz/%.03f MHz\n",
				BASEBAND_FILTER_BW_MAX, (float)(BASEBAND_FILTER_BW_MAX/FREQ_ONE_MHZ));
		return EXIT_FAILURE;
	}

	if (baseband_filter_bw_hz < BASEBAND_FILTER_BW_MIN) {
		printf("argument error: baseband_filter_bw_hz must be greater or equal to %u Hz/%.03f MHz\n",
				BASEBAND_FILTER_BW_MIN, (float)(BASEBAND_FILTER_BW_MIN/FREQ_ONE_MHZ));
		return EXIT_FAILURE;
	}

	if( receive ) {
		transceiver_mode = TRANSCEIVER_MODE_RX;
	} else if( transmit ) {
		transceiver_mode = TRANSCEIVER_MODE_TX;
	}

	if (signalsource) {
		transceiver_mode = TRANSCEIVER_MODE_SS;
		if (amplitude >127) {
			printf("argument error: amplitude shall be in between 0 and 128.\n");
			return EXIT_FAILURE;
		}
	}

	if( receive_wav )
	{
		time (&rawtime);
		timeinfo = localtime (&rawtime);
		transceiver_mode = TRANSCEIVER_MODE_RX;
		/* File format HackRF 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, "HackRF_%sZ_%ukHz_IQ.wav", date_time, (uint32_t)(freq_hz/(1000ull)) );
		rxpath = path_file;
		printf("Receive wav file: %s\n", rxpath);
	}	

	// In signal source mode, the PATH argument is neglected.
	if (transceiver_mode != TRANSCEIVER_MODE_SS) {
		if( rxpath == NULL && txpath == NULL) {
			printf("specify a path to a file to transmit/receive\n");
			return EXIT_FAILURE;
		}
	}

	result = hackrf_init();
	if( result != HACKRF_SUCCESS ) {
		printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
		return EXIT_FAILURE;
	}
	
	result = hackrf_open(&device);
	if( result != HACKRF_SUCCESS ) {
		printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
		return EXIT_FAILURE;
	}
	
	if (transceiver_mode != TRANSCEIVER_MODE_SS) {
		if( rxpath != NULL )
		{
			rxfd = fopen(rxpath, "wb");
			if( rxfd == NULL ) {
				printf("Failed to open file: %s\n", rxpath);
				return EXIT_FAILURE;
			}
			/* Change fd buffer to have bigger one to store or read data on/to HDD */
			setvbuf(rxfd , NULL , _IOFBF , FD_BUFFER_SIZE);
		}
		if( txpath != NULL )
		{
			txfd = fopen(txpath, "rb");
			if( txfd == NULL ) {
				printf("Failed to open file: %s\n", txpath);
				return EXIT_FAILURE;
			}
			/* Change fd buffer to have bigger one to store or read data on/to HDD */
			setvbuf(txfd , NULL , _IOFBF , FD_BUFFER_SIZE);
		}
	}

	/* Write Wav header */
	if( receive_wav ) 
	{
		fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), rxfd);
	}
	
#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 hackrf_sample_rate_set(%u Hz/%.03f MHz)\n", sample_rate_hz,((float)sample_rate_hz/(float)FREQ_ONE_MHZ));
	result = hackrf_set_sample_rate_manual(device, sample_rate_hz, 1);
	if( result != HACKRF_SUCCESS ) {
		printf("hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result);
		return EXIT_FAILURE;
	}

	printf("call hackrf_baseband_filter_bandwidth_set(%d Hz/%.03f MHz)\n",
			baseband_filter_bw_hz, ((float)baseband_filter_bw_hz/(float)FREQ_ONE_MHZ));
	result = hackrf_set_baseband_filter_bandwidth(device, baseband_filter_bw_hz);
	if( result != HACKRF_SUCCESS ) {
		printf("hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", hackrf_error_name(result), result);
		return EXIT_FAILURE;
	}

	result = hackrf_set_vga_gain(device, vga_gain);
	result |= hackrf_set_lna_gain(device, lna_gain);
	result |= hackrf_set_txvga_gain(device, txvga_gain);
	if (rxfd != NULL) {
		result |= hackrf_start_rx(device, rx_callback, NULL);
	} else {
		result |= hackrf_start_tx(device, tx_callback, NULL);
	}

#if 0
	if( transceiver_mode == TRANSCEIVER_MODE_RX ) {
		result |= hackrf_start_rx(device, rx_callback, NULL);
	} else {
		result |= hackrf_start_tx(device, tx_callback, NULL);
	}
#endif

	if( result != HACKRF_SUCCESS ) {
		printf("hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result);
		return EXIT_FAILURE;
	}

	if (automatic_tuning) {
		printf("call hackrf_set_freq(%s Hz/%.03f MHz)\n",
			u64toa(freq_hz, &ascii_u64_data1),((double)freq_hz/(double)FREQ_ONE_MHZ) );
		result = hackrf_set_freq(device, freq_hz);
		if( result != HACKRF_SUCCESS ) {
			printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result);
			return EXIT_FAILURE;
		}
	} else {
		printf("call hackrf_set_freq_explicit() with %s Hz IF, %s Hz LO, %s\n",
				u64toa(if_freq_hz,&ascii_u64_data1),
				u64toa(lo_freq_hz,&ascii_u64_data2),
				hackrf_filter_path_name(image_reject_selection));
		result = hackrf_set_freq_explicit(device, if_freq_hz, lo_freq_hz,
				image_reject_selection);
		if (result != HACKRF_SUCCESS) {
			printf("hackrf_set_freq_explicit() failed: %s (%d)\n",
					hackrf_error_name(result), result);
			return EXIT_FAILURE;
		}
	}

	if( amp ) {
		printf("call hackrf_set_amp_enable(%u)\n", amp_enable);
		result = hackrf_set_amp_enable(device, (uint8_t)amp_enable);
		if( result != HACKRF_SUCCESS ) {
			printf("hackrf_set_amp_enable() failed: %s (%d)\n", hackrf_error_name(result), result);
			return EXIT_FAILURE;
		}
	}

	if (antenna) {
		printf("call hackrf_set_antenna_enable(%u)\n", antenna_enable);
		result = hackrf_set_antenna_enable(device, (uint8_t)antenna_enable);
		if (result != HACKRF_SUCCESS) {
			printf("hackrf_set_antenna_enable() failed: %s (%d)\n", hackrf_error_name(result), result);
			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(  /*(hackrf_is_streaming(device) == HACKRF_TRUE) && */
			(request_exit == false) ) 
	{
#if 0
		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;
		}
#endif
		printf("hackrf_is%s_streaming\n", 
			hackrf_is_streaming(device)==HACKRF_TRUE ? "":"_not");
		usbsoftrock(udpport);
	}
	
	result = hackrf_is_streaming(device);	
	if (request_exit)
	{
		printf("\nUser cancel, exiting...\n");
	} else {
		printf("\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result);
	}
	do_exit = true;
	
	gettimeofday(&t_end, NULL);
	time_diff = TimevalDiff(&t_end, &t_start);
	printf("Total time: %5.5f s\n", time_diff);
	
	if(device != NULL)
	{
		if( receive ) 
		{
			result = hackrf_stop_rx(device);
			if( result != HACKRF_SUCCESS ) {
				printf("hackrf_stop_rx() failed: %s (%d)\n", hackrf_error_name(result), result);
			}else {
				printf("hackrf_stop_rx() done\n");
			}
		}
	
		if( transmit ) 
		{
			result = hackrf_stop_tx(device);
			if( result != HACKRF_SUCCESS ) {
				printf("hackrf_stop_tx() failed: %s (%d)\n", hackrf_error_name(result), result);
			}else {
				printf("hackrf_stop_tx() done\n");
			}
		}
		
		result = hackrf_close(device);
		if( result != HACKRF_SUCCESS ) 
		{
			printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
		}else {
			printf("hackrf_close() done\n");
		}
		
		hackrf_exit();
		printf("hackrf_exit() done\n");
	}
		
	if(rxfd != NULL)
	{
		if( receive_wav ) 
		{
			/* Get size of file */
			file_pos = ftell(rxfd);
			/* Update Wav Header */
			wave_file_hdr.hdr.size = file_pos+8;
			wave_file_hdr.fmt_chunk.dwSamplesPerSec = 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(rxfd);
			fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), rxfd);
		}	
		fclose(rxfd);
		rxfd = NULL;
		printf("fclose(rxfd) done\n");
	}
	printf("exit\n");
	return exit_code;
}
コード例 #2
0
ファイル: ch-meas.c プロジェクト: cpavlina/200a
//# CALibrate1:OFFSET?  ident(0x10)
//# CALibrate1:SCALE2?  ident(0x11)
//# CALibrate1:SCALE10? ident(0x12)
//# CALibrate2:OFFSET?  ident(0x20)
//# CALibrate2:SCALE2?  ident(0x21)
//# CALibrate2:SCALE10? ident(0x22)
void ch_calN_P (bool arg_valid, uint8_t ident)
{
    (void) arg_valid;
    int64_t response;

    if (ident == 0x10) {
        response = INST_1.offset;
    } else if (ident == 0x11) {
        response = INST_1.scale_2x;
    } else if (ident == 0x12) {
        response = INST_1.scale_10x;
    } else if (ident == 0x20) {
        response = INST_2.offset;
    } else if (ident == 0x21) {
        response = INST_2.scale_2x;
    } else if (ident == 0x22) {
        response = INST_2.scale_10x;
    } else {
        sassert (0);
        return;
    }

    char buffer[21];
    u64toa ((uint64_t) response, buffer);
    fputs (buffer, user_stdout);
    fputc ('\n', user_stdout);
}
コード例 #3
0
ファイル: String.cpp プロジェクト: FlyingSwan999/Vlpp
	vuint64_t atou64_test(const AString& string, bool& success)
	{
		char* endptr = 0;
		vuint64_t result = _strtoui64(string.Buffer(), &endptr, 10);
		success = endptr == string.Buffer() + string.Length() && u64toa(result) == string;
		return result;
	}
コード例 #4
0
ファイル: hackrf_rxtx.c プロジェクト: glenoverby/hackrf
static void usage() {
	printf("Usage:\n");
	printf("\t-r <filename> # Receive data into file.\n");
	printf("\t-t <filename> # Transmit data from file.\n");
	printf("\t-w # Receive data into file with WAV header and automatic name.\n");
	printf("\t   # This is for SDR# compatibility and may not work with other software.\n");
	printf("\t[-f freq_hz] # Frequency in Hz [%sMHz to %sMHz].\n",
		u64toa((FREQ_MIN_HZ/FREQ_ONE_MHZ),&ascii_u64_data1),
		u64toa((FREQ_MAX_HZ/FREQ_ONE_MHZ),&ascii_u64_data2));
	printf("\t[-i if_freq_hz] # Intermediate Frequency (IF) in Hz [%sMHz to %sMHz].\n",
		u64toa((IF_MIN_HZ/FREQ_ONE_MHZ),&ascii_u64_data1),
		u64toa((IF_MAX_HZ/FREQ_ONE_MHZ),&ascii_u64_data2));
	printf("\t[-o lo_freq_hz] # Front-end Local Oscillator (LO) frequency in Hz [%sMHz to %sMHz].\n",
		u64toa((LO_MIN_HZ/FREQ_ONE_MHZ),&ascii_u64_data1),
		u64toa((LO_MAX_HZ/FREQ_ONE_MHZ),&ascii_u64_data2));
	printf("\t[-m image_reject] # Image rejection filter selection, 0=bypass, 1=low pass, 2=high pass.\n");
	printf("\t[-a amp_enable] # RX/TX RF amplifier 1=Enable, 0=Disable.\n");
	printf("\t[-p antenna_enable] # Antenna port power, 1=Enable, 0=Disable.\n");
	printf("\t[-l gain_db] # RX LNA (IF) gain, 0-40dB, 8dB steps\n");
	printf("\t[-g gain_db] # RX VGA (baseband) gain, 0-62dB, 2dB steps\n");
	printf("\t[-x gain_db] # TX VGA (IF) gain, 0-47dB, 1dB steps\n");
	printf("\t[-s sample_rate_hz] # Sample rate in Hz (8/10/12.5/16/20MHz, default %sMHz).\n",
		u64toa((DEFAULT_SAMPLE_RATE_HZ/FREQ_ONE_MHZ),&ascii_u64_data1));
	printf("\t[-n num_samples] # Number of samples to transfer (default is unlimited).\n");
	printf("\t[-c amplitude] # CW signal source mode, amplitude 0-127 (DC value to DAC).\n");
	printf("\t[-b baseband_filter_bw_hz] # Set baseband filter bandwidth in MHz.\n\tPossible values: 1.75/2.5/3.5/5/5.5/6/7/8/9/10/12/14/15/20/24/28MHz, default < sample_rate_hz.\n" );
}
コード例 #5
0
ファイル: loa_main.c プロジェクト: MayKeoN/loa
void			curse_mask_tab(int *pl, register unsigned long int acc, register const unsigned long int upper_lim, register const unsigned long int lower_lim)
{
	if (acc > lower_lim)
	{
		// 1499999948900
		if (acc < upper_lim)
		{
			// 1500000051100
			// printf("%ld\n", acc);
			// long int finalgap = (acc - lower_lim);
			// printf("FINAL GAP: %ld\n", finalgap);
			char *numsave = u64toa(acc - lower_lim);
			write(fdout, numsave, strlen(numsave));
			// printf("%ld:%d\t", acc, *pl);
			// long int finalgap = (acc - (upper_lim+lower_lim)/2);
			// printf("FINAL GAP: %ld\n", finalgap);
			// int i = 0;
			// while (mingap[i])
				// i++;
			// if (finalgap < mingap)
			{
				// mingap[i] = finalgap;
				// printf("%p\n", mingap);
				// printf("I: %d\tmin: %ld\tfin:%ld\n", i, mingap[i], finalgap);
			}
			// exit(0);
			return;
		}
		return;
	}
	if (*(pl + 1))
	{
		// printf("%d->", pl[1]);		
		curse_mask_tab(pl + 1, acc * *(pl + 1), upper_lim, lower_lim);
		// if (*pl < 6)
		// 	fork();

		// printf("%d->", 1);
		curse_mask_tab(pl + 1, acc, upper_lim, lower_lim);
	}
	// printf("\n");
	// if (*(pl + 1))
	{
		// printf("%d->", 1);
	}
	// printf("\n");
	return;
	// exit(0);
}
コード例 #6
0
ファイル: airspy_rx.c プロジェクト: alisterwan/BadIMSIRadio
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;
	struct timeval t_end;
	float time_diff;
	uint32_t file_pos;
	int exit_code = EXIT_SUCCESS;

	uint32_t count;
	uint32_t packing_val_u32;
	uint32_t *supported_samplerates;
	uint32_t sample_rate_u32;
	uint32_t sample_type_u32;
	double freq_hz_temp;
	char str[20];

	while( (opt = getopt(argc, argv, "r:ws:p:f:a:t:b:v:m:l:g:h:n:d")) != EOF )
	{
		result = AIRSPY_SUCCESS;
		switch( opt ) 
		{
			case 'r':
				receive = true;
				path = optarg;
			break;

			case 'w':
				receive_wav = true;
			 break;

			case 's':
				serial_number = true;
				result = parse_u64(optarg, &serial_number_val);
			break;

			case 'p': /* packing */
				result = parse_u32(optarg, &packing_val_u32);
				switch (packing_val_u32)
				{
					case 0:
					case 1:
						packing_val = packing_val_u32;
						call_set_packing = true;
					break;

					default:
						/* Invalid value will display error */
						packing_val = PACKING_MAX;
						call_set_packing = false;
					break;
				}
			break;

			case 'f':
				freq = true;
				freq_hz_temp = strtod(optarg, NULL) * (double)FREQ_ONE_MHZ;
				if(freq_hz_temp <= (double)FREQ_HZ_MAX)
					freq_hz = (uint32_t)freq_hz_temp;
				else
					freq_hz = UINT_MAX;
			break;

			case 'a': /* Sample rate */
				sample_rate = true;
				result = parse_u32(optarg, &sample_rate_u32);
			break;

			case 't': /* Sample type see also airspy_sample_type */
				result = parse_u32(optarg, &sample_type_u32);
				switch (sample_type_u32)
				{
					case 0:
						sample_type_val = AIRSPY_SAMPLE_FLOAT32_IQ;
						wav_format_tag = 3; /* Float32 */
						wav_nb_channels = 2;
						wav_nb_bits_per_sample = 32;
						wav_nb_byte_per_sample = (wav_nb_bits_per_sample / 8);
					break;

					case 1:
						sample_type_val = AIRSPY_SAMPLE_FLOAT32_REAL;
						wav_format_tag = 3; /* Float32 */
						wav_nb_channels = 1;
						wav_nb_bits_per_sample = 32;
						wav_nb_byte_per_sample = (wav_nb_bits_per_sample / 8);
					break;

					case 2:
						sample_type_val = AIRSPY_SAMPLE_INT16_IQ;
						wav_format_tag = 1; /* PCM8 or PCM16 */
						wav_nb_channels = 2;
						wav_nb_bits_per_sample = 16;
						wav_nb_byte_per_sample = (wav_nb_bits_per_sample / 8);
					break;

					case 3:
						sample_type_val = AIRSPY_SAMPLE_INT16_REAL;
						wav_format_tag = 1; /* PCM8 or PCM16 */
						wav_nb_channels = 1;
						wav_nb_bits_per_sample = 16;
						wav_nb_byte_per_sample = (wav_nb_bits_per_sample / 8);
					break;

					case 4:
						sample_type_val = AIRSPY_SAMPLE_UINT16_REAL;
						wav_format_tag = 1; /* PCM8 or PCM16 */
						wav_nb_channels = 1;
						wav_nb_bits_per_sample = 16;
						wav_nb_byte_per_sample = (wav_nb_bits_per_sample / 8);
					break;

					default:
						/* Invalid value will display error */
						sample_type_val = SAMPLE_TYPE_MAX+1;
					break;
				}
			break;

			case 'b':
				serial_number = true;
				result = parse_u32(optarg, &biast_val);
			break;

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

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

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

			case 'g':
				linearity_gain = true;
				result = parse_u32(optarg, &linearity_gain_val);		
			break;

			case 'h':
				sensitivity_gain = true;
				result = parse_u32(optarg, &sensitivity_gain_val);
			break;

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

			case 'd':
				verbose = true;
			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 (sample_rate)
	{
		sample_rate_val = sample_rate_u32;
	}

	bytes_to_xfer = samples_to_xfer * wav_nb_byte_per_sample * wav_nb_channels;

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

	if( freq ) {
		if( (freq_hz >= FREQ_HZ_MAX) || (freq_hz < FREQ_HZ_MIN) )
		{
			printf("argument error: frequency_MHz=%.6f MHz and shall be between [%lu, %lu[ MHz\n",
							((double)freq_hz/(double)FREQ_ONE_MHZ), FREQ_HZ_MIN/FREQ_ONE_MHZ, FREQ_HZ_MAX/FREQ_ONE_MHZ);
			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("error: you shall specify at least -r <with filename> or -w option\n");
		usage();
		return EXIT_FAILURE;
	}

	if(packing_val == PACKING_MAX) {
		printf("argument error: packing out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(sample_type_val > SAMPLE_TYPE_MAX) {
		printf("argument error: sample_type out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(biast_val > BIAST_MAX) {
		printf("argument error: biast_val out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(vga_gain > VGA_GAIN_MAX) {
		printf("argument error: vga_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(mixer_gain > MIXER_GAIN_MAX) {
		printf("argument error: mixer_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(lna_gain > LNA_GAIN_MAX) {
		printf("argument error: lna_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(linearity_gain_val > LINEARITY_GAIN_MAX) {
		printf("argument error: linearity_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if(sensitivity_gain_val > SENSITIVITY_GAIN_MAX) {
		printf("argument error: sensitivity_gain out of range\n");
		usage();
		return EXIT_FAILURE;
	}

	if( (linearity_gain == true) && (sensitivity_gain == true) )
	{
		printf("argument error: linearity_gain and sensitivity_gain are both set (choose only one option)\n");
		usage();
		return EXIT_FAILURE;
	}

	if(verbose == true)
	{
		uint32_t serial_number_msb_val;
		uint32_t serial_number_lsb_val;

		printf("airspy_rx v%s\n", AIRSPY_RX_VERSION);
		serial_number_msb_val = (uint32_t)(serial_number_val >> 32);
		serial_number_lsb_val = (uint32_t)(serial_number_val & 0xFFFFFFFF);
		if(serial_number)
			printf("serial_number_64bits -s 0x%08X%08X\n", serial_number_msb_val, serial_number_lsb_val);
		printf("packing -p %d\n", packing_val);
		printf("frequency_MHz -f %.6fMHz (%sHz)\n",((double)freq_hz/(double)FREQ_ONE_MHZ), u64toa(freq_hz, &ascii_u64_data1) );
		printf("sample_type -t %d\n", sample_type_val);
		printf("biast -b %d\n", biast_val);

		if( (linearity_gain == false) && (sensitivity_gain == false) )
		{
			printf("vga_gain -v %u\n", vga_gain);
			printf("mixer_gain -m %u\n", mixer_gain);
			printf("lna_gain -l %u\n", lna_gain);
		} else
		{
			if( linearity_gain == true)
			{
				printf("linearity_gain -g %u\n", linearity_gain_val);
			}

			if( sensitivity_gain == true)
			{
				printf("sensitivity_gain -h %u\n", sensitivity_gain_val);
			}
		}

		if( limit_num_samples ) {
			printf("num_samples -n %s (%sM)\n",
							u64toa(samples_to_xfer, &ascii_u64_data1),
							u64toa((samples_to_xfer/FREQ_ONE_MHZ), &ascii_u64_data2));
		}
	}
コード例 #7
0
ファイル: airspy_rx.c プロジェクト: steve-m/host
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;
}