Merge pull request #820 from mossmann/tools

Clean up output file handling
This commit is contained in:
Michael Ossmann
2021-01-28 14:30:53 -07:00
committed by GitHub
4 changed files with 100 additions and 94 deletions

View File

@ -96,7 +96,7 @@ int main(int argc, char** argv)
hackrf_device* device = NULL; hackrf_device* device = NULL;
int result = HACKRF_SUCCESS; int result = HACKRF_SUCCESS;
int option_index = 0; int option_index = 0;
FILE* fd = NULL; FILE* infile = NULL;
ssize_t bytes_read; ssize_t bytes_read;
uint8_t* pdata = &data[0]; uint8_t* pdata = &data[0];
@ -128,17 +128,17 @@ int main(int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
fd = fopen(path, "rb"); infile = fopen(path, "rb");
if (fd == NULL) if (infile == NULL)
{ {
fprintf(stderr, "Failed to open file: %s\n", path); fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
/* Get size of the file */ /* Get size of the file */
fseek(fd, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */ fseek(infile, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */
length = ftell(fd); length = ftell(infile);
/* Move to start */ /* Move to start */
rewind(fd); rewind(infile);
printf("File size %d bytes.\n", length); printf("File size %d bytes.\n", length);
if (length > MAX_XSVF_LENGTH) { if (length > MAX_XSVF_LENGTH) {
@ -148,13 +148,13 @@ int main(int argc, char** argv)
} }
total_length = length; total_length = length;
bytes_read = fread(data, 1, total_length, fd); bytes_read = fread(data, 1, total_length, infile);
if (bytes_read != total_length) if (bytes_read != total_length)
{ {
fprintf(stderr, "Failed to read all bytes (read %d bytes instead of %d bytes).\n", fprintf(stderr, "Failed to read all bytes (read %d bytes instead of %d bytes).\n",
(int)bytes_read, total_length); (int)bytes_read, total_length);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -179,8 +179,8 @@ int main(int argc, char** argv)
{ {
fprintf(stderr, "hackrf_cpld_write() failed: %s (%d)\n", fprintf(stderr, "hackrf_cpld_write() failed: %s (%d)\n",
hackrf_error_name(result), result); hackrf_error_name(result), result);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -193,15 +193,15 @@ int main(int argc, char** argv)
{ {
fprintf(stderr, "hackrf_close() failed: %s (%d)\n", fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
hackrf_error_name(result), result); hackrf_error_name(result), result);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
hackrf_exit(); hackrf_exit();
if (fd != NULL) { if (infile != NULL) {
fclose(fd); fclose(infile);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -168,7 +168,7 @@ int main(int argc, char** argv)
int option_index = 0; int option_index = 0;
static uint8_t data[MAX_LENGTH]; static uint8_t data[MAX_LENGTH];
uint8_t* pdata = &data[0]; uint8_t* pdata = &data[0];
FILE* fd = NULL; FILE* infile = NULL;
bool read = false; bool read = false;
bool write = false; bool write = false;
bool ignore_compat_check = false; bool ignore_compat_check = false;
@ -256,24 +256,24 @@ int main(int argc, char** argv)
if( write ) if( write )
{ {
fd = fopen(path, "rb"); infile = fopen(path, "rb");
if(fd == NULL) if(infile == NULL)
{ {
printf("Error opening file %s\n", path); printf("Error opening file %s\n", path);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
/* Get size of the file */ /* Get size of the file */
fseek(fd, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */ fseek(infile, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */
length = ftell(fd); length = ftell(infile);
/* Move to start */ /* Move to start */
rewind(fd); rewind(infile);
printf("File size %d bytes.\n", length); printf("File size %d bytes.\n", length);
} }
if (length == 0) { if (length == 0) {
fprintf(stderr, "Requested transfer of zero bytes.\n"); fprintf(stderr, "Requested transfer of zero bytes.\n");
if(fd != NULL) if(infile != NULL)
fclose(fd); fclose(infile);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -281,15 +281,15 @@ int main(int argc, char** argv)
if ((length > MAX_LENGTH) || (address > MAX_LENGTH) if ((length > MAX_LENGTH) || (address > MAX_LENGTH)
|| ((address + length) > MAX_LENGTH)) { || ((address + length) > MAX_LENGTH)) {
fprintf(stderr, "Request exceeds size of flash memory.\n"); fprintf(stderr, "Request exceeds size of flash memory.\n");
if(fd != NULL) if(infile != NULL)
fclose(fd); fclose(infile);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (read) { if (read) {
fd = fopen(path, "wb"); infile = fopen(path, "wb");
if(fd == NULL) if(infile == NULL)
{ {
printf("Error to open file %s\n", path); printf("Error to open file %s\n", path);
return EXIT_FAILURE; return EXIT_FAILURE;
@ -357,31 +357,31 @@ int main(int argc, char** argv)
if (result != HACKRF_SUCCESS) { if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_spiflash_read() failed: %s (%d)\n", fprintf(stderr, "hackrf_spiflash_read() failed: %s (%d)\n",
hackrf_error_name(result), result); hackrf_error_name(result), result);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
address += xfer_len; address += xfer_len;
pdata += xfer_len; pdata += xfer_len;
tmp_length -= xfer_len; tmp_length -= xfer_len;
} }
bytes_written = fwrite(data, 1, length, fd); bytes_written = fwrite(data, 1, length, infile);
if (bytes_written != length) { if (bytes_written != length) {
fprintf(stderr, "Failed write to file (wrote %d bytes).\n", fprintf(stderr, "Failed write to file (wrote %d bytes).\n",
(int)bytes_written); (int)bytes_written);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
if(write) { if(write) {
ssize_t bytes_read = fread(data, 1, length, fd); ssize_t bytes_read = fread(data, 1, length, infile);
if (bytes_read != length) { if (bytes_read != length) {
fprintf(stderr, "Failed read file (read %d bytes).\n", fprintf(stderr, "Failed read file (read %d bytes).\n",
(int)bytes_read); (int)bytes_read);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(!ignore_compat_check) { if(!ignore_compat_check) {
@ -389,8 +389,8 @@ int main(int argc, char** argv)
result = compatibility_check(data, length, device); result = compatibility_check(data, length, device);
if(result) { if(result) {
printf("Compatibility test failed.\n"); printf("Compatibility test failed.\n");
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
@ -399,8 +399,8 @@ int main(int argc, char** argv)
if (result != HACKRF_SUCCESS) { if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_spiflash_erase() failed: %s (%d)\n", fprintf(stderr, "hackrf_spiflash_erase() failed: %s (%d)\n",
hackrf_error_name(result), result); hackrf_error_name(result), result);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if( !verbose ) printf("Writing %d bytes at 0x%06x.\n", length, address); if( !verbose ) printf("Writing %d bytes at 0x%06x.\n", length, address);
@ -411,8 +411,8 @@ int main(int argc, char** argv)
if (result != HACKRF_SUCCESS) { if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_spiflash_write() failed: %s (%d)\n", fprintf(stderr, "hackrf_spiflash_write() failed: %s (%d)\n",
hackrf_error_name(result), result); hackrf_error_name(result), result);
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
address += xfer_len; address += xfer_len;
@ -421,9 +421,9 @@ int main(int argc, char** argv)
} }
} }
if (fd != NULL) { if (infile != NULL) {
fclose(fd); fclose(infile);
fd = NULL; infile = NULL;
} }
if(reset) { if(reset) {

View File

@ -165,7 +165,7 @@ int parse_u32_range(char* s, uint32_t* const value_min, uint32_t* const value_ma
volatile bool do_exit = false; volatile bool do_exit = false;
FILE* fd = NULL; FILE* outfile = NULL;
volatile uint32_t byte_count = 0; volatile uint32_t byte_count = 0;
volatile uint64_t sweep_count = 0; volatile uint64_t sweep_count = 0;
@ -216,7 +216,7 @@ int rx_callback(hackrf_transfer* transfer) {
char time_str[50]; char time_str[50];
struct timeval usb_transfer_time; struct timeval usb_transfer_time;
if(NULL == fd) { if(NULL == outfile) {
return -1; return -1;
} }
@ -241,8 +241,8 @@ int rx_callback(hackrf_transfer* transfer) {
for(i=0; i < ifft_bins; i++) { for(i=0; i < ifft_bins; i++) {
ifftwOut[i][0] *= 1.0f / ifft_bins; ifftwOut[i][0] *= 1.0f / ifft_bins;
ifftwOut[i][1] *= 1.0f / ifft_bins; ifftwOut[i][1] *= 1.0f / ifft_bins;
fwrite(&ifftwOut[i][0], sizeof(float), 1, fd); fwrite(&ifftwOut[i][0], sizeof(float), 1, outfile);
fwrite(&ifftwOut[i][1], sizeof(float), 1, fd); fwrite(&ifftwOut[i][1], sizeof(float), 1, outfile);
} }
} }
sweep_count++; sweep_count++;
@ -289,19 +289,19 @@ int rx_callback(hackrf_transfer* transfer) {
record_length = 2 * sizeof(band_edge) record_length = 2 * sizeof(band_edge)
+ (fftSize/4) * sizeof(float); + (fftSize/4) * sizeof(float);
fwrite(&record_length, sizeof(record_length), 1, fd); fwrite(&record_length, sizeof(record_length), 1, outfile);
band_edge = frequency; band_edge = frequency;
fwrite(&band_edge, sizeof(band_edge), 1, fd); fwrite(&band_edge, sizeof(band_edge), 1, outfile);
band_edge = frequency + DEFAULT_SAMPLE_RATE_HZ / 4; band_edge = frequency + DEFAULT_SAMPLE_RATE_HZ / 4;
fwrite(&band_edge, sizeof(band_edge), 1, fd); fwrite(&band_edge, sizeof(band_edge), 1, outfile);
fwrite(&pwr[1+(fftSize*5)/8], sizeof(float), fftSize/4, fd); fwrite(&pwr[1+(fftSize*5)/8], sizeof(float), fftSize/4, outfile);
fwrite(&record_length, sizeof(record_length), 1, fd); fwrite(&record_length, sizeof(record_length), 1, outfile);
band_edge = frequency + DEFAULT_SAMPLE_RATE_HZ / 2; band_edge = frequency + DEFAULT_SAMPLE_RATE_HZ / 2;
fwrite(&band_edge, sizeof(band_edge), 1, fd); fwrite(&band_edge, sizeof(band_edge), 1, outfile);
band_edge = frequency + (DEFAULT_SAMPLE_RATE_HZ * 3) / 4; band_edge = frequency + (DEFAULT_SAMPLE_RATE_HZ * 3) / 4;
fwrite(&band_edge, sizeof(band_edge), 1, fd); fwrite(&band_edge, sizeof(band_edge), 1, outfile);
fwrite(&pwr[1+fftSize/8], sizeof(float), fftSize/4, fd); fwrite(&pwr[1+fftSize/8], sizeof(float), fftSize/4, outfile);
} else if(ifft_output) { } else if(ifft_output) {
ifft_idx = (uint32_t) round((frequency - (uint64_t)(FREQ_ONE_MHZ*frequencies[0])) ifft_idx = (uint32_t) round((frequency - (uint64_t)(FREQ_ONE_MHZ*frequencies[0]))
/ fft_bin_width); / fft_bin_width);
@ -320,7 +320,7 @@ int rx_callback(hackrf_transfer* transfer) {
time_t time_stamp_seconds = time_stamp.tv_sec; time_t time_stamp_seconds = time_stamp.tv_sec;
fft_time = localtime(&time_stamp_seconds); fft_time = localtime(&time_stamp_seconds);
strftime(time_str, 50, "%Y-%m-%d, %H:%M:%S", fft_time); strftime(time_str, 50, "%Y-%m-%d, %H:%M:%S", fft_time);
fprintf(fd, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u", fprintf(outfile, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u",
time_str, time_str,
(long int)time_stamp.tv_usec, (long int)time_stamp.tv_usec,
(uint64_t)(frequency), (uint64_t)(frequency),
@ -328,10 +328,10 @@ int rx_callback(hackrf_transfer* transfer) {
fft_bin_width, fft_bin_width,
fftSize); fftSize);
for(i = 0; (fftSize / 4) > i; i++) { for(i = 0; (fftSize / 4) > i; i++) {
fprintf(fd, ", %.2f", pwr[i + 1 + (fftSize*5)/8]); fprintf(outfile, ", %.2f", pwr[i + 1 + (fftSize*5)/8]);
} }
fprintf(fd, "\n"); fprintf(outfile, "\n");
fprintf(fd, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u", fprintf(outfile, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u",
time_str, time_str,
(long int)time_stamp.tv_usec, (long int)time_stamp.tv_usec,
(uint64_t)(frequency+(DEFAULT_SAMPLE_RATE_HZ/2)), (uint64_t)(frequency+(DEFAULT_SAMPLE_RATE_HZ/2)),
@ -339,9 +339,9 @@ int rx_callback(hackrf_transfer* transfer) {
fft_bin_width, fft_bin_width,
fftSize); fftSize);
for(i = 0; (fftSize / 4) > i; i++) { for(i = 0; (fftSize / 4) > i; i++) {
fprintf(fd, ", %.2f", pwr[i + 1 + (fftSize/8)]); fprintf(outfile, ", %.2f", pwr[i + 1 + (fftSize/8)]);
} }
fprintf(fd, "\n"); fprintf(outfile, "\n");
} }
} }
return 0; return 0;
@ -595,17 +595,17 @@ int main(int argc, char** argv) {
} }
if((NULL == path) || (strcmp(path, "-") == 0)) { if((NULL == path) || (strcmp(path, "-") == 0)) {
fd = stdout; outfile = stdout;
} else { } else {
fd = fopen(path, "wb"); outfile = fopen(path, "wb");
} }
if(NULL == fd) { if(NULL == outfile) {
fprintf(stderr, "Failed to open file: %s\n", path); fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
/* Change fd buffer to have bigger one to store or read data on/to HDD */ /* Change outfile buffer to have bigger one to store or read data on/to HDD */
result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE); result = setvbuf(outfile , NULL , _IOFBF , FD_BUFFER_SIZE);
if( result != 0 ) { if( result != 0 ) {
fprintf(stderr, "setvbuf() failed: %d\n", result); fprintf(stderr, "setvbuf() failed: %d\n", result);
usage(); usage();
@ -757,10 +757,11 @@ int main(int argc, char** argv) {
fprintf(stderr, "hackrf_exit() done\n"); fprintf(stderr, "hackrf_exit() done\n");
} }
if ( ( fd != NULL ) && ( fd != stdout ) ) { fflush(outfile);
fclose(fd); if ( ( outfile != NULL ) && ( outfile != stdout ) ) {
fd = NULL; fclose(outfile);
fprintf(stderr, "fclose(fd) done\n"); outfile = NULL;
fprintf(stderr, "fclose() done\n");
} }
fftwf_free(fftwIn); fftwf_free(fftwIn);
fftwf_free(fftwOut); fftwf_free(fftwOut);

View File

@ -319,7 +319,7 @@ char* u64toa(uint64_t val, t_u64toa* str)
static volatile bool do_exit = false; static volatile bool do_exit = false;
FILE* fd = NULL; FILE* file = NULL;
volatile uint32_t byte_count = 0; volatile uint32_t byte_count = 0;
bool signalsource = false; bool signalsource = false;
@ -380,7 +380,7 @@ int rx_callback(hackrf_transfer* transfer) {
size_t bytes_written; size_t bytes_written;
unsigned int i; unsigned int i;
if( fd != NULL ) if( file != NULL )
{ {
byte_count += transfer->valid_length; byte_count += transfer->valid_length;
bytes_to_write = transfer->valid_length; bytes_to_write = transfer->valid_length;
@ -412,7 +412,7 @@ int rx_callback(hackrf_transfer* transfer) {
#endif #endif
return 0; return 0;
} else { } else {
bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd); bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, file);
if ((bytes_written != bytes_to_write) if ((bytes_written != bytes_to_write)
|| (limit_num_samples && (bytes_to_xfer == 0))) { || (limit_num_samples && (bytes_to_xfer == 0))) {
return -1; return -1;
@ -430,7 +430,7 @@ int tx_callback(hackrf_transfer* transfer) {
size_t bytes_read; size_t bytes_read;
unsigned int i; unsigned int i;
if( fd != NULL ) if( file != NULL )
{ {
byte_count += transfer->valid_length; byte_count += transfer->valid_length;
bytes_to_read = transfer->valid_length; bytes_to_read = transfer->valid_length;
@ -444,15 +444,15 @@ int tx_callback(hackrf_transfer* transfer) {
} }
bytes_to_xfer -= bytes_to_read; bytes_to_xfer -= bytes_to_read;
} }
bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd); bytes_read = fread(transfer->buffer, 1, bytes_to_read, file);
if (limit_num_samples && (bytes_to_xfer == 0)) { if (limit_num_samples && (bytes_to_xfer == 0)) {
return -1; return -1;
} }
if (bytes_read != bytes_to_read) { if (bytes_read != bytes_to_read) {
if (repeat) { if (repeat) {
fprintf(stderr, "Input file end reached. Rewind to beginning.\n"); fprintf(stderr, "Input file end reached. Rewind to beginning.\n");
rewind(fd); rewind(file);
fread(transfer->buffer + bytes_read, 1, bytes_to_read - bytes_read, fd); fread(transfer->buffer + bytes_read, 1, bytes_to_read - bytes_read, file);
return 0; return 0;
} else { } else {
return -1; /* not repeat mode, end of file */ return -1; /* not repeat mode, end of file */
@ -892,24 +892,24 @@ int main(int argc, char** argv) {
if( transceiver_mode == TRANSCEIVER_MODE_RX ) if( transceiver_mode == TRANSCEIVER_MODE_RX )
{ {
if (strcmp(path, "-") == 0) { if (strcmp(path, "-") == 0) {
fd = stdout; file = stdout;
} else { } else {
fd = fopen(path, "wb"); file = fopen(path, "wb");
} }
} else { } else {
if (strcmp(path, "-") == 0) { if (strcmp(path, "-") == 0) {
fd = stdin; file = stdin;
} else { } else {
fd = fopen(path, "rb"); file = fopen(path, "rb");
} }
} }
if( fd == NULL ) { if( file == NULL ) {
fprintf(stderr, "Failed to open file: %s\n", path); fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
/* Change fd buffer to have bigger one to store or read data on/to HDD */ /* Change file buffer to have bigger one to store or read data on/to HDD */
result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE); result = setvbuf(file , NULL , _IOFBF , FD_BUFFER_SIZE);
if( result != 0 ) { if( result != 0 ) {
fprintf(stderr, "setvbuf() failed: %d\n", result); fprintf(stderr, "setvbuf() failed: %d\n", result);
usage(); usage();
@ -920,7 +920,7 @@ int main(int argc, char** argv) {
/* Write Wav header */ /* Write Wav header */
if( receive_wav ) if( receive_wav )
{ {
fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd); fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), file);
} }
#ifdef _MSC_VER #ifdef _MSC_VER
@ -1045,7 +1045,7 @@ int main(int argc, char** argv) {
len=_st-stream_head; len=_st-stream_head;
else else
len=stream_size-stream_head; len=stream_size-stream_head;
bytes_written = fwrite(stream_buf+stream_head, 1, len, fd); bytes_written = fwrite(stream_buf+stream_head, 1, len, file);
if (len != bytes_written) { if (len != bytes_written) {
fprintf(stderr, "write failed"); fprintf(stderr, "write failed");
do_exit=true; do_exit=true;
@ -1126,24 +1126,29 @@ int main(int argc, char** argv) {
fprintf(stderr, "hackrf_exit() done\n"); fprintf(stderr, "hackrf_exit() done\n");
} }
if(fd != NULL) if(file != NULL)
{ {
if( receive_wav ) if( receive_wav )
{ {
/* Get size of file */ /* Get size of file */
file_pos = ftell(fd); file_pos = ftell(file);
/* Update Wav Header */ /* Update Wav Header */
wave_file_hdr.hdr.size = file_pos-8; wave_file_hdr.hdr.size = file_pos-8;
wave_file_hdr.fmt_chunk.dwSamplesPerSec = sample_rate_hz; 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.fmt_chunk.dwAvgBytesPerSec = wave_file_hdr.fmt_chunk.dwSamplesPerSec*2;
wave_file_hdr.data_chunk.chunkSize = file_pos - sizeof(t_wav_file_hdr); wave_file_hdr.data_chunk.chunkSize = file_pos - sizeof(t_wav_file_hdr);
/* Overwrite header with updated data */ /* Overwrite header with updated data */
rewind(fd); rewind(file);
fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd); fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), file);
} }
fclose(fd); if (file != stdin) {
fd = NULL; fflush(file);
fprintf(stderr, "fclose(fd) done\n"); }
if ((file != stdout) && (file != stdin)) {
fclose(file);
file = NULL;
fprintf(stderr, "fclose() done\n");
}
} }
fprintf(stderr, "exit\n"); fprintf(stderr, "exit\n");
return exit_code; return exit_code;